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