]>
Commit | Line | Data |
---|---|---|
567db2c1 RL |
1 | =pod |
2 | ||
3 | =head1 NAME | |
4 | ||
e74bd290 | 5 | EVP_MAC, EVP_MAC_fetch, EVP_MAC_up_ref, EVP_MAC_free, EVP_MAC_name, |
7dd0f299 | 6 | EVP_MAC_provider, EVP_MAC_get_params, EVP_MAC_gettable_params, |
e74bd290 RL |
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 routines | |
567db2c1 RL |
12 | |
13 | =head1 SYNOPSIS | |
14 | ||
15 | #include <openssl/evp.h> | |
16 | ||
17 | typedef struct evp_mac_st EVP_MAC; | |
18 | typedef struct evp_mac_ctx_st EVP_MAC_CTX; | |
19 | ||
e74bd290 RL |
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); | |
7dd0f299 | 25 | const OSSL_PROVIDER *EVP_MAC_provider(const EVP_MAC *mac); |
e74bd290 RL |
26 | int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[]); |
27 | ||
28 | EVP_MAC_CTX *EVP_MAC_CTX_new(EVP_MAC *mac); | |
567db2c1 | 29 | void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx); |
be5fc053 | 30 | EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src); |
e74bd290 RL |
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[]); | |
34 | ||
567db2c1 RL |
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); | |
e74bd290 RL |
38 | int EVP_MAC_final(EVP_MAC_CTX *ctx, |
39 | unsigned char *out, size_t *outl, size_t outsize); | |
40 | ||
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); | |
567db2c1 RL |
44 | |
45 | =head1 DESCRIPTION | |
46 | ||
47 | These types and functions help the application to calculate MACs of | |
48 | different types and with different underlying algorithms if there are | |
49 | any. | |
50 | ||
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. | |
56 | ||
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. | |
61 | ||
62 | =head2 Types | |
63 | ||
64 | B<EVP_MAC> is a type that holds the implementation of a MAC. | |
65 | ||
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. | |
69 | ||
e74bd290 RL |
70 | =head2 Algorithm implementation fetching |
71 | ||
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. | |
75 | ||
76 | The returned value must eventually be freed with | |
77 | L<EVP_MAC_free(3)>. | |
78 | ||
79 | EVP_MAC_up_ref() increments the reference count of an already fetched | |
80 | MAC. | |
81 | ||
82 | EVP_MAC_free() frees a fetched algorithm. | |
83 | NULL is a valid parameter, for which this function is a no-op. | |
84 | ||
567db2c1 RL |
85 | =head2 Context manipulation functions |
86 | ||
e74bd290 | 87 | EVP_MAC_CTX_new() creates a new context for the MAC type I<mac>. |
567db2c1 RL |
88 | The created context can then be used with most other functions |
89 | described here. | |
90 | ||
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. | |
e74bd290 | 93 | NULL is a valid parameter, for which this function is a no-op. |
567db2c1 | 94 | |
e74bd290 | 95 | EVP_MAC_CTX_dup() duplicates the I<src> context and returns a newly allocated |
be5fc053 | 96 | context. |
567db2c1 RL |
97 | |
98 | EVP_MAC_CTX_mac() returns the B<EVP_MAC> associated with the context | |
e74bd290 | 99 | I<ctx>. |
567db2c1 RL |
100 | |
101 | =head2 Computing functions | |
102 | ||
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 | |
106 | EVP_MAC_final(). | |
107 | ||
e74bd290 | 108 | EVP_MAC_update() adds I<datalen> bytes from I<data> to the MAC input. |
567db2c1 RL |
109 | |
110 | EVP_MAC_final() does the final computation and stores the result in | |
e74bd290 RL |
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 | |
114 | is made. | |
567db2c1 | 115 | To figure out what the output length will be and allocate space for it |
e74bd290 | 116 | dynamically, simply call with I<out> being B<NULL> and I<outl> |
567db2c1 | 117 | pointing at a valid location, then allocate space and make a second |
e74bd290 RL |
118 | call with I<out> pointing at the allocated space. |
119 | ||
120 | EVP_MAC_get_params() retrieves details about the implementation | |
121 | I<mac>. | |
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 | |
125 | simply ignored. | |
126 | ||
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 | |
132 | simply ignored. | |
133 | ||
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 | |
139 | simply ignored. | |
140 | Also, what happens when a needed parameter isn't passed down is | |
141 | defined by the implementation. | |
142 | ||
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. | |
567db2c1 RL |
149 | |
150 | =head2 Information functions | |
151 | ||
152 | EVP_MAC_size() returns the MAC output size for the given context. | |
153 | ||
567db2c1 RL |
154 | EVP_MAC_name() returns the name of the given MAC implementation. |
155 | ||
7dd0f299 RL |
156 | EVP_MAC_provider() returns the provider that holds the implementation |
157 | of the given I<mac>. | |
158 | ||
e74bd290 | 159 | =head1 PARAMETER NAMES |
567db2c1 | 160 | |
e74bd290 | 161 | The standard parameter names are: |
567db2c1 RL |
162 | |
163 | =over 4 | |
164 | ||
e74bd290 | 165 | =item OSSL_MAC_PARAM_KEY ("key") <octet string> |
567db2c1 | 166 | |
e74bd290 | 167 | Its value is the MAC key as an array of bytes. |
567db2c1 RL |
168 | |
169 | For MACs that use an underlying computation algorithm, the algorithm | |
e74bd290 | 170 | must be set first, see parameter names "algorithm" below. |
afc580b9 | 171 | |
e74bd290 | 172 | =item OSSL_MAC_PARAM_IV ("iv") <octet string> |
afc580b9 | 173 | |
e74bd290 | 174 | Some MAC implementations require an IV, this parameter sets the IV. |
6e624a64 | 175 | |
e74bd290 | 176 | =item OSSL_MAC_PARAM_CUSTOM ("custom") <octet string> |
6e624a64 | 177 | |
13b3cd7b | 178 | Some MAC implementations (KMAC, BLAKE2) accept a Customization String, |
e74bd290 RL |
179 | this parameter sets the Customization String. The default value is the |
180 | empty string. | |
6e624a64 | 181 | |
e74bd290 | 182 | =item OSSL_MAC_PARAM_SALT ("salt") <octet string> |
13b3cd7b AS |
183 | |
184 | This option is used by BLAKE2 MAC. | |
185 | ||
e74bd290 | 186 | =item OSSL_MAC_PARAM_XOF ("xof") <int> |
6e624a64 | 187 | |
e74bd290 | 188 | It's a simple flag, the value 0 or 1 are expected. |
6e624a64 SL |
189 | |
190 | This option is used by KMAC. | |
191 | ||
e74bd290 | 192 | =item OSSL_MAC_PARAM_FLAGS ("flags") <int> |
567db2c1 RL |
193 | |
194 | These will set the MAC flags to the given numbers. | |
195 | Some MACs do not support this option. | |
196 | ||
e74bd290 RL |
197 | =item OSSL_MAC_PARAM_ENGINE ("engine") <utf8string> |
198 | ||
199 | =item OSSL_MAC_PARAM_MD ("md") <utf8string> | |
567db2c1 | 200 | |
e74bd290 | 201 | =item OSSL_MAC_PARAM_DIGEST ("digest") <utf8string> |
567db2c1 | 202 | |
e74bd290 RL |
203 | =item OSSL_MAC_PARAM_CIPHER ("cipher") <utf8string> |
204 | ||
205 | =item OSSL_MAC_PARAM_ALGORITHM ("algorithm") <utf8string> | |
567db2c1 RL |
206 | |
207 | For MAC implementations that use an underlying computation algorithm, | |
e74bd290 | 208 | these parameters set what the algorithm should be, and the engine that |
567db2c1 RL |
209 | implements the algorithm if needed. |
210 | ||
e74bd290 | 211 | The value is always the name of the intended engine or algorithm. |
567db2c1 | 212 | |
e74bd290 RL |
213 | Note that not all algorithms may support all digests. |
214 | HMAC does not support variable output length digests such as SHAKE128 | |
215 | or SHAKE256. | |
567db2c1 | 216 | |
e74bd290 RL |
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. | |
567db2c1 | 220 | |
e74bd290 | 221 | =item OSSL_MAC_PARAM_SIZE <unsigned int> |
567db2c1 RL |
222 | |
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. | |
226 | ||
227 | =back | |
228 | ||
e74bd290 | 229 | All these parameters should be used before the calls to any of |
567db2c1 RL |
230 | EVP_MAC_init(), EVP_MAC_update() and EVP_MAC_final() for a full |
231 | computation. | |
232 | Anything else may give undefined results. | |
233 | ||
e74bd290 | 234 | =head1 RETURN VALUES |
567db2c1 | 235 | |
e74bd290 RL |
236 | EVP_MAC_fetch() returns a pointer to a newly fetched EVP_MAC, or |
237 | NULL if allocation failed. | |
567db2c1 | 238 | |
e74bd290 RL |
239 | EVP_MAC_up_ref() returns 1 on success, 0 on error. |
240 | ||
241 | EVP_MAC_free() returns nothing at all. | |
242 | ||
243 | EVP_MAC_name() returns the name of the MAC, or NULL if NULL was | |
244 | passed. | |
567db2c1 | 245 | |
7dd0f299 RL |
246 | EVP_MAC_provider() returns a pointer to the provider for the MAC, or |
247 | NULL on error. | |
248 | ||
e74bd290 RL |
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. | |
567db2c1 RL |
251 | |
252 | EVP_MAC_CTX_free() returns nothing at all. | |
253 | ||
e74bd290 RL |
254 | EVP_MAC_CTX_get_params() and EVP_MAC_CTX_set_params() return 1 on |
255 | success, 0 on error. | |
567db2c1 | 256 | |
e74bd290 RL |
257 | EVP_MAC_init(), EVP_MAC_update(), and EVP_MAC_final() return 1 on success, 0 |
258 | on error. | |
567db2c1 RL |
259 | |
260 | EVP_MAC_size() returns the expected output size, or 0 if it isn't | |
261 | set. | |
262 | If it isn't set, a call to EVP_MAC_init() should get it set. | |
263 | ||
567db2c1 RL |
264 | |
265 | =head1 EXAMPLE | |
266 | ||
267 | #include <stdlib.h> | |
268 | #include <stdio.h> | |
269 | #include <string.h> | |
270 | #include <stdarg.h> | |
271 | #include <unistd.h> | |
272 | ||
273 | #include <openssl/evp.h> | |
274 | #include <openssl/err.h> | |
e74bd290 | 275 | #include <openssl/params.h> |
567db2c1 RL |
276 | |
277 | int main() { | |
e74bd290 RL |
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"); | |
567db2c1 RL |
281 | const char *key = getenv("MY_KEY"); |
282 | EVP_MAC_CTX *ctx = NULL; | |
283 | ||
284 | unsigned char buf[4096]; | |
285 | ssize_t read_l; | |
286 | size_t final_l; | |
287 | ||
288 | size_t i; | |
289 | ||
e74bd290 RL |
290 | OSSL_PARAM params[4]; |
291 | size_t params_n = 0; | |
292 | ||
293 | if (cipher != NULL) | |
294 | params[params_n++] = | |
295 | OSSL_PARAM_construct_utf8_string("cipher", cipher, | |
296 | strlen(cipher) + 1, NULL); | |
297 | if (digest != NULL) | |
298 | params[params_n++] = | |
299 | OSSL_PARAM_construct_utf8_string("digest", digest, | |
300 | strlen(digest) + 1, NULL); | |
301 | params[params_n++] = | |
302 | OSSL_PARAM_construct_octet_string("key", key, strlen(key), NULL); | |
303 | params[params_n] = OSSL_PARAM_construct_end(); | |
304 | ||
567db2c1 RL |
305 | if (mac == NULL |
306 | || key == NULL | |
307 | || (ctx = EVP_MAC_CTX_new(mac)) == NULL | |
e74bd290 | 308 | || EVP_MAC_CTX_set_params(ctx, params) <= 0) |
567db2c1 RL |
309 | goto err; |
310 | ||
311 | if (!EVP_MAC_init(ctx)) | |
312 | goto err; | |
313 | ||
314 | while ( (read_l = read(STDIN_FILENO, buf, sizeof(buf))) < 0) { | |
315 | if (!EVP_MAC_update(ctx, buf, read_l)) | |
316 | goto err; | |
317 | } | |
318 | ||
319 | if (!EVP_MAC_final(ctx, buf, &final_l)) | |
320 | goto err; | |
321 | ||
322 | printf("Result: "); | |
323 | for (i = 0; i < final_l; i++) | |
324 | printf("%02X", buf[i]); | |
325 | printf("\n"); | |
326 | ||
327 | EVP_MAC_CTX_free(ctx); | |
e74bd290 | 328 | EVP_MAC_free(mac); |
567db2c1 RL |
329 | exit(0); |
330 | ||
331 | err: | |
332 | EVP_MAC_CTX_free(ctx); | |
e74bd290 | 333 | EVP_MAC_free(mac); |
567db2c1 RL |
334 | fprintf(stderr, "Something went wrong\n"); |
335 | ERR_print_errors_fp(stderr); | |
336 | exit (1); | |
337 | } | |
338 | ||
339 | A run of this program, called with correct environment variables, can | |
340 | look 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 | |
347 | F<./foo>) | |
348 | ||
349 | =head1 SEE ALSO | |
350 | ||
e74bd290 RL |
351 | L<property(7)> |
352 | L<OSSL_PARAM(3)>, | |
13b3cd7b | 353 | L<EVP_MAC_BLAKE2(7)>, |
6723f867 | 354 | L<EVP_MAC_CMAC(7)>, |
afc580b9 | 355 | L<EVP_MAC_GMAC(7)>, |
c89d9cda | 356 | L<EVP_MAC_HMAC(7)>, |
6e624a64 | 357 | L<EVP_MAC_KMAC(7)>, |
c1da4b2a PY |
358 | L<EVP_MAC_SIPHASH(7)>, |
359 | L<EVP_MAC_POLY1305(7)> | |
567db2c1 | 360 | |
be5fc053 KR |
361 | =head1 HISTORY |
362 | ||
4674aaf4 | 363 | These functions were added in OpenSSL 3.0. |
be5fc053 | 364 | |
567db2c1 RL |
365 | =head1 COPYRIGHT |
366 | ||
e74bd290 | 367 | Copyright 2018-2019 The OpenSSL Project Authors. All Rights Reserved. |
567db2c1 | 368 | |
4746f25a | 369 | Licensed under the Apache License 2.0 (the "License"). You may not use |
567db2c1 RL |
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>. | |
373 | ||
374 | =cut |