Skip to main content

capsules_extra/test/
siphash24.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//! Test the software implementation of SipHash24 by performing a hash
6//! and checking it against the expected hash value. It uses
7//! DigestData::add_date and DigestVerify::verify through the
8//! Digest trait.
9
10use crate::sip_hash::SipHasher24;
11use capsules_core::test::capsule_test::{CapsuleTest, CapsuleTestClient, CapsuleTestError};
12use kernel::ErrorCode;
13use kernel::hil::hasher::{Client, Hasher};
14use kernel::utilities::cells::OptionalCell;
15use kernel::utilities::cells::TakeCell;
16use kernel::utilities::leasable_buffer::{SubSlice, SubSliceMut};
17
18pub struct TestSipHash24 {
19    hasher: &'static SipHasher24<'static>,
20    data: TakeCell<'static, [u8]>,            // The data to hash
21    hash: TakeCell<'static, [u8; 8]>,         // The supplied hash
22    correct_hash: TakeCell<'static, [u8; 8]>, // The correct hash
23    client: OptionalCell<&'static dyn CapsuleTestClient>,
24}
25
26impl TestSipHash24 {
27    pub fn new(
28        hasher: &'static SipHasher24<'static>,
29        data: &'static mut [u8],
30        hash: &'static mut [u8; 8],
31        correct_hash: &'static mut [u8; 8],
32    ) -> Self {
33        TestSipHash24 {
34            hasher,
35            data: TakeCell::new(data),
36            hash: TakeCell::new(hash),
37            correct_hash: TakeCell::new(correct_hash),
38            client: OptionalCell::empty(),
39        }
40    }
41
42    pub fn run(&'static self) {
43        self.hasher.set_client(self);
44        let data = self.data.take().unwrap();
45        let buffer = SubSliceMut::new(data);
46        let r = self.hasher.add_mut_data(buffer);
47        if r.is_err() {
48            panic!("SipHash24Test: failed to add data: {:?}", r);
49        }
50    }
51}
52
53impl Client<8> for TestSipHash24 {
54    fn add_mut_data_done(&self, _result: Result<(), ErrorCode>, data: SubSliceMut<'static, u8>) {
55        self.data.replace(data.take());
56        self.hasher.run(self.hash.take().unwrap()).unwrap();
57    }
58
59    fn add_data_done(&self, _result: Result<(), ErrorCode>, _data: SubSlice<'static, u8>) {}
60
61    fn hash_done(&self, _result: Result<(), ErrorCode>, digest: &'static mut [u8; 8]) {
62        let correct = self.correct_hash.take().unwrap();
63
64        let matches = correct == digest;
65        if !matches {
66            kernel::debug!("TestSipHash24: incorrect hash output!");
67            kernel::debug!("TestSipHash24: correct:  {:?}", correct);
68            kernel::debug!("TestSipHash24: computed: {:?}", digest);
69        } else {
70            kernel::debug!("TestSipHash24: matches!");
71        }
72
73        self.hash.replace(digest);
74        self.hasher.clear_data();
75
76        self.client.map(|client| {
77            let res = if matches {
78                Ok(())
79            } else {
80                Err(CapsuleTestError::IncorrectResult)
81            };
82            client.done(res);
83        });
84    }
85}
86
87impl CapsuleTest for TestSipHash24 {
88    fn set_client(&self, client: &'static dyn CapsuleTestClient) {
89        self.client.set(client);
90    }
91}