From a1e14413311427daf952f1be5b2c62d69fdccfb7 Mon Sep 17 00:00:00 2001 From: Brad Campbell Date: Wed, 7 Jun 2023 19:35:52 -0400 Subject: [PATCH] siphash24: add test --- boards/nordic/nrf52840dk/src/test/mod.rs | 5 ++ .../nrf52840dk/src/test/siphash24_test.rs | 40 ++++++++++++ capsules/extra/src/test/mod.rs | 1 + capsules/extra/src/test/siphash24.rs | 65 +++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 boards/nordic/nrf52840dk/src/test/mod.rs create mode 100644 boards/nordic/nrf52840dk/src/test/siphash24_test.rs create mode 100644 capsules/extra/src/test/siphash24.rs diff --git a/boards/nordic/nrf52840dk/src/test/mod.rs b/boards/nordic/nrf52840dk/src/test/mod.rs new file mode 100644 index 0000000000..d84e7c0d78 --- /dev/null +++ b/boards/nordic/nrf52840dk/src/test/mod.rs @@ -0,0 +1,5 @@ +// Licensed under the Apache License, Version 2.0 or the MIT License. +// SPDX-License-Identifier: Apache-2.0 OR MIT +// Copyright Tock Contributors 2023. + +pub(crate) mod siphash24_test; diff --git a/boards/nordic/nrf52840dk/src/test/siphash24_test.rs b/boards/nordic/nrf52840dk/src/test/siphash24_test.rs new file mode 100644 index 0000000000..e7223ec0b2 --- /dev/null +++ b/boards/nordic/nrf52840dk/src/test/siphash24_test.rs @@ -0,0 +1,40 @@ +// Licensed under the Apache License, Version 2.0 or the MIT License. +// SPDX-License-Identifier: Apache-2.0 OR MIT +// Copyright Tock Contributors 2023. + +//! This tests a software SipHash24 implementation. To run this test, +//! add this line to the boot sequence: +//! ``` +//! test::siphash24_test::run_siphash24(); +//! ``` + +use capsules_extra::sip_hash::SipHasher24; +use capsules_extra::test::siphash24::TestSipHash24; +use kernel::static_init; + +pub unsafe fn run_siphash24() { + let t = static_init_test_siphash24(); + t.run(); +} + +pub static mut HSTRING: [u8; 15] = *b"tickv-super-key"; +pub static mut HBUF: [u8; 64] = [0; 64]; + +pub static mut HHASH: [u8; 8] = [0; 8]; +pub static mut CHASH: [u8; 8] = [0xd1, 0xdc, 0x3b, 0x92, 0xc2, 0x5a, 0x1b, 0x30]; + +unsafe fn static_init_test_siphash24() -> &'static TestSipHash24 { + let sha = static_init!(SipHasher24<'static>, SipHasher24::new()); + kernel::deferred_call::DeferredCallClient::register(sha); + + // Copy to the 64 byte buffer because we always hash 64 bytes. + for i in 0..15 { + HBUF[i] = HSTRING[i]; + } + let test = static_init!( + TestSipHash24, + TestSipHash24::new(sha, &mut HBUF, &mut HHASH, &mut CHASH) + ); + + test +} diff --git a/capsules/extra/src/test/mod.rs b/capsules/extra/src/test/mod.rs index 08792694cd..2f918e5634 100644 --- a/capsules/extra/src/test/mod.rs +++ b/capsules/extra/src/test/mod.rs @@ -8,4 +8,5 @@ pub mod aes_gcm; pub mod crc; pub mod kv_system; pub mod sha256; +pub mod siphash24; pub mod udp; diff --git a/capsules/extra/src/test/siphash24.rs b/capsules/extra/src/test/siphash24.rs new file mode 100644 index 0000000000..2f0a6620e4 --- /dev/null +++ b/capsules/extra/src/test/siphash24.rs @@ -0,0 +1,65 @@ +// 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 SipHash24 by performing a hash +//! and checking it against the expected hash value. It uses +//! DigestData::add_date and DigestVerify::verify through the +//! Digest trait. + +use crate::sip_hash::SipHasher24; +use kernel::debug; +use kernel::hil::hasher::{Client, Hasher}; +use kernel::utilities::cells::TakeCell; +use kernel::utilities::leasable_buffer::LeasableMutableBuffer; +use kernel::ErrorCode; + +pub struct TestSipHash24 { + hasher: &'static SipHasher24<'static>, + data: TakeCell<'static, [u8]>, // The data to hash + hash: TakeCell<'static, [u8; 8]>, // The supplied hash + correct_hash: TakeCell<'static, [u8; 8]>, // The correct hash +} + +impl TestSipHash24 { + pub fn new( + hasher: &'static SipHasher24<'static>, + data: &'static mut [u8], + hash: &'static mut [u8; 8], + correct_hash: &'static mut [u8; 8], + ) -> Self { + TestSipHash24 { + hasher, + data: TakeCell::new(data), + hash: TakeCell::new(hash), + correct_hash: TakeCell::new(correct_hash), + } + } + + pub fn run(&'static self) { + self.hasher.set_client(self); + let data = self.data.take().unwrap(); + let buffer = LeasableMutableBuffer::new(data); + let r = self.hasher.add_mut_data(buffer); + if r.is_err() { + panic!("SipHash24Test: failed to add data: {:?}", r); + } + } +} + +impl Client<8> for TestSipHash24 { + fn add_mut_data_done(&self, _result: Result<(), ErrorCode>, data: &'static mut [u8]) { + self.data.replace(data); + self.hasher.run(self.hash.take().unwrap()).unwrap(); + } + + fn add_data_done(&self, _result: Result<(), ErrorCode>, _data: &'static [u8]) {} + + fn hash_done(&self, _result: Result<(), ErrorCode>, digest: &'static mut [u8; 8]) { + debug!("hashed result: {:?}", digest); + debug!("expected result: {:?}", self.correct_hash.take().unwrap()); + + self.hash.replace(digest); + self.hasher.clear_data(); + } +}