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