Skip to content

Commit d9ad085

Browse files
authored
Implement CollisionResistance for XOFs (#694)
This PR implements `CollisionResistance` for all XOFs. I started with those to add support for `ExpandMsgXof` in `elliptic-curve` and will do follow-up PRs for at least SHA2 and SHA3 fixed output hashes. Companion PR: RustCrypto/traits#1862. See RustCrypto/traits#1816 for previous discussions.
1 parent 7d44caf commit d9ad085

File tree

5 files changed

+50
-10
lines changed

5 files changed

+50
-10
lines changed

ascon-hash/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ use core::marker::PhantomData;
1212
use ascon::State;
1313
pub use digest::{self, Digest, ExtendableOutput, Reset, Update, XofReader};
1414
use digest::{
15-
HashMarker, Output, OutputSizeUser,
15+
CollisionResistance, HashMarker, Output, OutputSizeUser,
1616
block_api::{
1717
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, Eager, ExtendableOutputCore,
1818
FixedOutputCore, UpdateCore, XofReaderCore,
1919
},
20-
consts::{U8, U32, U40},
20+
consts::{U8, U16, U32, U40},
2121
crypto_common::hazmat::{DeserializeStateError, SerializableState, SerializedState},
2222
};
2323

@@ -294,3 +294,8 @@ digest::buffer_xof!(
294294
pub struct AsconXof128Reader(AsconXofReaderCore);
295295
impl: XofReaderTraits;
296296
);
297+
298+
impl CollisionResistance for AsconXof128 {
299+
// https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-232.ipd.pdf#table.caption.25
300+
type CollisionResistance = U16;
301+
}

k12/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ pub mod block_api;
1515

1616
use core::fmt;
1717
use digest::{
18-
ExtendableOutput, HashMarker, Reset, Update, XofReader,
18+
CollisionResistance, ExtendableOutput, HashMarker, Reset, Update, XofReader,
1919
block_api::{AlgorithmName, BlockSizeUser, ExtendableOutputCore, UpdateCore, XofReaderCore},
2020
block_buffer::{BlockBuffer, Eager, ReadBuffer},
21-
consts::{U128, U168},
21+
consts::{U16, U128, U168},
2222
};
2323

2424
/// `KangarooTwelve` hasher.
@@ -82,6 +82,11 @@ impl ExtendableOutput for KangarooTwelve<'_> {
8282
}
8383
}
8484

85+
impl CollisionResistance for KangarooTwelve<'_> {
86+
// https://www.ietf.org/archive/id/draft-irtf-cfrg-kangarootwelve-17.html#section-7-7
87+
type CollisionResistance = U16;
88+
}
89+
8590
#[cfg(feature = "zeroize")]
8691
impl digest::zeroize::ZeroizeOnDrop for KangarooTwelve<'_> {}
8792

sha3/src/cshake.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use crate::{
44
};
55
use core::fmt;
66
use digest::{
7-
CustomizedInit, HashMarker, Reset,
7+
CollisionResistance, CustomizedInit, HashMarker, Reset,
88
block_api::{
99
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, Eager, ExtendableOutputCore,
1010
UpdateCore,
1111
},
12-
consts::{U136, U168, U400},
12+
consts::{U16, U32, U136, U168, U400},
1313
crypto_common::hazmat::{DeserializeStateError, SerializableState, SerializedState},
1414
typenum::Unsigned,
1515
};
@@ -212,3 +212,13 @@ macro_rules! impl_cshake {
212212

213213
impl_cshake!(CShake128Core, CShake128, CShake128Reader, U168, "cSHAKE128");
214214
impl_cshake!(CShake256Core, CShake256, CShake256Reader, U136, "cSHAKE256");
215+
216+
impl CollisionResistance for CShake128 {
217+
// https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-185.pdf#[{"num":68,"gen":0},{"name":"XYZ"},108,440,null]
218+
type CollisionResistance = U16;
219+
}
220+
221+
impl CollisionResistance for CShake256 {
222+
// https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-185.pdf#[{"num":68,"gen":0},{"name":"XYZ"},108,440,null]
223+
type CollisionResistance = U32;
224+
}

sha3/src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#![forbid(unsafe_code)]
99
#![warn(missing_docs, missing_debug_implementations)]
1010

11-
pub use digest::{self, CustomizedInit, Digest};
11+
pub use digest::{self, CollisionResistance, CustomizedInit, Digest};
1212

1313
/// Block-level types
1414
pub mod block_api;
@@ -19,7 +19,7 @@ pub use cshake::{CShake128, CShake128Reader, CShake256, CShake256Reader};
1919
pub use turbo_shake::{TurboShake128, TurboShake128Reader, TurboShake256, TurboShake256Reader};
2020

2121
use block_api::{Sha3HasherCore, Sha3ReaderCore};
22-
use digest::consts::{U0, U28, U32, U48, U64, U72, U104, U136, U144, U168, U200};
22+
use digest::consts::{U0, U16, U28, U32, U48, U64, U72, U104, U136, U144, U168, U200};
2323

2424
// Paddings
2525
const KECCAK_PAD: u8 = 0x01;
@@ -98,3 +98,13 @@ digest::buffer_fixed!(
9898
pub struct Keccak512(Sha3HasherCore<U72, U64, KECCAK_PAD>);
9999
impl: FixedHashTraits;
100100
);
101+
102+
impl CollisionResistance for Shake128 {
103+
// https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf#page=31
104+
type CollisionResistance = U16;
105+
}
106+
107+
impl CollisionResistance for Shake256 {
108+
// https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf#page=31
109+
type CollisionResistance = U32;
110+
}

sha3/src/turbo_shake.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::{Sha3HasherCore, Sha3ReaderCore};
22
use core::fmt;
33
use digest::{
4-
ExtendableOutput, ExtendableOutputReset, HashMarker, Update, XofReader,
4+
CollisionResistance, ExtendableOutput, ExtendableOutputReset, HashMarker, Update, XofReader,
55
block_api::{
66
AlgorithmName, BlockSizeUser, ExtendableOutputCore, Reset, UpdateCore, XofReaderCore,
77
},
88
block_buffer::{EagerBuffer, ReadBuffer},
9-
consts::{U0, U136, U168},
9+
consts::{U0, U16, U32, U136, U168},
1010
};
1111

1212
const TURBO_SHAKE_ROUND_COUNT: usize = 12;
@@ -121,3 +121,13 @@ macro_rules! impl_turbo_shake {
121121

122122
impl_turbo_shake!(TurboShake128, TurboShake128Reader, U168, "TurboSHAKE128");
123123
impl_turbo_shake!(TurboShake256, TurboShake256Reader, U136, "TurboSHAKE256");
124+
125+
impl<const DS: u8> CollisionResistance for TurboShake128<DS> {
126+
// https://www.ietf.org/archive/id/draft-irtf-cfrg-kangarootwelve-17.html#section-7-7
127+
type CollisionResistance = U16;
128+
}
129+
130+
impl<const DS: u8> CollisionResistance for TurboShake256<DS> {
131+
// https://www.ietf.org/archive/id/draft-irtf-cfrg-kangarootwelve-17.html#section-7-8
132+
type CollisionResistance = U32;
133+
}

0 commit comments

Comments
 (0)