Skip to content

Commit 1ee5e7f

Browse files
committed
auto merge of #13820 : klutzy/rust/urandom, r=alexcrichton
This patch adds document which explains when to use `OSRng` in cryptographic context, and explains why we use `/dev/urandom` instead of `/dev/random`.
2 parents 07d6322 + c92f519 commit 1ee5e7f

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

src/librand/lib.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,27 @@ after generating 32 KiB of random data.
3030
3131
# Cryptographic security
3232
33-
An application that requires random numbers for cryptographic purposes
34-
should prefer `OSRng`, which reads randomness from one of the source
35-
that the operating system provides (e.g. `/dev/urandom` on
36-
Unixes). The other random number generators provided by this module
37-
are either known to be insecure (`XorShiftRng`), or are not verified
38-
to be secure (`IsaacRng`, `Isaac64Rng` and `StdRng`).
39-
40-
*Note*: on Linux, `/dev/random` is more secure than `/dev/urandom`,
41-
but it is a blocking RNG, and will wait until it has determined that
42-
it has collected enough entropy to fulfill a request for random
43-
data. It can be used with the `Rng` trait provided by this module by
44-
opening the file and passing it to `reader::ReaderRng`. Since it
45-
blocks, `/dev/random` should only be used to retrieve small amounts of
46-
randomness.
33+
An application that requires an entropy source for cryptographic purposes
34+
must use `OSRng`, which reads randomness from the source that the operating
35+
system provides (e.g. `/dev/urandom` on Unixes or `CryptGenRandom()` on Windows).
36+
The other random number generators provided by this module are not suitable
37+
for such purposes.
38+
39+
*Note*: many Unix systems provide `/dev/random` as well as `/dev/urandom`.
40+
This module uses `/dev/urandom` for the following reasons:
41+
42+
- On Linux, `/dev/random` may block if entropy pool is empty; `/dev/urandom` will not block.
43+
This does not mean that `/dev/random` provides better output than
44+
`/dev/urandom`; the kernel internally runs a cryptographically secure pseudorandom
45+
number generator (CSPRNG) based on entropy pool for random number generation,
46+
so the "quality" of `/dev/random` is not better than `/dev/urandom` in most cases.
47+
However, this means that `/dev/urandom` can yield somewhat predictable randomness
48+
if the entropy pool is very small, such as immediately after first booting.
49+
If an application likely to be run soon after first booting, or on a system with very
50+
few entropy sources, one should consider using `/dev/random` via `ReaderRng`.
51+
- On some systems (e.g. FreeBSD, OpenBSD and Mac OS X) there is no difference
52+
between the two sources. (Also note that, on some systems e.g. FreeBSD, both `/dev/random`
53+
and `/dev/urandom` may block once if the CSPRNG has not seeded yet.)
4754
4855
# Examples
4956

src/librand/os.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ mod imp {
109109
CRYPT_VERIFYCONTEXT | CRYPT_SILENT)
110110
};
111111

112+
// FIXME #13259:
112113
// It turns out that if we can't acquire a context with the
113114
// NTE_BAD_SIGNATURE error code, the documentation states:
114115
//

0 commit comments

Comments
 (0)