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
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions elliptic-curve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ zeroize = { version = "1.7", default-features = false }
# optional dependencies
digest = { version = "0.11.0-rc.4", optional = true }
ff = { version = "=0.14.0-pre.0", optional = true, default-features = false }
getrandom = { version = "0.3", optional = true }
group = { version = "=0.14.0-pre.0", optional = true, default-features = false }
hkdf = { version = "0.13.0-rc.3", optional = true, default-features = false }
hex-literal = { version = "1", optional = true }
Expand Down Expand Up @@ -63,6 +64,7 @@ critical-section = ["basepoint-table", "once_cell/critical-section"]
bits = ["arithmetic", "ff/bits"]
dev = ["arithmetic", "dep:hex-literal", "pem", "pkcs8"]
ecdh = ["arithmetic", "digest", "dep:hkdf"]
getrandom = ["dep:getrandom", "arithmetic"]
group = ["dep:group", "ff"]
pkcs8 = ["dep:pkcs8", "sec1"]
pem = ["dep:pem-rfc7468", "alloc", "arithmetic", "pkcs8/pem", "sec1/pem"]
Expand Down
7 changes: 4 additions & 3 deletions elliptic-curve/src/ecdh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::{
};
use core::{borrow::Borrow, fmt};
use hkdf::Hkdf;
use rand_core::{CryptoRng, TryCryptoRng};
use rand_core::TryCryptoRng;
use zeroize::{Zeroize, ZeroizeOnDrop};

/// Low-level Elliptic Curve Diffie-Hellman (ECDH) function.
Expand Down Expand Up @@ -108,9 +108,10 @@ where
C: CurveArithmetic,
{
/// Generate a cryptographically random [`EphemeralSecret`].
pub fn random<R: CryptoRng + ?Sized>(rng: &mut R) -> Self {
#[cfg(feature = "getrandom")]
pub fn generate() -> Self {
Self {
scalar: NonZeroScalar::random(rng),
scalar: NonZeroScalar::generate(),
}
}

Expand Down
11 changes: 7 additions & 4 deletions elliptic-curve/src/scalar/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use core::{
str,
};
use ff::{Field, PrimeField};
use rand_core::{CryptoRng, TryCryptoRng};
use rand_core::TryCryptoRng;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
use zeroize::Zeroize;

Expand Down Expand Up @@ -51,12 +51,15 @@ where
C: CurveArithmetic,
{
/// Generate a random `NonZeroScalar`.
pub fn random<R: CryptoRng + ?Sized>(rng: &mut R) -> Self {
// Use rejection sampling to eliminate zero values.
#[cfg(feature = "getrandom")]
pub fn generate() -> Self {
// Use rejection sampling to eliminate invalid values
// While this method isn't constant-time, the attacker shouldn't learn
// anything about unrelated outputs so long as `rng` is a secure `CryptoRng`.
loop {
if let Some(result) = Self::new(Field::random(rng)).into() {
let mut repr = FieldBytes::<C>::default();
getrandom::fill(&mut repr).expect("RNG failure");
if let Some(result) = Self::from_repr(repr).into() {
break result;
}
}
Expand Down
11 changes: 4 additions & 7 deletions elliptic-curve/src/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ use subtle::{Choice, ConstantTimeEq, CtOption};
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};

#[cfg(feature = "arithmetic")]
use crate::{
CurveArithmetic, NonZeroScalar, PublicKey,
rand_core::{CryptoRng, TryCryptoRng},
};
use crate::{CurveArithmetic, NonZeroScalar, PublicKey, rand_core::TryCryptoRng};

#[cfg(feature = "pem")]
use pem_rfc7468::{self as pem, PemLabel};
Expand Down Expand Up @@ -87,13 +84,13 @@ where
const MIN_SIZE: usize = 24;

/// Generate a random [`SecretKey`].
#[cfg(feature = "arithmetic")]
pub fn random<R: CryptoRng + ?Sized>(rng: &mut R) -> Self
#[cfg(feature = "getrandom")]
pub fn generate() -> Self
where
C: CurveArithmetic,
{
Self {
inner: NonZeroScalar::<C>::random(rng).into(),
inner: NonZeroScalar::<C>::generate().into(),
}
}

Expand Down