feat(core): add HMAC-SHA512, expose HMAC guards
What changed, and why it matters
This commit adds a new HMAC-SHA512 cryptographic primitive to the Trezor firmware's Rust crypto wrapper and makes the existing HMAC-SHA256 guard methods public. It is a feature addition with no visible security bug or fix. The code follows the same safe patterns already used for HMAC-SHA256, including memory-zeroing guards and test vectors from RFC 4231.
No security action required. Treat as routine feature commit. If reviewing for downstream use, verify that callers of the newly public guard methods maintain the existing SecretContext/HazardGuard safety invariants.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change extends core/embed/crypto to bind HMAC_SHA512_CTX and the hmac_sha512_Init/Update/Final functions from the C crypto library into Rust. It introduces HmacSha512Ctx, HmacSha512Guard, Digest512, and mirrors the HmacSha256 implementation. It also renames DIGEST_SIZE/Digest to DIGEST_SIZE_256/Digest256 and changes init/update/finalize on HmacSha256Guard from private to pub. No vulnerability is present in the diff; the additions are cryptographic plumbing and unit tests.
Changed components
core/embed/crypto/build.rscore/embed/crypto/src/hmac.rsInspect captured patch +162 / −9
### core/embed/crypto/build.rs
@@ -190,6 +190,11 @@ fn add_crypto_base(lib: &mut CLibrary, common_attrs: &CompileAttrs) -> Result<()
.allowlist_function("hmac_sha256_Init")
.allowlist_function("hmac_sha256_Update")
.allowlist_function("hmac_sha256_Final")
+ .allowlist_type("HMAC_SHA512_CTX")
+ .no_copy("HMAC_SHA512_CTX")
+ .allowlist_function("hmac_sha512_Init")
+ .allowlist_function("hmac_sha512_Update")
+ .allowlist_function("hmac_sha512_Final")
// sha256
.allowlist_var("SHA256_BLOCK_LENGTH")
.allowlist_var("SHA256_DIGEST_LENGTH")
### core/embed/crypto/src/hmac.rs
@@ -5,36 +5,40 @@ use rtl::CSlice;
use super::ffi;
use super::secret::{HazardGuard, SecretContext, SecretContextLock, ZeroableMemory};
-pub const DIGEST_SIZE: usize = ffi::SHA256_DIGEST_LENGTH as usize;
-pub type Digest = [u8; DIGEST_SIZE];
+pub const DIGEST_SIZE_256: usize = ffi::SHA256_DIGEST_LENGTH as usize;
+pub type Digest256 = [u8; DIGEST_SIZE_256];
+
+pub const DIGEST_SIZE_512: usize = ffi::SHA512_DIGEST_LENGTH as usize;
+pub type Digest512 = [u8; DIGEST_SIZE_512];
pub type HmacSha256Ctx = SecretContext<ffi::HMAC_SHA256_CTX>;
+pub type HmacSha256Guard<'a> = HazardGuard<'a, ffi::HMAC_SHA256_CTX>;
// SAFETY: HMAC_SHA256_CTX is valid when zeroed
unsafe impl ZeroableMemory for ffi::HMAC_SHA256_CTX {}
-impl HazardGuard<'_, ffi::HMAC_SHA256_CTX> {
+impl HmacSha256Guard<'_> {
/// Initialize the HMAC context with the given key.
///
/// Called by [`HmacSha256::new`].
- fn init(&mut self, key: &[u8]) {
+ pub fn init(&mut self, key: &[u8]) {
let ptr = CSlice::from(key);
// SAFETY: ffi
// COPY HAZARD: operates on the guarded context in place
unsafe { ffi::hmac_sha256_Init(self.hazard_mut(), ptr.ptr(), ptr.len() as u32) };
}
/// Update the HMAC context with the given data.
- fn update(&mut self, data: &[u8]) {
+ pub fn update(&mut self, data: &[u8]) {
let ptr = CSlice::from(data);
// SAFETY: ffi
// COPY HAZARD: operates on the guarded context in place
unsafe { ffi::hmac_sha256_Update(self.hazard_mut(), ptr.ptr(), ptr.len() as u32) };
}
/// Finalize the HMAC context and return the digest.
- fn finalize(&mut self) -> Digest {
- let mut digest = [0u8; DIGEST_SIZE];
+ pub fn finalize(&mut self) -> Digest256 {
+ let mut digest = [0u8; DIGEST_SIZE_256];
// SAFETY: ffi
// COPY HAZARD: operates on the guarded context in place
unsafe { ffi::hmac_sha256_Final(self.hazard_mut(), digest.as_mut_ptr()) };
@@ -62,21 +66,54 @@ impl<D: DerefMut<Target = HmacSha256Ctx>> HmacSha256<D> {
}
/// Finalize the HMAC context and return the digest.
- pub fn finalize(mut self) -> Digest {
+ pub fn finalize(mut self) -> Digest256 {
self.0.guarded().finalize()
}
}
impl HmacSha256<&'_ mut HmacSha256Ctx> {
/// Calculate the HMAC-SHA256 digest of `data` under `key`.
- pub fn digest(key: &[u8], data: &[u8]) -> Digest {
+ pub fn digest(key: &[u8], data: &[u8]) -> Digest256 {
let mut ctx = HmacSha256Ctx::default();
let mut hmac = HmacSha256::new(&mut ctx, key);
hmac.update(data);
hmac.finalize()
}
}
+pub type HmacSha512Ctx = SecretContext<ffi::HMAC_SHA512_CTX>;
+pub type HmacSha512Guard<'a> = HazardGuard<'a, ffi::HMAC_SHA512_CTX>;
+
+// SAFETY: HMAC_SHA512_CTX is valid when zeroed
+unsafe impl ZeroableMemory for ffi::HMAC_SHA512_CTX {}
+
+impl HmacSha512Guard<'_> {
+ /// Initialize the HMAC context with the given key.
+ pub fn init(&mut self, key: &[u8]) {
+ let ptr = CSlice::from(key);
+ // SAFETY: ffi
+ // COPY HAZARD: operates on the guarded context in place
+ unsafe { ffi::hmac_sha512_Init(self.hazard_mut(), ptr.ptr(), ptr.len() as u32) };
+ }
+
+ /// Update the HMAC context with the given data.
+ pub fn update(&mut self, data: &[u8]) {
+ let ptr = CSlice::from(data);
+ // SAFETY: ffi
+ // COPY HAZARD: operates on the guarded context in place
+ unsafe { ffi::hmac_sha512_Update(self.hazard_mut(), ptr.ptr(), ptr.len() as u32) };
+ }
+
+ /// Finalize the HMAC context and return the digest.
+ pub fn finalize(&mut self) -> Digest512 {
+ let mut digest = [0u8; DIGEST_SIZE_512];
+ // SAFETY: ffi
+ // COPY HAZARD: operates on the guarded context in place
+ unsafe { ffi::hmac_sha512_Final(self.hazard_mut(), digest.as_mut_ptr()) };
+ digest
+ }
+}
+
#[cfg(test)]
mod test {
use super::*;
@@ -175,4 +212,115 @@ mod test {
"82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b"
);
}
+
+ #[test]
+ fn test_guard_reuse() {
+ let (key, data) = (b"Jefe", b"what do ya want for nothing?");
+ let mut ctx = HmacSha256Ctx::default();
+ for _ in 0..2 {
+ let mut guard = HmacSha256Guard::hazard_new(&mut ctx);
+ guard.init(key);
+ guard.update(data);
+ assert_eq!(hex::encode(guard.finalize()), hexdigest(key, data));
+ }
+ }
+
+ const HMAC_SHA512_EMPTY: &str = "b936cee86c9f87aa5d3c6f2e84cb5a4239a5fe50480a6ec66b70ab5b1f4ac6730c6c515421b327ec1d69402e53dfb49ad7381eb067b338fd7b0cb22247225d47";
+ // RFC 4231
+ const HMAC_SHA512_VECTORS: &[(&[u8], &[u8], &str)] = &[
+ (
+ &[0x0b; 20],
+ b"Hi There",
+ "87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854",
+ ),
+ (
+ b"Jefe",
+ b"what do ya want for nothing?",
+ "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737",
+ ),
+ (
+ &[0xaa; 20],
+ &[0xdd; 50],
+ "fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb",
+ ),
+ (
+ &[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19],
+ &[0xcd; 50],
+ "b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3dba91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dd",
+ ),
+ // skipping case with truncation
+ (
+ &[0xaa; 131],
+ b"Test Using Larger Than Block-Size Key - Hash Key First",
+ "80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f3526b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598",
+ ),
+ (
+ &[0xaa; 131],
+ b"This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm.",
+ "e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58",
+ ),
+ (
+ b"",
+ b"",
+ HMAC_SHA512_EMPTY,
+ ),
+ ];
+
+ fn hmac_512(key: &[u8], data: &[u8]) -> Digest512 {
+ let mut ctx = HmacSha512Ctx::default();
+ let mut guard = HmacSha512Guard::hazard_new(&mut ctx);
+ guard.init(key);
+ guard.update(data);
+ guard.finalize()
+ }
+
+ #[test]
+ fn test_empty_ctx_512() {
+ assert_eq!(hex::encode(hmac_512(b"", b"")), HMAC_SHA512_EMPTY);
+ }
+
+ #[test]
+ fn test_vectors_512() {
+ for (key, data, expected) in HMAC_SHA512_VECTORS {
+ assert_eq!(hex::encode(hmac_512(key, data)), *expected);
+ }
+ }
+
+ #[test]
+ fn test_update_512() {
+ // RFC 4231 case 3, fed byte by byte
+ let key = [0xaa; 20];
+ let mut ctx = HmacSha512Ctx::default();
+ let mut guard = HmacSha512Guard::hazard_new(&mut ctx);
+ guard.init(&key);
+ for _ in 0..50 {
+ guard.update(&[0xdd]);
+ }
+ assert_eq!(hex::encode(guard.finalize()), HMAC_SHA512_VECTORS[2].2);
+
+ // RFC 4231 case 4
+ let key = HMAC_SHA512_VECTORS[3].0;
+ let mut ctx = HmacSha512Ctx::default();
+ let mut guard = HmacSha512Guard::hazard_new(&mut ctx);
+ guard.init(key);
+ for _ in 0..50 {
+ guard.update(&[0xcd]);
+ }
+ assert_eq!(hex::encode(guard.finalize()), HMAC_SHA512_VECTORS[3].2);
+ }
+
+ #[test]
+ fn test_guard_reuse_512() {
+ let (key, data) = (b"Jefe", b"what do ya want for nothing?");
+ let mut ctx = HmacSha512Ctx::default();
+ for _ in 0..2 {
+ let mut guard = HmacSha512Guard::hazard_new(&mut ctx);
+ guard.init(key);
+ guard.update(data);
+ assert_eq!(
+ hex::encode(guard.finalize()),
+ hex::encode(hmac_512(key, data))
+ );
+ }
+ }
}Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.