EVP_MAC(3SSL)
β¨ EVP_MAC(3SSL) β OpenSSL β¨
okay bestie, this is the OpenSSL manual for the whole EVP_MAC era. Itβs giving βmessage authentication codes but make it modular.β no cap.
π NAME π
this page is the collective girlboss documentation for:
EVP_MACEVP_MAC_fetchEVP_MAC_up_refEVP_MAC_freeEVP_MAC_is_aEVP_MAC_get0_nameEVP_MAC_names_do_allEVP_MAC_get0_descriptionEVP_MAC_get0_providerEVP_MAC_get_paramsEVP_MAC_gettable_paramsEVP_MAC_CTXEVP_MAC_CTX_newEVP_MAC_CTX_freeEVP_MAC_CTX_dupEVP_MAC_CTX_get0_macEVP_MAC_CTX_get_paramsEVP_MAC_CTX_set_paramsEVP_MAC_CTX_get_mac_sizeEVP_MAC_CTX_get_block_sizeEVP_Q_macEVP_MAC_initEVP_MAC_init_SKEYEVP_MAC_updateEVP_MAC_finalEVP_MAC_finalXOFEVP_MAC_gettable_ctx_paramsEVP_MAC_settable_ctx_paramsEVP_MAC_CTX_gettable_paramsEVP_MAC_CTX_settable_paramsEVP_MAC_do_all_providedβ and together theyβre the EVP MAC routines π
π SYNOPSIS π
#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 π¦
okay so these types and functions help your application calculate MACs of different types and with different underlying algorithms if there are any. yes bestie, MACs can have layers.
ngl MACs are a bit complex because some of them use other algorithms for the actual computation. like, HMAC uses a digest, and CMAC uses a cipher. therefore, there are sometimes two contexts to keep track of: one for the MAC algorithm itself, and one for the underlying computation algorithm if there is one.
to make things less ambiguous, this manual talks about a βcontextβ or βMAC contextβ to mean the MAC-level context, and about an βunderlying contextβ or βcomputation contextβ to mean the context for the underlying computation algorithm if there is one. periodt.
πΈ Types πΈ
EVP_MACis a type that holds the implementation of a MAC.EVP_MAC_CTXis 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 contextlibctxand a set of properties. See βALGORITHM FETCHINGβ incrypto(7)for further information.- See βMessage Authentication Code (MAC)β in
OSSL_PROVIDER-default(7)for the list of algorithms supported by the default provider. - The returned value must eventually be freed with
EVP_MAC_free(3). EVP_MAC_up_ref()increments the reference count of an already fetched MAC.EVP_MAC_free()frees a fetched algorithm.NULLis a valid parameter, for which this function is a no-op.
π«Ά Context manipulation functions π«Ά
EVP_MAC_CTX_new()creates a new context for the MAC typemac. 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, as well as the context itself.NULLis a valid parameter, for which this function is a no-op.EVP_MAC_CTX_dup()duplicates thesrccontext and returns a newly allocated context.EVP_MAC_CTX_get0_mac()returns theEVP_MACassociated with the contextctx.
π₯ Computing functions π₯
EVP_Q_mac()β computes the message authentication code ofdatawith lengthdatalenusing the MAC algorithmnameand the keykeywith lengthkeylen. The MAC algorithm is fetched using any givenlibctxand property query stringpropq. It takes parameterssubalgand furtherparams, both of which may beNULLif not needed. Ifoutis notNULL, it places the result in the memory pointed at byout, but only ifoutsizeis sufficient (otherwise no computation is made). IfoutisNULL, 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, also on error, it assigns the number of bytes written to*outlenunlessoutlenisNULL.EVP_MAC_init()β sets up the underlying contextctxwith information given via thekeyandparamsarguments. The MAC key has a length ofkeylenand the parameters inparamsare processed before setting the key. IfkeyisNULL, the key must be set viaparamseither as part of this call or separately usingEVP_MAC_CTX_set_params(). Providing non-NULLparamsto this function is equivalent to callingEVP_MAC_CTX_set_params()with thoseparamsfor the samectxbeforehand. Note: There are additional requirements for some MAC algorithms during re-initalization (i.e. 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().EVP_MAC_init_SKEY()β is similar toEVP_MAC_init()but it accepts an opaqueEVP_SKEYobject as a key.EVP_MAC_update()β addsdatalenbytes fromdatato the MAC input.EVP_MAC_final()β does the final computation and stores the result in the memory pointed at byoutof sizeoutsize, and sets the number of bytes written in*outlat. IfoutisNULLoroutsizeis too small, then no computation is made. To figure out what the output length will be and allocate space for it dynamically, simply call withoutbeingNULLandoutlpointing at a valid location, then allocate space and make a second call withoutpointing at the allocated space.EVP_MAC_finalXOF()β does the final computation for an XOF based MAC and stores the result in the memory pointed at byoutof sizeoutsize.EVP_MAC_get_params()β retrieves details about the implementationmac. The set of parameters given withparamsdetermine 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 contextctxand its underlying context. The set of parameters given withparamsdetermine 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 contextctx. The set of parameters given withparamsdetermine exactly what parameters are passed down. IfparamsareNULL, the underlying context should do nothing and return 1. 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.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.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 givenmacis an implementation of an algorithm thatβs identifiable withname.EVP_MAC_get0_provider()returns the provider that holds the implementation of the givenmac.EVP_MAC_do_all_provided()traverses all MAC implemented by all activated providers in the given library contextlibctx, and for each of the implementations, calls the given functionfnwith the implementation method and the givenargas argument.EVP_MAC_get0_name()return 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.EVP_MAC_names_do_all()traverses all names formac, and callsfnwith each name anddata.EVP_MAC_get0_description()returns a description of themac, meant for display and human consumption. The description is at the discretion of themacimplementation.
πΈ THE PARAMETERS (a.k.a. the flavor knobs) πΈ
Sooo parameters are identified by name as strings, and each one has an expected data type and a maximum size. OpenSSL ships a whole set of macros for the parameter names it expects to see in its own MAC implementations. Bestie, I'm gonna show you all three things for each one: the OpenSSL macro for the parameter name, the name in string form, and a type description.
The standard parameter names are giving:
π "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 β peep the "cipher" and "digest" parameters below.
π§ "iv" (OSSL_MAC_PARAM_IV) β <octet string>
Some MAC implementations (hi GMAC π ) require an IV, and this parameter sets the IV.
π "custom" (OSSL_MAC_PARAM_CUSTOM) β <octet string>
Some MAC implementations (KMAC, BLAKE2) accept a Customization String, and 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, and the value 0 or 1 are 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 value 0 or 1 is 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 value 0 or 1 is 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 as SHAKE128 or SHAKE256.
π "size" (OSSL_MAC_PARAM_SIZE) β <unsigned integer>
For MAC implementations that support it, set the output size that EVP_MAC_final() should produce. The allowed sizes vary between MAC implementations, but must never exceed what can be given with a size_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 used EVP_MAC_update must 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 the datalen parameter to EVP_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 of EVP_MAC_init(), EVP_MAC_update() and EVP_MAC_final() for a full computation. Anything else may give undefined results. π
π NOTES π
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 an EVP_MAC object after EVP_MAC_final() has been called cannot reset its cipher state to what it was when the IV was initially generated. For such instances, an OSSL_MAC_PARAM_IV parameter must be passed with each call to EVP_MAC_init(). β¨
π RETURN VALUES π
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 π§ (the moment you've been waiting for)
#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 π¦
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 π
These functions were added in OpenSSL 3.0.
The EVP_MAC_init_SKEY() function was added in OpenSSL 3.5.
π COPYRIGHT π
Copyright 2018-2026 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!! π this one's living in OpenSSL 4.0.3, dated 2026-09-29, filed under EVP_MAC(3SSL) β go forth and MAC responsibly, no cap π
β¨
π¦ where this page comes from
- ubuntu stonking: libssl-doc 4.0.3-1ubuntu1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms