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