1use core::future::{Future, ready};
10
11use hmac::{Hmac, Mac};
12use sha2::{Digest, Sha256 as Sha256Impl};
13use ssh_stamp_hal::{HashError, HashHal};
14
15pub struct EspHmac;
17
18impl HashHal for EspHmac {
19 fn hmac_sha256(
20 &mut self,
21 key: &[u8],
22 message: &[u8],
23 output: &mut [u8; 32],
24 ) -> impl Future<Output = Result<(), ssh_stamp_hal::HalError>> {
25 ready(match Hmac::<Sha256Impl>::new_from_slice(key) {
28 Ok(mut mac) => {
29 mac.update(message);
30 output.copy_from_slice(&mac.finalize().into_bytes());
31 Ok(())
32 }
33 Err(_) => Err(ssh_stamp_hal::HalError::Hash(HashError::Config)),
34 })
35 }
36
37 fn sha256(
38 &mut self,
39 message: &[u8],
40 output: &mut [u8; 32],
41 ) -> impl Future<Output = Result<(), ssh_stamp_hal::HalError>> {
42 let mut hasher = Sha256Impl::new();
43 hasher.update(message);
44 let result = hasher.finalize();
45 output.copy_from_slice(&result);
46 ready(Ok(()))
47 }
48}