Skip to main content

capsule_core/crypto/
rng.rs

1//! OS CSPRNG access. Every key, salt, and nonce is drawn here; Capsule never seeds
2//! its own PRNG (SSoT: [Cryptography — Primitives § Randomness]).
3//!
4//! [Cryptography — Primitives § Randomness]: https://docs/design/cryptography/primitives/#randomness
5
6/// Fill `buf` with cryptographically secure random bytes from the OS.
7///
8/// Panics only if the OS RNG is unavailable — an unrecoverable environment fault on
9/// every platform Capsule targets, where continuing would be a security defect.
10pub fn fill(buf: &mut [u8]) {
11    getrandom::fill(buf).expect("OS CSPRNG (getrandom) must be available");
12}
13
14/// Draw a fresh `N`-byte random array (key, salt, or nonce prefix).
15pub fn random_array<const N: usize>() -> [u8; N] {
16    let mut a = [0u8; N];
17    fill(&mut a);
18    a
19}
20
21#[cfg(test)]
22mod tests;