diff --git a/digest/CHANGELOG.md b/digest/CHANGELOG.md index f715b731..afff142f 100644 --- a/digest/CHANGELOG.md +++ b/digest/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## UNRELEASED +### Added +- `CustomizedInit` trait ([#1334]). + ### Changed - `crypto-common` dependency bumped to v0.2 ([#1173]) - Edition changed to 2021 and MSRV bumped to 1.57 ([#1173]) @@ -14,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `Mac::new`, `Mac::new_from_slice`, and `Mac::generate_key` methods ([#1173]) [#1173]: https://github.com/RustCrypto/traits/pull/1173 +[#1334]: https://github.com/RustCrypto/traits/pull/1334 ## 0.10.7 (2023-05-19) ### Changed diff --git a/digest/src/core_api/wrapper.rs b/digest/src/core_api/wrapper.rs index 32b8a5a1..9b72e223 100644 --- a/digest/src/core_api/wrapper.rs +++ b/digest/src/core_api/wrapper.rs @@ -3,7 +3,8 @@ use super::{ UpdateCore, XofReaderCoreWrapper, }; use crate::{ - ExtendableOutput, ExtendableOutputReset, FixedOutput, FixedOutputReset, HashMarker, Update, + CustomizedInit, ExtendableOutput, ExtendableOutputReset, FixedOutput, FixedOutputReset, + HashMarker, Update, }; use block_buffer::BlockBuffer; use core::{ @@ -160,8 +161,28 @@ impl Drop for CoreWrapper { #[cfg(feature = "zeroize")] impl zeroize::ZeroizeOnDrop for CoreWrapper {} +impl CustomizedInit for CoreWrapper +where + T: BufferKindUser + CustomizedInit, +{ + type CustomizedExtArg = T::CustomizedExtArg; + + #[inline] + fn new_customized(customization: &[u8]) -> Self { + Self::from_core(T::new_customized(customization)) + } + + #[inline] + fn new_ext_customized(customization_ext: &Self::CustomizedExtArg) -> Self { + Self::from_core(T::new_ext_customized(customization_ext)) + } +} + #[cfg(feature = "oid")] -impl AssociatedOid for CoreWrapper { +impl AssociatedOid for CoreWrapper +where + T: BufferKindUser + AssociatedOid, +{ const OID: ObjectIdentifier = T::OID; } diff --git a/digest/src/lib.rs b/digest/src/lib.rs index 29fd0863..e129f4e5 100644 --- a/digest/src/lib.rs +++ b/digest/src/lib.rs @@ -268,6 +268,21 @@ pub trait VariableOutputReset: VariableOutput + Reset { } } +/// Trait for hash functions with customization string for domain separation. +pub trait CustomizedInit: Sized { + // TODO: It would be nice to define a default value equal to `[u8]`, but unfortunately + // associated type defaults are currently unstable + + /// Extended customization + type CustomizedExtArg; + + /// Create new hasher instance with the given customization string. + fn new_customized(customization: &[u8]) -> Self; + + /// Create new hasher instance with the given extended customization. + fn new_ext_customized(customization_ext: &Self::CustomizedExtArg) -> Self; +} + /// The error type used in variable hash traits. #[derive(Clone, Copy, Debug, Default)] pub struct InvalidOutputSize;