Skip to content

Commit 9fe087a

Browse files
authored
ecdsa: add getrandom feature (#1107)
Adds a `getrandom` feature which allows infallible generation of `SigningKey`s using the system's cryptographically secure RNG. Proxies through to the newly added `getrandom` feature in the `elliptic-curve` crate: RustCrypto/traits#2085
1 parent 7738cf0 commit 9fe087a

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

.github/workflows/ecdsa.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
toolchain: ${{ matrix.rust }}
4141
targets: ${{ matrix.target }}
4242
- uses: RustCrypto/actions/cargo-hack-install@master
43-
- run: cargo hack build --target ${{ matrix.target }} --feature-powerset --exclude-features std
43+
- run: cargo hack build --target ${{ matrix.target }} --feature-powerset --exclude-features std,getrandom
4444

4545
test:
4646
runs-on: ubuntu-latest

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ecdsa/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ algorithm = ["dep:rfc6979", "digest", "elliptic-curve/arithmetic", "hazmat"]
4343
dev = ["algorithm", "digest/dev", "elliptic-curve/dev"]
4444
der = ["dep:der"]
4545
digest = ["dep:digest", "elliptic-curve/digest", "signature/digest"]
46+
getrandom = ["elliptic-curve/getrandom"]
4647
hazmat = []
4748
pkcs8 = ["der", "digest", "elliptic-curve/pkcs8"]
4849
pem = ["elliptic-curve/pem", "pkcs8"]

ecdsa/src/signing.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use signature::{
1818
DigestSigner, MultipartSigner, RandomizedDigestSigner, RandomizedMultipartSigner,
1919
RandomizedSigner, Signer,
2020
hazmat::{PrehashSigner, RandomizedPrehashSigner},
21-
rand_core::{CryptoRng, TryCryptoRng},
21+
rand_core::TryCryptoRng,
2222
};
2323

2424
#[cfg(feature = "der")]
@@ -85,11 +85,16 @@ where
8585
SignatureSize<C>: ArraySize,
8686
{
8787
/// Generate a cryptographically random [`SigningKey`].
88-
pub fn random<R: CryptoRng + ?Sized>(rng: &mut R) -> Self {
89-
NonZeroScalar::<C>::random(rng).into()
88+
///
89+
/// # Panics
90+
///
91+
/// If the system's cryptographically secure RNG has an internal error.
92+
#[cfg(feature = "getrandom")]
93+
pub fn generate() -> Self {
94+
NonZeroScalar::<C>::generate().into()
9095
}
9196

92-
/// Generate a cryptographically random [`SigningKey`].
97+
/// Generate a cryptographically random [`SigningKey`], returning underlying RNG errors.
9398
pub fn try_from_rng<R: TryCryptoRng + ?Sized>(
9499
rng: &mut R,
95100
) -> core::result::Result<Self, R::Error> {

0 commit comments

Comments
 (0)