File
Blob: firmware/vendor/str0m-rust-crypto/src/sha1.rs
| 1 | //! SHA1-HMAC implementation using RustCrypto. |
| 2 | |
| 3 | use hmac::{Hmac, Mac}; |
| 4 | use sha1::Sha1; |
| 5 | |
| 6 | use str0m_proto::crypto::Sha1HmacProvider; |
| 7 | |
| 8 | type HmacSha1 = Hmac<Sha1>; |
| 9 | |
| 10 | // ============================================================================ |
| 11 | // SHA1 HMAC Provider Implementation |
| 12 | // ============================================================================ |
| 13 | |
| 14 | pub(super) struct RustCryptoSha1HmacProvider; |
| 15 | |
| 16 | impl std::fmt::Debug for RustCryptoSha1HmacProvider { |
| 17 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 18 | f.debug_struct("RustCryptoSha1HmacProvider").finish() |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | impl Sha1HmacProvider for RustCryptoSha1HmacProvider { |
| 23 | fn sha1_hmac(&self, key: &[u8], payloads: &[&[u8]]) -> [u8; 20] { |
| 24 | let mut mac = HmacSha1::new_from_slice(key).expect("HMAC can take key of any size"); |
| 25 | |
| 26 | for payload in payloads { |
| 27 | mac.update(payload); |
| 28 | } |
| 29 | |
| 30 | let result = mac.finalize(); |
| 31 | let bytes = result.into_bytes(); |
| 32 | let mut output = [0u8; 20]; |
| 33 | output.copy_from_slice(&bytes); |
| 34 | output |
| 35 | } |
| 36 | } |