Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dsa/src/generate/secret_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::block_api::EagerHash;
use digest::{Digest, FixedOutputReset, common::BlockSizeUser};
use signature::rand_core::TryCryptoRng;
use zeroize::Zeroizing;

Expand All @@ -25,7 +25,7 @@ pub(crate) fn secret_number_rfc6979<D>(
hash: &[u8],
) -> Result<(BoxedUint, BoxedUint), signature::Error>
where
D: EagerHash,
D: Digest + BlockSizeUser + FixedOutputReset,
{
let q = signing_key.verifying_key().components().q();
let size = (q.bits() / 8) as usize;
Expand Down
8 changes: 4 additions & 4 deletions dsa/src/signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crypto_bigint::{
BoxedUint, ConcatenatingMul, NonZero, Resize,
modular::{BoxedMontyForm, BoxedMontyParams},
};
use digest::{Update, block_api::EagerHash};
use digest::{Digest, FixedOutputReset, common::BlockSizeUser};
use signature::{
DigestSigner, MultipartSigner, RandomizedDigestSigner, Signer,
hazmat::{PrehashSigner, RandomizedPrehashSigner},
Expand Down Expand Up @@ -94,7 +94,7 @@ impl SigningKey {
#[cfg(feature = "hazmat")]
pub fn sign_prehashed_rfc6979<D>(&self, prehash: &[u8]) -> Result<Signature, signature::Error>
where
D: EagerHash,
D: Digest + BlockSizeUser + FixedOutputReset,
{
let k_kinv = crate::generate::secret_number_rfc6979::<D>(self, prehash)?;
self.sign_prehashed(k_kinv, prehash)
Expand Down Expand Up @@ -193,7 +193,7 @@ impl RandomizedPrehashSigner<Signature> for SigningKey {

impl<D> DigestSigner<D, Signature> for SigningKey
where
D: EagerHash + Update,
D: Digest + FixedOutputReset + BlockSizeUser,
{
fn try_sign_digest<F: Fn(&mut D) -> Result<(), signature::Error>>(
&self,
Expand All @@ -210,7 +210,7 @@ where

impl<D> RandomizedDigestSigner<D, Signature> for SigningKey
where
D: EagerHash + Update,
D: Digest + FixedOutputReset + BlockSizeUser,
{
fn try_sign_digest_with_rng<
R: TryCryptoRng + ?Sized,
Expand Down
10 changes: 5 additions & 5 deletions dsa/tests/deterministic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![cfg(feature = "hazmat")]
use crypto_bigint::BoxedUint;
use digest::{Update, block_api::EagerHash};
use digest::{Digest, FixedOutputReset, common::BlockSizeUser};
use dsa::{Components, Signature, SigningKey, VerifyingKey};
use sha1::Sha1;
use sha2::{Sha224, Sha256, Sha384, Sha512};
Expand Down Expand Up @@ -100,23 +100,23 @@ fn dsa_2048_signing_key() -> SigningKey {
/// Generate a signature given the unhashed message and a private key
fn generate_signature<D>(signing_key: SigningKey, data: &[u8]) -> Signature
where
D: EagerHash + Update,
D: Digest + FixedOutputReset + BlockSizeUser,
{
signing_key.sign_digest(|digest: &mut D| Update::update(digest, data))
signing_key.sign_digest(|digest: &mut D| Digest::update(digest, data))
}

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

/// Generate a signature using the 2048-bit DSA key
fn generate_2048_signature<D>(data: &[u8]) -> Signature
where
D: EagerHash + Update,
D: Digest + FixedOutputReset + BlockSizeUser,
{
generate_signature::<D>(dsa_2048_signing_key(), data)
}
Expand Down
8 changes: 4 additions & 4 deletions ecdsa/src/hazmat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ use {
};

#[cfg(feature = "digest")]
use digest::block_api::EagerHash;
use digest::{Digest, FixedOutput, FixedOutputReset, common::BlockSizeUser};

/// Bind a preferred [`EagerHash`] algorithm to an elliptic curve type.
/// Bind a preferred [`Digest`] algorithm to an elliptic curve type.
///
/// Generally there is a preferred variety of the SHA-2 family used with ECDSA
/// for a particular elliptic curve.
#[cfg(feature = "digest")]
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: EagerHash + digest::Update;
type Digest: BlockSizeUser + Digest + FixedOutput + FixedOutputReset;
}

/// Partial implementation of the `bits2int` function as defined in
Expand Down Expand Up @@ -166,7 +166,7 @@ pub fn sign_prehashed_rfc6979<C, D>(
) -> Result<(Signature<C>, RecoveryId)>
where
C: EcdsaCurve + CurveArithmetic,
D: EagerHash,
D: Digest + BlockSizeUser + FixedOutput + FixedOutputReset,
SignatureSize<C>: ArraySize,
{
// From RFC6979 § 2.4:
Expand Down
17 changes: 9 additions & 8 deletions ecdsa/src/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use {
EcdsaCurve, Signature, SignatureSize, SigningKey, VerifyingKey,
hazmat::{DigestAlgorithm, bits2field, sign_prehashed_rfc6979, verify_prehashed},
},
digest::{Digest, block_api::EagerHash},
digest::{Digest, FixedOutputReset, Update},
elliptic_curve::{
AffinePoint, FieldBytesEncoding, FieldBytesSize, Group, PrimeField, ProjectivePoint,
bigint::CheckedAdd,
Expand Down Expand Up @@ -108,7 +108,7 @@ impl RecoveryId {
) -> Result<Self>
where
C: EcdsaCurve + CurveArithmetic,
D: EagerHash,
D: Digest,
AffinePoint<C>: DecompressPoint<C> + FromSec1Point<C> + ToSec1Point<C>,
FieldBytesSize<C>: sec1::ModulusSize,
SignatureSize<C>: ArraySize,
Expand Down Expand Up @@ -203,7 +203,7 @@ where
/// Sign the given message digest, returning a signature and recovery ID.
pub fn sign_digest_recoverable<D>(&self, msg_digest: D) -> Result<(Signature<C>, RecoveryId)>
where
D: EagerHash,
D: Digest,
{
self.sign_prehash_recoverable(&msg_digest.finalize())
}
Expand All @@ -219,7 +219,7 @@ where
impl<C, D> DigestSigner<D, (Signature<C>, RecoveryId)> for SigningKey<C>
where
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
D: EagerHash + digest::Update,
D: Digest + Update,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
SignatureSize<C>: ArraySize,
{
Expand Down Expand Up @@ -253,7 +253,7 @@ where
impl<C, D> RandomizedDigestSigner<D, (Signature<C>, RecoveryId)> for SigningKey<C>
where
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
D: EagerHash + digest::Update,
D: Digest + Update + FixedOutputReset,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
SignatureSize<C>: ArraySize,
{
Expand All @@ -264,7 +264,7 @@ where
) -> Result<(Signature<C>, RecoveryId)> {
let mut digest = D::new();
f(&mut digest)?;
self.sign_prehash_with_rng(rng, &digest.finalize())
self.sign_prehash_with_rng(rng, &digest.finalize_reset())
}
}

Expand Down Expand Up @@ -301,7 +301,8 @@ where
{
fn try_multipart_sign(&self, msg: &[&[u8]]) -> Result<(Signature<C>, RecoveryId)> {
let mut digest = C::Digest::new();
msg.iter().for_each(|slice| digest.update(slice));
msg.iter()
.for_each(|slice| Update::update(&mut digest, slice));
self.sign_digest_recoverable(digest)
}
}
Expand Down Expand Up @@ -337,7 +338,7 @@ where
recovery_id: RecoveryId,
) -> Result<Self>
where
D: EagerHash,
D: Digest,
{
Self::recover_from_prehash(&msg_digest.finalize(), signature, recovery_id)
}
Expand Down
16 changes: 8 additions & 8 deletions ecdsa/src/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
hazmat::{DigestAlgorithm, bits2field, sign_prehashed_rfc6979},
};
use core::fmt::{self, Debug};
use digest::{Update, block_api::EagerHash, const_oid::AssociatedOid};
use digest::{Digest, FixedOutput, const_oid::AssociatedOid};
use elliptic_curve::{
CurveArithmetic, FieldBytes, Generate, NonZeroScalar, Scalar, SecretKey,
array::ArraySize,
Expand Down Expand Up @@ -146,14 +146,14 @@ where
impl<C, D> DigestSigner<D, Signature<C>> for SigningKey<C>
where
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
D: EagerHash + Update,
D: Digest + FixedOutput,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
SignatureSize<C>: ArraySize,
{
fn try_sign_digest<F: Fn(&mut D) -> Result<()>>(&self, f: F) -> Result<Signature<C>> {
let mut digest = D::new();
f(&mut digest)?;
self.sign_prehash(&digest.finalize())
self.sign_prehash(&digest.finalize_fixed())
}
}

Expand Down Expand Up @@ -205,7 +205,7 @@ where
impl<C, D> RandomizedDigestSigner<D, Signature<C>> for SigningKey<C>
where
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
D: EagerHash + Update,
D: Digest + FixedOutput,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
SignatureSize<C>: ArraySize,
{
Expand All @@ -216,7 +216,7 @@ where
) -> Result<Signature<C>> {
let mut digest = D::new();
f(&mut digest)?;
self.sign_prehash_with_rng(rng, &digest.finalize())
self.sign_prehash_with_rng(rng, &digest.finalize_fixed())
}
}

Expand Down Expand Up @@ -284,7 +284,7 @@ where
impl<C, D> DigestSigner<D, SignatureWithOid<C>> for SigningKey<C>
where
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
D: AssociatedOid + EagerHash + Update,
D: AssociatedOid + Digest + FixedOutput,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
SignatureSize<C>: ArraySize,
{
Expand Down Expand Up @@ -354,7 +354,7 @@ where
impl<C, D> RandomizedDigestSigner<D, der::Signature<C>> for SigningKey<C>
where
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
D: EagerHash + Update,
D: Digest + FixedOutput,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
SignatureSize<C>: ArraySize,
der::MaxSize<C>: ArraySize,
Expand Down Expand Up @@ -393,7 +393,7 @@ where
impl<D, C> DigestSigner<D, der::Signature<C>> for SigningKey<C>
where
C: EcdsaCurve + CurveArithmetic + DigestAlgorithm,
D: EagerHash + Update,
D: Digest + FixedOutput,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>>,
SignatureSize<C>: ArraySize,
der::MaxSize<C>: ArraySize,
Expand Down
30 changes: 16 additions & 14 deletions ecdsa/src/verifying.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
hazmat::{self, DigestAlgorithm, bits2field},
};
use core::{cmp::Ordering, fmt::Debug};
use digest::{Update, block_api::EagerHash};
use digest::{Digest, Update};
use elliptic_curve::{
AffinePoint, CurveArithmetic, FieldBytesSize, ProjectivePoint, PublicKey,
array::ArraySize,
Expand Down Expand Up @@ -144,7 +144,7 @@ where
impl<C, D> DigestVerifier<D, Signature<C>> for VerifyingKey<C>
where
C: EcdsaCurve + CurveArithmetic,
D: EagerHash + Update,
D: Digest + Update,
SignatureSize<C>: ArraySize,
{
fn verify_digest<F: Fn(&mut D) -> Result<()>>(
Expand Down Expand Up @@ -194,7 +194,7 @@ where
fn multipart_verify(&self, msg: &[&[u8]], signature: &Signature<C>) -> Result<()> {
self.verify_digest(
|digest: &mut C::Digest| {
msg.iter().for_each(|slice| digest.update(slice));
msg.iter().for_each(|slice| Update::update(digest, slice));
Ok(())
},
signature,
Expand All @@ -220,28 +220,30 @@ where
SignatureSize<C>: ArraySize,
{
fn multipart_verify(&self, msg: &[&[u8]], sig: &SignatureWithOid<C>) -> Result<()> {
use digest::FixedOutput;

match sig.oid() {
ECDSA_SHA224_OID => {
let mut digest = Sha224::default();
msg.iter().for_each(|slice| digest.update(slice));
self.verify_prehash(&digest.finalize_fixed(), sig.signature())
msg.iter()
.for_each(|slice| Update::update(&mut digest, slice));
self.verify_prehash(&digest.finalize(), sig.signature())
}
ECDSA_SHA256_OID => {
let mut digest = Sha256::default();
msg.iter().for_each(|slice| digest.update(slice));
self.verify_prehash(&digest.finalize_fixed(), sig.signature())
msg.iter()
.for_each(|slice| Update::update(&mut digest, slice));
self.verify_prehash(&digest.finalize(), sig.signature())
}
ECDSA_SHA384_OID => {
let mut digest = Sha384::default();
msg.iter().for_each(|slice| digest.update(slice));
self.verify_prehash(&digest.finalize_fixed(), sig.signature())
msg.iter()
.for_each(|slice| Update::update(&mut digest, slice));
self.verify_prehash(&digest.finalize(), sig.signature())
}
ECDSA_SHA512_OID => {
let mut digest = Sha512::default();
msg.iter().for_each(|slice| digest.update(slice));
self.verify_prehash(&digest.finalize_fixed(), sig.signature())
msg.iter()
.for_each(|slice| Update::update(&mut digest, slice));
self.verify_prehash(&digest.finalize(), sig.signature())
}
_ => Err(Error::new()),
}
Expand All @@ -252,7 +254,7 @@ where
impl<C, D> DigestVerifier<D, der::Signature<C>> for VerifyingKey<C>
where
C: EcdsaCurve + CurveArithmetic,
D: EagerHash + Update,
D: Digest + Update,
SignatureSize<C>: ArraySize,
der::MaxSize<C>: ArraySize,
<FieldBytesSize<C> as Add>::Output: Add<der::MaxOverhead> + ArraySize,
Expand Down
1 change: 1 addition & 0 deletions rfc6979/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ hmac = { version = "0.13", default-features = false }
[dev-dependencies]
hex-literal = "1"
sha2 = "0.11"
sha3 = "0.12"

[lints]
workspace = true
Loading
Loading