Debian Codegraph @

debian/rust-vendor/digest/src/dev/variable.rs

raw
1 use crate::{VariableOutput, VariableOutputReset, dev::TestVector};
2
3 /// Variable-output resettable digest test
4 pub fn variable_reset_test<D: VariableOutputReset + Clone>(
5 &TestVector { input, output }: &TestVector,
6 ) -> Result<(), &'static str> {
7 let mut hasher = D::new(output.len()).unwrap();
8 let mut buf = [0u8; 128];
9 let buf = &mut buf[..output.len()];
10 // Test that it works when accepting the message all at once
11 hasher.update(input);
12 let mut hasher2 = hasher.clone();
13 hasher.finalize_variable(buf).unwrap();
14 if buf != output {
15 return Err("whole message");
16 }
17 buf.iter_mut().for_each(|b| *b = 0);
18
19 // Test if reset works correctly
20 hasher2.reset();
21 hasher2.update(input);
22 hasher2.finalize_variable_reset(buf).unwrap();
23 if buf != output {
24 return Err("whole message after reset");
25 }
26 buf.iter_mut().for_each(|b| *b = 0);
27
28 // Test that it works when accepting the message in chunks
29 for n in 1..core::cmp::min(17, input.len()) {
30 let mut hasher = D::new(output.len()).unwrap();
31 for chunk in input.chunks(n) {
32 hasher.update(chunk);
33 hasher2.update(chunk);
34 }
35 hasher.finalize_variable(buf).unwrap();
36 if buf != output {
37 return Err("message in chunks");
38 }
39 buf.iter_mut().for_each(|b| *b = 0);
40
41 hasher2.finalize_variable_reset(buf).unwrap();
42 if buf != output {
43 return Err("message in chunks");
44 }
45 buf.iter_mut().for_each(|b| *b = 0);
46 }
47
48 Ok(())
49 }
50
51 /// Variable-output resettable digest test
52 pub fn variable_test<D: VariableOutput>(
53 &TestVector { input, output }: &TestVector,
54 ) -> Result<(), &'static str> {
55 let mut hasher = D::new(output.len()).unwrap();
56 let mut buf = [0u8; 128];
57 let buf = &mut buf[..output.len()];
58 // Test that it works when accepting the message all at once
59 hasher.update(input);
60 hasher.finalize_variable(buf).unwrap();
61 if buf != output {
62 return Err("whole message");
63 }
64 buf.iter_mut().for_each(|b| *b = 0);
65
66 // Test that it works when accepting the message in chunks
67 for n in 1..core::cmp::min(17, input.len()) {
68 let mut hasher = D::new(output.len()).unwrap();
69 for chunk in input.chunks(n) {
70 hasher.update(chunk);
71 }
72 hasher.finalize_variable(buf).unwrap();
73 if buf != output {
74 return Err("message in chunks");
75 }
76 buf.iter_mut().for_each(|b| *b = 0);
77 }
78 Ok(())
79 }