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
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! This tests a software SHA256 implementation.
//!
//! This test uses a deferred call (for callbacks). It tries to
//! hash 'hello world' and uses Digest::validate to check that the hash
//! is correct.
//!
//! The expected output is
//! Sha256Test: Verification result: Ok(true)
//!
//! This tests whether the SHA-256 hash of the string "hello hello
//! hello hello hello hello hello hello hello hello hello hello "
//! hashes correctly. This string is 12 repetitions of "hello ", so is
//! 72 bytes long. As SHA uses 64-byte/512 bit blocks, this verifies
//! that multi-block hashes work correctly.

use core::ptr::addr_of_mut;

use capsules_core::test::capsule_test::{CapsuleTest, CapsuleTestClient};
use capsules_extra::sha256::Sha256Software;
use capsules_extra::test::sha256::TestSha256;
use kernel::static_init;

pub unsafe fn run_sha256(client: &'static dyn CapsuleTestClient) {
    let t = static_init_test_sha256(client);
    t.run();
}

// // HSTRING is "hello world" and HHASH is the SHA-256 hash of this string.
// pub static mut HSTRING: [u8; 11] = *b"hello world";

// pub static mut HHASH: [u8; 32] = [
//     0xB9, 0x4D, 0x27, 0xB9, 0x93, 0x4D, 0x3E, 0x08, 0xA5, 0x2E, 0x52, 0xD7, 0xDA, 0x7D, 0xAB, 0xFA,
//     0xC4, 0x84, 0xEF, 0xE3, 0x7A, 0x53, 0x80, 0xEE, 0x90, 0x88, 0xF7, 0xAC, 0xE2, 0xEF, 0xCD, 0xE9,
// ];

// LSTRING is 12 repetitions of "hello " (72 bytes long) and LHASH is
// the SHA-256 hash of this string.
pub static mut LSTRING: [u8; 72] = [0; 72];
pub static mut LHASH: [u8; 32] = [
    0x59, 0x42, 0xc3, 0x71, 0x6f, 0x02, 0x82, 0x89, 0x3f, 0xbe, 0x04, 0x9b, 0xa2, 0x0e, 0x56, 0x0e,
    0x45, 0x94, 0xd5, 0xee, 0x15, 0xcb, 0x8a, 0x1e, 0x28, 0x7c, 0x20, 0x12, 0xc2, 0xce, 0xb5, 0xa9,
];

unsafe fn static_init_test_sha256(client: &'static dyn CapsuleTestClient) -> &'static TestSha256 {
    let sha = static_init!(Sha256Software<'static>, Sha256Software::new());
    kernel::deferred_call::DeferredCallClient::register(sha);
    let bytes = b"hello ";
    for i in 0..12 {
        for j in 0..6 {
            LSTRING[i * 6 + j] = bytes[j];
        }
    }
    // We expect LSTRING to hash to LHASH, so final argument is true
    let test = static_init!(
        TestSha256,
        TestSha256::new(
            sha,
            &mut *addr_of_mut!(LSTRING),
            &mut *addr_of_mut!(LHASH),
            true
        )
    );
    test.set_client(client);

    test
}