capsules_extra/test/
hmac_sha256.rs1use capsules_core::test::capsule_test::{CapsuleTest, CapsuleTestClient, CapsuleTestError};
10use kernel::ErrorCode;
11use kernel::hil::digest;
12use kernel::hil::digest::HmacSha256;
13use kernel::utilities::cells::OptionalCell;
14use kernel::utilities::cells::TakeCell;
15use kernel::utilities::leasable_buffer::SubSlice;
16use kernel::utilities::leasable_buffer::SubSliceMut;
17
18const HMAC_SHA256_DIGEST_LEN: usize = 32;
19
20pub struct TestHmacSha256<'a, H: digest::Digest<'a, HMAC_SHA256_DIGEST_LEN>> {
21 hmac: &'a H,
22 key: TakeCell<'static, [u8]>, data: TakeCell<'static, [u8]>, digest: TakeCell<'static, [u8; HMAC_SHA256_DIGEST_LEN]>, correct: &'static [u8; HMAC_SHA256_DIGEST_LEN], client: OptionalCell<&'static dyn CapsuleTestClient>,
27}
28
29impl<'a, H: digest::Digest<'a, HMAC_SHA256_DIGEST_LEN> + HmacSha256> TestHmacSha256<'a, H> {
30 pub fn new(
31 hmac: &'a H,
32 key: &'static mut [u8],
33 data: &'static mut [u8],
34 digest: &'static mut [u8; HMAC_SHA256_DIGEST_LEN],
35 correct: &'static [u8; HMAC_SHA256_DIGEST_LEN],
36 ) -> Self {
37 TestHmacSha256 {
38 hmac,
39 key: TakeCell::new(key),
40 data: TakeCell::new(data),
41 digest: TakeCell::new(digest),
42 correct,
43 client: OptionalCell::empty(),
44 }
45 }
46
47 pub fn run(&'static self) {
48 kernel::hil::digest::Digest::set_client(self.hmac, self);
49 let key = self.key.take().unwrap();
50 let r = self.hmac.set_mode_hmacsha256(key);
51 if r.is_err() {
52 panic!("HmacSha256Test: failed to set key: {:?}", r);
53 }
54 let data = self.data.take().unwrap();
55 let buffer = SubSliceMut::new(data);
56 let r = self.hmac.add_mut_data(buffer);
57 if r.is_err() {
58 panic!("HmacSha256Test: failed to add data: {:?}", r);
59 }
60 }
61}
62
63impl<'a, H: digest::Digest<'a, HMAC_SHA256_DIGEST_LEN> + HmacSha256>
64 digest::ClientData<HMAC_SHA256_DIGEST_LEN> for TestHmacSha256<'a, H>
65{
66 fn add_data_done(&self, _result: Result<(), ErrorCode>, _data: SubSlice<'static, u8>) {
67 unimplemented!()
68 }
69
70 fn add_mut_data_done(&self, result: Result<(), ErrorCode>, data: SubSliceMut<'static, u8>) {
71 self.data.replace(data.take());
72
73 match result {
74 Ok(()) => {}
75 Err(e) => {
76 kernel::debug!("HmacSha256Test: failed to add data: {:?}", e);
77 self.client.map(|client| {
78 client.done(Err(CapsuleTestError::ErrorCode(e)));
79 });
80 return;
81 }
82 }
83
84 let r = self.hmac.run(self.digest.take().unwrap());
85 match r {
86 Ok(()) => {}
87 Err((e, d)) => {
88 kernel::debug!("HmacSha256Test: failed to run HMAC: {:?}", e);
89
90 self.digest.replace(d);
91 self.client.map(|client| {
92 client.done(Err(CapsuleTestError::ErrorCode(e)));
93 });
94 }
95 }
96 }
97}
98
99impl<'a, H: digest::Digest<'a, HMAC_SHA256_DIGEST_LEN> + HmacSha256>
100 digest::ClientHash<HMAC_SHA256_DIGEST_LEN> for TestHmacSha256<'a, H>
101{
102 fn hash_done(
103 &self,
104 _result: Result<(), ErrorCode>,
105 digest: &'static mut [u8; HMAC_SHA256_DIGEST_LEN],
106 ) {
107 let mut error = false;
108 for i in 0..HMAC_SHA256_DIGEST_LEN {
109 if self.correct[i] != digest[i] {
110 error = true;
111 break;
112 }
113 }
114 if !error {
115 kernel::debug!("HMAC-SHA256 matches!");
116 self.client.map(|client| {
117 client.done(Ok(()));
118 });
119 } else {
120 kernel::debug!("HmacSha256Test: incorrect HMAC output!");
121 self.client.map(|client| {
122 client.done(Err(CapsuleTestError::IncorrectResult));
123 });
124 }
125 }
126}
127
128impl<'a, H: digest::Digest<'a, HMAC_SHA256_DIGEST_LEN> + HmacSha256>
129 digest::ClientVerify<HMAC_SHA256_DIGEST_LEN> for TestHmacSha256<'a, H>
130{
131 fn verification_done(
132 &self,
133 _result: Result<bool, ErrorCode>,
134 _compare: &'static mut [u8; HMAC_SHA256_DIGEST_LEN],
135 ) {
136 }
137}
138
139impl<'a, H: digest::Digest<'a, HMAC_SHA256_DIGEST_LEN> + HmacSha256> CapsuleTest
140 for TestHmacSha256<'a, H>
141{
142 fn set_client(&self, client: &'static dyn CapsuleTestClient) {
143 self.client.set(client);
144 }
145}