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
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);
47 These types and functions help the application to calculate MACs of
48 different types and with different underlying algorithms if there are
51 MACs are a bit complex insofar that some of them use other algorithms
52 for actual computation. HMAC uses a digest, and CMAC uses a cipher.
53 Therefore, there are sometimes two contexts to keep track of, one for
54 the MAC algorithm itself and one for the underlying computation
55 algorithm if there is one.
57 To 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
60 context for the underlying computation algorithm if there is one.
64 B<EVP_MAC> is a type that holds the implementation of a MAC.
66 B<EVP_MAC_CTX> is a context type that holds internal MAC information
67 as well as a reference to a computation context, for those MACs that
68 rely on an underlying computation algorithm.
70 =head2 Algorithm implementation fetching
72 EVP_MAC_fetch() fetches an implementation of a MAC I<algorithm>, given
73 a library context I<libctx> and a set of I<properties>.
74 See L<provider(7)/Fetching algorithms> for further information.
76 The returned value must eventually be freed with
79 EVP_MAC_up_ref() increments the reference count of an already fetched
82 EVP_MAC_free() frees a fetched algorithm.
83 NULL is a valid parameter, for which this function is a no-op.
85 =head2 Context manipulation functions
87 EVP_MAC_CTX_new() creates a new context for the MAC type I<mac>.
88 The created context can then be used with most other functions
91 EVP_MAC_CTX_free() frees the contents of the context, including an
92 underlying context if there is one, as well as the context itself.
93 NULL is a valid parameter, for which this function is a no-op.
95 EVP_MAC_CTX_dup() duplicates the I<src> context and returns a newly allocated
98 EVP_MAC_CTX_mac() returns the B<EVP_MAC> associated with the context
101 =head2 Computing functions
103 EVP_MAC_init() sets up the underlying context with information given
104 through diverse controls.
105 This should be called before calling EVP_MAC_update() and
108 EVP_MAC_update() adds I<datalen> bytes from I<data> to the MAC input.
110 EVP_MAC_final() does the final computation and stores the result in
111 the memory pointed at by I<out> of size I<outsize>, and sets the number
112 of bytes written in I<*outl> at.
113 If I<out> is B<NULL> or I<outsize> is too small, then no computation
115 To figure out what the output length will be and allocate space for it
116 dynamically, simply call with I<out> being B<NULL> and I<outl>
117 pointing at a valid location, then allocate space and make a second
118 call with I<out> pointing at the allocated space.
120 EVP_MAC_get_params() retrieves details about the implementation
122 The set of parameters given with I<params> determine exactly what
123 parameters should be retrieved.
124 Note that a parameter that is unknown in the underlying context is
127 EVP_MAC_CTX_get_params() retrieves chosen parameters, given the
128 context I<ctx> and its underlying context.
129 The set of parameters given with I<params> determine exactly what
130 parameters should be retrieved.
131 Note that a parameter that is unknown in the underlying context is
134 EVP_MAC_CTX_set_params() passes chosen parameters to the underlying
135 context, given a context I<ctx>.
136 The set of parameters given with I<params> determine exactly what
137 parameters are passed down.
138 Note that a parameter that is unknown in the underlying context is
140 Also, what happens when a needed parameter isn't passed down is
141 defined by the implementation.
143 EVP_MAC_gettable_params(), EVP_MAC_CTX_gettable_params() and
144 EVP_MAC_CTX_settable_params() get a constant B<OSSL_PARAM> array that
145 decribes the retrievable and settable parameters, i.e. parameters that
146 can be used with EVP_MAC_CTX_get_params(), EVP_MAC_CTX_get_params()
147 and EVP_MAC_CTX_set_params(), respectively.
148 See L<OSSL_PARAM(3)> for the use of B<OSSL_PARAM> as parameter descriptor.
150 =head2 Information functions
152 EVP_MAC_size() returns the MAC output size for the given context.
154 EVP_MAC_name() returns the name of the given MAC implementation.
156 EVP_MAC_provider() returns the provider that holds the implementation
159 =head1 PARAMETER NAMES
161 The standard parameter names are:
165 =item OSSL_MAC_PARAM_KEY ("key") <octet string>
167 Its value is the MAC key as an array of bytes.
169 For MACs that use an underlying computation algorithm, the algorithm
170 must be set first, see parameter names "algorithm" below.
172 =item OSSL_MAC_PARAM_IV ("iv") <octet string>
174 Some MAC implementations require an IV, this parameter sets the IV.
176 =item OSSL_MAC_PARAM_CUSTOM ("custom") <octet string>
178 Some MAC implementations (KMAC, BLAKE2) accept a Customization String,
179 this parameter sets the Customization String. The default value is the
182 =item OSSL_MAC_PARAM_SALT ("salt") <octet string>
184 This option is used by BLAKE2 MAC.
186 =item OSSL_MAC_PARAM_XOF ("xof") <int>
188 It's a simple flag, the value 0 or 1 are expected.
190 This option is used by KMAC.
192 =item OSSL_MAC_PARAM_FLAGS ("flags") <int>
194 These will set the MAC flags to the given numbers.
195 Some MACs do not support this option.
197 =item OSSL_MAC_PARAM_ENGINE ("engine") <utf8string>
199 =item OSSL_MAC_PARAM_MD ("md") <utf8string>
201 =item OSSL_MAC_PARAM_DIGEST ("digest") <utf8string>
203 =item OSSL_MAC_PARAM_CIPHER ("cipher") <utf8string>
205 =item OSSL_MAC_PARAM_ALGORITHM ("algorithm") <utf8string>
207 For MAC implementations that use an underlying computation algorithm,
208 these parameters set what the algorithm should be, and the engine that
209 implements the algorithm if needed.
211 The value is always the name of the intended engine or algorithm.
213 Note that not all algorithms may support all digests.
214 HMAC does not support variable output length digests such as SHAKE128
217 Also note that OSSL_MAC_PARAM_ALGORITHM can be use generically instead
218 of OSSL_MAC_PARAM_MD, OSSL_MAC_PARAM_DIGEST or OSSL_MAC_PARAM_CIPHER,
219 and that OSSL_MAC_PARAM_MD and OSSL_MAC_PARAM_DIGEST are also interchangable.
221 =item OSSL_MAC_PARAM_SIZE <unsigned int>
223 For MAC implementations that support it, set the output size that
224 EVP_MAC_final() should produce.
225 The allowed sizes vary between MAC implementations.
229 All these parameters should be used before the calls to any of
230 EVP_MAC_init(), EVP_MAC_update() and EVP_MAC_final() for a full
232 Anything else may give undefined results.
236 EVP_MAC_fetch() returns a pointer to a newly fetched EVP_MAC, or
237 NULL if allocation failed.
239 EVP_MAC_up_ref() returns 1 on success, 0 on error.
241 EVP_MAC_free() returns nothing at all.
243 EVP_MAC_name() returns the name of the MAC, or NULL if NULL was
246 EVP_MAC_provider() returns a pointer to the provider for the MAC, or
249 EVP_MAC_CTX_new() and EVP_MAC_CTX_dup() return a pointer to a newly
250 created EVP_MAC_CTX, or NULL if allocation failed.
252 EVP_MAC_CTX_free() returns nothing at all.
254 EVP_MAC_CTX_get_params() and EVP_MAC_CTX_set_params() return 1 on
257 EVP_MAC_init(), EVP_MAC_update(), and EVP_MAC_final() return 1 on success, 0
260 EVP_MAC_size() returns the expected output size, or 0 if it isn't
262 If it isn't set, a call to EVP_MAC_init() should get it set.
273 #include <openssl/evp.h>
274 #include <openssl/err.h>
275 #include <openssl/params.h>
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");
281 const char *key = getenv("MY_KEY");
282 EVP_MAC_CTX *ctx = NULL;
284 unsigned char buf[4096];
290 OSSL_PARAM params[4];
295 OSSL_PARAM_construct_utf8_string("cipher", cipher,
296 strlen(cipher) + 1, NULL);
299 OSSL_PARAM_construct_utf8_string("digest", digest,
300 strlen(digest) + 1, NULL);
302 OSSL_PARAM_construct_octet_string("key", key, strlen(key), NULL);
303 params[params_n] = OSSL_PARAM_construct_end();
307 || (ctx = EVP_MAC_CTX_new(mac)) == NULL
308 || EVP_MAC_CTX_set_params(ctx, params) <= 0)
311 if (!EVP_MAC_init(ctx))
314 while ( (read_l = read(STDIN_FILENO, buf, sizeof(buf))) < 0) {
315 if (!EVP_MAC_update(ctx, buf, read_l))
319 if (!EVP_MAC_final(ctx, buf, &final_l))
323 for (i = 0; i < final_l; i++)
324 printf("%02X", buf[i]);
327 EVP_MAC_CTX_free(ctx);
332 EVP_MAC_CTX_free(ctx);
334 fprintf(stderr, "Something went wrong\n");
335 ERR_print_errors_fp(stderr);
339 A run of this program, called with correct environment variables, can
342 $ MY_MAC=cmac MY_KEY=secret0123456789 MY_MAC_CIPHER=aes-128-cbc \
343 LD_LIBRARY_PATH=. ./foo < foo.c
344 Result: ECCAAFF041B22A2299EB90A1B53B6D45
346 (in this example, that program was stored in F<foo.c> and compiled to
353 L<EVP_MAC_BLAKE2(7)>,
358 L<EVP_MAC_SIPHASH(7)>,
359 L<EVP_MAC_POLY1305(7)>
363 These functions were added in OpenSSL 3.0.
367 Copyright 2018-2019 The OpenSSL Project Authors. All Rights Reserved.
369 Licensed under the Apache License 2.0 (the "License"). You may not use
370 this file except in compliance with the License. You can obtain a copy
371 in the file LICENSE in the source distribution or at
372 L<https://www.openssl.org/source/license.html>.