-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cSHAKE128 and cSHAKE256 implementations (#355)
- Loading branch information
Showing
6 changed files
with
348 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,4 @@ hex-literal = "0.2.2" | |
[features] | ||
default = ["std"] | ||
std = ["digest/std"] | ||
reset = [] # Enable reset functionality |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
use core::fmt::Debug; | ||
use digest::ExtendableOutput; | ||
#[cfg(feature = "reset")] | ||
use digest::ExtendableOutputReset; | ||
|
||
#[cfg(feature = "reset")] | ||
pub(crate) fn cshake_reset_test<D, F>(input: &[u8], output: &[u8], new: F) -> Option<&'static str> | ||
where | ||
D: ExtendableOutputReset + Debug + Clone, | ||
F: Fn() -> D, | ||
{ | ||
let mut hasher = new(); | ||
let mut buf = [0u8; 1024]; | ||
let buf = &mut buf[..output.len()]; | ||
// Test that it works when accepting the message all at once | ||
hasher.update(input); | ||
let mut hasher2 = hasher.clone(); | ||
hasher.finalize_xof_into(buf); | ||
if buf != output { | ||
return Some("whole message"); | ||
} | ||
buf.iter_mut().for_each(|b| *b = 0); | ||
|
||
// Test if reset works correctly | ||
hasher2.reset(); | ||
hasher2.update(input); | ||
hasher2.finalize_xof_reset_into(buf); | ||
if buf != output { | ||
return Some("whole message after reset"); | ||
} | ||
buf.iter_mut().for_each(|b| *b = 0); | ||
|
||
// Test that it works when accepting the message in chunks | ||
for n in 1..core::cmp::min(17, input.len()) { | ||
let mut hasher = new(); | ||
for chunk in input.chunks(n) { | ||
hasher.update(chunk); | ||
hasher2.update(chunk); | ||
} | ||
hasher.finalize_xof_into(buf); | ||
if buf != output { | ||
return Some("message in chunks"); | ||
} | ||
buf.iter_mut().for_each(|b| *b = 0); | ||
|
||
hasher2.finalize_xof_reset_into(buf); | ||
if buf != output { | ||
return Some("message in chunks"); | ||
} | ||
buf.iter_mut().for_each(|b| *b = 0); | ||
} | ||
|
||
None | ||
} | ||
|
||
pub(crate) fn cshake_test<D, F>(input: &[u8], output: &[u8], new: F) -> Option<&'static str> | ||
where | ||
D: ExtendableOutput + Debug + Clone, | ||
F: Fn() -> D, | ||
{ | ||
let mut hasher = new(); | ||
let mut buf = [0u8; 1024]; | ||
let buf = &mut buf[..output.len()]; | ||
// Test that it works when accepting the message all at once | ||
hasher.update(input); | ||
let mut hasher2 = hasher.clone(); | ||
hasher.finalize_xof_into(buf); | ||
if buf != output { | ||
return Some("whole message"); | ||
} | ||
buf.iter_mut().for_each(|b| *b = 0); | ||
|
||
// Test that it works when accepting the message in chunks | ||
for n in 1..core::cmp::min(17, input.len()) { | ||
let mut hasher = new(); | ||
for chunk in input.chunks(n) { | ||
hasher.update(chunk); | ||
hasher2.update(chunk); | ||
} | ||
hasher.finalize_xof_into(buf); | ||
if buf != output { | ||
return Some("message in chunks"); | ||
} | ||
buf.iter_mut().for_each(|b| *b = 0); | ||
} | ||
|
||
None | ||
} | ||
|
||
macro_rules! new_cshake_test { | ||
($name:ident, $test_name:expr, $hasher:ty, $hasher_core:ty, $test_func:ident $(,)?) => { | ||
#[test] | ||
fn $name() { | ||
use digest::dev::blobby::Blob3Iterator; | ||
let data = include_bytes!(concat!("data/", $test_name, ".blb")); | ||
|
||
for (i, row) in Blob3Iterator::new(data).unwrap().enumerate() { | ||
let [customization, input, output] = row.unwrap(); | ||
if let Some(desc) = $test_func(input, output, || { | ||
<$hasher>::from_core(<$hasher_core>::new(customization)) | ||
}) { | ||
panic!( | ||
"\n\ | ||
Failed test №{}: {}\n\ | ||
input:\t{:?}\n\ | ||
output:\t{:?}\n", | ||
i, desc, input, output, | ||
); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
#[cfg(feature = "reset")] | ||
new_cshake_test!( | ||
cshake128_reset, | ||
"cshake128", | ||
sha3::CShake128, | ||
sha3::CShake128Core, | ||
cshake_reset_test | ||
); | ||
#[cfg(feature = "reset")] | ||
new_cshake_test!( | ||
cshake256_reset, | ||
"cshake256", | ||
sha3::CShake256, | ||
sha3::CShake256Core, | ||
cshake_reset_test | ||
); | ||
|
||
new_cshake_test!( | ||
cshake128, | ||
"cshake128", | ||
sha3::CShake128, | ||
sha3::CShake128Core, | ||
cshake_test | ||
); | ||
new_cshake_test!( | ||
cshake256, | ||
"cshake256", | ||
sha3::CShake256, | ||
sha3::CShake256Core, | ||
cshake_test | ||
); |
Binary file not shown.
Binary file not shown.