From 99fea54006b21ca42020761def497daa72e11f78 Mon Sep 17 00:00:00 2001 From: Eric Lagergren Date: Tue, 7 Jan 2025 15:58:33 -0800 Subject: [PATCH] crypto: get rid of ThreadRng Fixes #43 Signed-off-by: Eric Lagergren --- crates/aranya-crypto-core/Cargo.toml | 4 ---- crates/aranya-crypto-core/src/csprng.rs | 8 ++++---- crates/aranya-crypto-core/src/default.rs | 15 ++++++--------- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/crates/aranya-crypto-core/Cargo.toml b/crates/aranya-crypto-core/Cargo.toml index 80cdf56c..e5967368 100644 --- a/crates/aranya-crypto-core/Cargo.toml +++ b/crates/aranya-crypto-core/Cargo.toml @@ -16,7 +16,6 @@ workspace = true [features] default = [ "getrandom", - #"trng", ] # Enable allocations. @@ -93,9 +92,6 @@ std = [ # `#[cfg(...)]` blocks easier to manage. "getrandom", - # Pull in `rand` for `ThreadRng`. - "dep:rand", - "aes-gcm/std", "aranya-buggy/std", "crypto-common/std", diff --git a/crates/aranya-crypto-core/src/csprng.rs b/crates/aranya-crypto-core/src/csprng.rs index 36b62ce9..cdd856c0 100644 --- a/crates/aranya-crypto-core/src/csprng.rs +++ b/crates/aranya-crypto-core/src/csprng.rs @@ -46,16 +46,16 @@ impl Csprng for &mut R { } } -#[cfg(feature = "getrandom")] -#[cfg_attr(docsrs, doc(cfg(feature = "getrandom")))] +#[cfg(all(feature = "getrandom", feature = "rand_compat"))] +#[cfg_attr(docsrs, doc(cfg(all(feature = "getrandom", feature = "rand_compat"))))] impl Csprng for rand_core::OsRng { fn fill_bytes(&mut self, dst: &mut [u8]) { rand_core::RngCore::fill_bytes(self, dst) } } -#[cfg(feature = "std")] -#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +#[cfg(all(feature = "rand_compat", feature = "std"))] +#[cfg_attr(docsrs, doc(cfg(all(feature = "rand_compat", feature = "std"))))] impl Csprng for rand::rngs::ThreadRng { fn fill_bytes(&mut self, dst: &mut [u8]) { rand_core::RngCore::fill_bytes(self, dst) diff --git a/crates/aranya-crypto-core/src/default.rs b/crates/aranya-crypto-core/src/default.rs index 07828f69..db641c85 100644 --- a/crates/aranya-crypto-core/src/default.rs +++ b/crates/aranya-crypto-core/src/default.rs @@ -9,14 +9,12 @@ use crate::csprng::Csprng; /// Certain feature flags will change the default CSPRNG: /// /// - `trng`: Uses a TRNG provided by the system. -/// - `std`: Uses a thread-local CSPRNG seeded from the system -/// CSPRNG. -/// - `libc`: Uses the system CSPRNG. +/// - `getrandom`: Uses the system CSPRNG. /// -/// The `libc` flag is enabled by default. +/// The `getrandom` flag is enabled by default. /// -/// If all of those feature flags are disabled, `Rng` invokes the -/// following routine: +/// If none of those feature flags are disabled, `Rng` invokes +/// the following routine: /// /// ``` /// extern "C" { @@ -56,9 +54,6 @@ impl Csprng for Rng { cfg_if! { if #[cfg(feature = "trng")] { crate::csprng::trng::thread_rng().fill_bytes(dst) - } else if #[cfg(feature = "std")] { - // Try to use `ThreadRng` if possible. - rand_core::RngCore::fill_bytes(&mut rand::thread_rng(), dst) } else if #[cfg(feature = "getrandom")] { getrandom::getrandom(dst).expect("should not fail") } else { @@ -75,9 +70,11 @@ impl Csprng for Rng { } #[cfg(feature = "rand_compat")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand_compat")))] impl rand_core::CryptoRng for Rng {} #[cfg(feature = "rand_compat")] +#[cfg_attr(docsrs, doc(cfg(feature = "rand_compat")))] impl rand_core::RngCore for Rng { fn next_u32(&mut self) -> u32 { rand_core::impls::next_u32_via_fill(self)