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