From a99efe2304429b9284c495735d4fda9f49dcf5b8 Mon Sep 17 00:00:00 2001 From: Arthur Gautier Date: Mon, 7 Jul 2025 21:36:11 +0200 Subject: [PATCH] curve,ed,x: Bump rand_core to 0.9 (#777) --------- Co-authored-by: pinkforest <36498018+pinkforest@users.noreply.github.com> --- .github/workflows/workspace.yml | 2 +- curve25519-dalek/Cargo.toml | 10 +-- curve25519-dalek/benches/dalek_benchmarks.rs | 18 ++--- curve25519-dalek/src/edwards.rs | 38 ++++++---- curve25519-dalek/src/montgomery.rs | 18 ++--- curve25519-dalek/src/ristretto.rs | 78 +++++++++++++------- curve25519-dalek/src/scalar.rs | 26 +++---- ed25519-dalek/Cargo.toml | 8 +- ed25519-dalek/src/batch.rs | 8 +- ed25519-dalek/src/batch/transcript.rs | 5 -- ed25519-dalek/src/context.rs | 9 ++- ed25519-dalek/src/hazmat.rs | 16 ++-- ed25519-dalek/src/lib.rs | 28 ++++--- ed25519-dalek/src/signing.rs | 16 ++-- ed25519-dalek/tests/ed25519.rs | 17 +++-- x25519-dalek/Cargo.toml | 8 +- x25519-dalek/README.md | 21 +++--- x25519-dalek/benches/x25519.rs | 6 +- x25519-dalek/src/x25519.rs | 40 +++++----- x25519-dalek/tests/x25519_tests.rs | 18 ++--- 20 files changed, 217 insertions(+), 173 deletions(-) diff --git a/.github/workflows/workspace.yml b/.github/workflows/workspace.yml index cf5c47e..dc5ac8d 100644 --- a/.github/workflows/workspace.yml +++ b/.github/workflows/workspace.yml @@ -78,7 +78,7 @@ jobs: - name: no_std / no feat ${{ matrix.crate }} run: cargo build -p ${{ matrix.crate }} --target thumbv7em-none-eabi --release --no-default-features - name: no_std / cargo hack ${{ matrix.crate }} - run: cargo hack build -p ${{ matrix.crate }} --target thumbv7em-none-eabi --release --each-feature --exclude-features default,std,getrandom + run: cargo hack build -p ${{ matrix.crate }} --target thumbv7em-none-eabi --release --each-feature --exclude-features default,std,os_rng clippy: name: Check that clippy is happy diff --git a/curve25519-dalek/Cargo.toml b/curve25519-dalek/Cargo.toml index b785725..736be7b 100644 --- a/curve25519-dalek/Cargo.toml +++ b/curve25519-dalek/Cargo.toml @@ -34,8 +34,8 @@ sha2 = { version = "0.11.0-rc.0", default-features = false } bincode = "1" criterion = { version = "0.5", features = ["html_reports"] } hex = "0.4.2" -rand = "0.8" -rand_core = { version = "0.6", default-features = false, features = ["getrandom"] } +rand = "0.9" +rand_core = { version = "0.9", default-features = false, features = ["os_rng"] } [build-dependencies] rustc_version = "0.4.0" @@ -47,9 +47,9 @@ required-features = ["alloc", "rand_core"] [dependencies] cfg-if = "1" -ff = { version = "0.13", default-features = false, optional = true } -group = { version = "0.13", default-features = false, optional = true } -rand_core = { version = "0.6.4", default-features = false, optional = true } +ff = { version = "=0.14.0-pre.0", default-features = false, optional = true } +group = { version = "=0.14.0-pre.0", default-features = false, optional = true } +rand_core = { version = "0.9", default-features = false, optional = true } digest = { version = "0.11.0-rc.0", default-features = false, optional = true, features = ["block-api"] } subtle = { version = "2.6.0", default-features = false, features = ["const-generics"] } serde = { version = "1.0", default-features = false, optional = true, features = ["derive"] } diff --git a/curve25519-dalek/benches/dalek_benchmarks.rs b/curve25519-dalek/benches/dalek_benchmarks.rs index 72a326e..47386b2 100644 --- a/curve25519-dalek/benches/dalek_benchmarks.rs +++ b/curve25519-dalek/benches/dalek_benchmarks.rs @@ -1,6 +1,6 @@ #![allow(non_snake_case)] -use rand::{RngCore, rngs::OsRng, thread_rng}; +use rand::{RngCore, TryRngCore, rng, rngs::OsRng}; use criterion::{ BatchSize, BenchmarkGroup, BenchmarkId, Criterion, criterion_main, measurement::Measurement, @@ -31,7 +31,7 @@ mod edwards_benches { BenchmarkId::new("Batch EdwardsPoint compression", batch_size), &batch_size, |b, &size| { - let mut rng = OsRng; + let mut rng = OsRng.unwrap_err(); let points: Vec = (0..size).map(|_| EdwardsPoint::random(&mut rng)).collect(); b.iter(|| EdwardsPoint::compress_batch(&points)); @@ -64,7 +64,7 @@ mod edwards_benches { fn vartime_double_base_scalar_mul(c: &mut BenchmarkGroup) { c.bench_function("Variable-time aA+bB, A variable, B fixed", |bench| { - let mut rng = thread_rng(); + let mut rng = rng(); let A = EdwardsPoint::mul_base(&Scalar::random(&mut rng)); bench.iter_batched( || (Scalar::random(&mut rng), Scalar::random(&mut rng)), @@ -76,7 +76,7 @@ mod edwards_benches { #[cfg(feature = "digest")] fn hash_to_curve(c: &mut BenchmarkGroup) { - let mut rng = thread_rng(); + let mut rng = rng(); let mut msg = [0u8; 32]; let mut domain_sep = [0u8; 32]; @@ -114,12 +114,12 @@ mod multiscalar_benches { use curve25519_dalek::traits::VartimePrecomputedMultiscalarMul; fn construct_scalars(n: usize) -> Vec { - let mut rng = thread_rng(); + let mut rng = rng(); (0..n).map(|_| Scalar::random(&mut rng)).collect() } fn construct_points(n: usize) -> Vec { - let mut rng = thread_rng(); + let mut rng = rng(); (0..n) .map(|_| EdwardsPoint::mul_base(&Scalar::random(&mut rng))) .collect() @@ -287,7 +287,7 @@ mod ristretto_benches { |b, &&size| { let mut rng = OsRng; let points: Vec = (0..size) - .map(|_| RistrettoPoint::random(&mut rng)) + .map(|_| RistrettoPoint::try_from_rng(&mut rng).unwrap()) .collect(); b.iter(|| RistrettoPoint::double_and_compress_batch(&points)); }, @@ -337,7 +337,7 @@ mod scalar_benches { use super::*; fn scalar_arith(c: &mut BenchmarkGroup) { - let mut rng = thread_rng(); + let mut rng = rng(); c.bench_function("Scalar inversion", |b| { let s = Scalar::from(897987897u64).invert(); @@ -372,7 +372,7 @@ mod scalar_benches { BenchmarkId::new("Batch scalar inversion", *batch_size), &batch_size, |b, &&size| { - let mut rng = OsRng; + let mut rng = OsRng.unwrap_err(); let scalars: Vec = (0..size).map(|_| Scalar::random(&mut rng)).collect(); b.iter(|| { diff --git a/curve25519-dalek/src/edwards.rs b/curve25519-dalek/src/edwards.rs index d1f0089..f5778f0 100644 --- a/curve25519-dalek/src/edwards.rs +++ b/curve25519-dalek/src/edwards.rs @@ -113,6 +113,7 @@ use digest::{ #[cfg(feature = "group")] use { group::{GroupEncoding, cofactor::CofactorGroup, prime::PrimeGroup}, + rand_core::TryRngCore, subtle::CtOption, }; @@ -722,7 +723,7 @@ impl EdwardsPoint { /// Uses rejection sampling, generating a random `CompressedEdwardsY` and then attempting point /// decompression, rejecting invalid points. #[cfg(any(test, feature = "rand_core"))] - pub fn random(mut rng: impl RngCore) -> Self { + pub fn random(rng: &mut R) -> Self { let mut repr = CompressedEdwardsY([0u8; 32]); loop { rng.fill_bytes(&mut repr.0); @@ -1419,9 +1420,16 @@ impl Debug for EdwardsPoint { impl group::Group for EdwardsPoint { type Scalar = Scalar; - fn random(rng: impl RngCore) -> Self { - // Call the inherent `pub fn random` defined above - Self::random(rng) + fn try_from_rng(rng: &mut R) -> Result { + let mut repr = CompressedEdwardsY([0u8; 32]); + loop { + rng.try_fill_bytes(&mut repr.0)?; + if let Some(p) = repr.decompress() { + if !IsIdentity::is_identity(&p) { + break Ok(p); + } + } + } } fn identity() -> Self { @@ -1664,20 +1672,20 @@ impl Zeroize for SubgroupPoint { impl group::Group for SubgroupPoint { type Scalar = Scalar; - fn random(mut rng: impl RngCore) -> Self { + fn try_from_rng(rng: &mut R) -> Result { use group::ff::Field; // This will almost never loop, but `Group::random` is documented as returning a // non-identity element. let s = loop { - let s: Scalar = Field::random(&mut rng); + let s: Scalar = Field::try_from_rng(rng)?; if !s.is_zero_vartime() { break s; } }; // This gives an element of the prime-order subgroup. - Self::generator() * s + Ok(Self::generator() * s) } fn identity() -> Self { @@ -1743,9 +1751,7 @@ impl CofactorGroup for EdwardsPoint { mod test { use super::*; - // If `group` is set, then this is already imported in super - #[cfg(not(feature = "group"))] - use rand_core::RngCore; + use rand_core::TryRngCore; #[cfg(feature = "alloc")] use alloc::vec::Vec; @@ -2040,7 +2046,7 @@ mod test { #[cfg(feature = "precomputed-tables")] let random_point = { let mut b = [0u8; 32]; - csprng.fill_bytes(&mut b); + csprng.try_fill_bytes(&mut b).unwrap(); EdwardsPoint::mul_base_clamped(b) + constants::EIGHT_TORSION[1] }; // Make a basepoint table from the random point. We'll use this with mul_base_clamped @@ -2066,7 +2072,7 @@ mod test { for _ in 0..100 { // This will be reduced mod l with probability l / 2^256 ≈ 6.25% let mut a_bytes = [0u8; 32]; - csprng.fill_bytes(&mut a_bytes); + csprng.try_fill_bytes(&mut a_bytes).unwrap(); assert_eq!( EdwardsPoint::mul_base_clamped(a_bytes), @@ -2151,7 +2157,7 @@ mod test { #[cfg(feature = "alloc")] #[test] fn compress_batch() { - let mut rng = rand::thread_rng(); + let mut rng = rand::rng(); // TODO(tarcieri): proptests? // Make some points deterministically then randomly @@ -2207,7 +2213,7 @@ mod test { // A single iteration of a consistency check for MSM. #[cfg(feature = "alloc")] fn multiscalar_consistency_iter(n: usize) { - let mut rng = rand::thread_rng(); + let mut rng = rand::rng(); // Construct random coefficients x0, ..., x_{n-1}, // followed by some extra hardcoded ones. @@ -2270,7 +2276,7 @@ mod test { #[test] #[cfg(feature = "alloc")] fn batch_to_montgomery() { - let mut rng = rand::thread_rng(); + let mut rng = rand::rng(); let scalars = (0..128) .map(|_| Scalar::random(&mut rng)) @@ -2295,7 +2301,7 @@ mod test { #[test] #[cfg(feature = "alloc")] fn vartime_precomputed_vs_nonprecomputed_multiscalar() { - let mut rng = rand::thread_rng(); + let mut rng = rand::rng(); let static_scalars = (0..128) .map(|_| Scalar::random(&mut rng)) diff --git a/curve25519-dalek/src/montgomery.rs b/curve25519-dalek/src/montgomery.rs index 577d9f6..8f6fa4a 100644 --- a/curve25519-dalek/src/montgomery.rs +++ b/curve25519-dalek/src/montgomery.rs @@ -437,7 +437,7 @@ mod test { #[cfg(feature = "alloc")] use alloc::vec::Vec; - use rand_core::{CryptoRng, RngCore}; + use rand_core::{CryptoRng, RngCore, TryRngCore}; #[test] fn identity_in_different_coordinates() { @@ -521,8 +521,8 @@ mod test { } /// Returns a random point on the prime-order subgroup - fn rand_prime_order_point(mut rng: impl RngCore + CryptoRng) -> EdwardsPoint { - let s: Scalar = Scalar::random(&mut rng); + fn rand_prime_order_point(rng: &mut R) -> EdwardsPoint { + let s: Scalar = Scalar::random(rng); EdwardsPoint::mul_base(&s) } @@ -540,10 +540,10 @@ mod test { #[test] fn montgomery_ladder_matches_edwards_scalarmult() { - let mut csprng = rand_core::OsRng; + let mut csprng = rand_core::OsRng.unwrap_err(); for _ in 0..100 { - let p_edwards = rand_prime_order_point(csprng); + let p_edwards = rand_prime_order_point(&mut csprng); let p_montgomery: MontgomeryPoint = p_edwards.to_montgomery(); let s: Scalar = Scalar::random(&mut csprng); @@ -558,11 +558,11 @@ mod test { // multiplying by the Scalar representation of the same bits #[test] fn montgomery_mul_bits_be() { - let mut csprng = rand_core::OsRng; + let mut csprng = rand_core::OsRng.unwrap_err(); for _ in 0..100 { // Make a random prime-order point P - let p_edwards = rand_prime_order_point(csprng); + let p_edwards = rand_prime_order_point(&mut csprng); let p_montgomery: MontgomeryPoint = p_edwards.to_montgomery(); // Make a random integer b @@ -583,7 +583,7 @@ mod test { // integers b₁, b₂ and random (curve or twist) point P. #[test] fn montgomery_mul_bits_be_twist() { - let mut csprng = rand_core::OsRng; + let mut csprng = rand_core::OsRng.unwrap_err(); for _ in 0..100 { // Make a random point P on the curve or its twist @@ -629,7 +629,7 @@ mod test { for _ in 0..100 { // This will be reduced mod l with probability l / 2^256 ≈ 6.25% let mut a_bytes = [0u8; 32]; - csprng.fill_bytes(&mut a_bytes); + csprng.try_fill_bytes(&mut a_bytes).unwrap(); assert_eq!( MontgomeryPoint::mul_base_clamped(a_bytes), diff --git a/curve25519-dalek/src/ristretto.rs b/curve25519-dalek/src/ristretto.rs index c7446d9..9c71728 100644 --- a/curve25519-dalek/src/ristretto.rs +++ b/curve25519-dalek/src/ristretto.rs @@ -169,9 +169,6 @@ use core::ops::{Add, Neg, Sub}; use core::ops::{AddAssign, SubAssign}; use core::ops::{Mul, MulAssign}; -#[cfg(any(test, feature = "rand_core"))] -use rand_core::CryptoRngCore; - #[cfg(feature = "digest")] use digest::Digest; #[cfg(feature = "digest")] @@ -183,10 +180,16 @@ use crate::field::FieldElement; #[cfg(feature = "group")] use { group::{GroupEncoding, cofactor::CofactorGroup, prime::PrimeGroup}, - rand_core::RngCore, + rand_core::TryRngCore, subtle::CtOption, }; +#[cfg(any(test, feature = "rand_core"))] +use { + core::convert::Infallible, + rand_core::{CryptoRng, TryCryptoRng}, +}; + use subtle::Choice; use subtle::ConditionallyNegatable; use subtle::ConditionallySelectable; @@ -532,12 +535,12 @@ impl RistrettoPoint { #[cfg_attr(feature = "rand_core", doc = "```")] #[cfg_attr(not(feature = "rand_core"), doc = "```ignore")] /// # use curve25519_dalek::ristretto::RistrettoPoint; - /// use rand_core::OsRng; + /// use rand_core::{OsRng, TryRngCore}; /// /// # // Need fn main() here in comment so the doctest compiles /// # // See https://doc.rust-lang.org/book/documentation.html#documentation-as-tests /// # fn main() { - /// let mut rng = OsRng; + /// let mut rng = OsRng.unwrap_err(); /// /// let points: Vec = /// (0..32).map(|_| RistrettoPoint::random(&mut rng)).collect(); @@ -696,8 +699,7 @@ impl RistrettoPoint { /// /// # Inputs /// - /// * `rng`: any RNG which implements `CryptoRngCore` - /// (i.e. `CryptoRng` + `RngCore`) interface. + /// * `rng`: any RNG which implements `CryptoRng` interface. /// /// # Returns /// @@ -709,11 +711,34 @@ impl RistrettoPoint { /// discrete log of the output point with respect to any other /// point should be unknown. The map is applied twice and the /// results are added, to ensure a uniform distribution. - pub fn random(rng: &mut R) -> Self { - let mut uniform_bytes = [0u8; 64]; - rng.fill_bytes(&mut uniform_bytes); + pub fn random(rng: &mut R) -> Self { + Self::try_from_rng(rng) + .map_err(|_: Infallible| {}) + .expect("[bug] unfallible rng failed") + } - RistrettoPoint::from_uniform_bytes(&uniform_bytes) + #[cfg(any(test, feature = "rand_core"))] + /// Return a `RistrettoPoint` chosen uniformly at random using a user-provided RNG. + /// + /// # Inputs + /// + /// * `rng`: any RNG which implements `TryCryptoRng` interface. + /// + /// # Returns + /// + /// A random element of the Ristretto group. + /// + /// # Implementation + /// + /// Uses the Ristretto-flavoured Elligator 2 map, so that the + /// discrete log of the output point with respect to any other + /// point should be unknown. The map is applied twice and the + /// results are added, to ensure a uniform distribution. + pub fn try_from_rng(rng: &mut R) -> Result { + let mut uniform_bytes = [0u8; 64]; + rng.try_fill_bytes(&mut uniform_bytes)?; + + Ok(RistrettoPoint::from_uniform_bytes(&uniform_bytes)) } #[cfg(feature = "digest")] @@ -1192,11 +1217,11 @@ impl Debug for RistrettoPoint { impl group::Group for RistrettoPoint { type Scalar = Scalar; - fn random(mut rng: impl RngCore) -> Self { + fn try_from_rng(rng: &mut R) -> Result { // NOTE: this is duplicated due to different `rng` bounds let mut uniform_bytes = [0u8; 64]; - rng.fill_bytes(&mut uniform_bytes); - RistrettoPoint::from_uniform_bytes(&uniform_bytes) + rng.try_fill_bytes(&mut uniform_bytes)?; + Ok(RistrettoPoint::from_uniform_bytes(&uniform_bytes)) } fn identity() -> Self { @@ -1289,7 +1314,7 @@ mod test { use super::*; use crate::edwards::CompressedEdwardsY; - use rand_core::OsRng; + use rand_core::{OsRng, TryRngCore}; #[test] #[cfg(feature = "serde")] @@ -1482,7 +1507,7 @@ mod test { #[test] fn four_torsion_random() { - let mut rng = OsRng; + let mut rng = OsRng.unwrap_err(); let P = RistrettoPoint::mul_base(&Scalar::random(&mut rng)); let P_coset = P.coset4(); for point in P_coset { @@ -1807,7 +1832,7 @@ mod test { #[test] fn random_roundtrip() { - let mut rng = OsRng; + let mut rng = OsRng.unwrap_err(); for _ in 0..100 { let P = RistrettoPoint::mul_base(&Scalar::random(&mut rng)); let compressed_P = P.compress(); @@ -1817,14 +1842,15 @@ mod test { } #[test] - #[cfg(all(feature = "alloc", feature = "rand_core"))] + #[cfg(all(feature = "alloc", feature = "rand_core", feature = "group"))] fn double_and_compress_1024_random_points() { + use group::Group; let mut rng = OsRng; let mut points: Vec = (0..1024) - .map(|_| RistrettoPoint::random(&mut rng)) + .map(|_| RistrettoPoint::try_from_rng(&mut rng).unwrap()) .collect(); - points[500] = RistrettoPoint::identity(); + points[500] = ::identity(); let compressed = RistrettoPoint::double_and_compress_batch(&points); @@ -1836,7 +1862,7 @@ mod test { #[test] #[cfg(feature = "alloc")] fn vartime_precomputed_vs_nonprecomputed_multiscalar() { - let mut rng = rand::thread_rng(); + let mut rng = rand::rng(); let static_scalars = (0..128) .map(|_| Scalar::random(&mut rng)) @@ -1887,7 +1913,7 @@ mod test { #[test] #[cfg(feature = "alloc")] fn partial_precomputed_mixed_multiscalar_empty() { - let mut rng = rand::thread_rng(); + let mut rng = rand::rng(); let n_static = 16; let n_dynamic = 8; @@ -1930,7 +1956,7 @@ mod test { #[test] #[cfg(feature = "alloc")] fn partial_precomputed_mixed_multiscalar() { - let mut rng = rand::thread_rng(); + let mut rng = rand::rng(); let n_static = 16; let n_dynamic = 8; @@ -1975,7 +2001,7 @@ mod test { #[test] #[cfg(feature = "alloc")] fn partial_precomputed_multiscalar() { - let mut rng = rand::thread_rng(); + let mut rng = rand::rng(); let n_static = 16; @@ -2004,7 +2030,7 @@ mod test { #[test] #[cfg(feature = "alloc")] fn partial_precomputed_multiscalar_empty() { - let mut rng = rand::thread_rng(); + let mut rng = rand::rng(); let n_static = 16; diff --git a/curve25519-dalek/src/scalar.rs b/curve25519-dalek/src/scalar.rs index 8b61690..3a3e585 100644 --- a/curve25519-dalek/src/scalar.rs +++ b/curve25519-dalek/src/scalar.rs @@ -127,11 +127,11 @@ use group::ff::{Field, FromUniformBytes, PrimeField}; #[cfg(feature = "group-bits")] use group::ff::{FieldBits, PrimeFieldBits}; -#[cfg(any(test, feature = "group"))] -use rand_core::RngCore; +#[cfg(feature = "group")] +use rand_core::TryRngCore; #[cfg(any(test, feature = "rand_core"))] -use rand_core::CryptoRngCore; +use rand_core::CryptoRng; #[cfg(feature = "digest")] use digest::Digest; @@ -576,8 +576,7 @@ impl Scalar { /// /// # Inputs /// - /// * `rng`: any RNG which implements `CryptoRngCore` - /// (i.e. `CryptoRng` + `RngCore`) interface. + /// * `rng`: any RNG which implements `CryptoRng` interface. /// /// # Returns /// @@ -589,12 +588,12 @@ impl Scalar { /// # fn main() { /// use curve25519_dalek::scalar::Scalar; /// - /// use rand_core::OsRng; + /// use rand_core::{OsRng, TryRngCore}; /// - /// let mut csprng = OsRng; + /// let mut csprng = OsRng.unwrap_err(); /// let a: Scalar = Scalar::random(&mut csprng); /// # } - pub fn random(rng: &mut R) -> Self { + pub fn random(rng: &mut R) -> Self { let mut scalar_bytes = [0u8; 64]; rng.fill_bytes(&mut scalar_bytes); Scalar::from_bytes_mod_order_wide(&scalar_bytes) @@ -1213,11 +1212,11 @@ impl Field for Scalar { const ZERO: Self = Self::ZERO; const ONE: Self = Self::ONE; - fn random(mut rng: impl RngCore) -> Self { + fn try_from_rng(rng: &mut R) -> Result { // NOTE: this is duplicated due to different `rng` bounds let mut scalar_bytes = [0u8; 64]; - rng.fill_bytes(&mut scalar_bytes); - Self::from_bytes_mod_order_wide(&scalar_bytes) + rng.try_fill_bytes(&mut scalar_bytes)?; + Ok(Self::from_bytes_mod_order_wide(&scalar_bytes)) } fn square(&self) -> Self { @@ -1393,6 +1392,7 @@ pub const fn clamp_integer(mut bytes: [u8; 32]) -> [u8; 32] { #[cfg(test)] pub(crate) mod test { use super::*; + use rand_core::RngCore; #[cfg(feature = "alloc")] use alloc::vec::Vec; @@ -1552,7 +1552,7 @@ pub(crate) mod test { #[test] fn non_adjacent_form_random() { - let mut rng = rand::thread_rng(); + let mut rng = rand::rng(); for _ in 0..1_000 { let x = Scalar::random(&mut rng); for w in &[5, 6, 7, 8] { @@ -2046,7 +2046,7 @@ pub(crate) mod test { // was reduced and b was clamped and unreduced. This checks that was always well-defined. #[test] fn test_mul_reduction_invariance() { - let mut rng = rand::thread_rng(); + let mut rng = rand::rng(); for _ in 0..10 { // Also define c that's clamped. We'll make sure that clamping doesn't affect diff --git a/ed25519-dalek/Cargo.toml b/ed25519-dalek/Cargo.toml index be1928c..4d31ff5 100644 --- a/ed25519-dalek/Cargo.toml +++ b/ed25519-dalek/Cargo.toml @@ -34,7 +34,7 @@ subtle = { version = "2.3.0", default-features = false } # optional features keccak = { version = "0.1", default-features = false, optional = true } -rand_core = { version = "0.6.4", default-features = false, optional = true } +rand_core = { version = "0.9", default-features = false, optional = true } serde = { version = "1.0", default-features = false, optional = true } zeroize = { version = "1.5", default-features = false, optional = true } @@ -48,9 +48,9 @@ bincode = "1.0" serde_json = "1.0" criterion = { version = "0.5", features = ["html_reports"] } hex-literal = "0.4" -rand = "0.8" -rand_core = { version = "0.6.4", default-features = false } -rand_chacha = "0.3.1" +rand = "0.9" +rand_core = { version = "0.9", default-features = false } +rand_chacha = "0.9" serde = { version = "1.0", features = ["derive"] } strobe-rs = "0.5" toml = { version = "0.7" } diff --git a/ed25519-dalek/src/batch.rs b/ed25519-dalek/src/batch.rs index 4e1decb..dc8a5d1 100644 --- a/ed25519-dalek/src/batch.rs +++ b/ed25519-dalek/src/batch.rs @@ -66,11 +66,6 @@ impl rand_core::RngCore for ZeroRng { /// `ENC_{state}(00000000000000000000000000000000)` operation, which is /// identical to the STROBE `MAC` operation. fn fill_bytes(&mut self, _dest: &mut [u8]) {} - - fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> { - self.fill_bytes(dest); - Ok(()) - } } // `TranscriptRngBuilder::finalize()` requires a `CryptoRng` @@ -130,9 +125,10 @@ fn gen_u128(rng: &mut R) -> u128 { /// verify_batch, SigningKey, VerifyingKey, Signer, Signature, /// }; /// use rand::rngs::OsRng; +/// use rand_core::TryRngCore; /// /// # fn main() { -/// let mut csprng = OsRng; +/// let mut csprng = OsRng.unwrap_err(); /// let signing_keys: Vec<_> = (0..64).map(|_| SigningKey::generate(&mut csprng)).collect(); /// let msg: &[u8] = b"They're good dogs Brant"; /// let messages: Vec<_> = (0..64).map(|_| msg).collect(); diff --git a/ed25519-dalek/src/batch/transcript.rs b/ed25519-dalek/src/batch/transcript.rs index d3a4c95..1035c99 100644 --- a/ed25519-dalek/src/batch/transcript.rs +++ b/ed25519-dalek/src/batch/transcript.rs @@ -199,11 +199,6 @@ impl rand_core::RngCore for TranscriptRng { self.strobe.meta_ad(&dest_len, false); self.strobe.prf(dest, false); } - - fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> { - self.fill_bytes(dest); - Ok(()) - } } impl rand_core::CryptoRng for TranscriptRng {} diff --git a/ed25519-dalek/src/context.rs b/ed25519-dalek/src/context.rs index 2a27edd..8ef18dc 100644 --- a/ed25519-dalek/src/context.rs +++ b/ed25519-dalek/src/context.rs @@ -24,9 +24,10 @@ use crate::{InternalError, SignatureError}; /// use ed25519_dalek::{Signature, SigningKey, VerifyingKey, Sha512}; /// # use curve25519_dalek::digest::Digest; /// # use rand::rngs::OsRng; +/// # use rand_core::TryRngCore; /// use ed25519_dalek::{DigestSigner, DigestVerifier}; /// -/// # let mut csprng = OsRng; +/// # let mut csprng = OsRng.unwrap_err(); /// # let signing_key = SigningKey::generate(&mut csprng); /// # let verifying_key = signing_key.verifying_key(); /// let context_str = b"Local Channel 3"; @@ -84,13 +85,13 @@ mod test { use crate::{Signature, SigningKey, VerifyingKey}; use curve25519_dalek::digest::Digest; - use ed25519::signature::{DigestSigner, DigestVerifier}; - use rand::rngs::OsRng; + use rand::{TryRngCore, rngs::OsRng}; use sha2::Sha512; + use signature::{DigestSigner, DigestVerifier}; #[test] fn context_correctness() { - let mut csprng = OsRng; + let mut csprng = OsRng.unwrap_err(); let signing_key: SigningKey = SigningKey::generate(&mut csprng); let verifying_key: VerifyingKey = signing_key.verifying_key(); diff --git a/ed25519-dalek/src/hazmat.rs b/ed25519-dalek/src/hazmat.rs index fd463b5..ce53f43 100644 --- a/ed25519-dalek/src/hazmat.rs +++ b/ed25519-dalek/src/hazmat.rs @@ -258,7 +258,7 @@ mod test { use super::*; - use rand::{CryptoRng, RngCore, rngs::OsRng}; + use rand::{CryptoRng, TryRngCore, rngs::OsRng}; // Pick distinct, non-spec 512-bit hash functions for message and sig-context hashing type CtxDigest = blake2::Blake2b512; @@ -267,7 +267,7 @@ mod test { impl ExpandedSecretKey { // Make a random expanded secret key for testing purposes. This is NOT how you generate // expanded secret keys IRL. They're the hash of a seed. - fn random(mut rng: R) -> Self { + fn random(rng: &mut R) -> Self { let mut bytes = [0u8; 64]; rng.fill_bytes(&mut bytes); ExpandedSecretKey::from_bytes(&bytes) @@ -278,8 +278,8 @@ mod test { #[test] fn sign_verify_nonspec() { // Generate the keypair - let rng = OsRng; - let esk = ExpandedSecretKey::random(rng); + let mut rng = OsRng.unwrap_err(); + let esk = ExpandedSecretKey::random(&mut rng); let vk = VerifyingKey::from(&esk); let msg = b"Then one day, a piano fell on my head"; @@ -297,8 +297,8 @@ mod test { use curve25519_dalek::digest::Digest; // Generate the keypair - let rng = OsRng; - let esk = ExpandedSecretKey::random(rng); + let mut rng = OsRng.unwrap_err(); + let esk = ExpandedSecretKey::random(&mut rng); let vk = VerifyingKey::from(&esk); // Hash the message @@ -317,8 +317,8 @@ mod test { #[test] fn sign_byupdate() { // Generate the keypair - let rng = OsRng; - let esk = ExpandedSecretKey::random(rng); + let mut rng = OsRng.unwrap_err(); + let esk = ExpandedSecretKey::random(&mut rng); let vk = VerifyingKey::from(&esk); let msg = b"realistic"; diff --git a/ed25519-dalek/src/lib.rs b/ed25519-dalek/src/lib.rs index 436c215..d728d74 100644 --- a/ed25519-dalek/src/lib.rs +++ b/ed25519-dalek/src/lib.rs @@ -23,10 +23,11 @@ //! # fn main() { //! // $ cargo add ed25519_dalek --features rand_core //! use rand::rngs::OsRng; +//! use rand_core::TryRngCore; //! use ed25519_dalek::SigningKey; //! use ed25519_dalek::Signature; //! -//! let mut csprng = OsRng; +//! let mut csprng = OsRng.unwrap_err(); //! let signing_key: SigningKey = SigningKey::generate(&mut csprng); //! # } //! ``` @@ -37,8 +38,9 @@ #![cfg_attr(not(feature = "rand_core"), doc = "```ignore")] //! # fn main() { //! # use rand::rngs::OsRng; +//! # use rand_core::TryRngCore; //! # use ed25519_dalek::SigningKey; -//! # let mut csprng = OsRng; +//! # let mut csprng = OsRng.unwrap_err(); //! # let signing_key: SigningKey = SigningKey::generate(&mut csprng); //! use ed25519_dalek::{Signature, Signer}; //! let message: &[u8] = b"This is a test of the tsunami alert system."; @@ -53,8 +55,9 @@ #![cfg_attr(not(feature = "rand_core"), doc = "```ignore")] //! # fn main() { //! # use rand::rngs::OsRng; +//! # use rand_core::TryRngCore; //! # use ed25519_dalek::{SigningKey, Signature, Signer}; -//! # let mut csprng = OsRng; +//! # let mut csprng = OsRng.unwrap_err(); //! # let signing_key: SigningKey = SigningKey::generate(&mut csprng); //! # let message: &[u8] = b"This is a test of the tsunami alert system."; //! # let signature: Signature = signing_key.sign(message); @@ -70,11 +73,12 @@ #![cfg_attr(not(feature = "rand_core"), doc = "```ignore")] //! # fn main() { //! # use rand::rngs::OsRng; +//! # use rand_core::TryRngCore; //! # use ed25519_dalek::SigningKey; //! # use ed25519_dalek::Signature; //! # use ed25519_dalek::Signer; //! use ed25519_dalek::{VerifyingKey, Verifier}; -//! # let mut csprng = OsRng; +//! # let mut csprng = OsRng.unwrap_err(); //! # let signing_key: SigningKey = SigningKey::generate(&mut csprng); //! # let message: &[u8] = b"This is a test of the tsunami alert system."; //! # let signature: Signature = signing_key.sign(message); @@ -96,9 +100,10 @@ #![cfg_attr(not(feature = "rand_core"), doc = "```ignore")] //! # fn main() { //! # use rand::rngs::OsRng; +//! # use rand_core::TryRngCore; //! # use ed25519_dalek::{SigningKey, Signature, Signer, VerifyingKey}; //! use ed25519_dalek::{PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH, KEYPAIR_LENGTH, SIGNATURE_LENGTH}; -//! # let mut csprng = OsRng; +//! # let mut csprng = OsRng.unwrap_err(); //! # let signing_key: SigningKey = SigningKey::generate(&mut csprng); //! # let message: &[u8] = b"This is a test of the tsunami alert system."; //! # let signature: Signature = signing_key.sign(message); @@ -116,10 +121,11 @@ #![cfg_attr(not(feature = "rand_core"), doc = "```ignore")] //! # use core::convert::{TryFrom, TryInto}; //! # use rand::rngs::OsRng; +//! # use rand_core::TryRngCore; //! # use ed25519_dalek::{SigningKey, Signature, Signer, VerifyingKey, SecretKey, SignatureError}; //! # use ed25519_dalek::{PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH, KEYPAIR_LENGTH, SIGNATURE_LENGTH}; //! # fn do_test() -> Result<(SigningKey, VerifyingKey, Signature), SignatureError> { -//! # let mut csprng = OsRng; +//! # let mut csprng = OsRng.unwrap_err(); //! # let signing_key_orig: SigningKey = SigningKey::generate(&mut csprng); //! # let message: &[u8] = b"This is a test of the tsunami alert system."; //! # let signature_orig: Signature = signing_key_orig.sign(message); @@ -193,9 +199,10 @@ #![cfg_attr(not(all(feature = "rand_core", feature = "serde")), doc = "```ignore")] //! # fn main() { //! # use rand::rngs::OsRng; +//! # use rand_core::TryRngCore; //! # use ed25519_dalek::{SigningKey, Signature, Signer, Verifier, VerifyingKey}; //! use bincode::serialize; -//! # let mut csprng = OsRng; +//! # let mut csprng = OsRng.unwrap_err(); //! # let signing_key: SigningKey = SigningKey::generate(&mut csprng); //! # let message: &[u8] = b"This is a test of the tsunami alert system."; //! # let signature: Signature = signing_key.sign(message); @@ -214,11 +221,12 @@ #![cfg_attr(not(all(feature = "rand_core", feature = "serde")), doc = "```ignore")] //! # fn main() { //! # use rand::rngs::OsRng; +//! # use rand_core::TryRngCore; //! # use ed25519_dalek::{SigningKey, Signature, Signer, Verifier, VerifyingKey}; //! # use bincode::serialize; //! use bincode::deserialize; //! -//! # let mut csprng = OsRng; +//! # let mut csprng = OsRng.unwrap_err(); //! # let signing_key: SigningKey = SigningKey::generate(&mut csprng); //! let message: &[u8] = b"This is a test of the tsunami alert system."; //! # let signature: Signature = signing_key.sign(message); @@ -285,9 +293,9 @@ pub use crate::signing::*; pub use crate::verifying::*; // Re-export the `Signer` and `Verifier` traits from the `signature` crate -pub use ed25519::Signature; #[cfg(feature = "digest")] -pub use ed25519::signature::{DigestSigner, DigestVerifier}; +pub use ::signature::{DigestSigner, DigestVerifier}; +pub use ed25519::Signature; pub use ed25519::signature::{Signer, Verifier}; #[cfg(feature = "pkcs8")] diff --git a/ed25519-dalek/src/signing.rs b/ed25519-dalek/src/signing.rs index 4372148..e3b4e8e 100644 --- a/ed25519-dalek/src/signing.rs +++ b/ed25519-dalek/src/signing.rs @@ -15,7 +15,7 @@ use core::fmt::Debug; use ed25519::pkcs8; #[cfg(any(test, feature = "rand_core"))] -use rand_core::CryptoRngCore; +use rand_core::CryptoRng; #[cfg(feature = "serde")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -188,9 +188,10 @@ impl SigningKey { #[cfg_attr(not(feature = "rand_core"), doc = "```ignore")] /// # fn main() { /// use rand::rngs::OsRng; + /// use rand_core::TryRngCore; /// use ed25519_dalek::{Signature, SigningKey}; /// - /// let mut csprng = OsRng; + /// let mut csprng = OsRng.unwrap_err(); /// let signing_key: SigningKey = SigningKey::generate(&mut csprng); /// # } /// ``` @@ -199,7 +200,7 @@ impl SigningKey { /// /// A CSPRNG with a `fill_bytes()` method, e.g. `rand_os::OsRng`. #[cfg(any(test, feature = "rand_core"))] - pub fn generate(csprng: &mut R) -> SigningKey { + pub fn generate(csprng: &mut R) -> SigningKey { let mut secret = SecretKey::default(); csprng.fill_bytes(&mut secret); Self::from_bytes(&secret) @@ -240,9 +241,10 @@ impl SigningKey { /// use ed25519_dalek::Signature; /// use sha2::Sha512; /// use rand::rngs::OsRng; + /// use rand_core::TryRngCore; /// /// # fn main() { - /// let mut csprng = OsRng; + /// let mut csprng = OsRng.unwrap_err(); /// let signing_key: SigningKey = SigningKey::generate(&mut csprng); /// let message: &[u8] = b"All I want is to pet all of the dogs."; /// @@ -285,9 +287,10 @@ impl SigningKey { /// # use ed25519_dalek::SignatureError; /// # use sha2::Sha512; /// # use rand::rngs::OsRng; + /// # use rand_core::TryRngCore; /// # /// # fn do_test() -> Result { - /// # let mut csprng = OsRng; + /// # let mut csprng = OsRng.unwrap_err(); /// # let signing_key: SigningKey = SigningKey::generate(&mut csprng); /// # let message: &[u8] = b"All I want is to pet all of the dogs."; /// # let mut prehashed: Sha512 = Sha512::new(); @@ -364,9 +367,10 @@ impl SigningKey { /// use ed25519_dalek::SignatureError; /// use sha2::Sha512; /// use rand::rngs::OsRng; + /// use rand_core::TryRngCore; /// /// # fn do_test() -> Result<(), SignatureError> { - /// let mut csprng = OsRng; + /// let mut csprng = OsRng.unwrap_err(); /// let signing_key: SigningKey = SigningKey::generate(&mut csprng); /// let message: &[u8] = b"All I want is to pet all of the dogs."; /// diff --git a/ed25519-dalek/tests/ed25519.rs b/ed25519-dalek/tests/ed25519.rs index 5285ac2..8f8031e 100644 --- a/ed25519-dalek/tests/ed25519.rs +++ b/ed25519-dalek/tests/ed25519.rs @@ -27,6 +27,7 @@ mod vectors { scalar::Scalar, traits::IsIdentity, }; + use rand_core::TryRngCore; #[cfg(not(feature = "digest"))] use sha2::{Sha512, digest::Digest}; @@ -182,7 +183,7 @@ mod vectors { // Pick a random Scalar fn non_null_scalar() -> Scalar { - let mut rng = rand::rngs::OsRng; + let mut rng = rand::rngs::OsRng.unwrap_err(); let mut s_candidate = Scalar::random(&mut rng); while s_candidate == Scalar::ZERO { s_candidate = Scalar::random(&mut rng); @@ -294,7 +295,7 @@ mod vectors { #[cfg(feature = "rand_core")] mod integrations { use super::*; - use rand::rngs::OsRng; + use rand::{TryRngCore, rngs::OsRng}; use std::collections::HashMap; #[test] @@ -304,7 +305,7 @@ mod integrations { let good: &[u8] = "test message".as_bytes(); let bad: &[u8] = "wrong message".as_bytes(); - let mut csprng = OsRng; + let mut csprng = OsRng.unwrap_err(); let signing_key: SigningKey = SigningKey::generate(&mut csprng); let verifying_key = signing_key.verifying_key(); @@ -345,7 +346,7 @@ mod integrations { fn sign_verify_digest_equivalence() { // TestSignVerify - let mut csprng = OsRng {}; + let mut csprng = OsRng.unwrap_err(); let good: &[u8] = "test message".as_bytes(); let bad: &[u8] = "wrong message".as_bytes(); @@ -390,7 +391,7 @@ mod integrations { let good: &[u8] = b"test message"; let bad: &[u8] = b"wrong message"; - let mut csprng = OsRng; + let mut csprng = OsRng.unwrap_err(); // ugh… there's no `impl Copy for Sha512`… i hope we can all agree these are the same hashes let mut prehashed_good1: Sha512 = Sha512::default(); @@ -465,7 +466,7 @@ mod integrations { b"Fuck dumbin' it down, spit ice, skip jewellery: Molotov cocktails on me like accessories.", b"Hey, I never cared about your bucks, so if I run up with a mask on, probably got a gas can too.", b"And I'm not here to fill 'er up. Nope, we came to riot, here to incite, we don't want any of your stuff.", ]; - let mut csprng = OsRng; + let mut csprng = OsRng.unwrap_err(); let mut signing_keys: Vec = Vec::new(); let mut signatures: Vec = Vec::new(); @@ -484,7 +485,7 @@ mod integrations { #[test] fn public_key_hash_trait_check() { - let mut csprng = OsRng {}; + let mut csprng = OsRng.unwrap_err(); let secret: SigningKey = SigningKey::generate(&mut csprng); let public_from_secret: VerifyingKey = (&secret).into(); @@ -511,7 +512,7 @@ mod integrations { #[test] fn montgomery_and_edwards_conversion() { - let mut rng = rand::rngs::OsRng; + let mut rng = rand::rngs::OsRng.unwrap_err(); let signing_key = SigningKey::generate(&mut rng); let verifying_key = signing_key.verifying_key(); diff --git a/x25519-dalek/Cargo.toml b/x25519-dalek/Cargo.toml index 7fc77ae..cd90109 100644 --- a/x25519-dalek/Cargo.toml +++ b/x25519-dalek/Cargo.toml @@ -35,18 +35,18 @@ rustdoc-args = [ "--html-in-header", "docs/assets/rustdoc-include-katex-header.html", "--cfg", "docsrs", ] -features = ["getrandom", "reusable_secrets", "serde", "static_secrets"] +features = ["os_rng", "reusable_secrets", "serde", "static_secrets"] [dependencies] curve25519-dalek = { version = "=5.0.0-pre", path = "../curve25519-dalek", default-features = false } -rand_core = { version = "0.6", default-features = false } +rand_core = { version = "0.9", default-features = false } serde = { version = "1", default-features = false, optional = true, features = ["derive"] } zeroize = { version = "1", default-features = false, optional = true } [dev-dependencies] bincode = "1" criterion = "0.5" -rand_core = { version = "0.6", default-features = false, features = ["getrandom"] } +rand_core = { version = "0.9", default-features = false, features = ["os_rng"] } [[bench]] name = "x25519" @@ -54,7 +54,7 @@ harness = false [features] default = ["alloc", "precomputed-tables", "zeroize"] -getrandom = ["rand_core/getrandom"] +os_rng = ["rand_core/os_rng"] zeroize = ["dep:zeroize", "curve25519-dalek/zeroize"] serde = ["dep:serde", "curve25519-dalek/serde"] alloc = ["curve25519-dalek/alloc", "serde?/alloc", "zeroize?/alloc"] diff --git a/x25519-dalek/README.md b/x25519-dalek/README.md index c1604da..0dc801f 100644 --- a/x25519-dalek/README.md +++ b/x25519-dalek/README.md @@ -51,11 +51,12 @@ loudly meows `bob_public` back to Alice. Alice now computes her shared secret with Bob by doing: ```rust -# use rand_core::OsRng; +# use rand_core::{OsRng, TryRngCore}; # use x25519_dalek::{EphemeralSecret, PublicKey}; -# let alice_secret = EphemeralSecret::random_from_rng(OsRng); +# let mut rng = OsRng.unwrap_err(); +# let alice_secret = EphemeralSecret::random_from_rng(&mut rng); # let alice_public = PublicKey::from(&alice_secret); -# let bob_secret = EphemeralSecret::random_from_rng(OsRng); +# let bob_secret = EphemeralSecret::random_from_rng(&mut rng); # let bob_public = PublicKey::from(&bob_secret); let alice_shared_secret = alice_secret.diffie_hellman(&bob_public); ``` @@ -63,11 +64,12 @@ let alice_shared_secret = alice_secret.diffie_hellman(&bob_public); Similarly, Bob computes a shared secret by doing: ```rust -# use rand_core::OsRng; +# use rand_core::{OsRng, TryRngCore}; # use x25519_dalek::{EphemeralSecret, PublicKey}; -# let alice_secret = EphemeralSecret::random_from_rng(OsRng); +# let mut rng = OsRng.unwrap_err(); +# let alice_secret = EphemeralSecret::random_from_rng(&mut rng); # let alice_public = PublicKey::from(&alice_secret); -# let bob_secret = EphemeralSecret::random_from_rng(OsRng); +# let bob_secret = EphemeralSecret::random_from_rng(&mut rng); # let bob_public = PublicKey::from(&bob_secret); let bob_shared_secret = bob_secret.diffie_hellman(&alice_public); ``` @@ -75,11 +77,12 @@ let bob_shared_secret = bob_secret.diffie_hellman(&alice_public); These secrets are the same: ```rust -# use rand_core::OsRng; +# use rand_core::{OsRng, TryRngCore}; # use x25519_dalek::{EphemeralSecret, PublicKey}; -# let alice_secret = EphemeralSecret::random_from_rng(OsRng); +# let mut rng = OsRng.unwrap_err(); +# let alice_secret = EphemeralSecret::random_from_rng(&mut rng); # let alice_public = PublicKey::from(&alice_secret); -# let bob_secret = EphemeralSecret::random_from_rng(OsRng); +# let bob_secret = EphemeralSecret::random_from_rng(&mut rng); # let bob_public = PublicKey::from(&bob_secret); # let alice_shared_secret = alice_secret.diffie_hellman(&bob_public); # let bob_shared_secret = bob_secret.diffie_hellman(&alice_public); diff --git a/x25519-dalek/benches/x25519.rs b/x25519-dalek/benches/x25519.rs index e779a97..941bbd7 100644 --- a/x25519-dalek/benches/x25519.rs +++ b/x25519-dalek/benches/x25519.rs @@ -13,18 +13,18 @@ use criterion::{Criterion, criterion_group, criterion_main}; -use rand_core::OsRng; +use rand_core::{OsRng, TryRngCore}; use x25519_dalek::EphemeralSecret; use x25519_dalek::PublicKey; fn bench_diffie_hellman(c: &mut Criterion) { - let bob_secret = EphemeralSecret::random_from_rng(OsRng); + let bob_secret = EphemeralSecret::random_from_rng(&mut OsRng.unwrap_err()); let bob_public = PublicKey::from(&bob_secret); c.bench_function("diffie_hellman", move |b| { b.iter_with_setup( - || EphemeralSecret::random_from_rng(OsRng), + || EphemeralSecret::random_from_rng(&mut OsRng.unwrap_err()), |alice_secret| alice_secret.diffie_hellman(&bob_public), ) }); diff --git a/x25519-dalek/src/x25519.rs b/x25519-dalek/src/x25519.rs index cdbc025..26083ed 100644 --- a/x25519-dalek/src/x25519.rs +++ b/x25519-dalek/src/x25519.rs @@ -17,7 +17,8 @@ use curve25519_dalek::{edwards::EdwardsPoint, montgomery::MontgomeryPoint, traits::IsIdentity}; use rand_core::CryptoRng; -use rand_core::RngCore; +#[cfg(feature = "os_rng")] +use rand_core::TryRngCore; #[cfg(feature = "zeroize")] use zeroize::{Zeroize, ZeroizeOnDrop}; @@ -90,12 +91,12 @@ impl EphemeralSecret { since = "2.0.0", note = "Renamed to `random_from_rng`. This will be removed in 2.1.0" )] - pub fn new(mut csprng: T) -> Self { - Self::random_from_rng(&mut csprng) + pub fn new(csprng: &mut R) -> Self { + Self::random_from_rng(csprng) } /// Generate a new [`EphemeralSecret`] with the supplied RNG. - pub fn random_from_rng(mut csprng: T) -> Self { + pub fn random_from_rng(csprng: &mut R) -> Self { // The secret key is random bytes. Clamping is done later. let mut bytes = [0u8; 32]; csprng.fill_bytes(&mut bytes); @@ -103,9 +104,9 @@ impl EphemeralSecret { } /// Generate a new [`EphemeralSecret`]. - #[cfg(feature = "getrandom")] + #[cfg(feature = "os_rng")] pub fn random() -> Self { - Self::random_from_rng(rand_core::OsRng) + Self::random_from_rng(&mut rand_core::OsRng.unwrap_err()) } } @@ -168,12 +169,12 @@ impl ReusableSecret { since = "2.0.0", note = "Renamed to `random_from_rng`. This will be removed in 2.1.0." )] - pub fn new(mut csprng: T) -> Self { - Self::random_from_rng(&mut csprng) + pub fn new(csprng: &mut R) -> Self { + Self::random_from_rng(csprng) } /// Generate a new [`ReusableSecret`] with the supplied RNG. - pub fn random_from_rng(mut csprng: T) -> Self { + pub fn random_from_rng(csprng: &mut R) -> Self { // The secret key is random bytes. Clamping is done later. let mut bytes = [0u8; 32]; csprng.fill_bytes(&mut bytes); @@ -181,9 +182,9 @@ impl ReusableSecret { } /// Generate a new [`ReusableSecret`]. - #[cfg(feature = "getrandom")] + #[cfg(feature = "os_rng")] pub fn random() -> Self { - Self::random_from_rng(rand_core::OsRng) + Self::random_from_rng(&mut rand_core::OsRng.unwrap_mut()) } } @@ -245,12 +246,12 @@ impl StaticSecret { since = "2.0.0", note = "Renamed to `random_from_rng`. This will be removed in 2.1.0" )] - pub fn new(mut csprng: T) -> Self { - Self::random_from_rng(&mut csprng) + pub fn new(csprng: &mut R) -> Self { + Self::random_from_rng(csprng) } /// Generate a new [`StaticSecret`] with the supplied RNG. - pub fn random_from_rng(mut csprng: T) -> Self { + pub fn random_from_rng(csprng: &mut R) -> Self { // The secret key is random bytes. Clamping is done later. let mut bytes = [0u8; 32]; csprng.fill_bytes(&mut bytes); @@ -258,9 +259,9 @@ impl StaticSecret { } /// Generate a new [`StaticSecret`]. - #[cfg(feature = "getrandom")] + #[cfg(feature = "os_rng")] pub fn random() -> Self { - Self::random_from_rng(rand_core::OsRng) + Self::random_from_rng(&mut rand_core::OsRng.unwrap_mut()) } /// Extract this key's bytes for serialization. @@ -412,17 +413,20 @@ impl ZeroizeOnDrop for SharedSecret {} #[cfg_attr(not(feature = "static_secrets"), doc = "```ignore")] /// use rand_core::OsRng; /// use rand_core::RngCore; +/// use rand_core::TryRngCore; /// /// use x25519_dalek::x25519; /// use x25519_dalek::StaticSecret; /// use x25519_dalek::PublicKey; /// +/// let mut rng = OsRng.unwrap_err(); +/// /// // Generate Alice's key pair. -/// let alice_secret = StaticSecret::random_from_rng(&mut OsRng); +/// let alice_secret = StaticSecret::random_from_rng(&mut rng); /// let alice_public = PublicKey::from(&alice_secret); /// /// // Generate Bob's key pair. -/// let bob_secret = StaticSecret::random_from_rng(&mut OsRng); +/// let bob_secret = StaticSecret::random_from_rng(&mut rng); /// let bob_public = PublicKey::from(&bob_secret); /// /// // Alice and Bob should now exchange their public keys. diff --git a/x25519-dalek/tests/x25519_tests.rs b/x25519-dalek/tests/x25519_tests.rs index d589b3e..8304b77 100644 --- a/x25519-dalek/tests/x25519_tests.rs +++ b/x25519-dalek/tests/x25519_tests.rs @@ -181,34 +181,34 @@ fn rfc7748_ladder_test2() { mod rand_core { use super::*; - use ::rand_core::OsRng; + use ::rand_core::{OsRng, TryRngCore}; #[test] fn ephemeral_from_rng() { #[allow(deprecated)] - EphemeralSecret::new(OsRng); - EphemeralSecret::random_from_rng(OsRng); + EphemeralSecret::new(&mut OsRng.unwrap_err()); + EphemeralSecret::random_from_rng(&mut OsRng.unwrap_err()); } #[test] #[cfg(feature = "reusable_secrets")] fn reusable_from_rng() { #[allow(deprecated)] - ReusableSecret::new(OsRng); - ReusableSecret::random_from_rng(OsRng); + ReusableSecret::new(&mut OsRng.unwrap_err()); + ReusableSecret::random_from_rng(&mut OsRng.unwrap_err()); } #[test] #[cfg(feature = "static_secrets")] fn static_from_rng() { #[allow(deprecated)] - StaticSecret::new(OsRng); - StaticSecret::random_from_rng(OsRng); + StaticSecret::new(&mut OsRng.unwrap_err()); + StaticSecret::random_from_rng(&mut OsRng.unwrap_err()); } } -#[cfg(feature = "getrandom")] -mod getrandom { +#[cfg(feature = "os_rng")] +mod os_rng { use super::*;