noxtls-platform 0.2.12

Internal implementation crate for noxtls: time, RNG, and storage abstractions.
Documentation
// Copyright (c) 2019-2026, Argenox Technologies LLC
// All rights reserved.
//
// SPDX-License-Identifier: GPL-2.0-only OR LicenseRef-Argenox-Commercial-License

//! Entropy sources for DRBG seeding on embedded and hosted targets.

/// Fills `dst` with unpredictable bytes suitable for cryptographic seeding.
pub trait EntropySource {
    /// Writes entropy into every byte of `dst`.
    fn fill(&mut self, dst: &mut [u8]);
}

/// Deterministic bytes for unit tests only.
#[derive(Debug, Clone)]
pub struct TestEntropySource {
    counter: u64,
}

impl TestEntropySource {
    /// Creates a test source seeded at zero.
    #[must_use]
    pub fn noxtls_new() -> Self {
        Self { counter: 0 }
    }
}

impl EntropySource for TestEntropySource {
    fn fill(&mut self, dst: &mut [u8]) {
        for byte in dst.iter_mut() {
            *byte = (self.counter & 0xff) as u8;
            self.counter = self.counter.wrapping_add(1);
        }
    }
}

/// Adapts any `rand_core::RngCore` (Embassy TRNG, STM32 RNG, etc.).
#[cfg(feature = "rand-core")]
pub struct RandCoreEntropy<R>(pub R);

#[cfg(feature = "rand-core")]
impl<R: rand_core::RngCore> EntropySource for RandCoreEntropy<R> {
    fn fill(&mut self, dst: &mut [u8]) {
        self.0.fill_bytes(dst);
    }
}

/// OS randomness via `getrandom` when `std` is enabled.
#[cfg(feature = "std")]
#[derive(Debug, Clone, Copy, Default)]
pub struct StdEntropy;

#[cfg(feature = "std")]
impl EntropySource for StdEntropy {
    fn fill(&mut self, dst: &mut [u8]) {
        getrandom::fill(dst).expect("OS entropy source failed");
    }
}