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