Skip to content

Commit 7b82f39

Browse files
Add hash customization trait for variable output hash (#1787)
New trait for customized hash function with variable output `CtVariableCoreWrapper` like Blake2 to implement the `CustomizedInit` trait.
1 parent 266b044 commit 7b82f39

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

digest/src/core_api/ct_variable.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use super::{
22
AlgorithmName, Buffer, BufferKindUser, FixedOutputCore, Reset, TruncSide, UpdateCore,
33
VariableOutputCore,
44
};
5-
use crate::HashMarker;
65
#[cfg(feature = "mac")]
76
use crate::MacMarker;
7+
use crate::{CustomizedInit, HashMarker, VarOutputCustomized};
88
#[cfg(feature = "oid")]
99
use const_oid::{AssociatedOid, ObjectIdentifier};
1010
use core::{
@@ -131,6 +131,21 @@ where
131131
}
132132
}
133133

134+
impl<T, OutSize, O> CustomizedInit for CtVariableCoreWrapper<T, OutSize, O>
135+
where
136+
T: VariableOutputCore + VarOutputCustomized,
137+
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
138+
LeEq<OutSize, T::OutputSize>: NonZero,
139+
{
140+
#[inline]
141+
fn new_customized(customization: &[u8]) -> Self {
142+
Self {
143+
inner: T::new_customized(customization, OutSize::USIZE),
144+
_out: PhantomData,
145+
}
146+
}
147+
}
148+
134149
impl<T, OutSize, O> Reset for CtVariableCoreWrapper<T, OutSize, O>
135150
where
136151
T: VariableOutputCore,

digest/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,12 @@ pub trait CustomizedInit: Sized {
274274
fn new_customized(customization: &[u8]) -> Self;
275275
}
276276

277+
/// Trait adding customization string to hash functions with variable output.
278+
pub trait VarOutputCustomized: Sized {
279+
/// Create new hasher instance with the given customization string and output size.
280+
fn new_customized(customization: &[u8], output_size: usize) -> Self;
281+
}
282+
277283
/// The error type used in variable hash traits.
278284
#[derive(Clone, Copy, Debug, Default)]
279285
pub struct InvalidOutputSize;

0 commit comments

Comments
 (0)