Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,3 @@ members = [
[patch.crates-io]
digest = { path = "digest" }
signature = { path = "signature" }

getrandom = { git = "https://github.com/rust-random/getrandom" }
2 changes: 1 addition & 1 deletion crypto-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ description = "Common cryptographic traits"
hybrid-array = "0.4"

# optional dependencies
getrandom = { version = "0.3", optional = true, features = ["sys_rng"] }
getrandom = { version = "0.3", optional = true }
rand_core = { version = "0.10.0-rc-3", optional = true }

[features]
Expand Down
34 changes: 33 additions & 1 deletion crypto-common/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub trait Generate: Sized {
/// Returns [`RngError`] in the event the system's ambient RNG experiences an internal failure.
#[cfg(feature = "getrandom")]
fn try_generate() -> Result<Self, RngError> {
Self::try_from_rng(&mut getrandom::SysRng)
Self::try_from_rng(&mut sys_rng::SysRng)
}

/// Randomly generate a value of this type using the system's ambient cryptographically secure
Expand Down Expand Up @@ -84,3 +84,35 @@ impl<U: ArraySize> Generate for Array<u64, U> {
Self::try_from_fn(|_| rng.try_next_u64())
}
}

#[cfg(feature = "getrandom")]
mod sys_rng {
use getrandom::Error;
use rand_core::{TryCryptoRng, TryRngCore};

/// A [`TryRngCore`] interface over the system's preferred random number source
// TODO(tarcieri): replace this with `getrandom::SysRng` when `sys_rng` feature is available
#[derive(Clone, Copy, Debug, Default)]
pub struct SysRng;

impl TryRngCore for SysRng {
type Error = Error;

#[inline]
fn try_next_u32(&mut self) -> Result<u32, Error> {
getrandom::u32()
}

#[inline]
fn try_next_u64(&mut self) -> Result<u64, Error> {
getrandom::u64()
}

#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
getrandom::fill(dest)
}
}

impl TryCryptoRng for SysRng {}
}