Skip to content

Commit f824d91

Browse files
committed
Update OsRng documentation
1 parent 07f86f1 commit f824d91

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

src/rngs/os.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ use rand_core::{CryptoRng, RngCore, Error, impls};
2424
/// not entirely theoretical, for `OsRng` to fail. In such cases [`EntropyRng`]
2525
/// falls back on a good alternative entropy source.
2626
///
27-
/// `OsRng` usually does not block. On some systems, and notably virtual
28-
/// machines, it may block very early in the init process, when the OS CSPRNG
29-
/// has not yet been seeded.
30-
///
3127
/// `OsRng::new()` is guaranteed to be very cheap (after the first successful
3228
/// call), and will never consume more than one file handle per process.
3329
///
@@ -52,7 +48,7 @@ use rand_core::{CryptoRng, RngCore, Error, impls};
5248
///
5349
/// Rand doesn't have a blanket implementation for all Unix-like operating
5450
/// systems that reads from `/dev/urandom`. This ensures all supported operating
55-
/// systems are using the recommended interface and maximum permitted buffer
51+
/// systems are using the recommended interface and respect maximum buffer
5652
/// sizes.
5753
///
5854
/// ## Support for WebAssembly and ams.js
@@ -67,21 +63,26 @@ use rand_core::{CryptoRng, RngCore, Error, impls};
6763
/// methods directly, using `stdweb` in combination with `cargo-web`.
6864
/// `wasm-bindgen` is not yet supported.
6965
///
70-
/// ## Notes on Unix `/dev/urandom`
66+
/// ## Early boot
67+
///
68+
/// It is possible that early in the boot process the OS hasn't had enough time
69+
/// yet to collect entropy to securely seed its RNG, especially on virtual
70+
/// machines.
71+
///
72+
/// Some operating systems always block the thread until the RNG is securely
73+
/// seeded. This can take anywhere from a few seconds to more than a minute.
74+
/// Others make a best effort to use a seed from before the shutdown and don't
75+
/// document much.
7176
///
72-
/// Many Unix systems provide `/dev/random` as well as `/dev/urandom`. On all
73-
/// modern systems these two interfaces offer identical quality, with the
74-
/// difference that on some systems `/dev/random` may block. This is a dated
75-
/// design, and `/dev/urandom` is preferred by cryptography experts.
76-
/// See [Myths about urandom](https://www.2uo.de/myths-about-urandom/).
77+
/// A few, Linux, NetBSD and Solaris, offer a choice between blocking, and
78+
/// getting an error. With `try_fill_bytes` we choose to get the error
79+
/// ([`ErrorKind::NotReady`]), while the other methods use a blocking interface.
7780
///
78-
/// On some systems reading from `/dev/urandom` “may return data prior to the
79-
/// entropy pool being initialized”. I.e., early in the boot process, and
80-
/// especially on virtual machines, `/dev/urandom` may return data that is less
81-
/// random. As a countermeasure we try to do a single read from `/dev/random` in
82-
/// non-blocking mode. If the OS RNG is not yet properly seeded, we will get an
83-
/// error. Because we keep one file descriptor to `/dev/urandom` open when
84-
/// succesful, this is only a small one-time cost.
81+
/// On Linux (when the `genrandom` system call is not available) and on NetBSD
82+
/// reading from `/dev/urandom` never blocks, even when the OS hasn't collected
83+
/// enough entropy yet. As a countermeasure we try to do a single read from
84+
/// `/dev/random` until we know the OS RNG is initialized (and store this in a
85+
/// global static).
8586
///
8687
/// # Panics
8788
///
@@ -93,6 +94,7 @@ use rand_core::{CryptoRng, RngCore, Error, impls};
9394
/// [`EntropyRng`]: struct.EntropyRng.html
9495
/// [`RngCore`]: ../trait.RngCore.html
9596
/// [`try_fill_bytes`]: ../trait.RngCore.html#method.tymethod.try_fill_bytes
97+
/// [`ErrorKind::NotReady`]: ../enum.ErrorKind.html#variant.NotReady
9698
///
9799
/// [1]: http://man7.org/linux/man-pages/man2/getrandom.2.html
98100
/// [2]: http://man7.org/linux/man-pages/man4/urandom.4.html

0 commit comments

Comments
 (0)