From d68d8167e811d980def3350be9605e90606ce445 Mon Sep 17 00:00:00 2001 From: Sylvain Pelissier Date: Fri, 15 Mar 2024 15:57:21 +0100 Subject: [PATCH] Add Customized trait for CSHAKE. Use new customized in tests. --- sha3/src/lib.rs | 2 +- sha3/src/macros.rs | 12 +++++++----- sha3/tests/cshake.rs | 26 ++++++-------------------- 3 files changed, 14 insertions(+), 26 deletions(-) diff --git a/sha3/src/lib.rs b/sha3/src/lib.rs index 68c519a09..3448770cb 100644 --- a/sha3/src/lib.rs +++ b/sha3/src/lib.rs @@ -8,7 +8,7 @@ #![forbid(unsafe_code)] #![warn(missing_docs, rust_2018_idioms)] -pub use digest::{self, Digest}; +pub use digest::{self, CustomizedInit, Digest}; use core::fmt; use digest::{ diff --git a/sha3/src/macros.rs b/sha3/src/macros.rs index 639703119..dee339435 100644 --- a/sha3/src/macros.rs +++ b/sha3/src/macros.rs @@ -440,11 +440,6 @@ macro_rules! impl_cshake { pub type $full_name = CoreWrapper<$name>; impl $name { - /// Creates a new CSHAKE instance with the given customization. - pub fn new(customization: &[u8]) -> Self { - Self::new_with_function_name(&[], customization) - } - /// Creates a new CSHAKE instance with the given function name and customization. /// Note that the function name is intended for use by NIST and should only be set to /// values defined by NIST. You probably don't need to use this function. @@ -613,5 +608,12 @@ macro_rules! impl_cshake { #[cfg(feature = "zeroize")] impl ZeroizeOnDrop for $reader {} + + impl CustomizedInit for $name { + #[inline] + fn new_customized(customization: &[u8]) -> Self { + Self::new_with_function_name(&[], customization) + } + } }; } diff --git a/sha3/tests/cshake.rs b/sha3/tests/cshake.rs index 0ba62e2c9..f85f31385 100644 --- a/sha3/tests/cshake.rs +++ b/sha3/tests/cshake.rs @@ -88,7 +88,7 @@ where } macro_rules! new_cshake_test { - ($name:ident, $test_name:expr, $hasher:ty, $hasher_core:ty, $test_func:ident $(,)?) => { + ($name:ident, $test_name:expr, $hasher:ty, $test_func:ident $(,)?) => { #[test] fn $name() { use digest::dev::blobby::Blob3Iterator; @@ -96,9 +96,9 @@ macro_rules! new_cshake_test { 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)) - }) { + if let Some(desc) = + $test_func(input, output, || <$hasher>::new_customized(customization)) + { panic!( "\n\ Failed test №{}: {}\n\ @@ -117,7 +117,6 @@ new_cshake_test!( cshake128_reset, "cshake128", sha3::CShake128, - sha3::CShake128Core, cshake_reset_test ); #[cfg(feature = "reset")] @@ -125,21 +124,8 @@ 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 -); +new_cshake_test!(cshake128, "cshake128", sha3::CShake128, cshake_test); +new_cshake_test!(cshake256, "cshake256", sha3::CShake256, cshake_test);