5 EVP_MAC, EVP_MAC_fetch, EVP_MAC_up_ref, EVP_MAC_free,
6 EVP_MAC_is_a, EVP_MAC_number, EVP_MAC_name, EVP_MAC_names_do_all,
7 EVP_MAC_provider, EVP_MAC_get_params, EVP_MAC_gettable_params,
8 EVP_MAC_CTX, EVP_MAC_CTX_new, EVP_MAC_CTX_free, EVP_MAC_CTX_dup,
9 EVP_MAC_CTX_mac, EVP_MAC_CTX_get_params, EVP_MAC_CTX_set_params,
10 EVP_MAC_CTX_get_mac_size, EVP_MAC_init, EVP_MAC_update, EVP_MAC_final,
11 EVP_MAC_gettable_ctx_params, EVP_MAC_settable_ctx_params,
12 EVP_MAC_CTX_gettable_params, EVP_MAC_CTX_settable_params,
13 EVP_MAC_do_all_provided - EVP MAC routines
17 #include <openssl/evp.h>
19 typedef struct evp_mac_st EVP_MAC;
20 typedef struct evp_mac_ctx_st EVP_MAC_CTX;
22 EVP_MAC *EVP_MAC_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
23 const char *properties);
24 int EVP_MAC_up_ref(EVP_MAC *mac);
25 void EVP_MAC_free(EVP_MAC *mac);
26 int EVP_MAC_is_a(const EVP_MAC *mac, const char *name);
27 int EVP_MAC_number(const EVP_MAC *mac);
28 const char *EVP_MAC_name(const EVP_MAC *mac);
29 int EVP_MAC_names_do_all(const EVP_MAC *mac,
30 void (*fn)(const char *name, void *data),
32 const OSSL_PROVIDER *EVP_MAC_provider(const EVP_MAC *mac);
33 int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[]);
35 EVP_MAC_CTX *EVP_MAC_CTX_new(EVP_MAC *mac);
36 void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx);
37 EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src);
38 EVP_MAC *EVP_MAC_CTX_mac(EVP_MAC_CTX *ctx);
39 int EVP_MAC_CTX_get_params(EVP_MAC_CTX *ctx, OSSL_PARAM params[]);
40 int EVP_MAC_CTX_set_params(EVP_MAC_CTX *ctx, const OSSL_PARAM params[]);
42 size_t EVP_MAC_CTX_get_mac_size(EVP_MAC_CTX *ctx);
43 int EVP_MAC_init(EVP_MAC_CTX *ctx);
44 int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen);
45 int EVP_MAC_final(EVP_MAC_CTX *ctx,
46 unsigned char *out, size_t *outl, size_t outsize);
48 const OSSL_PARAM *EVP_MAC_gettable_params(const EVP_MAC *mac);
49 const OSSL_PARAM *EVP_MAC_gettable_ctx_params(const EVP_MAC *mac);
50 const OSSL_PARAM *EVP_MAC_settable_ctx_params(const EVP_MAC *mac);
51 const OSSL_PARAM *EVP_MAC_CTX_gettable_params(EVP_MAC_CTX *ctx);
52 const OSSL_PARAM *EVP_MAC_CTX_settable_params(EVP_MAC_CTX *ctx);
54 void EVP_MAC_do_all_provided(OSSL_LIB_CTX *libctx,
55 void (*fn)(EVP_MAC *mac, void *arg),
60 These types and functions help the application to calculate MACs of
61 different types and with different underlying algorithms if there are
64 MACs are a bit complex insofar that some of them use other algorithms
65 for actual computation. HMAC uses a digest, and CMAC uses a cipher.
66 Therefore, there are sometimes two contexts to keep track of, one for
67 the MAC algorithm itself and one for the underlying computation
68 algorithm if there is one.
70 To make things less ambiguous, this manual talks about a "context" or
71 "MAC context", which is to denote the MAC level context, and about a
72 "underlying context", or "computation context", which is to denote the
73 context for the underlying computation algorithm if there is one.
77 B<EVP_MAC> is a type that holds the implementation of a MAC.
79 B<EVP_MAC_CTX> is a context type that holds internal MAC information
80 as well as a reference to a computation context, for those MACs that
81 rely on an underlying computation algorithm.
83 =head2 Algorithm implementation fetching
85 EVP_MAC_fetch() fetches an implementation of a MAC I<algorithm>, given
86 a library context I<libctx> and a set of I<properties>.
87 See L<provider(7)/Fetching algorithms> for further information.
89 See L<OSSL_PROVIDER-default(7)/Message Authentication Code (MAC)> for the list
90 of algorithms supported by the default provider.
92 The returned value must eventually be freed with
95 EVP_MAC_up_ref() increments the reference count of an already fetched
98 EVP_MAC_free() frees a fetched algorithm.
99 NULL is a valid parameter, for which this function is a no-op.
101 =head2 Context manipulation functions
103 EVP_MAC_CTX_new() creates a new context for the MAC type I<mac>.
104 The created context can then be used with most other functions
107 EVP_MAC_CTX_free() frees the contents of the context, including an
108 underlying context if there is one, as well as the context itself.
109 NULL is a valid parameter, for which this function is a no-op.
111 EVP_MAC_CTX_dup() duplicates the I<src> context and returns a newly allocated
114 EVP_MAC_CTX_mac() returns the B<EVP_MAC> associated with the context
117 =head2 Computing functions
119 EVP_MAC_init() sets up the underlying context with information given
120 through diverse controls.
121 This should be called before calling EVP_MAC_update() and
124 EVP_MAC_update() adds I<datalen> bytes from I<data> to the MAC input.
126 EVP_MAC_final() does the final computation and stores the result in
127 the memory pointed at by I<out> of size I<outsize>, and sets the number
128 of bytes written in I<*outl> at.
129 If I<out> is NULL or I<outsize> is too small, then no computation
131 To figure out what the output length will be and allocate space for it
132 dynamically, simply call with I<out> being NULL and I<outl>
133 pointing at a valid location, then allocate space and make a second
134 call with I<out> pointing at the allocated space.
136 EVP_MAC_get_params() retrieves details about the implementation
138 The set of parameters given with I<params> determine exactly what
139 parameters should be retrieved.
140 Note that a parameter that is unknown in the underlying context is
143 EVP_MAC_CTX_get_params() retrieves chosen parameters, given the
144 context I<ctx> and its underlying context.
145 The set of parameters given with I<params> determine exactly what
146 parameters should be retrieved.
147 Note that a parameter that is unknown in the underlying context is
150 EVP_MAC_CTX_set_params() passes chosen parameters to the underlying
151 context, given a context I<ctx>.
152 The set of parameters given with I<params> determine exactly what
153 parameters are passed down.
154 Note that a parameter that is unknown in the underlying context is
156 Also, what happens when a needed parameter isn't passed down is
157 defined by the implementation.
159 EVP_MAC_gettable_params() returns an B<OSSL_PARAM> array that describes
160 the retrievable and settable parameters. EVP_MAC_gettable_params()
161 returns parameters that can be used with EVP_MAC_get_params().
162 See L<OSSL_PARAM(3)> for the use of B<OSSL_PARAM> as a parameter descriptor.
164 EVP_MAC_gettable_ctx_params() and EVP_MAC_CTX_gettable_params()
165 return constant B<OSSL_PARAM> arrays that describe the retrievable
166 parameters that can be used with EVP_MAC_CTX_get_params().
167 EVP_MAC_gettable_ctx_params() returns the parameters that can be retrieved
168 from the algorithm, whereas EVP_MAC_CTX_gettable_params() returns
169 the parameters that can be retrieved in the context's current state.
170 See L<OSSL_PARAM(3)> for the use of B<OSSL_PARAM> as a parameter descriptor.
172 EVP_MAC_settable_ctx_params() and EVP_MAC_CTX_settable_params() return
173 constant B<OSSL_PARAM> arrays that describe the settable parameters that
174 can be used with EVP_MAC_CTX_set_params(). EVP_MAC_settable_ctx_params()
175 returns the parameters that can be retrieved from the algorithm,
176 whereas EVP_MAC_CTX_settable_params() returns the parameters that can
177 be retrieved in the context's current state. See L<OSSL_PARAM(3)>
178 for the use of B<OSSL_PARAM> as a parameter descriptor.
180 =head2 Information functions
182 EVP_MAC_CTX_get_mac_size() returns the MAC output size for the given context.
184 EVP_MAC_is_a() checks if the given I<mac> is an implementation of an
185 algorithm that's identifiable with I<name>.
187 EVP_MAC_provider() returns the provider that holds the implementation
190 EVP_MAC_do_all_provided() traverses all MAC implemented by all activated
191 providers in the given library context I<libctx>, and for each of the
192 implementations, calls the given function I<fn> with the implementation method
193 and the given I<arg> as argument.
195 EVP_MAC_number() returns the internal dynamic number assigned to
198 EVP_MAC_name() return the name of the given MAC. For fetched MACs
199 with multiple names, only one of them is returned; it's
200 recommended to use EVP_MAC_names_do_all() instead.
202 EVP_MAC_names_do_all() traverses all names for I<mac>, and calls
203 I<fn> with each name and I<data>.
207 Parameters are identified by name as strings, and have an expected
208 data type and maximum size.
209 OpenSSL has a set of macros for parameter names it expects to see in
210 its own MAC implementations.
211 Here, we show all three, the OpenSSL macro for the parameter name, the
212 name in string form, and a type description.
214 The standard parameter names are:
218 =item "key" (B<OSSL_MAC_PARAM_KEY>) <octet string>
220 Its value is the MAC key as an array of bytes.
222 For MACs that use an underlying computation algorithm, the algorithm
223 must be set first, see parameter names "algorithm" below.
225 =item "iv" (B<OSSL_MAC_PARAM_IV>) <octet string>
227 Some MAC implementations require an IV, this parameter sets the IV.
229 =item "custom" (B<OSSL_MAC_PARAM_CUSTOM>) <octet string>
231 Some MAC implementations (KMAC, BLAKE2) accept a Customization String,
232 this parameter sets the Customization String. The default value is the
235 =item "salt" (B<OSSL_MAC_PARAM_SALT>) <octet string>
237 This option is used by BLAKE2 MAC.
239 =item "xof" (B<OSSL_MAC_PARAM_XOF>) <integer>
241 It's a simple flag, the value 0 or 1 are expected.
243 This option is used by KMAC.
245 =item "digest-noinit" (B<OSSL_MAC_PARAM_DIGEST_NOINIT>) <integer>
247 A simple flag to set the MAC digest to not initialise the
248 implementation specific data. The value 0 or 1 is expected.
250 This option is used by HMAC.
252 =item "digest-oneshot" (B<OSSL_MAC_PARAM_DIGEST_ONESHOT>) <integer>
254 A simple flag to set the MAC digest to be a oneshot operation.
255 The value 0 or 1 is expected.
257 This option is used by HMAC.
259 =item "properties" (B<OSSL_MAC_PARAM_PROPERTIES>) <UTF8 string>
261 =item "digest" (B<OSSL_MAC_PARAM_DIGEST>) <UTF8 string>
263 =item "cipher" (B<OSSL_MAC_PARAM_CIPHER>) <UTF8 string>
265 For MAC implementations that use an underlying computation cipher or
266 digest, these parameters set what the algorithm should be.
268 The value is always the name of the intended algorithm,
271 Note that not all algorithms may support all digests.
272 HMAC does not support variable output length digests such as SHAKE128
275 =item "size" (B<OSSL_MAC_PARAM_SIZE>) <unsigned integer>
277 For MAC implementations that support it, set the output size that
278 EVP_MAC_final() should produce.
279 The allowed sizes vary between MAC implementations, but must never exceed
280 what can be given with a B<size_t>.
282 =item "tls-data-size" (B<OSSL_MAC_PARAM_TLS_DATA_SIZE>) <unsigned integer>
284 This parameter is only supported by HMAC. If set then special handling is
285 activated for calculating the MAC of a received mac-then-encrypt TLS record
286 where variable length record padding has been used (as in the case of CBC mode
287 ciphersuites). The value represents the total length of the record that is
288 having the MAC calculated including the received MAC and the record padding.
290 When used EVP_MAC_update must be called precisely twice. The first time with
291 the 13 bytes of TLS "header" data, and the second time with the entire record
292 including the MAC itself and any padding. The entire record length must equal
293 the value passed in the "tls-data-size" parameter. The length passed in the
294 B<datalen> parameter to EVP_MAC_update() should be equal to the length of the
295 record after the MAC and any padding has been removed.
299 All these parameters should be used before the calls to any of
300 EVP_MAC_init(), EVP_MAC_update() and EVP_MAC_final() for a full
302 Anything else may give undefined results.
306 EVP_MAC_fetch() returns a pointer to a newly fetched EVP_MAC, or
307 NULL if allocation failed.
309 EVP_MAC_up_ref() returns 1 on success, 0 on error.
311 EVP_MAC_names_do_all() returns 1 if the callback was called for all names. A
312 return value of 0 means that the callback was not called for any names.
314 EVP_MAC_free() returns nothing at all.
316 EVP_MAC_is_a() returns 1 if the given method can be identified with
317 the given name, otherwise 0.
319 EVP_MAC_name() returns a name of the MAC, or NULL on error.
321 EVP_MAC_provider() returns a pointer to the provider for the MAC, or
324 EVP_MAC_CTX_new() and EVP_MAC_CTX_dup() return a pointer to a newly
325 created EVP_MAC_CTX, or NULL if allocation failed.
327 EVP_MAC_CTX_free() returns nothing at all.
329 EVP_MAC_CTX_get_params() and EVP_MAC_CTX_set_params() return 1 on
332 EVP_MAC_init(), EVP_MAC_update(), and EVP_MAC_final() return 1 on success, 0
335 EVP_MAC_CTX_get_mac_size() returns the expected output size, or 0 if it isn't set.
336 If it isn't set, a call to EVP_MAC_init() should get it set.
338 EVP_MAC_do_all_provided() returns nothing at all.
348 #include <openssl/evp.h>
349 #include <openssl/err.h>
350 #include <openssl/params.h>
353 EVP_MAC *mac = EVP_MAC_fetch(NULL, getenv("MY_MAC"), NULL);
354 const char *cipher = getenv("MY_MAC_CIPHER");
355 const char *digest = getenv("MY_MAC_DIGEST");
356 const char *key = getenv("MY_KEY");
357 EVP_MAC_CTX *ctx = NULL;
359 unsigned char buf[4096];
365 OSSL_PARAM params[4];
370 OSSL_PARAM_construct_utf8_string("cipher", (char*)cipher, 0);
373 OSSL_PARAM_construct_utf8_string("digest", (char*)digest, 0);
375 OSSL_PARAM_construct_octet_string("key", (void*)key, strlen(key));
376 params[params_n] = OSSL_PARAM_construct_end();
380 || (ctx = EVP_MAC_CTX_new(mac)) == NULL
381 || EVP_MAC_CTX_set_params(ctx, params) <= 0)
384 if (!EVP_MAC_init(ctx))
387 while ( (read_l = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
388 if (!EVP_MAC_update(ctx, buf, read_l))
392 if (!EVP_MAC_final(ctx, buf, &final_l, sizeof(buf)))
396 for (i = 0; i < final_l; i++)
397 printf("%02X", buf[i]);
400 EVP_MAC_CTX_free(ctx);
405 EVP_MAC_CTX_free(ctx);
407 fprintf(stderr, "Something went wrong\n");
408 ERR_print_errors_fp(stderr);
412 A run of this program, called with correct environment variables, can
415 $ MY_MAC=cmac MY_KEY=secret0123456789 MY_MAC_CIPHER=aes-128-cbc \
416 LD_LIBRARY_PATH=. ./foo < foo.c
417 Result: C5C06683CD9DDEF904D754505C560A4E
419 (in this example, that program was stored in F<foo.c> and compiled to
426 L<EVP_MAC-BLAKE2(7)>,
431 L<EVP_MAC-Siphash(7)>,
432 L<EVP_MAC-Poly1305(7)>
436 These functions were added in OpenSSL 3.0.
440 Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
442 Licensed under the Apache License 2.0 (the "License"). You may not use
443 this file except in compliance with the License. You can obtain a copy
444 in the file LICENSE in the source distribution or at
445 L<https://www.openssl.org/source/license.html>.