1use core::cmp::min;
2use core::ops::Deref;
3
4use aes::Aes128;
5use aes::cipher::BlockEncrypt;
6use aes::cipher::KeyInit as AesKeyInit;
7use aes::cipher::generic_array::GenericArray as AesGenericArray;
8use ccm::Ccm;
9use ccm::KeyInit;
10use ccm::aead::AeadMutInPlace;
11use ccm::aead::generic_array::GenericArray;
12use ccm::consts::U4;
13use ccm::consts::U13;
14use itertools::Itertools;
15
16use crate::security::SecurityError;
17
18pub struct Aes128Mmo {
22 state: [u8; Self::BLOCK_SIZE],
23}
24
25#[allow(
26 clippy::cast_sign_loss,
27 clippy::cast_possible_wrap,
28 clippy::cast_possible_truncation
29)]
30impl Aes128Mmo {
31 const BLOCK_SIZE: usize = 16;
32 const PADDING_THRESHOLD: usize = 2usize.pow(Self::BLOCK_SIZE as u32);
33
34 pub fn new() -> Self {
36 Self {
37 state: [0u8; Self::BLOCK_SIZE],
38 }
39 }
40
41 pub fn initialize(iv: &[u8]) -> Self {
42 let mut mmo = Self::new();
43 let length = min(Self::BLOCK_SIZE, iv.len());
44 mmo.state[..length].copy_from_slice(&iv[..length]);
45 mmo
46 }
47
48 pub fn update(&mut self, data: &[u8]) -> Result<(), SecurityError> {
50 self.update_impl(data.iter().copied())
51 }
52
53 fn update_impl(&mut self, data: impl Iterator<Item = u8>) -> Result<(), SecurityError> {
54 let data = data.into_iter();
55 let (_, Some(length)) = data.size_hint() else {
56 unreachable!("invalid unbounded data");
57 };
58
59 let (padded_data, pad_len) = if length < Self::PADDING_THRESHOLD {
60 let pad_len =
62 ((14i32 - (length % Self::BLOCK_SIZE) as i32) % Self::BLOCK_SIZE as i32) as usize;
63 let mut padded_data = [0u8; Self::BLOCK_SIZE + 2];
64 padded_data[0] = 0x80;
65 padded_data[pad_len..pad_len + 2].copy_from_slice(&((length * 8) as u16).to_be_bytes());
66 (padded_data, pad_len + 2)
67 } else {
68 return Err(SecurityError::InvalidData);
70 };
71
72 let pad = &padded_data[..pad_len];
73 let mut iter = data.chain(pad.iter().copied());
74 let len = length + pad_len;
75 let mut i = 0;
76 while i < len {
77 let mut block = [0u8; Self::BLOCK_SIZE];
78 for b_i in 0..Self::BLOCK_SIZE {
79 let b = iter.next();
80 if let Some(b) = b {
81 i += 1;
82 block[b_i] = b;
83 }
84 }
85
86 let cipher = Aes128::new(&AesGenericArray::from(self.state));
88 let mut encrypted_block = AesGenericArray::from(block);
89 cipher.encrypt_block(&mut encrypted_block);
90
91 for i in 0..Self::BLOCK_SIZE {
93 self.state[i] = encrypted_block[i] ^ block[i];
94 }
95 }
96
97 Ok(())
98 }
99
100 pub fn finalize(self) -> [u8; 16] {
101 self.state
102 }
103
104 pub fn digest(data: &[u8]) -> Result<[u8; Self::BLOCK_SIZE], SecurityError> {
105 Self::digest_impl(data.iter().copied())
106 }
107
108 pub fn digest_impl(
109 data: impl Iterator<Item = u8>,
110 ) -> Result<[u8; Self::BLOCK_SIZE], SecurityError> {
111 let mut hasher = Self::new();
112 hasher.update_impl(data)?;
113 Ok(hasher.finalize())
114 }
115
116 pub fn digest_with_iv(iv: &[u8], data: &[u8]) -> Result<[u8; Self::BLOCK_SIZE], SecurityError> {
117 let mut hasher = Self::initialize(iv);
118 hasher.update(data)?;
119 Ok(hasher.finalize())
120 }
121}
122
123impl Default for Aes128Mmo {
124 fn default() -> Self {
125 Self::new()
126 }
127}
128
129pub struct HmacAes128Mmo;
131
132impl HmacAes128Mmo {
133 const IPAD: u8 = 0x36;
134 const OPAD: u8 = 0x5c;
135
136 pub fn hmac(key: &[u8], data: &[u8]) -> Result<[u8; Aes128Mmo::BLOCK_SIZE], SecurityError> {
138 if key.len() == Aes128Mmo::BLOCK_SIZE {
139 return Self::hmac_impl(key, data);
140 }
141
142 let key = Aes128Mmo::digest(key)?;
143 Self::hmac_impl(&key, data)
144 }
145
146 fn hmac_impl(key: &[u8], data: &[u8]) -> Result<[u8; Aes128Mmo::BLOCK_SIZE], SecurityError> {
147 let mut ipad = [Self::IPAD; Aes128Mmo::BLOCK_SIZE];
148 let mut opad = [Self::OPAD; Aes128Mmo::BLOCK_SIZE];
149
150 for i in 0..Aes128Mmo::BLOCK_SIZE {
151 ipad[i] ^= key[i];
152 opad[i] ^= key[i];
153 }
154
155 let inner = ipad.iter().chain(data.iter());
156 let inner_hash = Aes128Mmo::digest_impl(inner.copied())?;
157
158 let outer = opad.iter().chain(inner_hash.iter());
159 Aes128Mmo::digest_impl(outer.copied())
160 }
161}
162
163#[cfg(test)]
164mod tests {
165 use super::*;
166
167 #[test]
168 fn aes_mmo_test_vector() {
169 let message = [0xc0];
170 let want = [
171 0xae, 0x3a, 0x10, 0x2a, 0x28, 0xd4, 0x3e, 0xe0, 0xd4, 0xa0, 0x9e, 0x22, 0x78, 0x8b,
172 0x20, 0x6c,
173 ];
174
175 let result = Aes128Mmo::digest(&message).unwrap();
176
177 assert_eq!(result, want);
178 }
179
180 #[test]
181 fn aes_mmo_length_equal_block_size() {
182 let message = [
183 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd,
184 0xce, 0xcf,
185 ];
186 let want = [
187 0xa7, 0x97, 0x7e, 0x88, 0xbc, 0x0b, 0x61, 0xe8, 0x21, 0x08, 0x27, 0x10, 0x9a, 0x22,
188 0x8f, 0x2d,
189 ];
190
191 let result = Aes128Mmo::digest(&message).unwrap();
192
193 assert_eq!(result, want);
194 }
195
196 #[test]
197 fn hmac_aes_mmo_1() {
198 let key = [
199 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
200 0x4e, 0x4f,
201 ];
202 let want = [
203 0x45, 0x12, 0x80, 0x7b, 0xf9, 0x4c, 0xb3, 0x40, 0x0f, 0x0e, 0x2c, 0x25, 0xfb, 0x76,
204 0xe9, 0x99,
205 ];
206 let message = [0xc0];
207
208 let result = HmacAes128Mmo::hmac(&key, &message).unwrap();
209
210 assert_eq!(result, want);
211 }
212
213 #[test]
214 fn hmac_aes_mmo_2() {
215 let key = [
216 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
217 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b,
218 0x5c, 0x5d, 0x5e, 0x5f,
219 ];
220 let want = [
221 0xa3, 0xb0, 0x07, 0x99, 0x84, 0xbf, 0x15, 0x57, 0xf7, 0x4a, 0x0d, 0x63, 0x87, 0xe0,
222 0xa1, 0x1a,
223 ];
224 let message = [
225 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd,
226 0xce, 0xcf,
227 ];
228
229 let result = HmacAes128Mmo::hmac(&key, &message).unwrap();
230
231 assert_eq!(result, want);
232 }
233}