Skip to main content

blake3/
traits.rs

1//! Implementations of commonly used traits like `Digest` and `Mac` from the
2//! [`digest`](https://crates.io/crates/digest) crate.
3
4pub use digest;
5
6use crate::{Hasher, OutputReader};
7use digest::array::{Array, typenum::U32, typenum::U64};
8use digest::common;
9
10impl digest::HashMarker for Hasher {}
11
12impl digest::Update for Hasher {
13    #[inline]
14    fn update(&mut self, data: &[u8]) {
15        self.update(data);
16    }
17}
18
19impl digest::Reset for Hasher {
20    #[inline]
21    fn reset(&mut self) {
22        self.reset(); // the inherent method
23    }
24}
25
26impl digest::OutputSizeUser for Hasher {
27    type OutputSize = U32;
28}
29
30impl digest::FixedOutput for Hasher {
31    #[inline]
32    fn finalize_into(self, out: &mut Array<u8, Self::OutputSize>) {
33        out.copy_from_slice(self.finalize().as_bytes());
34    }
35}
36
37impl digest::FixedOutputReset for Hasher {
38    #[inline]
39    fn finalize_into_reset(&mut self, out: &mut Array<u8, Self::OutputSize>) {
40        out.copy_from_slice(self.finalize().as_bytes());
41        self.reset();
42    }
43}
44
45impl digest::ExtendableOutput for Hasher {
46    type Reader = OutputReader;
47
48    #[inline]
49    fn finalize_xof(self) -> Self::Reader {
50        Hasher::finalize_xof(&self)
51    }
52}
53
54impl digest::ExtendableOutputReset for Hasher {
55    #[inline]
56    fn finalize_xof_reset(&mut self) -> Self::Reader {
57        let reader = Hasher::finalize_xof(self);
58        self.reset();
59        reader
60    }
61}
62
63impl digest::XofReader for OutputReader {
64    #[inline]
65    fn read(&mut self, buffer: &mut [u8]) {
66        self.fill(buffer);
67    }
68}
69
70impl common::KeySizeUser for Hasher {
71    type KeySize = U32;
72}
73
74impl common::BlockSizeUser for Hasher {
75    type BlockSize = U64;
76}
77
78impl digest::MacMarker for Hasher {}
79
80impl digest::KeyInit for Hasher {
81    #[inline]
82    fn new(key: &digest::Key<Self>) -> Self {
83        let key_bytes: [u8; 32] = (*key).into();
84        Hasher::new_keyed(&key_bytes)
85    }
86}
87
88#[cfg(test)]
89mod test {
90    use digest::array::AsArrayMut;
91
92    use super::*;
93
94    #[test]
95    fn test_digest_traits() {
96        // Inherent methods.
97        let mut hasher1 = crate::Hasher::new();
98        hasher1.update(b"foo");
99        hasher1.update(b"bar");
100        hasher1.update(b"baz");
101        let out1 = hasher1.finalize();
102        let mut xof1 = [0; 301];
103        hasher1.finalize_xof().fill(&mut xof1);
104        assert_eq!(out1.as_bytes(), &xof1[..32]);
105
106        // Trait implementations.
107        let mut hasher2: crate::Hasher = digest::Digest::new();
108        digest::Digest::update(&mut hasher2, b"xxx");
109        digest::Digest::reset(&mut hasher2);
110        digest::Digest::update(&mut hasher2, b"foo");
111        digest::Digest::update(&mut hasher2, b"bar");
112        digest::Digest::update(&mut hasher2, b"baz");
113        let out2 = digest::Digest::finalize(hasher2.clone());
114        let mut xof2 = [0; 301];
115        digest::XofReader::read(
116            &mut digest::ExtendableOutput::finalize_xof(hasher2.clone()),
117            &mut xof2,
118        );
119        assert_eq!(out1.as_bytes(), &out2[..]);
120        assert_eq!(xof1[..], xof2[..]);
121
122        // Again with the resetting variants.
123        let mut hasher3: crate::Hasher = digest::Digest::new();
124        digest::Digest::update(&mut hasher3, b"foobarbaz");
125        let mut out3 = [0; 32];
126        digest::FixedOutputReset::finalize_into_reset(&mut hasher3, out3.as_array_mut());
127        digest::Digest::update(&mut hasher3, b"foobarbaz");
128        let mut out4 = [0; 32];
129        digest::FixedOutputReset::finalize_into_reset(&mut hasher3, out4.as_array_mut());
130        digest::Digest::update(&mut hasher3, b"foobarbaz");
131        let mut xof3 = [0; 301];
132        digest::XofReader::read(
133            &mut digest::ExtendableOutputReset::finalize_xof_reset(&mut hasher3),
134            &mut xof3,
135        );
136        digest::Digest::update(&mut hasher3, b"foobarbaz");
137        let mut xof4 = [0; 301];
138        digest::XofReader::read(
139            &mut digest::ExtendableOutputReset::finalize_xof_reset(&mut hasher3),
140            &mut xof4,
141        );
142        assert_eq!(out1.as_bytes(), &out3[..]);
143        assert_eq!(out1.as_bytes(), &out4[..]);
144        assert_eq!(xof1[..], xof3[..]);
145        assert_eq!(xof1[..], xof4[..]);
146    }
147
148    #[test]
149    fn test_mac_trait() {
150        // Inherent methods.
151        let key = b"some super secret key bytes fooo";
152        let mut hasher1 = crate::Hasher::new_keyed(key);
153        hasher1.update(b"foo");
154        hasher1.update(b"bar");
155        hasher1.update(b"baz");
156        let out1 = hasher1.finalize();
157
158        // Trait implementation.
159        let generic_key = (*key).into();
160        let mut hasher2: crate::Hasher = digest::KeyInit::new(&generic_key);
161        digest::Mac::update(&mut hasher2, b"xxx");
162        digest::Mac::reset(&mut hasher2);
163        digest::Mac::update(&mut hasher2, b"foo");
164        digest::Mac::update(&mut hasher2, b"bar");
165        digest::Mac::update(&mut hasher2, b"baz");
166        let out2 = digest::Mac::finalize(hasher2);
167        assert_eq!(out1.as_bytes(), out2.into_bytes().as_slice());
168    }
169
170    fn expected_hmac_blake3(key: &[u8], input: &[u8]) -> [u8; 32] {
171        // See https://en.wikipedia.org/wiki/HMAC.
172        let key_hash;
173        let key_prime = if key.len() <= 64 {
174            key
175        } else {
176            key_hash = *crate::hash(key).as_bytes();
177            &key_hash
178        };
179        let mut ipad = [0x36; 64];
180        let mut opad = [0x5c; 64];
181        for i in 0..key_prime.len() {
182            ipad[i] ^= key_prime[i];
183            opad[i] ^= key_prime[i];
184        }
185        let mut inner_state = crate::Hasher::new();
186        inner_state.update(&ipad);
187        inner_state.update(input);
188        let mut outer_state = crate::Hasher::new();
189        outer_state.update(&opad);
190        outer_state.update(inner_state.finalize().as_bytes());
191        outer_state.finalize().into()
192    }
193
194    #[test]
195    fn test_hmac_compatibility() {
196        use hmac::{KeyInit, Mac, SimpleHmac};
197
198        // Test a short key.
199        let mut x = SimpleHmac::<Hasher>::new_from_slice(b"key").unwrap();
200        hmac::digest::Update::update(&mut x, b"data");
201        let output = x.finalize().into_bytes();
202        assert_ne!(output.len(), 0);
203        let expected = expected_hmac_blake3(b"key", b"data");
204        assert_eq!(expected, output.as_ref());
205
206        // Test a range of key and data lengths, particularly to exercise the long-key logic.
207        let mut input_bytes = [0; crate::test::TEST_CASES_MAX];
208        crate::test::paint_test_input(&mut input_bytes);
209        for &input_len in crate::test::TEST_CASES {
210            #[cfg(feature = "std")]
211            dbg!(input_len);
212            let input = &input_bytes[..input_len];
213
214            let mut x = SimpleHmac::<Hasher>::new_from_slice(input).unwrap();
215            hmac::digest::Update::update(&mut x, input);
216            let output = x.finalize().into_bytes();
217            assert_ne!(output.len(), 0);
218
219            let expected = expected_hmac_blake3(input, input);
220            assert_eq!(expected, output.as_ref());
221        }
222    }
223}