nrf52840dk_test_kernel/test/
hmac_sha256_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//! This tests a software HMAC-SHA256 implementation.
6
7use core::ptr::{addr_of, addr_of_mut};
8
9use capsules_core::test::capsule_test::{CapsuleTest, CapsuleTestClient};
10use capsules_extra::hmac_sha256::HmacSha256Software;
11use capsules_extra::sha256::Sha256Software;
12use capsules_extra::test::hmac_sha256::TestHmacSha256;
13use kernel::deferred_call::DeferredCallClient;
14use kernel::static_init;
15
16pub unsafe fn run_hmacsha256(client: &'static dyn CapsuleTestClient) {
17    let t = static_init_test_hmacsha256(client);
18    t.run();
19}
20
21pub static mut DIGEST_DATA: [u8; 32] = [0; 32];
22
23// Test from https://en.wikipedia.org/wiki/HMAC#Examples
24pub static mut WIKI_STR: [u8; 43] = *b"The quick brown fox jumps over the lazy dog";
25pub static mut WIKI_KEY: [u8; 3] = *b"key";
26pub static mut WIKI_HMAC: [u8; 32] = [
27    0xf7, 0xbc, 0x83, 0xf4, 0x30, 0x53, 0x84, 0x24, 0xb1, 0x32, 0x98, 0xe6, 0xaa, 0x6f, 0xb1, 0x43,
28    0xef, 0x4d, 0x59, 0xa1, 0x49, 0x46, 0x17, 0x59, 0x97, 0x47, 0x9d, 0xbc, 0x2d, 0x1a, 0x3c, 0xd8,
29];
30
31unsafe fn static_init_test_hmacsha256(
32    client: &'static dyn CapsuleTestClient,
33) -> &'static TestHmacSha256 {
34    let sha256_hash_buf = static_init!([u8; 64], [0; 64]);
35
36    let sha256 = static_init!(Sha256Software<'static>, Sha256Software::new());
37    sha256.register();
38
39    let hmacsha256_verify_buf = static_init!([u8; 32], [0; 32]);
40
41    let hmacsha256 = static_init!(
42        HmacSha256Software<'static, Sha256Software<'static>>,
43        HmacSha256Software::new(sha256, sha256_hash_buf, hmacsha256_verify_buf)
44    );
45    kernel::hil::digest::Digest::set_client(sha256, hmacsha256);
46
47    let test = static_init!(
48        TestHmacSha256,
49        TestHmacSha256::new(
50            hmacsha256,
51            &mut *addr_of_mut!(WIKI_KEY),
52            &mut *addr_of_mut!(WIKI_STR),
53            &mut *addr_of_mut!(DIGEST_DATA),
54            &*addr_of!(WIKI_HMAC)
55        )
56    );
57    test.set_client(client);
58
59    test
60}