krypteia-quantica 0.1.0

Pure-Rust post-quantum cryptography: FIPS 203 ML-KEM, FIPS 204 ML-DSA, and FIPS 205 SLH-DSA. First-order arithmetic masking, shuffled NTT, FORS recompute-and-compare redundancy, constant-time rejection sampling. Targets embedded (no_std), STM32 M0/M4/M33, ESP32-C3 RISC-V. Zero runtime dependencies.
Documentation
/// Minimal cryptographic RNG trait and OS-backed implementation.
///
/// Provides the [`CryptoRng`] trait used throughout ML-KEM for obtaining
/// random bytes, and [`OsRng`], a zero-dependency implementation backed
/// by the operating system's entropy source.
use super::MlKemError;

/// Trait for cryptographic-quality random byte generation.
///
/// Implementations must provide bytes suitable for key material and nonces.
/// The trait is intentionally minimal (a single method) to allow easy
/// integration with external RNG libraries or hardware RNGs.
///
/// # Errors
///
/// Returns [`MlKemError::RngFailure`] if the underlying entropy source
/// is unavailable or fails.
pub trait CryptoRng {
    /// Fill `dest` with cryptographically secure random bytes.
    ///
    /// # Errors
    ///
    /// Returns [`MlKemError::RngFailure`] on failure.
    fn fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), MlKemError>;
}

/// OS-backed cryptographic RNG reading from `/dev/urandom` on Unix.
///
/// Only available with the `std` feature. In `no_std` builds, callers
/// must supply their own [`CryptoRng`] implementation (typically
/// wrapping a hardware RNG on embedded targets).
#[cfg(feature = "std")]
pub struct OsRng;

#[cfg(feature = "std")]
impl CryptoRng for OsRng {
    /// Fill `dest` with random bytes from `/dev/urandom`.
    fn fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), MlKemError> {
        use std::io::Read;
        let mut f = std::fs::File::open("/dev/urandom").map_err(|_| MlKemError::RngFailure)?;
        f.read_exact(dest).map_err(|_| MlKemError::RngFailure)
    }
}