5 EVP_MAC, EVP_MAC_fetch, EVP_MAC_up_ref, EVP_MAC_free, EVP_MAC_name,
6 EVP_MAC_provider, EVP_MAC_get_params, EVP_MAC_gettable_params,
7 EVP_MAC_CTX, EVP_MAC_CTX_new, EVP_MAC_CTX_free, EVP_MAC_CTX_dup,
8 EVP_MAC_CTX_mac, EVP_MAC_CTX_get_params, EVP_MAC_CTX_set_params,
9 EVP_MAC_size, EVP_MAC_init, EVP_MAC_update, EVP_MAC_final,
10 EVP_MAC_CTX_gettable_params, EVP_MAC_CTX_settable_params,
11 EVP_MAC_do_all_ex - EVP MAC routines
15 #include <openssl/evp.h>
17 typedef struct evp_mac_st EVP_MAC;
18 typedef struct evp_mac_ctx_st EVP_MAC_CTX;
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);
25 const OSSL_PROVIDER *EVP_MAC_provider(const EVP_MAC *mac);
26 int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[]);
28 EVP_MAC_CTX *EVP_MAC_CTX_new(EVP_MAC *mac);
29 void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx);
30 EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src);
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[]);
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);
38 int EVP_MAC_final(EVP_MAC_CTX *ctx,
39 unsigned char *out, size_t *outl, size_t outsize);
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);
45 void EVP_MAC_do_all_ex(OPENSSL_CTX *libctx,
46 void (*fn)(EVP_MAC *mac, void *arg),
51 These types and functions help the application to calculate MACs of
52 different types and with different underlying algorithms if there are
55 MACs are a bit complex insofar that some of them use other algorithms
56 for actual computation. HMAC uses a digest, and CMAC uses a cipher.
57 Therefore, there are sometimes two contexts to keep track of, one for
58 the MAC algorithm itself and one for the underlying computation
59 algorithm if there is one.
61 To 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
64 context for the underlying computation algorithm if there is one.
68 B<EVP_MAC> is a type that holds the implementation of a MAC.
70 B<EVP_MAC_CTX> is a context type that holds internal MAC information
71 as well as a reference to a computation context, for those MACs that
72 rely on an underlying computation algorithm.
74 =head2 Algorithm implementation fetching
76 EVP_MAC_fetch() fetches an implementation of a MAC I<algorithm>, given
77 a library context I<libctx> and a set of I<properties>.
78 See L<provider(7)/Fetching algorithms> for further information.
80 The returned value must eventually be freed with
83 EVP_MAC_up_ref() increments the reference count of an already fetched
86 EVP_MAC_free() frees a fetched algorithm.
87 NULL is a valid parameter, for which this function is a no-op.
89 =head2 Context manipulation functions
91 EVP_MAC_CTX_new() creates a new context for the MAC type I<mac>.
92 The created context can then be used with most other functions
95 EVP_MAC_CTX_free() frees the contents of the context, including an
96 underlying context if there is one, as well as the context itself.
97 NULL is a valid parameter, for which this function is a no-op.
99 EVP_MAC_CTX_dup() duplicates the I<src> context and returns a newly allocated
102 EVP_MAC_CTX_mac() returns the B<EVP_MAC> associated with the context
105 =head2 Computing functions
107 EVP_MAC_init() sets up the underlying context with information given
108 through diverse controls.
109 This should be called before calling EVP_MAC_update() and
112 EVP_MAC_update() adds I<datalen> bytes from I<data> to the MAC input.
114 EVP_MAC_final() does the final computation and stores the result in
115 the memory pointed at by I<out> of size I<outsize>, and sets the number
116 of bytes written in I<*outl> at.
117 If I<out> is B<NULL> or I<outsize> is too small, then no computation
119 To figure out what the output length will be and allocate space for it
120 dynamically, simply call with I<out> being B<NULL> and I<outl>
121 pointing at a valid location, then allocate space and make a second
122 call with I<out> pointing at the allocated space.
124 EVP_MAC_get_params() retrieves details about the implementation
126 The set of parameters given with I<params> determine exactly what
127 parameters should be retrieved.
128 Note that a parameter that is unknown in the underlying context is
131 EVP_MAC_CTX_get_params() retrieves chosen parameters, given the
132 context I<ctx> and its underlying context.
133 The set of parameters given with I<params> determine exactly what
134 parameters should be retrieved.
135 Note that a parameter that is unknown in the underlying context is
138 EVP_MAC_CTX_set_params() passes chosen parameters to the underlying
139 context, given a context I<ctx>.
140 The set of parameters given with I<params> determine exactly what
141 parameters are passed down.
142 Note that a parameter that is unknown in the underlying context is
144 Also, what happens when a needed parameter isn't passed down is
145 defined by the implementation.
147 EVP_MAC_gettable_params(), EVP_MAC_CTX_gettable_params() and
148 EVP_MAC_CTX_settable_params() get a constant B<OSSL_PARAM> array that
149 decribes the retrievable and settable parameters, i.e. parameters that
150 can be used with EVP_MAC_get_params(), EVP_MAC_CTX_get_params()
151 and EVP_MAC_CTX_set_params(), respectively.
152 See L<OSSL_PARAM(3)> for the use of B<OSSL_PARAM> as parameter descriptor.
154 =head2 Information functions
156 EVP_MAC_size() returns the MAC output size for the given context.
158 EVP_MAC_name() returns the name of the given MAC implementation.
160 EVP_MAC_provider() returns the provider that holds the implementation
163 EVP_MAC_do_all_ex() traverses all MAC implemented by all activated
164 providers in the given library context I<libctx>, and for each of the
165 implementations, calls the given function I<fn> with the implementation method
166 and the given I<arg> as argument.
168 =head1 PARAMETER NAMES
170 Parameters are identified by name as strings, and have an expected
171 data type and maximum size.
172 OpenSSL has a set of macros for parameter names it expects to see in
173 its own MAC implementations.
174 Here, we show all three, the OpenSSL macro for the parameter name, the
175 name in string form, and a type description.
177 The standard parameter names are:
181 =item B<OSSL_MAC_PARAM_KEY> ("key") <octet string>
183 Its value is the MAC key as an array of bytes.
185 For MACs that use an underlying computation algorithm, the algorithm
186 must be set first, see parameter names "algorithm" below.
188 =item B<OSSL_MAC_PARAM_IV> ("iv") <octet string>
190 Some MAC implementations require an IV, this parameter sets the IV.
192 =item B<OSSL_MAC_PARAM_CUSTOM> ("custom") <octet string>
194 Some MAC implementations (KMAC, BLAKE2) accept a Customization String,
195 this parameter sets the Customization String. The default value is the
198 =item B<OSSL_MAC_PARAM_SALT> ("salt") <octet string>
200 This option is used by BLAKE2 MAC.
202 =item B<OSSL_MAC_PARAM_XOF> ("xof") <integer>
204 It's a simple flag, the value 0 or 1 are expected.
206 This option is used by KMAC.
208 =item B<OSSL_MAC_PARAM_FLAGS> ("flags") <integer>
210 These will set the MAC flags to the given numbers.
211 Some MACs do not support this option.
213 =item B<OSSL_MAC_PARAM_ENGINE> ("engine") <utf8 string>
215 =item B<OSSL_MAC_PARAM_PROPERTIES> ("properties") <utf8 string>
217 =item B<OSSL_MAC_PARAM_DIGEST> ("digest") <utf8 string>
219 =item B<OSSL_MAC_PARAM_CIPHER> ("cipher") <utf8 string>
221 For MAC implementations that use an underlying computation cipher or
222 digest, these parameters set what the algorithm should be, and the
223 engine that implements the algorithm or the properties to fetch it
226 The value is always the name of the intended engine, algorithm,
229 Note that not all algorithms may support all digests.
230 HMAC does not support variable output length digests such as SHAKE128
233 =item B<OSSL_MAC_PARAM_SIZE> ("size") <unsigned integer>
235 For MAC implementations that support it, set the output size that
236 EVP_MAC_final() should produce.
237 The allowed sizes vary between MAC implementations.
241 All these parameters should be used before the calls to any of
242 EVP_MAC_init(), EVP_MAC_update() and EVP_MAC_final() for a full
244 Anything else may give undefined results.
248 EVP_MAC_fetch() returns a pointer to a newly fetched EVP_MAC, or
249 NULL if allocation failed.
251 EVP_MAC_up_ref() returns 1 on success, 0 on error.
253 EVP_MAC_free() returns nothing at all.
255 EVP_MAC_name() returns the name of the MAC, or NULL if NULL was
258 EVP_MAC_provider() returns a pointer to the provider for the MAC, or
261 EVP_MAC_CTX_new() and EVP_MAC_CTX_dup() return a pointer to a newly
262 created EVP_MAC_CTX, or NULL if allocation failed.
264 EVP_MAC_CTX_free() returns nothing at all.
266 EVP_MAC_CTX_get_params() and EVP_MAC_CTX_set_params() return 1 on
269 EVP_MAC_init(), EVP_MAC_update(), and EVP_MAC_final() return 1 on success, 0
272 EVP_MAC_size() returns the expected output size, or 0 if it isn't
274 If it isn't set, a call to EVP_MAC_init() should get it set.
276 EVP_MAC_do_all_ex() returns nothing at all.
286 #include <openssl/evp.h>
287 #include <openssl/err.h>
288 #include <openssl/params.h>
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");
294 const char *key = getenv("MY_KEY");
295 EVP_MAC_CTX *ctx = NULL;
297 unsigned char buf[4096];
303 OSSL_PARAM params[4];
308 OSSL_PARAM_construct_utf8_string("cipher", cipher, 0, NULL);
311 OSSL_PARAM_construct_utf8_string("digest", digest, 0, NULL);
313 OSSL_PARAM_construct_octet_string("key", key, strlen(key), NULL);
314 params[params_n] = OSSL_PARAM_construct_end();
318 || (ctx = EVP_MAC_CTX_new(mac)) == NULL
319 || EVP_MAC_CTX_set_params(ctx, params) <= 0)
322 if (!EVP_MAC_init(ctx))
325 while ( (read_l = read(STDIN_FILENO, buf, sizeof(buf))) < 0) {
326 if (!EVP_MAC_update(ctx, buf, read_l))
330 if (!EVP_MAC_final(ctx, buf, &final_l))
334 for (i = 0; i < final_l; i++)
335 printf("%02X", buf[i]);
338 EVP_MAC_CTX_free(ctx);
343 EVP_MAC_CTX_free(ctx);
345 fprintf(stderr, "Something went wrong\n");
346 ERR_print_errors_fp(stderr);
350 A run of this program, called with correct environment variables, can
353 $ MY_MAC=cmac MY_KEY=secret0123456789 MY_MAC_CIPHER=aes-128-cbc \
354 LD_LIBRARY_PATH=. ./foo < foo.c
355 Result: ECCAAFF041B22A2299EB90A1B53B6D45
357 (in this example, that program was stored in F<foo.c> and compiled to
364 L<EVP_MAC_BLAKE2(7)>,
369 L<EVP_MAC_SIPHASH(7)>,
370 L<EVP_MAC_POLY1305(7)>
374 These functions were added in OpenSSL 3.0.
378 Copyright 2018-2019 The OpenSSL Project Authors. All Rights Reserved.
380 Licensed under the Apache License 2.0 (the "License"). You may not use
381 this file except in compliance with the License. You can obtain a copy
382 in the file LICENSE in the source distribution or at
383 L<https://www.openssl.org/source/license.html>.