imix/test/
aes_test.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 that AES (either CTR or CBC mode) is working properly.
6//!
7//! To test CBC mode, add the following line to the imix boot sequence:
8//! ```
9//!     test::aes_test::run_aes128_cbc();
10//! ```
11//! You should see the following output:
12//! ```
13//!     aes_test passed (CBC Enc Src/Dst)
14//!     aes_test passed (CBC Dec Src/Dst)
15//!     aes_test passed (CBC Enc In-place)
16//!     aes_test passed (CBC Dec In-place)
17//! ```
18//! To test CTR mode, add the following line to the imix boot sequence:
19//! ```
20//!     test::aes_test::run_aes128_ctr();
21//! ```
22//! You should see the following output:
23//! ```
24//!     aes_test CTR passed: (CTR Enc Ctr Src/Dst)
25//!     aes_test CTR passed: (CTR Dec Ctr Src/Dst)
26//! ```
27
28use capsules_extra::test::aes::TestAes128Cbc;
29use capsules_extra::test::aes::TestAes128Ctr;
30use kernel::hil::symmetric_encryption::{AES128, AES128_BLOCK_SIZE, AES128_KEY_SIZE};
31use kernel::static_init;
32use sam4l::aes::Aes;
33
34pub unsafe fn run_aes128_ctr(aes: &'static Aes) {
35    let t = static_init_test_ctr(aes);
36    aes.set_client(t);
37
38    t.run();
39}
40
41pub unsafe fn run_aes128_cbc(aes: &'static Aes) {
42    let t = static_init_test_cbc(aes);
43    aes.set_client(t);
44
45    t.run();
46}
47
48unsafe fn static_init_test_ctr(aes: &'static Aes) -> &'static TestAes128Ctr<'static, Aes<'static>> {
49    let source = static_init!([u8; 4 * AES128_BLOCK_SIZE], [0; 4 * AES128_BLOCK_SIZE]);
50    let data = static_init!([u8; 6 * AES128_BLOCK_SIZE], [0; 6 * AES128_BLOCK_SIZE]);
51    let key = static_init!([u8; AES128_KEY_SIZE], [0; AES128_KEY_SIZE]);
52    let iv = static_init!([u8; AES128_BLOCK_SIZE], [0; AES128_BLOCK_SIZE]);
53
54    static_init!(
55        TestAes128Ctr<'static, Aes>,
56        TestAes128Ctr::new(aes, key, iv, source, data, true)
57    )
58}
59
60unsafe fn static_init_test_cbc(aes: &'static Aes) -> &'static TestAes128Cbc<'static, Aes<'static>> {
61    let source = static_init!([u8; 4 * AES128_BLOCK_SIZE], [0; 4 * AES128_BLOCK_SIZE]);
62    let data = static_init!([u8; 6 * AES128_BLOCK_SIZE], [0; 6 * AES128_BLOCK_SIZE]);
63    let key = static_init!([u8; AES128_KEY_SIZE], [0; AES128_KEY_SIZE]);
64    let iv = static_init!([u8; AES128_BLOCK_SIZE], [0; AES128_BLOCK_SIZE]);
65
66    static_init!(
67        TestAes128Cbc<'static, Aes>,
68        TestAes128Cbc::new(aes, key, iv, source, data, true)
69    )
70}