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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//! Cryptographically strong randomness for encryption **authoring** (`Pass 5.4`).
//!
//! One function, [`fill`], and a hard rule about it: it is the ONLY source of
//! the bytes that must be unpredictable in an encrypted document — the 256-bit
//! file encryption key (Algorithms 8/9 require it be "generated with a strong
//! random number generator"), the 8-byte validation and key salts, and the
//! 16-byte AES-CBC IV of every encrypted string and stream (Algorithm 1.A).
//!
//! # Why this is a seam and not a call to `getrandom` at each site
//!
//! Two reasons, and the second is the load-bearing one:
//!
//! 1. **Every caller must treat "no entropy" as a refusal, never a fallback.**
//! A weak key is worse than no encryption, because it *looks* encrypted. So
//! [`fill`] returns a `Result`, and there is exactly one place — here — where
//! the decision "what counts as a strong source" is made.
//! 2. **The engine crate must cross into the wasm32 web fork, where there is no
//! entropy source and no encryption authoring.** `getrandom` 0.2 does not
//! even *compile* on `wasm32-unknown-unknown` without a backend feature, so
//! the dependency is target-gated OFF wasm in `Cargo.toml` and this module
//! provides a refusing stub there. The web fork is a viewer; it never saves,
//! let alone encrypts, so the stub is unreachable in practice and correct in
//! principle — a `RngUnavailable` is exactly the right answer to "encrypt
//! this" on a target with no CSPRNG.
/// The one thing that can go wrong: the operating system's CSPRNG was not
/// available. On every real target this does not happen; it is modelled rather
/// than `unwrap`ped because a silently-weak key is the one failure this whole
/// module exists to make impossible.
/// Fill `buf` with cryptographically strong random bytes.
///
/// # Errors
///
/// [`RngError::Unavailable`] if the OS CSPRNG could not be reached (never, on a
/// normal desktop target) or if this is a `wasm32` build (always — see the
/// module docs).
/// The `wasm32` stub: there is no CSPRNG and no encryption authoring on the web
/// fork, so this refuses unconditionally. See the module docs.
///
/// # Errors
///
/// Always [`RngError::Unavailable`].
/// A fresh `[u8; N]` of strong random bytes.
///
/// # Errors
///
/// As [`fill`].