nrf52840dk_test_kernel/test/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 SHA256 implementation.
6//!
7//! This test uses a deferred call (for callbacks). It tries to
8//! hash 'hello world' and uses Digest::validate to check that the hash
9//! is correct.
10//!
11//! The expected output is
12//! Sha256Test: Verification result: Ok(true)
13//!
14//! This tests whether the SHA-256 hash of the string "hello hello
15//! hello hello hello hello hello hello hello hello hello hello "
16//! hashes correctly. This string is 12 repetitions of "hello ", so is
17//! 72 bytes long. As SHA uses 64-byte/512 bit blocks, this verifies
18//! that multi-block hashes work correctly.
19
20use core::ptr::addr_of_mut;
21
22use capsules_core::test::capsule_test::{CapsuleTest, CapsuleTestClient};
23use capsules_extra::sha256::Sha256Software;
24use capsules_extra::test::sha256::TestSha256;
25use kernel::static_init;
26
27pub unsafe fn run_sha256(client: &'static dyn CapsuleTestClient) {
28 let t = static_init_test_sha256(client);
29 t.run();
30}
31
32// // HSTRING is "hello world" and HHASH is the SHA-256 hash of this string.
33// pub static mut HSTRING: [u8; 11] = *b"hello world";
34
35// pub static mut HHASH: [u8; 32] = [
36// 0xB9, 0x4D, 0x27, 0xB9, 0x93, 0x4D, 0x3E, 0x08, 0xA5, 0x2E, 0x52, 0xD7, 0xDA, 0x7D, 0xAB, 0xFA,
37// 0xC4, 0x84, 0xEF, 0xE3, 0x7A, 0x53, 0x80, 0xEE, 0x90, 0x88, 0xF7, 0xAC, 0xE2, 0xEF, 0xCD, 0xE9,
38// ];
39
40// LSTRING is 12 repetitions of "hello " (72 bytes long) and LHASH is
41// the SHA-256 hash of this string.
42pub static mut LSTRING: [u8; 72] = [0; 72];
43pub static mut LHASH: [u8; 32] = [
44 0x59, 0x42, 0xc3, 0x71, 0x6f, 0x02, 0x82, 0x89, 0x3f, 0xbe, 0x04, 0x9b, 0xa2, 0x0e, 0x56, 0x0e,
45 0x45, 0x94, 0xd5, 0xee, 0x15, 0xcb, 0x8a, 0x1e, 0x28, 0x7c, 0x20, 0x12, 0xc2, 0xce, 0xb5, 0xa9,
46];
47
48unsafe fn static_init_test_sha256(client: &'static dyn CapsuleTestClient) -> &'static TestSha256 {
49 let sha = static_init!(Sha256Software<'static>, Sha256Software::new());
50 kernel::deferred_call::DeferredCallClient::register(sha);
51 let bytes = b"hello ";
52 for i in 0..12 {
53 for j in 0..6 {
54 LSTRING[i * 6 + j] = bytes[j];
55 }
56 }
57 // We expect LSTRING to hash to LHASH, so final argument is true
58 let test = static_init!(
59 TestSha256,
60 TestSha256::new(
61 sha,
62 &mut *addr_of_mut!(LSTRING),
63 &mut *addr_of_mut!(LHASH),
64 true
65 )
66 );
67 test.set_client(client);
68
69 test
70}