Skip to content

Commit

Permalink
crypto: get rid of ThreadRng
Browse files Browse the repository at this point in the history
Fixes #43

Signed-off-by: Eric Lagergren <[email protected]>
  • Loading branch information
elagergren-spideroak committed Jan 7, 2025
1 parent 6522a44 commit 99fea54
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
4 changes: 0 additions & 4 deletions crates/aranya-crypto-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ workspace = true
[features]
default = [
"getrandom",
#"trng",
]

# Enable allocations.
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions crates/aranya-crypto-core/src/csprng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ impl<R: Csprng + ?Sized> 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)
Expand Down
15 changes: 6 additions & 9 deletions crates/aranya-crypto-core/src/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 99fea54

Please sign in to comment.