embedded_cal_rustcrypto/
aead.rs1use super::*;
5use embedded_cal::{AeadProvider, Cal};
6
7type AesCcm16_64_128 = ccm::Ccm<aes::Aes128, ccm::consts::U8, ccm::consts::U13>;
8type AesCcm16_64_256 = ccm::Ccm<aes::Aes256, ccm::consts::U8, ccm::consts::U13>;
9
10#[derive(Clone, Debug, Eq, PartialEq)]
11pub enum AeadAlgorithm<BA> {
12 AesCcm16_64_128,
13 AesCcm16_64_256,
14 Direct(BA),
15}
16
17impl<BA: embedded_cal::AeadAlgorithm> embedded_cal::AeadAlgorithm for AeadAlgorithm<BA> {
18 fn key_length(&self) -> usize {
19 match self {
20 AeadAlgorithm::AesCcm16_64_128 => 16,
21 AeadAlgorithm::AesCcm16_64_256 => 32,
22 AeadAlgorithm::Direct(a) => a.key_length(),
23 }
24 }
25
26 fn tag_length(&self) -> usize {
27 match self {
28 AeadAlgorithm::AesCcm16_64_128 => 8,
29 AeadAlgorithm::AesCcm16_64_256 => 8,
30 AeadAlgorithm::Direct(a) => a.tag_length(),
31 }
32 }
33
34 fn nonce_length(&self) -> usize {
35 match self {
36 AeadAlgorithm::AesCcm16_64_128 => 13,
37 AeadAlgorithm::AesCcm16_64_256 => 13,
38 AeadAlgorithm::Direct(a) => a.nonce_length(),
39 }
40 }
41
42 #[inline]
43 fn from_cose_number(number: impl Into<i128>) -> Option<Self> {
44 let number: i128 = number.into();
45 if let Some(a) = BA::from_cose_number(number) {
46 return Some(AeadAlgorithm::Direct(a));
47 }
48 match number {
49 10 => Some(AeadAlgorithm::AesCcm16_64_128),
50 11 => Some(AeadAlgorithm::AesCcm16_64_256),
51 _ => None,
52 }
53 }
54}
55
56pub enum AeadKey<BK> {
57 AesCcm16_64_128([u8; 16]),
58 AesCcm16_64_256([u8; 32]),
59 Direct(BK),
60}
61
62pub enum AeadTag<BT> {
63 AesCcm16_64_128([u8; 8]),
64 AesCcm16_64_256([u8; 8]),
65 Direct(BT),
66}
67
68impl<BT: AsRef<[u8]>> AsRef<[u8]> for AeadTag<BT> {
69 fn as_ref(&self) -> &[u8] {
70 match self {
71 AeadTag::AesCcm16_64_128(t) => t,
72 AeadTag::AesCcm16_64_256(t) => t,
73 AeadTag::Direct(t) => t.as_ref(),
74 }
75 }
76}
77
78impl<Base: Cal> AeadProvider for RustcryptoCalExtender<Base> {
79 type Algorithm = AeadAlgorithm<AeadAlgorithmOf<Base>>;
80 type Key = AeadKey<AeadKeyOf<Base>>;
81 type Tag = AeadTag<AeadTagOf<Base>>;
82
83 fn load_from_keydata(&mut self, alg: Self::Algorithm, key: &[u8]) -> Self::Key {
84 match alg {
85 AeadAlgorithm::AesCcm16_64_128 => {
86 AeadKey::AesCcm16_64_128(key.try_into().expect("key length mismatch"))
87 }
88 AeadAlgorithm::AesCcm16_64_256 => {
89 AeadKey::AesCcm16_64_256(key.try_into().expect("key length mismatch"))
90 }
91 AeadAlgorithm::Direct(alg) => {
92 AeadKey::Direct(self.base.aead().load_from_keydata(alg, key))
93 }
94 }
95 }
96
97 #[allow(
98 clippy::unnecessary_fallible_conversions,
99 reason = "GenericArray has infallible conversions but they panic"
100 )]
101 fn encrypt_in_place(
102 &mut self,
103 key: &Self::Key,
104 nonce: &[u8],
105 message: &mut [u8],
106 aad: impl embedded_cal::AadGenerator,
107 ) -> Self::Tag {
108 use ccm::{KeyInit, aead::AeadInOut};
109
110 if let AeadKey::Direct(key) = key {
111 return AeadTag::Direct(self.base.aead().encrypt_in_place(key, nonce, message, aad));
112 }
113
114 let aad_linear = self.collect_aad(aad);
115
116 match key {
117 AeadKey::AesCcm16_64_128(key) => AeadTag::AesCcm16_64_128(
118 AesCcm16_64_128::new(key.into())
119 .encrypt_inout_detached(
120 nonce.try_into().expect("nonce length mismatch"),
121 aad_linear.as_ref(),
122 message.into(),
123 )
124 .expect("Preconfigured sizes should not allow encryption to fail")
125 .into(),
126 ),
127 AeadKey::AesCcm16_64_256(key) => AeadTag::AesCcm16_64_256(
128 AesCcm16_64_256::new(key.into())
129 .encrypt_inout_detached(
130 nonce.try_into().expect("nonce length mismatch"),
131 aad_linear.as_ref(),
132 message.into(),
133 )
134 .expect("Preconfigured sizes should not allow encryption to fail")
135 .into(),
136 ),
137 AeadKey::Direct(_) => {
138 unreachable!("Code path without common AAD collection was checked earlier")
139 }
140 }
141 }
142
143 #[allow(
144 clippy::unnecessary_fallible_conversions,
145 reason = "GenericArray has infallible conversions but they panic"
146 )]
147 fn decrypt_in_place(
148 &mut self,
149 key: &Self::Key,
150 nonce: &[u8],
151 message: &mut [u8],
152 tag: &[u8],
153 aad: impl embedded_cal::AadGenerator,
154 ) -> Result<(), embedded_cal::DecryptionFailed> {
155 use ccm::{KeyInit, aead::AeadInOut};
156
157 if let AeadKey::Direct(key) = key {
158 return self
159 .base
160 .aead()
161 .decrypt_in_place(key, nonce, message, tag, aad);
162 }
163
164 let aad_linear = self.collect_aad(aad);
165
166 match key {
167 AeadKey::AesCcm16_64_128(key) => AesCcm16_64_128::new(key.into())
168 .decrypt_inout_detached(
169 nonce.try_into().expect("nonce length mismatch"),
170 aad_linear.as_ref(),
171 message.into(),
172 tag.try_into().expect("tag length mismatch"),
173 )
174 .map_err(|_| embedded_cal::DecryptionFailed),
175 AeadKey::AesCcm16_64_256(key) => AesCcm16_64_256::new(key.into())
176 .decrypt_inout_detached(
177 nonce.try_into().expect("nonce length mismatch"),
178 aad_linear.as_ref(),
179 message.into(),
180 tag.try_into().expect("tag length mismatch"),
181 )
182 .map_err(|_| embedded_cal::DecryptionFailed),
183 AeadKey::Direct(_) => {
184 unreachable!("Code path without common AAD collection was checked earlier")
185 }
186 }
187 }
188}