Skip to content

Commit a8701a4

Browse files
authored
elliptic-curve: add back SecretKey::random with deprecation (RustCrypto#2086)
Has people use either the `generate` or `try_from_rng` methods instead.
1 parent e122175 commit a8701a4

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

elliptic-curve/src/scalar/nonzero.rs

Lines changed: 15 additions & 3 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::TryCryptoRng;
16+
use rand_core::{CryptoRng, TryCryptoRng};
1717
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
1818
use zeroize::Zeroize;
1919

@@ -50,7 +50,11 @@ impl<C> NonZeroScalar<C>
5050
where
5151
C: CurveArithmetic,
5252
{
53-
/// Generate a random `NonZeroScalar`.
53+
/// Generate a random [`NonZeroScalar`].
54+
///
55+
/// # Panics
56+
///
57+
/// If the system's cryptographically secure RNG has an internal error.
5458
#[cfg(feature = "getrandom")]
5559
pub fn generate() -> Self {
5660
// Use rejection sampling to eliminate invalid values
@@ -65,7 +69,7 @@ where
6569
}
6670
}
6771

68-
/// Generate a random `NonZeroScalar`.
72+
/// Generate a random [`NonZeroScalar`].
6973
pub fn try_from_rng<R: TryCryptoRng + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
7074
// Use rejection sampling to eliminate zero values.
7175
// While this method isn't constant-time, the attacker shouldn't learn
@@ -77,6 +81,14 @@ where
7781
}
7882
}
7983

84+
/// Deprecated: Generate a random [`NonZeroScalar`].
85+
#[cfg(feature = "arithmetic")]
86+
#[deprecated(since = "0.14.0", note = "use `generate` or `try_from_rng` instead")]
87+
pub fn random<R: CryptoRng + ?Sized>(rng: &mut R) -> Self {
88+
let Ok(ret) = Self::try_from_rng(rng);
89+
ret
90+
}
91+
8092
/// Create a [`NonZeroScalar`] from a scalar.
8193
pub fn new(scalar: Scalar<C>) -> CtOption<Self> {
8294
CtOption::new(Self { scalar }, !scalar.is_zero())

elliptic-curve/src/secret_key.rs

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

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

2023
#[cfg(feature = "pem")]
2124
use pem_rfc7468::{self as pem, PemLabel};
@@ -84,6 +87,10 @@ where
8487
const MIN_SIZE: usize = 24;
8588

8689
/// Generate a random [`SecretKey`].
90+
///
91+
/// # Panics
92+
///
93+
/// If the system's cryptographically secure RNG has an internal error.
8794
#[cfg(feature = "getrandom")]
8895
pub fn generate() -> Self
8996
where
@@ -107,6 +114,17 @@ where
107114
})
108115
}
109116

117+
/// Deprecated: Generate a random [`SecretKey`].
118+
#[cfg(feature = "arithmetic")]
119+
#[deprecated(since = "0.14.0", note = "use `generate` or `try_from_rng` instead")]
120+
pub fn random<R: CryptoRng + ?Sized>(rng: &mut R) -> Self
121+
where
122+
C: CurveArithmetic,
123+
{
124+
let Ok(ret) = Self::try_from_rng(rng);
125+
ret
126+
}
127+
110128
/// Create a new secret key from a scalar value.
111129
///
112130
/// # Returns

0 commit comments

Comments
 (0)