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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//! # nist-rand — SP 800-90A/B/C CSPRNG Implementation
//!
//! > **⚠️ DISCLAIMER:** This code is **NOT FIPS Verified** and has **NOT** been
//! > officially audited by a NIST lab. Furthermore, this code does not receive
//! > any formal security audits. It implements the best practices and algorithms
//! > described by NIST SP 800-90A/B/C, but does not claim or provide official
//! > compliance or certification. Use at your own risk.
//!
//! A high-assurance, production-ready Cryptographically Secure Pseudorandom Number
//! Generator (CSPRNG) that implements the architectural design and algorithms
//! described in **NIST SP 800-90A/B/C**.
//!
//! ## Architecture
//!
//! ```text
//! ┌───────────────────────────────────────────────────────────────┐
//! │ SP 800-90C Blender │
//! │ ┌──────────────┐ ┌────────────────┐ ┌──────────────────┐ │
//! │ │ OS RNG │ │ CPU Jitter │ │ System State │ │
//! │ │ /dev/urandom │ │ (advance feat) │ │ /proc, TSC, ASLR │ │
//! │ │ + 90B tests │ │ Von Neumann │ │ (advance feat) │ │
//! │ └──────┬───────┘ └───────┬────────┘ └────────┬─────────┘ │
//! │ └──────────────────┴────────────────────┘ │
//! │ │ SHA3-512 │
//! └────────────────────────────┼──────────────────────────────────┘
//! │ 512-bit conditioned seed
//! ┌────────────────────────────▼──────────────────────────────────┐
//! │ Hash_DRBG (SP 800-90A § 10.1.1) │
//! │ SHA3-512 (FIPS 202) │
//! │ reseed every 10 000 requests │
//! └────────────────────────────┬──────────────────────────────────┘
//! │
//! fill() / NistRng
//! ```
//!
//! ## SP 800-90 Design Principles
//!
//! | Requirement | Standard | Implementation |
//! |-------------|----------|----------------|
//! | Entropy source health tests | SP 800-90B § 4.4 | `rng::HealthTests` (RCT + APT) |
//! | Multi-source entropy blending | SP 800-90C § 4.1 | `entropy::get_blended_entropy()` with SHA3-512 |
//! | Approved DRBG mechanism | SP 800-90A § 10.1.1 | `drbg::HashDrbg` |
//! | Approved hash function | FIPS 202 | SHA3-512 |
//! | Reseed interval enforcement | SP 800-90A Table 2 | Panic-on-overrun after 10 000 calls |
//! | Fail-safe on entropy failure | FIPS 140-3 § 9.2 (Aligns with) | `panic!` on health-test or reseed failure |
//!
//! ## Crate Features
//!
//! | Feature | Default | Description |
//! |---------|---------|-------------|
//! | `rand_core` | ✓ | Exposes [`NistRng`] implementing `rand_core` 0.10 traits |
//! | `advance` | ✓ | Adds CPU jitter, System state, and Hardware RNG (RDSEED/RDRAND) + extended 90B health tests |
//! | `build_separator` | ✓ | Embeds a random build-time salt (via `build.rs`) |
//! | `zeroize` | ✓ | Derives [`zeroize::ZeroizeOnDrop`] on `HashDrbg`; wraps all sensitive buffers in [`zeroize::Zeroizing`] so keying material is erased even on panic |
//! | `exp_simd_rng` | ✗ | Experimental: AVX-256 thermal jitter (implies `advance`) |
//! | `exp_network_rng` | ✗ | Experimental: UDP network-latency jitter (implies `advance`) |
/// Hash_DRBG (SP 800-90A § 10.1.1) backed by SHA3-512.
/// Entropy sources and SP 800-90B continuous health tests.
/// OS RNG reader and `HealthTests` state machine.
use HashDrbg;
use RefCell;
// ────────────────────────────────────────────────────────────────────────────────
// Thread-local DRBG instance
// ────────────────────────────────────────────────────────────────────────────────
thread_local!
// ────────────────────────────────────────────────────────────────────────────────
// Public API
// ────────────────────────────────────────────────────────────────────────────────
/// Fills `dest` with cryptographically secure random bytes.
///
/// Bytes are produced by the thread-local SP 800-90A `Hash_DRBG` (SHA3-512).
/// When the DRBG's reseed counter is exhausted, fresh SP 800-90C-conditioned
/// entropy is gathered automatically before retrying.
///
/// # FIPS 140-3 Fail-Safe
///
/// This function **panics** if:
/// - The OS entropy source fails its SP 800-90B health tests, **or**
/// - Generation still fails after an automatic reseed (should never happen in a
/// correctly functioning environment).
///
/// Panicking is the correct FIPS 140-3 response to an entropy-source failure:
/// the module must enter an error state rather than produce predictable output.
///
/// # Example
///
/// ```rust
/// let mut key = [0u8; 32];
/// nist_rand::fill(&mut key);
/// assert!(key.iter().any(|&b| b != 0));
/// ```
/// Forces an immediate reseed of the thread-local DRBG using fresh,
/// SP 800-90C-conditioned entropy.
///
/// This provides Forward Secrecy on demand: an attacker who compromises
/// the system state *after* this call cannot determine any outputs
/// generated *before* this call.
/// Fills `dest` with cryptographically secure random bytes, with guaranteed
/// **Prediction Resistance** (SP 800-90A § 8.7.2).
///
/// This function gathers fresh physical entropy from the underlying OS/Jitter
/// sources and forces an immediate reseed of the thread-local DRBG **before**
/// generating the requested bytes.
///
/// Use this for ultra-sensitive material (e.g., long-term root keys or CA
/// private keys) where you want absolute assurance that the output is tied
/// to fresh physical entropy gathered at the exact moment of the request.
///
/// # Performance Warning
/// This is significantly slower than [`fill`] because it invokes the underlying
/// OS RNG and Jitter sources on every call. For most cryptographic purposes,
/// [`fill`] is completely secure and vastly faster.
// ────────────────────────────────────────────────────────────────────────────────
// Optional rand_core 0.10 integration
// ────────────────────────────────────────────────────────────────────────────────
pub use ;
// ────────────────────────────────────────────────────────────────────────────────
// Tests
// ────────────────────────────────────────────────────────────────────────────────