Skip to main content

rustls/crypto/
hmac.rs

1use alloc::boxed::Box;
2use core::mem;
3
4use pki_types::FipsStatus;
5use zeroize::Zeroize;
6
7/// A concrete HMAC implementation, for a single cryptographic hash function.
8///
9/// You should have one object that implements this trait for HMAC-SHA256, another
10/// for HMAC-SHA384, etc.
11pub trait Hmac: Send + Sync {
12    /// Prepare to use `key` as a HMAC key.
13    fn with_key(&self, key: &[u8]) -> Box<dyn Key>;
14
15    /// Give the length of the underlying hash function.  In RFC2104 terminology this is `L`.
16    fn hash_output_len(&self) -> usize;
17
18    /// Return the FIPS validation status of this implementation.
19    fn fips(&self) -> FipsStatus {
20        FipsStatus::Unvalidated
21    }
22}
23
24/// A secret HMAC tag, stored as a value.
25///
26/// The value is considered secret and sensitive, and is zeroized
27/// on drop.
28///
29/// This is suitable if the value is (for example) used as key
30/// material.
31#[derive(Clone)]
32pub struct Tag(PublicTag);
33
34impl Tag {
35    /// Build a tag by copying a byte slice.
36    ///
37    /// The slice can be up to [`Tag::MAX_LEN`] bytes in length.
38    pub fn new(bytes: &[u8]) -> Self {
39        Self(PublicTag::new(bytes))
40    }
41
42    /// Declare this tag is public.
43    ///
44    /// Uses of this function should explain why this tag is public.
45    pub(crate) fn into_public(self) -> PublicTag {
46        let public = self.0.clone();
47        mem::forget(self);
48        public
49    }
50
51    /// Maximum supported HMAC tag size: supports up to SHA512.
52    pub const MAX_LEN: usize = 64;
53}
54
55impl Drop for Tag {
56    #[inline(never)]
57    fn drop(&mut self) {
58        self.0.buf.zeroize();
59    }
60}
61
62impl AsRef<[u8]> for Tag {
63    fn as_ref(&self) -> &[u8] {
64        self.0.as_ref()
65    }
66}
67
68/// A non-secret HMAC tag, stored as a value.
69///
70/// A value of this type is **not** zeroized on drop.
71///
72/// A tag is "public" if it is published on the wire, as opposed to
73/// being used as key material. For example, the `verify_data` field
74/// of TLS `Finished` messages are public (as they are published on
75/// the wire in TLS1.2, or sent encrypted under pre-authenticated
76/// secrets in TLS1.3).
77#[derive(Clone)]
78pub(crate) struct PublicTag {
79    buf: [u8; Tag::MAX_LEN],
80    used: usize,
81}
82
83impl PublicTag {
84    /// Build a tag by copying a byte slice.
85    ///
86    /// The slice can be up to [`Tag::MAX_LEN`] bytes in length.
87    pub(crate) fn new(bytes: &[u8]) -> Self {
88        let mut tag = Self {
89            buf: [0u8; Tag::MAX_LEN],
90            used: bytes.len(),
91        };
92        tag.buf[..bytes.len()].copy_from_slice(bytes);
93        tag
94    }
95}
96
97impl AsRef<[u8]> for PublicTag {
98    fn as_ref(&self) -> &[u8] {
99        &self.buf[..self.used]
100    }
101}
102
103/// A HMAC key that is ready for use.
104///
105/// The algorithm used is implicit in the `Hmac` object that produced the key.
106pub trait Key: Send + Sync {
107    /// Calculates a tag over `data` -- a slice of byte slices.
108    fn sign(&self, data: &[&[u8]]) -> Tag {
109        self.sign_concat(&[], data, &[])
110    }
111
112    /// Calculates a tag over the concatenation of `first`, the items in `middle`, and `last`.
113    fn sign_concat(&self, first: &[u8], middle: &[&[u8]], last: &[u8]) -> Tag;
114
115    /// Returns the length of the tag returned by a computation using
116    /// this key.
117    fn tag_len(&self) -> usize;
118}