]>
Commit | Line | Data |
---|---|---|
567db2c1 RL |
1 | =pod |
2 | ||
3 | =head1 NAME | |
4 | ||
03888233 | 5 | EVP_MAC, EVP_MAC_fetch, EVP_MAC_up_ref, EVP_MAC_free, EVP_MAC_is_a, |
6ea964cd | 6 | EVP_MAC_get0_name, EVP_MAC_names_do_all, EVP_MAC_get0_description, |
ed576acd | 7 | EVP_MAC_get0_provider, EVP_MAC_get_params, EVP_MAC_gettable_params, |
865adf97 | 8 | EVP_MAC_CTX, EVP_MAC_CTX_new, EVP_MAC_CTX_free, EVP_MAC_CTX_dup, |
ed576acd | 9 | EVP_MAC_CTX_get0_mac, EVP_MAC_CTX_get_params, EVP_MAC_CTX_set_params, |
edc9ce8e | 10 | EVP_MAC_CTX_get_mac_size, EVP_MAC_CTX_get_block_size, EVP_Q_mac, |
0a8a6afd DDO |
11 | EVP_MAC_init, EVP_MAC_update, EVP_MAC_final, EVP_MAC_finalXOF, |
12 | EVP_MAC_gettable_ctx_params, EVP_MAC_settable_ctx_params, | |
8dd233bb | 13 | EVP_MAC_CTX_gettable_params, EVP_MAC_CTX_settable_params, |
251e610c | 14 | EVP_MAC_do_all_provided - EVP MAC routines |
567db2c1 RL |
15 | |
16 | =head1 SYNOPSIS | |
17 | ||
18 | #include <openssl/evp.h> | |
19 | ||
20 | typedef struct evp_mac_st EVP_MAC; | |
21 | typedef struct evp_mac_ctx_st EVP_MAC_CTX; | |
22 | ||
b4250010 | 23 | EVP_MAC *EVP_MAC_fetch(OSSL_LIB_CTX *libctx, const char *algorithm, |
e74bd290 RL |
24 | const char *properties); |
25 | int EVP_MAC_up_ref(EVP_MAC *mac); | |
26 | void EVP_MAC_free(EVP_MAC *mac); | |
7cfa1717 | 27 | int EVP_MAC_is_a(const EVP_MAC *mac, const char *name); |
ed576acd | 28 | const char *EVP_MAC_get0_name(const EVP_MAC *mac); |
d84f5515 MC |
29 | int EVP_MAC_names_do_all(const EVP_MAC *mac, |
30 | void (*fn)(const char *name, void *data), | |
31 | void *data); | |
ed576acd TM |
32 | const char *EVP_MAC_get0_description(const EVP_MAC *mac); |
33 | const OSSL_PROVIDER *EVP_MAC_get0_provider(const EVP_MAC *mac); | |
e74bd290 RL |
34 | int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[]); |
35 | ||
865adf97 MC |
36 | EVP_MAC_CTX *EVP_MAC_CTX_new(EVP_MAC *mac); |
37 | void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx); | |
38 | EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src); | |
ed576acd | 39 | EVP_MAC *EVP_MAC_CTX_get0_mac(EVP_MAC_CTX *ctx); |
865adf97 MC |
40 | int EVP_MAC_CTX_get_params(EVP_MAC_CTX *ctx, OSSL_PARAM params[]); |
41 | int EVP_MAC_CTX_set_params(EVP_MAC_CTX *ctx, const OSSL_PARAM params[]); | |
e74bd290 | 42 | |
90a2576b | 43 | size_t EVP_MAC_CTX_get_mac_size(EVP_MAC_CTX *ctx); |
edc9ce8e | 44 | size_t EVP_MAC_CTX_get_block_size(EVP_MAC_CTX *ctx); |
0a8a6afd DDO |
45 | unsigned char *EVP_Q_mac(OSSL_LIB_CTX *libctx, const char *name, const char *propq, |
46 | const char *subalg, const OSSL_PARAM *params, | |
47 | const void *key, size_t keylen, | |
48 | const unsigned char *data, size_t datalen, | |
49 | unsigned char *out, size_t outsize, unsigned int *outlen); | |
afa44486 P |
50 | int EVP_MAC_init(EVP_MAC_CTX *ctx, const unsigned char *key, size_t keylen, |
51 | const OSSL_PARAM params[]); | |
567db2c1 | 52 | int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen); |
e74bd290 RL |
53 | int EVP_MAC_final(EVP_MAC_CTX *ctx, |
54 | unsigned char *out, size_t *outl, size_t outsize); | |
a59c6972 | 55 | int EVP_MAC_finalXOF(EVP_MAC_CTX *ctx, unsigned char *out, size_t outsize); |
e74bd290 RL |
56 | |
57 | const OSSL_PARAM *EVP_MAC_gettable_params(const EVP_MAC *mac); | |
41f7ecf3 P |
58 | const OSSL_PARAM *EVP_MAC_gettable_ctx_params(const EVP_MAC *mac); |
59 | const OSSL_PARAM *EVP_MAC_settable_ctx_params(const EVP_MAC *mac); | |
8dd233bb P |
60 | const OSSL_PARAM *EVP_MAC_CTX_gettable_params(EVP_MAC_CTX *ctx); |
61 | const OSSL_PARAM *EVP_MAC_CTX_settable_params(EVP_MAC_CTX *ctx); | |
567db2c1 | 62 | |
b4250010 | 63 | void EVP_MAC_do_all_provided(OSSL_LIB_CTX *libctx, |
251e610c RL |
64 | void (*fn)(EVP_MAC *mac, void *arg), |
65 | void *arg); | |
d1cafb08 | 66 | |
567db2c1 RL |
67 | =head1 DESCRIPTION |
68 | ||
69 | These types and functions help the application to calculate MACs of | |
70 | different types and with different underlying algorithms if there are | |
71 | any. | |
72 | ||
73 | MACs are a bit complex insofar that some of them use other algorithms | |
74 | for actual computation. HMAC uses a digest, and CMAC uses a cipher. | |
75 | Therefore, there are sometimes two contexts to keep track of, one for | |
76 | the MAC algorithm itself and one for the underlying computation | |
77 | algorithm if there is one. | |
78 | ||
79 | To make things less ambiguous, this manual talks about a "context" or | |
80 | "MAC context", which is to denote the MAC level context, and about a | |
81 | "underlying context", or "computation context", which is to denote the | |
82 | context for the underlying computation algorithm if there is one. | |
83 | ||
84 | =head2 Types | |
85 | ||
86 | B<EVP_MAC> is a type that holds the implementation of a MAC. | |
87 | ||
88 | B<EVP_MAC_CTX> is a context type that holds internal MAC information | |
89 | as well as a reference to a computation context, for those MACs that | |
90 | rely on an underlying computation algorithm. | |
91 | ||
e74bd290 RL |
92 | =head2 Algorithm implementation fetching |
93 | ||
94 | EVP_MAC_fetch() fetches an implementation of a MAC I<algorithm>, given | |
95 | a library context I<libctx> and a set of I<properties>. | |
906bced1 | 96 | See L<crypto(7)/ALGORITHM FETCHING> for further information. |
e74bd290 | 97 | |
b8086652 SL |
98 | See L<OSSL_PROVIDER-default(7)/Message Authentication Code (MAC)> for the list |
99 | of algorithms supported by the default provider. | |
100 | ||
e74bd290 RL |
101 | The returned value must eventually be freed with |
102 | L<EVP_MAC_free(3)>. | |
103 | ||
104 | EVP_MAC_up_ref() increments the reference count of an already fetched | |
105 | MAC. | |
106 | ||
107 | EVP_MAC_free() frees a fetched algorithm. | |
108 | NULL is a valid parameter, for which this function is a no-op. | |
109 | ||
567db2c1 RL |
110 | =head2 Context manipulation functions |
111 | ||
865adf97 | 112 | EVP_MAC_CTX_new() creates a new context for the MAC type I<mac>. |
567db2c1 RL |
113 | The created context can then be used with most other functions |
114 | described here. | |
115 | ||
865adf97 | 116 | EVP_MAC_CTX_free() frees the contents of the context, including an |
567db2c1 | 117 | underlying context if there is one, as well as the context itself. |
e74bd290 | 118 | NULL is a valid parameter, for which this function is a no-op. |
567db2c1 | 119 | |
865adf97 | 120 | EVP_MAC_CTX_dup() duplicates the I<src> context and returns a newly allocated |
be5fc053 | 121 | context. |
567db2c1 | 122 | |
ed576acd | 123 | EVP_MAC_CTX_get0_mac() returns the B<EVP_MAC> associated with the context |
e74bd290 | 124 | I<ctx>. |
567db2c1 RL |
125 | |
126 | =head2 Computing functions | |
127 | ||
0a8a6afd DDO |
128 | EVP_Q_mac() computes the message authentication code |
129 | of I<data> with length I<datalen> | |
130 | using the MAC algorithm I<name> and the key I<key> with length I<keylen>. | |
131 | The MAC algorithm is fetched using any given I<libctx> and property query | |
132 | string I<propq>. It takes parameters I<subalg> and further I<params>, | |
133 | both of which may be NULL if not needed. | |
134 | If I<out> is not NULL, it places the result in the memory pointed at by I<out>, | |
135 | but only if I<outsize> is sufficient (otherwise no computation is made). | |
136 | If I<out> is NULL, it allocates and uses a buffer of suitable length, | |
137 | which will be returned on success and must be freed by the caller. | |
138 | In either case, also on error, | |
139 | it assigns the number of bytes written to I<*outlen> unless I<outlen> is NULL. | |
140 | ||
c0e724fc | 141 | EVP_MAC_init() sets up the underlying context I<ctx> with information given |
afa44486 P |
142 | via the I<key> and I<params> arguments. The MAC I<key> has a length of |
143 | I<keylen> and the parameters in I<params> are processed before setting | |
c0e724fc | 144 | the key. If I<key> is NULL, the key must be set via I<params> either |
afa44486 | 145 | as part of this call or separately using EVP_MAC_CTX_set_params(). |
c0e724fc DDO |
146 | Providing non-NULL I<params> to this function is equivalent to calling |
147 | EVP_MAC_CTX_set_params() with those I<params> for the same I<ctx> beforehand. | |
148 | ||
149 | EVP_MAC_init() should be called before EVP_MAC_update() and EVP_MAC_final(). | |
567db2c1 | 150 | |
e74bd290 | 151 | EVP_MAC_update() adds I<datalen> bytes from I<data> to the MAC input. |
567db2c1 RL |
152 | |
153 | EVP_MAC_final() does the final computation and stores the result in | |
e74bd290 RL |
154 | the memory pointed at by I<out> of size I<outsize>, and sets the number |
155 | of bytes written in I<*outl> at. | |
ee2161e8 | 156 | If I<out> is NULL or I<outsize> is too small, then no computation |
e74bd290 | 157 | is made. |
567db2c1 | 158 | To figure out what the output length will be and allocate space for it |
ee2161e8 | 159 | dynamically, simply call with I<out> being NULL and I<outl> |
567db2c1 | 160 | pointing at a valid location, then allocate space and make a second |
e74bd290 RL |
161 | call with I<out> pointing at the allocated space. |
162 | ||
a59c6972 P |
163 | EVP_MAC_finalXOF() does the final computation for an XOF based MAC and stores |
164 | the result in the memory pointed at by I<out> of size I<outsize>. | |
165 | ||
e74bd290 RL |
166 | EVP_MAC_get_params() retrieves details about the implementation |
167 | I<mac>. | |
168 | The set of parameters given with I<params> determine exactly what | |
169 | parameters should be retrieved. | |
170 | Note that a parameter that is unknown in the underlying context is | |
171 | simply ignored. | |
172 | ||
865adf97 | 173 | EVP_MAC_CTX_get_params() retrieves chosen parameters, given the |
e74bd290 RL |
174 | context I<ctx> and its underlying context. |
175 | The set of parameters given with I<params> determine exactly what | |
176 | parameters should be retrieved. | |
177 | Note that a parameter that is unknown in the underlying context is | |
178 | simply ignored. | |
179 | ||
865adf97 | 180 | EVP_MAC_CTX_set_params() passes chosen parameters to the underlying |
e74bd290 RL |
181 | context, given a context I<ctx>. |
182 | The set of parameters given with I<params> determine exactly what | |
183 | parameters are passed down. | |
0a8a6afd | 184 | If I<params> are NULL, the unterlying context should do nothing and return 1. |
e74bd290 RL |
185 | Note that a parameter that is unknown in the underlying context is |
186 | simply ignored. | |
187 | Also, what happens when a needed parameter isn't passed down is | |
188 | defined by the implementation. | |
189 | ||
8dd233bb P |
190 | EVP_MAC_gettable_params() returns an B<OSSL_PARAM> array that describes |
191 | the retrievable and settable parameters. EVP_MAC_gettable_params() | |
192 | returns parameters that can be used with EVP_MAC_get_params(). | |
193 | See L<OSSL_PARAM(3)> for the use of B<OSSL_PARAM> as a parameter descriptor. | |
194 | ||
195 | EVP_MAC_gettable_ctx_params() and EVP_MAC_CTX_gettable_params() | |
196 | return constant B<OSSL_PARAM> arrays that describe the retrievable | |
197 | parameters that can be used with EVP_MAC_CTX_get_params(). | |
198 | EVP_MAC_gettable_ctx_params() returns the parameters that can be retrieved | |
199 | from the algorithm, whereas EVP_MAC_CTX_gettable_params() returns | |
200 | the parameters that can be retrieved in the context's current state. | |
201 | See L<OSSL_PARAM(3)> for the use of B<OSSL_PARAM> as a parameter descriptor. | |
202 | ||
203 | EVP_MAC_settable_ctx_params() and EVP_MAC_CTX_settable_params() return | |
204 | constant B<OSSL_PARAM> arrays that describe the settable parameters that | |
205 | can be used with EVP_MAC_CTX_set_params(). EVP_MAC_settable_ctx_params() | |
206 | returns the parameters that can be retrieved from the algorithm, | |
207 | whereas EVP_MAC_CTX_settable_params() returns the parameters that can | |
208 | be retrieved in the context's current state. See L<OSSL_PARAM(3)> | |
209 | for the use of B<OSSL_PARAM> as a parameter descriptor. | |
567db2c1 RL |
210 | |
211 | =head2 Information functions | |
212 | ||
90a2576b | 213 | EVP_MAC_CTX_get_mac_size() returns the MAC output size for the given context. |
567db2c1 | 214 | |
edc9ce8e P |
215 | EVP_MAC_CTX_get_block_size() returns the MAC block size for the given context. |
216 | Not all MAC algorithms support this. | |
217 | ||
7cfa1717 RL |
218 | EVP_MAC_is_a() checks if the given I<mac> is an implementation of an |
219 | algorithm that's identifiable with I<name>. | |
220 | ||
ed576acd | 221 | EVP_MAC_get0_provider() returns the provider that holds the implementation |
7dd0f299 RL |
222 | of the given I<mac>. |
223 | ||
251e610c | 224 | EVP_MAC_do_all_provided() traverses all MAC implemented by all activated |
d1cafb08 RL |
225 | providers in the given library context I<libctx>, and for each of the |
226 | implementations, calls the given function I<fn> with the implementation method | |
227 | and the given I<arg> as argument. | |
228 | ||
ed576acd | 229 | EVP_MAC_get0_name() return the name of the given MAC. For fetched MACs |
c9452d74 P |
230 | with multiple names, only one of them is returned; it's |
231 | recommended to use EVP_MAC_names_do_all() instead. | |
232 | ||
f651c727 RL |
233 | EVP_MAC_names_do_all() traverses all names for I<mac>, and calls |
234 | I<fn> with each name and I<data>. | |
235 | ||
ed576acd TM |
236 | EVP_MAC_get0_description() returns a description of the I<mac>, meant |
237 | for display and human consumption. The description is at the discretion | |
238 | of the mac implementation. | |
03888233 | 239 | |
b1cabee8 | 240 | =head1 PARAMETERS |
567db2c1 | 241 | |
e592dbde RL |
242 | Parameters are identified by name as strings, and have an expected |
243 | data type and maximum size. | |
244 | OpenSSL has a set of macros for parameter names it expects to see in | |
245 | its own MAC implementations. | |
246 | Here, we show all three, the OpenSSL macro for the parameter name, the | |
247 | name in string form, and a type description. | |
248 | ||
e74bd290 | 249 | The standard parameter names are: |
567db2c1 RL |
250 | |
251 | =over 4 | |
252 | ||
0c452a51 | 253 | =item "key" (B<OSSL_MAC_PARAM_KEY>) <octet string> |
567db2c1 | 254 | |
e74bd290 | 255 | Its value is the MAC key as an array of bytes. |
567db2c1 RL |
256 | |
257 | For MACs that use an underlying computation algorithm, the algorithm | |
e74bd290 | 258 | must be set first, see parameter names "algorithm" below. |
afc580b9 | 259 | |
0c452a51 | 260 | =item "iv" (B<OSSL_MAC_PARAM_IV>) <octet string> |
afc580b9 | 261 | |
bbf5ccfd | 262 | Some MAC implementations (GMAC) require an IV, this parameter sets the IV. |
6e624a64 | 263 | |
0c452a51 | 264 | =item "custom" (B<OSSL_MAC_PARAM_CUSTOM>) <octet string> |
6e624a64 | 265 | |
13b3cd7b | 266 | Some MAC implementations (KMAC, BLAKE2) accept a Customization String, |
e74bd290 RL |
267 | this parameter sets the Customization String. The default value is the |
268 | empty string. | |
6e624a64 | 269 | |
0c452a51 | 270 | =item "salt" (B<OSSL_MAC_PARAM_SALT>) <octet string> |
13b3cd7b AS |
271 | |
272 | This option is used by BLAKE2 MAC. | |
273 | ||
0c452a51 | 274 | =item "xof" (B<OSSL_MAC_PARAM_XOF>) <integer> |
6e624a64 | 275 | |
e74bd290 | 276 | It's a simple flag, the value 0 or 1 are expected. |
6e624a64 SL |
277 | |
278 | This option is used by KMAC. | |
279 | ||
36978c19 | 280 | =item "digest-noinit" (B<OSSL_MAC_PARAM_DIGEST_NOINIT>) <integer> |
567db2c1 | 281 | |
36978c19 SL |
282 | A simple flag to set the MAC digest to not initialise the |
283 | implementation specific data. The value 0 or 1 is expected. | |
284 | ||
285 | This option is used by HMAC. | |
286 | ||
287 | =item "digest-oneshot" (B<OSSL_MAC_PARAM_DIGEST_ONESHOT>) <integer> | |
288 | ||
289 | A simple flag to set the MAC digest to be a oneshot operation. | |
290 | The value 0 or 1 is expected. | |
291 | ||
292 | This option is used by HMAC. | |
567db2c1 | 293 | |
0c452a51 | 294 | =item "properties" (B<OSSL_MAC_PARAM_PROPERTIES>) <UTF8 string> |
567db2c1 | 295 | |
0c452a51 | 296 | =item "digest" (B<OSSL_MAC_PARAM_DIGEST>) <UTF8 string> |
567db2c1 | 297 | |
0c452a51 | 298 | =item "cipher" (B<OSSL_MAC_PARAM_CIPHER>) <UTF8 string> |
e74bd290 | 299 | |
f3b8d77f | 300 | For MAC implementations that use an underlying computation cipher or |
9bd9c440 | 301 | digest, these parameters set what the algorithm should be. |
567db2c1 | 302 | |
9bd9c440 | 303 | The value is always the name of the intended algorithm, |
f3b8d77f | 304 | or the properties. |
567db2c1 | 305 | |
e74bd290 RL |
306 | Note that not all algorithms may support all digests. |
307 | HMAC does not support variable output length digests such as SHAKE128 | |
308 | or SHAKE256. | |
567db2c1 | 309 | |
0c452a51 | 310 | =item "size" (B<OSSL_MAC_PARAM_SIZE>) <unsigned integer> |
567db2c1 RL |
311 | |
312 | For MAC implementations that support it, set the output size that | |
313 | EVP_MAC_final() should produce. | |
1aa01009 P |
314 | The allowed sizes vary between MAC implementations, but must never exceed |
315 | what can be given with a B<size_t>. | |
567db2c1 | 316 | |
820d87bc MC |
317 | =item "tls-data-size" (B<OSSL_MAC_PARAM_TLS_DATA_SIZE>) <unsigned integer> |
318 | ||
319 | This parameter is only supported by HMAC. If set then special handling is | |
320 | activated for calculating the MAC of a received mac-then-encrypt TLS record | |
321 | where variable length record padding has been used (as in the case of CBC mode | |
322 | ciphersuites). The value represents the total length of the record that is | |
323 | having the MAC calculated including the received MAC and the record padding. | |
324 | ||
325 | When used EVP_MAC_update must be called precisely twice. The first time with | |
326 | the 13 bytes of TLS "header" data, and the second time with the entire record | |
327 | including the MAC itself and any padding. The entire record length must equal | |
328 | the value passed in the "tls-data-size" parameter. The length passed in the | |
329 | B<datalen> parameter to EVP_MAC_update() should be equal to the length of the | |
330 | record after the MAC and any padding has been removed. | |
331 | ||
567db2c1 RL |
332 | =back |
333 | ||
e74bd290 | 334 | All these parameters should be used before the calls to any of |
567db2c1 RL |
335 | EVP_MAC_init(), EVP_MAC_update() and EVP_MAC_final() for a full |
336 | computation. | |
337 | Anything else may give undefined results. | |
338 | ||
10b63e97 P |
339 | =head1 NOTES |
340 | ||
341 | The MAC life-cycle is described in L<life_cycle-mac(7)>. In the future, | |
342 | the transitions described there will be enforced. When this is done, it will | |
343 | not be considered a breaking change to the API. | |
344 | ||
bbf5ccfd P |
345 | The usage of the parameter names "custom", "iv" and "salt" correspond to |
346 | the names used in the standard where the algorithm was defined. | |
10b63e97 | 347 | |
e74bd290 | 348 | =head1 RETURN VALUES |
567db2c1 | 349 | |
0a8a6afd | 350 | EVP_MAC_fetch() returns a pointer to a newly fetched B<EVP_MAC>, or |
e74bd290 | 351 | NULL if allocation failed. |
567db2c1 | 352 | |
e74bd290 RL |
353 | EVP_MAC_up_ref() returns 1 on success, 0 on error. |
354 | ||
d84f5515 MC |
355 | EVP_MAC_names_do_all() returns 1 if the callback was called for all names. A |
356 | return value of 0 means that the callback was not called for any names. | |
357 | ||
e74bd290 RL |
358 | EVP_MAC_free() returns nothing at all. |
359 | ||
7cfa1717 RL |
360 | EVP_MAC_is_a() returns 1 if the given method can be identified with |
361 | the given name, otherwise 0. | |
362 | ||
ed576acd | 363 | EVP_MAC_get0_name() returns a name of the MAC, or NULL on error. |
c9452d74 | 364 | |
ed576acd | 365 | EVP_MAC_get0_provider() returns a pointer to the provider for the MAC, or |
7dd0f299 RL |
366 | NULL on error. |
367 | ||
865adf97 | 368 | EVP_MAC_CTX_new() and EVP_MAC_CTX_dup() return a pointer to a newly |
e74bd290 | 369 | created EVP_MAC_CTX, or NULL if allocation failed. |
567db2c1 | 370 | |
865adf97 | 371 | EVP_MAC_CTX_free() returns nothing at all. |
567db2c1 | 372 | |
865adf97 | 373 | EVP_MAC_CTX_get_params() and EVP_MAC_CTX_set_params() return 1 on |
e74bd290 | 374 | success, 0 on error. |
567db2c1 | 375 | |
0a8a6afd DDO |
376 | EVP_Q_mac() returns a pointer to the computed MAC value, or NULL on error. |
377 | ||
378 | EVP_MAC_init(), EVP_MAC_update(), EVP_MAC_final(), and EVP_MAC_finalXOF() | |
a59c6972 | 379 | return 1 on success, 0 on error. |
567db2c1 | 380 | |
edc9ce8e P |
381 | EVP_MAC_CTX_get_mac_size() returns the expected output size, or 0 if it isn't |
382 | set. If it isn't set, a call to EVP_MAC_init() will set it. | |
383 | ||
384 | EVP_MAC_CTX_get_block_size() returns the block size, or 0 if it isn't set. | |
385 | If it isn't set, a call to EVP_MAC_init() will set it. | |
567db2c1 | 386 | |
251e610c | 387 | EVP_MAC_do_all_provided() returns nothing at all. |
567db2c1 | 388 | |
cda77422 | 389 | =head1 EXAMPLES |
567db2c1 RL |
390 | |
391 | #include <stdlib.h> | |
392 | #include <stdio.h> | |
393 | #include <string.h> | |
394 | #include <stdarg.h> | |
395 | #include <unistd.h> | |
396 | ||
397 | #include <openssl/evp.h> | |
398 | #include <openssl/err.h> | |
e74bd290 | 399 | #include <openssl/params.h> |
567db2c1 RL |
400 | |
401 | int main() { | |
e74bd290 RL |
402 | EVP_MAC *mac = EVP_MAC_fetch(NULL, getenv("MY_MAC"), NULL); |
403 | const char *cipher = getenv("MY_MAC_CIPHER"); | |
404 | const char *digest = getenv("MY_MAC_DIGEST"); | |
567db2c1 RL |
405 | const char *key = getenv("MY_KEY"); |
406 | EVP_MAC_CTX *ctx = NULL; | |
407 | ||
408 | unsigned char buf[4096]; | |
d5b170a2 | 409 | size_t read_l; |
567db2c1 RL |
410 | size_t final_l; |
411 | ||
412 | size_t i; | |
413 | ||
afa44486 | 414 | OSSL_PARAM params[3]; |
e74bd290 RL |
415 | size_t params_n = 0; |
416 | ||
417 | if (cipher != NULL) | |
418 | params[params_n++] = | |
d5b170a2 | 419 | OSSL_PARAM_construct_utf8_string("cipher", (char*)cipher, 0); |
e74bd290 RL |
420 | if (digest != NULL) |
421 | params[params_n++] = | |
d5b170a2 | 422 | OSSL_PARAM_construct_utf8_string("digest", (char*)digest, 0); |
e74bd290 RL |
423 | params[params_n] = OSSL_PARAM_construct_end(); |
424 | ||
567db2c1 RL |
425 | if (mac == NULL |
426 | || key == NULL | |
865adf97 | 427 | || (ctx = EVP_MAC_CTX_new(mac)) == NULL |
afa44486 P |
428 | || !EVP_MAC_init(ctx, (const unsigned char *)key, strlen(key), |
429 | params)) | |
567db2c1 RL |
430 | goto err; |
431 | ||
38e6c490 | 432 | while ( (read_l = read(STDIN_FILENO, buf, sizeof(buf))) > 0) { |
567db2c1 RL |
433 | if (!EVP_MAC_update(ctx, buf, read_l)) |
434 | goto err; | |
435 | } | |
436 | ||
d5b170a2 | 437 | if (!EVP_MAC_final(ctx, buf, &final_l, sizeof(buf))) |
567db2c1 RL |
438 | goto err; |
439 | ||
440 | printf("Result: "); | |
441 | for (i = 0; i < final_l; i++) | |
442 | printf("%02X", buf[i]); | |
443 | printf("\n"); | |
444 | ||
865adf97 | 445 | EVP_MAC_CTX_free(ctx); |
e74bd290 | 446 | EVP_MAC_free(mac); |
567db2c1 RL |
447 | exit(0); |
448 | ||
449 | err: | |
865adf97 | 450 | EVP_MAC_CTX_free(ctx); |
e74bd290 | 451 | EVP_MAC_free(mac); |
567db2c1 RL |
452 | fprintf(stderr, "Something went wrong\n"); |
453 | ERR_print_errors_fp(stderr); | |
454 | exit (1); | |
455 | } | |
456 | ||
457 | A run of this program, called with correct environment variables, can | |
458 | look like this: | |
459 | ||
460 | $ MY_MAC=cmac MY_KEY=secret0123456789 MY_MAC_CIPHER=aes-128-cbc \ | |
461 | LD_LIBRARY_PATH=. ./foo < foo.c | |
38e6c490 | 462 | Result: C5C06683CD9DDEF904D754505C560A4E |
567db2c1 RL |
463 | |
464 | (in this example, that program was stored in F<foo.c> and compiled to | |
465 | F<./foo>) | |
466 | ||
467 | =head1 SEE ALSO | |
468 | ||
e74bd290 RL |
469 | L<property(7)> |
470 | L<OSSL_PARAM(3)>, | |
d7cea0b8 RS |
471 | L<EVP_MAC-BLAKE2(7)>, |
472 | L<EVP_MAC-CMAC(7)>, | |
473 | L<EVP_MAC-GMAC(7)>, | |
474 | L<EVP_MAC-HMAC(7)>, | |
475 | L<EVP_MAC-KMAC(7)>, | |
476 | L<EVP_MAC-Siphash(7)>, | |
10b63e97 P |
477 | L<EVP_MAC-Poly1305(7)>, |
478 | L<provider-mac(7)>, | |
479 | L<life_cycle-mac(7)> | |
567db2c1 | 480 | |
be5fc053 KR |
481 | =head1 HISTORY |
482 | ||
4674aaf4 | 483 | These functions were added in OpenSSL 3.0. |
be5fc053 | 484 | |
567db2c1 RL |
485 | =head1 COPYRIGHT |
486 | ||
a28d06f3 | 487 | Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved. |
567db2c1 | 488 | |
4746f25a | 489 | Licensed under the Apache License 2.0 (the "License"). You may not use |
567db2c1 RL |
490 | this file except in compliance with the License. You can obtain a copy |
491 | in the file LICENSE in the source distribution or at | |
492 | L<https://www.openssl.org/source/license.html>. | |
493 | ||
494 | =cut |