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 keyed inner / outer Sm3 states (the SM3 compression state after absorbing K' XOR ipad / K' XOR opad) hold key-derived material. They are wiped at the field layer by Sm3’s own Drop impl (v0.23): whenever an HmacSm3 — or the Sm3 values moved out of it by HmacSm3::finalize — is dropped, the compression state + input buffer are scrubbed. There is no impl Drop for HmacSm3 (and there must not be: finalize(self) moves the inner/outer fields out of self, which a Drop impl would forbid). Construction-time intermediates (K', K' XOR ipad, K' XOR opad) are zeroized inside HmacSm3::new after they are folded into the keyed states. The 32-byte tag returned by HmacSm3::finalize is the public output and is not wiped.

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.

self.inner is finalized (by value) into the inner digest and self.outer is moved out, fed the inner digest, and finalized. Both keyed Sm3 values are scrubbed by Sm3’s Drop impl (v0.23) when they go out of scope here; the returned tag is the public output.

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) -> Array<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. Read more
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 MacMarker for HmacSm3

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.

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 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: &Array<u8, <T as OutputSizeUser>::OutputSize>, ) -> Result<(), MacError>

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

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

Check if tag/code value is correct for the processed input and reset Mac instance. Read more
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.