1EVP_MAC(3ossl)                      OpenSSL                     EVP_MAC(3ossl)
2
3
4

NAME

6       EVP_MAC, EVP_MAC_fetch, EVP_MAC_up_ref, EVP_MAC_free, EVP_MAC_is_a,
7       EVP_MAC_get0_name, EVP_MAC_names_do_all, EVP_MAC_get0_description,
8       EVP_MAC_get0_provider, EVP_MAC_get_params, EVP_MAC_gettable_params,
9       EVP_MAC_CTX, EVP_MAC_CTX_new, EVP_MAC_CTX_free, EVP_MAC_CTX_dup,
10       EVP_MAC_CTX_get0_mac, EVP_MAC_CTX_get_params, EVP_MAC_CTX_set_params,
11       EVP_MAC_CTX_get_mac_size, EVP_MAC_CTX_get_block_size, EVP_Q_mac,
12       EVP_MAC_init, EVP_MAC_update, EVP_MAC_final, EVP_MAC_finalXOF,
13       EVP_MAC_gettable_ctx_params, EVP_MAC_settable_ctx_params,
14       EVP_MAC_CTX_gettable_params, EVP_MAC_CTX_settable_params,
15       EVP_MAC_do_all_provided - EVP MAC routines
16

SYNOPSIS

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

DESCRIPTION

68       These types and functions help the application to calculate MACs of
69       different types and with different underlying algorithms if there are
70       any.
71
72       MACs are a bit complex insofar that some of them use other algorithms
73       for actual computation.  HMAC uses a digest, and CMAC uses a cipher.
74       Therefore, there are sometimes two contexts to keep track of, one for
75       the MAC algorithm itself and one for the underlying computation
76       algorithm if there is one.
77
78       To make things less ambiguous, this manual talks about a "context" or
79       "MAC context", which is to denote the MAC level context, and about a
80       "underlying context", or "computation context", which is to denote the
81       context for the underlying computation algorithm if there is one.
82
83   Types
84       EVP_MAC is a type that holds the implementation of a MAC.
85
86       EVP_MAC_CTX is a context type that holds internal MAC information as
87       well as a reference to a computation context, for those MACs that rely
88       on an underlying computation algorithm.
89
90   Algorithm implementation fetching
91       EVP_MAC_fetch() fetches an implementation of a MAC algorithm, given a
92       library context libctx and a set of properties.  See "ALGORITHM
93       FETCHING" in crypto(7) for further information.
94
95       See "Message Authentication Code (MAC)" in OSSL_PROVIDER-default(7) for
96       the list of algorithms supported by the default provider.
97
98       The returned value must eventually be freed with EVP_MAC_free(3).
99
100       EVP_MAC_up_ref() increments the reference count of an already fetched
101       MAC.
102
103       EVP_MAC_free() frees a fetched algorithm.  NULL is a valid parameter,
104       for which this function is a no-op.
105
106   Context manipulation functions
107       EVP_MAC_CTX_new() creates a new context for the MAC type mac.  The
108       created context can then be used with most other functions described
109       here.
110
111       EVP_MAC_CTX_free() frees the contents of the context, including an
112       underlying context if there is one, as well as the context itself.
113       NULL is a valid parameter, for which this function is a no-op.
114
115       EVP_MAC_CTX_dup() duplicates the src context and returns a newly
116       allocated context.
117
118       EVP_MAC_CTX_get0_mac() returns the EVP_MAC associated with the context
119       ctx.
120
121   Computing functions
122       EVP_Q_mac() computes the message authentication code of data with
123       length datalen using the MAC algorithm name and the key key with length
124       keylen.  The MAC algorithm is fetched using any given libctx and
125       property query string propq. It takes parameters subalg and further
126       params, both of which may be NULL if not needed.  If out is not NULL,
127       it places the result in the memory pointed at by out, but only if
128       outsize is sufficient (otherwise no computation is made).  If out is
129       NULL, it allocates and uses a buffer of suitable length, which will be
130       returned on success and must be freed by the caller.  In either case,
131       also on error, it assigns the number of bytes written to *outlen unless
132       outlen is NULL.
133
134       EVP_MAC_init() sets up the underlying context ctx with information
135       given via the key and params arguments.  The MAC key has a length of
136       keylen and the parameters in params are processed before setting the
137       key.  If key is NULL, the key must be set via params either as part of
138       this call or separately using EVP_MAC_CTX_set_params().  Providing non-
139       NULL params to this function is equivalent to calling
140       EVP_MAC_CTX_set_params() with those params for the same ctx beforehand.
141
142       EVP_MAC_init() should be called before EVP_MAC_update() and
143       EVP_MAC_final().
144
145       EVP_MAC_update() adds datalen bytes from data to the MAC input.
146
147       EVP_MAC_final() does the final computation and stores the result in the
148       memory pointed at by out of size outsize, and sets the number of bytes
149       written in *outl at.  If out is NULL or outsize is too small, then no
150       computation is made.  To figure out what the output length will be and
151       allocate space for it dynamically, simply call with out being NULL and
152       outl pointing at a valid location, then allocate space and make a
153       second call with out pointing at the allocated space.
154
155       EVP_MAC_finalXOF() does the final computation for an XOF based MAC and
156       stores the result in the memory pointed at by out of size outsize.
157
158       EVP_MAC_get_params() retrieves details about the implementation mac.
159       The set of parameters given with params determine exactly what
160       parameters should be retrieved.  Note that a parameter that is unknown
161       in the underlying context is simply ignored.
162
163       EVP_MAC_CTX_get_params() retrieves chosen parameters, given the context
164       ctx and its underlying context.  The set of parameters given with
165       params determine exactly what parameters should be retrieved.  Note
166       that a parameter that is unknown in the underlying context is simply
167       ignored.
168
169       EVP_MAC_CTX_set_params() passes chosen parameters to the underlying
170       context, given a context ctx.  The set of parameters given with params
171       determine exactly what parameters are passed down.  If params are NULL,
172       the unterlying context should do nothing and return 1.  Note that a
173       parameter that is unknown in the underlying context is simply ignored.
174       Also, what happens when a needed parameter isn't passed down is defined
175       by the implementation.
176
177       EVP_MAC_gettable_params() returns an OSSL_PARAM(3) array that describes
178       the retrievable and settable parameters.  EVP_MAC_gettable_params()
179       returns parameters that can be used with EVP_MAC_get_params().
180
181       EVP_MAC_gettable_ctx_params() and EVP_MAC_CTX_gettable_params() return
182       constant OSSL_PARAM(3) arrays that describe the retrievable parameters
183       that can be used with EVP_MAC_CTX_get_params().
184       EVP_MAC_gettable_ctx_params() returns the parameters that can be
185       retrieved from the algorithm, whereas EVP_MAC_CTX_gettable_params()
186       returns the parameters that can be retrieved in the context's current
187       state.
188
189       EVP_MAC_settable_ctx_params() and EVP_MAC_CTX_settable_params() return
190       constant OSSL_PARAM(3) arrays that describe the settable parameters
191       that can be used with EVP_MAC_CTX_set_params().
192       EVP_MAC_settable_ctx_params() returns the parameters that can be
193       retrieved from the algorithm, whereas EVP_MAC_CTX_settable_params()
194       returns the parameters that can be retrieved in the context's current
195       state.
196
197   Information functions
198       EVP_MAC_CTX_get_mac_size() returns the MAC output size for the given
199       context.
200
201       EVP_MAC_CTX_get_block_size() returns the MAC block size for the given
202       context.  Not all MAC algorithms support this.
203
204       EVP_MAC_is_a() checks if the given mac is an implementation of an
205       algorithm that's identifiable with name.
206
207       EVP_MAC_get0_provider() returns the provider that holds the
208       implementation of the given mac.
209
210       EVP_MAC_do_all_provided() traverses all MAC implemented by all
211       activated providers in the given library context libctx, and for each
212       of the implementations, calls the given function fn with the
213       implementation method and the given arg as argument.
214
215       EVP_MAC_get0_name() return the name of the given MAC.  For fetched MACs
216       with multiple names, only one of them is returned; it's recommended to
217       use EVP_MAC_names_do_all() instead.
218
219       EVP_MAC_names_do_all() traverses all names for mac, and calls fn with
220       each name and data.
221
222       EVP_MAC_get0_description() returns a description of the mac, meant for
223       display and human consumption.  The description is at the discretion of
224       the mac implementation.
225

PARAMETERS

227       Parameters are identified by name as strings, and have an expected data
228       type and maximum size.  OpenSSL has a set of macros for parameter names
229       it expects to see in its own MAC implementations.  Here, we show all
230       three, the OpenSSL macro for the parameter name, the name in string
231       form, and a type description.
232
233       The standard parameter names are:
234
235       "key" (OSSL_MAC_PARAM_KEY) <octet string>
236           Its value is the MAC key as an array of bytes.
237
238           For MACs that use an underlying computation algorithm, the
239           algorithm must be set first, see parameter names "algorithm" below.
240
241       "iv" (OSSL_MAC_PARAM_IV) <octet string>
242           Some MAC implementations (GMAC) require an IV, this parameter sets
243           the IV.
244
245       "custom" (OSSL_MAC_PARAM_CUSTOM) <octet string>
246           Some MAC implementations (KMAC, BLAKE2) accept a Customization
247           String, this parameter sets the Customization String. The default
248           value is the empty string.
249
250       "salt" (OSSL_MAC_PARAM_SALT) <octet string>
251           This option is used by BLAKE2 MAC.
252
253       "xof" (OSSL_MAC_PARAM_XOF) <integer>
254           It's a simple flag, the value 0 or 1 are expected.
255
256           This option is used by KMAC.
257
258       "digest-noinit" (OSSL_MAC_PARAM_DIGEST_NOINIT) <integer>
259           A simple flag to set the MAC digest to not initialise the
260           implementation specific data. The value 0 or 1 is expected.
261
262           This option is used by HMAC.
263
264       "digest-oneshot" (OSSL_MAC_PARAM_DIGEST_ONESHOT) <integer>
265           A simple flag to set the MAC digest to be a oneshot operation.  The
266           value 0 or 1 is expected.
267
268           This option is used by HMAC.
269
270       "properties" (OSSL_MAC_PARAM_PROPERTIES) <UTF8 string>
271       "digest" (OSSL_MAC_PARAM_DIGEST) <UTF8 string>
272       "cipher" (OSSL_MAC_PARAM_CIPHER) <UTF8 string>
273           For MAC implementations that use an underlying computation cipher
274           or digest, these parameters set what the algorithm should be.
275
276           The value is always the name of the intended algorithm, or the
277           properties.
278
279           Note that not all algorithms may support all digests.  HMAC does
280           not support variable output length digests such as SHAKE128 or
281           SHAKE256.
282
283       "size" (OSSL_MAC_PARAM_SIZE) <unsigned integer>
284           For MAC implementations that support it, set the output size that
285           EVP_MAC_final() should produce.  The allowed sizes vary between MAC
286           implementations, but must never exceed what can be given with a
287           size_t.
288
289       "tls-data-size" (OSSL_MAC_PARAM_TLS_DATA_SIZE) <unsigned integer>
290           This parameter is only supported by HMAC. If set then special
291           handling is activated for calculating the MAC of a received mac-
292           then-encrypt TLS record where variable length record padding has
293           been used (as in the case of CBC mode ciphersuites). The value
294           represents the total length of the record that is having the MAC
295           calculated including the received MAC and the record padding.
296
297           When used EVP_MAC_update must be called precisely twice. The first
298           time with the 13 bytes of TLS "header" data, and the second time
299           with the entire record including the MAC itself and any padding.
300           The entire record length must equal the value passed in the "tls-
301           data-size" parameter. The length passed in the datalen parameter to
302           EVP_MAC_update() should be equal to the length of the record after
303           the MAC and any padding has been removed.
304
305       All these parameters should be used before the calls to any of
306       EVP_MAC_init(), EVP_MAC_update() and EVP_MAC_final() for a full
307       computation.  Anything else may give undefined results.
308

NOTES

310       The MAC life-cycle is described in life_cycle-mac(7).  In the future,
311       the transitions described there will be enforced.  When this is done,
312       it will not be considered a breaking change to the API.
313
314       The usage of the parameter names "custom", "iv" and "salt" correspond
315       to the names used in the standard where the algorithm was defined.
316

RETURN VALUES

318       EVP_MAC_fetch() returns a pointer to a newly fetched EVP_MAC, or NULL
319       if allocation failed.
320
321       EVP_MAC_up_ref() returns 1 on success, 0 on error.
322
323       EVP_MAC_names_do_all() returns 1 if the callback was called for all
324       names. A return value of 0 means that the callback was not called for
325       any names.
326
327       EVP_MAC_free() returns nothing at all.
328
329       EVP_MAC_is_a() returns 1 if the given method can be identified with the
330       given name, otherwise 0.
331
332       EVP_MAC_get0_name() returns a name of the MAC, or NULL on error.
333
334       EVP_MAC_get0_provider() returns a pointer to the provider for the MAC,
335       or NULL on error.
336
337       EVP_MAC_CTX_new() and EVP_MAC_CTX_dup() return a pointer to a newly
338       created EVP_MAC_CTX, or NULL if allocation failed.
339
340       EVP_MAC_CTX_free() returns nothing at all.
341
342       EVP_MAC_CTX_get_params() and EVP_MAC_CTX_set_params() return 1 on
343       success, 0 on error.
344
345       EVP_Q_mac() returns a pointer to the computed MAC value, or NULL on
346       error.
347
348       EVP_MAC_init(), EVP_MAC_update(), EVP_MAC_final(), and
349       EVP_MAC_finalXOF() return 1 on success, 0 on error.
350
351       EVP_MAC_CTX_get_mac_size() returns the expected output size, or 0 if it
352       isn't set.  If it isn't set, a call to EVP_MAC_init() will set it.
353
354       EVP_MAC_CTX_get_block_size() returns the block size, or 0 if it isn't
355       set.  If it isn't set, a call to EVP_MAC_init() will set it.
356
357       EVP_MAC_do_all_provided() returns nothing at all.
358

EXAMPLES

360         #include <stdlib.h>
361         #include <stdio.h>
362         #include <string.h>
363         #include <stdarg.h>
364         #include <unistd.h>
365
366         #include <openssl/evp.h>
367         #include <openssl/err.h>
368         #include <openssl/params.h>
369
370         int main() {
371             EVP_MAC *mac = EVP_MAC_fetch(NULL, getenv("MY_MAC"), NULL);
372             const char *cipher = getenv("MY_MAC_CIPHER");
373             const char *digest = getenv("MY_MAC_DIGEST");
374             const char *key = getenv("MY_KEY");
375             EVP_MAC_CTX *ctx = NULL;
376
377             unsigned char buf[4096];
378             size_t read_l;
379             size_t final_l;
380
381             size_t i;
382
383             OSSL_PARAM params[3];
384             size_t params_n = 0;
385
386             if (cipher != NULL)
387                 params[params_n++] =
388                     OSSL_PARAM_construct_utf8_string("cipher", (char*)cipher, 0);
389             if (digest != NULL)
390                 params[params_n++] =
391                     OSSL_PARAM_construct_utf8_string("digest", (char*)digest, 0);
392             params[params_n] = OSSL_PARAM_construct_end();
393
394             if (mac == NULL
395                 || key == NULL
396                 || (ctx = EVP_MAC_CTX_new(mac)) == NULL
397                 || !EVP_MAC_init(ctx, (const unsigned char *)key, strlen(key),
398                                  params))
399                 goto err;
400
401             while ( (read_l = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
402                 if (!EVP_MAC_update(ctx, buf, read_l))
403                     goto err;
404             }
405
406             if (!EVP_MAC_final(ctx, buf, &final_l, sizeof(buf)))
407                 goto err;
408
409             printf("Result: ");
410             for (i = 0; i < final_l; i++)
411                 printf("%02X", buf[i]);
412             printf("\n");
413
414             EVP_MAC_CTX_free(ctx);
415             EVP_MAC_free(mac);
416             exit(0);
417
418          err:
419             EVP_MAC_CTX_free(ctx);
420             EVP_MAC_free(mac);
421             fprintf(stderr, "Something went wrong\n");
422             ERR_print_errors_fp(stderr);
423             exit (1);
424         }
425
426       A run of this program, called with correct environment variables, can
427       look like this:
428
429         $ MY_MAC=cmac MY_KEY=secret0123456789 MY_MAC_CIPHER=aes-128-cbc \
430           LD_LIBRARY_PATH=. ./foo < foo.c
431         Result: C5C06683CD9DDEF904D754505C560A4E
432
433       (in this example, that program was stored in foo.c and compiled to
434       ./foo)
435

SEE ALSO

437       property(7) OSSL_PARAM(3), EVP_MAC-BLAKE2(7), EVP_MAC-CMAC(7),
438       EVP_MAC-GMAC(7), EVP_MAC-HMAC(7), EVP_MAC-KMAC(7), EVP_MAC-Siphash(7),
439       EVP_MAC-Poly1305(7), provider-mac(7), life_cycle-mac(7)
440

HISTORY

442       These functions were added in OpenSSL 3.0.
443
445       Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
446
447       Licensed under the Apache License 2.0 (the "License").  You may not use
448       this file except in compliance with the License.  You can obtain a copy
449       in the file LICENSE in the source distribution or at
450       <https://www.openssl.org/source/license.html>.
451
452
453
4543.0.9                             2023-07-27                    EVP_MAC(3ossl)
Impressum