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