diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /third_party/rust/sha3/tests | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/sha3/tests')
19 files changed, 296 insertions, 0 deletions
diff --git a/third_party/rust/sha3/tests/aliases.rs b/third_party/rust/sha3/tests/aliases.rs new file mode 100644 index 0000000000..9670bb54a8 --- /dev/null +++ b/third_party/rust/sha3/tests/aliases.rs @@ -0,0 +1,19 @@ +//! Checks that we defined reader type aliases correctly +#![allow(dead_code)] +use sha3::digest::ExtendableOutput; + +fn shake128(v: sha3::Shake128) -> sha3::Shake128Reader { + v.finalize_xof() +} + +fn shake256(v: sha3::Shake256) -> sha3::Shake256Reader { + v.finalize_xof() +} + +fn cshake128(v: sha3::CShake128) -> sha3::CShake128Reader { + v.finalize_xof() +} + +fn cshake256(v: sha3::CShake256) -> sha3::CShake256Reader { + v.finalize_xof() +} diff --git a/third_party/rust/sha3/tests/cshake.rs b/third_party/rust/sha3/tests/cshake.rs new file mode 100644 index 0000000000..0ba62e2c96 --- /dev/null +++ b/third_party/rust/sha3/tests/cshake.rs @@ -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 +); diff --git a/third_party/rust/sha3/tests/data/cshake128.blb b/third_party/rust/sha3/tests/data/cshake128.blb Binary files differnew file mode 100644 index 0000000000..e8ae154f75 --- /dev/null +++ b/third_party/rust/sha3/tests/data/cshake128.blb diff --git a/third_party/rust/sha3/tests/data/cshake256.blb b/third_party/rust/sha3/tests/data/cshake256.blb Binary files differnew file mode 100644 index 0000000000..9e15af1cc2 --- /dev/null +++ b/third_party/rust/sha3/tests/data/cshake256.blb diff --git a/third_party/rust/sha3/tests/data/keccak_224.blb b/third_party/rust/sha3/tests/data/keccak_224.blb Binary files differnew file mode 100644 index 0000000000..393d0acebd --- /dev/null +++ b/third_party/rust/sha3/tests/data/keccak_224.blb diff --git a/third_party/rust/sha3/tests/data/keccak_256.blb b/third_party/rust/sha3/tests/data/keccak_256.blb Binary files differnew file mode 100644 index 0000000000..d964e20947 --- /dev/null +++ b/third_party/rust/sha3/tests/data/keccak_256.blb diff --git a/third_party/rust/sha3/tests/data/keccak_256_full.blb b/third_party/rust/sha3/tests/data/keccak_256_full.blb Binary files differnew file mode 100644 index 0000000000..71af3f2347 --- /dev/null +++ b/third_party/rust/sha3/tests/data/keccak_256_full.blb diff --git a/third_party/rust/sha3/tests/data/keccak_384.blb b/third_party/rust/sha3/tests/data/keccak_384.blb Binary files differnew file mode 100644 index 0000000000..2509429442 --- /dev/null +++ b/third_party/rust/sha3/tests/data/keccak_384.blb diff --git a/third_party/rust/sha3/tests/data/keccak_512.blb b/third_party/rust/sha3/tests/data/keccak_512.blb Binary files differnew file mode 100644 index 0000000000..a9e47e0770 --- /dev/null +++ b/third_party/rust/sha3/tests/data/keccak_512.blb diff --git a/third_party/rust/sha3/tests/data/sha3_224.blb b/third_party/rust/sha3/tests/data/sha3_224.blb Binary files differnew file mode 100644 index 0000000000..510e160c4c --- /dev/null +++ b/third_party/rust/sha3/tests/data/sha3_224.blb diff --git a/third_party/rust/sha3/tests/data/sha3_256.blb b/third_party/rust/sha3/tests/data/sha3_256.blb Binary files differnew file mode 100644 index 0000000000..00fe6fcd91 --- /dev/null +++ b/third_party/rust/sha3/tests/data/sha3_256.blb diff --git a/third_party/rust/sha3/tests/data/sha3_384.blb b/third_party/rust/sha3/tests/data/sha3_384.blb Binary files differnew file mode 100644 index 0000000000..802759adf6 --- /dev/null +++ b/third_party/rust/sha3/tests/data/sha3_384.blb diff --git a/third_party/rust/sha3/tests/data/sha3_512.blb b/third_party/rust/sha3/tests/data/sha3_512.blb Binary files differnew file mode 100644 index 0000000000..b02800c0fa --- /dev/null +++ b/third_party/rust/sha3/tests/data/sha3_512.blb diff --git a/third_party/rust/sha3/tests/data/shake128.blb b/third_party/rust/sha3/tests/data/shake128.blb Binary files differnew file mode 100644 index 0000000000..58fb208148 --- /dev/null +++ b/third_party/rust/sha3/tests/data/shake128.blb diff --git a/third_party/rust/sha3/tests/data/shake256.blb b/third_party/rust/sha3/tests/data/shake256.blb Binary files differnew file mode 100644 index 0000000000..c5340e26a8 --- /dev/null +++ b/third_party/rust/sha3/tests/data/shake256.blb diff --git a/third_party/rust/sha3/tests/data/turboshake128.blb b/third_party/rust/sha3/tests/data/turboshake128.blb Binary files differnew file mode 100644 index 0000000000..5f1d64d11e --- /dev/null +++ b/third_party/rust/sha3/tests/data/turboshake128.blb diff --git a/third_party/rust/sha3/tests/data/turboshake256.blb b/third_party/rust/sha3/tests/data/turboshake256.blb Binary files differnew file mode 100644 index 0000000000..6c6635314d --- /dev/null +++ b/third_party/rust/sha3/tests/data/turboshake256.blb diff --git a/third_party/rust/sha3/tests/mod.rs b/third_party/rust/sha3/tests/mod.rs new file mode 100644 index 0000000000..120ff41e23 --- /dev/null +++ b/third_party/rust/sha3/tests/mod.rs @@ -0,0 +1,24 @@ +#![no_std] + +use digest::dev::{fixed_reset_test, xof_reset_test}; +use digest::new_test; + +new_test!(keccak_224, "keccak_224", sha3::Keccak224, fixed_reset_test); +new_test!(keccak_256, "keccak_256", sha3::Keccak256, fixed_reset_test); +new_test!(keccak_384, "keccak_384", sha3::Keccak384, fixed_reset_test); +new_test!(keccak_512, "keccak_512", sha3::Keccak512, fixed_reset_test); +// tests are from https://github.com/kazcw/yellowsun/blob/test-keccak/src/lib.rs#L171 +new_test!( + keccak_256_full, + "keccak_256_full", + sha3::Keccak256Full, + fixed_reset_test +); + +new_test!(sha3_224, "sha3_224", sha3::Sha3_224, fixed_reset_test); +new_test!(sha3_256, "sha3_256", sha3::Sha3_256, fixed_reset_test); +new_test!(sha3_384, "sha3_384", sha3::Sha3_384, fixed_reset_test); +new_test!(sha3_512, "sha3_512", sha3::Sha3_512, fixed_reset_test); + +new_test!(shake128, "shake128", sha3::Shake128, xof_reset_test); +new_test!(shake256, "shake256", sha3::Shake256, xof_reset_test); diff --git a/third_party/rust/sha3/tests/turboshake.rs b/third_party/rust/sha3/tests/turboshake.rs new file mode 100644 index 0000000000..3d08df1961 --- /dev/null +++ b/third_party/rust/sha3/tests/turboshake.rs @@ -0,0 +1,108 @@ +use core::{convert::TryInto, fmt::Debug}; +use digest::ExtendableOutput; + +pub(crate) fn turbo_shake_test<D, F>( + input: &[u8], + output: &[u8], + truncate_output: usize, + new: F, +) -> Option<&'static str> +where + D: ExtendableOutput + Debug + Clone, + F: Fn() -> D, +{ + let mut hasher = new(); + let mut buf = [0u8; 16 * 1024]; + let buf = &mut buf[..truncate_output + 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[truncate_output..] != 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[truncate_output..] != output { + return Some("message in chunks"); + } + buf.iter_mut().for_each(|b| *b = 0); + } + + None +} + +macro_rules! new_turbo_shake_test { + ($name:ident, $test_name:expr, $hasher:ty, $hasher_core:ty, $test_func:ident $(,)?) => { + #[test] + fn $name() { + use digest::dev::blobby::Blob5Iterator; + let data = include_bytes!(concat!("data/", $test_name, ".blb")); + + for (i, row) in Blob5Iterator::new(data).unwrap().enumerate() { + let [domain_separation, input, input_pattern_length, output, truncate_output] = + row.unwrap(); + + let input = if (input_pattern_length.len() == 0) { + input.to_vec() + } else if (input.len() == 0) { + let pattern_length = + u64::from_be_bytes(input_pattern_length.try_into().unwrap()); + let mut input = Vec::<u8>::new(); + for value in 0..pattern_length { + input.push((value % 0xFB).try_into().unwrap()); + } + input + } else { + panic!( + "\ + failed to read tests data\n\ + input:\t{:02X?}\n\ + input_pattern_length:\t{:02X?}\n", + input, input_pattern_length, + ); + }; + + if let Some(desc) = $test_func( + &input, + output, + u64::from_be_bytes(truncate_output.try_into().unwrap()) + .try_into() + .unwrap(), + || <$hasher>::from_core(<$hasher_core>::new(domain_separation[0])), + ) { + panic!( + "\n\ + Failed test №{}: {}\n\ + input:\t{:02X?}\n\ + output:\t{:02X?}\n", + i, desc, &input, output, + ); + } + } + } + }; +} + +new_turbo_shake_test!( + turboshake128, + "turboshake128", + sha3::TurboShake128, + sha3::TurboShake128Core, + turbo_shake_test, +); +new_turbo_shake_test!( + turboshake256, + "turboshake256", + sha3::TurboShake256, + sha3::TurboShake256Core, + turbo_shake_test, +); |