Skip to main content

Mac

Struct Mac 

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

Stateful MAC context.

See the module-level documentation for the cycle of life and the buffer ownership model.

Implementations§

Source§

impl Mac

Source

pub fn new(algo: Algorithm) -> Self

Create a new MAC context for the given algorithm. The actual key (and nonce / customization string for GMAC / KMAC) is loaded later by Self::init, Self::init_kmac or Self::init_with_nonce.

Source

pub fn init(&mut self, key: &[u8]) -> Result<(), Error>

Initialize the MAC with a key. This is the right entry point for HMAC, CMAC and KMAC (with an empty customization string). For GMAC, use Self::init_with_nonce instead.

HMAC accepts arbitrary key lengths per RFC 2104 (the key is hashed if it exceeds the hash block size and zero-padded otherwise). CMAC requires the exact key length for the chosen cipher. KMAC accepts arbitrary key lengths.

§Errors
Source

pub fn init_kmac(&mut self, key: &[u8], custom: &[u8]) -> Result<(), Error>

Initialize KMAC with a key and a customization string.

§Errors

Error::WrongInitVariant if called on a non-KMAC algorithm.

Source

pub fn init_with_nonce(&mut self, key: &[u8], nonce: &[u8]) -> Result<(), Error>

Initialize GMAC with a key and a 12-byte nonce. The nonce must be unique per message under a given key (reusing it breaks GMAC catastrophically).

§Errors
Source

pub const fn tag_len(&self) -> usize

Tag length in bytes for this MAC algorithm.

Source

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

Feed data into the MAC. May be called any number of times between init and sign / verify. Empty slices are allowed.

Source

pub fn sign(&mut self, out: &mut [u8]) -> Result<usize, Error>

Finalize the MAC and write the tag into out. Returns the number of bytes written, which is always Self::tag_len. After this call the context is in the finalized state and must be re-initialized before further use.

§Errors
Source

pub fn verify(&mut self, expected_tag: &[u8]) -> Result<(), Error>

Finalize and verify the tag against expected_tag in constant time. Supports tag truncation: expected_tag.len() may be less than or equal to tag_len(). Comparison is XOR-accumulate-then-test, with no early exit.

§Errors
Source

pub fn sign_to_vec(&mut self) -> Result<Vec<u8>, Error>

Allocating helper around Self::sign.

Auto Trait Implementations§

§

impl Freeze for Mac

§

impl RefUnwindSafe for Mac

§

impl Send for Mac

§

impl Sync for Mac

§

impl Unpin for Mac

§

impl UnsafeUnpin for Mac

§

impl UnwindSafe for Mac

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 = 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.