rustls/crypto/ring/
hmac.rs

1#![allow(clippy::duplicate_mod)]
2
3use alloc::boxed::Box;
4
5use super::ring_like;
6use crate::crypto;
7
8pub(crate) static HMAC_SHA256: Hmac = Hmac(&ring_like::hmac::HMAC_SHA256);
9pub(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.
11pub(crate) static HMAC_SHA512: Hmac = Hmac(&ring_like::hmac::HMAC_SHA512);
12
13pub(crate) struct Hmac(&'static ring_like::hmac::Algorithm);
14
15impl 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
29struct Key(ring_like::hmac::Key);
30
31impl crypto::hmac::Key for Key {
32    fn sign_concat(&self, first: &[u8], middle: &[&[u8]], last: &[u8]) -> crypto::hmac::Tag {
33        let mut ctx = ring_like::hmac::Context::with_key(&self.0);
34        ctx.update(first);
35        for d in middle {
36            ctx.update(d);
37        }
38        ctx.update(last);
39        crypto::hmac::Tag::new(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}