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