Skip to main content

capsules_extra/test/
aes_ccm.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! Test the AES CCM implementation on top of AES hardware.
6
7use core::cell::Cell;
8use kernel::ErrorCode;
9use kernel::debug;
10use kernel::hil::symmetric_encryption::{
11    AES128, AES128_KEY_SIZE, AESCCM, CCM_NONCE_LENGTH, CCMClient,
12};
13use kernel::utilities::cells::TakeCell;
14
15pub struct Test<'a, A: AESCCM<'a, AES128>> {
16    aes_ccm: &'a A,
17
18    buf: TakeCell<'static, [u8]>,
19    current_test: Cell<usize>,
20    encrypting: Cell<bool>,
21
22    // (a_data, m_data, c_data, nonce, confidential, mic_len)
23    tests: [(
24        &'static [u8],
25        &'static [u8],
26        &'static [u8],
27        &'static [u8],
28        bool,
29        usize,
30    ); 3],
31}
32
33impl<'a, A: AESCCM<'a, AES128>> Test<'a, A> {
34    pub fn new(aes_ccm: &'a A, buf: &'static mut [u8]) -> Test<'a, A> {
35        Test {
36            aes_ccm,
37            buf: TakeCell::new(buf),
38            current_test: Cell::new(0),
39            encrypting: Cell::new(true),
40            tests: [
41                (
42                    &BEACON_UNSECURED[0..26],
43                    &[],
44                    &BEACON_SECURED[26..34],
45                    &BEACON_NONCE,
46                    false,
47                    8,
48                ),
49                (
50                    &DATA_UNSECURED[0..26],
51                    &DATA_UNSECURED[26..30],
52                    &DATA_SECURED[26..30],
53                    &DATA_NONCE,
54                    true,
55                    0,
56                ),
57                (
58                    &MAC_UNSECURED[0..29],
59                    &MAC_UNSECURED[29..30],
60                    &MAC_SECURED[29..38],
61                    &MAC_NONCE,
62                    true,
63                    8,
64                ),
65            ],
66        }
67    }
68
69    pub fn run(&self) {
70        debug!("AES CCM* encryption/decryption tests");
71        self.trigger_test();
72    }
73
74    fn next_test(&self) -> bool {
75        if self.encrypting.get() {
76            self.encrypting.set(false);
77        } else {
78            self.encrypting.set(true);
79            self.current_test.set(self.current_test.get() + 1);
80            if self.current_test.get() >= self.tests.len() {
81                return false;
82            }
83        }
84        true
85    }
86
87    fn trigger_test(&self) {
88        let (a_data, m_data, c_data, nonce, confidential, mic_len) =
89            self.tests[self.current_test.get()];
90        let (a_off, m_off, m_len) = (0, a_data.len(), m_data.len());
91        let encrypting = self.encrypting.get();
92
93        let buf = match self.buf.take() {
94            None => panic!("aes_ccm_test failed: buffer is not present in trigger_test."),
95            Some(buf) => buf,
96        };
97
98        if encrypting {
99            buf[a_off..m_off].copy_from_slice(a_data);
100            buf[m_off..m_off + m_len].copy_from_slice(m_data);
101        } else {
102            buf[a_off..m_off].copy_from_slice(a_data);
103            buf[m_off..m_off + m_len + mic_len].copy_from_slice(c_data);
104        }
105
106        if self.aes_ccm.set_key(&KEY) != Ok(()) || self.aes_ccm.set_nonce(nonce) != Ok(()) {
107            panic!("aes_ccm_test failed: cannot set key or nonce.");
108        }
109
110        let _ = self
111            .aes_ccm
112            .crypt(buf, a_off, m_off, m_len, mic_len, confidential, encrypting)
113            .map_err(|(_code, buf)| {
114                debug!("Failed to start test.");
115                self.buf.replace(buf);
116            });
117    }
118
119    fn check_test(&self, tag_is_valid: bool) {
120        let (a_data, m_data, c_data, _nonce, _confidential, mic_len) =
121            self.tests[self.current_test.get()];
122        let (a_off, m_off, m_len) = (0, a_data.len(), m_data.len());
123        let encrypting = self.encrypting.get();
124        let buf = match self.buf.take() {
125            None => panic!("aes_ccm_test failed: buffer is not present in check_test."),
126            Some(buf) => buf,
127        };
128
129        if encrypting {
130            let a_matches = buf[a_off..m_off]
131                .iter()
132                .zip(a_data.iter())
133                .all(|(a, b)| *a == *b);
134            let c_matches = buf[m_off..m_off + m_len + mic_len]
135                .iter()
136                .zip(c_data.iter())
137                .all(|(a, b)| *a == *b);
138            if a_matches && c_matches && tag_is_valid {
139                debug!(
140                    "aes_ccm_test passed: (current_test={}, encrypting={}, tag_is_valid={})",
141                    self.current_test.get(),
142                    self.encrypting.get(),
143                    tag_is_valid
144                );
145            } else {
146                debug!(
147                    "aes_ccm_test failed: a_matches={}, c_matches={}, (current_test={}, encrypting={}, tag_is_valid={}",
148                    a_matches,
149                    c_matches,
150                    self.current_test.get(),
151                    self.encrypting.get(),
152                    tag_is_valid
153                );
154                for (a, b) in buf[m_off..m_off + m_len + mic_len]
155                    .iter()
156                    .zip(c_data.iter())
157                {
158                    debug!("{:x} vs {:x}", *a, *b);
159                }
160                panic!("aes_ccm_test failed");
161            }
162        } else {
163            let a_matches = buf[a_off..m_off]
164                .iter()
165                .zip(a_data.iter())
166                .all(|(a, b)| *a == *b);
167            let m_matches = buf[m_off..m_off + m_len]
168                .iter()
169                .zip(m_data.iter())
170                .all(|(a, b)| *a == *b);
171            if a_matches && m_matches && tag_is_valid {
172                debug!(
173                    "aes_ccm_test passed: (current_test={}, encrypting={}, tag_is_valid={})",
174                    self.current_test.get(),
175                    self.encrypting.get(),
176                    tag_is_valid
177                );
178            } else {
179                panic!(
180                    "aes_ccm_test failed: a_matches={}, m_matches={}, (current_test={}, encrypting={}, tag_is_valid={}",
181                    a_matches,
182                    m_matches,
183                    self.current_test.get(),
184                    self.encrypting.get(),
185                    tag_is_valid
186                );
187            }
188        }
189
190        self.buf.replace(buf);
191    }
192}
193
194impl<'a, A: AESCCM<'a, AES128>> CCMClient for Test<'a, A> {
195    fn crypt_done(&self, buf: &'static mut [u8], res: Result<(), ErrorCode>, tag_is_valid: bool) {
196        self.buf.replace(buf);
197        if res != Ok(()) {
198            panic!("aes_ccm_test failed: crypt_done returned {:?}", res);
199        } else {
200            self.check_test(tag_is_valid);
201            if self.next_test() {
202                self.trigger_test()
203            }
204        }
205    }
206}
207
208static KEY: [u8; AES128_KEY_SIZE] = [
209    0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
210];
211
212// IEEE 802.15.4-2015, Annex C.2.1.1, Secured beacon frame
213static BEACON_SECURED: [u8; 34] = [
214    0x08, 0xD0, 0x84, 0x21, 0x43, 0x01, 0x00, 0x00, 0x00, 0x00, 0x48, 0xDE, 0xAC, 0x02, 0x05, 0x00,
215    0x00, 0x00, 0x55, 0xCF, 0x00, 0x00, 0x51, 0x52, 0x53, 0x54, 0x22, 0x3B, 0xC1, 0xEC, 0x84, 0x1A,
216    0xB5, 0x53,
217];
218
219// IEEE 802.15.4-2015, Annex C.2.1.2, Unsecured beacon frame with auxiliary
220// security header included and the security bits set
221static BEACON_UNSECURED: [u8; 26] = [
222    0x08, 0xD0, 0x84, 0x21, 0x43, 0x01, 0x00, 0x00, 0x00, 0x00, 0x48, 0xDE, 0xAC, 0x02, 0x05, 0x00,
223    0x00, 0x00, 0x55, 0xCF, 0x00, 0x00, 0x51, 0x52, 0x53, 0x54,
224];
225
226// IEEE 802.15.4-2015, Annex C.2.1.3, Nonce for beacon frame
227static BEACON_NONCE: [u8; CCM_NONCE_LENGTH] = [
228    0xAC, 0xDE, 0x48, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x02,
229];
230
231// IEEE 802.15.4-2015, Annex C.2.2.1, Secured data frame
232static DATA_SECURED: [u8; 30] = [
233    0x69, 0xDC, 0x84, 0x21, 0x43, 0x02, 0x00, 0x00, 0x00, 0x00, 0x48, 0xDE, 0xAC, 0x01, 0x00, 0x00,
234    0x00, 0x00, 0x48, 0xDE, 0xAC, 0x04, 0x05, 0x00, 0x00, 0x00, 0xD4, 0x3E, 0x02, 0x2B,
235];
236
237// IEEE 802.15.4-2015, Annex C.2.2.2, Unsecured data frame with auxiliary
238// security header included and the security bits set
239static DATA_UNSECURED: [u8; 30] = [
240    0x69, 0xDC, 0x84, 0x21, 0x43, 0x02, 0x00, 0x00, 0x00, 0x00, 0x48, 0xDE, 0xAC, 0x01, 0x00, 0x00,
241    0x00, 0x00, 0x48, 0xDE, 0xAC, 0x04, 0x05, 0x00, 0x00, 0x00, 0x61, 0x62, 0x63, 0x64,
242];
243
244// IEEE 802.15.4-2015, Annex C.2.2.2, Nonce for data frame
245static DATA_NONCE: [u8; CCM_NONCE_LENGTH] = [
246    0xAC, 0xDE, 0x48, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x04,
247];
248
249// IEEE 802.15.4-2015, Annex C.2.3.1, Secured MAC command frame
250static MAC_SECURED: [u8; 38] = [
251    0x2B, 0xDC, 0x84, 0x21, 0x43, 0x02, 0x00, 0x00, 0x00, 0x00, 0x48, 0xDE, 0xAC, 0xFF, 0xFF, 0x01,
252    0x00, 0x00, 0x00, 0x00, 0x48, 0xDE, 0xAC, 0x06, 0x05, 0x00, 0x00, 0x00, 0x01, 0xD8, 0x4F, 0xDE,
253    0x52, 0x90, 0x61, 0xF9, 0xC6, 0xF1,
254];
255
256// IEEE 802.15.4-2015, Annex C.2.3.2, Unsecured MAC command frame with auxiliary
257// security header included and the security bits set
258static MAC_UNSECURED: [u8; 30] = [
259    0x2B, 0xDC, 0x84, 0x21, 0x43, 0x02, 0x00, 0x00, 0x00, 0x00, 0x48, 0xDE, 0xAC, 0xFF, 0xFF, 0x01,
260    0x00, 0x00, 0x00, 0x00, 0x48, 0xDE, 0xAC, 0x06, 0x05, 0x00, 0x00, 0x00, 0x01, 0xCE,
261];
262
263// IEEE 802.15.4-2015, Annex C.2.3.2, Nonce for MAC frame
264static MAC_NONCE: [u8; CCM_NONCE_LENGTH] = [
265    0xAC, 0xDE, 0x48, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x06,
266];