5 EVP_MAC, EVP_MAC_fetch, EVP_MAC_up_ref, EVP_MAC_free,
6 EVP_MAC_is_a, EVP_MAC_name,
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_size, EVP_MAC_init, EVP_MAC_update, EVP_MAC_final,
11 EVP_MAC_CTX_gettable_params, EVP_MAC_CTX_settable_params,
12 EVP_MAC_do_all_ex - EVP MAC routines
16 #include <openssl/evp.h>
18 typedef struct evp_mac_st EVP_MAC;
19 typedef struct evp_mac_ctx_st EVP_MAC_CTX;
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);
25 int EVP_MAC_is_a(const EVP_MAC *mac, const char *name);
26 const char *EVP_MAC_name(const EVP_MAC *mac);
27 const OSSL_PROVIDER *EVP_MAC_provider(const EVP_MAC *mac);
28 int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[]);
30 EVP_MAC_CTX *EVP_MAC_CTX_new(EVP_MAC *mac);
31 void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx);
32 EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src);
33 EVP_MAC *EVP_MAC_CTX_mac(EVP_MAC_CTX *ctx);
34 int EVP_MAC_CTX_get_params(EVP_MAC_CTX *ctx, OSSL_PARAM params[]);
35 int EVP_MAC_CTX_set_params(EVP_MAC_CTX *ctx, const OSSL_PARAM params[]);
37 size_t EVP_MAC_size(EVP_MAC_CTX *ctx);
38 int EVP_MAC_init(EVP_MAC_CTX *ctx);
39 int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen);
40 int EVP_MAC_final(EVP_MAC_CTX *ctx,
41 unsigned char *out, size_t *outl, size_t outsize);
43 const OSSL_PARAM *EVP_MAC_gettable_params(const EVP_MAC *mac);
44 const OSSL_PARAM *EVP_MAC_CTX_gettable_params(const EVP_MAC *mac);
45 const OSSL_PARAM *EVP_MAC_CTX_settable_params(const EVP_MAC *mac);
47 void EVP_MAC_do_all_ex(OPENSSL_CTX *libctx,
48 void (*fn)(EVP_MAC *mac, void *arg),
53 These types and functions help the application to calculate MACs of
54 different types and with different underlying algorithms if there are
57 MACs are a bit complex insofar that some of them use other algorithms
58 for actual computation. HMAC uses a digest, and CMAC uses a cipher.
59 Therefore, there are sometimes two contexts to keep track of, one for
60 the MAC algorithm itself and one for the underlying computation
61 algorithm if there is one.
63 To make things less ambiguous, this manual talks about a "context" or
64 "MAC context", which is to denote the MAC level context, and about a
65 "underlying context", or "computation context", which is to denote the
66 context for the underlying computation algorithm if there is one.
70 B<EVP_MAC> is a type that holds the implementation of a MAC.
72 B<EVP_MAC_CTX> is a context type that holds internal MAC information
73 as well as a reference to a computation context, for those MACs that
74 rely on an underlying computation algorithm.
76 =head2 Algorithm implementation fetching
78 EVP_MAC_fetch() fetches an implementation of a MAC I<algorithm>, given
79 a library context I<libctx> and a set of I<properties>.
80 See L<provider(7)/Fetching algorithms> for further information.
82 The returned value must eventually be freed with
85 EVP_MAC_up_ref() increments the reference count of an already fetched
88 EVP_MAC_free() frees a fetched algorithm.
89 NULL is a valid parameter, for which this function is a no-op.
91 =head2 Context manipulation functions
93 EVP_MAC_CTX_new() creates a new context for the MAC type I<mac>.
94 The created context can then be used with most other functions
97 EVP_MAC_CTX_free() frees the contents of the context, including an
98 underlying context if there is one, as well as the context itself.
99 NULL is a valid parameter, for which this function is a no-op.
101 EVP_MAC_CTX_dup() duplicates the I<src> context and returns a newly allocated
104 EVP_MAC_CTX_mac() returns the B<EVP_MAC> associated with the context
107 =head2 Computing functions
109 EVP_MAC_init() sets up the underlying context with information given
110 through diverse controls.
111 This should be called before calling EVP_MAC_update() and
114 EVP_MAC_update() adds I<datalen> bytes from I<data> to the MAC input.
116 EVP_MAC_final() does the final computation and stores the result in
117 the memory pointed at by I<out> of size I<outsize>, and sets the number
118 of bytes written in I<*outl> at.
119 If I<out> is B<NULL> or I<outsize> is too small, then no computation
121 To figure out what the output length will be and allocate space for it
122 dynamically, simply call with I<out> being B<NULL> and I<outl>
123 pointing at a valid location, then allocate space and make a second
124 call with I<out> pointing at the allocated space.
126 EVP_MAC_get_params() retrieves details about the implementation
128 The set of parameters given with I<params> determine exactly what
129 parameters should be retrieved.
130 Note that a parameter that is unknown in the underlying context is
133 EVP_MAC_CTX_get_params() retrieves chosen parameters, given the
134 context I<ctx> and its underlying context.
135 The set of parameters given with I<params> determine exactly what
136 parameters should be retrieved.
137 Note that a parameter that is unknown in the underlying context is
140 EVP_MAC_CTX_set_params() passes chosen parameters to the underlying
141 context, given a context I<ctx>.
142 The set of parameters given with I<params> determine exactly what
143 parameters are passed down.
144 Note that a parameter that is unknown in the underlying context is
146 Also, what happens when a needed parameter isn't passed down is
147 defined by the implementation.
149 EVP_MAC_gettable_params(), EVP_MAC_CTX_gettable_params() and
150 EVP_MAC_CTX_settable_params() get a constant B<OSSL_PARAM> array that
151 decribes the retrievable and settable parameters, i.e. parameters that
152 can be used with EVP_MAC_get_params(), EVP_MAC_CTX_get_params()
153 and EVP_MAC_CTX_set_params(), respectively.
154 See L<OSSL_PARAM(3)> for the use of B<OSSL_PARAM> as parameter descriptor.
156 =head2 Information functions
158 EVP_MAC_size() returns the MAC output size for the given context.
160 EVP_MAC_name() returns the name of the given MAC implementation.
162 EVP_MAC_is_a() checks if the given I<mac> is an implementation of an
163 algorithm that's identifiable with I<name>.
165 EVP_MAC_provider() returns the provider that holds the implementation
168 EVP_MAC_do_all_ex() traverses all MAC implemented by all activated
169 providers in the given library context I<libctx>, and for each of the
170 implementations, calls the given function I<fn> with the implementation method
171 and the given I<arg> as argument.
175 Parameters are identified by name as strings, and have an expected
176 data type and maximum size.
177 OpenSSL has a set of macros for parameter names it expects to see in
178 its own MAC implementations.
179 Here, we show all three, the OpenSSL macro for the parameter name, the
180 name in string form, and a type description.
182 The standard parameter names are:
186 =item B<OSSL_MAC_PARAM_KEY> ("key") <octet string>
188 Its value is the MAC key as an array of bytes.
190 For MACs that use an underlying computation algorithm, the algorithm
191 must be set first, see parameter names "algorithm" below.
193 =item B<OSSL_MAC_PARAM_IV> ("iv") <octet string>
195 Some MAC implementations require an IV, this parameter sets the IV.
197 =item B<OSSL_MAC_PARAM_CUSTOM> ("custom") <octet string>
199 Some MAC implementations (KMAC, BLAKE2) accept a Customization String,
200 this parameter sets the Customization String. The default value is the
203 =item B<OSSL_MAC_PARAM_SALT> ("salt") <octet string>
205 This option is used by BLAKE2 MAC.
207 =item B<OSSL_MAC_PARAM_XOF> ("xof") <integer>
209 It's a simple flag, the value 0 or 1 are expected.
211 This option is used by KMAC.
213 =item B<OSSL_MAC_PARAM_FLAGS> ("flags") <integer>
215 These will set the MAC flags to the given numbers.
216 Some MACs do not support this option.
218 =item B<OSSL_MAC_PARAM_PROPERTIES> ("properties") <UTF8 string>
220 =item B<OSSL_MAC_PARAM_DIGEST> ("digest") <UTF8 string>
222 =item B<OSSL_MAC_PARAM_CIPHER> ("cipher") <UTF8 string>
224 For MAC implementations that use an underlying computation cipher or
225 digest, these parameters set what the algorithm should be.
227 The value is always the name of the intended algorithm,
230 Note that not all algorithms may support all digests.
231 HMAC does not support variable output length digests such as SHAKE128
234 =item B<OSSL_MAC_PARAM_SIZE> ("size") <unsigned integer>
236 For MAC implementations that support it, set the output size that
237 EVP_MAC_final() should produce.
238 The allowed sizes vary between MAC implementations, but must never exceed
239 what can be given with a B<size_t>.
243 All these parameters should be used before the calls to any of
244 EVP_MAC_init(), EVP_MAC_update() and EVP_MAC_final() for a full
246 Anything else may give undefined results.
250 EVP_MAC_fetch() returns a pointer to a newly fetched EVP_MAC, or
251 NULL if allocation failed.
253 EVP_MAC_up_ref() returns 1 on success, 0 on error.
255 EVP_MAC_free() returns nothing at all.
257 EVP_MAC_name() returns the name of the MAC, or NULL if NULL was
260 EVP_MAC_is_a() returns 1 if the given method can be identified with
261 the given name, otherwise 0.
263 EVP_MAC_provider() returns a pointer to the provider for the MAC, or
266 EVP_MAC_CTX_new() and EVP_MAC_CTX_dup() return a pointer to a newly
267 created EVP_MAC_CTX, or NULL if allocation failed.
269 EVP_MAC_CTX_free() returns nothing at all.
271 EVP_MAC_CTX_get_params() and EVP_MAC_CTX_set_params() return 1 on
274 EVP_MAC_init(), EVP_MAC_update(), and EVP_MAC_final() return 1 on success, 0
277 EVP_MAC_size() returns the expected output size, or 0 if it isn't
279 If it isn't set, a call to EVP_MAC_init() should get it set.
281 EVP_MAC_do_all_ex() returns nothing at all.
291 #include <openssl/evp.h>
292 #include <openssl/err.h>
293 #include <openssl/params.h>
296 EVP_MAC *mac = EVP_MAC_fetch(NULL, getenv("MY_MAC"), NULL);
297 const char *cipher = getenv("MY_MAC_CIPHER");
298 const char *digest = getenv("MY_MAC_DIGEST");
299 const char *key = getenv("MY_KEY");
300 EVP_MAC_CTX *ctx = NULL;
302 unsigned char buf[4096];
308 OSSL_PARAM params[4];
313 OSSL_PARAM_construct_utf8_string("cipher", cipher, 0, NULL);
316 OSSL_PARAM_construct_utf8_string("digest", digest, 0, NULL);
318 OSSL_PARAM_construct_octet_string("key", key, strlen(key), NULL);
319 params[params_n] = OSSL_PARAM_construct_end();
323 || (ctx = EVP_MAC_CTX_new(mac)) == NULL
324 || EVP_MAC_CTX_set_params(ctx, params) <= 0)
327 if (!EVP_MAC_init(ctx))
330 while ( (read_l = read(STDIN_FILENO, buf, sizeof(buf))) < 0) {
331 if (!EVP_MAC_update(ctx, buf, read_l))
335 if (!EVP_MAC_final(ctx, buf, &final_l))
339 for (i = 0; i < final_l; i++)
340 printf("%02X", buf[i]);
343 EVP_MAC_CTX_free(ctx);
348 EVP_MAC_CTX_free(ctx);
350 fprintf(stderr, "Something went wrong\n");
351 ERR_print_errors_fp(stderr);
355 A run of this program, called with correct environment variables, can
358 $ MY_MAC=cmac MY_KEY=secret0123456789 MY_MAC_CIPHER=aes-128-cbc \
359 LD_LIBRARY_PATH=. ./foo < foo.c
360 Result: ECCAAFF041B22A2299EB90A1B53B6D45
362 (in this example, that program was stored in F<foo.c> and compiled to
369 L<EVP_MAC_BLAKE2(7)>,
374 L<EVP_MAC_SIPHASH(7)>,
375 L<EVP_MAC_POLY1305(7)>
379 These functions were added in OpenSSL 3.0.
383 Copyright 2018-2019 The OpenSSL Project Authors. All Rights Reserved.
385 Licensed under the Apache License 2.0 (the "License"). You may not use
386 this file except in compliance with the License. You can obtain a copy
387 in the file LICENSE in the source distribution or at
388 L<https://www.openssl.org/source/license.html>.