Skip to content

Commit d669c66

Browse files
authored
ecdsa: use the crypto_common::Generate trait (#1140)
Updates that go along with RustCrypto/traits#2173, which switched the `elliptic-curve` to use the `Generate` trait introduced in RustCrypto/traits#2096
1 parent 4112075 commit d669c66

3 files changed

Lines changed: 38 additions & 49 deletions

File tree

Cargo.lock

Lines changed: 16 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ lms-signature = { path = "./lms" }
2525
ml-dsa = { path = "./ml-dsa" }
2626
rfc6979 = { path = "./rfc6979" }
2727
slh-dsa = { path = "./slh-dsa" }
28+
29+
elliptic-curve = { git = "https://github.com/RustCrypto/traits" }

ecdsa/src/signing.rs

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ use crate::{
77
use core::fmt::{self, Debug};
88
use digest::{Update, block_api::EagerHash, const_oid::AssociatedOid};
99
use elliptic_curve::{
10-
CurveArithmetic, FieldBytes, NonZeroScalar, Scalar, SecretKey,
10+
CurveArithmetic, FieldBytes, Generate, NonZeroScalar, Scalar, SecretKey,
1111
array::ArraySize,
1212
group::ff::PrimeField,
1313
ops::Invert,
14+
rand_core::CryptoRng,
1415
subtle::{Choice, ConstantTimeEq, CtOption},
1516
zeroize::{Zeroize, ZeroizeOnDrop},
1617
};
@@ -67,8 +68,6 @@ use elliptic_curve::pkcs8::{EncodePrivateKey, SecretDocument};
6768
pub struct SigningKey<C>
6869
where
6970
C: EcdsaCurve + CurveArithmetic,
70-
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
71-
SignatureSize<C>: ArraySize,
7271
{
7372
/// ECDSA signing keys are non-zero elements of a given curve's scalar field.
7473
secret_scalar: NonZeroScalar<C>,
@@ -81,26 +80,7 @@ where
8180
impl<C> SigningKey<C>
8281
where
8382
C: EcdsaCurve + CurveArithmetic,
84-
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
85-
SignatureSize<C>: ArraySize,
8683
{
87-
/// Generate a cryptographically random [`SigningKey`].
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()
95-
}
96-
97-
/// Generate a cryptographically random [`SigningKey`], returning underlying RNG errors.
98-
pub fn try_from_rng<R: TryCryptoRng + ?Sized>(
99-
rng: &mut R,
100-
) -> core::result::Result<Self, R::Error> {
101-
Ok(NonZeroScalar::<C>::try_from_rng(rng)?.into())
102-
}
103-
10484
/// Initialize signing key from a raw scalar serialized as a byte array.
10585
pub fn from_bytes(bytes: &FieldBytes<C>) -> Result<Self> {
10686
SecretKey::<C>::from_bytes(bytes)
@@ -136,6 +116,23 @@ where
136116
pub fn verifying_key(&self) -> &VerifyingKey<C> {
137117
&self.verifying_key
138118
}
119+
120+
/// DEPRECATED: Generate a cryptographically random [`SigningKey`].
121+
#[deprecated(since = "0.17.0", note = "use the `Generate` trait instead")]
122+
pub fn random<R: CryptoRng + ?Sized>(rng: &mut R) -> Self {
123+
Self::generate_from_rng(rng)
124+
}
125+
}
126+
127+
impl<C> Generate for SigningKey<C>
128+
where
129+
C: EcdsaCurve + CurveArithmetic,
130+
{
131+
fn try_generate_from_rng<R: TryCryptoRng + ?Sized>(
132+
rng: &mut R,
133+
) -> core::result::Result<Self, R::Error> {
134+
Ok(NonZeroScalar::<C>::try_generate_from_rng(rng)?.into())
135+
}
139136
}
140137

141138
//
@@ -474,8 +471,6 @@ where
474471
impl<C> Debug for SigningKey<C>
475472
where
476473
C: EcdsaCurve + CurveArithmetic,
477-
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
478-
SignatureSize<C>: ArraySize,
479474
{
480475
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
481476
f.debug_struct("SigningKey").finish_non_exhaustive()
@@ -485,8 +480,6 @@ where
485480
impl<C> Drop for SigningKey<C>
486481
where
487482
C: EcdsaCurve + CurveArithmetic,
488-
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
489-
SignatureSize<C>: ArraySize,
490483
{
491484
fn drop(&mut self) {
492485
self.secret_scalar.zeroize();
@@ -515,8 +508,6 @@ where
515508
impl<C> From<NonZeroScalar<C>> for SigningKey<C>
516509
where
517510
C: EcdsaCurve + CurveArithmetic,
518-
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
519-
SignatureSize<C>: ArraySize,
520511
{
521512
fn from(secret_scalar: NonZeroScalar<C>) -> Self {
522513
#[cfg(feature = "algorithm")]
@@ -533,8 +524,6 @@ where
533524
impl<C> From<SecretKey<C>> for SigningKey<C>
534525
where
535526
C: EcdsaCurve + CurveArithmetic,
536-
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
537-
SignatureSize<C>: ArraySize,
538527
{
539528
fn from(secret_key: SecretKey<C>) -> Self {
540529
Self::from(&secret_key)
@@ -544,8 +533,6 @@ where
544533
impl<C> From<&SecretKey<C>> for SigningKey<C>
545534
where
546535
C: EcdsaCurve + CurveArithmetic,
547-
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
548-
SignatureSize<C>: ArraySize,
549536
{
550537
fn from(secret_key: &SecretKey<C>) -> Self {
551538
secret_key.to_nonzero_scalar().into()
@@ -566,8 +553,6 @@ where
566553
impl<C> From<&SigningKey<C>> for SecretKey<C>
567554
where
568555
C: EcdsaCurve + CurveArithmetic,
569-
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
570-
SignatureSize<C>: ArraySize,
571556
{
572557
fn from(secret_key: &SigningKey<C>) -> Self {
573558
secret_key.secret_scalar.into()
@@ -577,8 +562,6 @@ where
577562
impl<C> TryFrom<&[u8]> for SigningKey<C>
578563
where
579564
C: EcdsaCurve + CurveArithmetic,
580-
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
581-
SignatureSize<C>: ArraySize,
582565
{
583566
type Error = Error;
584567

@@ -587,13 +570,7 @@ where
587570
}
588571
}
589572

590-
impl<C> ZeroizeOnDrop for SigningKey<C>
591-
where
592-
C: EcdsaCurve + CurveArithmetic,
593-
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
594-
SignatureSize<C>: ArraySize,
595-
{
596-
}
573+
impl<C> ZeroizeOnDrop for SigningKey<C> where C: EcdsaCurve + CurveArithmetic {}
597574

598575
#[cfg(feature = "algorithm")]
599576
impl<C> From<SigningKey<C>> for VerifyingKey<C>

0 commit comments

Comments
 (0)