Skip to content

Commit d2a93b3

Browse files
authored
Revert "ecdsa: use blanket impl from signature 3 (#945)" (#955)
This reverts commit 8e6bb26.
1 parent 692353d commit d2a93b3

File tree

8 files changed

+175
-108
lines changed

8 files changed

+175
-108
lines changed

ecdsa/CHANGELOG.md

-8
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## 0.17.0 (UNRELEASED)
8-
9-
### Changed
10-
- `DigestPrimitive` was moved off of hazmat ([#945])
11-
- `DigestPrimitive` has been renamed `DigestAlgorithm` ([#945])
12-
13-
[#945]: https://github.com/RustCrypto/signatures/pull/945
14-
157
## 0.16.9 (2023-11-16)
168
### Changed
179
- Loosen `signature` bound to `2.0, <2.3` ([#756])

ecdsa/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ digest = ["dep:digest", "elliptic-curve/digest", "signature/digest"]
4545
hazmat = []
4646
pkcs8 = ["digest", "elliptic-curve/pkcs8", "der"]
4747
pem = ["elliptic-curve/pem", "pkcs8"]
48-
rfc6979 = ["arithmetic", "digest", "dep:rfc6979"]
4948
serde = ["elliptic-curve/serde", "pkcs8", "serdect"]
5049
signing = ["arithmetic", "digest", "hazmat", "rfc6979"]
5150
verifying = ["arithmetic", "digest", "hazmat"]

ecdsa/src/der.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,10 @@ fn find_scalar_range(outer: &[u8], inner: &[u8]) -> Result<Range<usize>> {
382382
Ok(Range { start, end })
383383
}
384384

385-
#[cfg(feature = "digest")]
385+
#[cfg(all(feature = "digest", feature = "hazmat"))]
386386
impl<C> signature::PrehashSignature for Signature<C>
387387
where
388-
C: EcdsaCurve + crate::DigestAlgorithm,
388+
C: EcdsaCurve + crate::hazmat::DigestPrimitive,
389389
MaxSize<C>: ArraySize,
390390
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArraySize,
391391
{

ecdsa/src/hazmat.rs

+34-9
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,46 @@ use {
2727
},
2828
};
2929

30-
#[cfg(feature = "rfc6979")]
30+
#[cfg(feature = "digest")]
3131
use {
32-
elliptic_curve::FieldBytesEncoding,
33-
signature::digest::{Digest, FixedOutput, FixedOutputReset, core_api::BlockSizeUser},
32+
elliptic_curve::FieldBytesSize,
33+
signature::{
34+
PrehashSignature,
35+
digest::{Digest, FixedOutput, FixedOutputReset, core_api::BlockSizeUser},
36+
},
3437
};
3538

36-
#[cfg(any(feature = "arithmetic", feature = "rfc6979"))]
39+
#[cfg(feature = "rfc6979")]
40+
use elliptic_curve::FieldBytesEncoding;
41+
42+
#[cfg(any(feature = "arithmetic", feature = "digest"))]
3743
use crate::{Signature, elliptic_curve::array::ArraySize};
3844

45+
/// Bind a preferred [`Digest`] algorithm to an elliptic curve type.
46+
///
47+
/// Generally there is a preferred variety of the SHA-2 family used with ECDSA
48+
/// for a particular elliptic curve.
49+
///
50+
/// This trait can be used to specify it, and with it receive a blanket impl of
51+
/// [`PrehashSignature`], used by [`signature_derive`][1]) for the [`Signature`]
52+
/// type for a particular elliptic curve.
53+
///
54+
/// [1]: https://github.com/RustCrypto/traits/tree/master/signature/derive
55+
#[cfg(feature = "digest")]
56+
pub trait DigestPrimitive: EcdsaCurve {
57+
/// Preferred digest to use when computing ECDSA signatures for this
58+
/// elliptic curve. This is typically a member of the SHA-2 family.
59+
type Digest: BlockSizeUser + Digest + FixedOutput + FixedOutputReset;
60+
}
61+
3962
#[cfg(feature = "digest")]
40-
#[deprecated(
41-
since = "0.17.0",
42-
note = "`DigestAlgorithm` is no longer in `hazmat`, please use `ecdsa::DigestAlgorithm` instead"
43-
)]
44-
pub use crate::DigestAlgorithm;
63+
impl<C> PrehashSignature for Signature<C>
64+
where
65+
C: DigestPrimitive,
66+
<FieldBytesSize<C> as core::ops::Add>::Output: ArraySize,
67+
{
68+
type Digest = C::Digest;
69+
}
4570

4671
/// Partial implementation of the `bits2int` function as defined in
4772
/// [RFC6979 § 2.3.2] as well as [SEC1] § 2.3.8.

ecdsa/src/lib.rs

+12-42
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,9 @@ use {
102102
};
103103

104104
#[cfg(feature = "digest")]
105-
use {
106-
digest::{
107-
Digest, FixedOutput, FixedOutputReset,
108-
const_oid::{AssociatedOid, ObjectIdentifier},
109-
core_api::BlockSizeUser,
110-
},
111-
signature::PrehashSignature,
105+
use digest::{
106+
Digest,
107+
const_oid::{AssociatedOid, ObjectIdentifier},
112108
};
113109

114110
#[cfg(feature = "pkcs8")]
@@ -468,15 +464,15 @@ where
468464
///
469465
/// To support non-default digest algorithms, use the [`SignatureWithOid`]
470466
/// type instead.
471-
#[cfg(feature = "digest")]
467+
#[cfg(all(feature = "digest", feature = "hazmat"))]
472468
impl<C> AssociatedOid for Signature<C>
473469
where
474-
C: DigestAlgorithm,
470+
C: hazmat::DigestPrimitive,
475471
C::Digest: AssociatedOid,
476472
{
477473
const OID: ObjectIdentifier = match ecdsa_oid_for_digest(C::Digest::OID) {
478474
Some(oid) => oid,
479-
None => panic!("no RFC5758 ECDSA OID defined for DigestAlgorithm::Digest"),
475+
None => panic!("no RFC5758 ECDSA OID defined for DigestPrimitive::Digest"),
480476
};
481477
}
482478

@@ -725,29 +721,29 @@ where
725721
}
726722

727723
/// NOTE: this implementation assumes the default digest for the given elliptic
728-
/// curve as defined by [`DigestAlgorithm`].
724+
/// curve as defined by [`hazmat::DigestPrimitive`].
729725
///
730726
/// When working with alternative digests, you will need to use e.g.
731727
/// [`SignatureWithOid::new_with_digest`].
732-
#[cfg(feature = "digest")]
728+
#[cfg(all(feature = "digest", feature = "hazmat"))]
733729
impl<C> SignatureEncoding for SignatureWithOid<C>
734730
where
735-
C: DigestAlgorithm,
731+
C: hazmat::DigestPrimitive,
736732
C::Digest: AssociatedOid,
737733
SignatureSize<C>: ArraySize,
738734
{
739735
type Repr = SignatureBytes<C>;
740736
}
741737

742738
/// NOTE: this implementation assumes the default digest for the given elliptic
743-
/// curve as defined by [`DigestAlgorithm`].
739+
/// curve as defined by [`hazmat::DigestPrimitive`].
744740
///
745741
/// When working with alternative digests, you will need to use e.g.
746742
/// [`SignatureWithOid::new_with_digest`].
747-
#[cfg(feature = "digest")]
743+
#[cfg(all(feature = "digest", feature = "hazmat"))]
748744
impl<C> TryFrom<&[u8]> for SignatureWithOid<C>
749745
where
750-
C: DigestAlgorithm,
746+
C: hazmat::DigestPrimitive,
751747
C::Digest: AssociatedOid,
752748
SignatureSize<C>: ArraySize,
753749
{
@@ -782,29 +778,3 @@ const fn ecdsa_oid_for_digest(digest_oid: ObjectIdentifier) -> Option<ObjectIden
782778
_ => None,
783779
}
784780
}
785-
786-
/// Bind a preferred [`Digest`] algorithm to an elliptic curve type.
787-
///
788-
/// Generally there is a preferred variety of the SHA-2 family used with ECDSA
789-
/// for a particular elliptic curve.
790-
///
791-
/// This trait can be used to specify it, and with it receive a blanket impl of
792-
/// [`PrehashSignature`], used by [`signature_derive`][1]) for the [`Signature`]
793-
/// type for a particular elliptic curve.
794-
///
795-
/// [1]: https://github.com/RustCrypto/traits/tree/master/signature/derive
796-
#[cfg(feature = "digest")]
797-
pub trait DigestAlgorithm: EcdsaCurve {
798-
/// Preferred digest to use when computing ECDSA signatures for this
799-
/// elliptic curve. This is typically a member of the SHA-2 family.
800-
type Digest: BlockSizeUser + Digest + FixedOutput + FixedOutputReset;
801-
}
802-
803-
#[cfg(feature = "digest")]
804-
impl<C> PrehashSignature for Signature<C>
805-
where
806-
C: DigestAlgorithm,
807-
<FieldBytesSize<C> as Add>::Output: ArraySize,
808-
{
809-
type Digest = C::Digest;
810-
}

ecdsa/src/recovery.rs

+25-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use {
77
crate::{SigningKey, hazmat::sign_prehashed_rfc6979},
88
elliptic_curve::{FieldBytes, subtle::CtOption},
99
signature::{
10-
DigestSigner, RandomizedDigestSigner,
10+
DigestSigner, RandomizedDigestSigner, Signer,
1111
digest::FixedOutput,
1212
hazmat::{PrehashSigner, RandomizedPrehashSigner},
1313
rand_core::TryCryptoRng,
@@ -28,7 +28,10 @@ use {
2828

2929
#[cfg(any(feature = "signing", feature = "verifying"))]
3030
use {
31-
crate::{DigestAlgorithm, EcdsaCurve, Signature, SignatureSize, hazmat::bits2field},
31+
crate::{
32+
EcdsaCurve, Signature, SignatureSize,
33+
hazmat::{DigestPrimitive, bits2field},
34+
},
3235
elliptic_curve::{CurveArithmetic, Scalar, array::ArraySize, ops::Invert},
3336
signature::digest::Digest,
3437
};
@@ -97,7 +100,7 @@ impl RecoveryId {
97100
signature: &Signature<C>,
98101
) -> Result<Self>
99102
where
100-
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
103+
C: EcdsaCurve + CurveArithmetic + DigestPrimitive,
101104
AffinePoint<C>: DecompressPoint<C> + FromEncodedPoint<C> + ToEncodedPoint<C>,
102105
FieldBytesSize<C>: sec1::ModulusSize,
103106
SignatureSize<C>: ArraySize,
@@ -176,7 +179,7 @@ impl From<RecoveryId> for u8 {
176179
#[cfg(feature = "signing")]
177180
impl<C> SigningKey<C>
178181
where
179-
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
182+
C: EcdsaCurve + CurveArithmetic + DigestPrimitive,
180183
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
181184
SignatureSize<C>: ArraySize,
182185
{
@@ -225,7 +228,7 @@ where
225228
#[cfg(feature = "signing")]
226229
impl<C, D> DigestSigner<D, (Signature<C>, RecoveryId)> for SigningKey<C>
227230
where
228-
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
231+
C: EcdsaCurve + CurveArithmetic + DigestPrimitive,
229232
D: Digest,
230233
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
231234
SignatureSize<C>: ArraySize,
@@ -238,7 +241,7 @@ where
238241
#[cfg(feature = "signing")]
239242
impl<C> RandomizedPrehashSigner<(Signature<C>, RecoveryId)> for SigningKey<C>
240243
where
241-
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
244+
C: EcdsaCurve + CurveArithmetic + DigestPrimitive,
242245
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
243246
SignatureSize<C>: ArraySize,
244247
{
@@ -254,7 +257,7 @@ where
254257
#[cfg(feature = "signing")]
255258
impl<C, D> RandomizedDigestSigner<D, (Signature<C>, RecoveryId)> for SigningKey<C>
256259
where
257-
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
260+
C: EcdsaCurve + CurveArithmetic + DigestPrimitive,
258261
D: Digest + FixedOutput,
259262
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
260263
SignatureSize<C>: ArraySize,
@@ -271,7 +274,7 @@ where
271274
#[cfg(feature = "signing")]
272275
impl<C> PrehashSigner<(Signature<C>, RecoveryId)> for SigningKey<C>
273276
where
274-
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
277+
C: EcdsaCurve + CurveArithmetic + DigestPrimitive,
275278
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
276279
SignatureSize<C>: ArraySize,
277280
{
@@ -280,6 +283,18 @@ where
280283
}
281284
}
282285

286+
#[cfg(feature = "signing")]
287+
impl<C> Signer<(Signature<C>, RecoveryId)> for SigningKey<C>
288+
where
289+
C: EcdsaCurve + CurveArithmetic + DigestPrimitive,
290+
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
291+
SignatureSize<C>: ArraySize,
292+
{
293+
fn try_sign(&self, msg: &[u8]) -> Result<(Signature<C>, RecoveryId)> {
294+
self.sign_recoverable(msg)
295+
}
296+
}
297+
283298
#[cfg(feature = "verifying")]
284299
impl<C> VerifyingKey<C>
285300
where
@@ -291,14 +306,14 @@ where
291306
/// Recover a [`VerifyingKey`] from the given message, signature, and
292307
/// [`RecoveryId`].
293308
///
294-
/// The message is first hashed using this curve's [`DigestAlgorithm`].
309+
/// The message is first hashed using this curve's [`DigestPrimitive`].
295310
pub fn recover_from_msg(
296311
msg: &[u8],
297312
signature: &Signature<C>,
298313
recovery_id: RecoveryId,
299314
) -> Result<Self>
300315
where
301-
C: DigestAlgorithm,
316+
C: DigestPrimitive,
302317
{
303318
Self::recover_from_digest(C::Digest::new_with_prefix(msg), signature, recovery_id)
304319
}

0 commit comments

Comments
 (0)