diff --git a/Cargo.lock b/Cargo.lock index 7d2dcbb9..46577b69 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -358,6 +358,7 @@ dependencies = [ "digest", "elliptic-curve", "hex-literal", + "hmac", "rfc6979", "serdect", "sha2", diff --git a/dsa/src/generate/secret_number.rs b/dsa/src/generate/secret_number.rs index e2665c78..b8516285 100644 --- a/dsa/src/generate/secret_number.rs +++ b/dsa/src/generate/secret_number.rs @@ -6,7 +6,7 @@ use crate::{Components, signing_key::SigningKey}; use alloc::vec; use core::cmp::min; use crypto_bigint::{BoxedUint, NonZero, RandomBits, Resize}; -use digest::{Digest, FixedOutputReset, block_api::BlockSizeUser}; +use rfc6979::hmac::EagerHash; use signature::rand_core::TryCryptoRng; use zeroize::Zeroizing; @@ -25,7 +25,7 @@ pub fn secret_number_rfc6979( hash: &[u8], ) -> Result<(BoxedUint, BoxedUint), signature::Error> where - D: Digest + BlockSizeUser + FixedOutputReset, + D: EagerHash, { let q = signing_key.verifying_key().components().q(); let size = (q.bits() / 8) as usize; diff --git a/dsa/src/signing_key.rs b/dsa/src/signing_key.rs index 09df9245..3a345260 100644 --- a/dsa/src/signing_key.rs +++ b/dsa/src/signing_key.rs @@ -13,7 +13,8 @@ use crypto_bigint::{ BoxedUint, NonZero, Resize, modular::{BoxedMontyForm, BoxedMontyParams}, }; -use digest::{Digest, FixedOutputReset, Update, block_api::BlockSizeUser}; +use digest::Update; +use rfc6979::hmac::EagerHash; use signature::{ DigestSigner, MultipartSigner, RandomizedDigestSigner, Signer, hazmat::{PrehashSigner, RandomizedPrehashSigner}, @@ -94,7 +95,7 @@ impl SigningKey { #[cfg(feature = "hazmat")] pub fn sign_prehashed_rfc6979(&self, prehash: &[u8]) -> Result where - D: Digest + BlockSizeUser + FixedOutputReset, + D: EagerHash, { let k_kinv = crate::generate::secret_number_rfc6979::(self, prehash)?; self.sign_prehashed(k_kinv, prehash) @@ -158,7 +159,7 @@ impl Signer for SigningKey { impl MultipartSigner for SigningKey { fn try_multipart_sign(&self, msg: &[&[u8]]) -> Result { self.try_sign_digest(|digest: &mut sha2::Sha256| { - msg.iter().for_each(|slice| Digest::update(digest, slice)); + msg.iter().for_each(|slice| digest.update(slice)); Ok(()) }) } @@ -190,7 +191,7 @@ impl RandomizedPrehashSigner for SigningKey { impl DigestSigner for SigningKey where - D: Digest + BlockSizeUser + FixedOutputReset, + D: EagerHash + Update, { fn try_sign_digest Result<(), signature::Error>>( &self, @@ -198,7 +199,7 @@ where ) -> Result { let mut digest = D::new(); f(&mut digest)?; - let hash = digest.finalize_fixed(); + let hash = digest.finalize(); let ks = crate::generate::secret_number_rfc6979::(self, &hash)?; self.sign_prehashed(ks, &hash) @@ -207,7 +208,7 @@ where impl RandomizedDigestSigner for SigningKey where - D: Digest + Update, + D: EagerHash + Update, { fn try_sign_digest_with_rng< R: TryCryptoRng + ?Sized, diff --git a/dsa/src/verifying_key.rs b/dsa/src/verifying_key.rs index 513aa37b..2ff7258a 100644 --- a/dsa/src/verifying_key.rs +++ b/dsa/src/verifying_key.rs @@ -8,7 +8,8 @@ use crypto_bigint::{ BoxedUint, NonZero, Resize, modular::{BoxedMontyForm, BoxedMontyParams}, }; -use digest::{Digest, Update}; +use digest::Update; +use rfc6979::hmac::EagerHash; use signature::{DigestVerifier, MultipartVerifier, Verifier, hazmat::PrehashVerifier}; #[cfg(feature = "pkcs8")] @@ -126,7 +127,7 @@ impl MultipartVerifier for VerifyingKey { ) -> Result<(), signature::Error> { self.verify_digest( |digest: &mut sha2::Sha256| { - msg.iter().for_each(|slice| Digest::update(digest, slice)); + msg.iter().for_each(|slice| digest.update(slice)); Ok(()) }, signature, @@ -150,7 +151,7 @@ impl PrehashVerifier for VerifyingKey { impl DigestVerifier for VerifyingKey where - D: Digest + Update, + D: EagerHash + Update, { fn verify_digest Result<(), signature::Error>>( &self, diff --git a/dsa/tests/deterministic.rs b/dsa/tests/deterministic.rs index 8995007a..a2cced1d 100644 --- a/dsa/tests/deterministic.rs +++ b/dsa/tests/deterministic.rs @@ -1,7 +1,8 @@ #![cfg(feature = "hazmat")] use crypto_bigint::BoxedUint; -use digest::{Digest, FixedOutputReset, block_api::BlockSizeUser}; +use digest::Update; use dsa::{Components, Signature, SigningKey, VerifyingKey}; +use rfc6979::hmac::EagerHash; use sha1::Sha1; use sha2::{Sha224, Sha256, Sha384, Sha512}; use signature::DigestSigner; @@ -100,15 +101,15 @@ fn dsa_2048_signing_key() -> SigningKey { /// Generate a signature given the unhashed message and a private key fn generate_signature(signing_key: SigningKey, data: &[u8]) -> Signature where - D: Digest + BlockSizeUser + FixedOutputReset, + D: EagerHash + Update, { - signing_key.sign_digest(|digest: &mut D| Digest::update(digest, data)) + signing_key.sign_digest(|digest: &mut D| Update::update(digest, data)) } /// Generate a signature using the 1024-bit DSA key fn generate_1024_signature(data: &[u8]) -> Signature where - D: Digest + BlockSizeUser + FixedOutputReset, + D: EagerHash + Update, { generate_signature::(dsa_1024_signing_key(), data) } @@ -116,7 +117,7 @@ where /// Generate a signature using the 2048-bit DSA key fn generate_2048_signature(data: &[u8]) -> Signature where - D: Digest + BlockSizeUser + FixedOutputReset, + D: EagerHash + Update, { generate_signature::(dsa_2048_signing_key(), data) } diff --git a/ecdsa/Cargo.toml b/ecdsa/Cargo.toml index 5cc405cd..cdae64fa 100644 --- a/ecdsa/Cargo.toml +++ b/ecdsa/Cargo.toml @@ -24,6 +24,7 @@ zeroize = { version = "1.5", default-features = false } # optional dependencies der = { version = "0.8.0-rc.8", optional = true } digest = { version = "0.11.0-rc.1", optional = true, default-features = false, features = ["oid"] } +hmac = { version = "0.13.0-rc.1", default-features = false, optional = true } rfc6979 = { version = "0.5.0-rc.1", optional = true } serdect = { version = "0.4", optional = true, default-features = false, features = ["alloc"] } sha2 = { version = "0.11.0-rc.2", optional = true, default-features = false, features = ["oid"] } @@ -39,15 +40,15 @@ default = ["digest"] alloc = ["elliptic-curve/alloc", "signature/alloc", "spki/alloc"] std = ["alloc", "elliptic-curve/std"] -arithmetic = ["elliptic-curve/arithmetic"] +arithmetic = ["dep:hmac", "dep:rfc6979", "elliptic-curve/arithmetic"] +algorithm = ["dep:rfc6979", "arithmetic", "digest", "hazmat"] dev = ["arithmetic", "digest", "elliptic-curve/dev", "hazmat"] -digest = ["dep:digest", "elliptic-curve/digest", "signature/digest"] +der = ["dep:der"] +digest = ["dep:digest", "dep:hmac", "elliptic-curve/digest", "signature/digest"] hazmat = [] -pkcs8 = ["digest", "elliptic-curve/pkcs8", "der"] +pkcs8 = ["der", "digest", "elliptic-curve/pkcs8"] pem = ["elliptic-curve/pem", "pkcs8"] -serde = ["elliptic-curve/serde", "pkcs8", "serdect"] -signing = ["arithmetic", "digest", "hazmat", "rfc6979"] -verifying = ["arithmetic", "digest", "hazmat"] +serde = ["dep:serdect", "elliptic-curve/serde", "pkcs8"] [package.metadata.docs.rs] all-features = true diff --git a/ecdsa/src/hazmat.rs b/ecdsa/src/hazmat.rs index e75f1fc2..911611f7 100644 --- a/ecdsa/src/hazmat.rs +++ b/ecdsa/src/hazmat.rs @@ -27,14 +27,14 @@ use { }, }; -#[cfg(feature = "digest")] -use signature::digest::{Digest, FixedOutput, FixedOutputReset, block_api::BlockSizeUser}; - -#[cfg(feature = "rfc6979")] -use elliptic_curve::FieldBytesEncoding; +#[cfg(feature = "arithmetic")] +use crate::{ + Signature, + elliptic_curve::{FieldBytesEncoding, array::ArraySize}, +}; -#[cfg(any(feature = "arithmetic", feature = "rfc6979"))] -use crate::{Signature, elliptic_curve::array::ArraySize}; +#[cfg(any(feature = "arithmetic", feature = "digest"))] +use hmac::EagerHash; /// Bind a preferred [`Digest`] algorithm to an elliptic curve type. /// @@ -44,7 +44,7 @@ use crate::{Signature, elliptic_curve::array::ArraySize}; pub trait DigestAlgorithm: EcdsaCurve { /// Preferred digest to use when computing ECDSA signatures for this /// elliptic curve. This is typically a member of the SHA-2 family. - type Digest: BlockSizeUser + Digest + FixedOutput + FixedOutputReset; + type Digest: EagerHash + digest::Update; } /// Partial implementation of the `bits2int` function as defined in @@ -159,7 +159,7 @@ where /// entropy `ad`. /// /// [RFC6979]: https://datatracker.ietf.org/doc/html/rfc6979 -#[cfg(feature = "rfc6979")] +#[cfg(feature = "arithmetic")] pub fn sign_prehashed_rfc6979( d: &NonZeroScalar, z: &FieldBytes, @@ -167,7 +167,7 @@ pub fn sign_prehashed_rfc6979( ) -> Result<(Signature, RecoveryId)> where C: EcdsaCurve + CurveArithmetic, - D: Digest + BlockSizeUser + FixedOutput + FixedOutputReset, + D: EagerHash, SignatureSize: ArraySize, { // From RFC6979 ยง 2.4: diff --git a/ecdsa/src/lib.rs b/ecdsa/src/lib.rs index 71537d6e..33495a8a 100644 --- a/ecdsa/src/lib.rs +++ b/ecdsa/src/lib.rs @@ -65,9 +65,9 @@ pub mod der; pub mod dev; #[cfg(feature = "hazmat")] pub mod hazmat; -#[cfg(feature = "signing")] +#[cfg(feature = "algorithm")] mod signing; -#[cfg(feature = "verifying")] +#[cfg(feature = "algorithm")] mod verifying; pub use crate::recovery::RecoveryId; @@ -79,9 +79,9 @@ pub use elliptic_curve::{self, PrimeCurve, sec1::EncodedPoint}; pub use signature::{self, Error, Result, SignatureEncoding}; use zeroize::Zeroize; -#[cfg(feature = "signing")] +#[cfg(feature = "algorithm")] pub use crate::signing::SigningKey; -#[cfg(feature = "verifying")] +#[cfg(feature = "algorithm")] pub use crate::verifying::VerifyingKey; use core::{fmt, ops::Add}; diff --git a/ecdsa/src/recovery.rs b/ecdsa/src/recovery.rs index 4009c160..e9234c02 100644 --- a/ecdsa/src/recovery.rs +++ b/ecdsa/src/recovery.rs @@ -2,21 +2,12 @@ use crate::{Error, Result}; -#[cfg(feature = "signing")] +#[cfg(feature = "algorithm")] use { - crate::{SigningKey, hazmat::sign_prehashed_rfc6979}, - elliptic_curve::{FieldBytes, subtle::CtOption}, - signature::{ - DigestSigner, MultipartSigner, RandomizedDigestSigner, Signer, - digest::{FixedOutput, Update}, - hazmat::{PrehashSigner, RandomizedPrehashSigner}, - rand_core::TryCryptoRng, + crate::{ + EcdsaCurve, Signature, SignatureSize, SigningKey, VerifyingKey, + hazmat::{DigestAlgorithm, bits2field, sign_prehashed_rfc6979, verify_prehashed}, }, -}; - -#[cfg(feature = "verifying")] -use { - crate::{VerifyingKey, hazmat::verify_prehashed}, elliptic_curve::{ AffinePoint, FieldBytesEncoding, FieldBytesSize, Group, PrimeField, ProjectivePoint, bigint::CheckedAdd, @@ -24,16 +15,16 @@ use { point::DecompressPoint, sec1::{self, FromEncodedPoint, ToEncodedPoint}, }, -}; - -#[cfg(any(feature = "signing", feature = "verifying"))] -use { - crate::{ - EcdsaCurve, Signature, SignatureSize, - hazmat::{DigestAlgorithm, bits2field}, + elliptic_curve::{ + CurveArithmetic, FieldBytes, Scalar, array::ArraySize, ops::Invert, subtle::CtOption, + }, + rfc6979::hmac::EagerHash, + signature::{ + DigestSigner, MultipartSigner, RandomizedDigestSigner, Signer, + digest::Digest, + hazmat::{PrehashSigner, RandomizedPrehashSigner}, + rand_core::TryCryptoRng, }, - elliptic_curve::{CurveArithmetic, Scalar, array::ArraySize, ops::Invert}, - signature::digest::Digest, }; /// Recovery IDs, a.k.a. "recid". @@ -89,7 +80,7 @@ impl RecoveryId { } } -#[cfg(feature = "verifying")] +#[cfg(feature = "algorithm")] impl RecoveryId { /// Given a public key, message, and signature, use trial recovery /// to determine if a suitable recovery ID exists, or return an error @@ -118,7 +109,7 @@ impl RecoveryId { ) -> Result where C: EcdsaCurve + CurveArithmetic, - D: Digest, + D: EagerHash, AffinePoint: DecompressPoint + FromEncodedPoint + ToEncodedPoint, FieldBytesSize: sec1::ModulusSize, SignatureSize: ArraySize, @@ -176,7 +167,7 @@ impl From for u8 { } } -#[cfg(feature = "signing")] +#[cfg(feature = "algorithm")] impl SigningKey where C: EcdsaCurve + CurveArithmetic + DigestAlgorithm, @@ -213,7 +204,7 @@ where /// Sign the given message digest, returning a signature and recovery ID. pub fn sign_digest_recoverable(&self, msg_digest: D) -> Result<(Signature, RecoveryId)> where - D: Digest, + D: EagerHash, { self.sign_prehash_recoverable(&msg_digest.finalize()) } @@ -225,11 +216,11 @@ where } } -#[cfg(feature = "signing")] +#[cfg(feature = "algorithm")] impl DigestSigner, RecoveryId)> for SigningKey where C: EcdsaCurve + CurveArithmetic + DigestAlgorithm, - D: Digest + Update, + D: EagerHash + digest::Update, Scalar: Invert>>, SignatureSize: ArraySize, { @@ -243,7 +234,7 @@ where } } -#[cfg(feature = "signing")] +#[cfg(feature = "algorithm")] impl RandomizedPrehashSigner<(Signature, RecoveryId)> for SigningKey where C: EcdsaCurve + CurveArithmetic + DigestAlgorithm, @@ -259,11 +250,11 @@ where } } -#[cfg(feature = "signing")] +#[cfg(feature = "algorithm")] impl RandomizedDigestSigner, RecoveryId)> for SigningKey where C: EcdsaCurve + CurveArithmetic + DigestAlgorithm, - D: Digest + FixedOutput, + D: EagerHash + digest::Update, Scalar: Invert>>, SignatureSize: ArraySize, { @@ -274,11 +265,11 @@ where ) -> Result<(Signature, RecoveryId)> { let mut digest = D::new(); f(&mut digest)?; - self.sign_prehash_with_rng(rng, &digest.finalize_fixed()) + self.sign_prehash_with_rng(rng, &digest.finalize()) } } -#[cfg(feature = "signing")] +#[cfg(feature = "algorithm")] impl PrehashSigner<(Signature, RecoveryId)> for SigningKey where C: EcdsaCurve + CurveArithmetic + DigestAlgorithm, @@ -290,7 +281,7 @@ where } } -#[cfg(feature = "signing")] +#[cfg(feature = "algorithm")] impl Signer<(Signature, RecoveryId)> for SigningKey where C: EcdsaCurve + CurveArithmetic + DigestAlgorithm, @@ -302,7 +293,7 @@ where } } -#[cfg(feature = "signing")] +#[cfg(feature = "algorithm")] impl MultipartSigner<(Signature, RecoveryId)> for SigningKey where C: EcdsaCurve + CurveArithmetic + DigestAlgorithm, @@ -311,13 +302,12 @@ where { fn try_multipart_sign(&self, msg: &[&[u8]]) -> Result<(Signature, RecoveryId)> { let mut digest = C::Digest::new(); - msg.iter() - .for_each(|slice| Digest::update(&mut digest, slice)); + msg.iter().for_each(|slice| digest.update(slice)); self.sign_digest_recoverable(digest) } } -#[cfg(feature = "verifying")] +#[cfg(feature = "algorithm")] impl VerifyingKey where C: EcdsaCurve + CurveArithmetic, @@ -348,7 +338,7 @@ where recovery_id: RecoveryId, ) -> Result where - D: Digest, + D: EagerHash, { Self::recover_from_prehash(&msg_digest.finalize(), signature, recovery_id) } diff --git a/ecdsa/src/signing.rs b/ecdsa/src/signing.rs index 060f27bc..cb31ef44 100644 --- a/ecdsa/src/signing.rs +++ b/ecdsa/src/signing.rs @@ -5,7 +5,7 @@ use crate::{ hazmat::{DigestAlgorithm, bits2field, sign_prehashed_rfc6979}, }; use core::fmt::{self, Debug}; -use digest::{Digest, FixedOutput, const_oid::AssociatedOid}; +use digest::{Update, const_oid::AssociatedOid}; use elliptic_curve::{ CurveArithmetic, FieldBytes, NonZeroScalar, Scalar, SecretKey, array::ArraySize, @@ -14,6 +14,7 @@ use elliptic_curve::{ subtle::{Choice, ConstantTimeEq, CtOption}, zeroize::{Zeroize, ZeroizeOnDrop}, }; +use rfc6979::hmac::EagerHash; use signature::{ DigestSigner, MultipartSigner, RandomizedDigestSigner, RandomizedMultipartSigner, RandomizedSigner, Signer, @@ -22,11 +23,14 @@ use signature::{ }; #[cfg(feature = "der")] -use {crate::der, core::ops::Add, elliptic_curve::FieldBytesSize}; +use {crate::der, core::ops::Add}; #[cfg(feature = "pem")] use {core::str::FromStr, elliptic_curve::pkcs8::DecodePrivateKey}; +#[cfg(any(feature = "der", feature = "pem"))] +use elliptic_curve::FieldBytesSize; + #[cfg(feature = "pkcs8")] use crate::elliptic_curve::{ AffinePoint, @@ -38,7 +42,7 @@ use crate::elliptic_curve::{ sec1::{self, FromEncodedPoint, ToEncodedPoint}, }; -#[cfg(feature = "verifying")] +#[cfg(feature = "algorithm")] use {crate::VerifyingKey, elliptic_curve::PublicKey, signature::KeypairRef}; #[cfg(all(feature = "alloc", feature = "pkcs8"))] @@ -71,7 +75,7 @@ where secret_scalar: NonZeroScalar, /// Verifying key which corresponds to this signing key. - #[cfg(feature = "verifying")] + #[cfg(feature = "algorithm")] verifying_key: VerifyingKey, } @@ -124,7 +128,7 @@ where } /// Get the [`VerifyingKey`] which corresponds to this [`SigningKey`]. - #[cfg(feature = "verifying")] + #[cfg(feature = "algorithm")] pub fn verifying_key(&self) -> &VerifyingKey { &self.verifying_key } @@ -141,14 +145,14 @@ where impl DigestSigner> for SigningKey where C: EcdsaCurve + CurveArithmetic + DigestAlgorithm, - D: Digest + FixedOutput, + D: EagerHash + Update, Scalar: Invert>>, SignatureSize: ArraySize, { fn try_sign_digest Result<()>>(&self, f: F) -> Result> { let mut digest = D::new(); f(&mut digest)?; - self.sign_prehash(&digest.finalize_fixed()) + self.sign_prehash(&digest.finalize()) } } @@ -200,7 +204,7 @@ where impl RandomizedDigestSigner> for SigningKey where C: EcdsaCurve + CurveArithmetic + DigestAlgorithm, - D: Digest + FixedOutput, + D: EagerHash + Update, Scalar: Invert>>, SignatureSize: ArraySize, { @@ -211,7 +215,7 @@ where ) -> Result> { let mut digest = D::new(); f(&mut digest)?; - self.sign_prehash_with_rng(rng, &digest.finalize_fixed()) + self.sign_prehash_with_rng(rng, &digest.finalize()) } } @@ -279,7 +283,7 @@ where impl DigestSigner> for SigningKey where C: EcdsaCurve + CurveArithmetic + DigestAlgorithm, - D: AssociatedOid + Digest + FixedOutput, + D: AssociatedOid + EagerHash + Update, Scalar: Invert>>, SignatureSize: ArraySize, { @@ -349,7 +353,7 @@ where impl RandomizedDigestSigner> for SigningKey where C: EcdsaCurve + CurveArithmetic + DigestAlgorithm, - D: Digest + FixedOutput, + D: EagerHash + Update, Scalar: Invert>>, SignatureSize: ArraySize, der::MaxSize: ArraySize, @@ -388,7 +392,7 @@ where impl DigestSigner> for SigningKey where C: EcdsaCurve + CurveArithmetic + DigestAlgorithm, - D: Digest + FixedOutput, + D: EagerHash + Update, Scalar: Invert>>, SignatureSize: ArraySize, der::MaxSize: ArraySize, @@ -440,7 +444,7 @@ where // Other trait impls // -#[cfg(feature = "verifying")] +#[cfg(feature = "algorithm")] impl AsRef> for SigningKey where C: EcdsaCurve + CurveArithmetic, @@ -511,12 +515,12 @@ where SignatureSize: ArraySize, { fn from(secret_scalar: NonZeroScalar) -> Self { - #[cfg(feature = "verifying")] + #[cfg(feature = "algorithm")] let public_key = PublicKey::from_secret_scalar(&secret_scalar); Self { secret_scalar, - #[cfg(feature = "verifying")] + #[cfg(feature = "algorithm")] verifying_key: public_key.into(), } } @@ -587,7 +591,7 @@ where { } -#[cfg(feature = "verifying")] +#[cfg(feature = "algorithm")] impl From> for VerifyingKey where C: EcdsaCurve + CurveArithmetic, @@ -599,7 +603,7 @@ where } } -#[cfg(feature = "verifying")] +#[cfg(feature = "algorithm")] impl From<&SigningKey> for VerifyingKey where C: EcdsaCurve + CurveArithmetic, @@ -611,7 +615,7 @@ where } } -#[cfg(feature = "verifying")] +#[cfg(feature = "algorithm")] impl KeypairRef for SigningKey where C: EcdsaCurve + CurveArithmetic, diff --git a/ecdsa/src/verifying.rs b/ecdsa/src/verifying.rs index b41bf41f..8eeff02b 100644 --- a/ecdsa/src/verifying.rs +++ b/ecdsa/src/verifying.rs @@ -5,6 +5,7 @@ use crate::{ hazmat::{self, DigestAlgorithm, bits2field}, }; use core::{cmp::Ordering, fmt::Debug}; +use digest::Update; use elliptic_curve::{ AffinePoint, CurveArithmetic, FieldBytesSize, ProjectivePoint, PublicKey, array::ArraySize, @@ -12,11 +13,8 @@ use elliptic_curve::{ scalar::IsHigh, sec1::{self, CompressedPoint, EncodedPoint, FromEncodedPoint, ToEncodedPoint}, }; -use signature::{ - DigestVerifier, MultipartVerifier, Verifier, - digest::{Digest, FixedOutput}, - hazmat::PrehashVerifier, -}; +use rfc6979::hmac::EagerHash; +use signature::{DigestVerifier, MultipartVerifier, Verifier, hazmat::PrehashVerifier}; #[cfg(feature = "alloc")] use alloc::boxed::Box; @@ -147,7 +145,7 @@ where impl DigestVerifier> for VerifyingKey where C: EcdsaCurve + CurveArithmetic, - D: Digest + FixedOutput, + D: EagerHash + Update, SignatureSize: ArraySize, { fn verify_digest Result<()>>( @@ -223,26 +221,28 @@ where SignatureSize: ArraySize, { fn multipart_verify(&self, msg: &[&[u8]], sig: &SignatureWithOid) -> Result<()> { + use digest::FixedOutput; + match sig.oid() { ECDSA_SHA224_OID => { - let mut digest = Sha224::new(); + let mut digest = Sha224::default(); msg.iter().for_each(|slice| digest.update(slice)); - self.verify_prehash(&digest.finalize(), sig.signature()) + self.verify_prehash(&digest.finalize_fixed(), sig.signature()) } ECDSA_SHA256_OID => { - let mut digest = Sha256::new(); + let mut digest = Sha256::default(); msg.iter().for_each(|slice| digest.update(slice)); - self.verify_prehash(&digest.finalize(), sig.signature()) + self.verify_prehash(&digest.finalize_fixed(), sig.signature()) } ECDSA_SHA384_OID => { - let mut digest = Sha384::new(); + let mut digest = Sha384::default(); msg.iter().for_each(|slice| digest.update(slice)); - self.verify_prehash(&digest.finalize(), sig.signature()) + self.verify_prehash(&digest.finalize_fixed(), sig.signature()) } ECDSA_SHA512_OID => { - let mut digest = Sha512::new(); + let mut digest = Sha512::default(); msg.iter().for_each(|slice| digest.update(slice)); - self.verify_prehash(&digest.finalize(), sig.signature()) + self.verify_prehash(&digest.finalize_fixed(), sig.signature()) } _ => Err(Error::new()), } @@ -253,7 +253,7 @@ where impl DigestVerifier> for VerifyingKey where C: EcdsaCurve + CurveArithmetic, - D: Digest + FixedOutput, + D: EagerHash + Update, SignatureSize: ArraySize, der::MaxSize: ArraySize, as Add>::Output: Add + ArraySize, diff --git a/rfc6979/src/lib.rs b/rfc6979/src/lib.rs index e54151d6..8e13e579 100644 --- a/rfc6979/src/lib.rs +++ b/rfc6979/src/lib.rs @@ -39,14 +39,14 @@ mod ct; +pub use hmac; pub use hmac::digest::array::typenum::consts; use hmac::{ - SimpleHmacReset, + EagerHash, HmacReset, digest::{ - Digest, FixedOutput, FixedOutputReset, KeyInit, Mac, + KeyInit, Mac, OutputSizeUser, array::{Array, ArraySize}, - block_api::BlockSizeUser, }, }; @@ -66,7 +66,7 @@ pub fn generate_k( data: &[u8], ) -> Array where - D: Digest + BlockSizeUser + FixedOutput + FixedOutputReset, + D: EagerHash, N: ArraySize, { let mut k = Array::default(); @@ -88,7 +88,7 @@ where #[inline] pub fn generate_k_mut(x: &[u8], q: &[u8], h: &[u8], data: &[u8], k: &mut [u8]) where - D: Digest + BlockSizeUser + FixedOutput + FixedOutputReset, + D: EagerHash, { let k_len = k.len(); assert_eq!(k_len, x.len()); @@ -121,22 +121,22 @@ where /// deterministic ephemeral scalar `k`. pub struct HmacDrbg where - D: Digest + BlockSizeUser + FixedOutputReset, + D: EagerHash, { /// HMAC key `K` (see RFC 6979 Section 3.2.c) - k: SimpleHmacReset, + k: HmacReset, /// Chaining value `V` (see RFC 6979 Section 3.2.c) - v: Array, + v: Array::OutputSize>, } impl HmacDrbg where - D: Digest + BlockSizeUser + FixedOutputReset, + D: EagerHash, { /// Initialize `HMAC_DRBG` pub fn new(entropy_input: &[u8], nonce: &[u8], personalization_string: &[u8]) -> Self { - let mut k = SimpleHmacReset::new(&Default::default()); + let mut k = HmacReset::new(&Default::default()); let mut v = Array::default(); v.fill(0x01); @@ -147,7 +147,7 @@ where k.update(entropy_input); k.update(nonce); k.update(personalization_string); - k = SimpleHmacReset::new_from_slice(&k.finalize().into_bytes()).expect("HMAC error"); + k = HmacReset::new_from_slice(&k.finalize().into_bytes()).expect("HMAC error"); // Steps 3.2.e,g: v = HMAC_k(v) k.update(&v); @@ -176,8 +176,8 @@ where self.k.update(&self.v); self.k.update(&[0x00]); - self.k = SimpleHmacReset::new_from_slice(&self.k.finalize_reset().into_bytes()) - .expect("HMAC error"); + self.k = + HmacReset::new_from_slice(&self.k.finalize_reset().into_bytes()).expect("HMAC error"); self.k.update(&self.v); self.v = self.k.finalize_reset().into_bytes(); }