diff --git a/Cargo.lock b/Cargo.lock index 97bb871c8..a8921a668 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -212,12 +212,12 @@ checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" [[package]] name = "getrandom" version = "0.3.4" -source = "git+https://github.com/rust-random/getrandom#658bb1a43cade84eb7aa63a3225cfa9d12b826b8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" dependencies = [ "cfg-if", "libc", "r-efi", - "rand_core", "wasip2", ] diff --git a/Cargo.toml b/Cargo.toml index 408b9a7f0..f89b4edce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,5 +16,3 @@ members = [ [patch.crates-io] digest = { path = "digest" } signature = { path = "signature" } - -getrandom = { git = "https://github.com/rust-random/getrandom" } diff --git a/crypto-common/Cargo.toml b/crypto-common/Cargo.toml index ce4535f61..b2055c93c 100644 --- a/crypto-common/Cargo.toml +++ b/crypto-common/Cargo.toml @@ -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] diff --git a/crypto-common/src/generate.rs b/crypto-common/src/generate.rs index bc3023289..2ba7f73c3 100644 --- a/crypto-common/src/generate.rs +++ b/crypto-common/src/generate.rs @@ -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::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 @@ -84,3 +84,35 @@ impl Generate for Array { 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 { + getrandom::u32() + } + + #[inline] + fn try_next_u64(&mut self) -> Result { + getrandom::u64() + } + + #[inline] + fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { + getrandom::fill(dest) + } + } + + impl TryCryptoRng for SysRng {} +}