Skip to main content

HmacSha256

Struct HmacSha256 

Source
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

Source

pub const TAG_LEN: usize = Sha256::DIGEST_LEN

Output tag length in bytes (32 bytes / 256 bits).

Source

pub const BLOCK_LEN: usize = Sha256::BLOCK_LEN

Underlying hash block length in bytes (64 bytes).

Source

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.

Source

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.

Source

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.

Source

pub fn try_update(&mut self, data: &[u8]) -> Result<()>

Absorbs data into the inner HMAC hash, rejecting uninitialized or finalized contexts.

Source

pub fn update(&mut self, data: &[u8])

Absorbs data into the inner HMAC hash.

Source

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.

Source

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.

Source

pub fn finalize(self) -> [u8; 32]

Finalizes the HMAC context and returns the 32-byte tag.

Source

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.

Source

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.

Source

pub fn mac(key: &[u8], data: &[u8]) -> [u8; 32]

Computes HMAC-SHA-256(key, data) in one shot.

Source

pub fn mac_chunks(key: &[u8], chunks: &[&[u8]]) -> [u8; 32]

Computes HMAC-SHA-256(key, chunks[0] || chunks[1] || ...) in one shot.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for HmacSha256

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for HmacSha256

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Zeroize for HmacSha256

Source§

fn zeroize(&mut self)

Overwrites all sensitive memory owned by self with zeros.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.