pub struct MacCtx { /* private fields */ }Expand description
Stateful MAC context (EVP_MAC_CTX*).
!Clone — use fork() to duplicate mid-stream state.
All stateful operations require &mut self.
Implementations§
Source§impl MacCtx
impl MacCtx
Sourcepub fn new(alg: &MacAlg) -> Result<Self, ErrorStack>
pub fn new(alg: &MacAlg) -> Result<Self, ErrorStack>
Create a new (uninitialised) MAC context from an algorithm descriptor.
Call MacCtx::init with a key before feeding data.
§Errors
Sourcepub fn init(
&mut self,
key: &[u8],
params: Option<&Params<'_>>,
) -> Result<(), ErrorStack>
pub fn init( &mut self, key: &[u8], params: Option<&Params<'_>>, ) -> Result<(), ErrorStack>
Initialise (or re-initialise) with a key and optional parameters.
key is always copied into the MAC context by EVP_MAC_init.
HMAC additionally pads the key into IPAD/OPAD. The copy is unavoidable.
§Errors
Sourcepub fn update(&mut self, data: &[u8]) -> Result<(), ErrorStack>
pub fn update(&mut self, data: &[u8]) -> Result<(), ErrorStack>
Feed data into the ongoing MAC computation.
§Errors
Sourcepub fn finish(&mut self, out: &mut [u8]) -> Result<usize, ErrorStack>
pub fn finish(&mut self, out: &mut [u8]) -> Result<usize, ErrorStack>
Finalise the MAC and write into out.
out must be at least self.mac_size() bytes.
Returns the number of bytes written.
§Errors
Sourcepub fn finish_xof(&mut self, out: &mut [u8]) -> Result<(), ErrorStack>
pub fn finish_xof(&mut self, out: &mut [u8]) -> Result<(), ErrorStack>
Finalise with variable-length XOF output (KMAC-128, KMAC-256).
§Errors
Sourcepub fn block_size(&self) -> usize
pub fn block_size(&self) -> usize
Block size of the underlying MAC algorithm in bytes.
For HMAC, this is the block size of the underlying hash (e.g. 64 for SHA-256, 128 for SHA-384/SHA-512). Required by protocols that derive IPAD/OPAD or align buffers to the hash block size.
Returns 0 before MacCtx::init is called.
Sourcepub fn alg(&self) -> Option<MacAlg>
pub fn alg(&self) -> Option<MacAlg>
Return the algorithm descriptor associated with this context.
Bumps the algorithm’s reference count so the returned MacAlg is
independently owned and may outlive the context.
Returns None if no algorithm is associated (e.g. context was just
allocated but not yet bound to an algorithm).