pub struct HmacSha256 { /* private fields */ }Expand description
Streaming HMAC-SHA-256 authentication context (FIPS 198-1, Section 4).
Key-derived inner and outer hash states are automatically zeroized on drop. To avoid moving
key-derived hash states across stack frames, callers in FIPS boundary code should create an
HmacSha256::uninitialized slot and initialize it in place via HmacSha256::init_into,
or use HmacSha256::mac_into / HmacSha256::mac_chunks_into.
Implementations§
Source§impl HmacSha256
impl HmacSha256
Sourcepub const TAG_LEN: usize = Sha256::DIGEST_LEN
pub const TAG_LEN: usize = Sha256::DIGEST_LEN
Output tag length in bytes (32 bytes / 256 bits).
Sourcepub const BLOCK_LEN: usize = Sha256::BLOCK_LEN
pub const BLOCK_LEN: usize = Sha256::BLOCK_LEN
Underlying hash block length in bytes (64 bytes).
Sourcepub const fn uninitialized() -> Self
pub const fn uninitialized() -> Self
Creates an uninitialized HMAC-SHA-256 slot that rejects updates and finalization until
initialized in place via Self::init_into.
Sourcepub fn init_into(&mut self, key: &[u8])
pub fn init_into(&mut self, key: &[u8])
Initializes self in place with key, avoiding by-value stack moves of key-derived
ipad/opad hash states.
Keys longer than 64 bytes are hashed directly into a zeroized stack buffer per FIPS 198-1
Table 1 Step 2; shorter keys (including 0-byte keys for non-approved low-level testing)
are zero-padded to 64 bytes. Approved-mode minimum key length policies (>= 112 bits) are
enforced by the FIPS module boundary layer.
Sourcepub fn new(key: &[u8]) -> Self
pub fn new(key: &[u8]) -> Self
Initializes a new HMAC-SHA-256 context with key.
For FIPS boundary code handling secret keys, prefer Self::uninitialized +
Self::init_into or Self::mac_into to initialize states in their resting place.
Sourcepub fn try_update(&mut self, data: &[u8]) -> Result<()>
pub fn try_update(&mut self, data: &[u8]) -> Result<()>
Absorbs data into the inner HMAC hash, rejecting uninitialized or finalized contexts.
Sourcepub fn try_finalize_into(&mut self, out: &mut [u8; 32]) -> Result<()>
pub fn try_finalize_into(&mut self, out: &mut [u8; 32]) -> Result<()>
Finalizes the authentication tag directly into out, zeroizes internal states, and marks
the context finalized until Self::init_into is called again.
Sourcepub fn finalize_into(&mut self, out: &mut [u8; 32])
pub fn finalize_into(&mut self, out: &mut [u8; 32])
Finalizes the authentication tag directly into out and zeroizes internal states.
If uninitialized or already finalized, zeroes out.
Sourcepub fn mac_into(key: &[u8], data: &[u8], out: &mut [u8; 32])
pub fn mac_into(key: &[u8], data: &[u8], out: &mut [u8; 32])
Computes HMAC-SHA-256(key, data) directly into out without moving key states or tag.
Sourcepub fn mac_chunks_into(key: &[u8], chunks: &[&[u8]], out: &mut [u8; 32])
pub fn mac_chunks_into(key: &[u8], chunks: &[&[u8]], out: &mut [u8; 32])
Computes HMAC-SHA-256(key, chunks[0] || chunks[1] || ...) directly into out.
Sourcepub fn mac_chunks(key: &[u8], chunks: &[&[u8]]) -> [u8; 32]
pub fn mac_chunks(key: &[u8], chunks: &[&[u8]]) -> [u8; 32]
Computes HMAC-SHA-256(key, chunks[0] || chunks[1] || ...) in one shot.
Sourcepub fn verify(key: &[u8], data: &[u8], expected_tag: &[u8; 32]) -> Choice
pub fn verify(key: &[u8], data: &[u8], expected_tag: &[u8; 32]) -> Choice
Computes HMAC-SHA-256(key, data) directly into zeroized storage and compares against
expected_tag in constant time.
Sourcepub fn self_test() -> Result<()>
pub fn self_test() -> Result<()>
Executes the FIPS 140-3 known-answer self-test (CAST) for HMAC-SHA-256.
Covers both a 256-bit (32-byte) approved-strength key (meeting FIPS 140-3’s minimum approved key-length requirement for pre-integrity HMAC CAST) and a 131-byte key exceeding the 64-byte block size (RFC 4231 Test Case 6) in constant time.
Per FIPS 140-3 IG 10.2.A, this CAST MUST succeed before running any software integrity
check that relies on HmacSha256.