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