ring-native-ossl 0.1.8

A ring-compatible API backed by native-ossl (OpenSSL)
Documentation
//! Cryptographically secure random byte generation, mirroring `ring::rand`.
//!
//! [`SystemRandom`] delegates to OpenSSL's `RAND_bytes` and implements the
//! [`SecureRandom`] trait, which serves the same role as `ring::rand::SecureRandom`
//! as a bound in [`crate::agreement`] and [`crate::signature`].

use crate::error::Unspecified;
use native_ossl::rand::Rand;

/// A random number generator backed by OpenSSL (`RAND_bytes`).
///
/// Mirrors `ring::rand::SystemRandom`.
#[derive(Debug, Clone)]
pub struct SystemRandom;

impl SystemRandom {
    #[must_use]
    pub fn new() -> Self {
        Self
    }
}

impl Default for SystemRandom {
    fn default() -> Self {
        Self::new()
    }
}

/// Sealed trait, mirroring `ring::rand::SecureRandom`.
pub trait SecureRandom: sealed::SecureRandom {
    /// # Errors
    ///
    /// Returns `Unspecified` if the random number generator fails.
    fn fill(&self, dest: &mut [u8]) -> Result<(), Unspecified>;
}

mod sealed {
    pub trait SecureRandom {}
}

impl sealed::SecureRandom for SystemRandom {}

impl SecureRandom for SystemRandom {
    fn fill(&self, dest: &mut [u8]) -> Result<(), Unspecified> {
        Rand::fill(dest).map_err(|_| Unspecified)
    }
}