ed38f912df447383edf7cfc8fb9a64745e54eb78
[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_name,
6 EVP_MAC_provider, EVP_MAC_get_params, EVP_MAC_gettable_params,
7 EVP_MAC_CTX, EVP_MAC_CTX_new, EVP_MAC_CTX_free, EVP_MAC_CTX_dup,
8 EVP_MAC_CTX_mac, EVP_MAC_CTX_get_params, EVP_MAC_CTX_set_params,
9 EVP_MAC_size, EVP_MAC_init, EVP_MAC_update, EVP_MAC_final,
10 EVP_MAC_CTX_gettable_params, EVP_MAC_CTX_settable_params
11 - EVP MAC routines
12
13 =head1 SYNOPSIS
14
15  #include <openssl/evp.h>
16
17  typedef struct evp_mac_st EVP_MAC;
18  typedef struct evp_mac_ctx_st EVP_MAC_CTX;
19
20  EVP_MAC *EVP_MAC_fetch(OPENSSL_CTX *libctx, const char *algorithm,
21                         const char *properties);
22  int EVP_MAC_up_ref(EVP_MAC *mac);
23  void EVP_MAC_free(EVP_MAC *mac);
24  const char *EVP_MAC_name(const EVP_MAC *mac);
25  const OSSL_PROVIDER *EVP_MAC_provider(const EVP_MAC *mac);
26  int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[]);
27
28  EVP_MAC_CTX *EVP_MAC_CTX_new(EVP_MAC *mac);
29  void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx);
30  EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src);
31  EVP_MAC *EVP_MAC_CTX_mac(EVP_MAC_CTX *ctx);
32  int EVP_MAC_CTX_get_params(EVP_MAC_CTX *ctx, OSSL_PARAM params[]);
33  int EVP_MAC_CTX_set_params(EVP_MAC_CTX *ctx, const OSSL_PARAM params[]);
34
35  size_t EVP_MAC_size(EVP_MAC_CTX *ctx);
36  int EVP_MAC_init(EVP_MAC_CTX *ctx);
37  int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen);
38  int EVP_MAC_final(EVP_MAC_CTX *ctx,
39                    unsigned char *out, size_t *outl, size_t outsize);
40
41  const OSSL_PARAM *EVP_MAC_gettable_params(const EVP_MAC *mac);
42  const OSSL_PARAM *EVP_MAC_CTX_gettable_params(const EVP_MAC *mac);
43  const OSSL_PARAM *EVP_MAC_CTX_settable_params(const EVP_MAC *mac);
44
45 =head1 DESCRIPTION
46
47 These types and functions help the application to calculate MACs of
48 different types and with different underlying algorithms if there are
49 any.
50
51 MACs are a bit complex insofar that some of them use other algorithms
52 for actual computation.  HMAC uses a digest, and CMAC uses a cipher.
53 Therefore, there are sometimes two contexts to keep track of, one for
54 the MAC algorithm itself and one for the underlying computation
55 algorithm if there is one.
56
57 To make things less ambiguous, this manual talks about a "context" or
58 "MAC context", which is to denote the MAC level context, and about a
59 "underlying context", or "computation context", which is to denote the
60 context for the underlying computation algorithm if there is one.
61
62 =head2 Types
63
64 B<EVP_MAC> is a type that holds the implementation of a MAC.
65
66 B<EVP_MAC_CTX> is a context type that holds internal MAC information
67 as well as a reference to a computation context, for those MACs that
68 rely on an underlying computation algorithm.
69
70 =head2 Algorithm implementation fetching
71
72 EVP_MAC_fetch() fetches an implementation of a MAC I<algorithm>, given
73 a library context I<libctx> and a set of I<properties>.
74 See L<provider(7)/Fetching algorithms> for further information.
75
76 The returned value must eventually be freed with
77 L<EVP_MAC_free(3)>.
78
79 EVP_MAC_up_ref() increments the reference count of an already fetched
80 MAC.
81
82 EVP_MAC_free() frees a fetched algorithm.
83 NULL is a valid parameter, for which this function is a no-op.
84
85 =head2 Context manipulation functions
86
87 EVP_MAC_CTX_new() creates a new context for the MAC type I<mac>.
88 The created context can then be used with most other functions
89 described here.
90
91 EVP_MAC_CTX_free() frees the contents of the context, including an
92 underlying context if there is one, as well as the context itself.
93 NULL is a valid parameter, for which this function is a no-op.
94
95 EVP_MAC_CTX_dup() duplicates the I<src> context and returns a newly allocated
96 context.
97
98 EVP_MAC_CTX_mac() returns the B<EVP_MAC> associated with the context
99 I<ctx>.
100
101 =head2 Computing functions
102
103 EVP_MAC_init() sets up the underlying context with information given
104 through diverse controls.
105 This should be called before calling EVP_MAC_update() and
106 EVP_MAC_final().
107
108 EVP_MAC_update() adds I<datalen> bytes from I<data> to the MAC input.
109
110 EVP_MAC_final() does the final computation and stores the result in
111 the memory pointed at by I<out> of size I<outsize>, and sets the number
112 of bytes written in I<*outl> at.
113 If I<out> is B<NULL> or I<outsize> is too small, then no computation
114 is made.
115 To figure out what the output length will be and allocate space for it
116 dynamically, simply call with I<out> being B<NULL> and I<outl>
117 pointing at a valid location, then allocate space and make a second
118 call with I<out> pointing at the allocated space.
119
120 EVP_MAC_get_params() retrieves details about the implementation
121 I<mac>.
122 The set of parameters given with I<params> determine exactly what
123 parameters should be retrieved.
124 Note that a parameter that is unknown in the underlying context is
125 simply ignored.
126
127 EVP_MAC_CTX_get_params() retrieves chosen parameters, given the
128 context I<ctx> and its underlying context.
129 The set of parameters given with I<params> determine exactly what
130 parameters should be retrieved.
131 Note that a parameter that is unknown in the underlying context is
132 simply ignored.
133
134 EVP_MAC_CTX_set_params() passes chosen parameters to the underlying
135 context, given a context I<ctx>.
136 The set of parameters given with I<params> determine exactly what
137 parameters are passed down.
138 Note that a parameter that is unknown in the underlying context is
139 simply ignored.
140 Also, what happens when a needed parameter isn't passed down is
141 defined by the implementation.
142
143 EVP_MAC_gettable_params(), EVP_MAC_CTX_gettable_params() and
144 EVP_MAC_CTX_settable_params() get a constant B<OSSL_PARAM> array that
145 decribes the retrievable and settable parameters, i.e. parameters that
146 can be used with EVP_MAC_CTX_get_params(), EVP_MAC_CTX_get_params()
147 and EVP_MAC_CTX_set_params(), respectively.
148 See L<OSSL_PARAM(3)> for the use of B<OSSL_PARAM> as parameter descriptor.
149
150 =head2 Information functions
151
152 EVP_MAC_size() returns the MAC output size for the given context.
153
154 EVP_MAC_name() returns the name of the given MAC implementation.
155
156 EVP_MAC_provider() returns the provider that holds the implementation
157 of the given I<mac>.
158
159 =head1 PARAMETER NAMES
160
161 The standard parameter names are:
162
163 =over 4
164
165 =item OSSL_MAC_PARAM_KEY ("key") <octet string>
166
167 Its value is the MAC key as an array of bytes.
168
169 For MACs that use an underlying computation algorithm, the algorithm
170 must be set first, see parameter names "algorithm" below.
171
172 =item OSSL_MAC_PARAM_IV ("iv") <octet string>
173
174 Some MAC implementations require an IV, this parameter sets the IV.
175
176 =item OSSL_MAC_PARAM_CUSTOM ("custom") <octet string>
177
178 Some MAC implementations (KMAC, BLAKE2) accept a Customization String,
179 this parameter sets the Customization String. The default value is the
180 empty string.
181
182 =item OSSL_MAC_PARAM_SALT ("salt") <octet string>
183
184 This option is used by BLAKE2 MAC.
185
186 =item OSSL_MAC_PARAM_XOF ("xof") <int>
187
188 It's a simple flag, the value 0 or 1 are expected.
189
190 This option is used by KMAC.
191
192 =item OSSL_MAC_PARAM_FLAGS ("flags") <int>
193
194 These will set the MAC flags to the given numbers.
195 Some MACs do not support this option.
196
197 =item OSSL_MAC_PARAM_ENGINE ("engine") <utf8string>
198
199 =item OSSL_MAC_PARAM_MD ("md") <utf8string>
200
201 =item OSSL_MAC_PARAM_DIGEST ("digest") <utf8string>
202
203 =item OSSL_MAC_PARAM_CIPHER ("cipher") <utf8string>
204
205 =item OSSL_MAC_PARAM_ALGORITHM ("algorithm") <utf8string>
206
207 For MAC implementations that use an underlying computation algorithm,
208 these parameters set what the algorithm should be, and the engine that
209 implements the algorithm if needed.
210
211 The value is always the name of the intended engine or algorithm.
212
213 Note that not all algorithms may support all digests.
214 HMAC does not support variable output length digests such as SHAKE128
215 or SHAKE256.
216
217 Also note that OSSL_MAC_PARAM_ALGORITHM can be use generically instead
218 of OSSL_MAC_PARAM_MD, OSSL_MAC_PARAM_DIGEST or OSSL_MAC_PARAM_CIPHER,
219 and that OSSL_MAC_PARAM_MD and OSSL_MAC_PARAM_DIGEST are also interchangable.
220
221 =item OSSL_MAC_PARAM_SIZE <unsigned int>
222
223 For MAC implementations that support it, set the output size that
224 EVP_MAC_final() should produce.
225 The allowed sizes vary between MAC implementations.
226
227 =back
228
229 All these parameters should be used before the calls to any of
230 EVP_MAC_init(), EVP_MAC_update() and EVP_MAC_final() for a full
231 computation.
232 Anything else may give undefined results.
233
234 =head1 RETURN VALUES
235
236 EVP_MAC_fetch() returns a pointer to a newly fetched EVP_MAC, or
237 NULL if allocation failed.
238
239 EVP_MAC_up_ref() returns 1 on success, 0 on error.
240
241 EVP_MAC_free() returns nothing at all.
242
243 EVP_MAC_name() returns the name of the MAC, or NULL if NULL was
244 passed.
245
246 EVP_MAC_provider() returns a pointer to the provider for the MAC, or
247 NULL on error.
248
249 EVP_MAC_CTX_new() and EVP_MAC_CTX_dup() return a pointer to a newly
250 created EVP_MAC_CTX, or NULL if allocation failed.
251
252 EVP_MAC_CTX_free() returns nothing at all.
253
254 EVP_MAC_CTX_get_params() and EVP_MAC_CTX_set_params() return 1 on
255 success, 0 on error.
256
257 EVP_MAC_init(), EVP_MAC_update(), and EVP_MAC_final() return 1 on success, 0
258 on error.
259
260 EVP_MAC_size() returns the expected output size, or 0 if it isn't
261 set.
262 If it isn't set, a call to EVP_MAC_init() should get it set.
263
264
265 =head1 EXAMPLE
266
267   #include <stdlib.h>
268   #include <stdio.h>
269   #include <string.h>
270   #include <stdarg.h>
271   #include <unistd.h>
272
273   #include <openssl/evp.h>
274   #include <openssl/err.h>
275   #include <openssl/params.h>
276
277   int main() {
278       EVP_MAC *mac = EVP_MAC_fetch(NULL, getenv("MY_MAC"), NULL);
279       const char *cipher = getenv("MY_MAC_CIPHER");
280       const char *digest = getenv("MY_MAC_DIGEST");
281       const char *key = getenv("MY_KEY");
282       EVP_MAC_CTX *ctx = NULL;
283
284       unsigned char buf[4096];
285       ssize_t read_l;
286       size_t final_l;
287
288       size_t i;
289
290       OSSL_PARAM params[4];
291       size_t params_n = 0;
292
293       if (cipher != NULL)
294           params[params_n++] =
295               OSSL_PARAM_construct_utf8_string("cipher", cipher,
296                                                strlen(cipher) + 1, NULL);
297       if (digest != NULL)
298           params[params_n++] =
299               OSSL_PARAM_construct_utf8_string("digest", digest,
300                                                strlen(digest) + 1, NULL);
301       params[params_n++] =
302           OSSL_PARAM_construct_octet_string("key", key, strlen(key), NULL);
303       params[params_n] = OSSL_PARAM_construct_end();
304
305       if (mac == NULL
306           || key == NULL
307           || (ctx = EVP_MAC_CTX_new(mac)) == NULL
308           || EVP_MAC_CTX_set_params(ctx, params) <= 0)
309           goto err;
310
311       if (!EVP_MAC_init(ctx))
312           goto err;
313
314       while ( (read_l = read(STDIN_FILENO, buf, sizeof(buf))) < 0) {
315           if (!EVP_MAC_update(ctx, buf, read_l))
316               goto err;
317       }
318
319       if (!EVP_MAC_final(ctx, buf, &final_l))
320           goto err;
321
322       printf("Result: ");
323       for (i = 0; i < final_l; i++)
324           printf("%02X", buf[i]);
325       printf("\n");
326
327       EVP_MAC_CTX_free(ctx);
328       EVP_MAC_free(mac);
329       exit(0);
330
331    err:
332       EVP_MAC_CTX_free(ctx);
333       EVP_MAC_free(mac);
334       fprintf(stderr, "Something went wrong\n");
335       ERR_print_errors_fp(stderr);
336       exit (1);
337   }
338
339 A run of this program, called with correct environment variables, can
340 look like this:
341
342   $ MY_MAC=cmac MY_KEY=secret0123456789 MY_MAC_CIPHER=aes-128-cbc \
343     LD_LIBRARY_PATH=. ./foo < foo.c
344   Result: ECCAAFF041B22A2299EB90A1B53B6D45
345
346 (in this example, that program was stored in F<foo.c> and compiled to
347 F<./foo>)
348
349 =head1 SEE ALSO
350
351 L<property(7)>
352 L<OSSL_PARAM(3)>,
353 L<EVP_MAC_BLAKE2(7)>,
354 L<EVP_MAC_CMAC(7)>,
355 L<EVP_MAC_GMAC(7)>,
356 L<EVP_MAC_HMAC(7)>,
357 L<EVP_MAC_KMAC(7)>,
358 L<EVP_MAC_SIPHASH(7)>,
359 L<EVP_MAC_POLY1305(7)>
360
361 =head1 HISTORY
362
363 These functions were added in OpenSSL 3.0.
364
365 =head1 COPYRIGHT
366
367 Copyright 2018-2019 The OpenSSL Project Authors. All Rights Reserved.
368
369 Licensed under the Apache License 2.0 (the "License").  You may not use
370 this file except in compliance with the License.  You can obtain a copy
371 in the file LICENSE in the source distribution or at
372 L<https://www.openssl.org/source/license.html>.
373
374 =cut