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