Skip to main content

embedded_cal_rustcrypto/
hmac.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2// SPDX-FileCopyrightText: Inria-AIO, Cryspen, and Christian Amsüss
3
4use super::*;
5use ::hmac::{KeyInit, Mac};
6use embedded_cal::{Cal, HmacProvider};
7
8type HmacSha256 = ::hmac::Hmac<sha2::Sha256>;
9
10// std only macro-generates `Default` for arrays up to 32 elements
11type MaxLenBuf = generic_array::GenericArray<u8, digest::consts::U64>;
12
13#[derive(PartialEq, Eq, Debug, Clone)]
14pub enum HmacAlgorithm<BA> {
15    HmacSha256,
16    Direct(BA),
17}
18
19impl<BA: embedded_cal::HmacAlgorithm> embedded_cal::HmacAlgorithm for HmacAlgorithm<BA> {
20    // FIXME: computing a max(32, BA::MAX_LEN) here would need a generic const expression
21    // which is not available on stable Rust.
22    const MAX_LEN: usize = 64;
23
24    type MaxLenBuf = MaxLenBuf;
25
26    fn len(&self) -> usize {
27        match self {
28            HmacAlgorithm::HmacSha256 => 32,
29            HmacAlgorithm::Direct(a) => a.len(),
30        }
31    }
32
33    #[inline]
34    fn from_cose_number(number: impl Into<i128>) -> Option<Self> {
35        let number: i128 = number.into();
36        if let Some(a) = BA::from_cose_number(number) {
37            return Some(HmacAlgorithm::Direct(a));
38        }
39        match number {
40            5 => Some(HmacAlgorithm::HmacSha256),
41            _ => None,
42        }
43    }
44}
45
46#[derive(Clone)]
47pub enum HmacKey<BK> {
48    HmacSha256(HmacSha256),
49    Direct(BK),
50}
51
52pub enum HmacState<BS> {
53    HmacSha256(HmacSha256),
54    Direct(BS),
55}
56
57pub enum HmacResult<BR> {
58    HmacSha256([u8; 32]),
59    Direct(BR),
60}
61
62impl<BR: AsRef<[u8]>> AsRef<[u8]> for HmacResult<BR> {
63    fn as_ref(&self) -> &[u8] {
64        match self {
65            HmacResult::HmacSha256(r) => &r[..],
66            HmacResult::Direct(r) => r.as_ref(),
67        }
68    }
69}
70
71impl<Base: Cal> HmacProvider for RustcryptoCalExtender<Base> {
72    type Algorithm = HmacAlgorithm<HmacAlgorithmOf<Base>>;
73    type Key = HmacKey<HmacKeyOf<Base>>;
74    type State = HmacState<HmacStateOf<Base>>;
75    type Output = HmacResult<HmacOutputOf<Base>>;
76
77    fn load_from_keydata(&mut self, algorithm: Self::Algorithm, key: &[u8]) -> Self::Key {
78        match algorithm {
79            HmacAlgorithm::HmacSha256 => HmacKey::HmacSha256(
80                HmacSha256::new_from_slice(key).expect("HMAC accepts keys of any length"),
81            ),
82            HmacAlgorithm::Direct(a) => HmacKey::Direct(self.base.hmac().load_from_keydata(a, key)),
83        }
84    }
85
86    fn init(&mut self, key: Self::Key) -> Self::State {
87        match key {
88            HmacKey::HmacSha256(h) => HmacState::HmacSha256(h),
89            HmacKey::Direct(k) => HmacState::Direct(self.base.hmac().init(k)),
90        }
91    }
92
93    fn update(&mut self, state: &mut Self::State, data: &[u8]) {
94        match state {
95            HmacState::HmacSha256(h) => h.update(data),
96            HmacState::Direct(s) => self.base.hmac().update(s, data),
97        }
98    }
99
100    fn finalize(&mut self, state: Self::State) -> Self::Output {
101        match state {
102            HmacState::HmacSha256(h) => HmacResult::HmacSha256(h.finalize().into_bytes().into()),
103            HmacState::Direct(s) => HmacResult::Direct(self.base.hmac().finalize(s)),
104        }
105    }
106}