nrf52840dk_test_kernel/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_core::test::capsule_test::{CapsuleTest, CapsuleTestClient};
29use capsules_extra::test::aes::TestAes128Cbc;
30use capsules_extra::test::aes::TestAes128Ctr;
31use capsules_extra::test::aes::TestAes128Ecb;
32use kernel::hil::symmetric_encryption::{AES128, AES128_BLOCK_SIZE, AES128_KEY_SIZE};
33use kernel::static_init;
34use nrf52840::aes::AesECB;
35
36pub unsafe fn run_aes128_ctr(aes: &'static AesECB, client: &'static dyn CapsuleTestClient) {
37    let t = static_init_test_ctr(aes, client);
38    aes.set_client(t);
39
40    t.run();
41}
42
43pub unsafe fn run_aes128_cbc(aes: &'static AesECB, client: &'static dyn CapsuleTestClient) {
44    let t = static_init_test_cbc(aes, client);
45    aes.set_client(t);
46
47    t.run();
48}
49
50pub unsafe fn run_aes128_ecb(aes: &'static AesECB, client: &'static dyn CapsuleTestClient) {
51    let t = static_init_test_ecb(aes, client);
52    aes.set_client(t);
53
54    t.run();
55}
56
57unsafe fn static_init_test_ctr(
58    aes: &'static AesECB,
59    client: &'static dyn CapsuleTestClient,
60) -> &'static TestAes128Ctr<'static, AesECB<'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    let test = static_init!(
67        TestAes128Ctr<'static, AesECB>,
68        TestAes128Ctr::new(aes, key, iv, source, data, true)
69    );
70    test.set_client(client);
71    test
72}
73
74unsafe fn static_init_test_cbc(
75    aes: &'static AesECB,
76    client: &'static dyn CapsuleTestClient,
77) -> &'static TestAes128Cbc<'static, AesECB<'static>> {
78    let source = static_init!([u8; 4 * AES128_BLOCK_SIZE], [0; 4 * AES128_BLOCK_SIZE]);
79    let data = static_init!([u8; 6 * AES128_BLOCK_SIZE], [0; 6 * AES128_BLOCK_SIZE]);
80    let key = static_init!([u8; AES128_KEY_SIZE], [0; AES128_KEY_SIZE]);
81    let iv = static_init!([u8; AES128_BLOCK_SIZE], [0; AES128_BLOCK_SIZE]);
82
83    let test = static_init!(
84        TestAes128Cbc<'static, AesECB>,
85        TestAes128Cbc::new(aes, key, iv, source, data, false)
86    );
87    test.set_client(client);
88    test
89}
90
91unsafe fn static_init_test_ecb(
92    aes: &'static AesECB,
93    client: &'static dyn CapsuleTestClient,
94) -> &'static TestAes128Ecb<'static, AesECB<'static>> {
95    let source = static_init!([u8; 4 * AES128_BLOCK_SIZE], [0; 4 * AES128_BLOCK_SIZE]);
96    let data = static_init!([u8; 6 * AES128_BLOCK_SIZE], [0; 6 * AES128_BLOCK_SIZE]);
97    let key = static_init!([u8; AES128_KEY_SIZE], [0; AES128_KEY_SIZE]);
98
99    let test = static_init!(
100        TestAes128Ecb<'static, AesECB>,
101        TestAes128Ecb::new(aes, key, source, data, false)
102    );
103    test.set_client(client);
104    test
105}