interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / digest / src / dev / variable.rs

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