EVP_MAC(3ossl)
π¦ EVP_MAC β the OpenSSL MAC routine handbook (3ossl)
ok so this is the EVP_MAC(3ossl) page from OpenSSL, and it's a whole vibe β it's the manual for the EVP MAC routines, aka everything you need to make message authentication codes behave π
This page is long, so bestie, buckle up.
πΈ NAME
One page, a whole squad of functions. Here's everyone who lives here:
- The MAC object crew:
EVP_MAC,EVP_MAC_fetch,EVP_MAC_up_ref,EVP_MAC_free,EVP_MAC_is_a,EVP_MAC_get0_name,EVP_MAC_names_do_all,EVP_MAC_get0_description,EVP_MAC_get0_provider,EVP_MAC_get_params,EVP_MAC_gettable_params,EVP_MAC_do_all_provided - The context crew:
EVP_MAC_CTX,EVP_MAC_CTX_new,EVP_MAC_CTX_free,EVP_MAC_CTX_dup,EVP_MAC_CTX_get0_mac,EVP_MAC_CTX_get_params,EVP_MAC_CTX_set_params,EVP_MAC_CTX_get_mac_size,EVP_MAC_CTX_get_block_size,EVP_MAC_CTX_gettable_params,EVP_MAC_CTX_settable_params - The actual-computing crew:
EVP_Q_mac,EVP_MAC_init,EVP_MAC_init_SKEY,EVP_MAC_update,EVP_MAC_final,EVP_MAC_finalXOF,EVP_MAC_gettable_ctx_params,EVP_MAC_settable_ctx_params
and they're all EVP MAC routines π«Ά
β¨ SYNOPSIS β¨
ok literally here's the exact shape of everything, no cap:
#include <openssl/evp.h>
typedef struct evp_mac_st EVP_MAC;
typedef struct evp_mac_ctx_st EVP_MAC_CTX;
EVP_MAC *EVP_MAC_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
const char *properties);
int EVP_MAC_up_ref(EVP_MAC *mac);
void EVP_MAC_free(EVP_MAC *mac);
int EVP_MAC_is_a(const EVP_MAC *mac, const char *name);
const char *EVP_MAC_get0_name(const EVP_MAC *mac);
int EVP_MAC_names_do_all(const EVP_MAC *mac,
void (*fn)(const char *name, void *data),
void *data);
const char *EVP_MAC_get0_description(const EVP_MAC *mac);
const OSSL_PROVIDER *EVP_MAC_get0_provider(const EVP_MAC *mac);
int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[]);
EVP_MAC_CTX *EVP_MAC_CTX_new(EVP_MAC *mac);
void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx);
EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src);
EVP_MAC *EVP_MAC_CTX_get0_mac(EVP_MAC_CTX *ctx);
int EVP_MAC_CTX_get_params(EVP_MAC_CTX *ctx, OSSL_PARAM params[]);
int EVP_MAC_CTX_set_params(EVP_MAC_CTX *ctx, const OSSL_PARAM params[]);
size_t EVP_MAC_CTX_get_mac_size(EVP_MAC_CTX *ctx);
size_t EVP_MAC_CTX_get_block_size(EVP_MAC_CTX *ctx);
unsigned char *EVP_Q_mac(OSSL_LIB_CTX *libctx, const char *name, const char *propq,
const char *subalg, const OSSL_PARAM *params,
const void *key, size_t keylen,
const unsigned char *data, size_t datalen,
unsigned char *out, size_t outsize, size_t *outlen);
int EVP_MAC_init(EVP_MAC_CTX *ctx, const unsigned char *key, size_t keylen,
const OSSL_PARAM params[]);
int EVP_MAC_init_SKEY(EVP_MAC_CTX *ctx, EVP_SKEY *skey, const OSSL_PARAM params[]);
int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen);
int EVP_MAC_final(EVP_MAC_CTX *ctx,
unsigned char *out, size_t *outl, size_t outsize);
int EVP_MAC_finalXOF(EVP_MAC_CTX *ctx, unsigned char *out, size_t outsize);
const OSSL_PARAM *EVP_MAC_gettable_params(const EVP_MAC *mac);
const OSSL_PARAM *EVP_MAC_gettable_ctx_params(const EVP_MAC *mac);
const OSSL_PARAM *EVP_MAC_settable_ctx_params(const EVP_MAC *mac);
const OSSL_PARAM *EVP_MAC_CTX_gettable_params(EVP_MAC_CTX *ctx);
const OSSL_PARAM *EVP_MAC_CTX_settable_params(EVP_MAC_CTX *ctx);
void EVP_MAC_do_all_provided(OSSL_LIB_CTX *libctx,
void (*fn)(EVP_MAC *mac, void *arg),
void *arg);
π DESCRIPTION π
So these types and functions are here to help your application calculate MACs of different types, and with different underlying algorithms if there are any.
Here's the tea on why MACs are a bit complex: some of them straight up use other algorithms to do the actual computation. HMAC uses a digest, and CMAC uses a cipher. So yeah β sometimes there are two contexts to keep track of: one for the MAC algorithm itself, and one for the underlying computation algorithm, if there is one π
And to make things less ambiguous, this manual says "context" or "MAC context" when it means the MAC level context, and "underlying context" or "computation context" when it means the context for the underlying computation algorithm, if there is one. iykyk π
π§ Types
EVP_MACβ a type that holds the implementation of a MAC. main character energy.EVP_MAC_CTXβ a context type that holds internal MAC information as well as a reference to a computation context, for those MACs that rely on an underlying computation algorithm.
π Algorithm implementation fetching
EVP_MAC_fetch()β fetches an implementation of a MAC algorithm, given a library context libctx and a set of properties. See "ALGORITHM FETCHING" incrypto(7)for further info. And see "Message Authentication Code (MAC)" inOSSL_PROVIDER-default(7)for the list of algorithms supported by the default provider. The returned value must eventually be freed withEVP_MAC_free(3)πEVP_MAC_up_ref()β increments the reference count of an already fetched MAC. protect your fave β¨EVP_MAC_free()β frees a fetched algorithm.NULLis a valid parameter, and for that this function is a no-op. no drama π
π¦ Context manipulation functions
EVP_MAC_CTX_new()β creates a new context for the MAC type mac. The created context can then be used with most other functions described here.EVP_MAC_CTX_free()β frees the contents of the context, including an underlying context if there is one, and the context itself too.NULLis a valid parameter, and for that this function is a no-op.EVP_MAC_CTX_dup()β duplicates the src context and returns a newly allocated context. clone era πEVP_MAC_CTX_get0_mac()β returns theEVP_MACassociated with the context ctx.
π₯ Computing functions
EVP_Q_mac()β computes the message authentication code of data with length datalen, using the MAC algorithm name and the key key with length keylen. The MAC algorithm is fetched using any given libctx and property query string propq. It takes parameters subalg and further params, both of which may beNULLif not needed. If out is notNULL, it places the result in the memory pointed at by out, but only if outsize is sufficient β otherwise, no computation is made. If out isNULL, it allocates and uses a buffer of suitable length, which will be returned on success and must be freed by the caller. In either case, and also on error, it assigns the number of bytes written to*outlenunless outlen isNULL.EVP_MAC_init()β sets up the underlying context ctx with the info given via the key and params arguments. The MAC key has a length of keylen, and the parameters in params are processed before setting the key. If key isNULL, the key must be set via params either as part of this call or separately usingEVP_MAC_CTX_set_params(). Providing non-NULLparams to this function is equivalent to callingEVP_MAC_CTX_set_params()with those params for the same ctx beforehand. π Note: there are additional requirements for some MAC algorithms during re-initialization β that's callingEVP_MAC_init()on anEVP_MACafterEVP_MAC_final()has been called on the same object. See the NOTES section below.EVP_MAC_init()should be called beforeEVP_MAC_update()andEVP_MAC_final(). order matters bestie π«ΆEVP_MAC_init_SKEY()β similar toEVP_MAC_init()but it accepts an opaqueEVP_SKEYobject as a key.EVP_MAC_update()β adds datalen bytes from data to the MAC input. feed her πEVP_MAC_final()β does the final computation and stores the result in the memory pointed at by out of size outsize, and sets the number of bytes written in*outl. If out isNULLor outsize is too small, then no computation is made. To figure out what the output length will be and allocate space for it dynamically, simply call with out beingNULLand outl pointing at a valid location, then allocate space and make a second call with out pointing at the allocated space. two-step slay β¨EVP_MAC_finalXOF()β does the final computation for an XOF based MAC and stores the result in the memory pointed at by out of size outsize.EVP_MAC_get_params()β retrieves details about the implementation mac. The set of parameters given with params determine exactly what parameters should be retrieved. Note that a parameter that is unknown in the underlying context is simply ignored. πEVP_MAC_CTX_get_params()β retrieves chosen parameters, given the context ctx and its underlying context. The set of parameters given with params determine exactly what parameters should be retrieved. Note that a parameter that is unknown in the underlying context is simply ignored.EVP_MAC_CTX_set_params()β passes chosen parameters to the underlying context, given a context ctx. The set of parameters given with params determine exactly what parameters are passed down. If params areNULL, the underlying context should do nothing and return1. Note that a parameter that is unknown in the underlying context is simply ignored. Also, what happens when a needed parameter isn't passed down is defined by the implementation. no cap, that's just how it is πEVP_MAC_gettable_params()β returns anOSSL_PARAM(3)array that describes the retrievable and settable parameters.EVP_MAC_gettable_params()returns parameters that can be used withEVP_MAC_get_params().EVP_MAC_gettable_ctx_params()andEVP_MAC_CTX_gettable_params()β return constantOSSL_PARAM(3)arrays that describe the retrievable parameters that can be used withEVP_MAC_CTX_get_params().EVP_MAC_gettable_ctx_params()returns the parameters that can be retrieved from the algorithm, whereasEVP_MAC_CTX_gettable_params()returns the parameters that can be retrieved in the context's current state. the way these two are almost the same but not πEVP_MAC_settable_ctx_params()andEVP_MAC_CTX_settable_params()β return constantOSSL_PARAM(3)arrays that describe the settable parameters that can be used withEVP_MAC_CTX_set_params().EVP_MAC_settable_ctx_params()returns the parameters that can be retrieved from the algorithm, whereasEVP_MAC_CTX_settable_params()returns the parameters that can be retrieved in the context's current state.
π Information functions
EVP_MAC_CTX_get_mac_size()β returns the MAC output size for the given context.EVP_MAC_CTX_get_block_size()β returns the MAC block size for the given context. Not all MAC algorithms support this. π«EVP_MAC_is_a()β checks if the given mac is an implementation of an algorithm that's identifiable with name.EVP_MAC_get0_provider()β returns the provider that holds the implementation of the given mac.EVP_MAC_do_all_provided()β traverses all MAC implemented by all activated providers in the given library context libctx, and for each of the implementations, calls the given function fn with the implementation method and the given arg as argument. tour de force π₯EVP_MAC_get0_name()β returns the name of the given MAC. For fetched MACs with multiple names, only one of them is returned; it's recommended to useEVP_MAC_names_do_all()instead. (she's a little limited, use the other one π)EVP_MAC_names_do_all()β traverses all names for mac, and calls fn with each name and data.EVP_MAC_get0_description()β returns a description of the mac, meant for display and human consumption. The description is at the discretion of the mac implementation.
π PARAMETERS (the knobs & dials) β¨
Okay so parameters are identified by name as strings, and each one has an expected data type and a maximum size. OpenSSL has a set of macros for the parameter names it expects to see in its own MAC implementations. Here are all three for each param: the OpenSSL macro for the parameter name, the name in string form, and a type description. Iconic, honestly. π
The standard parameter names are:
"key"(OSSL_MAC_PARAM_KEY) β<octet string>β¨ Its value is the MAC key as an array of bytes. For MACs that use an underlying computation algorithm, the algorithm must be set first β see the parameter names"algorithm"below."iv"(OSSL_MAC_PARAM_IV) β<octet string>π¦ Some MAC implementations (GMAC) require an IV; this parameter sets the IV."custom"(OSSL_MAC_PARAM_CUSTOM) β<octet string>π Some MAC implementations (KMAC, BLAKE2) accept a Customization String; this parameter sets the Customization String. The default value is the empty string."salt"(OSSL_MAC_PARAM_SALT) β<octet string>π This option is used by BLAKE2 MAC."xof"(OSSL_MAC_PARAM_XOF) β<integer>β Itβs a simple flag; the value0or1are expected. This option is used by KMAC."digest-noinit"(OSSL_MAC_PARAM_DIGEST_NOINIT) β<integer>π A simple flag to set the MAC digest to not initialise the implementation specific data. The value0or1is expected. This option is deprecated and will be removed in a future release. The option may be set, but is ignored."digest-oneshot"(OSSL_MAC_PARAM_DIGEST_ONESHOT) β<integer>π A simple flag to set the MAC digest to be a oneshot operation. The value0or1is expected. This option is deprecated and will be removed in a future release. The option may be set, but is ignored."properties"(OSSL_MAC_PARAM_PROPERTIES) β<UTF8 string>π"digest"(OSSL_MAC_PARAM_DIGEST) β<UTF8 string>π"cipher"(OSSL_MAC_PARAM_CIPHER) β<UTF8 string>π For MAC implementations that use an underlying computation cipher or digest, these parameters set what the algorithm should be. The value is always the name of the intended algorithm, or the properties. Note that not all algorithms may support all digests. HMAC does not support variable output length digests such asSHAKE128orSHAKE256."size"(OSSL_MAC_PARAM_SIZE) β<unsigned integer>π«Ά For MAC implementations that support it, set the output size thatEVP_MAC_final()should produce. The allowed sizes vary between MAC implementations, but must never exceed what can be given with asize_t."tls-data-size"(OSSL_MAC_PARAM_TLS_DATA_SIZE) β<unsigned integer>π₯ This parameter is only supported by HMAC. If set then special handling is activated for calculating the MAC of a received mac-then-encrypt TLS record where variable length record padding has been used (as in the case of CBC mode ciphersuites). The value represents the total length of the record that is having the MAC calculated including the received MAC and the record padding. When usedEVP_MAC_updatemust be called precisely twice. The first time with the 13 bytes of TLS"header"data, and the second time with the entire record including the MAC itself and any padding. The entire record length must equal the value passed in the"tls-data-size"parameter. The length passed in thedatalenparameter toEVP_MAC_update()should be equal to the length of the record after the MAC and any padding has been removed.
π¨ All these parameters should be used before the calls to any ofEVP_MAC_init(),EVP_MAC_update()andEVP_MAC_final()for a full computation. Anything else may give undefined results. No cap.
π NOTES (the fine print, but make it cute) π§
- The MAC life-cycle is described in
life_cycle-mac(7). In the future, the transitions described there will be enforced. When this is done, it will not be considered a breaking change to the API. - The usage of the parameter names
"custom","iv"and"salt"correspond to the names used in the standard where the algorithm was defined. - Some MAC algorithms store internal state that cannot be extracted during re-initalization. For example GMAC cannot extract an IV from the underlying CIPHER context, and so calling
EVP_MAC_init()on anEVP_MACobject afterEVP_MAC_final()has been called cannot reset its cipher state to what it was when the IV was initially generated. For such instances, anOSSL_MAC_PARAM_IVparameter must be passed with each call toEVP_MAC_init().
π RETURN VALUES (what you get back) π
EVP_MAC_fetch()returns a pointer to a newly fetchedEVP_MAC, orNULLif allocation failed.EVP_MAC_up_ref()returns1on success,0on error.EVP_MAC_names_do_all()returns1if the callback was called for all names. A return value of0means that the callback was not called for any names.EVP_MAC_free()returns nothing at all.EVP_MAC_is_a()returns1if the given method can be identified with the given name, otherwise0.EVP_MAC_get0_name()returns a name of the MAC, orNULLon error.EVP_MAC_get0_provider()returns a pointer to the provider for the MAC, orNULLon error.EVP_MAC_CTX_new()andEVP_MAC_CTX_dup()return a pointer to a newly createdEVP_MAC_CTX, orNULLif allocation failed.EVP_MAC_CTX_free()returns nothing at all.EVP_MAC_CTX_get_params()andEVP_MAC_CTX_set_params()return1on success,0on error.EVP_Q_mac()returns a pointer to the computed MAC value, orNULLon error.EVP_MAC_init(),EVP_MAC_init_SKEY(),EVP_MAC_update(),EVP_MAC_final(), andEVP_MAC_finalXOF()return1on success,0on error.EVP_MAC_CTX_get_mac_size()returns the expected output size, or0if it isnβt set. If it isnβt set, a call toEVP_MAC_init()will set it.EVP_MAC_CTX_get_block_size()returns the block size, or0if it isnβt set. If it isnβt set, a call toEVP_MAC_init()will set it.EVP_MAC_do_all_provided()returns nothing at all.
π» EXAMPLES (look, actual code!) π
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/params.h>
int main() {
EVP_MAC *mac = EVP_MAC_fetch(NULL, getenv("MY_MAC"), NULL);
const char *cipher = getenv("MY_MAC_CIPHER");
const char *digest = getenv("MY_MAC_DIGEST");
const char *key = getenv("MY_KEY");
EVP_MAC_CTX *ctx = NULL;
unsigned char buf[4096];
size_t read_l;
size_t final_l;
size_t i;
OSSL_PARAM params[3];
size_t params_n = 0;
if (cipher != NULL)
params[params_n++] =
OSSL_PARAM_construct_utf8_string("cipher", (char*)cipher, 0);
if (digest != NULL)
params[params_n++] =
OSSL_PARAM_construct_utf8_string("digest", (char*)digest, 0);
params[params_n] = OSSL_PARAM_construct_end();
if (mac == NULL
|| key == NULL
|| (ctx = EVP_MAC_CTX_new(mac)) == NULL
|| !EVP_MAC_init(ctx, (const unsigned char *)key, strlen(key),
params))
goto err;
while ( (read_l = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
if (!EVP_MAC_update(ctx, buf, read_l))
goto err;
}
if (!EVP_MAC_final(ctx, buf, &final_l, sizeof(buf)))
goto err;
printf("Result: ");
for (i = 0; i < final_l; i++)
printf("%02X", buf[i]);
printf("\n");
EVP_MAC_CTX_free(ctx);
EVP_MAC_free(mac);
exit(0);
err:
EVP_MAC_CTX_free(ctx);
EVP_MAC_free(mac);
fprintf(stderr, "Something went wrong\n");
ERR_print_errors_fp(stderr);
exit (1);
}
A run of this program, called with correct environment variables, can look like this:
$ MY_MAC=cmac MY_KEY=secret0123456789 MY_MAC_CIPHER=aes-128-cbc \
LD_LIBRARY_PATH=. ./foo < foo.c
Result: C5C06683CD9DDEF904D754505C560A4E
(in this example, that program was stored in foo.c and compiled to ./foo)
π SEE ALSO (go read these too) π
property(7)OSSL_PARAM(3)EVP_MAC-BLAKE2(7)EVP_MAC-CMAC(7)EVP_MAC-GMAC(7)EVP_MAC-HMAC(7)EVP_MAC-KMAC(7)EVP_MAC-Siphash(7)EVP_MAC-Poly1305(7)provider-mac(7)life_cycle-mac(7)
π°οΈ HISTORY (the lore) β¨
These functions were added in OpenSSL 3.0.
The EVP_MAC_init_SKEY() function was added in OpenSSL 3.5.
Β©οΈ COPYRIGHT (respect the license, bestie) π
Copyright 2018-2025 The OpenSSL Project Authors. All Rights Reserved.
Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.
β¨ And thatβs the whole page, bestie β source/version 3.5.0, dated 2026-09-25, page EVP_MAC(3ossl). Stay hydrated and parameterized! ππ
π¦ where this page comes from
- opensuse-leap 16.0: openssl-3-doc 3.5.0-160000.11.1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms