Skip to content

Commit 09589dd

Browse files
authored
crypto-common: vendor getrandom::SysRng (#2135)
It's now been a month since #2096 was merged and there still has not been a corresponding `getrandom` release. This commit temporarily internally vendors `SysRng` (which has a very small implementation) until there is a `getrandom` crate release which is usable. This will unblock work on rolling out the `Generate` trait. Once there is, this commit can be reverted.
1 parent 6c316b3 commit 09589dd

4 files changed

Lines changed: 36 additions & 6 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,3 @@ members = [
1616
[patch.crates-io]
1717
digest = { path = "digest" }
1818
signature = { path = "signature" }
19-
20-
getrandom = { git = "https://github.com/rust-random/getrandom" }

crypto-common/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ description = "Common cryptographic traits"
1616
hybrid-array = "0.4"
1717

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

2222
[features]

crypto-common/src/generate.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub trait Generate: Sized {
2222
/// Returns [`RngError`] in the event the system's ambient RNG experiences an internal failure.
2323
#[cfg(feature = "getrandom")]
2424
fn try_generate() -> Result<Self, RngError> {
25-
Self::try_from_rng(&mut getrandom::SysRng)
25+
Self::try_from_rng(&mut sys_rng::SysRng)
2626
}
2727

2828
/// Randomly generate a value of this type using the system's ambient cryptographically secure
@@ -84,3 +84,35 @@ impl<U: ArraySize> Generate for Array<u64, U> {
8484
Self::try_from_fn(|_| rng.try_next_u64())
8585
}
8686
}
87+
88+
#[cfg(feature = "getrandom")]
89+
mod sys_rng {
90+
use getrandom::Error;
91+
use rand_core::{TryCryptoRng, TryRngCore};
92+
93+
/// A [`TryRngCore`] interface over the system's preferred random number source
94+
// TODO(tarcieri): replace this with `getrandom::SysRng` when `sys_rng` feature is available
95+
#[derive(Clone, Copy, Debug, Default)]
96+
pub struct SysRng;
97+
98+
impl TryRngCore for SysRng {
99+
type Error = Error;
100+
101+
#[inline]
102+
fn try_next_u32(&mut self) -> Result<u32, Error> {
103+
getrandom::u32()
104+
}
105+
106+
#[inline]
107+
fn try_next_u64(&mut self) -> Result<u64, Error> {
108+
getrandom::u64()
109+
}
110+
111+
#[inline]
112+
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
113+
getrandom::fill(dest)
114+
}
115+
}
116+
117+
impl TryCryptoRng for SysRng {}
118+
}

0 commit comments

Comments
 (0)