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::crypto_common;
8use digest::generic_array::{typenum::U32, typenum::U64, GenericArray};
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 GenericArray<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 GenericArray<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 crypto_common::KeySizeUser for Hasher {
71    type KeySize = U32;
72}
73
74impl crypto_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 super::*;
91
92    #[test]
93    fn test_digest_traits() {
94        // Inherent methods.
95        let mut hasher1 = crate::Hasher::new();
96        hasher1.update(b"foo");
97        hasher1.update(b"bar");
98        hasher1.update(b"baz");
99        let out1 = hasher1.finalize();
100        let mut xof1 = [0; 301];
101        hasher1.finalize_xof().fill(&mut xof1);
102        assert_eq!(out1.as_bytes(), &xof1[..32]);
103
104        // Trait implementations.
105        let mut hasher2: crate::Hasher = digest::Digest::new();
106        digest::Digest::update(&mut hasher2, b"xxx");
107        digest::Digest::reset(&mut hasher2);
108        digest::Digest::update(&mut hasher2, b"foo");
109        digest::Digest::update(&mut hasher2, b"bar");
110        digest::Digest::update(&mut hasher2, b"baz");
111        let out2 = digest::Digest::finalize(hasher2.clone());
112        let mut xof2 = [0; 301];
113        digest::XofReader::read(
114            &mut digest::ExtendableOutput::finalize_xof(hasher2.clone()),
115            &mut xof2,
116        );
117        assert_eq!(out1.as_bytes(), &out2[..]);
118        assert_eq!(xof1[..], xof2[..]);
119
120        // Again with the resetting variants.
121        let mut hasher3: crate::Hasher = digest::Digest::new();
122        digest::Digest::update(&mut hasher3, b"foobarbaz");
123        let mut out3 = [0; 32];
124        digest::FixedOutputReset::finalize_into_reset(
125            &mut hasher3,
126            GenericArray::from_mut_slice(&mut out3),
127        );
128        digest::Digest::update(&mut hasher3, b"foobarbaz");
129        let mut out4 = [0; 32];
130        digest::FixedOutputReset::finalize_into_reset(
131            &mut hasher3,
132            GenericArray::from_mut_slice(&mut out4),
133        );
134        digest::Digest::update(&mut hasher3, b"foobarbaz");
135        let mut xof3 = [0; 301];
136        digest::XofReader::read(
137            &mut digest::ExtendableOutputReset::finalize_xof_reset(&mut hasher3),
138            &mut xof3,
139        );
140        digest::Digest::update(&mut hasher3, b"foobarbaz");
141        let mut xof4 = [0; 301];
142        digest::XofReader::read(
143            &mut digest::ExtendableOutputReset::finalize_xof_reset(&mut hasher3),
144            &mut xof4,
145        );
146        assert_eq!(out1.as_bytes(), &out3[..]);
147        assert_eq!(out1.as_bytes(), &out4[..]);
148        assert_eq!(xof1[..], xof3[..]);
149        assert_eq!(xof1[..], xof4[..]);
150    }
151
152    #[test]
153    fn test_mac_trait() {
154        // Inherent methods.
155        let key = b"some super secret key bytes fooo";
156        let mut hasher1 = crate::Hasher::new_keyed(key);
157        hasher1.update(b"foo");
158        hasher1.update(b"bar");
159        hasher1.update(b"baz");
160        let out1 = hasher1.finalize();
161
162        // Trait implementation.
163        let generic_key = (*key).into();
164        let mut hasher2: crate::Hasher = digest::Mac::new(&generic_key);
165        digest::Mac::update(&mut hasher2, b"xxx");
166        digest::Mac::reset(&mut hasher2);
167        digest::Mac::update(&mut hasher2, b"foo");
168        digest::Mac::update(&mut hasher2, b"bar");
169        digest::Mac::update(&mut hasher2, b"baz");
170        let out2 = digest::Mac::finalize(hasher2);
171        assert_eq!(out1.as_bytes(), out2.into_bytes().as_slice());
172    }
173
174    fn expected_hmac_blake3(key: &[u8], input: &[u8]) -> [u8; 32] {
175        // See https://en.wikipedia.org/wiki/HMAC.
176        let key_hash;
177        let key_prime = if key.len() <= 64 {
178            key
179        } else {
180            key_hash = *crate::hash(key).as_bytes();
181            &key_hash
182        };
183        let mut ipad = [0x36; 64];
184        let mut opad = [0x5c; 64];
185        for i in 0..key_prime.len() {
186            ipad[i] ^= key_prime[i];
187            opad[i] ^= key_prime[i];
188        }
189        let mut inner_state = crate::Hasher::new();
190        inner_state.update(&ipad);
191        inner_state.update(input);
192        let mut outer_state = crate::Hasher::new();
193        outer_state.update(&opad);
194        outer_state.update(inner_state.finalize().as_bytes());
195        outer_state.finalize().into()
196    }
197
198    #[test]
199    fn test_hmac_compatibility() {
200        use hmac::{Mac, SimpleHmac};
201
202        // Test a short key.
203        let mut x = SimpleHmac::<Hasher>::new_from_slice(b"key").unwrap();
204        hmac::digest::Update::update(&mut x, b"data");
205        let output = x.finalize().into_bytes();
206        assert_ne!(output.len(), 0);
207        let expected = expected_hmac_blake3(b"key", b"data");
208        assert_eq!(expected, output.as_ref());
209
210        // Test a range of key and data lengths, particularly to exercise the long-key logic.
211        let mut input_bytes = [0; crate::test::TEST_CASES_MAX];
212        crate::test::paint_test_input(&mut input_bytes);
213        for &input_len in crate::test::TEST_CASES {
214            #[cfg(feature = "std")]
215            dbg!(input_len);
216            let input = &input_bytes[..input_len];
217
218            let mut x = SimpleHmac::<Hasher>::new_from_slice(input).unwrap();
219            hmac::digest::Update::update(&mut x, input);
220            let output = x.finalize().into_bytes();
221            assert_ne!(output.len(), 0);
222
223            let expected = expected_hmac_blake3(input, input);
224            assert_eq!(expected, output.as_ref());
225        }
226    }
227}