Cipher-based message authentication
const void *key, size_t key_len,
const EVP_CIPHER *cipher, ENGINE
const void *in_data, size_t
unsigned char *out_mac, size_t
CMAC is a message authentication code algorithm that can employ an arbitrary block cipher using a symmetric key.
The present manual page describes low-level functions implementing
CMAC. Instead of using these functions directly, application programs
EVP_PKEY_CTX_new_id(3) with an argument of
EVP_PKEY_CMAC and then pass the resulting
EVP_MD_CTX object to
The CMAC API is object-oriented. Calculating a
message authentication code requires a CMAC_CTX
object. Usually, the functions
CMAC_CTX_free() need to be called in this order.
allocates a new CMAC_CTX object, initializes the
embedded EVP_CIPHER_CTX object, and marks the object
itself as uninitialized.
selects the given block cipher for use by
ctx. Functions to obtain suitable
EVP_CIPHER objects are listed in the CIPHER LISTING
section of the
EVP_Cipher(3) manual page. Unless key
initializes ctx for use with the given symmetric
key that is key_len bytes long.
In particular, it calculates and internally stores the two subkeys and
initializes ctx for subsequently feeding in data with
CMAC_Update(). To use the default cipher
implementations provided by the library, pass
as the impl argument.
If ctx is already
can be called again with key,
cipher, and impl all set to
NULL and key_len set to 0. In
that case, any data already processed is discarded and
ctx is re-initialized to start reading data anew.
processes in_len bytes of input data pointed to by
in_data. Depending on the number of input bytes
already cached in ctx, on
in_len, and on the block size, this may encrypt zero
or more blocks. Unless in_len is zero, this function
leaves at least one byte and at most one block of input cached but
unprocessed inside the ctx object.
CMAC_Update() can be called multiple times to
concatenate several chunks of input data of varying sizes.
stores the length of the message authentication code in bytes, which equals
the cipher block size, into *out_len. Unless
NULL, it encrypts
the last block, padding it if required, and copies the resulting message
authentication code to out_mac. The caller is
responsible for providing a buffer of sufficient size.
CMAC_Final() allows the user to subsequently
append additional data with
CMAC_Init() is called to start
CMAC_Update() can no longer be used
performs a deep copy of the already initialized in_ctx
zeros out both subkeys and all temporary data in ctx
and in the embedded EVP_CIPHER_CTX object, frees all
allocated memory associated with it, except for ctx
itself, and marks it as uninitialized, such that it can be reused for
CMAC_CTX_cleanup(), then frees
ctx itself. If ctx is
NULL, no action occurs.
CMAC_CTX_new() returns the new context
NULL in case of failure. It succeeds
unless memory is exhausted.
CMAC_CTX_copy() return 1 on success or 0 on failure.
CMAC_Init() fails if initializing the embedded
EVP_CIPHER_CTX object fails. The others fail if
in_ctx is uninitialized.
CMAC_Final() also fail if encrypting a block fails,
CMAC_CTX_copy() if copying the embedded
EVP_CIPHER_CTX object fails, which can for example
happen when memory is exhausted.
CMAC_CTX_get0_cipher_ctx() returns an
internal pointer to the EVP_CIPHER_CTX object that is
embedded in ctx.
The CMAC code itself does not use the
so in general, the reasons for failure cannot be found out with
ERR_get_error(3). However, since the
EVP_Cipher(3) functions are used internally, entries may
still get pushed onto the error stack in some cases of failure.
EVP_aes_128_cbc(3), EVP_Cipher(3), EVP_DigestInit(3), EVP_PKEY_CTX_new_id(3), HMAC(3)
Morris Dworkin, Recommendation for Block Cipher Modes of Operation: The CMAC Mode for Authentication, National Institute of Standards and Technology, NIST Special Publication 800-38B, https://doi.org/10.6028/NIST.SP.800-38B, Gaithersburg, Maryland, May 2005, updated October 6, 2016.
These functions first appeared in OpenSSL 1.0.1 and have been available since OpenBSD 5.3.