]> git.ipfire.org Git - thirdparty/openssl.git/blame - doc/man3/EVP_MAC.pod
Add EVP_MAC_provider()
[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,
10EVP_MAC_CTX_gettable_params, EVP_MAC_CTX_settable_params
11- 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
RL
44
45=head1 DESCRIPTION
46
47These types and functions help the application to calculate MACs of
48different types and with different underlying algorithms if there are
49any.
50
51MACs are a bit complex insofar that some of them use other algorithms
52for actual computation. HMAC uses a digest, and CMAC uses a cipher.
53Therefore, there are sometimes two contexts to keep track of, one for
54the MAC algorithm itself and one for the underlying computation
55algorithm if there is one.
56
57To 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
60context for the underlying computation algorithm if there is one.
61
62=head2 Types
63
64B<EVP_MAC> is a type that holds the implementation of a MAC.
65
66B<EVP_MAC_CTX> is a context type that holds internal MAC information
67as well as a reference to a computation context, for those MACs that
68rely on an underlying computation algorithm.
69
e74bd290
RL
70=head2 Algorithm implementation fetching
71
72EVP_MAC_fetch() fetches an implementation of a MAC I<algorithm>, given
73a library context I<libctx> and a set of I<properties>.
74See L<provider(7)/Fetching algorithms> for further information.
75
76The returned value must eventually be freed with
77L<EVP_MAC_free(3)>.
78
79EVP_MAC_up_ref() increments the reference count of an already fetched
80MAC.
81
82EVP_MAC_free() frees a fetched algorithm.
83NULL is a valid parameter, for which this function is a no-op.
84
567db2c1
RL
85=head2 Context manipulation functions
86
e74bd290 87EVP_MAC_CTX_new() creates a new context for the MAC type I<mac>.
567db2c1
RL
88The created context can then be used with most other functions
89described here.
90
91EVP_MAC_CTX_free() frees the contents of the context, including an
92underlying context if there is one, as well as the context itself.
e74bd290 93NULL is a valid parameter, for which this function is a no-op.
567db2c1 94
e74bd290 95EVP_MAC_CTX_dup() duplicates the I<src> context and returns a newly allocated
be5fc053 96context.
567db2c1
RL
97
98EVP_MAC_CTX_mac() returns the B<EVP_MAC> associated with the context
e74bd290 99I<ctx>.
567db2c1
RL
100
101=head2 Computing functions
102
103EVP_MAC_init() sets up the underlying context with information given
104through diverse controls.
105This should be called before calling EVP_MAC_update() and
106EVP_MAC_final().
107
e74bd290 108EVP_MAC_update() adds I<datalen> bytes from I<data> to the MAC input.
567db2c1
RL
109
110EVP_MAC_final() does the final computation and stores the result in
e74bd290
RL
111the memory pointed at by I<out> of size I<outsize>, and sets the number
112of bytes written in I<*outl> at.
113If I<out> is B<NULL> or I<outsize> is too small, then no computation
114is made.
567db2c1 115To figure out what the output length will be and allocate space for it
e74bd290 116dynamically, simply call with I<out> being B<NULL> and I<outl>
567db2c1 117pointing at a valid location, then allocate space and make a second
e74bd290
RL
118call with I<out> pointing at the allocated space.
119
120EVP_MAC_get_params() retrieves details about the implementation
121I<mac>.
122The set of parameters given with I<params> determine exactly what
123parameters should be retrieved.
124Note that a parameter that is unknown in the underlying context is
125simply ignored.
126
127EVP_MAC_CTX_get_params() retrieves chosen parameters, given the
128context I<ctx> and its underlying context.
129The set of parameters given with I<params> determine exactly what
130parameters should be retrieved.
131Note that a parameter that is unknown in the underlying context is
132simply ignored.
133
134EVP_MAC_CTX_set_params() passes chosen parameters to the underlying
135context, given a context I<ctx>.
136The set of parameters given with I<params> determine exactly what
137parameters are passed down.
138Note that a parameter that is unknown in the underlying context is
139simply ignored.
140Also, what happens when a needed parameter isn't passed down is
141defined by the implementation.
142
143EVP_MAC_gettable_params(), EVP_MAC_CTX_gettable_params() and
144EVP_MAC_CTX_settable_params() get a constant B<OSSL_PARAM> array that
145decribes the retrievable and settable parameters, i.e. parameters that
146can be used with EVP_MAC_CTX_get_params(), EVP_MAC_CTX_get_params()
147and EVP_MAC_CTX_set_params(), respectively.
148See L<OSSL_PARAM(3)> for the use of B<OSSL_PARAM> as parameter descriptor.
567db2c1
RL
149
150=head2 Information functions
151
152EVP_MAC_size() returns the MAC output size for the given context.
153
567db2c1
RL
154EVP_MAC_name() returns the name of the given MAC implementation.
155
7dd0f299
RL
156EVP_MAC_provider() returns the provider that holds the implementation
157of the given I<mac>.
158
e74bd290 159=head1 PARAMETER NAMES
567db2c1 160
e74bd290 161The standard parameter names are:
567db2c1
RL
162
163=over 4
164
e74bd290 165=item OSSL_MAC_PARAM_KEY ("key") <octet string>
567db2c1 166
e74bd290 167Its value is the MAC key as an array of bytes.
567db2c1
RL
168
169For MACs that use an underlying computation algorithm, the algorithm
e74bd290 170must be set first, see parameter names "algorithm" below.
afc580b9 171
e74bd290 172=item OSSL_MAC_PARAM_IV ("iv") <octet string>
afc580b9 173
e74bd290 174Some MAC implementations require an IV, this parameter sets the IV.
6e624a64 175
e74bd290 176=item OSSL_MAC_PARAM_CUSTOM ("custom") <octet string>
6e624a64 177
13b3cd7b 178Some MAC implementations (KMAC, BLAKE2) accept a Customization String,
e74bd290
RL
179this parameter sets the Customization String. The default value is the
180empty string.
6e624a64 181
e74bd290 182=item OSSL_MAC_PARAM_SALT ("salt") <octet string>
13b3cd7b
AS
183
184This option is used by BLAKE2 MAC.
185
e74bd290 186=item OSSL_MAC_PARAM_XOF ("xof") <int>
6e624a64 187
e74bd290 188It's a simple flag, the value 0 or 1 are expected.
6e624a64
SL
189
190This option is used by KMAC.
191
e74bd290 192=item OSSL_MAC_PARAM_FLAGS ("flags") <int>
567db2c1
RL
193
194These will set the MAC flags to the given numbers.
195Some MACs do not support this option.
196
e74bd290
RL
197=item OSSL_MAC_PARAM_ENGINE ("engine") <utf8string>
198
199=item OSSL_MAC_PARAM_MD ("md") <utf8string>
567db2c1 200
e74bd290 201=item OSSL_MAC_PARAM_DIGEST ("digest") <utf8string>
567db2c1 202
e74bd290
RL
203=item OSSL_MAC_PARAM_CIPHER ("cipher") <utf8string>
204
205=item OSSL_MAC_PARAM_ALGORITHM ("algorithm") <utf8string>
567db2c1
RL
206
207For MAC implementations that use an underlying computation algorithm,
e74bd290 208these parameters set what the algorithm should be, and the engine that
567db2c1
RL
209implements the algorithm if needed.
210
e74bd290 211The value is always the name of the intended engine or algorithm.
567db2c1 212
e74bd290
RL
213Note that not all algorithms may support all digests.
214HMAC does not support variable output length digests such as SHAKE128
215or SHAKE256.
567db2c1 216
e74bd290
RL
217Also note that OSSL_MAC_PARAM_ALGORITHM can be use generically instead
218of OSSL_MAC_PARAM_MD, OSSL_MAC_PARAM_DIGEST or OSSL_MAC_PARAM_CIPHER,
219and that OSSL_MAC_PARAM_MD and OSSL_MAC_PARAM_DIGEST are also interchangable.
567db2c1 220
e74bd290 221=item OSSL_MAC_PARAM_SIZE <unsigned int>
567db2c1
RL
222
223For MAC implementations that support it, set the output size that
224EVP_MAC_final() should produce.
225The allowed sizes vary between MAC implementations.
226
227=back
228
e74bd290 229All these parameters should be used before the calls to any of
567db2c1
RL
230EVP_MAC_init(), EVP_MAC_update() and EVP_MAC_final() for a full
231computation.
232Anything else may give undefined results.
233
e74bd290 234=head1 RETURN VALUES
567db2c1 235
e74bd290
RL
236EVP_MAC_fetch() returns a pointer to a newly fetched EVP_MAC, or
237NULL if allocation failed.
567db2c1 238
e74bd290
RL
239EVP_MAC_up_ref() returns 1 on success, 0 on error.
240
241EVP_MAC_free() returns nothing at all.
242
243EVP_MAC_name() returns the name of the MAC, or NULL if NULL was
244passed.
567db2c1 245
7dd0f299
RL
246EVP_MAC_provider() returns a pointer to the provider for the MAC, or
247NULL on error.
248
e74bd290
RL
249EVP_MAC_CTX_new() and EVP_MAC_CTX_dup() return a pointer to a newly
250created EVP_MAC_CTX, or NULL if allocation failed.
567db2c1
RL
251
252EVP_MAC_CTX_free() returns nothing at all.
253
e74bd290
RL
254EVP_MAC_CTX_get_params() and EVP_MAC_CTX_set_params() return 1 on
255success, 0 on error.
567db2c1 256
e74bd290
RL
257EVP_MAC_init(), EVP_MAC_update(), and EVP_MAC_final() return 1 on success, 0
258on error.
567db2c1
RL
259
260EVP_MAC_size() returns the expected output size, or 0 if it isn't
261set.
262If it isn't set, a call to EVP_MAC_init() should get it set.
263
567db2c1
RL
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>
e74bd290 275 #include <openssl/params.h>
567db2c1
RL
276
277 int main() {
e74bd290
RL
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");
567db2c1
RL
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
e74bd290
RL
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
567db2c1
RL
305 if (mac == NULL
306 || key == NULL
307 || (ctx = EVP_MAC_CTX_new(mac)) == NULL
e74bd290 308 || EVP_MAC_CTX_set_params(ctx, params) <= 0)
567db2c1
RL
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);
e74bd290 328 EVP_MAC_free(mac);
567db2c1
RL
329 exit(0);
330
331 err:
332 EVP_MAC_CTX_free(ctx);
e74bd290 333 EVP_MAC_free(mac);
567db2c1
RL
334 fprintf(stderr, "Something went wrong\n");
335 ERR_print_errors_fp(stderr);
336 exit (1);
337 }
338
339A run of this program, called with correct environment variables, can
340look 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
347F<./foo>)
348
349=head1 SEE ALSO
350
e74bd290
RL
351L<property(7)>
352L<OSSL_PARAM(3)>,
13b3cd7b 353L<EVP_MAC_BLAKE2(7)>,
6723f867 354L<EVP_MAC_CMAC(7)>,
afc580b9 355L<EVP_MAC_GMAC(7)>,
c89d9cda 356L<EVP_MAC_HMAC(7)>,
6e624a64 357L<EVP_MAC_KMAC(7)>,
c1da4b2a
PY
358L<EVP_MAC_SIPHASH(7)>,
359L<EVP_MAC_POLY1305(7)>
567db2c1 360
be5fc053
KR
361=head1 HISTORY
362
4674aaf4 363These functions were added in OpenSSL 3.0.
be5fc053 364
567db2c1
RL
365=head1 COPYRIGHT
366
e74bd290 367Copyright 2018-2019 The OpenSSL Project Authors. All Rights Reserved.
567db2c1 368
4746f25a 369Licensed under the Apache License 2.0 (the "License"). You may not use
567db2c1
RL
370this file except in compliance with the License. You can obtain a copy
371in the file LICENSE in the source distribution or at
372L<https://www.openssl.org/source/license.html>.
373
374=cut