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
impl Mac
Sourcepub fn new(algo: Algorithm) -> Self
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.
Sourcepub fn init(&mut self, key: &[u8]) -> Result<(), Error>
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
Error::InvalidKeyLenfor CMAC if the length is wrong.Error::WrongInitVariantif called on GMAC.
Sourcepub fn init_kmac(&mut self, key: &[u8], custom: &[u8]) -> Result<(), Error>
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.
Sourcepub fn init_with_nonce(&mut self, key: &[u8], nonce: &[u8]) -> Result<(), Error>
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
Error::WrongInitVariantif called on a non-GMAC algorithm.Error::InvalidKeyLenif the key length does not match.Error::InvalidNonceLenif the nonce is not 12 bytes.
Sourcepub fn update(&mut self, data: &[u8]) -> Result<(), Error>
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.
Sourcepub fn sign(&mut self, out: &mut [u8]) -> Result<usize, Error>
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
Error::NotInitialized/Error::AlreadyFinalized.Error::OutputBufferTooSmallifout.len() < tag_len().
Sourcepub fn verify(&mut self, expected_tag: &[u8]) -> Result<(), Error>
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
Error::NotInitialized/Error::AlreadyFinalized.Error::VerificationFailedif the tag does not match (orexpected_tag.len() == 0or> tag_len()).
Sourcepub fn sign_to_vec(&mut self) -> Result<Vec<u8>, Error>
pub fn sign_to_vec(&mut self) -> Result<Vec<u8>, Error>
Allocating helper around Self::sign.