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