1 | #![allow (clippy::duplicate_mod)] |
2 | |
3 | use alloc::boxed::Box; |
4 | |
5 | use super::ring_like; |
6 | use crate::crypto; |
7 | |
8 | pub(crate) static HMAC_SHA256: Hmac = Hmac(&ring_like::hmac::HMAC_SHA256); |
9 | pub(crate) static HMAC_SHA384: Hmac = Hmac(&ring_like::hmac::HMAC_SHA384); |
10 | #[allow (dead_code)] // Only used for TLS 1.2 prf test, and aws-lc-rs HPKE suites. |
11 | pub(crate) static HMAC_SHA512: Hmac = Hmac(&ring_like::hmac::HMAC_SHA512); |
12 | |
13 | pub(crate) struct Hmac(&'static ring_like::hmac::Algorithm); |
14 | |
15 | impl crypto::hmac::Hmac for Hmac { |
16 | fn with_key(&self, key: &[u8]) -> Box<dyn crypto::hmac::Key> { |
17 | Box::new(Key(ring_like::hmac::Key::new(*self.0, key))) |
18 | } |
19 | |
20 | fn hash_output_len(&self) -> usize { |
21 | self.0.digest_algorithm().output_len() |
22 | } |
23 | |
24 | fn fips(&self) -> bool { |
25 | super::fips() |
26 | } |
27 | } |
28 | |
29 | struct Key(ring_like::hmac::Key); |
30 | |
31 | impl crypto::hmac::Key for Key { |
32 | fn sign_concat(&self, first: &[u8], middle: &[&[u8]], last: &[u8]) -> crypto::hmac::Tag { |
33 | let mut ctx: Context = ring_like::hmac::Context::with_key(&self.0); |
34 | ctx.update(data:first); |
35 | for d: &&[u8] in middle { |
36 | ctx.update(data:d); |
37 | } |
38 | ctx.update(data:last); |
39 | crypto::hmac::Tag::new(bytes:ctx.sign().as_ref()) |
40 | } |
41 | |
42 | fn tag_len(&self) -> usize { |
43 | self.0 |
44 | .algorithm() |
45 | .digest_algorithm() |
46 | .output_len() |
47 | } |
48 | } |
49 | |