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