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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// 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 the software implementation of HMAC-SHA256 by performing a hash and
//! checking it against the expected hash value.

use crate::hmac_sha256::HmacSha256Software;
use crate::sha256::Sha256Software;
use capsules_core::test::capsule_test::{CapsuleTest, CapsuleTestClient, CapsuleTestError};
use kernel::hil::digest;
use kernel::hil::digest::HmacSha256;
use kernel::hil::digest::{DigestData, DigestHash};
use kernel::utilities::cells::OptionalCell;
use kernel::utilities::cells::TakeCell;
use kernel::utilities::leasable_buffer::SubSlice;
use kernel::utilities::leasable_buffer::SubSliceMut;
use kernel::ErrorCode;

pub struct TestHmacSha256 {
    hmac: &'static HmacSha256Software<'static, Sha256Software<'static>>,
    key: TakeCell<'static, [u8]>,        // The key to use for HMAC
    data: TakeCell<'static, [u8]>,       // The data to hash
    digest: TakeCell<'static, [u8; 32]>, // The supplied hash
    correct: &'static [u8; 32],          // The supplied hash
    client: OptionalCell<&'static dyn CapsuleTestClient>,
}

impl TestHmacSha256 {
    pub fn new(
        hmac: &'static HmacSha256Software<'static, Sha256Software<'static>>,
        key: &'static mut [u8],
        data: &'static mut [u8],
        digest: &'static mut [u8; 32],
        correct: &'static [u8; 32],
    ) -> Self {
        TestHmacSha256 {
            hmac,
            key: TakeCell::new(key),
            data: TakeCell::new(data),
            digest: TakeCell::new(digest),
            correct,
            client: OptionalCell::empty(),
        }
    }

    pub fn run(&'static self) {
        kernel::hil::digest::Digest::set_client(self.hmac, self);

        let key = self.key.take().unwrap();
        let r = self.hmac.set_mode_hmacsha256(key);
        if r.is_err() {
            panic!("HmacSha256Test: failed to set key: {:?}", r);
        }
        let data = self.data.take().unwrap();
        let buffer = SubSliceMut::new(data);
        let r = self.hmac.add_mut_data(buffer);
        if r.is_err() {
            panic!("HmacSha256Test: failed to add data: {:?}", r);
        }
    }
}

impl digest::ClientData<32> for TestHmacSha256 {
    fn add_data_done(&self, _result: Result<(), ErrorCode>, _data: SubSlice<'static, u8>) {
        unimplemented!()
    }

    fn add_mut_data_done(&self, result: Result<(), ErrorCode>, data: SubSliceMut<'static, u8>) {
        self.data.replace(data.take());

        match result {
            Ok(()) => {}
            Err(e) => {
                kernel::debug!("HmacSha256Test: failed to add data: {:?}", e);
                self.client.map(|client| {
                    client.done(Err(CapsuleTestError::ErrorCode(e)));
                });
                return;
            }
        }

        let r = self.hmac.run(self.digest.take().unwrap());
        match r {
            Ok(()) => {}
            Err((e, d)) => {
                kernel::debug!("HmacSha256Test: failed to run HMAC: {:?}", e);

                self.digest.replace(d);
                self.client.map(|client| {
                    client.done(Err(CapsuleTestError::ErrorCode(e)));
                });
            }
        }
    }
}

impl digest::ClientHash<32> for TestHmacSha256 {
    fn hash_done(&self, _result: Result<(), ErrorCode>, digest: &'static mut [u8; 32]) {
        let mut error = false;
        for i in 0..32 {
            if self.correct[i] != digest[i] {
                error = true;
            }
        }
        if !error {
            kernel::debug!("HMAC-SHA256 matches!");
            self.client.map(|client| {
                client.done(Ok(()));
            });
        } else {
            kernel::debug!("HmacSha256Test: incorrect HMAC output!");
            self.client.map(|client| {
                client.done(Err(CapsuleTestError::IncorrectResult));
            });
        }
    }
}

impl digest::ClientVerify<32> for TestHmacSha256 {
    fn verification_done(&self, _result: Result<bool, ErrorCode>, _compare: &'static mut [u8; 32]) {
    }
}

impl CapsuleTest for TestHmacSha256 {
    fn set_client(&self, client: &'static dyn CapsuleTestClient) {
        self.client.set(client);
    }
}