From cf0c4eda5340f12a4bde3f203937b587e4665035 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Mon, 15 Sep 2025 16:41:37 -0600 Subject: [PATCH] digest: move `EagerHash` from `hmac` Originally added in https://github.com/RustCrypto/MACs/pull/151 The situation is `ecdsa` has a trait for the "default" hash function to use for a particular curve, which we want to work with RFC6979 and ergo HMAC, so we would like to use an `EagerHash` bound. But `rfc6979` is an optional dependency, so we can't depend on its transitive accessibility to `rfc6979::hmac`. This PR added `hmac` as a dependency gated on the `digest` feature of `ecdsa`: https://github.com/RustCrypto/signatures/pull/1076 But as I was looking at `EagerHash` I noticed there's nothing HMAC-specific about it at all and it can easily be moved to `digest`, so we're able to use it in bounds without any other dependencies besides `digest`, which would be nice. --- digest/src/block_api.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/digest/src/block_api.rs b/digest/src/block_api.rs index c0ec6cb28..56dda9cb5 100644 --- a/digest/src/block_api.rs +++ b/digest/src/block_api.rs @@ -3,7 +3,7 @@ //! Usage of traits in this module in user code is discouraged. Instead use //! core algorithm wrapped by the wrapper types, which implement the //! higher-level traits. -use crate::InvalidOutputSize; +use crate::{Digest, HashMarker, InvalidOutputSize}; pub use block_buffer::{Eager, Lazy}; pub use crypto_common::{AlgorithmName, Block, BlockSizeUser, OutputSizeUser, Reset}; @@ -30,6 +30,32 @@ pub trait BufferKindUser: BlockSizeUser { type BufferKind: BufferKind; } +/// Trait implemented by eager hashes which expose their block-level core. +pub trait EagerHash: BlockSizeUser + Digest { + /// Block-level core type of the hash. + type Core: HashMarker + + UpdateCore + + FixedOutputCore + + BlockSizeUser::BlockSize> + + BufferKindUser + + Default + + Clone; +} + +impl EagerHash for T +where + T: CoreProxy + BlockSizeUser + Digest, + ::Core: HashMarker + + UpdateCore + + FixedOutputCore + + BlockSizeUser::BlockSize> + + BufferKindUser + + Default + + Clone, +{ + type Core = T::Core; +} + /// Core trait for hash functions with fixed output size. pub trait FixedOutputCore: UpdateCore + BufferKindUser + OutputSizeUser { /// Finalize state using remaining data stored in the provided block buffer,