EVP_MAC(3ossl)
πΈ EVP_MAC β the EVP MAC routines (3ossl) πΈ
okay so bestie, this is the OpenSSL manual page for the whole EVP_MAC fam β every function, every type, no cap. It's lowkey long so like, hydrate first. π
β¨ NAME β¨
Everything in the EVP_MAC universe, aka the EVP MAC routines:
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_paramsEVP_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_sizeEVP_Q_mac,EVP_MAC_init,EVP_MAC_init_SKEY,EVP_MAC_update,EVP_MAC_final,EVP_MAC_finalXOFEVP_MAC_gettable_ctx_params,EVP_MAC_settable_ctx_params,EVP_MAC_CTX_gettable_params,EVP_MAC_CTX_settable_paramsEVP_MAC_do_all_provided
β and yes, all of those are 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 π
These types and functions are the whole glow-up that lets your app calculate MACs (message authentication codes) of different types, with different underlying algorithms if there even are any. Iconic.
So here's the tea: MACs are a bit complex, because some of them straight up use other algorithms to do the actual math. HMAC uses a digest, and CMAC uses a cipher. Which means sometimes you've got two contexts to keep track of β one for the MAC algorithm itself, and one for the underlying computation algorithm if there is one. π
To keep things from getting ambiguous (and we hate 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.EVP_MAC_CTXβ a context type that holds internal MAC info plus a reference to a computation context, for those MACs that rely on an underlying computation algorithm. bestie holds it all together fr.
π§ 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 more. The returned value must eventually be freed withEVP_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, and for it this function is a no-op, so no stress. π«Ά
Also, see "Message Authentication Code (MAC)" in OSSL_PROVIDER-default(7) for the list of algorithms the default provider supports. β¨
π¦ Context manipulation functions π¦
EVP_MAC_CTX_new()β creates a new context for the MAC type mac. Once you've got it, you can use it with most other functions described here. main character energy honestly.EVP_MAC_CTX_free()β frees the contents of the context, including an underlying context if there is one, and the context itself.NULLis a valid parameter, and for it this function is a no-op.EVP_MAC_CTX_dup()β duplicates the src context and returns a newly allocated context. twin behavior π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, periodt. 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, also on error, it assigns the number of bytes written to*outlenunless outlen isNULL.EVP_MAC_init()β sets up the underlying context ctx with 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 before EVP_MAC_update() and EVP_MAC_final().
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.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's 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's 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 return 1. Note that a parameter that's unknown in the underlying context is simply ignored. Also, what happens when a needed parameter isn't passed down is defined by the implementation. (so like, it's giving "implementation's problem" π)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, so don't come for it if yours doesn't. π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. it's giving a tour of the whole neighborhood π§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. lowkey don't rely on this 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 π
okay so parameters are identified by name as strings, and they have an expected data type and a maximum size. OpenSSL has a set of macros for parameter names it expects to see in its own MAC implementations. Below, each standard parameter gives you all three: the OpenSSL macro for the parameter name, the name in string form, and a type description. slay.
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 parameter names "algorithm" below.
"iv"(OSSL_MAC_PARAM_IV)<octet string>β some MAC implementations (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 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 as SHAKE128 or SHAKE256.
"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 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 ofEVP_MAC_init(),EVP_MAC_update()andEVP_MAC_final()for a full computation. Anything else may give undefined results. no cap, don't play with that.
π¦ 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 π
okay bestie, here's a whole little program. copy it exactly, no vibes-based edits:
#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 be nosy: 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-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.
that's the whole tea β β OpenSSL 3.5.6, dated 2026-04-07, EVP_MAC(3ossl), mwah π
π¦ where this page comes from
- freebsd 15.1: base 15.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms