Skip to content

Commit e122175

Browse files
authored
elliptic-curve: getrandom feature (RustCrypto#2085)
Adds support for generating `SecretKey` and `NonZeroScalar` using the system's cryptographically secure random number generator. Notably this renames the former `SecretKey::random` and `NonZeroScalar::random` methods to `SecretKey::generate` and `NonZeroScalar::generate`, which take no parameters and are infallible. This avoids the need for the user to import an `OsRng` type, or worry about the generation failing (which it won't on most notable modern OSes). If a user still wants to handle RNG errors, the `try_from_rng` method still exists, and they can pass `OsRng` if they'd like.
1 parent 2cf79f8 commit e122175

File tree

5 files changed

+18
-14
lines changed

5 files changed

+18
-14
lines changed

Cargo.lock

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

elliptic-curve/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ zeroize = { version = "1.7", default-features = false }
2727
# optional dependencies
2828
digest = { version = "0.11.0-rc.4", optional = true }
2929
ff = { version = "=0.14.0-pre.0", optional = true, default-features = false }
30+
getrandom = { version = "0.3", optional = true }
3031
group = { version = "=0.14.0-pre.0", optional = true, default-features = false }
3132
hkdf = { version = "0.13.0-rc.3", optional = true, default-features = false }
3233
hex-literal = { version = "1", optional = true }
@@ -63,6 +64,7 @@ critical-section = ["basepoint-table", "once_cell/critical-section"]
6364
bits = ["arithmetic", "ff/bits"]
6465
dev = ["arithmetic", "dep:hex-literal", "pem", "pkcs8"]
6566
ecdh = ["arithmetic", "digest", "dep:hkdf"]
67+
getrandom = ["dep:getrandom", "arithmetic"]
6668
group = ["dep:group", "ff"]
6769
pkcs8 = ["dep:pkcs8", "sec1"]
6870
pem = ["dep:pem-rfc7468", "alloc", "arithmetic", "pkcs8/pem", "sec1/pem"]

elliptic-curve/src/ecdh.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use crate::{
3434
};
3535
use core::{borrow::Borrow, fmt};
3636
use hkdf::Hkdf;
37-
use rand_core::{CryptoRng, TryCryptoRng};
37+
use rand_core::TryCryptoRng;
3838
use zeroize::{Zeroize, ZeroizeOnDrop};
3939

4040
/// Low-level Elliptic Curve Diffie-Hellman (ECDH) function.
@@ -108,9 +108,10 @@ where
108108
C: CurveArithmetic,
109109
{
110110
/// Generate a cryptographically random [`EphemeralSecret`].
111-
pub fn random<R: CryptoRng + ?Sized>(rng: &mut R) -> Self {
111+
#[cfg(feature = "getrandom")]
112+
pub fn generate() -> Self {
112113
Self {
113-
scalar: NonZeroScalar::random(rng),
114+
scalar: NonZeroScalar::generate(),
114115
}
115116
}
116117

elliptic-curve/src/scalar/nonzero.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use core::{
1313
str,
1414
};
1515
use ff::{Field, PrimeField};
16-
use rand_core::{CryptoRng, TryCryptoRng};
16+
use rand_core::TryCryptoRng;
1717
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
1818
use zeroize::Zeroize;
1919

@@ -51,12 +51,15 @@ where
5151
C: CurveArithmetic,
5252
{
5353
/// Generate a random `NonZeroScalar`.
54-
pub fn random<R: CryptoRng + ?Sized>(rng: &mut R) -> Self {
55-
// Use rejection sampling to eliminate zero values.
54+
#[cfg(feature = "getrandom")]
55+
pub fn generate() -> Self {
56+
// Use rejection sampling to eliminate invalid values
5657
// While this method isn't constant-time, the attacker shouldn't learn
5758
// anything about unrelated outputs so long as `rng` is a secure `CryptoRng`.
5859
loop {
59-
if let Some(result) = Self::new(Field::random(rng)).into() {
60+
let mut repr = FieldBytes::<C>::default();
61+
getrandom::fill(&mut repr).expect("RNG failure");
62+
if let Some(result) = Self::from_repr(repr).into() {
6063
break result;
6164
}
6265
}

elliptic-curve/src/secret_key.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ use subtle::{Choice, ConstantTimeEq, CtOption};
1515
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
1616

1717
#[cfg(feature = "arithmetic")]
18-
use crate::{
19-
CurveArithmetic, NonZeroScalar, PublicKey,
20-
rand_core::{CryptoRng, TryCryptoRng},
21-
};
18+
use crate::{CurveArithmetic, NonZeroScalar, PublicKey, rand_core::TryCryptoRng};
2219

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

8986
/// Generate a random [`SecretKey`].
90-
#[cfg(feature = "arithmetic")]
91-
pub fn random<R: CryptoRng + ?Sized>(rng: &mut R) -> Self
87+
#[cfg(feature = "getrandom")]
88+
pub fn generate() -> Self
9289
where
9390
C: CurveArithmetic,
9491
{
9592
Self {
96-
inner: NonZeroScalar::<C>::random(rng).into(),
93+
inner: NonZeroScalar::<C>::generate().into(),
9794
}
9895
}
9996

0 commit comments

Comments
 (0)