Skip to main content

HmacSm3

Struct HmacSm3 

Source
pub struct HmacSm3 { /* private fields */ }
Expand description

Streaming HMAC-SM3 (v0.3 W5).

Construct with new(&key), feed message chunks via update, finalize with finalize (32-byte tag) or verify (constant- time compare against an expected tag).

Equivalent to hmac_sm3 for the same (key, message) byte sequence — chunking does not affect the output.

§Zeroization

The pre-computed outer keyed-state (SM3 after absorbing K' XOR opad) holds key-derived material. HmacSm3::finalize and HmacSm3::verify consume self and zeroize it before returning. If the caller drops the HmacSm3 without calling either method, the Drop impl wipes the state.

Implementations§

Source§

impl HmacSm3

Source

pub fn new(key: &[u8]) -> Self

Construct a new keyed HMAC-SM3 instance.

key may be any length; the standard RFC 2104 hash-first reduction applies for key.len() > 64. Both intermediate K' / K' XOR ipad / K' XOR opad buffers are zeroized after the inner/outer SM3 instances absorb them.

Source

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

Absorb message bytes into the inner hash.

Source

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

Consume the instance and produce the 32-byte MAC tag.

The outer keyed-state and the inner final state are both dropped after consuming self; Sm3’s Drop impl is the one we rely on here. To be defensive against a future change where Sm3 is no longer ZeroizeOnDrop, both fields are explicitly wiped via clone-then-drop would be safer — but Sm3 does not currently implement Zeroize directly. The state is consumed by outer.finalize() which produces the public output and discards the rest.

Source

pub fn verify(self, expected: &[u8; 32]) -> bool

Constant-time verify a candidate tag against the finalized HMAC. Returns true on match.

Trait Implementations§

Source§

impl FixedOutput for HmacSm3

Source§

fn finalize_into(self, out: &mut Output<Self>)

Consume value and write result into provided array.
Source§

fn finalize_fixed(self) -> GenericArray<u8, Self::OutputSize>

Retrieve result and consume the hasher instance.
Source§

impl KeyInit for HmacSm3

Source§

fn new(key: &Key<Self>) -> Self

Create new value from fixed size key.
Source§

fn new_from_slice(key: &[u8]) -> Result<Self, InvalidLength>

Create new value from variable size key.
Source§

impl KeySizeUser for HmacSm3

Source§

type KeySize = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>

Key size in bytes.
Source§

fn key_size() -> usize

Return key size in bytes.
Source§

impl Mac for HmacSm3

Source§

type Output = [u8; 32]

The fixed-size MAC tag output.
Source§

fn new(key: &[u8]) -> Self

Construct a fresh MAC keyed with key.
Source§

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

Absorb message bytes.
Source§

fn finalize(self) -> Self::Output

Consume the MAC instance and produce the final tag.
Source§

fn verify(self, expected: &Self::Output) -> bool

Verify a candidate tag against the computed one in constant-time. Returns true on match. Implementations MUST use a constant-time comparison primitive (e.g. subtle::ConstantTimeEq).
Source§

impl OutputSizeUser for HmacSm3

Source§

type OutputSize = UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>

Size of the output in bytes.
Source§

fn output_size() -> usize

Return output size in bytes.
Source§

impl Reset for HmacSm3

Source§

fn reset(&mut self)

Reset state to its initial value.
Source§

impl Update for HmacSm3

Source§

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

Update state using the provided data.
Source§

fn chain(self, data: impl AsRef<[u8]>) -> Self
where Self: Sized,

Digest input data in a chained manner.
Source§

impl MacMarker for HmacSm3

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> Mac for T

Source§

fn new(key: &GenericArray<u8, <T as KeySizeUser>::KeySize>) -> T
where T: KeyInit,

Create new value from fixed size key.
Source§

fn new_from_slice(key: &[u8]) -> Result<T, InvalidLength>
where T: KeyInit,

Create new value from variable size key.
Source§

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

Update state using the provided data.
Source§

fn chain_update(self, data: impl AsRef<[u8]>) -> T

Process input data in a chained manner.
Source§

fn finalize(self) -> CtOutput<T>

Obtain the result of a Mac computation as a CtOutput and consume Mac instance.
Source§

fn finalize_reset(&mut self) -> CtOutput<T>

Obtain the result of a Mac computation as a CtOutput and reset Mac instance.
Source§

fn reset(&mut self)
where T: Reset,

Reset MAC instance to its initial state.
Source§

fn verify( self, tag: &GenericArray<u8, <T as OutputSizeUser>::OutputSize>, ) -> Result<(), MacError>

Check if tag/code value is correct for the processed input.
Source§

fn verify_reset( &mut self, tag: &GenericArray<u8, <T as OutputSizeUser>::OutputSize>, ) -> Result<(), MacError>

Check if tag/code value is correct for the processed input and reset Mac instance.
Source§

fn verify_slice(self, tag: &[u8]) -> Result<(), MacError>

Check truncated tag correctness using all bytes of calculated tag. Read more
Source§

fn verify_slice_reset(&mut self, tag: &[u8]) -> Result<(), MacError>

Check truncated tag correctness using all bytes of calculated tag and reset Mac instance. Read more
Source§

fn verify_truncated_left(self, tag: &[u8]) -> Result<(), MacError>

Check truncated tag correctness using left side bytes (i.e. tag[..n]) of calculated tag. Read more
Source§

fn verify_truncated_right(self, tag: &[u8]) -> Result<(), MacError>

Check truncated tag correctness using right side bytes (i.e. tag[n..]) of calculated tag. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.