



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




“Anyone who uses software to produce random numbers is in a state of sin.” —John von Neumann
“The generation of random numbers is too important to be left to chance.” —Robert R. Coveyou
Many cryptographic protocols require the parties to generate random numbers. For instance, cryptographic keys have to be generated in a way that makes them unpredictable to anyone other than the authorized creator of the key. How should we generate these random numbers?
In this lecture, you will learn two things: how to generate truly random bits; and cryptographic methods to stretch a little bit of true randomness into a large stream of pseudorandom values that are indistinguishable from true random bits.
It turns out that random number generation is very easy to get wrong. For instance, what is wrong with the following code?
unsigned char key[16];
srand(time(NULL)); for (i=0; i<16; i++) key[i] = rand() & 0xFF;
There all sorts of problems with this code. Can you spot any of them?
In case you are not familiar with the rand() function, here is a quick refresher. Here are their function prototypes:
int rand(void); void srand(unsigned int seed); time_t time(time_t *t);
Each call to rand() returns a pseudorandom value in the range 0 to RAND MAX, calculated as a deter- ministic function of the seed. Also, srand(s) sets the seed to s. For instance, here is one possible implementation of rand() and srand():
static unsigned int next = 0; void srand(unsigned int seed) { next = seed; }
/* RAND_MAX assumed to be 32767 */ int rand(void) { next = next * 1103515245 + 12345; return next % 32768; }
Finally, time(NULL) returns the current time, as measured by the number of seconds since Jan 1, 1970.
With that background, here are two of the security holes in the code snippet listed previously:
some partial information about a few of the outputs from this pseudorandom number generator, and infer the seed used to shuffle the deck. This lets a smart player infer what cards everyone else holds, which obviously allows to rake in the cash at the poker table. Oops. Fortunately, the folks who discovered the flaw notified the web site and wrote a paper rather than exploiting it to cheat others.
The lesson to take away is that these flaws are rampant, and their consequences can be severe.
These flaws suggest that there are two essential principles for secure random number generation:
We will see next how these principles may be met.
We can distinguish two different kinds of random number generation:
As you might have guessed by now, in most systems the random number generation process is structured as follow:
Then, any place in the system that would normally require a random value can instead use pseudorandom bits computed by the CS-PRNG. The cryptographic properties of the CS-PRNG ensure that using pseudorandom bits instead of true-random bits makes no (detectable) difference.
The rest of this lecture is broken down into two parts: (1) how to build a CS-PRNG; (2) how to build a TRNG.
The function of a CS-PRNG might seem magical. Given only 128 bits of randomness, a CS-PRNG is sup- posed to somehow amplify it into billions of random bits. How on earth can that be possible? Surprisingly, it turns out that this is not only possible, but it is quite easy, once you know a little cryptography.
One very simple idea is to think of the seed as a cryptographic key, pick some symmetric-key encryption scheme, encrypt some fixed message under this key, and use the ciphertext as our pseudorandom bits. For many encryption schemes, the ciphertext is indistinguishable from true-random bits. For instance, we might generate n pseudorandom bits from a 128-bit seed k as AES-CBC( k , 0 n ), where 0 n^ denotes the all-zeros message that is n bits long^1. It can be proven that this is a secure CS-PRNG, assuming that AES is a secure block cipher. This CS-PRNG is also very efficient, because it needs only one call to AES per 128 bits of output generated. Standard AES implementations can generate output at rates in excess of 50 MB/sec on a modern desktop machine, so this is extremely efficient.
We can formalize the requirement for what it means for a CS-PRNG to be secure. Suppose we have a CS- PRNG that expands a 128-bit seed to one million bits of output. This CS-PRNG is a deterministic function G : { 0 , 1 }^128 → { 0 , 1 }^1 ,^000 ,^000 that maps the seed s to the output G ( s ). Let K denote a random variable that is distributed uniformly at random on { 0 , 1 }^128 , and let U denote a random variable distributed uniformly on { 0 , 1 }^1 ,^000 ,^000. Roughly speaking, we say that G is secure if the output G ( K ) is indistinguishable from U. In other words, no attacker A who is given a sequence of one million bits can tell whether this sequence was contains pseudorandom bits computed by applying G to a random 128-bit seed, or contains true-random bits generated by one million coin flips. More precisely, an attacker A is an algorithm that takes a sequence of one million bits as input, and outputs a guess as to how those bits were computed. The advantage of the attacker A at breaking G is given by
Adv A = | Pr[ A ( G ( K )) = “pseudorandom”] − Pr[ A ( U ) = “pseudorandom”].
The function G is a secure CS-PRNG if there is no feasible attacker A that has a non-negligible advantage at breaking G.
There is essentially nothing more to say about CS-PRNGs. They are easy to build, and any good crypto- graphic library will provide an implementation of a CS-PRNG.
Building a TRNG is more challenging. On some platforms, it is easy to build a TRNG, because the CPU provides built-in hardware capability to generate truly random bits by using an appropriate random physical process. However, unfortunately most platforms do not have this capability.
Another possibility is to buy a hardware peripheral that acts as a dedicated TRNG, against using truly random physical processes. These can be obtained for only a few hundred dollars, so for high-value servers, this might be a reasonable solution.
However, for many systems—including desktop machines—this is not viable. We have to make do with what we’ve got. How do we build a TRNG when there is no perfect source of physically random bits?
(^1) We can use any fixed IV, such as the all-zeros IV.
with SHA1; and (if necessary) you truncate to an appropriate length. The result is a short true-random value, which can be used as a seed for a CS-PRNG.
It is worth pointing out that some sources that might seem attractive are actually useless for cryptography. Some examples:
In general, we want values that will be random and unpredictable even to an adversary. Also, because the cost of a RNG failure is so high, we are usually very conservative when we analyze potential randomness sources, and we evaluate them according to the assumptions that are most favorable to the attacker (among all scenarios that are remotely plausible).
Many programming environments provide utility functions that implement all of this for you, so you don’t have to. Many Unix platforms provide a special device /dev/urandom; reading from it provides pseu- dorandom bytes. On Microsoft platforms, you can use CryptGenRandom(). In Java, you can use java.security.SecureRandom. All of these generate pseudorandom bytes by applying a CS-PRNG to a short seed collected with a TRNG. Many cryptographic libraries also provide APIs with this function- ality: see, e.g., RAND bytes() in the open-source OpenSSL library.
Avoid rand(), random(), drand48(), java.util.Random, Mersenne twister, LSFRs, LCRNGs, and any other PRNG that hasn’t been verified to be cryptographically secure.