Skip to content

Commit 2007344

Browse files
committed
update xofs
1 parent fc69e6e commit 2007344

4 files changed

Lines changed: 37 additions & 17 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ascon-hash/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ digest::newtype_fixed_hash!(
289289
digest::newtype_xof_hash!(
290290
/// Ascon-XOF128 hasher.
291291
pub struct AsconXof128(AsconXofCore);
292+
impl: XofHasherTraits;
292293
/// Ascon-XOF128 reader.
293294
pub struct AsconXof128Reader(AsconXofReaderCore);
295+
impl: XofReaderTraits;
294296
);

sha3/src/cshake.rs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
use core::fmt;
66
use digest::{
77
CustomizedInit, HashMarker, Reset,
8-
consts::{U136, U168},
8+
consts::{U136, U168, U400},
99
core_api::{
1010
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, Eager, ExtendableOutputCore,
1111
UpdateCore,
@@ -151,36 +151,50 @@ macro_rules! impl_cshake {
151151
impl digest::zeroize::ZeroizeOnDrop for $name {}
152152

153153
impl SerializableState for $name {
154-
// TODO: implement properly with U400
155-
type SerializedStateSize = digest::consts::U0;
154+
type SerializedStateSize = U400;
156155

157156
fn serialize(&self) -> SerializedState<Self> {
158-
todo!()
157+
let mut serialized_state = SerializedState::<Self>::default();
158+
let mut chunks = serialized_state.chunks_exact_mut(8);
159+
160+
for (val, chunk) in self.state.iter().zip(&mut chunks) {
161+
chunk.copy_from_slice(&val.to_le_bytes());
162+
}
163+
for (val, chunk) in self.initial_state.iter().zip(&mut chunks) {
164+
chunk.copy_from_slice(&val.to_le_bytes());
165+
}
166+
167+
serialized_state
159168
}
160169

161170
fn deserialize(
162-
_serialized_state: &SerializedState<Self>,
171+
serialized_state: &SerializedState<Self>,
163172
) -> Result<Self, DeserializeStateError> {
164-
todo!()
173+
let (state_src, initial_state_src) = serialized_state.split_at(200);
174+
let state = core::array::from_fn(|i| {
175+
let chunk = state_src[8 * i..][..8].try_into().unwrap();
176+
u64::from_le_bytes(chunk)
177+
});
178+
let initial_state = core::array::from_fn(|i| {
179+
let chunk = initial_state_src[8 * i..][..8].try_into().unwrap();
180+
u64::from_le_bytes(chunk)
181+
});
182+
Ok(Self{ state, initial_state })
165183
}
166184
}
167185

168186
digest::newtype_xof_hash!(
169187
#[doc = $alg_name]
170188
#[doc = " hasher."]
171189
pub struct $full_name($name);
190+
// TODO: Use `XofHasherTraits CustomizedInit` after serialization for buffers is fixed
191+
impl: Debug AlgorithmName Clone Default BlockSizeUser CoreProxy HashMarker Update Reset ExtendableOutputReset CustomizedInit;
172192
#[doc = $alg_name]
173193
#[doc = " XOF reader."]
174194
pub struct $reader_name(Sha3ReaderCore<$rate>);
195+
impl: XofReaderTraits;
175196
);
176197

177-
impl CustomizedInit for $full_name {
178-
#[inline]
179-
fn new_customized(customization: &[u8]) -> Self {
180-
Self::new_with_function_name(&[], customization)
181-
}
182-
}
183-
184198
impl $full_name {
185199
/// Creates a new cSHAKE instance with the given function name and customization.
186200
///

sha3/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,20 @@ digest::newtype_fixed_hash!(
6060
digest::newtype_xof_hash!(
6161
/// SHAKE128 hasher.
6262
pub struct Shake128(Sha3HasherCore<U168, U0, SHAKE_PAD>);
63+
oid: "2.16.840.1.101.3.4.2.11";
64+
impl: XofHasherTraits;
6365
/// SHAKE128 XOF reader.
6466
pub struct Shake128Reader(Sha3ReaderCore<U168>);
65-
oid: "2.16.840.1.101.3.4.2.11";
67+
impl: XofReaderTraits;
6668
);
6769
digest::newtype_xof_hash!(
6870
/// SHAKE256 hasher.
6971
pub struct Shake256(Sha3HasherCore<U136, U0, SHAKE_PAD>);
72+
oid: "2.16.840.1.101.3.4.2.12";
73+
impl: XofHasherTraits;
7074
/// SHAKE256 XOF reader.
7175
pub struct Shake256Reader(Sha3ReaderCore<U136>);
72-
oid: "2.16.840.1.101.3.4.2.12";
76+
impl: XofReaderTraits;
7377
);
7478

7579
digest::newtype_fixed_hash!(

0 commit comments

Comments
 (0)