nrf52840dk_test_kernel/test/
aes_test.rs

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
// 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 that AES (either CTR or CBC mode) is working properly.
//!
//! To test CBC mode, add the following line to the imix boot sequence:
//! ```
//!     test::aes_test::run_aes128_cbc();
//! ```
//! You should see the following output:
//! ```
//!     aes_test passed (CBC Enc Src/Dst)
//!     aes_test passed (CBC Dec Src/Dst)
//!     aes_test passed (CBC Enc In-place)
//!     aes_test passed (CBC Dec In-place)
//! ```
//! To test CTR mode, add the following line to the imix boot sequence:
//! ```
//!     test::aes_test::run_aes128_ctr();
//! ```
//! You should see the following output:
//! ```
//!     aes_test CTR passed: (CTR Enc Ctr Src/Dst)
//!     aes_test CTR passed: (CTR Dec Ctr Src/Dst)
//! ```

use capsules_core::test::capsule_test::{CapsuleTest, CapsuleTestClient};
use capsules_extra::test::aes::TestAes128Cbc;
use capsules_extra::test::aes::TestAes128Ctr;
use capsules_extra::test::aes::TestAes128Ecb;
use kernel::hil::symmetric_encryption::{AES128, AES128_BLOCK_SIZE, AES128_KEY_SIZE};
use kernel::static_init;
use nrf52840::aes::AesECB;

pub unsafe fn run_aes128_ctr(aes: &'static AesECB, client: &'static dyn CapsuleTestClient) {
    let t = static_init_test_ctr(aes, client);
    aes.set_client(t);

    t.run();
}

pub unsafe fn run_aes128_cbc(aes: &'static AesECB, client: &'static dyn CapsuleTestClient) {
    let t = static_init_test_cbc(aes, client);
    aes.set_client(t);

    t.run();
}

pub unsafe fn run_aes128_ecb(aes: &'static AesECB, client: &'static dyn CapsuleTestClient) {
    let t = static_init_test_ecb(aes, client);
    aes.set_client(t);

    t.run();
}

unsafe fn static_init_test_ctr(
    aes: &'static AesECB,
    client: &'static dyn CapsuleTestClient,
) -> &'static TestAes128Ctr<'static, AesECB<'static>> {
    let source = static_init!([u8; 4 * AES128_BLOCK_SIZE], [0; 4 * AES128_BLOCK_SIZE]);
    let data = static_init!([u8; 6 * AES128_BLOCK_SIZE], [0; 6 * AES128_BLOCK_SIZE]);
    let key = static_init!([u8; AES128_KEY_SIZE], [0; AES128_KEY_SIZE]);
    let iv = static_init!([u8; AES128_BLOCK_SIZE], [0; AES128_BLOCK_SIZE]);

    let test = static_init!(
        TestAes128Ctr<'static, AesECB>,
        TestAes128Ctr::new(aes, key, iv, source, data, true)
    );
    test.set_client(client);
    test
}

unsafe fn static_init_test_cbc(
    aes: &'static AesECB,
    client: &'static dyn CapsuleTestClient,
) -> &'static TestAes128Cbc<'static, AesECB<'static>> {
    let source = static_init!([u8; 4 * AES128_BLOCK_SIZE], [0; 4 * AES128_BLOCK_SIZE]);
    let data = static_init!([u8; 6 * AES128_BLOCK_SIZE], [0; 6 * AES128_BLOCK_SIZE]);
    let key = static_init!([u8; AES128_KEY_SIZE], [0; AES128_KEY_SIZE]);
    let iv = static_init!([u8; AES128_BLOCK_SIZE], [0; AES128_BLOCK_SIZE]);

    let test = static_init!(
        TestAes128Cbc<'static, AesECB>,
        TestAes128Cbc::new(aes, key, iv, source, data, false)
    );
    test.set_client(client);
    test
}

unsafe fn static_init_test_ecb(
    aes: &'static AesECB,
    client: &'static dyn CapsuleTestClient,
) -> &'static TestAes128Ecb<'static, AesECB<'static>> {
    let source = static_init!([u8; 4 * AES128_BLOCK_SIZE], [0; 4 * AES128_BLOCK_SIZE]);
    let data = static_init!([u8; 6 * AES128_BLOCK_SIZE], [0; 6 * AES128_BLOCK_SIZE]);
    let key = static_init!([u8; AES128_KEY_SIZE], [0; AES128_KEY_SIZE]);

    let test = static_init!(
        TestAes128Ecb<'static, AesECB>,
        TestAes128Ecb::new(aes, key, source, data, false)
    );
    test.set_client(client);
    test
}