1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Test the AES GCM implementation on top of AES hardware.

use core::cell::Cell;
use kernel::debug;
use kernel::hil::symmetric_encryption::{GCMClient, AES128GCM, AES128_KEY_SIZE};
use kernel::utilities::cells::TakeCell;
use kernel::ErrorCode;

pub struct Test<'a, A: AES128GCM<'a>> {
    aes_gcm: &'a A,

    buf: TakeCell<'static, [u8]>,
    current_test: Cell<usize>,
    encrypting: Cell<bool>,

    // (key, iv, pt, aad, ct, tag)
    tests: [(
        &'static [u8],
        &'static [u8],
        &'static [u8],
        &'static [u8],
        &'static [u8],
        &'static [u8],
    ); 2],
}

impl<'a, A: AES128GCM<'a>> Test<'a, A> {
    pub fn new(aes_gcm: &'a A, buf: &'static mut [u8]) -> Test<'a, A> {
        Test {
            aes_gcm,
            buf: TakeCell::new(buf),
            current_test: Cell::new(0),
            encrypting: Cell::new(true),
            tests: [
                (
                    &KEY_128_TWELVE,
                    &IV_128_TWELVE,
                    &[],
                    &AAD_128_TWELVE,
                    &[],
                    &TAG_128_TWELVE,
                ),
                (
                    &KEY_128_THIRTEEN,
                    &IV_128_THIRTEEN,
                    &PT_128_THIRTEEN,
                    &[],
                    &CT_128_THIRTEEN,
                    &TAG_128_THIRTEEN,
                ),
            ],
        }
    }

    pub fn run(&self) {
        debug!("AES GCM* encryption/decryption tests");
        self.trigger_test();
    }

    fn next_test(&self) -> bool {
        if self.encrypting.get() {
            self.encrypting.set(false);
        } else {
            self.encrypting.set(true);
            self.current_test.set(self.current_test.get() + 1);
            if self.current_test.get() >= self.tests.len() {
                return false;
            }
        }
        true
    }

    fn trigger_test(&self) {
        let (key, iv, pt, aad, ct, tag) = self.tests[self.current_test.get()];
        let (aad_off, pt_off, pt_len) = (0, aad.len(), pt.len());
        let encrypting = self.encrypting.get();

        let buf = match self.buf.take() {
            None => panic!("aes_gcm_test failed: buffer is not present in trigger_test."),
            Some(buf) => buf,
        };

        if encrypting {
            buf[aad_off..pt_off].copy_from_slice(aad);
            buf[pt_off..pt_off + pt_len].copy_from_slice(pt);
        } else {
            buf[aad_off..pt_off].copy_from_slice(aad);
            buf[pt_off..pt_off + pt_len].copy_from_slice(ct);
            buf[pt_off + pt_len..(pt_off + pt_len + tag.len())].copy_from_slice(tag);
        }

        if self.aes_gcm.set_key(key) != Ok(()) {
            panic!("aes_gcm_test failed: cannot set key.");
        }

        if self.aes_gcm.set_iv(iv) != Ok(()) {
            panic!("aes_gcm_test failed: cannot set IV.");
        }

        let _ = self
            .aes_gcm
            .crypt(buf, aad_off, pt_off, pt_len, encrypting)
            .map_err(|(_code, buf)| {
                self.buf.replace(buf);
                panic!("Failed to start test.");
            });
    }

    fn check_test(&self, tag_is_valid: bool) {
        let (_key, _iv, pt, aad, ct, tag) = self.tests[self.current_test.get()];
        let (_aad_off, pt_off, pt_len) = (0, aad.len(), pt.len());
        let encrypting = self.encrypting.get();

        let buf = match self.buf.take() {
            None => panic!("aes_gcm_test failed: buffer is not present in check_test."),
            Some(buf) => buf,
        };

        if encrypting {
            let ct_matches = buf[pt_off..(pt_off + pt_len)]
                .iter()
                .zip(ct.iter())
                .all(|(a, b)| *a == *b);
            let tag_matches = buf[(pt_off + pt_len)..(pt_off + pt_len + tag.len())]
                .iter()
                .zip(tag.iter())
                .all(|(a, b)| *a == *b);

            if ct_matches && tag_matches && tag_is_valid {
                debug!(
                    "aes_gcm_test passed: (current_test={}, encrypting={}, tag_is_valid={})",
                    self.current_test.get(),
                    self.encrypting.get(),
                    tag_is_valid
                );
            } else {
                panic!("aes_gcm_test failed: ct_matches={}, tag_matches={}, (current_test={}, encrypting={}, tag_is_valid={}",
                       ct_matches,
                       tag_matches,
                       self.current_test.get(),
                       self.encrypting.get(),
                       tag_is_valid);
            }
        } else {
            let pt_matches = buf[pt_off..(pt_off + pt_len)]
                .iter()
                .zip(pt.iter())
                .all(|(a, b)| *a == *b);
            let tag_matches = buf[(pt_off + pt_len)..(pt_off + pt_len + tag.len())]
                .iter()
                .zip(tag.iter())
                .all(|(a, b)| *a == *b);

            if pt_matches && tag_matches && tag_is_valid {
                debug!(
                    "aes_gcm_test passed: (current_test={}, encrypting={}, tag_is_valid={})",
                    self.current_test.get(),
                    self.encrypting.get(),
                    tag_is_valid
                );
            } else {
                panic!("aes_gcm_test failed: pt_matches={}, tag_matches={}, (current_test={}, encrypting={}, tag_is_valid={}",
                       pt_matches,
                       tag_matches,
                       self.current_test.get(),
                       self.encrypting.get(),
                       tag_is_valid);
            }
        }

        self.buf.replace(buf);
    }
}

impl<'a, A: AES128GCM<'a>> GCMClient for Test<'a, A> {
    fn crypt_done(&self, buf: &'static mut [u8], res: Result<(), ErrorCode>, tag_is_valid: bool) {
        self.buf.replace(buf);
        if res != Ok(()) {
            panic!("aes_gcm_test failed: crypt_done returned {:?}", res);
        } else {
            self.check_test(tag_is_valid);
            if self.next_test() {
                self.trigger_test()
            }
        }
    }
}

static KEY_128_TWELVE: [u8; AES128_KEY_SIZE] = [
    0x26, 0x73, 0x0f, 0x1a, 0xd2, 0x4b, 0x76, 0xd6, 0x6f, 0x7a, 0xb8, 0x45, 0x9d, 0xdc, 0xd1, 0x17,
];

static IV_128_TWELVE: [u8; 12] = [
    0x1f, 0xfb, 0x3e, 0x75, 0x71, 0xcb, 0x70, 0x14, 0x5e, 0xa5, 0x16, 0x53,
];

static AAD_128_TWELVE: [u8; 90] = [
    0xbf, 0xc3, 0xa8, 0x08, 0xc0, 0x60, 0xcd, 0xfd, 0x2a, 0xb7, 0x69, 0x1b, 0x32, 0x4a, 0xb3, 0x59,
    0x29, 0xe8, 0x0f, 0x26, 0x2b, 0xf3, 0xb9, 0x4c, 0xc2, 0xf4, 0x5c, 0x62, 0xbb, 0x0f, 0x32, 0xbc,
    0x4e, 0x4b, 0x96, 0x73, 0x69, 0x11, 0x0a, 0x7b, 0x4c, 0x47, 0x82, 0x7e, 0x93, 0xa9, 0xec, 0xd7,
    0xfc, 0xda, 0x5e, 0x6a, 0x97, 0x39, 0xa0, 0xd1, 0x78, 0x6d, 0x6d, 0xc7, 0xa4, 0x5c, 0x9c, 0x1e,
    0x8e, 0xcc, 0x8f, 0x90, 0xdc, 0x70, 0xbc, 0x5a, 0x5a, 0xe1, 0xa0, 0x31, 0x3f, 0xd6, 0xef, 0x87,
    0xd7, 0xb3, 0x6e, 0x3d, 0x48, 0xc4, 0x44, 0x8f, 0x70, 0x3e,
];

static TAG_128_TWELVE: [u8; 14] = [
    0x45, 0xa9, 0xbe, 0x4c, 0x84, 0x9e, 0xcb, 0x25, 0x85, 0x42, 0x1a, 0x1f, 0x08, 0xe6,
];

static KEY_128_THIRTEEN: [u8; AES128_KEY_SIZE] = [
    0x8f, 0x85, 0xd3, 0x66, 0x16, 0xa9, 0x5f, 0xc1, 0x05, 0x86, 0xc3, 0x16, 0xb3, 0x05, 0x37, 0x70,
];

static IV_128_THIRTEEN: [u8; 12] = [
    0xd3, 0x20, 0xb5, 0x00, 0x26, 0x96, 0x09, 0xac, 0xe1, 0xbe, 0x67, 0xce,
];

static PT_128_THIRTEEN: [u8; 32] = [
    0x3a, 0x75, 0x8e, 0xe0, 0x72, 0xfc, 0x70, 0xa6, 0x42, 0x75, 0xb5, 0x6e, 0x72, 0xcb, 0x23, 0xa1,
    0x59, 0x04, 0x58, 0x9c, 0xef, 0xbe, 0xeb, 0x58, 0x48, 0xec, 0x53, 0xff, 0xc0, 0x6c, 0x7a, 0x5d,
];

static CT_128_THIRTEEN: [u8; 32] = [
    0xfb, 0x2f, 0xe3, 0xeb, 0x40, 0xed, 0xfb, 0xd2, 0x2a, 0x51, 0x6b, 0xec, 0x35, 0x9d, 0x4b, 0xb4,
    0x23, 0x8a, 0x07, 0x00, 0xa4, 0x6f, 0xee, 0x11, 0x36, 0xa0, 0x61, 0x85, 0x40, 0x22, 0x9c, 0x41,
];

static TAG_128_THIRTEEN: [u8; 16] = [
    0x42, 0x26, 0x93, 0x16, 0xce, 0xce, 0x7d, 0x88, 0x2c, 0xc6, 0x8c, 0x3e, 0xd9, 0xd2, 0xf0, 0xae,
];