Docsity

Computer Security Random Number Generation - Essay - Computer Science, Essays (high school) of Software Engineering

Toggle sidebar
bg1
CS 161 Computer Security
Fall 2005 Joseph/Tygar/Vazirani/Wagner Notes 21
“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
Random Number Generation
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.
1 What Can Go Wrong
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 0to 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():
CS 161, Fall 2005, Notes 21 1
pf3
pf4
pf5

Partial preview of the text

Download Computer Security Random Number Generation - Essay - Computer Science and more Essays (high school) Software Engineering in PDF only on Docsity!

CS 161 Computer Security

Fall 2005 Joseph/Tygar/Vazirani/Wagner Notes 21

“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

Random Number Generation

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.

1 What Can Go Wrong

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:

  • It is easy to guess the value of the key. The seed is highly predictable, and anyone who can guess the seed can calculate the value of the cryptographic key. The algorithm that rand() uses for computing its outputs as a function of the seed is publicly known. Consequently, anyone who guess the time at which the key was generated can apply the very same algorithm to infer each of the bytes of the key. For instance, if Alice generates a new session key at the start of each session using this code, then anyone who eavesdrops on a session will probably be able to determine the time of day on Alice’s machine at the start of the session (and hence the seed passed to srand()) and then decrypt everything that is encrypted using this session key. Even if the eavesdropper doesn’t know at what time the key was generated, there just aren’t that many possibilities. For instance, suppose we know the key was generated this year. There are 3600 × 24 × 365 = 31 , 536 , 000 ≈ 225 seconds in a year. This means that, if I can narrow down when the key was generated to within a one-year window, then there are only 2^25 possible values for the seed passed to srand(). Of course, it is not hard to try each one of them, and see which key would result in each case. Also, once you have found the right key, it is easy to recognize this fact: you can simply try decrypting the ciphertext with each potential key, and see which one provides plausible- looking cleartext (the wrong keys will cause the decryption to look like gibberish; checksums and message authentication codes will be invalid; and so on). Since we have narrowed down the key to only 2^25 possibilities, the system essentially has 25 bits of strength. Modern machines can try all 2^25 possibilities in seconds or minutes, which demonstrates that cracking this system is utter triviality.
  • The output of this generator isn’t very random. If we use the sample generator listed above, then the sequence of 16 key bytes will be highly non-random. For instance, it turns out that the low bit of each successive output of rand() will alternate (e.g., 0, 1 , 0 , 1 , 0 , 1 ,.. .). Do you see why? The low bit of x * 1103515245 is the same as the low bit of x, and then adding 12345 just flips the low bit. Thus the low bit alternates. This narrows down the set of possible keys to only 2^113 possibilities; much less than the desired value of 2^128. Actually, it’s even worse than that: each output from rand() depends only on the last output from rand(). Do you see why? If we let N 0 , N 1 , N 2 ,... denote the sequence of values of next during successive calls to rand(), then on a 32-bit machine we have Ni + 1 = 1103515245 × Ni + 12345 mod
    1. Moreover if we denote the output from the i th call to rand() by Xi , we have Xi = Ni mod 2^15. From this it follows that Xi + 1 ≡ 1103515245 × Xi + 12345 (mod 2^15 ). In other words, each output from rand() depends only on the previous output from rand(). If I can guess the first output from rand(), then I will be able to derive all the subsequent outputs from rand(). This means that there

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:

  • Seeds must be unpredictable. There should be sufficiently many possibilities that they cannot be enumerated (2^128 is enough). All possibilities should be equally likely, and nothing the attacker knows should help him to predict the value of the seed. Ideally, the seed would be truly random.
  • The algorithm for generating pseudorandom bits must be secure. There should be no discernable patterns in the pseudorandom bits produced by this algorithm. Even if an attacker manages to learn (or guess) some of the pseudorandom bits, this should not help him predict the value of any other pseudorandom bits that he does not already know.

We will see next how these principles may be met.

2 Generating Pseudorandom Numbers: Outline

We can distinguish two different kinds of random number generation:

  • True random number generator (TRNG). A TRNG generates bits that are distributed uniformly at random, so that all outputs are equally likely, and with no patterns, correlations, etc.
  • Cryptographically secure pseudorandom number generator (CS-PRNG). A CS-PRNG generates a sequence of bits that appear, as far as anyone can tell, to be indistinguishable from true random bits. Typically, there is some short true-random seed, which is then stretched in a deterministic way to a long output in a way that ensures that the output is computationally indistinguishable from true- random. CS-PRNGs use cryptographic techniques to achieve this task.

As you might have guessed by now, in most systems the random number generation process is structured as follow:

  1. Generate a seed. We typically use a TRNG to generate a short seed that is truly random. The seed only needs to be long enough to prevent someone from guessing it. For instance, the seed might be 128 bits. The seed plays a role similar to that of a cryptographic key. Using a TRNG ensures that the seed will be unpredictable by any attacker.
  2. Generate pseudorandom output, using this seed. We then use a CS-PRNG to stretch the seed to a long pseudorandom output. Modern cryptographic CS-PRNGs allow to generate an essentially unlimited amount of output (billions of bits are no problem). Using a CS-PRNG ensures that the pseudorandom bits thus generated have no discernable patterns.

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.

3 Cryptographically Secure Pseudorandom Number Genera-

tion

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.

4 True Random Number Generation

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.

4.1 Caution

It is worth pointing out that some sources that might seem attractive are actually useless for cryptography. Some examples:

  • IP addresses. Useless, since we’d better assume that an adversary will know our IP address and thus be able to predict this value.
  • Contents of network packets. We can’t count on this to have any predictability, since an eavesdropper can see all of these values. Network packet timings may also be predictable, too, depending on the precision of our timer.
  • Process IDs. The process IDs for many servers and daemons are easily predictable, since the boot process is deterministic and thus anything started at boot time is likely to received the same pid each time the machine is booted. We’d better assume that the adversary knows what operating system we are running, so for many processes the pid doesn’t contribute any useful randomness.

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).

5 Practical Considerations

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.