CMAC_INIT(3)
π CMAC_INIT(3) π
Bestie, welcome to CMAC_INIT(3) from the Library Functions Manual β it's giving low-level libcrypto crypto realness, and we're about to make CMAC make sense. π
πΈ NAME πΈ
CMAC_CTX_new, CMAC_Init, CMAC_Update, CMAC_Final, CMAC_CTX_copy, CMAC_CTX_get0_cipher_ctx, CMAC_CTX_cleanup, CMAC_CTX_free β Cipher-based message authentication code. No cap, that's the whole squad. π«Ά
β¨ SYNOPSIS β¨
library βlibcryptoβ #include <openssl/cmac.h>
CMAC_CTX *
CMAC_CTX_new(void);
int
CMAC_Init(CMAC_CTX *ctx, const void *key, size_t key_len,
const EVP_CIPHER *cipher, ENGINE *engine);
int
CMAC_Update(CMAC_CTX *ctx, const void *in_data, size_t in_len);
int
CMAC_Final(CMAC_CTX *ctx, unsigned char *out_mac, size_t *out_len);
int
CMAC_CTX_copy(CMAC_CTX *out_ctx, CMAC_CTX *in_ctx);
EVP_CIPHER_CTX *
CMAC_CTX_get0_cipher_ctx(CMAC_CTX *ctx);
void
CMAC_CTX_cleanup(CMAC_CTX *ctx);
void
CMAC_CTX_free(CMAC_CTX *ctx);
π¦ DESCRIPTION π¦
Okay so CMAC is a message authentication code algorithm that can employ an arbitrary block cipher using a symmetric key. Iconic behavior, honestly. π
This manual page is about the low-level functions that implement CMAC. But bestie, listen: instead of using these functions directly, application programs normally call EVP_PKEY_new_CMAC_key(3) and then pass the resulting EVP_PKEY object to EVP_DigestSignInit(3). That's the main-character route. π
The CMAC API is object-oriented. Calculating a message authentication code requires a CMAC_CTX object. Usually, the functions CMAC_CTX_new(), CMAC_Init(), CMAC_Update(), CMAC_Final(), and CMAC_CTX_free() need to be called in this order. Periodt. π
CMAC_CTX_new()β allocates a newCMAC_CTXobject, initializes the embeddedEVP_CIPHER_CTXobject, and marks the object itself as uninitialized. Fresh out the box, but not ready to go yet. β¨CMAC_Init()β selects the given block cipher for use byctx. Functions to obtain suitableEVP_CIPHERobjects are listed in the CIPHER LISTING section of theEVP_EncryptInit(3)manual page. UnlesskeyisNULL,CMAC_Init()also initializesctxfor use with the given symmetric key that iskey_lenbytes long. In particular, it calculates and internally stores the two subkeys and initializesctxfor subsequently feeding in data withCMAC_Update(). Theengineargument is ignored; passingNULLis recommended. Lowkey just passNULL, bestie. πCMAC_Init()again β ifctxis already initialized,CMAC_Init()can be called again withkeyandcipherboth set toNULLandkey_lenset to0. In that case, any data already processed is discarded andctxis re-initialized to start reading data anew. It's a reset era. πCMAC_Update()β processesin_lenbytes of input data pointed to byin_data. Depending on the number of input bytes already cached inctx, onin_len, and on the block size, this may encrypt zero or more blocks. Unlessin_lenis zero, this function leaves at least one byte and at most one block of input cached but unprocessed inside thectxobject.CMAC_Update()can be called multiple times to concatenate several chunks of input data of varying sizes. Chunk it, bestie. π§CMAC_Final()β stores the length of the message authentication code in bytes, which equals the cipher block size, into*out_len. Unlessout_macisNULL, it encrypts the last block, padding it if required, and copies the resulting message authentication code toout_mac. The caller is responsible for providing a buffer of sufficient size. Do not skip the buffer math, fr fr. πCMAC_CTX_copy()β performs a deep copy of the already initializedin_ctxintoout_ctx. Deep copy, not shallow vibes. πCMAC_CTX_cleanup()β zeros out both subkeys and all temporary data inctxand in the embeddedEVP_CIPHER_CTXobject, frees all allocated memory associated with it, except forctxitself, and marks it as uninitialized, such that it can be reused for subsequentCMAC_Init(). Cleanup era, but the object itself survives. π§ΌCMAC_CTX_free()β callsCMAC_CTX_cleanup(), then freesctxitself. IfctxisNULL, no action occurs. Very "nothing to see here" of it. π«Ά
π₯ RETURN VALUES π₯
CMAC_CTX_new() returns the new context object or NULL in case of failure. It succeeds unless memory is exhausted. Memory said "not today" and NULL said "bet." π
CMAC_Init(), CMAC_Update(), CMAC_Final(), and 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_Update() and CMAC_Final() also fail if encrypting a block fails, and CMAC_CTX_copy() if copying the embedded EVP_CIPHER_CTX object fails, which can for example happen when memory is exhausted. So yeah, check those returns, bestie. π
CMAC_CTX_get0_cipher_ctx() returns an internal pointer to the EVP_CIPHER_CTX object that is embedded in ctx. It's an inside look, not a copy. π
π ERRORS π
The CMAC code itself does not use the <openssl/err.h> framework, so in general, the reasons for failure cannot be found out with ERR_get_error(3). That's the tea. π΅
However, since the EVP_EncryptInit(3) functions are used internally, entries may still get pushed onto the error stack in some cases of failure. So sometimes the error stack is still serving looks, even if CMAC didn't put them there. β¨
π SEE ALSO π
β STANDARDS β
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. Academic icon behavior. π
π HISTORY π
These functions first appeared in OpenSSL 1.0.1 and have been available since OpenBSD 5.3. Old enough to have lore, still relevant enough to slay. π
And that's the footer tea, bestie: this page is from Debian, dated June 8, 2025, and it's the CMAC_INIT(3) page. Mwah. ππ
π¦ where this page comes from
- alpine 3.24: libressl-doc 4.3.1-r0 Β· custom
- arch rolling: libressl 4.4.0-1 Β· ISC AND custom:OpenSSL
- Copyright (c) 2020 Ingo Schwarze <schwarze@openbsd.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms