/src/openssl/providers/implementations/macs/hmac_prov.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2018-2024 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | |
11 | | /* |
12 | | * HMAC low level APIs are deprecated for public use, but still ok for internal |
13 | | * use. |
14 | | */ |
15 | | #include "internal/deprecated.h" |
16 | | |
17 | | #include <string.h> |
18 | | |
19 | | #include <openssl/core_dispatch.h> |
20 | | #include <openssl/core_names.h> |
21 | | #include <openssl/params.h> |
22 | | #include <openssl/evp.h> |
23 | | #include <openssl/hmac.h> |
24 | | #include <openssl/proverr.h> |
25 | | #include <openssl/err.h> |
26 | | |
27 | | #include "internal/ssl3_cbc.h" |
28 | | #include "internal/cryptlib.h" |
29 | | |
30 | | #include "prov/implementations.h" |
31 | | #include "prov/provider_ctx.h" |
32 | | #include "prov/provider_util.h" |
33 | | #include "prov/providercommon.h" |
34 | | #include "prov/securitycheck.h" |
35 | | |
36 | | /* |
37 | | * Forward declaration of everything implemented here. This is not strictly |
38 | | * necessary for the compiler, but provides an assurance that the signatures |
39 | | * of the functions in the dispatch table are correct. |
40 | | */ |
41 | | static OSSL_FUNC_mac_newctx_fn hmac_new; |
42 | | static OSSL_FUNC_mac_dupctx_fn hmac_dup; |
43 | | static OSSL_FUNC_mac_freectx_fn hmac_free; |
44 | | static OSSL_FUNC_mac_gettable_ctx_params_fn hmac_gettable_ctx_params; |
45 | | static OSSL_FUNC_mac_get_ctx_params_fn hmac_get_ctx_params; |
46 | | static OSSL_FUNC_mac_settable_ctx_params_fn hmac_settable_ctx_params; |
47 | | static OSSL_FUNC_mac_set_ctx_params_fn hmac_set_ctx_params; |
48 | | static OSSL_FUNC_mac_init_fn hmac_init; |
49 | | static OSSL_FUNC_mac_update_fn hmac_update; |
50 | | static OSSL_FUNC_mac_final_fn hmac_final; |
51 | | |
52 | | /* local HMAC context structure */ |
53 | | |
54 | | /* typedef EVP_MAC_IMPL */ |
55 | | struct hmac_data_st { |
56 | | void *provctx; |
57 | | HMAC_CTX *ctx; /* HMAC context */ |
58 | | PROV_DIGEST digest; |
59 | | unsigned char *key; |
60 | | size_t keylen; |
61 | | /* Length of full TLS record including the MAC and any padding */ |
62 | | size_t tls_data_size; |
63 | | unsigned char tls_header[13]; |
64 | | int tls_header_set; |
65 | | unsigned char tls_mac_out[EVP_MAX_MD_SIZE]; |
66 | | size_t tls_mac_out_size; |
67 | | #ifdef FIPS_MODULE |
68 | | /* |
69 | | * 'internal' is set to 1 if HMAC is used inside another algorithm such as a |
70 | | * KDF. In this case it is the parent algorithm that is responsible for |
71 | | * performing any conditional FIPS indicator related checks for the HMAC. |
72 | | */ |
73 | | int internal; |
74 | | #endif |
75 | | OSSL_FIPS_IND_DECLARE |
76 | | }; |
77 | | |
78 | | static void *hmac_new(void *provctx) |
79 | 0 | { |
80 | 0 | struct hmac_data_st *macctx; |
81 | |
|
82 | 0 | if (!ossl_prov_is_running()) |
83 | 0 | return NULL; |
84 | | |
85 | 0 | if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL |
86 | 0 | || (macctx->ctx = HMAC_CTX_new()) == NULL) { |
87 | 0 | OPENSSL_free(macctx); |
88 | 0 | return NULL; |
89 | 0 | } |
90 | 0 | macctx->provctx = provctx; |
91 | 0 | OSSL_FIPS_IND_INIT(macctx) |
92 | |
|
93 | 0 | return macctx; |
94 | 0 | } |
95 | | |
96 | | static void hmac_free(void *vmacctx) |
97 | 0 | { |
98 | 0 | struct hmac_data_st *macctx = vmacctx; |
99 | |
|
100 | 0 | if (macctx != NULL) { |
101 | 0 | HMAC_CTX_free(macctx->ctx); |
102 | 0 | ossl_prov_digest_reset(&macctx->digest); |
103 | 0 | OPENSSL_secure_clear_free(macctx->key, macctx->keylen); |
104 | 0 | OPENSSL_free(macctx); |
105 | 0 | } |
106 | 0 | } |
107 | | |
108 | | static void *hmac_dup(void *vsrc) |
109 | 0 | { |
110 | 0 | struct hmac_data_st *src = vsrc; |
111 | 0 | struct hmac_data_st *dst; |
112 | 0 | HMAC_CTX *ctx; |
113 | |
|
114 | 0 | if (!ossl_prov_is_running()) |
115 | 0 | return NULL; |
116 | 0 | dst = hmac_new(src->provctx); |
117 | 0 | if (dst == NULL) |
118 | 0 | return NULL; |
119 | | |
120 | 0 | ctx = dst->ctx; |
121 | 0 | *dst = *src; |
122 | 0 | dst->ctx = ctx; |
123 | 0 | dst->key = NULL; |
124 | 0 | memset(&dst->digest, 0, sizeof(dst->digest)); |
125 | |
|
126 | 0 | if (!HMAC_CTX_copy(dst->ctx, src->ctx) |
127 | 0 | || !ossl_prov_digest_copy(&dst->digest, &src->digest)) { |
128 | 0 | hmac_free(dst); |
129 | 0 | return NULL; |
130 | 0 | } |
131 | 0 | if (src->key != NULL) { |
132 | | /* There is no "secure" OPENSSL_memdup */ |
133 | 0 | dst->key = OPENSSL_secure_malloc(src->keylen > 0 ? src->keylen : 1); |
134 | 0 | if (dst->key == NULL) { |
135 | 0 | hmac_free(dst); |
136 | 0 | return 0; |
137 | 0 | } |
138 | 0 | memcpy(dst->key, src->key, src->keylen); |
139 | 0 | } |
140 | 0 | return dst; |
141 | 0 | } |
142 | | |
143 | | static size_t hmac_size(struct hmac_data_st *macctx) |
144 | 0 | { |
145 | 0 | return HMAC_size(macctx->ctx); |
146 | 0 | } |
147 | | |
148 | | static int hmac_block_size(struct hmac_data_st *macctx) |
149 | 0 | { |
150 | 0 | const EVP_MD *md = ossl_prov_digest_md(&macctx->digest); |
151 | |
|
152 | 0 | if (md == NULL) |
153 | 0 | return 0; |
154 | 0 | return EVP_MD_block_size(md); |
155 | 0 | } |
156 | | |
157 | | static int hmac_setkey(struct hmac_data_st *macctx, |
158 | | const unsigned char *key, size_t keylen) |
159 | 0 | { |
160 | 0 | const EVP_MD *digest; |
161 | |
|
162 | | #ifdef FIPS_MODULE |
163 | | /* |
164 | | * KDF's pass a salt rather than a key, |
165 | | * which is why it skips the key check unless "HMAC" is fetched directly. |
166 | | */ |
167 | | if (!macctx->internal) { |
168 | | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(macctx->provctx); |
169 | | int approved = ossl_mac_check_key_size(keylen); |
170 | | |
171 | | if (!approved) { |
172 | | if (!OSSL_FIPS_IND_ON_UNAPPROVED(macctx, OSSL_FIPS_IND_SETTABLE0, |
173 | | libctx, "HMAC", "keysize", |
174 | | ossl_fips_config_hmac_key_check)) { |
175 | | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
176 | | return 0; |
177 | | } |
178 | | } |
179 | | } |
180 | | #endif |
181 | |
|
182 | 0 | if (macctx->key != NULL) |
183 | 0 | OPENSSL_secure_clear_free(macctx->key, macctx->keylen); |
184 | | /* Keep a copy of the key in case we need it for TLS HMAC */ |
185 | 0 | macctx->key = OPENSSL_secure_malloc(keylen > 0 ? keylen : 1); |
186 | 0 | if (macctx->key == NULL) |
187 | 0 | return 0; |
188 | | |
189 | 0 | memcpy(macctx->key, key, keylen); |
190 | 0 | macctx->keylen = keylen; |
191 | |
|
192 | 0 | digest = ossl_prov_digest_md(&macctx->digest); |
193 | | /* HMAC_Init_ex doesn't tolerate all zero params, so we must be careful */ |
194 | 0 | if (key != NULL || (macctx->tls_data_size == 0 && digest != NULL)) |
195 | 0 | return HMAC_Init_ex(macctx->ctx, key, (int)keylen, digest, |
196 | 0 | ossl_prov_digest_engine(&macctx->digest)); |
197 | 0 | return 1; |
198 | 0 | } |
199 | | |
200 | | static int hmac_init(void *vmacctx, const unsigned char *key, |
201 | | size_t keylen, const OSSL_PARAM params[]) |
202 | 0 | { |
203 | 0 | struct hmac_data_st *macctx = vmacctx; |
204 | |
|
205 | 0 | if (!ossl_prov_is_running() || !hmac_set_ctx_params(macctx, params)) |
206 | 0 | return 0; |
207 | | |
208 | 0 | if (key != NULL) |
209 | 0 | return hmac_setkey(macctx, key, keylen); |
210 | | |
211 | | /* Just reinit the HMAC context */ |
212 | 0 | return HMAC_Init_ex(macctx->ctx, NULL, 0, NULL, NULL); |
213 | 0 | } |
214 | | |
215 | | static int hmac_update(void *vmacctx, const unsigned char *data, |
216 | | size_t datalen) |
217 | 0 | { |
218 | 0 | struct hmac_data_st *macctx = vmacctx; |
219 | |
|
220 | 0 | if (macctx->tls_data_size > 0) { |
221 | | /* We're doing a TLS HMAC */ |
222 | 0 | if (!macctx->tls_header_set) { |
223 | | /* We expect the first update call to contain the TLS header */ |
224 | 0 | if (datalen != sizeof(macctx->tls_header)) |
225 | 0 | return 0; |
226 | 0 | memcpy(macctx->tls_header, data, datalen); |
227 | 0 | macctx->tls_header_set = 1; |
228 | 0 | return 1; |
229 | 0 | } |
230 | | /* macctx->tls_data_size is datalen plus the padding length */ |
231 | 0 | if (macctx->tls_data_size < datalen) |
232 | 0 | return 0; |
233 | | |
234 | 0 | return ssl3_cbc_digest_record(ossl_prov_digest_md(&macctx->digest), |
235 | 0 | macctx->tls_mac_out, |
236 | 0 | &macctx->tls_mac_out_size, |
237 | 0 | macctx->tls_header, |
238 | 0 | data, |
239 | 0 | datalen, |
240 | 0 | macctx->tls_data_size, |
241 | 0 | macctx->key, |
242 | 0 | macctx->keylen, |
243 | 0 | 0); |
244 | 0 | } |
245 | | |
246 | 0 | return HMAC_Update(macctx->ctx, data, datalen); |
247 | 0 | } |
248 | | |
249 | | static int hmac_final(void *vmacctx, unsigned char *out, size_t *outl, |
250 | | size_t outsize) |
251 | 0 | { |
252 | 0 | unsigned int hlen; |
253 | 0 | struct hmac_data_st *macctx = vmacctx; |
254 | |
|
255 | 0 | if (!ossl_prov_is_running()) |
256 | 0 | return 0; |
257 | 0 | if (macctx->tls_data_size > 0) { |
258 | 0 | if (macctx->tls_mac_out_size == 0) |
259 | 0 | return 0; |
260 | 0 | if (outl != NULL) |
261 | 0 | *outl = macctx->tls_mac_out_size; |
262 | 0 | memcpy(out, macctx->tls_mac_out, macctx->tls_mac_out_size); |
263 | 0 | return 1; |
264 | 0 | } |
265 | 0 | if (!HMAC_Final(macctx->ctx, out, &hlen)) |
266 | 0 | return 0; |
267 | 0 | *outl = hlen; |
268 | 0 | return 1; |
269 | 0 | } |
270 | | |
271 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
272 | | #ifndef hmac_get_ctx_params_list |
273 | | static const OSSL_PARAM hmac_get_ctx_params_list[] = { |
274 | | OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), |
275 | | OSSL_PARAM_size_t(OSSL_MAC_PARAM_BLOCK_SIZE, NULL), |
276 | | # if defined(FIPS_MODULE) |
277 | | OSSL_PARAM_int(OSSL_ALG_PARAM_FIPS_APPROVED_INDICATOR, NULL), |
278 | | # endif |
279 | | OSSL_PARAM_END |
280 | | }; |
281 | | #endif |
282 | | |
283 | | #ifndef hmac_get_ctx_params_st |
284 | | struct hmac_get_ctx_params_st { |
285 | | OSSL_PARAM *bsize; |
286 | | # if defined(FIPS_MODULE) |
287 | | OSSL_PARAM *ind; |
288 | | # endif |
289 | | OSSL_PARAM *size; |
290 | | }; |
291 | | #endif |
292 | | |
293 | | #ifndef hmac_get_ctx_params_decoder |
294 | | static int hmac_get_ctx_params_decoder |
295 | | (const OSSL_PARAM *p, struct hmac_get_ctx_params_st *r) |
296 | 0 | { |
297 | 0 | const char *s; |
298 | |
|
299 | 0 | memset(r, 0, sizeof(*r)); |
300 | 0 | if (p != NULL) |
301 | 0 | for (; (s = p->key) != NULL; p++) |
302 | 0 | switch(s[0]) { |
303 | 0 | default: |
304 | 0 | break; |
305 | 0 | case 'b': |
306 | 0 | if (ossl_likely(strcmp("lock-size", s + 1) == 0)) { |
307 | | /* MAC_PARAM_BLOCK_SIZE */ |
308 | 0 | if (ossl_unlikely(r->bsize != NULL)) { |
309 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
310 | 0 | "param %s is repeated", s); |
311 | 0 | return 0; |
312 | 0 | } |
313 | 0 | r->bsize = (OSSL_PARAM *)p; |
314 | 0 | } |
315 | 0 | break; |
316 | 0 | case 'f': |
317 | | # if defined(FIPS_MODULE) |
318 | | if (ossl_likely(strcmp("ips-indicator", s + 1) == 0)) { |
319 | | /* ALG_PARAM_FIPS_APPROVED_INDICATOR */ |
320 | | if (ossl_unlikely(r->ind != NULL)) { |
321 | | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
322 | | "param %s is repeated", s); |
323 | | return 0; |
324 | | } |
325 | | r->ind = (OSSL_PARAM *)p; |
326 | | } |
327 | | # endif |
328 | 0 | break; |
329 | 0 | case 's': |
330 | 0 | if (ossl_likely(strcmp("ize", s + 1) == 0)) { |
331 | | /* MAC_PARAM_SIZE */ |
332 | 0 | if (ossl_unlikely(r->size != NULL)) { |
333 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
334 | 0 | "param %s is repeated", s); |
335 | 0 | return 0; |
336 | 0 | } |
337 | 0 | r->size = (OSSL_PARAM *)p; |
338 | 0 | } |
339 | 0 | } |
340 | 0 | return 1; |
341 | 0 | } |
342 | | #endif |
343 | | /* End of machine generated */ |
344 | | |
345 | | static const OSSL_PARAM *hmac_gettable_ctx_params(ossl_unused void *ctx, |
346 | | ossl_unused void *provctx) |
347 | 0 | { |
348 | 0 | return hmac_get_ctx_params_list; |
349 | 0 | } |
350 | | |
351 | | static int hmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[]) |
352 | 0 | { |
353 | 0 | struct hmac_data_st *macctx = vmacctx; |
354 | 0 | struct hmac_get_ctx_params_st p; |
355 | |
|
356 | 0 | if (macctx == NULL || !hmac_get_ctx_params_decoder(params, &p)) |
357 | 0 | return 0; |
358 | | |
359 | 0 | if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, hmac_size(macctx))) |
360 | 0 | return 0; |
361 | | |
362 | 0 | if (p.bsize != NULL && !OSSL_PARAM_set_int(p.bsize, hmac_block_size(macctx))) |
363 | 0 | return 0; |
364 | | |
365 | | #ifdef FIPS_MODULE |
366 | | if (p.ind != NULL) { |
367 | | int approved = 0; |
368 | | |
369 | | if (!macctx->internal) |
370 | | approved = OSSL_FIPS_IND_GET(macctx)->approved; |
371 | | if (!OSSL_PARAM_set_int(p.ind, approved)) |
372 | | return 0; |
373 | | } |
374 | | #endif |
375 | 0 | return 1; |
376 | 0 | } |
377 | | |
378 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
379 | | #ifndef hmac_set_ctx_params_list |
380 | | static const OSSL_PARAM hmac_set_ctx_params_list[] = { |
381 | | OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, NULL, 0), |
382 | | OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0), |
383 | | OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0), |
384 | | OSSL_PARAM_size_t(OSSL_MAC_PARAM_TLS_DATA_SIZE, NULL), |
385 | | # if defined(FIPS_MODULE) |
386 | | OSSL_PARAM_int(OSSL_MAC_PARAM_FIPS_KEY_CHECK, NULL), |
387 | | # endif |
388 | | OSSL_PARAM_END |
389 | | }; |
390 | | #endif |
391 | | |
392 | | #ifndef hmac_set_ctx_params_st |
393 | | struct hmac_set_ctx_params_st { |
394 | | OSSL_PARAM *digest; |
395 | | OSSL_PARAM *engine; |
396 | | # if defined(FIPS_MODULE) |
397 | | OSSL_PARAM *ind_k; |
398 | | # endif |
399 | | OSSL_PARAM *key; |
400 | | OSSL_PARAM *propq; |
401 | | OSSL_PARAM *tlssize; |
402 | | }; |
403 | | #endif |
404 | | |
405 | | #ifndef hmac_set_ctx_params_decoder |
406 | | static int hmac_set_ctx_params_decoder |
407 | | (const OSSL_PARAM *p, struct hmac_set_ctx_params_st *r) |
408 | 0 | { |
409 | 0 | const char *s; |
410 | |
|
411 | 0 | memset(r, 0, sizeof(*r)); |
412 | 0 | if (p != NULL) |
413 | 0 | for (; (s = p->key) != NULL; p++) |
414 | 0 | switch(s[0]) { |
415 | 0 | default: |
416 | 0 | break; |
417 | 0 | case 'd': |
418 | 0 | if (ossl_likely(strcmp("igest", s + 1) == 0)) { |
419 | | /* MAC_PARAM_DIGEST */ |
420 | 0 | if (ossl_unlikely(r->digest != NULL)) { |
421 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
422 | 0 | "param %s is repeated", s); |
423 | 0 | return 0; |
424 | 0 | } |
425 | 0 | r->digest = (OSSL_PARAM *)p; |
426 | 0 | } |
427 | 0 | break; |
428 | 0 | case 'e': |
429 | 0 | if (ossl_likely(strcmp("ngine", s + 1) == 0)) { |
430 | | /* ALG_PARAM_ENGINE */ |
431 | 0 | if (ossl_unlikely(r->engine != NULL)) { |
432 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
433 | 0 | "param %s is repeated", s); |
434 | 0 | return 0; |
435 | 0 | } |
436 | 0 | r->engine = (OSSL_PARAM *)p; |
437 | 0 | } |
438 | 0 | break; |
439 | 0 | case 'k': |
440 | 0 | switch(s[1]) { |
441 | 0 | default: |
442 | 0 | break; |
443 | 0 | case 'e': |
444 | 0 | switch(s[2]) { |
445 | 0 | default: |
446 | 0 | break; |
447 | 0 | case 'y': |
448 | 0 | switch(s[3]) { |
449 | 0 | default: |
450 | 0 | break; |
451 | 0 | case '-': |
452 | | # if defined(FIPS_MODULE) |
453 | | if (ossl_likely(strcmp("check", s + 4) == 0)) { |
454 | | /* MAC_PARAM_FIPS_KEY_CHECK */ |
455 | | if (ossl_unlikely(r->ind_k != NULL)) { |
456 | | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
457 | | "param %s is repeated", s); |
458 | | return 0; |
459 | | } |
460 | | r->ind_k = (OSSL_PARAM *)p; |
461 | | } |
462 | | # endif |
463 | 0 | break; |
464 | 0 | case '\0': |
465 | 0 | if (ossl_unlikely(r->key != NULL)) { |
466 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
467 | 0 | "param %s is repeated", s); |
468 | 0 | return 0; |
469 | 0 | } |
470 | 0 | r->key = (OSSL_PARAM *)p; |
471 | 0 | } |
472 | 0 | } |
473 | 0 | } |
474 | 0 | break; |
475 | 0 | case 'p': |
476 | 0 | if (ossl_likely(strcmp("roperties", s + 1) == 0)) { |
477 | | /* MAC_PARAM_PROPERTIES */ |
478 | 0 | if (ossl_unlikely(r->propq != NULL)) { |
479 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
480 | 0 | "param %s is repeated", s); |
481 | 0 | return 0; |
482 | 0 | } |
483 | 0 | r->propq = (OSSL_PARAM *)p; |
484 | 0 | } |
485 | 0 | break; |
486 | 0 | case 't': |
487 | 0 | if (ossl_likely(strcmp("ls-data-size", s + 1) == 0)) { |
488 | | /* MAC_PARAM_TLS_DATA_SIZE */ |
489 | 0 | if (ossl_unlikely(r->tlssize != NULL)) { |
490 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
491 | 0 | "param %s is repeated", s); |
492 | 0 | return 0; |
493 | 0 | } |
494 | 0 | r->tlssize = (OSSL_PARAM *)p; |
495 | 0 | } |
496 | 0 | } |
497 | 0 | return 1; |
498 | 0 | } |
499 | | #endif |
500 | | /* End of machine generated */ |
501 | | |
502 | | static const OSSL_PARAM *hmac_settable_ctx_params(ossl_unused void *ctx, |
503 | | ossl_unused void *provctx) |
504 | 0 | { |
505 | 0 | return hmac_set_ctx_params_list; |
506 | 0 | } |
507 | | |
508 | | /* |
509 | | * ALL parameters should be set before init(). |
510 | | */ |
511 | | static int hmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[]) |
512 | 0 | { |
513 | 0 | struct hmac_data_st *macctx = vmacctx; |
514 | 0 | OSSL_LIB_CTX *ctx; |
515 | 0 | struct hmac_set_ctx_params_st p; |
516 | |
|
517 | 0 | if (macctx == NULL || !hmac_set_ctx_params_decoder(params, &p)) |
518 | 0 | return 0; |
519 | | |
520 | 0 | ctx = PROV_LIBCTX_OF(macctx->provctx); |
521 | |
|
522 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(macctx, OSSL_FIPS_IND_SETTABLE0, p.ind_k)) |
523 | 0 | return 0; |
524 | | |
525 | 0 | if (p.digest != NULL |
526 | 0 | && !ossl_prov_digest_load(&macctx->digest, p.digest, p.propq, |
527 | 0 | p.engine, ctx)) |
528 | 0 | return 0; |
529 | | |
530 | 0 | if (p.key != NULL) { |
531 | 0 | if (p.key->data_type != OSSL_PARAM_OCTET_STRING) |
532 | 0 | return 0; |
533 | | |
534 | 0 | if (!hmac_setkey(macctx, p.key->data, p.key->data_size)) |
535 | 0 | return 0; |
536 | 0 | } |
537 | | |
538 | 0 | if (p.tlssize != NULL && !OSSL_PARAM_get_size_t(p.tlssize, &macctx->tls_data_size)) |
539 | 0 | return 0; |
540 | | |
541 | 0 | return 1; |
542 | 0 | } |
543 | | |
544 | | const OSSL_DISPATCH ossl_hmac_functions[] = { |
545 | | { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))hmac_new }, |
546 | | { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))hmac_dup }, |
547 | | { OSSL_FUNC_MAC_FREECTX, (void (*)(void))hmac_free }, |
548 | | { OSSL_FUNC_MAC_INIT, (void (*)(void))hmac_init }, |
549 | | { OSSL_FUNC_MAC_UPDATE, (void (*)(void))hmac_update }, |
550 | | { OSSL_FUNC_MAC_FINAL, (void (*)(void))hmac_final }, |
551 | | { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS, |
552 | | (void (*)(void))hmac_gettable_ctx_params }, |
553 | | { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))hmac_get_ctx_params }, |
554 | | { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS, |
555 | | (void (*)(void))hmac_settable_ctx_params }, |
556 | | { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))hmac_set_ctx_params }, |
557 | | OSSL_DISPATCH_END |
558 | | }; |
559 | | |
560 | | #ifdef FIPS_MODULE |
561 | | static OSSL_FUNC_mac_newctx_fn hmac_internal_new; |
562 | | |
563 | | static void *hmac_internal_new(void *provctx) |
564 | | { |
565 | | struct hmac_data_st *macctx = hmac_new(provctx); |
566 | | |
567 | | if (macctx != NULL) |
568 | | macctx->internal = 1; |
569 | | return macctx; |
570 | | } |
571 | | |
572 | | const OSSL_DISPATCH ossl_hmac_internal_functions[] = { |
573 | | { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))hmac_internal_new }, |
574 | | { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))hmac_dup }, |
575 | | { OSSL_FUNC_MAC_FREECTX, (void (*)(void))hmac_free }, |
576 | | { OSSL_FUNC_MAC_INIT, (void (*)(void))hmac_init }, |
577 | | { OSSL_FUNC_MAC_UPDATE, (void (*)(void))hmac_update }, |
578 | | { OSSL_FUNC_MAC_FINAL, (void (*)(void))hmac_final }, |
579 | | { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS, |
580 | | (void (*)(void))hmac_gettable_ctx_params }, |
581 | | { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))hmac_get_ctx_params }, |
582 | | { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS, |
583 | | (void (*)(void))hmac_settable_ctx_params }, |
584 | | { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))hmac_set_ctx_params }, |
585 | | OSSL_DISPATCH_END |
586 | | }; |
587 | | |
588 | | #endif /* FIPS_MODULE */ |