Skip to content

librand: Revise crypto part of document #13820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions src/librand/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,27 @@ after generating 32 KiB of random data.

# Cryptographic security

An application that requires random numbers for cryptographic purposes
should prefer `OSRng`, which reads randomness from one of the source
that the operating system provides (e.g. `/dev/urandom` on
Unixes). The other random number generators provided by this module
are either known to be insecure (`XorShiftRng`), or are not verified
to be secure (`IsaacRng`, `Isaac64Rng` and `StdRng`).

*Note*: on Linux, `/dev/random` is more secure than `/dev/urandom`,
but it is a blocking RNG, and will wait until it has determined that
it has collected enough entropy to fulfill a request for random
data. It can be used with the `Rng` trait provided by this module by
opening the file and passing it to `reader::ReaderRng`. Since it
blocks, `/dev/random` should only be used to retrieve small amounts of
randomness.
An application that requires an entropy source for cryptographic purposes
must use `OSRng`, which reads randomness from the source that the operating
system provides (e.g. `/dev/urandom` on Unixes or `CryptGenRandom()` on Windows).
The other random number generators provided by this module are not suitable
for such purposes.

*Note*: many Unix systems provide `/dev/random` as well as `/dev/urandom`.
This module uses `/dev/urandom` for the following reasons:

- On Linux, `/dev/random` may block if entropy pool is empty; `/dev/urandom` will not block.
This does not mean that `/dev/random` provides better output than
`/dev/urandom`; the kernel internally runs a cryptographically secure pseudorandom
number generator (CSPRNG) based on entropy pool for random number generation,
so the "quality" of `/dev/random` is not better than `/dev/urandom` in most cases.
However, this means that `/dev/urandom` can yield somewhat predictable randomness
if the entropy pool is very small, such as immediately after first booting.
If an application likely to be run soon after first booting, or on a system with very
few entropy sources, one should consider using `/dev/random` via `ReaderRng`.
- On some systems (e.g. FreeBSD, OpenBSD and Mac OS X) there is no difference
between the two sources. (Also note that, on some systems e.g. FreeBSD, both `/dev/random`
and `/dev/urandom` may block once if the CSPRNG has not seeded yet.)

# Examples

Expand Down
1 change: 1 addition & 0 deletions src/librand/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ mod imp {
CRYPT_VERIFYCONTEXT | CRYPT_SILENT)
};

// FIXME #13259:
// It turns out that if we can't acquire a context with the
// NTE_BAD_SIGNATURE error code, the documentation states:
//
Expand Down