1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//! The operating system's random number generator.
//!
//! [`fill`] and [`fill_uninit`] write bytes from the system's
//! cryptographically secure generator: unpredictable, and suitable for keys.
//!
//! | System | Source |
//! |---|---|
//! | Linux | `getrandom` |
//! | macOS | `getentropy`, 256 bytes at a time |
//! | Windows | `ProcessPrng` (`bcryptprimitives.dll`) |
//! | WASI | `random_get` |
//!
//! Each call reaches the system: nothing is buffered in the process. A
//! program that wants many small random values should seed its own generator
//! from one call.
use MaybeUninit;
use crateResult;
use cratesys;
/// Fills `buf` with random bytes.
///
/// The whole buffer is filled or an error is returned: where the system
/// hands out bytes in pieces, this function asks again for the rest. This is
/// one of the few places oscall loops, because a partly filled buffer is of
/// no use to anybody.
///
/// On Linux, early in the boot of a system that has not gathered entropy
/// yet, the call blocks until the generator is ready; a signal handler
/// running in the meantime makes it fail with
/// [`Interrupted`](crate::ErrorKind::Interrupted). Once the generator is
/// ready the call neither blocks nor is interrupted. Elsewhere it does not
/// fail.
///
/// Fails with [`Unsupported`](crate::ErrorKind::Unsupported) on WebAssembly
/// without WASI.
/// Fills `buf` with random bytes, and returns it as initialised.
///
/// This is [`fill`] for a buffer that has not been initialised: it saves
/// zeroing memory that is about to be overwritten.