Debian Codegraph @

third_party/rust/sha3/tests/cshake.rs

raw
1 use core::fmt::Debug;
2 use digest::ExtendableOutput;
3 #[cfg(feature = "reset")]
4 use digest::ExtendableOutputReset;
5
6 #[cfg(feature = "reset")]
7 pub(crate) fn cshake_reset_test<D, F>(input: &[u8], output: &[u8], new: F) -> Option<&'static str>
8 where
9 D: ExtendableOutputReset + Debug + Clone,
10 F: Fn() -> D,
11 {
12 let mut hasher = new();
13 let mut buf = [0u8; 1024];
14 let buf = &mut buf[..output.len()];
15 // Test that it works when accepting the message all at once
16 hasher.update(input);
17 let mut hasher2 = hasher.clone();
18 hasher.finalize_xof_into(buf);
19 if buf != output {
20 return Some("whole message");
21 }
22 buf.iter_mut().for_each(|b| *b = 0);
23
24 // Test if reset works correctly
25 hasher2.reset();
26 hasher2.update(input);
27 hasher2.finalize_xof_reset_into(buf);
28 if buf != output {
29 return Some("whole message after reset");
30 }
31 buf.iter_mut().for_each(|b| *b = 0);
32
33 // Test that it works when accepting the message in chunks
34 for n in 1..core::cmp::min(17, input.len()) {
35 let mut hasher = new();
36 for chunk in input.chunks(n) {
37 hasher.update(chunk);
38 hasher2.update(chunk);
39 }
40 hasher.finalize_xof_into(buf);
41 if buf != output {
42 return Some("message in chunks");
43 }
44 buf.iter_mut().for_each(|b| *b = 0);
45
46 hasher2.finalize_xof_reset_into(buf);
47 if buf != output {
48 return Some("message in chunks");
49 }
50 buf.iter_mut().for_each(|b| *b = 0);
51 }
52
53 None
54 }
55
56 pub(crate) fn cshake_test<D, F>(input: &[u8], output: &[u8], new: F) -> Option<&'static str>
57 where
58 D: ExtendableOutput + Debug + Clone,
59 F: Fn() -> D,
60 {
61 let mut hasher = new();
62 let mut buf = [0u8; 1024];
63 let buf = &mut buf[..output.len()];
64 // Test that it works when accepting the message all at once
65 hasher.update(input);
66 let mut hasher2 = hasher.clone();
67 hasher.finalize_xof_into(buf);
68 if buf != output {
69 return Some("whole message");
70 }
71 buf.iter_mut().for_each(|b| *b = 0);
72
73 // Test that it works when accepting the message in chunks
74 for n in 1..core::cmp::min(17, input.len()) {
75 let mut hasher = new();
76 for chunk in input.chunks(n) {
77 hasher.update(chunk);
78 hasher2.update(chunk);
79 }
80 hasher.finalize_xof_into(buf);
81 if buf != output {
82 return Some("message in chunks");
83 }
84 buf.iter_mut().for_each(|b| *b = 0);
85 }
86
87 None
88 }
89
90 macro_rules! new_cshake_test {
91 ($name:ident, $test_name:expr, $hasher:ty, $hasher_core:ty, $test_func:ident $(,)?) => {
92 #[test]
93 fn $name() {
94 use digest::dev::blobby::Blob3Iterator;
95 let data = include_bytes!(concat!("data/", $test_name, ".blb"));
96
97 for (i, row) in Blob3Iterator::new(data).unwrap().enumerate() {
98 let [customization, input, output] = row.unwrap();
99 if let Some(desc) = $test_func(input, output, || {
100 <$hasher>::from_core(<$hasher_core>::new(customization))
101 }) {
102 panic!(
103 "\n\
104 Failed test №{}: {}\n\
105 input:\t{:?}\n\
106 output:\t{:?}\n",
107 i, desc, input, output,
108 );
109 }
110 }
111 }
112 };
113 }
114
115 #[cfg(feature = "reset")]
116 new_cshake_test!(
117 cshake128_reset,
118 "cshake128",
119 sha3::CShake128,
120 sha3::CShake128Core,
121 cshake_reset_test
122 );
123 #[cfg(feature = "reset")]
124 new_cshake_test!(
125 cshake256_reset,
126 "cshake256",
127 sha3::CShake256,
128 sha3::CShake256Core,
129 cshake_reset_test
130 );
131
132 new_cshake_test!(
133 cshake128,
134 "cshake128",
135 sha3::CShake128,
136 sha3::CShake128Core,
137 cshake_test
138 );
139 new_cshake_test!(
140 cshake256,
141 "cshake256",
142 sha3::CShake256,
143 sha3::CShake256Core,
144 cshake_test
145 );