1=pod 2 3=head1 NAME 4 5EVP_MAC, EVP_MAC_fetch, EVP_MAC_up_ref, EVP_MAC_free, EVP_MAC_is_a, 6EVP_MAC_get0_name, EVP_MAC_names_do_all, EVP_MAC_get0_description, 7EVP_MAC_get0_provider, EVP_MAC_get_params, EVP_MAC_gettable_params, 8EVP_MAC_CTX, EVP_MAC_CTX_new, EVP_MAC_CTX_free, EVP_MAC_CTX_dup, 9EVP_MAC_CTX_get0_mac, EVP_MAC_CTX_get_params, EVP_MAC_CTX_set_params, 10EVP_MAC_CTX_get_mac_size, EVP_MAC_CTX_get_block_size, EVP_Q_mac, 11EVP_MAC_init, EVP_MAC_init_SKEY, EVP_MAC_update, EVP_MAC_final, EVP_MAC_finalXOF, 12EVP_MAC_gettable_ctx_params, EVP_MAC_settable_ctx_params, 13EVP_MAC_CTX_gettable_params, EVP_MAC_CTX_settable_params, 14EVP_MAC_do_all_provided - EVP MAC routines 15 16=head1 SYNOPSIS 17 18 #include <openssl/evp.h> 19 20 typedef struct evp_mac_st EVP_MAC; 21 typedef struct evp_mac_ctx_st EVP_MAC_CTX; 22 23 EVP_MAC *EVP_MAC_fetch(OSSL_LIB_CTX *libctx, const char *algorithm, 24 const char *properties); 25 int EVP_MAC_up_ref(EVP_MAC *mac); 26 void EVP_MAC_free(EVP_MAC *mac); 27 int EVP_MAC_is_a(const EVP_MAC *mac, const char *name); 28 const char *EVP_MAC_get0_name(const EVP_MAC *mac); 29 int EVP_MAC_names_do_all(const EVP_MAC *mac, 30 void (*fn)(const char *name, void *data), 31 void *data); 32 const char *EVP_MAC_get0_description(const EVP_MAC *mac); 33 const OSSL_PROVIDER *EVP_MAC_get0_provider(const EVP_MAC *mac); 34 int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[]); 35 36 EVP_MAC_CTX *EVP_MAC_CTX_new(EVP_MAC *mac); 37 void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx); 38 EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src); 39 EVP_MAC *EVP_MAC_CTX_get0_mac(EVP_MAC_CTX *ctx); 40 int EVP_MAC_CTX_get_params(EVP_MAC_CTX *ctx, OSSL_PARAM params[]); 41 int EVP_MAC_CTX_set_params(EVP_MAC_CTX *ctx, const OSSL_PARAM params[]); 42 43 size_t EVP_MAC_CTX_get_mac_size(EVP_MAC_CTX *ctx); 44 size_t EVP_MAC_CTX_get_block_size(EVP_MAC_CTX *ctx); 45 unsigned char *EVP_Q_mac(OSSL_LIB_CTX *libctx, const char *name, const char *propq, 46 const char *subalg, const OSSL_PARAM *params, 47 const void *key, size_t keylen, 48 const unsigned char *data, size_t datalen, 49 unsigned char *out, size_t outsize, size_t *outlen); 50 int EVP_MAC_init(EVP_MAC_CTX *ctx, const unsigned char *key, size_t keylen, 51 const OSSL_PARAM params[]); 52 int EVP_MAC_init_SKEY(EVP_MAC_CTX *ctx, EVP_SKEY *skey, const OSSL_PARAM params[]); 53 int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen); 54 int EVP_MAC_final(EVP_MAC_CTX *ctx, 55 unsigned char *out, size_t *outl, size_t outsize); 56 int EVP_MAC_finalXOF(EVP_MAC_CTX *ctx, unsigned char *out, size_t outsize); 57 58 const OSSL_PARAM *EVP_MAC_gettable_params(const EVP_MAC *mac); 59 const OSSL_PARAM *EVP_MAC_gettable_ctx_params(const EVP_MAC *mac); 60 const OSSL_PARAM *EVP_MAC_settable_ctx_params(const EVP_MAC *mac); 61 const OSSL_PARAM *EVP_MAC_CTX_gettable_params(EVP_MAC_CTX *ctx); 62 const OSSL_PARAM *EVP_MAC_CTX_settable_params(EVP_MAC_CTX *ctx); 63 64 void EVP_MAC_do_all_provided(OSSL_LIB_CTX *libctx, 65 void (*fn)(EVP_MAC *mac, void *arg), 66 void *arg); 67 68=head1 DESCRIPTION 69 70These types and functions help the application to calculate MACs of 71different types and with different underlying algorithms if there are 72any. 73 74MACs are a bit complex insofar that some of them use other algorithms 75for actual computation. HMAC uses a digest, and CMAC uses a cipher. 76Therefore, there are sometimes two contexts to keep track of, one for 77the MAC algorithm itself and one for the underlying computation 78algorithm if there is one. 79 80To make things less ambiguous, this manual talks about a "context" or 81"MAC context", which is to denote the MAC level context, and about a 82"underlying context", or "computation context", which is to denote the 83context for the underlying computation algorithm if there is one. 84 85=head2 Types 86 87B<EVP_MAC> is a type that holds the implementation of a MAC. 88 89B<EVP_MAC_CTX> is a context type that holds internal MAC information 90as well as a reference to a computation context, for those MACs that 91rely on an underlying computation algorithm. 92 93=head2 Algorithm implementation fetching 94 95EVP_MAC_fetch() fetches an implementation of a MAC I<algorithm>, given 96a library context I<libctx> and a set of I<properties>. 97See L<crypto(7)/ALGORITHM FETCHING> for further information. 98 99See L<OSSL_PROVIDER-default(7)/Message Authentication Code (MAC)> for the list 100of algorithms supported by the default provider. 101 102The returned value must eventually be freed with 103L<EVP_MAC_free(3)>. 104 105EVP_MAC_up_ref() increments the reference count of an already fetched 106MAC. 107 108EVP_MAC_free() frees a fetched algorithm. 109NULL is a valid parameter, for which this function is a no-op. 110 111=head2 Context manipulation functions 112 113EVP_MAC_CTX_new() creates a new context for the MAC type I<mac>. 114The created context can then be used with most other functions 115described here. 116 117EVP_MAC_CTX_free() frees the contents of the context, including an 118underlying context if there is one, as well as the context itself. 119NULL is a valid parameter, for which this function is a no-op. 120 121EVP_MAC_CTX_dup() duplicates the I<src> context and returns a newly allocated 122context. 123 124EVP_MAC_CTX_get0_mac() returns the B<EVP_MAC> associated with the context 125I<ctx>. 126 127=head2 Computing functions 128 129EVP_Q_mac() computes the message authentication code 130of I<data> with length I<datalen> 131using the MAC algorithm I<name> and the key I<key> with length I<keylen>. 132The MAC algorithm is fetched using any given I<libctx> and property query 133string I<propq>. It takes parameters I<subalg> and further I<params>, 134both of which may be NULL if not needed. 135If I<out> is not NULL, it places the result in the memory pointed at by I<out>, 136but only if I<outsize> is sufficient (otherwise no computation is made). 137If I<out> is NULL, it allocates and uses a buffer of suitable length, 138which will be returned on success and must be freed by the caller. 139In either case, also on error, 140it assigns the number of bytes written to I<*outlen> unless I<outlen> is NULL. 141 142EVP_MAC_init() sets up the underlying context I<ctx> with information given 143via the I<key> and I<params> arguments. The MAC I<key> has a length of 144I<keylen> and the parameters in I<params> are processed before setting 145the key. If I<key> is NULL, the key must be set via I<params> either 146as part of this call or separately using EVP_MAC_CTX_set_params(). 147Providing non-NULL I<params> to this function is equivalent to calling 148EVP_MAC_CTX_set_params() with those I<params> for the same I<ctx> beforehand. 149Note: There are additional requirements for some MAC algorithms during 150re-initalization (i.e. calling EVP_MAC_init() on an EVP_MAC after EVP_MAC_final() 151has been called on the same object). See the NOTES section below. 152 153EVP_MAC_init() should be called before EVP_MAC_update() and EVP_MAC_final(). 154 155EVP_MAC_init_SKEY() is similar to EVP_MAC_init() but it accepts an opaque 156B<EVP_SKEY> object as a key. 157 158EVP_MAC_update() adds I<datalen> bytes from I<data> to the MAC input. 159 160EVP_MAC_final() does the final computation and stores the result in 161the memory pointed at by I<out> of size I<outsize>, and sets the number 162of bytes written in I<*outl> at. 163If I<out> is NULL or I<outsize> is too small, then no computation 164is made. 165To figure out what the output length will be and allocate space for it 166dynamically, simply call with I<out> being NULL and I<outl> 167pointing at a valid location, then allocate space and make a second 168call with I<out> pointing at the allocated space. 169 170EVP_MAC_finalXOF() does the final computation for an XOF based MAC and stores 171the result in the memory pointed at by I<out> of size I<outsize>. 172 173EVP_MAC_get_params() retrieves details about the implementation 174I<mac>. 175The set of parameters given with I<params> determine exactly what 176parameters should be retrieved. 177Note that a parameter that is unknown in the underlying context is 178simply ignored. 179 180EVP_MAC_CTX_get_params() retrieves chosen parameters, given the 181context I<ctx> and its underlying context. 182The set of parameters given with I<params> determine exactly what 183parameters should be retrieved. 184Note that a parameter that is unknown in the underlying context is 185simply ignored. 186 187EVP_MAC_CTX_set_params() passes chosen parameters to the underlying 188context, given a context I<ctx>. 189The set of parameters given with I<params> determine exactly what 190parameters are passed down. 191If I<params> are NULL, the underlying context should do nothing and return 1. 192Note that a parameter that is unknown in the underlying context is 193simply ignored. 194Also, what happens when a needed parameter isn't passed down is 195defined by the implementation. 196 197EVP_MAC_gettable_params() returns an L<OSSL_PARAM(3)> array that describes 198the retrievable and settable parameters. EVP_MAC_gettable_params() 199returns parameters that can be used with EVP_MAC_get_params(). 200 201EVP_MAC_gettable_ctx_params() and EVP_MAC_CTX_gettable_params() 202return constant L<OSSL_PARAM(3)> arrays that describe the retrievable 203parameters that can be used with EVP_MAC_CTX_get_params(). 204EVP_MAC_gettable_ctx_params() returns the parameters that can be retrieved 205from the algorithm, whereas EVP_MAC_CTX_gettable_params() returns 206the parameters that can be retrieved in the context's current state. 207 208EVP_MAC_settable_ctx_params() and EVP_MAC_CTX_settable_params() return 209constant L<OSSL_PARAM(3)> arrays that describe the settable parameters that 210can be used with EVP_MAC_CTX_set_params(). EVP_MAC_settable_ctx_params() 211returns the parameters that can be retrieved from the algorithm, 212whereas EVP_MAC_CTX_settable_params() returns the parameters that can 213be retrieved in the context's current state. 214 215=head2 Information functions 216 217EVP_MAC_CTX_get_mac_size() returns the MAC output size for the given context. 218 219EVP_MAC_CTX_get_block_size() returns the MAC block size for the given context. 220Not all MAC algorithms support this. 221 222EVP_MAC_is_a() checks if the given I<mac> is an implementation of an 223algorithm that's identifiable with I<name>. 224 225EVP_MAC_get0_provider() returns the provider that holds the implementation 226of the given I<mac>. 227 228EVP_MAC_do_all_provided() traverses all MAC implemented by all activated 229providers in the given library context I<libctx>, and for each of the 230implementations, calls the given function I<fn> with the implementation method 231and the given I<arg> as argument. 232 233EVP_MAC_get0_name() return the name of the given MAC. For fetched MACs 234with multiple names, only one of them is returned; it's 235recommended to use EVP_MAC_names_do_all() instead. 236 237EVP_MAC_names_do_all() traverses all names for I<mac>, and calls 238I<fn> with each name and I<data>. 239 240EVP_MAC_get0_description() returns a description of the I<mac>, meant 241for display and human consumption. The description is at the discretion 242of the mac implementation. 243 244=head1 PARAMETERS 245 246Parameters are identified by name as strings, and have an expected 247data type and maximum size. 248OpenSSL has a set of macros for parameter names it expects to see in 249its own MAC implementations. 250Here, we show all three, the OpenSSL macro for the parameter name, the 251name in string form, and a type description. 252 253The standard parameter names are: 254 255=over 4 256 257=item "key" (B<OSSL_MAC_PARAM_KEY>) <octet string> 258 259Its value is the MAC key as an array of bytes. 260 261For MACs that use an underlying computation algorithm, the algorithm 262must be set first, see parameter names "algorithm" below. 263 264=item "iv" (B<OSSL_MAC_PARAM_IV>) <octet string> 265 266Some MAC implementations (GMAC) require an IV, this parameter sets the IV. 267 268=item "custom" (B<OSSL_MAC_PARAM_CUSTOM>) <octet string> 269 270Some MAC implementations (KMAC, BLAKE2) accept a Customization String, 271this parameter sets the Customization String. The default value is the 272empty string. 273 274=item "salt" (B<OSSL_MAC_PARAM_SALT>) <octet string> 275 276This option is used by BLAKE2 MAC. 277 278=item "xof" (B<OSSL_MAC_PARAM_XOF>) <integer> 279 280It's a simple flag, the value 0 or 1 are expected. 281 282This option is used by KMAC. 283 284=item "digest-noinit" (B<OSSL_MAC_PARAM_DIGEST_NOINIT>) <integer> 285 286A simple flag to set the MAC digest to not initialise the 287implementation specific data. The value 0 or 1 is expected. 288 289This option is deprecated and will be removed in a future release. 290The option may be set, but is ignored. 291 292=item "digest-oneshot" (B<OSSL_MAC_PARAM_DIGEST_ONESHOT>) <integer> 293 294A simple flag to set the MAC digest to be a oneshot operation. 295The value 0 or 1 is expected. 296 297This option is deprecated and will be removed in a future release. 298The option may be set, but is ignored. 299 300=item "properties" (B<OSSL_MAC_PARAM_PROPERTIES>) <UTF8 string> 301 302=item "digest" (B<OSSL_MAC_PARAM_DIGEST>) <UTF8 string> 303 304=item "cipher" (B<OSSL_MAC_PARAM_CIPHER>) <UTF8 string> 305 306For MAC implementations that use an underlying computation cipher or 307digest, these parameters set what the algorithm should be. 308 309The value is always the name of the intended algorithm, 310or the properties. 311 312Note that not all algorithms may support all digests. 313HMAC does not support variable output length digests such as SHAKE128 314or SHAKE256. 315 316=item "size" (B<OSSL_MAC_PARAM_SIZE>) <unsigned integer> 317 318For MAC implementations that support it, set the output size that 319EVP_MAC_final() should produce. 320The allowed sizes vary between MAC implementations, but must never exceed 321what can be given with a B<size_t>. 322 323=item "tls-data-size" (B<OSSL_MAC_PARAM_TLS_DATA_SIZE>) <unsigned integer> 324 325This parameter is only supported by HMAC. If set then special handling is 326activated for calculating the MAC of a received mac-then-encrypt TLS record 327where variable length record padding has been used (as in the case of CBC mode 328ciphersuites). The value represents the total length of the record that is 329having the MAC calculated including the received MAC and the record padding. 330 331When used EVP_MAC_update must be called precisely twice. The first time with 332the 13 bytes of TLS "header" data, and the second time with the entire record 333including the MAC itself and any padding. The entire record length must equal 334the value passed in the "tls-data-size" parameter. The length passed in the 335B<datalen> parameter to EVP_MAC_update() should be equal to the length of the 336record after the MAC and any padding has been removed. 337 338=back 339 340All these parameters should be used before the calls to any of 341EVP_MAC_init(), EVP_MAC_update() and EVP_MAC_final() for a full 342computation. 343Anything else may give undefined results. 344 345=head1 NOTES 346 347The MAC life-cycle is described in L<life_cycle-mac(7)>. In the future, 348the transitions described there will be enforced. When this is done, it will 349not be considered a breaking change to the API. 350 351The usage of the parameter names "custom", "iv" and "salt" correspond to 352the names used in the standard where the algorithm was defined. 353 354Some MAC algorithms store internal state that cannot be extracted during 355re-initalization. For example GMAC cannot extract an B<IV> from the 356underlying CIPHER context, and so calling EVP_MAC_init() on an EVP_MAC object 357after EVP_MAC_final() has been called cannot reset its cipher state to what it 358was when the B<IV> was initially generated. For such instances, an 359B<OSSL_MAC_PARAM_IV> parameter must be passed with each call to EVP_MAC_init(). 360 361=head1 RETURN VALUES 362 363EVP_MAC_fetch() returns a pointer to a newly fetched B<EVP_MAC>, or 364NULL if allocation failed. 365 366EVP_MAC_up_ref() returns 1 on success, 0 on error. 367 368EVP_MAC_names_do_all() returns 1 if the callback was called for all names. A 369return value of 0 means that the callback was not called for any names. 370 371EVP_MAC_free() returns nothing at all. 372 373EVP_MAC_is_a() returns 1 if the given method can be identified with 374the given name, otherwise 0. 375 376EVP_MAC_get0_name() returns a name of the MAC, or NULL on error. 377 378EVP_MAC_get0_provider() returns a pointer to the provider for the MAC, or 379NULL on error. 380 381EVP_MAC_CTX_new() and EVP_MAC_CTX_dup() return a pointer to a newly 382created EVP_MAC_CTX, or NULL if allocation failed. 383 384EVP_MAC_CTX_free() returns nothing at all. 385 386EVP_MAC_CTX_get_params() and EVP_MAC_CTX_set_params() return 1 on 387success, 0 on error. 388 389EVP_Q_mac() returns a pointer to the computed MAC value, or NULL on error. 390 391EVP_MAC_init(), EVP_MAC_init_SKEY(), EVP_MAC_update(), EVP_MAC_final(), and 392EVP_MAC_finalXOF() return 1 on success, 0 on error. 393 394EVP_MAC_CTX_get_mac_size() returns the expected output size, or 0 if it isn't 395set. If it isn't set, a call to EVP_MAC_init() will set it. 396 397EVP_MAC_CTX_get_block_size() returns the block size, or 0 if it isn't set. 398If it isn't set, a call to EVP_MAC_init() will set it. 399 400EVP_MAC_do_all_provided() returns nothing at all. 401 402=head1 EXAMPLES 403 404 #include <stdlib.h> 405 #include <stdio.h> 406 #include <string.h> 407 #include <stdarg.h> 408 #include <unistd.h> 409 410 #include <openssl/evp.h> 411 #include <openssl/err.h> 412 #include <openssl/params.h> 413 414 int main() { 415 EVP_MAC *mac = EVP_MAC_fetch(NULL, getenv("MY_MAC"), NULL); 416 const char *cipher = getenv("MY_MAC_CIPHER"); 417 const char *digest = getenv("MY_MAC_DIGEST"); 418 const char *key = getenv("MY_KEY"); 419 EVP_MAC_CTX *ctx = NULL; 420 421 unsigned char buf[4096]; 422 size_t read_l; 423 size_t final_l; 424 425 size_t i; 426 427 OSSL_PARAM params[3]; 428 size_t params_n = 0; 429 430 if (cipher != NULL) 431 params[params_n++] = 432 OSSL_PARAM_construct_utf8_string("cipher", (char*)cipher, 0); 433 if (digest != NULL) 434 params[params_n++] = 435 OSSL_PARAM_construct_utf8_string("digest", (char*)digest, 0); 436 params[params_n] = OSSL_PARAM_construct_end(); 437 438 if (mac == NULL 439 || key == NULL 440 || (ctx = EVP_MAC_CTX_new(mac)) == NULL 441 || !EVP_MAC_init(ctx, (const unsigned char *)key, strlen(key), 442 params)) 443 goto err; 444 445 while ( (read_l = read(STDIN_FILENO, buf, sizeof(buf))) > 0) { 446 if (!EVP_MAC_update(ctx, buf, read_l)) 447 goto err; 448 } 449 450 if (!EVP_MAC_final(ctx, buf, &final_l, sizeof(buf))) 451 goto err; 452 453 printf("Result: "); 454 for (i = 0; i < final_l; i++) 455 printf("%02X", buf[i]); 456 printf("\n"); 457 458 EVP_MAC_CTX_free(ctx); 459 EVP_MAC_free(mac); 460 exit(0); 461 462 err: 463 EVP_MAC_CTX_free(ctx); 464 EVP_MAC_free(mac); 465 fprintf(stderr, "Something went wrong\n"); 466 ERR_print_errors_fp(stderr); 467 exit (1); 468 } 469 470A run of this program, called with correct environment variables, can 471look like this: 472 473 $ MY_MAC=cmac MY_KEY=secret0123456789 MY_MAC_CIPHER=aes-128-cbc \ 474 LD_LIBRARY_PATH=. ./foo < foo.c 475 Result: C5C06683CD9DDEF904D754505C560A4E 476 477(in this example, that program was stored in F<foo.c> and compiled to 478F<./foo>) 479 480=head1 SEE ALSO 481 482L<property(7)> 483L<OSSL_PARAM(3)>, 484L<EVP_MAC-BLAKE2(7)>, 485L<EVP_MAC-CMAC(7)>, 486L<EVP_MAC-GMAC(7)>, 487L<EVP_MAC-HMAC(7)>, 488L<EVP_MAC-KMAC(7)>, 489L<EVP_MAC-Siphash(7)>, 490L<EVP_MAC-Poly1305(7)>, 491L<provider-mac(7)>, 492L<life_cycle-mac(7)> 493 494=head1 HISTORY 495 496These functions were added in OpenSSL 3.0. 497 498The EVP_MAC_init_SKEY() function was added in OpenSSL 3.5. 499 500=head1 COPYRIGHT 501 502Copyright 2018-2025 The OpenSSL Project Authors. All Rights Reserved. 503 504Licensed under the Apache License 2.0 (the "License"). You may not use 505this file except in compliance with the License. You can obtain a copy 506in the file LICENSE in the source distribution or at 507L<https://www.openssl.org/source/license.html>. 508 509=cut 510