---
name: "libressl-CMAC_Init"
section: "3"
platform: "linux"
package: "libressl"
version: "4.4.0-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/libressl-CMAC_Init"
---

# libressl-CMAC\_Init(3)

Cipher-based message authentication code

## 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

## SYNOPSIS

Crypto Library (libcrypto, -lcrypto)

**#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

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 normally call EVP\_PKEY\_new\_CMAC\_key(3) and then pass the resulting *EVP\_PKEY* object to EVP\_DigestSignInit(3).

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.

**CMAC\_CTX\_new**() allocates a new *CMAC\_CTX* object, initializes the embedded *EVP\_CIPHER\_CTX* object, and marks the object itself as uninitialized.

**CMAC\_Init**() 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\_EncryptInit(3) manual page. Unless *key* is NULL, **CMAC\_Init**() also 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**(). The *engine* argument is ignored; passing NULL is recommended.

If *ctx* is already initialized, **CMAC\_Init**() can be called again with *key* and *cipher* both 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.

**CMAC\_Update**() 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.

**CMAC\_Final**() stores the length of the message authentication code in bytes, which equals the cipher block size, into \**out\_len*. Unless *out\_mac* is 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\_CTX\_copy**() performs a deep copy of the already initialized *in\_ctx* into *out\_ctx*.

**CMAC\_CTX\_cleanup**() 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 subsequent **CMAC\_Init**().

**CMAC\_CTX\_free**() calls **CMAC\_CTX\_cleanup**(), then frees *ctx* itself. If *ctx* is NULL, no action occurs.

## RETURN VALUES

**CMAC\_CTX\_new**() returns the new context object or NULL in case of failure. It succeeds unless memory is exhausted.

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

**CMAC\_CTX\_get0\_cipher\_ctx**() returns an internal pointer to the *EVP\_CIPHER\_CTX* object that is embedded in *ctx*.

## 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). However, since the EVP\_EncryptInit(3) functions are used internally, entries may still get pushed onto the error stack in some cases of failure.

## SEE ALSO

EVP\_aes\_128\_cbc(3), EVP\_DigestSignInit(3), EVP\_EncryptInit(3), EVP\_PKEY\_new\_CMAC\_key(3), HMAC(3)

## STANDARDS

Morris Dworkin, *Recommendation for Block Cipher Modes of Operation: The CMAC Mode for Authentication*, NIST Special Publication 800-38B, National Institute of Standards and Technology, Gaithersburg, Maryland, May 2005, updated October 6, 2016, https://doi.org/10.6028/NIST.SP.800-38B.

## HISTORY

These functions first appeared in OpenSSL 1.0.1 and have been available since OpenBSD 5.3.
