@@ -24,10 +24,6 @@ use rand_core::{CryptoRng, RngCore, Error, impls};
24
24
/// not entirely theoretical, for `OsRng` to fail. In such cases [`EntropyRng`]
25
25
/// falls back on a good alternative entropy source.
26
26
///
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
- ///
31
27
/// `OsRng::new()` is guaranteed to be very cheap (after the first successful
32
28
/// call), and will never consume more than one file handle per process.
33
29
///
@@ -52,7 +48,7 @@ use rand_core::{CryptoRng, RngCore, Error, impls};
52
48
///
53
49
/// Rand doesn't have a blanket implementation for all Unix-like operating
54
50
/// 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
56
52
/// sizes.
57
53
///
58
54
/// ## Support for WebAssembly and ams.js
@@ -67,21 +63,26 @@ use rand_core::{CryptoRng, RngCore, Error, impls};
67
63
/// methods directly, using `stdweb` in combination with `cargo-web`.
68
64
/// `wasm-bindgen` is not yet supported.
69
65
///
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.
71
76
///
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.
77
80
///
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).
85
86
///
86
87
/// # Panics
87
88
///
@@ -93,6 +94,7 @@ use rand_core::{CryptoRng, RngCore, Error, impls};
93
94
/// [`EntropyRng`]: struct.EntropyRng.html
94
95
/// [`RngCore`]: ../trait.RngCore.html
95
96
/// [`try_fill_bytes`]: ../trait.RngCore.html#method.tymethod.try_fill_bytes
97
+ /// [`ErrorKind::NotReady`]: ../enum.ErrorKind.html#variant.NotReady
96
98
///
97
99
/// [1]: http://man7.org/linux/man-pages/man2/getrandom.2.html
98
100
/// [2]: http://man7.org/linux/man-pages/man4/urandom.4.html
0 commit comments