oxicrypto-rand 0.1.0

Pure Rust CSPRNG for OxiCrypto (ChaCha20 seeded from getrandom)
Documentation
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! ChaCha-based CSPRNG implementations: `OxiRng` (ChaCha20), `OxiRng8`
//! (ChaCha8), and `OxiRng12` (ChaCha12).
//!
//! All three variants are fork-safe on Unix via PID tracking.

use oxicrypto_core::{CryptoError, Rng};
use rand_chacha::ChaCha20Rng;
use rand_core::{SeedableRng, TryRng};

// ── OxiRng (ChaCha20) ────────────────────────────────────────────────────────

/// A ChaCha20 CSPRNG seeded from the OS random source.
///
/// Use [`OxiRng::new`] to create an instance.  The seed is obtained from
/// `getrandom::fill` which calls `/dev/urandom`, `RtlGenRandom`, or
/// `arc4random` depending on the platform — no C library required.
///
/// On Unix platforms, [`OxiRng`] automatically reseeds itself if the process
/// PID changes (i.e. after a `fork()`), preventing parent/child state sharing.
pub struct OxiRng {
    pub(crate) inner: ChaCha20Rng,
    #[cfg(unix)]
    pub(crate) last_pid: u32,
}

impl OxiRng {
    /// Create a new [`OxiRng`] seeded from the OS.
    ///
    /// Returns [`CryptoError::Internal`] if `getrandom` fails.
    #[must_use = "the RNG must be stored and used; discarding it serves no purpose"]
    pub fn new() -> Result<Self, CryptoError> {
        let mut seed = [0u8; 32];
        getrandom::fill(&mut seed).map_err(|_| CryptoError::Internal("getrandom failed"))?;
        Ok(Self {
            inner: ChaCha20Rng::from_seed(seed),
            #[cfg(unix)]
            last_pid: std::process::id(),
        })
    }

    /// Reseed this RNG from OS entropy.
    ///
    /// Replaces the internal ChaCha20 state with a fresh 32-byte seed and
    /// updates the stored PID to the current process.  The free function
    /// [`crate::reseed`] is kept for backward compatibility.
    ///
    /// Returns [`CryptoError::Rng`] if `getrandom` fails.
    pub fn reseed(&mut self) -> Result<(), CryptoError> {
        crate::helpers::reseed(self)
    }

    /// Fill a fixed-size array with cryptographically random bytes.
    pub fn fill_exact<const N: usize>(&mut self, dst: &mut [u8; N]) -> Result<(), CryptoError> {
        self.fill(dst.as_mut_slice())
    }

    /// Detect fork: if PID changed since construction, reseed from OS entropy.
    ///
    /// This prevents child processes from sharing CSPRNG state with the parent.
    #[cfg(unix)]
    pub(crate) fn check_fork(&mut self) -> Result<(), CryptoError> {
        let current_pid = std::process::id();
        if current_pid != self.last_pid {
            // PID changed: we're in a child process — reseed to avoid state sharing.
            let mut seed = [0u8; 32];
            getrandom::fill(&mut seed).map_err(|_| CryptoError::Rng)?;
            self.inner = ChaCha20Rng::from_seed(seed);
            self.last_pid = current_pid;
        }
        Ok(())
    }
}

impl Rng for OxiRng {
    fn fill(&mut self, dst: &mut [u8]) -> Result<(), CryptoError> {
        #[cfg(unix)]
        self.check_fork()?;
        self.inner
            .try_fill_bytes(dst)
            .map_err(|_| CryptoError::Rng)?;
        Ok(())
    }
}

impl core::fmt::Debug for OxiRng {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str("OxiRng { [state redacted] }")
    }
}

impl core::fmt::Display for OxiRng {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str("OxiRng(ChaCha20)")
    }
}

// Implement `rand_core::TryRng` so that `OxiRng` can be used with crates
// requiring `TryCryptoRng` bounds (e.g. `ml-kem`, `ml-dsa`, `p256`, etc.).
//
// Error type is `CryptoError` because `try_fill_bytes` calls `check_fork`,
// which may fail if `getrandom` is unavailable after a fork.

impl rand_core::TryRng for OxiRng {
    type Error = CryptoError;

    fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
        self.inner.try_next_u32().map_err(|_| CryptoError::Rng)
    }

    fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
        self.inner.try_next_u64().map_err(|_| CryptoError::Rng)
    }

    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Self::Error> {
        #[cfg(unix)]
        self.check_fork()?;
        self.inner
            .try_fill_bytes(dest)
            .map_err(|_| CryptoError::Rng)
    }
}

impl rand_core::TryCryptoRng for OxiRng {}

// ── OxiRng8 (ChaCha8) ────────────────────────────────────────────────────────

/// A ChaCha8 CSPRNG seeded from the OS random source.
///
/// ChaCha8 uses 8 rounds instead of 20, offering higher throughput at the cost
/// of a smaller security margin.  Suitable for performance-critical paths where
/// full ChaCha20 is not needed.
///
/// Fork-safe: on Unix, detects `fork()` via PID tracking and reseeds.
pub struct OxiRng8 {
    inner: rand_chacha::ChaCha8Rng,
    #[cfg(unix)]
    last_pid: u32,
}

impl OxiRng8 {
    /// Create a new [`OxiRng8`] seeded from the OS.
    pub fn new() -> Result<Self, CryptoError> {
        let mut seed = [0u8; 32];
        getrandom::fill(&mut seed).map_err(|_| CryptoError::Internal("getrandom failed"))?;
        Ok(Self {
            inner: rand_chacha::ChaCha8Rng::from_seed(seed),
            #[cfg(unix)]
            last_pid: std::process::id(),
        })
    }

    /// Reseed from OS entropy.
    pub fn reseed(&mut self) -> Result<(), CryptoError> {
        let mut seed = [0u8; 32];
        getrandom::fill(&mut seed).map_err(|_| CryptoError::Rng)?;
        self.inner = rand_chacha::ChaCha8Rng::from_seed(seed);
        #[cfg(unix)]
        {
            self.last_pid = std::process::id();
        }
        Ok(())
    }

    #[cfg(unix)]
    fn check_fork(&mut self) -> Result<(), CryptoError> {
        let current_pid = std::process::id();
        if current_pid != self.last_pid {
            let mut seed = [0u8; 32];
            getrandom::fill(&mut seed).map_err(|_| CryptoError::Rng)?;
            self.inner = rand_chacha::ChaCha8Rng::from_seed(seed);
            self.last_pid = current_pid;
        }
        Ok(())
    }
}

impl Rng for OxiRng8 {
    fn fill(&mut self, dst: &mut [u8]) -> Result<(), CryptoError> {
        #[cfg(unix)]
        self.check_fork()?;
        self.inner.try_fill_bytes(dst).map_err(|_| CryptoError::Rng)
    }
}

impl rand_core::TryRng for OxiRng8 {
    type Error = CryptoError;

    fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
        self.inner.try_next_u32().map_err(|_| CryptoError::Rng)
    }

    fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
        self.inner.try_next_u64().map_err(|_| CryptoError::Rng)
    }

    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Self::Error> {
        #[cfg(unix)]
        self.check_fork()?;
        self.inner
            .try_fill_bytes(dest)
            .map_err(|_| CryptoError::Rng)
    }
}

impl rand_core::TryCryptoRng for OxiRng8 {}

impl core::fmt::Debug for OxiRng8 {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("OxiRng8").finish_non_exhaustive()
    }
}

// ── OxiRng12 (ChaCha12) ──────────────────────────────────────────────────────

/// A ChaCha12 CSPRNG seeded from the OS random source.
///
/// ChaCha12 uses 12 rounds — a middle ground between ChaCha8 (8 rounds) and
/// ChaCha20 (20 rounds), offering good performance with a higher security
/// margin than ChaCha8.
///
/// Fork-safe: on Unix, detects `fork()` via PID tracking and reseeds.
pub struct OxiRng12 {
    inner: rand_chacha::ChaCha12Rng,
    #[cfg(unix)]
    last_pid: u32,
}

impl OxiRng12 {
    /// Create a new [`OxiRng12`] seeded from the OS.
    pub fn new() -> Result<Self, CryptoError> {
        let mut seed = [0u8; 32];
        getrandom::fill(&mut seed).map_err(|_| CryptoError::Internal("getrandom failed"))?;
        Ok(Self {
            inner: rand_chacha::ChaCha12Rng::from_seed(seed),
            #[cfg(unix)]
            last_pid: std::process::id(),
        })
    }

    /// Reseed from OS entropy.
    pub fn reseed(&mut self) -> Result<(), CryptoError> {
        let mut seed = [0u8; 32];
        getrandom::fill(&mut seed).map_err(|_| CryptoError::Rng)?;
        self.inner = rand_chacha::ChaCha12Rng::from_seed(seed);
        #[cfg(unix)]
        {
            self.last_pid = std::process::id();
        }
        Ok(())
    }

    #[cfg(unix)]
    fn check_fork(&mut self) -> Result<(), CryptoError> {
        let current_pid = std::process::id();
        if current_pid != self.last_pid {
            let mut seed = [0u8; 32];
            getrandom::fill(&mut seed).map_err(|_| CryptoError::Rng)?;
            self.inner = rand_chacha::ChaCha12Rng::from_seed(seed);
            self.last_pid = current_pid;
        }
        Ok(())
    }
}

impl Rng for OxiRng12 {
    fn fill(&mut self, dst: &mut [u8]) -> Result<(), CryptoError> {
        #[cfg(unix)]
        self.check_fork()?;
        self.inner.try_fill_bytes(dst).map_err(|_| CryptoError::Rng)
    }
}

impl rand_core::TryRng for OxiRng12 {
    type Error = CryptoError;

    fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
        self.inner.try_next_u32().map_err(|_| CryptoError::Rng)
    }

    fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
        self.inner.try_next_u64().map_err(|_| CryptoError::Rng)
    }

    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Self::Error> {
        #[cfg(unix)]
        self.check_fork()?;
        self.inner
            .try_fill_bytes(dest)
            .map_err(|_| CryptoError::Rng)
    }
}

impl rand_core::TryCryptoRng for OxiRng12 {}

impl core::fmt::Debug for OxiRng12 {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("OxiRng12").finish_non_exhaustive()
    }
}

// ── Unit tests that require private field access ──────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn oxi_rng_creates_ok() {
        let rng = OxiRng::new();
        assert!(rng.is_ok(), "OxiRng::new() should succeed on this platform");
    }

    #[test]
    fn oxi_rng_fills_buffer() {
        let mut rng = OxiRng::new().expect("OxiRng::new failed");
        let mut buf = [0u8; 64];
        rng.fill(&mut buf).expect("fill should succeed");
        assert_ne!(buf, [0u8; 64], "Random bytes should not all be zero");
    }

    #[test]
    fn oxi_rng_two_outputs_differ() {
        let mut rng = OxiRng::new().expect("OxiRng::new failed");
        let mut buf1 = [0u8; 32];
        let mut buf2 = [0u8; 32];
        rng.fill(&mut buf1).expect("fill 1 failed");
        rng.fill(&mut buf2).expect("fill 2 failed");
        assert_ne!(buf1, buf2, "Consecutive RNG outputs should differ");
    }

    #[test]
    fn oxi_rng_reseed_method_changes_output() {
        let mut rng = OxiRng::new().expect("new failed");
        let mut buf1 = [0u8; 32];
        rng.fill(&mut buf1).expect("fill 1 failed");
        rng.reseed().expect("OxiRng::reseed() failed");
        let mut buf2 = [0u8; 32];
        rng.fill(&mut buf2).expect("fill 2 failed");
        assert_ne!(buf1, buf2, "Output after reseed() should differ");
    }

    #[cfg(unix)]
    #[test]
    fn fork_safe_pid_simulation() {
        let mut rng = OxiRng::new().expect("OxiRng::new failed");
        let mut before = [0u8; 32];
        rng.fill(&mut before).expect("fill before failed");
        // Simulate a fork by setting last_pid to a fake value.
        rng.last_pid = 0; // PID 0 is never a real user process PID.
        let mut after = [0u8; 32];
        rng.fill(&mut after)
            .expect("fill after fork-simulation failed");
        assert_ne!(
            before, after,
            "After fork simulation, RNG should have reseeded"
        );
        assert_eq!(rng.last_pid, std::process::id());
    }

    #[test]
    fn oxi_rng_implements_try_crypto_rng() {
        fn requires_try_crypto_rng<R: rand_core::TryCryptoRng>(_rng: &mut R) {}
        let mut rng = OxiRng::new().expect("new failed");
        requires_try_crypto_rng(&mut rng);
    }

    #[test]
    fn oxi_rng_debug_does_not_leak_state() {
        let rng = OxiRng::new().expect("OxiRng::new failed");
        let dbg = std::format!("{rng:?}");
        assert!(dbg.contains("OxiRng"), "Debug must include type name");
        assert!(
            dbg.contains("redacted"),
            "Debug must not expose internal state"
        );
    }

    #[test]
    fn oxi_rng_display_shows_algorithm() {
        let rng = OxiRng::new().expect("OxiRng::new failed");
        let display = std::format!("{rng}");
        assert_eq!(
            display, "OxiRng(ChaCha20)",
            "Display must identify the algorithm"
        );
    }

    #[test]
    fn fill_exact_works() {
        let mut rng = OxiRng::new().expect("OxiRng::new failed");
        let mut arr = [0u8; 16];
        rng.fill_exact(&mut arr).expect("fill_exact failed");
        assert_ne!(
            arr, [0u8; 16],
            "fill_exact must not produce all-zero output"
        );
    }

    #[test]
    fn oxi_rng8_fills_buffer() {
        let mut rng = OxiRng8::new().expect("OxiRng8::new failed");
        let mut buf = [0u8; 64];
        rng.fill(&mut buf).expect("OxiRng8::fill failed");
        assert_ne!(buf, [0u8; 64], "OxiRng8 output should not be all zeros");
    }

    #[test]
    fn oxi_rng12_fills_buffer() {
        let mut rng = OxiRng12::new().expect("OxiRng12::new failed");
        let mut buf = [0u8; 64];
        rng.fill(&mut buf).expect("OxiRng12::fill failed");
        assert_ne!(buf, [0u8; 64], "OxiRng12 output should not be all zeros");
    }

    #[test]
    fn oxi_rng8_two_instances_differ() {
        let mut rng1 = OxiRng8::new().expect("OxiRng8::new 1 failed");
        let mut rng2 = OxiRng8::new().expect("OxiRng8::new 2 failed");
        let mut buf1 = [0u8; 32];
        let mut buf2 = [0u8; 32];
        rng1.fill(&mut buf1).expect("fill 1 failed");
        rng2.fill(&mut buf2).expect("fill 2 failed");
        assert_ne!(
            buf1, buf2,
            "Two independently seeded OxiRng8 instances should differ"
        );
    }

    #[test]
    fn oxi_rng12_two_instances_differ() {
        let mut rng1 = OxiRng12::new().expect("OxiRng12::new 1 failed");
        let mut rng2 = OxiRng12::new().expect("OxiRng12::new 2 failed");
        let mut buf1 = [0u8; 32];
        let mut buf2 = [0u8; 32];
        rng1.fill(&mut buf1).expect("fill 1 failed");
        rng2.fill(&mut buf2).expect("fill 2 failed");
        assert_ne!(
            buf1, buf2,
            "Two independently seeded OxiRng12 instances should differ"
        );
    }

    #[test]
    fn oxi_rng8_reseed_changes_output() {
        let mut rng = OxiRng8::new().expect("OxiRng8::new failed");
        let mut buf1 = [0u8; 32];
        rng.fill(&mut buf1).expect("fill 1 failed");
        rng.reseed().expect("OxiRng8::reseed failed");
        let mut buf2 = [0u8; 32];
        rng.fill(&mut buf2).expect("fill 2 failed");
        assert_ne!(buf1, buf2, "Output after OxiRng8::reseed should differ");
    }

    #[test]
    fn oxi_rng12_reseed_changes_output() {
        let mut rng = OxiRng12::new().expect("OxiRng12::new failed");
        let mut buf1 = [0u8; 32];
        rng.fill(&mut buf1).expect("fill 1 failed");
        rng.reseed().expect("OxiRng12::reseed failed");
        let mut buf2 = [0u8; 32];
        rng.fill(&mut buf2).expect("fill 2 failed");
        assert_ne!(buf1, buf2, "Output after OxiRng12::reseed should differ");
    }

    #[test]
    fn oxi_rng8_implements_try_crypto_rng() {
        fn requires_try_crypto_rng<R: rand_core::TryCryptoRng>(_rng: &mut R) {}
        let mut rng = OxiRng8::new().expect("OxiRng8::new failed");
        requires_try_crypto_rng(&mut rng);
    }

    #[test]
    fn oxi_rng12_implements_try_crypto_rng() {
        fn requires_try_crypto_rng<R: rand_core::TryCryptoRng>(_rng: &mut R) {}
        let mut rng = OxiRng12::new().expect("OxiRng12::new failed");
        requires_try_crypto_rng(&mut rng);
    }

    #[test]
    fn fill_various_sizes() {
        let mut rng = OxiRng::new().expect("OxiRng::new failed");
        for size in [0usize, 1, 31, 32, 33, 1024] {
            let mut buf = std::vec![0u8; size];
            rng.fill(&mut buf)
                .expect("fill should succeed for all sizes");
            assert_eq!(buf.len(), size, "fill must not change buffer length");
        }
    }

    #[test]
    fn fill_one_byte_not_stuck_at_zero() {
        let mut rng = OxiRng::new().expect("OxiRng::new failed");
        let mut saw_nonzero = false;
        for _ in 0..256 {
            let mut buf = [0u8; 1];
            rng.fill(&mut buf).expect("fill 1 byte");
            if buf[0] != 0 {
                saw_nonzero = true;
                break;
            }
        }
        assert!(
            saw_nonzero,
            "At least one single-byte fill should be non-zero"
        );
    }
}