Skip to main content

ssh_stamp_esp32/
hash.rs

1// SPDX-FileCopyrightText: 2026 Roman Valls Guimera <brainstorm@nopcode.org>
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5//! HMAC-SHA256 implementation for ESP32 family
6//!
7//! Uses ESP32's hardware-accelerated HMAC peripheral.
8
9use core::future::{Future, ready};
10
11use hmac::{Hmac, Mac};
12use sha2::{Digest, Sha256 as Sha256Impl};
13use ssh_stamp_hal::{HashError, HashHal};
14
15/// ESP32 HMAC implementation  
16pub 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        // Use software HMAC implementation for now
26        // ESP32 hardware HMAC requires special key handling
27        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}