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