]> git.ipfire.org Git - thirdparty/openssl.git/blame - doc/man3/EVP_MAC.pod
Make sure all BIGNUM operations work within the FIPS provider
[thirdparty/openssl.git] / doc / man3 / EVP_MAC.pod
CommitLineData
567db2c1
RL
1=pod
2
3=head1 NAME
4
5EVP_MAC, EVP_MAC_CTX, EVP_MAC_CTX_new, EVP_MAC_CTX_new_id, EVP_MAC_CTX_free,
be5fc053 6EVP_MAC_CTX_dup, EVP_MAC_CTX_mac, EVP_MAC_size, EVP_MAC_init, EVP_MAC_update,
567db2c1
RL
7EVP_MAC_final, EVP_MAC_ctrl, EVP_MAC_vctrl, EVP_MAC_ctrl_str,
8EVP_MAC_str2ctrl, EVP_MAC_hex2ctrl, EVP_MAC_nid, EVP_MAC_name,
9EVP_get_macbyname, EVP_get_macbynid, EVP_get_macbyobj - EVP MAC routines
10
11=head1 SYNOPSIS
12
13 #include <openssl/evp.h>
14
15 typedef struct evp_mac_st EVP_MAC;
16 typedef struct evp_mac_ctx_st EVP_MAC_CTX;
17
18 EVP_MAC_CTX *EVP_MAC_CTX_new(const EVP_MAC *mac);
19 EVP_MAC_CTX *EVP_MAC_CTX_new_id(int nid);
20 void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx);
be5fc053 21 EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src);
567db2c1
RL
22 const EVP_MAC *EVP_MAC_CTX_mac(EVP_MAC_CTX *ctx);
23 size_t EVP_MAC_size(EVP_MAC_CTX *ctx);
24 int EVP_MAC_init(EVP_MAC_CTX *ctx);
25 int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen);
26 int EVP_MAC_final(EVP_MAC_CTX *ctx, unsigned char *out, size_t *poutlen);
27 int EVP_MAC_ctrl(EVP_MAC_CTX *ctx, int cmd, ...);
28 int EVP_MAC_vctrl(EVP_MAC_CTX *ctx, int cmd, va_list args);
29 int EVP_MAC_ctrl_str(EVP_MAC_CTX *ctx, const char *type, const char *value);
30 int EVP_MAC_str2ctrl(EVP_MAC_CTX *ctx, int cmd, const char *value);
31 int EVP_MAC_hex2ctrl(EVP_MAC_CTX *ctx, int cmd, const char *value);
32 int EVP_MAC_nid(const EVP_MAC *mac);
33 const char *EVP_MAC_name(const EVP_MAC *mac);
34 const EVP_MAC *EVP_get_macbyname(const char *name);
35 const EVP_MAC *EVP_get_macbynid(int nid);
36 const EVP_MAC *EVP_get_macbyobj(const ASN1_OBJECT *o);
37
38=head1 DESCRIPTION
39
40These types and functions help the application to calculate MACs of
41different types and with different underlying algorithms if there are
42any.
43
44MACs are a bit complex insofar that some of them use other algorithms
45for actual computation. HMAC uses a digest, and CMAC uses a cipher.
46Therefore, there are sometimes two contexts to keep track of, one for
47the MAC algorithm itself and one for the underlying computation
48algorithm if there is one.
49
50To make things less ambiguous, this manual talks about a "context" or
51"MAC context", which is to denote the MAC level context, and about a
52"underlying context", or "computation context", which is to denote the
53context for the underlying computation algorithm if there is one.
54
55=head2 Types
56
57B<EVP_MAC> is a type that holds the implementation of a MAC.
58
59B<EVP_MAC_CTX> is a context type that holds internal MAC information
60as well as a reference to a computation context, for those MACs that
61rely on an underlying computation algorithm.
62
63=head2 Context manipulation functions
64
65EVP_MAC_CTX_new() creates a new context for the MAC type C<mac>.
66EVP_MAC_CTX_new_id() creates a new context for the numerical MAC
67identity <nid>.
68The created context can then be used with most other functions
69described here.
70
71EVP_MAC_CTX_free() frees the contents of the context, including an
72underlying context if there is one, as well as the context itself.
73B<NULL> is a valid parameter, for which this function is a no-op.
74
be5fc053
KR
75EVP_MAC_CTX_dup() duplicates the C<src> context and returns a newly allocated
76context.
567db2c1
RL
77
78EVP_MAC_CTX_mac() returns the B<EVP_MAC> associated with the context
79C<ctx>.
80
81=head2 Computing functions
82
83EVP_MAC_init() sets up the underlying context with information given
84through diverse controls.
85This should be called before calling EVP_MAC_update() and
86EVP_MAC_final().
87
567db2c1
RL
88EVP_MAC_update() adds C<datalen> bytes from C<data> to the MAC input.
89
90EVP_MAC_final() does the final computation and stores the result in
91the memory pointed at by C<out>, and sets its size in the B<size_t>
92the C<poutlen> points at.
93If C<out> is B<NULL>, then no computation is made.
94To figure out what the output length will be and allocate space for it
95dynamically, simply call with C<out> being B<NULL> and C<poutlen>
96pointing at a valid location, then allocate space and make a second
97call with C<out> pointing at the allocated space.
98
99EVP_MAC_ctrl() is used to manipulate or get information on aspects of
100the MAC which may vary depending on the MAC algorithm or its
101implementation.
102This includes the MAC key, and for MACs that use other algorithms to
103do their computation, this is also the way to tell it which one to
104use.
105This functions takes variable arguments, the exact expected arguments
106depend on C<cmd>.
107EVP_MAC_ctrl() can be called both before and after EVP_MAC_init(), but
108the effect will depend on what control is being use.
cf1698cb 109See L</CONTROLS> below for a description of standard controls.
567db2c1
RL
110
111EVP_MAC_vctrl() is the variant of EVP_MAC_ctrl() that takes a
112C<va_list> argument instead of variadic arguments.
113
114EVP_MAC_ctrl_str() is an alternative to EVP_MAC_ctrl() to control the
115MAC implementation as E<lt> C<type>, C<value> E<gt> pairs.
116The MAC implementation documentation should specify what control type
117strings are accepted.
118
119EVP_MAC_str2ctrl() and EVP_MAC_hex2ctrl() are helper functions to
120control the MAC implementation with raw strings or with strings
121containing hexadecimal numbers.
122The latter are decoded into bitstrings that are sent on to
123EVP_MAC_ctrl().
124
125=head2 Information functions
126
127EVP_MAC_size() returns the MAC output size for the given context.
128
129EVP_MAC_nid() returns the numeric identity of the given MAC implementation.
130
131EVP_MAC_name() returns the name of the given MAC implementation.
132
133=head2 Object database functions
134
135EVP_get_macbyname() fetches a MAC implementation from the object
136database by name.
137
138EVP_get_macbynid() fetches a MAC implementation from the object
139database by numeric identity.
140
141EVP_get_macbyobj() fetches a MAC implementation from the object
142database by ASN.1 OBJECT (i.e. an encoded OID).
143
144=head1 CONTROLS
145
146The standard controls are:
147
148=over 4
149
150=item B<EVP_MAC_CTRL_SET_KEY>
151
152This control expects two arguments: C<unsigned char *key>, C<size_t keylen>
153
154These will set the MAC key from the given string of the given length.
155The string may be any bitstring, and can contain NUL bytes.
156
157For MACs that use an underlying computation algorithm, the algorithm
158I<must> be set first, see B<EVP_MAC_CTRL_SET_ENGINE>,
159B<EVP_MAC_CTRL_SET_MD> and B<EVP_MAC_CTRL_SET_CIPHER> below.
160
afc580b9
P
161=item B<EVP_MAC_CTRL_SET_IV>
162
163This control expects two arguments: C<unsigned char *key>, C<size_t keylen>
164
165Some MAC implementations require an IV, this control sets the IV.
166
6e624a64
SL
167=item B<EVP_MAC_CTRL_SET_CUSTOM>
168
13b3cd7b 169This control expects two arguments: C<unsigned char *custom>, C<size_t customlen>
6e624a64 170
13b3cd7b 171Some MAC implementations (KMAC, BLAKE2) accept a Customization String,
6e624a64
SL
172this control sets the Customization String. The default value is "".
173
13b3cd7b
AS
174=item B<EVP_MAC_CTRL_SET_SALT>
175
176This control expects two arguments: C<unsigned char *salt>, C<size_t saltlen>
177
178This option is used by BLAKE2 MAC.
179
6e624a64
SL
180=item B<EVP_MAC_CTRL_SET_XOF>
181
182This control expects one argument: C<int xof>
183
184This option is used by KMAC.
185
567db2c1
RL
186=item B<EVP_MAC_CTRL_SET_FLAGS>
187
6e624a64 188This control expects one argument: C<unsigned long flags>
567db2c1
RL
189
190These will set the MAC flags to the given numbers.
191Some MACs do not support this option.
192
193=item B<EVP_MAC_CTRL_SET_ENGINE>
194
195=item B<EVP_MAC_CTRL_SET_MD>
196
197=item B<EVP_MAC_CTRL_SET_CIPHER>
198
199For MAC implementations that use an underlying computation algorithm,
200these controls set what the algorithm should be, and the engine that
201implements the algorithm if needed.
202
48fdeca0
MC
203Note that not all algorithms may support all digests. HMAC does not support
204variable output length digests such as SHAKE128 or SHAKE256.
205
567db2c1
RL
206B<EVP_MAC_CTRL_SET_ENGINE> takes one argument: C<ENGINE *>
207
208B<EVP_MAC_CTRL_SET_MD> takes one argument: C<EVP_MD *>
209
210B<EVP_MAC_CTRL_SET_CIPHER> takes one argument: C<EVP_CIPHER *>
211
212=item B<EVP_MAC_CTRL_SET_SIZE>
213
214For MAC implementations that support it, set the output size that
215EVP_MAC_final() should produce.
216The allowed sizes vary between MAC implementations.
217
218=back
219
220All these control should be used before the calls to any of
221EVP_MAC_init(), EVP_MAC_update() and EVP_MAC_final() for a full
222computation.
223Anything else may give undefined results.
224
225=head1 NOTES
226
227EVP_get_macbynid(), EVP_get_macbyobj() and EVP_MAC_name() are
228implemented as a macro.
229
230=head1 RETURN VALUES
231
be5fc053
KR
232EVP_MAC_CTX_new(), EVP_MAC_CTX_new_id() and EVP_MAC_CTX_dup() return a pointer
233to a newly created EVP_MAC_CTX, or NULL if allocation failed.
567db2c1
RL
234
235EVP_MAC_CTX_free() returns nothing at all.
236
be5fc053 237EVP_MAC_init(), EVP_MAC_update(), and EVP_MAC_final() return 1 on success, 0 on error.
567db2c1
RL
238
239EVP_MAC_ctrl(), EVP_MAC_ctrl_str(), EVP_MAC_str2ctrl() and
240EVP_MAC_hex2ctrl() return 1 on success and 0 or a negative value on
241error.
242In particular, the value -2 indicates that the given control type
243isn't supported by the MAC implementation.
244
245EVP_MAC_size() returns the expected output size, or 0 if it isn't
246set.
247If it isn't set, a call to EVP_MAC_init() should get it set.
248
249EVP_MAC_nid() returns the numeric identity for the given C<mac>.
250
251EVP_MAC_name() returns the name for the given C<mac>, if it has been
252added to the object database.
253
254EVP_add_mac() returns 1 if the given C<mac> was successfully added to
255the object database, otherwise 0.
256
257EVP_get_macbyname(), EVP_get_macbynid() and EVP_get_macbyobj() return
258the request MAC implementation, if it exists in the object database,
259otherwise B<NULL>.
260
261=head1 EXAMPLE
262
263 #include <stdlib.h>
264 #include <stdio.h>
265 #include <string.h>
266 #include <stdarg.h>
267 #include <unistd.h>
268
269 #include <openssl/evp.h>
270 #include <openssl/err.h>
271
272 int ctrl_ign_unsupported(EVP_MAC_CTX *ctx, int cmd, ...)
273 {
274 va_list args;
275 int rv;
276
277 va_start(args, cmd);
278 rv = EVP_MAC_vctrl(ctx, cmd, args);
279 va_end(args);
280
281 if (rv == -2)
282 rv = 1; /* Ignore unsupported, pretend it worked fine */
283
284 return rv;
285 }
286
287 int main() {
288 const EVP_MAC *mac =
289 EVP_get_macbyname(getenv("MY_MAC"));
290 const EVP_CIPHER *cipher =
291 EVP_get_cipherbyname(getenv("MY_MAC_CIPHER"));
292 const EVP_MD *digest =
293 EVP_get_digestbyname(getenv("MY_MAC_DIGEST"));
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
303 if (mac == NULL
304 || key == NULL
305 || (ctx = EVP_MAC_CTX_new(mac)) == NULL
306 || (cipher != NULL
307 && !ctrl_ign_unsupported(ctx, EVP_MAC_CTRL_SET_CIPHER, cipher))
308 || (digest != NULL
309 && !ctrl_ign_unsupported(ctx, EVP_MAC_CTRL_SET_MD, digest))
310 || EVP_MAC_ctrl(ctx, EVP_MAC_CTRL_SET_KEY, key, strlen(key)) <= 0)
311 goto err;
312
313 if (!EVP_MAC_init(ctx))
314 goto err;
315
316 while ( (read_l = read(STDIN_FILENO, buf, sizeof(buf))) < 0) {
317 if (!EVP_MAC_update(ctx, buf, read_l))
318 goto err;
319 }
320
321 if (!EVP_MAC_final(ctx, buf, &final_l))
322 goto err;
323
324 printf("Result: ");
325 for (i = 0; i < final_l; i++)
326 printf("%02X", buf[i]);
327 printf("\n");
328
329 EVP_MAC_CTX_free(ctx);
330 exit(0);
331
332 err:
333 EVP_MAC_CTX_free(ctx);
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
13b3cd7b 351L<EVP_MAC_BLAKE2(7)>,
6723f867 352L<EVP_MAC_CMAC(7)>,
afc580b9 353L<EVP_MAC_GMAC(7)>,
c89d9cda 354L<EVP_MAC_HMAC(7)>,
6e624a64 355L<EVP_MAC_KMAC(7)>,
c1da4b2a
PY
356L<EVP_MAC_SIPHASH(7)>,
357L<EVP_MAC_POLY1305(7)>
567db2c1 358
be5fc053
KR
359=head1 HISTORY
360
361These functions were added in OpenSSL 3.0.0.
362
567db2c1
RL
363=head1 COPYRIGHT
364
365Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
366
4746f25a 367Licensed under the Apache License 2.0 (the "License"). You may not use
567db2c1
RL
368this file except in compliance with the License. You can obtain a copy
369in the file LICENSE in the source distribution or at
370L<https://www.openssl.org/source/license.html>.
371
372=cut