Section 5.27

CSPRNG

Full name: Cryptographically secure pseudorandom number generator.

Definition

A CSPRNG (cryptographically secure pseudorandom number generator) is an algorithm §14.1 that expands a short secret starting value, the seed, into as many output bytes as requested, such that without the seed no feasible computation can predict the next output bit or tell the output apart from truly random bytes. It is deterministic: the same seed gives the same output, so its security rests on a seed that is secret and hard to guess and on a secret internal state. NIST calls such an algorithm a deterministic random bit generator (DRBG).

The idea

A device needs unpredictable bytes all the time: keys, nonces §5.18 (values that must have at most a negligible chance of repeating) and challenges that a peer must answer. All of them must start from physical randomness. NIST Special Publication (SP) 800-90A describes an entropy §5.26 source as a noise source, such as thermal noise, together with health tests and an optional conditioning step; its raw output can be slow to collect and need not be uniformly random [305]. A CSPRNG collects enough of that randomness once, as the seed, and stretches it into a long output stream at the speed of ordinary computation.

A fair picture is a sealed machine with a secret setting: anyone may watch the output, but no amount of it reveals the setting or the next output. The picture fails in one respect. The setting fixes the whole output, so anyone who learns or copies the setting recomputes every byte, and a machine started twice from one setting produces the same stream twice.

An ordinary pseudorandom number generator aims only to look statistically random. The Python §12.23 documentation calls its standard generator, the Mersenne Twister, one of the most extensively tested random number generators in existence and completely unsuitable for cryptographic purposes. Passing statistical tests does not show that the next output is unpredictable.

How it works

NIST SP 800-90A specifies approved designs. The steps below follow its design HMAC_DRBG, which is built from HMAC §5.8 (hash-based message authentication code), a function that computes a fixed-length value from a key and a message. Write HMAC(k,m)\mathrm{HMAC}(k, m) for HMAC with key kk and message mm, and a‖ba \mathbin{\Vert} b for the byte string aa followed by the byte string bb.

  1. Obtain entropy input. Let ss be the security strength, meaning that breaking the generator should take roughly 2s2^s operations. The entropy input must contain at least ss bits of min-entropy, a measure of how hard a value is to guess in one try. If the entropy source cannot supply it, the request returns an error and the generator produces nothing.

  2. Instantiate. The seed is the entropy input, then a nonce, then an optional personalization string, a value such as a device serial number that makes this instance differ from all others. The nonce must have at least s/2s/2 bits of entropy, or repeat no more often than an s/2s/2-bit random value would. The internal state is a secret key KK and a secret value VV, each as long as the hash output (32 bytes with SHA-256 §5.2, the Secure Hash Algorithm with a 256-bit output), plus a counter of requests. The state starts as KK = 32 bytes of 0x00 and VV = 32 bytes of 0x01, the seed is mixed in with the update function, and the counter is set to 1.

  3. Update. With input data dd, the update function computes K←HMAC(K,V‖𝟶𝚡𝟶𝟶‖d)K \leftarrow \mathrm{HMAC}(K, V \mathbin{\Vert} \texttt{0x00} \mathbin{\Vert} d), then V←HMAC(K,V)V \leftarrow \mathrm{HMAC}(K, V). If dd is not empty, both lines run again with 0x01 in place of 0x00.

  4. Generate. The generator repeats V←HMAC(K,V)V \leftarrow \mathrm{HMAC}(K, V), joining the successive values of VV until it has enough bytes, returns the requested number of bytes, runs the update function with no data and adds 1 to the counter. The final update replaces KK and VV, so someone who reads the state later cannot recompute the outputs already returned.

  5. Reseed. The generator mixes fresh entropy input into its state with the update function and resets the counter. SP 800-90A requires a reseed after at most 2482^{48} requests for HMAC_DRBG, and describes reseeding as the means of restoring the secrecy of the output after the seed or the internal state becomes known.

Worked example

A seed that is too small. The numbers in this example are invented. A device seeds its generator with a millisecond timer read during start-up, and that code always runs between 100 ms and 1,123 ms after power-on. The seed is therefore one of 1,123−100+1=1,0241{,}123 - 100 + 1 = 1{,}024 values and has at most log21024=10\log_2 1024 = 10 bits of min-entropy. A 256-bit key drawn from the generator is then one of at most 1,024 keys. An attacker who knows the firmware runs the generator from each of the 1,024 seeds and tests each resulting key, for example against one message the device encrypted. A seed with 256 bits of min-entropy would leave the attacker 22562^{256} candidates instead.

One request to HMAC_DRBG. An instance that uses SHA-256 is asked for 40 bytes. Each HMAC call gives 32 bytes, so it needs ⌈40/32⌉=2\lceil 40 / 32 \rceil = 2 calls, where ⌈x⌉\lceil x \rceil means xx rounded up to a whole number. The two calls produce 2×32=642 \times 32 = 64 bytes; the generator returns the first 40 and discards the other 64−40=2464 - 40 = 24. The final update, with no input data, costs 2 more HMAC calls, so the request costs 2+2=42 + 2 = 4 HMAC calls, and the counter goes from 1 to 2. Two instances started from the same seed return the same 40 bytes.

Stated precisely

Pass and shelat, A Course in Cryptography (2010), Definition 77.1, gives the textbook definition. A function GG is a pseudorandom generator if it runs in time polynomial in the length of its input, its output is longer than its input, and, for a seed chosen uniformly at random, no efficient algorithm can tell GG’s output apart from a uniformly random string of the same length. Efficient means running in time polynomial in the seed length. Telling apart means that the algorithm’s probability of answering “generator” differs between the two cases by more than a negligible amount, one that shrinks faster than one over any polynomial in the seed length. By Theorem 75.4 of the same text, it suffices that no efficient algorithm, shown any first part of the output, predicts the next bit with probability better than 1/21/2 plus a negligible amount; this is the next-bit test.

A real generator has a fixed seed length, so NIST states the guarantee as a security strength ss: telling the output apart from random should take roughly 2s2^s operations. SP 800-90A allows ss to be 112, 128, 192 or 256. The guarantee rests on three assumptions.

  1. The entropy input has at least ss bits of min-entropy from the attacker’s point of view.

  2. The internal state, KK and VV for HMAC_DRBG, stays secret.

  3. The hash function inside HMAC meets or exceeds the security strength ss, as SP 800-90A requires.

Two properties concern an attacker who learns the internal state. Fix a time TT. Backtracking resistance relative to TT: an attacker who learns the state at some time after TT still cannot tell the outputs produced up to TT apart from random. SP 800-90A states that all its designs provide it. Prediction resistance relative to TT: an attacker who learned the state at some time before TT cannot tell the outputs produced from TT onwards apart from random. SP 800-90A states that this is possible only by reseeding with fresh entropy, at least ss bits, between requests.

One bound holds for every design: the number of different output streams is at most the number of different combinations of seed and other secret inputs.

Limits and common mistakes

A CSPRNG creates no entropy. A seed with 10 bits of min-entropy gives keys with at most 10 bits, however long the keys are.

Copying the state copies the future output. If a running program is duplicated, or a whole system is restored from a saved copy of its memory, both copies continue from the same internal state and return the same bytes, for example the same nonce twice. SP 800-90A forbids using the internal state of one instance as the internal state of another, and SP 800-38D, the NIST standard for the encryption mode GCM (Galois/Counter Mode §15.5) [319], requires that the generators which produce its per-message values in two distinct devices be independent, for example through distinct seeds. After any copy, each copy must mix in fresh entropy before producing output again.

A fixed seed that makes tests reproducible makes every device that uses it produce the same stream. It must never reach a device in service.

If the internal state leaks and no fresh entropy is mixed in, the attacker computes every later output. Backtracking resistance protects only outputs produced before the leak.

Early in start-up, a generator may not yet be seeded. The Linux manual page for random states that reads from /dev/urandom during early boot may return data before the entropy pool is initialized, while getrandom() waits until it is initialized.

Questions answered

What source seeds the generator before enough environmental entropy exists?

Before environmental entropy has accumulated (the unpredictability an operating system gathers from hardware events, such as device driver §6.38 activity), the generator must be seeded from a source that exists from power-on and that the attacker cannot predict: a hardware entropy source inside the chip that has passed its start-up tests, or a secret seed stored at manufacture in protected nonvolatile memory, preferably both mixed together. If neither is available, the generator must release no output: it waits, or it reports an error that the caller handles by waiting or refusing the operation.

Never fall back silently to a constant, a timestamp, a serial number or a boot counter. An attacker can predict each of these, so every output, and every key made from it, would be predictable too.

Related entries: Entropy and min-entropy (§5.26), Nonce (§5.18), Post-compromise security (§5.34), HMAC (§5.8), Pseudorandom function (PRF) (§15.1), Root key and root seed (§5.25). Further reading: [66, 148, 305, 319].