Skip to content

Commit a6a31aa

Browse files
committed
Deprecate TinyRng
1 parent 849e588 commit a6a31aa

7 files changed

Lines changed: 148 additions & 66 deletions

File tree

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@ der = { version = "0.8.0-rc.10", default-features = false }
2626
digest = { version = "0.11.0-rc.5", default-features = false }
2727
ecdsa = { version = "0.17.0-rc.12", default-features = false, features = ["alloc"] }
2828
ed25519-dalek = { version = "3.0.0-pre.4", default-features = false, features = ["pkcs8"] }
29-
getrandom = { version = "0.2", default-features = false, features = ["custom"] }
3029
hmac = { version = "0.13.0-rc.3", default-features = false }
3130
p256 = { version = "0.14.0-rc.4", default-features = false, features = ["pem", "ecdsa", "ecdh"] }
3231
p384 = { version = "0.14.0-rc.4", default-features = false, features = ["pem", "ecdsa", "ecdh"] }
3332
paste = { version = "1", default-features = false }
3433
pkcs8 = { version = "0.11.0-rc.8", default-features = false }
3534
pki-types = { package = "rustls-pki-types", version = "1", default-features = false }
36-
rand_core = { version = "0.10.0-rc-3", default-features = false }
35+
rand = { version = "=0.10.0-rc.6", default-features = false, features = ["sys_rng"] }
3736
rsa = { version = "0.10.0-rc.12", default-features = false, features = ["sha2", "encoding"] }
3837
rustls = { version = "0.23", default-features = false }
3938
sha2 = { version = "0.11.0-rc.3", default-features = false }

src/kx.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use alloc::boxed::Box;
44
use crypto::{SharedSecret, SupportedKxGroup};
55
use crypto_common::Generate;
66
use paste::paste;
7+
use rand::TryRngCore;
78
use rustls::crypto;
89

910
#[derive(Debug)]
@@ -15,7 +16,8 @@ impl crypto::SupportedKxGroup for X25519 {
1516
}
1617

1718
fn start(&self) -> Result<Box<dyn crypto::ActiveKeyExchange>, rustls::Error> {
18-
let priv_key = x25519_dalek::EphemeralSecret::random_from_rng(&mut crate::misc::TinyRng);
19+
let mut rng = rand::rngs::SysRng.unwrap_err();
20+
let priv_key = x25519_dalek::EphemeralSecret::random_from_rng(&mut rng);
1921
let pub_key = (&priv_key).into();
2022
Ok(Box::new(X25519KeyExchange { priv_key, pub_key }))
2123
}
@@ -61,7 +63,8 @@ macro_rules! impl_kx {
6163
}
6264

6365
fn start(&self) -> Result<Box<dyn crypto::ActiveKeyExchange>, rustls::Error> {
64-
let priv_key = <$secret>::try_generate_from_rng(&mut crate::misc::TinyRng).map_err(|_| rustls::Error::from(rustls::PeerMisbehaved::InvalidKeyShare))?;
66+
let mut rng = rand::rngs::SysRng.unwrap_err();
67+
let priv_key = <$secret>::try_generate_from_rng(&mut rng).map_err(|_| rustls::Error::from(rustls::PeerMisbehaved::InvalidKeyShare))?;
6568
let pub_key: $public_key = (&priv_key).into();
6669
Ok(Box::new([<$name KeyExchange>] {
6770
priv_key,

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extern crate alloc;
4141
#[cfg(feature = "alloc")]
4242
use alloc::sync::Arc;
4343

44+
use rand::TryRngCore;
4445
use rustls::crypto::{
4546
CipherSuiteCommon, CryptoProvider, GetRandomFailed, KeyProvider, SecureRandom,
4647
};
@@ -64,7 +65,8 @@ pub fn provider() -> CryptoProvider {
6465

6566
impl SecureRandom for Provider {
6667
fn fill(&self, bytes: &mut [u8]) -> Result<(), GetRandomFailed> {
67-
getrandom::getrandom(bytes).map_err(|_| GetRandomFailed)
68+
let mut rng = rand::rngs::SysRng.unwrap_err();
69+
rng.try_fill_bytes(bytes).map_err(|_| GetRandomFailed)
6870
}
6971
}
7072

src/misc.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,3 @@ macro_rules! const_concat_slices {
3838
}
3939

4040
pub(crate) use const_concat_slices;
41-
42-
/// A tiny RNG adapter that uses `getrandom` directly and implements the
43-
/// `rand_core::RngCore` and `rand_core::CryptoRng` traits so it can be used
44-
/// in places that expect those traits without pulling in the `rand` crate.
45-
pub struct TinyRng;
46-
47-
impl rand_core::CryptoRng for TinyRng {}
48-
49-
impl rand_core::RngCore for TinyRng {
50-
fn next_u32(&mut self) -> u32 {
51-
let mut b = [0u8; 4];
52-
self.fill_bytes(&mut b);
53-
u32::from_ne_bytes(b)
54-
}
55-
56-
fn next_u64(&mut self) -> u64 {
57-
let mut b = [0u8; 8];
58-
self.fill_bytes(&mut b);
59-
u64::from_ne_bytes(b)
60-
}
61-
62-
fn fill_bytes(&mut self, dst: &mut [u8]) {
63-
getrandom::getrandom(dst).expect("getrandom failure");
64-
}
65-
}

src/sign.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use self::eddsa::Ed25519SigningKey;
77
use self::rsa::RsaSigningKey;
88

99
use pki_types::PrivateKeyDer;
10+
use rand::TryRngCore;
1011
use rustls::sign::{Signer, SigningKey};
1112
use rustls::{Error, SignatureScheme};
1213
use signature::{RandomizedSigner, SignatureEncoding};
@@ -28,8 +29,9 @@ where
2829
T: RandomizedSigner<S> + Send + Sync + core::fmt::Debug,
2930
{
3031
fn sign(&self, message: &[u8]) -> Result<Vec<u8>, Error> {
32+
let mut rng = rand::rngs::SysRng.unwrap_err();
3133
self.key
32-
.try_sign_with_rng(&mut crate::misc::TinyRng, message)
34+
.try_sign_with_rng(&mut rng, message)
3335
.map_err(|_| rustls::Error::General("signing failed".into()))
3436
.map(|sig: S| sig.to_vec())
3537
}

0 commit comments

Comments
 (0)