For example, on some versions of Linux, the generator consists of a
random pool of 512 bytes represented as 128 words of 4 bytes each.
When an event occurs, such as a disk drive interrupt, the time of the
event is XOR’ed into the pool, and the pool is stirred via a
primitive polynomial of degree 128. The pool itself is treated as a
ring buffer, with new data being XOR’ed (after stirring with the
polynomial) across the entire pool.
Each call that adds entropy to the pool estimates the amount of
likely true entropy the input contains. The pool itself contains a
accumulator that estimates the total over all entropy of the pool.
Input events come from several sources, as listed below.
Unfortunately, for server machines without human operators, the first
and third are not available, and entropy may be added slowly in that
case.
1. Keyboard interrupts. The time of the interrupt and the scan code
are added to the pool. This in effect adds entropy from the human
operator by measuring inter-keystroke arrival times.
2. Disk completion and other interrupts. A system being used by a
person will likely have a hard-to-predict pattern of disk
accesses. (But not all disk drivers support capturing this timing
information with sufficient accuracy to be useful.)
3. Mouse motion. The timing and mouse position are added in.
When random bytes are required, the pool is hashed with SHA-1 [SHA*]
to yield the returned bytes of randomness. If more bytes are
required than the output of SHA-1 (20 bytes), then the hashed output
is stirred back into the pool and a new hash is performed to obtain
the next 20 bytes. As bytes are removed from the pool, the estimate
of entropy is correspondingly decremented.
To ensure a reasonably random pool upon system startup, the standard
startup and shutdown scripts save the pool to a disk file at shutdown
and read this file at system startup.
There are two user-exported interfaces. /dev/random returns bytes
from the pool but blocks when the estimated entropy drops to zero.
As entropy is added to the pool from events, more data becomes
available via /dev/random. Random data obtained from such a
/dev/random device is suitable for key generation for long term keys,
if enough random bits are in the pool or are added in a reasonable
amount of time.
/dev/urandom works like /dev/random; however, it provides data even
when the entropy estimate for the random pool drops to zero. This
may be adequate for session keys or for other key generation tasks
for which blocking to await more random bits is not acceptable. The
risk of continuing to take data even when the pool’s entropy estimate
is small in that past output may be computable from current output,
provided that an attacker can reverse SHA-1. Given that SHA-1 is
designed to be non-invertible, this is a reasonable risk.
To obtain random numbers under Linux, Solaris, or other UNIX systems
equipped with code as described above, all an application has to do
is open either /dev/random or /dev/urandom and read the desired
number of bytes.
(The Linux Random device was written by Theodore Ts’o. It was based
loosely on the random number generator in PGP 2.X and PGP 3.0 (aka
PGP 5.0).)
7.1.3. Windows CryptGenRandom
Microsoft’s recommendation to users of the widely deployed Windows
operating system is generally to use the CryptGenRandom pseudo-random
number generation call with the CryptAPI cryptographic service
provider. This takes a handle to a cryptographic service provider
library, a pointer to a buffer by which the caller can provide
entropy and into which the generated pseudo-randomness is returned,
and an indication of how many octets of randomness are desired.
The Windows CryptAPI cryptographic service provider stores a seed
state variable with every user. When CryptGenRandom is called, this
is combined with any randomness provided in the call and with various
system and user data such as the process ID, thread ID, system clock,
system time, system counter, memory status, free disk clusters, and
hashed user environment block. This data is all fed to SHA-1, and
the output is used to seed an RC4 key stream. That key stream is
used to produce the pseudo-random data requested and to update the
user’s seed state variable.
Users of Windows ".NET" will probably find it easier to use the
RNGCryptoServiceProvider.GetBytes method interface.
For further information, see [WSC].
7.2. Generators Assuming a Source of Entropy
The pseudo-random number generators described in the following three
sections all assume that a seed value with sufficient entropy is
provided to them. They then generate a strong sequence (see Section
6.2) from that seed.
7.2.1. X9.82 Pseudo-Random Number Generation
The ANSI X9F1 committee is in the final stages of creating a standard
for random number generation covering both true randomness generators
and pseudo-random number generators. It includes a number of
pseudo-random number generators based on hash functions, one of which
will probably be based on HMAC SHA hash constructs [RFC2104]. The
draft version of this generator is described below, omitting a number
of optional features [X9.82].
In the subsections below, the HMAC hash construct is simply referred
to as HMAC but, of course, a particular standard SHA function must be
selected in an particular use. Generally speaking, if the strength
of the pseudo-random values to be generated is to be N bits, the SHA
function chosen must generate N or more bits of output, and a source
of at least N bits of input entropy will be required. The same hash
function must be used throughout an instantiation of this generator.
7.2.1.1. Notation
In the following sections, the notation give below is used:
hash_length is the output size of the underlying hash function in
use.
input_entropy is the input bit string that provides entropy to the
generator.
K is a bit string of size hash_length that is part of the state of
the generator and is updated at least once each time random
bits are generated.
V is a bit string of size hash_length and is part of the state of
the generator. It is updated each time hash_length bits of
output are generated.
"|" represents concatenation.
7.2.1.2. Initializing the Generator
Set V to all zero bytes, except the low-order bit of each byte is set
to one.
Set K to all zero bytes, then set:
K = HMAC ( K, V | 0x00 | input_entropy )
V = HMAC ( K, V )
K = HMAC ( K, V | 0x01 | input_entropy )
V = HMAC ( K, V )
Note: All SHA algorithms produce an integral number of bytes, so the
lengths of K and V will be integral numbers of bytes.
7.2.1.3. Generating Random Bits
When output is called for, simply set:
V = HMAC ( K, V )
and use the leading bits from V. If more bits are needed than the
length of V, set "temp" to a null bit string and then repeatedly
perform:
V = HMAC ( K, V )
temp = temp | V
stopping as soon as temp is equal to or longer than the number of
random bits requested. Use the requested number of leading bits from
temp. The definition of the algorithm prohibits requesting more than
2^35 bits.
After extracting and saving the pseudo-random output bits as
described above, before returning you must also perform two more
HMACs as follows:
K = HMAC ( K, V | 0x00 )
V = HMAC ( K, V )
7.2.2. X9.17 Key Generation
The American National Standards Institute has specified the
following method for generating a sequence of keys [X9.17]:
s is the initial 64 bit seed.
0
g is the sequence of generated 64-bit key quantities
n
k is a random key reserved for generating this key sequence.
t is the time at which a key is generated, to as fine a resolution
as is available (up to 64 bits).
DES ( K, Q ) is the DES encryption of quantity Q with key K.
Then:
g = DES ( k, DES ( k, t ) XOR s )
n n
s = DES ( k, DES ( k, t ) XOR g )
n+1 n
If g sub n is to be used as a DES key, then every eighth bit should
be adjusted for parity for that use, but the entire 64 bit unmodified
g should be used in calculating the next s.
7.2.3. DSS Pseudo-random Number Generation
Appendix 3 of the NIST Digital Signature Standard [DSS] provides a
method of producing a sequence of pseudo-random 160 bit quantities
for use as private keys or the like. This has been modified by
Change Notice 1 [DSS_CN1] to produce the following algorithm for
generating general-purpose pseudo-random numbers:
t = 0x 67452301 EFCDAB89 98BADCFE 10325476 C3D2E1F0
XKEY = initial seed
0
For j = 0 to ...
XVAL = ( XKEY + optional user input ) (Mod 2^512)
j
X = G( t, XVAL )
j
XKEY = ( 1 + XKEY + X ) (Mod 2^512)
j+1 j j
The quantities X thus produced are the pseudo-random sequence of
160-bit values. Two functions can be used for "G" above. Each
produces a 160-bit value and takes two arguments, a 160-bit value and
a 512 bit value.
The first is based on SHA-1 and works by setting the 5 linking
variables, denoted H with subscripts in the SHA-1 specification, to
the first argument divided into fifths. Then steps (a) through (e)
of section 7 of the NIST SHA-1 specification are run over the second
argument as if it were a 512-bit data block. The values of the
linking variable after those steps are then concatenated to produce
the output of G [SHA*].
As an alternative method, NIST also defined an alternate G function
based on multiple applications of the DES encryption function [DSS].
8. Examples of Randomness Required
Below are two examples showing rough calculations of randomness
needed for security. The first is for moderate security passwords,
while the second assumes a need for a very high-security
cryptographic key.
In addition, [ORMAN] and [RSA_BULL13] provide information on the
public key lengths that should be used for exchanging symmetric keys.
8.1. Password Generation
Assume that user passwords change once a year and that it is desired
that the probability that an adversary could guess the password for a
particular account be less than one in a thousand. Further assume
that sending a password to the system is the only way to try a
password. Then the crucial question is how often an adversary can
try possibilities. Assume that delays have been introduced into a
system so that an adversary can make at most one password try every
six seconds. That’s 600 per hour, or about 15,000 per day, or about
5,000,000 tries in a year. Assuming any sort of monitoring, it is
unlikely that someone could actually try continuously for a year.
Even if log files are only checked monthly, 500,000 tries is more
plausible before the attack is noticed and steps are taken to change
passwords and make it harder to try more passwords.
To have a one-in-a-thousand chance of guessing the password in
500,000 tries implies a universe of at least 500,000,000 passwords,
or about 2^29. Thus, 29 bits of randomness are needed. This can
probably be achieved by using the US DoD-recommended inputs for
password generation, as it has 8 inputs that probably average over 5
bits of randomness each (see section 7.1). Using a list of 1,000
words, the password could be expressed as a three-word phrase
(1,000,000,000 possibilities). By using case-insensitive letters and
digits, six characters would suffice ((26+10)^6 = 2,176,782,336
possibilities).
For a higher-security password, the number of bits required goes up.
To decrease the probability by 1,000 requires increasing the universe
of passwords by the same factor, which adds about 10 bits. Thus, to
have only a one in a million chance of a password being guessed under
the above scenario would require 39 bits of randomness and a password
that was a four-word phrase from a 1,000 word list, or eight
letters/digits. To go to a one-in-10^9 chance, 49 bits of randomness
are needed, implying a five-word phrase or a ten-letter/digit
password.
In a real system, of course, there are other factors. For example,
the larger and harder to remember passwords are, the more likely
users will bed to write them down, resulting in an additional risk of
compromise.
8.2. A Very High Security Cryptographic Key
Assume that a very high security key is needed for symmetric
encryption/decryption between two parties. Assume also that an
adversary can observe communications and knows the algorithm being
used. Within the field of random possibilities, the adversary can
try key values in hopes of finding the one in use. Assume further
that brute force trial of keys is the best the adversary can do.
8.2.1. Effort per Key Trial
How much effort will it take to try each key? For very high-security
applications, it is best to assume a low value of effort. Even if it
would clearly take tens of thousands of computer cycles or more to
try a single key, there may be some pattern that enables huge blocks
of key values to be tested with much less effort per key. Thus, it
is probably best to assume no more than a couple of hundred cycles
per key. (There is no clear lower bound on this, as computers
operate in parallel on a number of bits and a poor encryption
algorithm could allow many keys or even groups of keys to be tested
in parallel. However, we need to assume some value and can hope that
a reasonably strong algorithm has been chosen for our hypothetical
high-security task.)
If the adversary can command a highly parallel processor or a large
network of work stations, 10^11 cycles per second is probably a
minimum assumption today. Looking forward a few years, there should
be at least an order of magnitude improvement. Thus, it is
reasonable to assume that 10^10 keys could be checked per second, or
3.6*10^12 per hour or 6*10^14 per week, or 2.4*10^15 per month. This
implies a need for a minimum of 63 bits of randomness in keys, to be
sure that they cannot be found in a month. Even then it is possible
that, a few years from now, a highly determined and resourceful
adversary could break the key in 2 weeks; on average, they need try
only half the keys.
These questions are considered in detail in "Minimal Key Lengths for
Symmetric Ciphers to Provide Adequate Commercial Security: A Report
by an Ad Hoc Group of Cryptographers and Computer Scientists"
[KeyStudy] that was sponsored by the Business Software Alliance. It
concluded that a reasonable key length in 1995 for very high security
is in the range of 75 to 90 bits and, since the cost of cryptography
does not vary much with the key size, it recommends 90 bits. To
update these recommendations, just add 2/3 of a bit per year for
Moore’s law [MOORE]. This translates to a determination, in the year
2004, a reasonable key length is in the 81- to 96-bit range. In
fact, today, it is increasingly common to use keys longer than 96
bits, such as 128-bit (or longer) keys with AES and keys with
effective lengths of 112-bits with triple-DES.
8.2.2. Meet-in-the-Middle Attacks
If chosen or known plain text and the resulting encrypted text are
available, a "meet-in-the-middle" attack is possible if the structure
of the encryption algorithm allows it. (In a known plain text
attack, the adversary knows all or part (possibly some standard
header or trailer fields) of the messages being encrypted. In a
chosen plain text attack, the adversary can force some chosen plain
text to be encrypted, possibly by "leaking" an exciting text that is
sent by the adversary over an encrypted channel because the text is
so interesting.
The following is an oversimplified explanation of the meet-in-the-
middle attack: the adversary can half-encrypt the known or chosen
plain text with all possible first half-keys, sort the output, and
then half-decrypt the encoded text with all the second half-keys. If
a match is found, the full key can be assembled from the halves and
used to decrypt other parts of the message or other messages. At its
best, this type of attack can halve the exponent of the work required
by the adversary while adding a very large but roughly constant
factor of effort. Thus, if this attack can be mounted, a doubling of
the amount of randomness in the very strong key to a minimum of 192
bits (96*2) is required for the year 2004, based on the [KeyStudy]
analysis.
This amount of randomness is well beyond the limit of that in the
inputs recommended by the US DoD for password generation and could
require user-typing timing, hardware random number generation, or
other sources of randomness.
The meet-in-the-middle attack assumes that the cryptographic
algorithm can be decomposed in this way. Hopefully no modern
algorithm has this weakness, but there may be cases where we are not
sure of that or even of what algorithm a key will be used with. Even
if a basic algorithm is not subject to a meet-in-the-middle attack,
an attempt to produce a stronger algorithm by applying the basic
algorithm twice (or two different algorithms sequentially) with
different keys will gain less added security than would be expected.
Such a composite algorithm would be subject to a meet-in-the-middle
attack.
Enormous resources may be required to mount a meet-in-the-middle
attack, but they are probably within the range of the national
security services of a major nation. Essentially all nations spy on
other nations’ traffic.
8.2.3. Other Considerations
[KeyStudy] also considers the possibilities of special-purpose code-
breaking hardware and having an adequate safety margin.
Note that key length calculations such as those above are
controversial and depend on various assumptions about the
cryptographic algorithms in use. In some cases, a professional with
a deep knowledge of algorithm-breaking techniques and of the strength
of the algorithm in use could be satisfied with less than half of the
192 bit key size derived above.
For further examples of conservative design principles, see
[FERGUSON].
9. Conclusion
Generation of unguessable "random" secret quantities for security use
is an essential but difficult task.
Hardware techniques for producing the needed entropy would be
relatively simple. In particular, the volume and quality would not
need to be high, and existing computer hardware, such as audio input
or disk drives, can be used.
Widely-available computational techniques can process low-quality
random quantities from multiple sources, or a larger quantity of such
low-quality input from one source, to produce a smaller quantity of
higher-quality keying material. In the absence of hardware sources
of randomness, a variety of user and software sources can frequently,
with care, be used instead. However, most modern systems already
have hardware, such as disk drives or audio input, that could be used
to produce high-quality randomness.
Once a sufficient quantity of high-quality seed key material (a
couple of hundred bits) is available, computational techniques are
available to produce cryptographically-strong sequences of
computationally-unpredictable quantities from this seed material.
10. Security Considerations
The entirety of this document concerns techniques and recommendations
for generating unguessable "random" quantities for use as passwords,
cryptographic keys, initialization vectors, sequence numbers, and
similar security applications.
11. Acknowledgements
Special thanks to Paul Hoffman and John Kelsey for their extensive
comments and to Peter Gutmann, who has permitted the incorporation of
material from his paper "Software Generation of Practically Strong
Random Numbers".
The following people (in alphabetic order) have contributed
substantially to this document:
Steve Bellovin, Daniel Brown, Don Davis, Peter Gutmann, Tony
Hansen, Sandy Harris, Paul Hoffman, Scott Hollenback, Russ
Housley, Christian Huitema, John Kelsey, Mats Naslund, and Damir
Rajnovic.
The following people (in alphabetic order) contributed to RFC 1750,
the predecessor of this document:
David M. Balenson, Don T. Davis, Carl Ellison, Marc Horowitz,
Christian Huitema, Charlie Kaufman, Steve Kent, Hal Murray, Neil
Haller, Richard Pitkin, Tim Redmond, and Doug Tygar.
Appendix A: Changes from RFC 1750
1. Additional acknowledgements have been added.
2. Insertion of section 5.3 on mixing with S-boxes.
3. Addition of section 3.3 on Ring Oscillator randomness sources.
4. Addition of AES and the members of the SHA series producing more
than 160 bits. Use of AES has been emphasized and the use of DES
de-emphasized.
5. Addition of section 6.3 on entropy pool techniques.
6. Addition of section 7.2.3 on the pseudo-random number generation
techniques given in FIPS 186-2 (with Change Notice 1), 7.2.1 on
those given in X9.82, section 7.1.2 on the random number
generation techniques of the /dev/random device in Linux and other
UNIX systems, and section 7.1.3 on random number generation
techniques in the Windows operating system.
7. Addition of references to the "Minimal Key Lengths for Symmetric
Ciphers to Provide Adequate Commercial Security" study published
in January 1996 [KeyStudy] and to [RFC1948].
8. Added caveats to using Diffie-Hellman as a mixing function and,
because of those caveats and its computationally intensive nature,
recommend against its use.
9. Addition of references to the X9.82 effort and the [TURBID] and
[NASLUND] papers.
10. Addition of discussion of min-entropy and Renyi entropy and
references to the [LUBY] book.
11. Major restructuring, minor wording changes, and a variety of
reference updates.
Informative References
[AES] "Specification of the Advanced Encryption Standard
(AES)", United States of America, US National
Institute of Standards and Technology, FIPS 197,
November 2001.
[ASYMMETRIC] Simmons, G., Ed., "Secure Communications and
Asymmetric Cryptosystems", AAAS Selected Symposium
69, ISBN 0-86531-338-5, Westview Press, 1982.
[BBS] Blum, L., Blum, M., and M. Shub, "A Simple
Unpredictable Pseudo-Random Number Generator", SIAM
Journal on Computing, v. 15, n. 2, 1986.
[BRILLINGER] Brillinger, D., "Time Series: Data Analysis and
Theory", Holden-Day, 1981.
[CRC] "C.R.C. Standard Mathematical Tables", Chemical