chore(core): expose SHA-256/512 hazard guards
What changed, and why it matters
This commit is a routine code cleanup with no security relevance. It simply renames an internal Rust type alias to make the SHA-256 and SHA-512 'hazard guard' wrappers easier to use elsewhere, and adds extra unit tests. There is no bug fix, no vulnerability patch, and no change to cryptographic behavior.
No action required. This is a non-security refactoring and test expansion.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change exposes Sha256Guard<'a> and Sha512Guard<'a> as public type aliases for HazardGuard<'a, ffi::SHA256_CTX> and HazardGuard<'a, ffi::SHA512_CTX>, and updates the impl blocks to use those aliases. It also adds standard NIST-style test vectors and tests for incremental update and guard reuse. The underlying C crypto code is untouched.
Changed components
core/embed/crypto/src/sha256.rscore/embed/crypto/src/sha512.rsInspect captured patch +78 / −2
### core/embed/crypto/src/sha256.rs
@@ -10,6 +10,7 @@ pub const DIGEST_SIZE: usize = ffi::SHA256_DIGEST_LENGTH as usize;
pub type Digest = [u8; DIGEST_SIZE];
pub type Sha256Ctx = SecretContext<ffi::SHA256_CTX>;
+pub type Sha256Guard<'a> = HazardGuard<'a, ffi::SHA256_CTX>;
// SAFETY: SHA256_CTX is valid when zeroed
unsafe impl ZeroableMemory for ffi::SHA256_CTX {}
@@ -25,7 +26,7 @@ impl ffi::SHA256_CTX {
}
}
-impl HazardGuard<'_, ffi::SHA256_CTX> {
+impl Sha256Guard<'_> {
/// Update the SHA256 context with the given data.
pub fn update(&mut self, data: &[u8]) {
let ptr = CSlice::from(data);
@@ -98,13 +99,22 @@ impl Sha256<&'_ mut Sha256Ctx> {
mod test {
use super::*;
+ // vectors from http://www.di-mgt.com.au/sha_testvectors.html
const SHA256_EMPTY: &str = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
const SHA256_VECTORS: &[(&[u8], &str)] = &[
(b"", SHA256_EMPTY),
(
b"abc",
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
),
+ (
+ b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
+ "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",
+ ),
+ (
+ b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
+ "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1",
+ ),
];
fn hexdigest(data: &[u8]) -> String {
@@ -128,4 +138,36 @@ mod test {
assert_eq!(out_hex, *expected);
}
}
+
+ #[test]
+ fn test_update() {
+ for (data, expected) in SHA256_VECTORS {
+ let mut ctx = Sha256Ctx::default();
+ let mut sha = Sha256::new(&mut ctx);
+ sha.update(data);
+ assert_eq!(hex::encode(sha.finalize()), *expected);
+ }
+
+ let mut ctx = Sha256Ctx::default();
+ let mut sha = Sha256::new(&mut ctx);
+ for _ in 0..1_000_000 {
+ sha.update(b"a");
+ }
+ assert_eq!(
+ hex::encode(sha.finalize()),
+ "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"
+ );
+ }
+
+ #[test]
+ fn test_guard_reuse() {
+ // reinitializing the context after `finalize` allows reusing it
+ let mut ctx = Sha256Ctx::default();
+ for _ in 0..2 {
+ ctx.hazard_mut().init();
+ let mut guard = Sha256Guard::hazard_new(&mut ctx);
+ guard.update(b"abc");
+ assert_eq!(hex::encode(guard.finalize()), hexdigest(b"abc"));
+ }
+ }
}
### core/embed/crypto/src/sha512.rs
@@ -10,6 +10,7 @@ pub const DIGEST_SIZE: usize = ffi::SHA512_DIGEST_LENGTH as usize;
pub type Digest = [u8; DIGEST_SIZE];
pub type Sha512Ctx = SecretContext<ffi::SHA512_CTX>;
+pub type Sha512Guard<'a> = HazardGuard<'a, ffi::SHA512_CTX>;
// SAFETY: SHA512_CTX is valid when zeroed
unsafe impl ZeroableMemory for ffi::SHA512_CTX {}
@@ -25,7 +26,7 @@ impl ffi::SHA512_CTX {
}
}
-impl HazardGuard<'_, ffi::SHA512_CTX> {
+impl Sha512Guard<'_> {
/// Update the SHA512 context with the given data.
pub fn update(&mut self, data: &[u8]) {
let data_slice = CSlice::from(data);
@@ -89,6 +90,7 @@ impl Sha512<&'_ mut Sha512Ctx> {
mod test {
use super::*;
+ // vectors from http://www.di-mgt.com.au/sha_testvectors.html
const SHA512_EMPTY: &str = "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e";
const SHA512_VECTORS: &[(&[u8], &str)] = &[
(b"", SHA512_EMPTY),
@@ -127,4 +129,36 @@ mod test {
assert_eq!(out_hex, *expected);
}
}
+
+ #[test]
+ fn test_update() {
+ for (data, expected) in SHA512_VECTORS {
+ let mut ctx = Sha512Ctx::default();
+ let mut sha = Sha512::new(&mut ctx);
+ sha.update(data);
+ assert_eq!(hex::encode(sha.finalize()), *expected);
+ }
+
+ let mut ctx = Sha512Ctx::default();
+ let mut sha = Sha512::new(&mut ctx);
+ for _ in 0..1_000_000 {
+ sha.update(b"a");
+ }
+ assert_eq!(
+ hex::encode(sha.finalize()),
+ "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"
+ );
+ }
+
+ #[test]
+ fn test_guard_reuse() {
+ // reinitializing the context after `finalize` allows reusing it
+ let mut ctx = Sha512Ctx::default();
+ for _ in 0..2 {
+ ctx.hazard_mut().init();
+ let mut guard = Sha512Guard::hazard_new(&mut ctx);
+ guard.update(b"abc");
+ assert_eq!(hex::encode(guard.finalize()), hexdigest(b"abc"));
+ }
+ }
}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.