Skip to main content

ssh_stamp_hal/traits/
hash.rs

1// SPDX-FileCopyrightText: 2026 Roman Valls Guimera <brainstorm@nopcode.org>
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5//! Hash/HMAC operations trait.
6
7use core::future::Future;
8
9use crate::HalError;
10
11/// Hash/HMAC hardware abstraction.
12///
13/// Provides cryptographic hash functions. Implementations may use
14/// hardware accelerators or software implementations depending on
15/// platform capabilities.
16///
17/// # Example
18///
19/// ```ignore
20/// async fn compute_hash<H: HashHal>(hash: &mut H, data: &[u8]) -> Result<[u8; 32], HalError> {
21///     let mut output = [0u8; 32];
22///     hash.sha256(data, &mut output).await?;
23///     Ok(output)
24/// }
25/// ```
26pub trait HashHal {
27    /// Compute HMAC-SHA256.
28    ///
29    /// Computes Hash-based Message Authentication Code using SHA256.
30    /// Useful for message authentication and key derivation.
31    ///
32    /// # Arguments
33    ///
34    /// * `key` - Secret key for HMAC.
35    /// * `message` - Data to authenticate.
36    /// * `output` - Output buffer for 32-byte HMAC result.
37    ///
38    /// # Returns
39    ///
40    /// `Ok(())` on success with result in `output`, or an error on failure.
41    fn hmac_sha256(
42        &mut self,
43        key: &[u8],
44        message: &[u8],
45        output: &mut [u8; 32],
46    ) -> impl Future<Output = Result<(), HalError>>;
47
48    /// Compute SHA256.
49    ///
50    /// Computes the SHA256 hash of the input message.
51    ///
52    /// # Arguments
53    ///
54    /// * `message` - Data to hash.
55    /// * `output` - Output buffer for 32-byte hash result.
56    ///
57    /// # Returns
58    ///
59    /// `Ok(())` on success with result in `output`, or an error on failure.
60    fn sha256(
61        &mut self,
62        message: &[u8],
63        output: &mut [u8; 32],
64    ) -> impl Future<Output = Result<(), HalError>>;
65}