Skip to content

Commit 2c80eca

Browse files
committed
Replace Digest requirement with EagerHash
1 parent b648424 commit 2c80eca

File tree

11 files changed

+102
-111
lines changed

11 files changed

+102
-111
lines changed

dsa/src/generate/secret_number.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{Components, signing_key::SigningKey};
66
use alloc::vec;
77
use core::cmp::min;
88
use crypto_bigint::{BoxedUint, NonZero, RandomBits, Resize};
9-
use digest::{Digest, FixedOutputReset, block_api::BlockSizeUser};
9+
use rfc6979::hmac::EagerHash;
1010
use signature::rand_core::TryCryptoRng;
1111
use zeroize::Zeroizing;
1212

@@ -25,7 +25,7 @@ pub fn secret_number_rfc6979<D>(
2525
hash: &[u8],
2626
) -> Result<(BoxedUint, BoxedUint), signature::Error>
2727
where
28-
D: Digest + BlockSizeUser + FixedOutputReset,
28+
D: EagerHash,
2929
{
3030
let q = signing_key.verifying_key().components().q();
3131
let size = (q.bits() / 8) as usize;

dsa/src/signing_key.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use crypto_bigint::{
1313
BoxedUint, NonZero, Resize,
1414
modular::{BoxedMontyForm, BoxedMontyParams},
1515
};
16-
use digest::{Digest, FixedOutputReset, Update, block_api::BlockSizeUser};
16+
use digest::Update;
17+
use rfc6979::hmac::EagerHash;
1718
use signature::{
1819
DigestSigner, MultipartSigner, RandomizedDigestSigner, Signer,
1920
hazmat::{PrehashSigner, RandomizedPrehashSigner},
@@ -94,7 +95,7 @@ impl SigningKey {
9495
#[cfg(feature = "hazmat")]
9596
pub fn sign_prehashed_rfc6979<D>(&self, prehash: &[u8]) -> Result<Signature, signature::Error>
9697
where
97-
D: Digest + BlockSizeUser + FixedOutputReset,
98+
D: EagerHash,
9899
{
99100
let k_kinv = crate::generate::secret_number_rfc6979::<D>(self, prehash)?;
100101
self.sign_prehashed(k_kinv, prehash)
@@ -158,7 +159,7 @@ impl Signer<Signature> for SigningKey {
158159
impl MultipartSigner<Signature> for SigningKey {
159160
fn try_multipart_sign(&self, msg: &[&[u8]]) -> Result<Signature, signature::Error> {
160161
self.try_sign_digest(|digest: &mut sha2::Sha256| {
161-
msg.iter().for_each(|slice| Digest::update(digest, slice));
162+
msg.iter().for_each(|slice| digest.update(slice));
162163
Ok(())
163164
})
164165
}
@@ -190,15 +191,15 @@ impl RandomizedPrehashSigner<Signature> for SigningKey {
190191

191192
impl<D> DigestSigner<D, Signature> for SigningKey
192193
where
193-
D: Digest + BlockSizeUser + FixedOutputReset,
194+
D: EagerHash + Update,
194195
{
195196
fn try_sign_digest<F: Fn(&mut D) -> Result<(), signature::Error>>(
196197
&self,
197198
f: F,
198199
) -> Result<Signature, signature::Error> {
199200
let mut digest = D::new();
200201
f(&mut digest)?;
201-
let hash = digest.finalize_fixed();
202+
let hash = digest.finalize();
202203
let ks = crate::generate::secret_number_rfc6979::<D>(self, &hash)?;
203204

204205
self.sign_prehashed(ks, &hash)
@@ -207,7 +208,7 @@ where
207208

208209
impl<D> RandomizedDigestSigner<D, Signature> for SigningKey
209210
where
210-
D: Digest + Update,
211+
D: EagerHash + Update,
211212
{
212213
fn try_sign_digest_with_rng<
213214
R: TryCryptoRng + ?Sized,

dsa/src/verifying_key.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use crypto_bigint::{
88
BoxedUint, NonZero, Resize,
99
modular::{BoxedMontyForm, BoxedMontyParams},
1010
};
11-
use digest::{Digest, Update};
11+
use digest::Update;
12+
use rfc6979::hmac::EagerHash;
1213
use signature::{DigestVerifier, MultipartVerifier, Verifier, hazmat::PrehashVerifier};
1314

1415
#[cfg(feature = "pkcs8")]
@@ -126,7 +127,7 @@ impl MultipartVerifier<Signature> for VerifyingKey {
126127
) -> Result<(), signature::Error> {
127128
self.verify_digest(
128129
|digest: &mut sha2::Sha256| {
129-
msg.iter().for_each(|slice| Digest::update(digest, slice));
130+
msg.iter().for_each(|slice| digest.update(slice));
130131
Ok(())
131132
},
132133
signature,
@@ -150,7 +151,7 @@ impl PrehashVerifier<Signature> for VerifyingKey {
150151

151152
impl<D> DigestVerifier<D, Signature> for VerifyingKey
152153
where
153-
D: Digest + Update,
154+
D: EagerHash + Update,
154155
{
155156
fn verify_digest<F: Fn(&mut D) -> Result<(), signature::Error>>(
156157
&self,

dsa/tests/deterministic.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#![cfg(feature = "hazmat")]
22
use crypto_bigint::BoxedUint;
3-
use digest::{Digest, FixedOutputReset, block_api::BlockSizeUser};
3+
use digest::Update;
44
use dsa::{Components, Signature, SigningKey, VerifyingKey};
5+
use rfc6979::hmac::EagerHash;
56
use sha1::Sha1;
67
use sha2::{Sha224, Sha256, Sha384, Sha512};
78
use signature::DigestSigner;
@@ -100,23 +101,23 @@ fn dsa_2048_signing_key() -> SigningKey {
100101
/// Generate a signature given the unhashed message and a private key
101102
fn generate_signature<D>(signing_key: SigningKey, data: &[u8]) -> Signature
102103
where
103-
D: Digest + BlockSizeUser + FixedOutputReset,
104+
D: EagerHash + Update,
104105
{
105-
signing_key.sign_digest(|digest: &mut D| Digest::update(digest, data))
106+
signing_key.sign_digest(|digest: &mut D| Update::update(digest, data))
106107
}
107108

108109
/// Generate a signature using the 1024-bit DSA key
109110
fn generate_1024_signature<D>(data: &[u8]) -> Signature
110111
where
111-
D: Digest + BlockSizeUser + FixedOutputReset,
112+
D: EagerHash + Update,
112113
{
113114
generate_signature::<D>(dsa_1024_signing_key(), data)
114115
}
115116

116117
/// Generate a signature using the 2048-bit DSA key
117118
fn generate_2048_signature<D>(data: &[u8]) -> Signature
118119
where
119-
D: Digest + BlockSizeUser + FixedOutputReset,
120+
D: EagerHash + Update,
120121
{
121122
generate_signature::<D>(dsa_2048_signing_key(), data)
122123
}

ecdsa/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ hazmat = []
4646
pkcs8 = ["digest", "elliptic-curve/pkcs8", "der"]
4747
pem = ["elliptic-curve/pem", "pkcs8"]
4848
serde = ["elliptic-curve/serde", "pkcs8", "serdect"]
49-
signing = ["arithmetic", "digest", "hazmat", "rfc6979"]
50-
verifying = ["arithmetic", "digest", "hazmat"]
49+
signature = ["arithmetic", "digest", "hazmat", "rfc6979"]
5150

5251
[package.metadata.docs.rs]
5352
all-features = true

ecdsa/src/hazmat.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ use {
2727
},
2828
};
2929

30-
#[cfg(feature = "digest")]
31-
use signature::digest::{Digest, FixedOutput, FixedOutputReset, block_api::BlockSizeUser};
32-
3330
#[cfg(feature = "rfc6979")]
3431
use elliptic_curve::FieldBytesEncoding;
3532

33+
#[cfg(any(feature = "digest", feature = "rfc6979"))]
34+
use rfc6979::hmac::EagerHash;
35+
3636
#[cfg(any(feature = "arithmetic", feature = "rfc6979"))]
3737
use crate::{Signature, elliptic_curve::array::ArraySize};
3838

@@ -44,7 +44,7 @@ use crate::{Signature, elliptic_curve::array::ArraySize};
4444
pub trait DigestAlgorithm: EcdsaCurve {
4545
/// Preferred digest to use when computing ECDSA signatures for this
4646
/// elliptic curve. This is typically a member of the SHA-2 family.
47-
type Digest: BlockSizeUser + Digest + FixedOutput + FixedOutputReset;
47+
type Digest: EagerHash + digest::Update;
4848
}
4949

5050
/// Partial implementation of the `bits2int` function as defined in
@@ -167,7 +167,7 @@ pub fn sign_prehashed_rfc6979<C, D>(
167167
) -> Result<(Signature<C>, RecoveryId)>
168168
where
169169
C: EcdsaCurve + CurveArithmetic,
170-
D: Digest + BlockSizeUser + FixedOutput + FixedOutputReset,
170+
D: EagerHash,
171171
SignatureSize<C>: ArraySize,
172172
{
173173
// From RFC6979 § 2.4:

ecdsa/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ pub mod der;
6565
pub mod dev;
6666
#[cfg(feature = "hazmat")]
6767
pub mod hazmat;
68-
#[cfg(feature = "signing")]
68+
#[cfg(feature = "signature")]
6969
mod signing;
70-
#[cfg(feature = "verifying")]
70+
#[cfg(feature = "signature")]
7171
mod verifying;
7272

7373
pub use crate::recovery::RecoveryId;
@@ -79,9 +79,9 @@ pub use elliptic_curve::{self, PrimeCurve, sec1::EncodedPoint};
7979
pub use signature::{self, Error, Result, SignatureEncoding};
8080
use zeroize::Zeroize;
8181

82-
#[cfg(feature = "signing")]
82+
#[cfg(feature = "signature")]
8383
pub use crate::signing::SigningKey;
84-
#[cfg(feature = "verifying")]
84+
#[cfg(feature = "signature")]
8585
pub use crate::verifying::VerifyingKey;
8686

8787
use core::{fmt, ops::Add};

ecdsa/src/recovery.rs

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,29 @@
22
33
use crate::{Error, Result};
44

5-
#[cfg(feature = "signing")]
5+
#[cfg(feature = "signature")]
66
use {
7-
crate::{SigningKey, hazmat::sign_prehashed_rfc6979},
8-
elliptic_curve::{FieldBytes, subtle::CtOption},
9-
signature::{
10-
DigestSigner, MultipartSigner, RandomizedDigestSigner, Signer,
11-
digest::{FixedOutput, Update},
12-
hazmat::{PrehashSigner, RandomizedPrehashSigner},
13-
rand_core::TryCryptoRng,
7+
crate::{
8+
EcdsaCurve, Signature, SignatureSize, SigningKey, VerifyingKey,
9+
hazmat::{DigestAlgorithm, bits2field, sign_prehashed_rfc6979, verify_prehashed},
1410
},
15-
};
16-
17-
#[cfg(feature = "verifying")]
18-
use {
19-
crate::{VerifyingKey, hazmat::verify_prehashed},
2011
elliptic_curve::{
2112
AffinePoint, FieldBytesEncoding, FieldBytesSize, Group, PrimeField, ProjectivePoint,
2213
bigint::CheckedAdd,
2314
ops::{LinearCombination, Reduce},
2415
point::DecompressPoint,
2516
sec1::{self, FromEncodedPoint, ToEncodedPoint},
2617
},
27-
};
28-
29-
#[cfg(any(feature = "signing", feature = "verifying"))]
30-
use {
31-
crate::{
32-
EcdsaCurve, Signature, SignatureSize,
33-
hazmat::{DigestAlgorithm, bits2field},
18+
elliptic_curve::{
19+
CurveArithmetic, FieldBytes, Scalar, array::ArraySize, ops::Invert, subtle::CtOption,
20+
},
21+
rfc6979::hmac::EagerHash,
22+
signature::{
23+
DigestSigner, MultipartSigner, RandomizedDigestSigner, Signer,
24+
digest::Digest,
25+
hazmat::{PrehashSigner, RandomizedPrehashSigner},
26+
rand_core::TryCryptoRng,
3427
},
35-
elliptic_curve::{CurveArithmetic, Scalar, array::ArraySize, ops::Invert},
36-
signature::digest::Digest,
3728
};
3829

3930
/// Recovery IDs, a.k.a. "recid".
@@ -89,7 +80,7 @@ impl RecoveryId {
8980
}
9081
}
9182

92-
#[cfg(feature = "verifying")]
83+
#[cfg(feature = "signature")]
9384
impl RecoveryId {
9485
/// Given a public key, message, and signature, use trial recovery
9586
/// to determine if a suitable recovery ID exists, or return an error
@@ -118,7 +109,7 @@ impl RecoveryId {
118109
) -> Result<Self>
119110
where
120111
C: EcdsaCurve + CurveArithmetic,
121-
D: Digest,
112+
D: EagerHash,
122113
AffinePoint<C>: DecompressPoint<C> + FromEncodedPoint<C> + ToEncodedPoint<C>,
123114
FieldBytesSize<C>: sec1::ModulusSize,
124115
SignatureSize<C>: ArraySize,
@@ -176,7 +167,7 @@ impl From<RecoveryId> for u8 {
176167
}
177168
}
178169

179-
#[cfg(feature = "signing")]
170+
#[cfg(feature = "signature")]
180171
impl<C> SigningKey<C>
181172
where
182173
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
@@ -213,7 +204,7 @@ where
213204
/// Sign the given message digest, returning a signature and recovery ID.
214205
pub fn sign_digest_recoverable<D>(&self, msg_digest: D) -> Result<(Signature<C>, RecoveryId)>
215206
where
216-
D: Digest,
207+
D: EagerHash,
217208
{
218209
self.sign_prehash_recoverable(&msg_digest.finalize())
219210
}
@@ -225,11 +216,11 @@ where
225216
}
226217
}
227218

228-
#[cfg(feature = "signing")]
219+
#[cfg(feature = "signature")]
229220
impl<C, D> DigestSigner<D, (Signature<C>, RecoveryId)> for SigningKey<C>
230221
where
231222
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
232-
D: Digest + Update,
223+
D: EagerHash + digest::Update,
233224
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
234225
SignatureSize<C>: ArraySize,
235226
{
@@ -243,7 +234,7 @@ where
243234
}
244235
}
245236

246-
#[cfg(feature = "signing")]
237+
#[cfg(feature = "signature")]
247238
impl<C> RandomizedPrehashSigner<(Signature<C>, RecoveryId)> for SigningKey<C>
248239
where
249240
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
@@ -259,11 +250,11 @@ where
259250
}
260251
}
261252

262-
#[cfg(feature = "signing")]
253+
#[cfg(feature = "signature")]
263254
impl<C, D> RandomizedDigestSigner<D, (Signature<C>, RecoveryId)> for SigningKey<C>
264255
where
265256
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
266-
D: Digest + FixedOutput,
257+
D: EagerHash + digest::Update,
267258
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
268259
SignatureSize<C>: ArraySize,
269260
{
@@ -274,11 +265,11 @@ where
274265
) -> Result<(Signature<C>, RecoveryId)> {
275266
let mut digest = D::new();
276267
f(&mut digest)?;
277-
self.sign_prehash_with_rng(rng, &digest.finalize_fixed())
268+
self.sign_prehash_with_rng(rng, &digest.finalize())
278269
}
279270
}
280271

281-
#[cfg(feature = "signing")]
272+
#[cfg(feature = "signature")]
282273
impl<C> PrehashSigner<(Signature<C>, RecoveryId)> for SigningKey<C>
283274
where
284275
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
@@ -290,7 +281,7 @@ where
290281
}
291282
}
292283

293-
#[cfg(feature = "signing")]
284+
#[cfg(feature = "signature")]
294285
impl<C> Signer<(Signature<C>, RecoveryId)> for SigningKey<C>
295286
where
296287
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
@@ -302,7 +293,7 @@ where
302293
}
303294
}
304295

305-
#[cfg(feature = "signing")]
296+
#[cfg(feature = "signature")]
306297
impl<C> MultipartSigner<(Signature<C>, RecoveryId)> for SigningKey<C>
307298
where
308299
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
@@ -311,13 +302,12 @@ where
311302
{
312303
fn try_multipart_sign(&self, msg: &[&[u8]]) -> Result<(Signature<C>, RecoveryId)> {
313304
let mut digest = C::Digest::new();
314-
msg.iter()
315-
.for_each(|slice| Digest::update(&mut digest, slice));
305+
msg.iter().for_each(|slice| digest.update(slice));
316306
self.sign_digest_recoverable(digest)
317307
}
318308
}
319309

320-
#[cfg(feature = "verifying")]
310+
#[cfg(feature = "signature")]
321311
impl<C> VerifyingKey<C>
322312
where
323313
C: EcdsaCurve + CurveArithmetic,
@@ -348,7 +338,7 @@ where
348338
recovery_id: RecoveryId,
349339
) -> Result<Self>
350340
where
351-
D: Digest,
341+
D: EagerHash,
352342
{
353343
Self::recover_from_prehash(&msg_digest.finalize(), signature, recovery_id)
354344
}

0 commit comments

Comments
 (0)