From 0e16ed1a4319203d5907661c6bc7bf334b77faee Mon Sep 17 00:00:00 2001 From: Sylvain Pelissier Date: Mon, 17 Jul 2023 13:01:39 +0200 Subject: [PATCH] Add hash personalization traits --- digest/src/core_api/wrapper.rs | 18 +++++++++++++++++- digest/src/lib.rs | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/digest/src/core_api/wrapper.rs b/digest/src/core_api/wrapper.rs index af77b11c8..342cb90b2 100644 --- a/digest/src/core_api/wrapper.rs +++ b/digest/src/core_api/wrapper.rs @@ -3,7 +3,8 @@ use super::{ Reset, UpdateCore, XofReaderCoreWrapper, }; use crate::{ - ExtendableOutput, ExtendableOutputReset, FixedOutput, FixedOutputReset, HashMarker, Update, + ExtendableOutput, ExtendableOutputReset, FixedOutput, FixedOutputReset, HashMarker, + PersonalizationInit, Update, }; use block_buffer::BlockBuffer; use core::fmt; @@ -184,6 +185,21 @@ where } } +impl PersonalizationInit for CoreWrapper +where + T: BufferKindUser + PersonalizationInit, +{ + type PersonalizationExtArg = T::PersonalizationExtArg; + #[inline] + fn new(personalization: &[u8]) -> Self { + Self::from_core(T::new(personalization)) + } + + fn new_ext(personalization_ext: &Self::PersonalizationExtArg) -> Self { + Self::from_core(T::new_ext(personalization_ext)) + } +} + #[cfg(feature = "oid")] #[cfg_attr(docsrs, doc(cfg(feature = "oid")))] impl AssociatedOid for CoreWrapper diff --git a/digest/src/lib.rs b/digest/src/lib.rs index 04ea98931..3bfcff684 100644 --- a/digest/src/lib.rs +++ b/digest/src/lib.rs @@ -273,6 +273,21 @@ pub trait VariableOutputReset: VariableOutput + Reset { } } +/// Trait for hash functions with personalization string for domain separation. +pub trait PersonalizationInit: Sized { + // It would be nice to define a default value equal to `[u8]`, but unfortunately + // associated type defaults are currently unstable + + /// Extended personalization + type PersonalizationExtArg; + + /// Create new hasher instance with the given personalization string. + fn new(personalization: &[u8]) -> Self; + + /// Create new hasher instance with the given extended personalization. + fn new_ext(personalization_ext: &Self::PersonalizationExtArg) -> Self; +} + /// The error type used in variable hash traits. #[derive(Clone, Copy, Debug, Default)] pub struct InvalidOutputSize;