/src/openssl36/providers/implementations/rands/drbg_hmac.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2011-2026 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 | | #include <stdlib.h> |
14 | | #include <string.h> |
15 | | #include <openssl/crypto.h> |
16 | | #include <openssl/err.h> |
17 | | #include <openssl/rand.h> |
18 | | #include <openssl/proverr.h> |
19 | | #include "internal/cryptlib.h" |
20 | | #include "internal/thread_once.h" |
21 | | #include "prov/providercommon.h" |
22 | | #include "prov/implementations.h" |
23 | | #include "prov/provider_ctx.h" |
24 | | #include "prov/hmac_drbg.h" |
25 | | #include "prov/drbg.h" |
26 | | #include "crypto/evp.h" |
27 | | #include "crypto/evp/evp_local.h" |
28 | | #include "internal/provider.h" |
29 | | |
30 | | static OSSL_FUNC_rand_newctx_fn drbg_hmac_new_wrapper; |
31 | | static OSSL_FUNC_rand_freectx_fn drbg_hmac_free; |
32 | | static OSSL_FUNC_rand_instantiate_fn drbg_hmac_instantiate_wrapper; |
33 | | static OSSL_FUNC_rand_uninstantiate_fn drbg_hmac_uninstantiate_wrapper; |
34 | | static OSSL_FUNC_rand_generate_fn drbg_hmac_generate_wrapper; |
35 | | static OSSL_FUNC_rand_reseed_fn drbg_hmac_reseed_wrapper; |
36 | | static OSSL_FUNC_rand_settable_ctx_params_fn drbg_hmac_settable_ctx_params; |
37 | | static OSSL_FUNC_rand_set_ctx_params_fn drbg_hmac_set_ctx_params; |
38 | | static OSSL_FUNC_rand_gettable_ctx_params_fn drbg_hmac_gettable_ctx_params; |
39 | | static OSSL_FUNC_rand_get_ctx_params_fn drbg_hmac_get_ctx_params; |
40 | | static OSSL_FUNC_rand_verify_zeroization_fn drbg_hmac_verify_zeroization; |
41 | | |
42 | | static int drbg_hmac_set_ctx_params_locked(PROV_DRBG *drbg, const struct drbg_set_ctx_params_st *p); |
43 | | static int drbg_hmac_set_ctx_params_decoder(const OSSL_PARAM params[], |
44 | | struct drbg_set_ctx_params_st *p); |
45 | | |
46 | | /* |
47 | | * Called twice by SP800-90Ar1 10.1.2.2 HMAC_DRBG_Update_Process. |
48 | | * |
49 | | * hmac is an object that holds the input/output Key and Value (K and V). |
50 | | * inbyte is 0x00 on the first call and 0x01 on the second call. |
51 | | * in1, in2, in3 are optional inputs that can be NULL. |
52 | | * in1len, in2len, in3len are the lengths of the input buffers. |
53 | | * |
54 | | * The returned K,V is: |
55 | | * hmac->K = HMAC(hmac->K, hmac->V || inbyte || [in1] || [in2] || [in3]) |
56 | | * hmac->V = HMAC(hmac->K, hmac->V) |
57 | | * |
58 | | * Returns zero if an error occurs otherwise it returns 1. |
59 | | */ |
60 | | static int do_hmac(PROV_DRBG_HMAC *hmac, unsigned char inbyte, |
61 | | const unsigned char *in1, size_t in1len, |
62 | | const unsigned char *in2, size_t in2len, |
63 | | const unsigned char *in3, size_t in3len) |
64 | 2.89k | { |
65 | 2.89k | EVP_MAC_CTX *ctx = hmac->ctx; |
66 | | |
67 | 2.89k | if (!EVP_MAC_init(ctx, hmac->K, hmac->blocklen, NULL) |
68 | | /* K = HMAC(K, V || inbyte || [in1] || [in2] || [in3]) */ |
69 | 2.87k | || !EVP_MAC_update(ctx, hmac->V, hmac->blocklen) |
70 | 2.87k | || !EVP_MAC_update(ctx, &inbyte, 1) |
71 | 2.87k | || !(in1 == NULL || in1len == 0 || EVP_MAC_update(ctx, in1, in1len)) |
72 | 2.87k | || !(in2 == NULL || in2len == 0 || EVP_MAC_update(ctx, in2, in2len)) |
73 | 2.87k | || !(in3 == NULL || in3len == 0 || EVP_MAC_update(ctx, in3, in3len)) |
74 | 2.87k | || !EVP_MAC_final(ctx, hmac->K, NULL, sizeof(hmac->K))) |
75 | 23 | return 0; |
76 | | |
77 | | /* V = HMAC(K, V) */ |
78 | 2.87k | return EVP_MAC_init(ctx, hmac->K, hmac->blocklen, NULL) |
79 | 2.87k | && EVP_MAC_update(ctx, hmac->V, hmac->blocklen) |
80 | 2.87k | && EVP_MAC_final(ctx, hmac->V, NULL, sizeof(hmac->V)); |
81 | 2.89k | } |
82 | | |
83 | | /* |
84 | | * SP800-90Ar1 10.1.2.2 HMAC_DRBG_Update_Process |
85 | | * |
86 | | * |
87 | | * Updates the drbg objects Key(K) and Value(V) using the following algorithm: |
88 | | * K,V = do_hmac(hmac, 0, in1, in2, in3) |
89 | | * if (any input is not NULL) |
90 | | * K,V = do_hmac(hmac, 1, in1, in2, in3) |
91 | | * |
92 | | * where in1, in2, in3 are optional input buffers that can be NULL. |
93 | | * in1len, in2len, in3len are the lengths of the input buffers. |
94 | | * |
95 | | * Returns zero if an error occurs otherwise it returns 1. |
96 | | */ |
97 | | static int drbg_hmac_update(PROV_DRBG_HMAC *hmac, |
98 | | const unsigned char *in1, size_t in1len, |
99 | | const unsigned char *in2, size_t in2len, |
100 | | const unsigned char *in3, size_t in3len) |
101 | 1.79k | { |
102 | | /* (Steps 1-2) K = HMAC(K, V||0x00||provided_data). V = HMAC(K,V) */ |
103 | 1.79k | if (!do_hmac(hmac, 0x00, in1, in1len, in2, in2len, in3, in3len)) |
104 | 23 | return 0; |
105 | | /* (Step 3) If provided_data == NULL then return (K,V) */ |
106 | 1.77k | if (in1len == 0 && in2len == 0 && in3len == 0) |
107 | 676 | return 1; |
108 | | /* (Steps 4-5) K = HMAC(K, V||0x01||provided_data). V = HMAC(K,V) */ |
109 | 1.09k | return do_hmac(hmac, 0x01, in1, in1len, in2, in2len, in3, in3len); |
110 | 1.77k | } |
111 | | |
112 | | /* |
113 | | * SP800-90Ar1 10.1.2.3 HMAC_DRBG_Instantiate_Process: |
114 | | * |
115 | | * This sets the drbg Key (K) to all zeros, and Value (V) to all 1's. |
116 | | * and then calls (K,V) = drbg_hmac_update() with input parameters: |
117 | | * ent = entropy data (Can be NULL) of length ent_len. |
118 | | * nonce = nonce data (Can be NULL) of length nonce_len. |
119 | | * pstr = personalization data (Can be NULL) of length pstr_len. |
120 | | * |
121 | | * Returns zero if an error occurs otherwise it returns 1. |
122 | | */ |
123 | | int ossl_drbg_hmac_init(PROV_DRBG_HMAC *hmac, |
124 | | const unsigned char *ent, size_t ent_len, |
125 | | const unsigned char *nonce, size_t nonce_len, |
126 | | const unsigned char *pstr, size_t pstr_len) |
127 | 725 | { |
128 | 725 | if (hmac->ctx == NULL) { |
129 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MAC); |
130 | 0 | return 0; |
131 | 0 | } |
132 | | |
133 | | /* (Step 2) Key = 0x00 00...00 */ |
134 | 725 | memset(hmac->K, 0x00, hmac->blocklen); |
135 | | /* (Step 3) V = 0x01 01...01 */ |
136 | 725 | memset(hmac->V, 0x01, hmac->blocklen); |
137 | | /* (Step 4) (K,V) = HMAC_DRBG_Update(entropy||nonce||pers string, K, V) */ |
138 | 725 | return drbg_hmac_update(hmac, ent, ent_len, nonce, nonce_len, pstr, |
139 | 725 | pstr_len); |
140 | 725 | } |
141 | | static int drbg_hmac_instantiate(PROV_DRBG *drbg, |
142 | | const unsigned char *ent, size_t ent_len, |
143 | | const unsigned char *nonce, size_t nonce_len, |
144 | | const unsigned char *pstr, size_t pstr_len) |
145 | 388 | { |
146 | 388 | return ossl_drbg_hmac_init((PROV_DRBG_HMAC *)drbg->data, ent, ent_len, |
147 | 388 | nonce, nonce_len, pstr, pstr_len); |
148 | 388 | } |
149 | | |
150 | | static int drbg_hmac_instantiate_wrapper(void *vdrbg, unsigned int strength, |
151 | | int prediction_resistance, |
152 | | const unsigned char *pstr, |
153 | | size_t pstr_len, |
154 | | const OSSL_PARAM params[]) |
155 | 0 | { |
156 | 0 | PROV_DRBG *drbg = (PROV_DRBG *)vdrbg; |
157 | 0 | struct drbg_set_ctx_params_st p; |
158 | 0 | int ret = 0; |
159 | |
|
160 | 0 | if (drbg == NULL || !drbg_hmac_set_ctx_params_decoder(params, &p)) |
161 | 0 | return 0; |
162 | | |
163 | 0 | if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock)) |
164 | 0 | return 0; |
165 | | |
166 | 0 | if (!ossl_prov_is_running() |
167 | 0 | || !drbg_hmac_set_ctx_params_locked(drbg, &p)) |
168 | 0 | goto err; |
169 | 0 | ret = ossl_prov_drbg_instantiate(drbg, strength, prediction_resistance, |
170 | 0 | pstr, pstr_len); |
171 | 0 | err: |
172 | 0 | if (drbg->lock != NULL) |
173 | 0 | CRYPTO_THREAD_unlock(drbg->lock); |
174 | 0 | return ret; |
175 | 0 | } |
176 | | |
177 | | /* |
178 | | * SP800-90Ar1 10.1.2.4 HMAC_DRBG_Reseed_Process: |
179 | | * |
180 | | * Reseeds the drbg's Key (K) and Value (V) by calling |
181 | | * (K,V) = drbg_hmac_update() with the following input parameters: |
182 | | * ent = entropy input data (Can be NULL) of length ent_len. |
183 | | * adin = additional input data (Can be NULL) of length adin_len. |
184 | | * |
185 | | * Returns zero if an error occurs otherwise it returns 1. |
186 | | */ |
187 | | static int drbg_hmac_reseed(PROV_DRBG *drbg, |
188 | | const unsigned char *ent, size_t ent_len, |
189 | | const unsigned char *adin, size_t adin_len) |
190 | 396 | { |
191 | 396 | PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data; |
192 | | |
193 | | /* (Step 2) (K,V) = HMAC_DRBG_Update(entropy||additional_input, K, V) */ |
194 | 396 | return drbg_hmac_update(hmac, ent, ent_len, adin, adin_len, NULL, 0); |
195 | 396 | } |
196 | | |
197 | | static int drbg_hmac_reseed_wrapper(void *vdrbg, int prediction_resistance, |
198 | | const unsigned char *ent, size_t ent_len, |
199 | | const unsigned char *adin, size_t adin_len) |
200 | 339 | { |
201 | 339 | PROV_DRBG *drbg = (PROV_DRBG *)vdrbg; |
202 | | |
203 | 339 | return ossl_prov_drbg_reseed(drbg, prediction_resistance, ent, ent_len, |
204 | 339 | adin, adin_len); |
205 | 339 | } |
206 | | |
207 | | /* |
208 | | * SP800-90Ar1 10.1.2.5 HMAC_DRBG_Generate_Process: |
209 | | * |
210 | | * Generates pseudo random bytes and updates the internal K,V for the drbg. |
211 | | * out is a buffer to fill with outlen bytes of pseudo random data. |
212 | | * adin is an additional_input string of size adin_len that may be NULL. |
213 | | * |
214 | | * Returns zero if an error occurs otherwise it returns 1. |
215 | | */ |
216 | | int ossl_drbg_hmac_generate(PROV_DRBG_HMAC *hmac, |
217 | | unsigned char *out, size_t outlen, |
218 | | const unsigned char *adin, size_t adin_len) |
219 | 702 | { |
220 | 702 | EVP_MAC_CTX *ctx = hmac->ctx; |
221 | 702 | const unsigned char *temp = hmac->V; |
222 | | |
223 | | /* (Step 2) if adin != NULL then (K,V) = HMAC_DRBG_Update(adin, K, V) */ |
224 | 702 | if (adin != NULL |
225 | 0 | && adin_len > 0 |
226 | 0 | && !drbg_hmac_update(hmac, adin, adin_len, NULL, 0, NULL, 0)) |
227 | 0 | return 0; |
228 | | |
229 | | /* |
230 | | * (Steps 3-5) temp = NULL |
231 | | * while (len(temp) < outlen) { |
232 | | * V = HMAC(K, V) |
233 | | * temp = temp || V |
234 | | * } |
235 | | */ |
236 | 54.0k | for (;;) { |
237 | 54.0k | if (!EVP_MAC_init(ctx, hmac->K, hmac->blocklen, NULL) |
238 | 54.0k | || !EVP_MAC_update(ctx, temp, hmac->blocklen)) |
239 | 0 | return 0; |
240 | | |
241 | 54.0k | if (outlen > hmac->blocklen) { |
242 | 53.4k | if (!EVP_MAC_final(ctx, out, NULL, outlen)) |
243 | 26 | return 0; |
244 | 53.3k | temp = out; |
245 | 53.3k | } else { |
246 | 676 | if (!EVP_MAC_final(ctx, hmac->V, NULL, sizeof(hmac->V))) |
247 | 0 | return 0; |
248 | 676 | memcpy(out, hmac->V, outlen); |
249 | 676 | break; |
250 | 676 | } |
251 | 53.3k | out += hmac->blocklen; |
252 | 53.3k | outlen -= hmac->blocklen; |
253 | 53.3k | } |
254 | | /* (Step 6) (K,V) = HMAC_DRBG_Update(adin, K, V) */ |
255 | 676 | if (!drbg_hmac_update(hmac, adin, adin_len, NULL, 0, NULL, 0)) |
256 | 0 | return 0; |
257 | | |
258 | 676 | return 1; |
259 | 676 | } |
260 | | |
261 | | static int drbg_hmac_generate(PROV_DRBG *drbg, |
262 | | unsigned char *out, size_t outlen, |
263 | | const unsigned char *adin, size_t adin_len) |
264 | 365 | { |
265 | 365 | return ossl_drbg_hmac_generate((PROV_DRBG_HMAC *)drbg->data, out, outlen, |
266 | 365 | adin, adin_len); |
267 | 365 | } |
268 | | |
269 | | static int drbg_hmac_generate_wrapper(void *vdrbg, |
270 | | unsigned char *out, size_t outlen, unsigned int strength, |
271 | | int prediction_resistance, const unsigned char *adin, size_t adin_len) |
272 | 388 | { |
273 | 388 | PROV_DRBG *drbg = (PROV_DRBG *)vdrbg; |
274 | | |
275 | 388 | return ossl_prov_drbg_generate(drbg, out, outlen, strength, |
276 | 388 | prediction_resistance, adin, adin_len); |
277 | 388 | } |
278 | | |
279 | | static int drbg_hmac_uninstantiate(PROV_DRBG *drbg) |
280 | 0 | { |
281 | 0 | PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data; |
282 | |
|
283 | 0 | OPENSSL_cleanse(hmac->K, sizeof(hmac->K)); |
284 | 0 | OPENSSL_cleanse(hmac->V, sizeof(hmac->V)); |
285 | 0 | return ossl_prov_drbg_uninstantiate(drbg); |
286 | 0 | } |
287 | | |
288 | | static int drbg_hmac_uninstantiate_wrapper(void *vdrbg) |
289 | 0 | { |
290 | 0 | PROV_DRBG *drbg = (PROV_DRBG *)vdrbg; |
291 | 0 | int ret; |
292 | |
|
293 | 0 | if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock)) |
294 | 0 | return 0; |
295 | | |
296 | 0 | ret = drbg_hmac_uninstantiate(drbg); |
297 | |
|
298 | 0 | if (drbg->lock != NULL) |
299 | 0 | CRYPTO_THREAD_unlock(drbg->lock); |
300 | |
|
301 | 0 | return ret; |
302 | 0 | } |
303 | | |
304 | | static int drbg_hmac_verify_zeroization(void *vdrbg) |
305 | 0 | { |
306 | 0 | PROV_DRBG *drbg = (PROV_DRBG *)vdrbg; |
307 | 0 | PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data; |
308 | 0 | int ret = 0; |
309 | |
|
310 | 0 | if (drbg->lock != NULL && !CRYPTO_THREAD_read_lock(drbg->lock)) |
311 | 0 | return 0; |
312 | | |
313 | 0 | PROV_DRBG_VERIFY_ZEROIZATION(hmac->K); |
314 | 0 | PROV_DRBG_VERIFY_ZEROIZATION(hmac->V); |
315 | |
|
316 | 0 | ret = 1; |
317 | 0 | err: |
318 | 0 | if (drbg->lock != NULL) |
319 | 0 | CRYPTO_THREAD_unlock(drbg->lock); |
320 | 0 | return ret; |
321 | 0 | } |
322 | | |
323 | | static int drbg_hmac_new(PROV_DRBG *drbg) |
324 | 567 | { |
325 | 567 | PROV_DRBG_HMAC *hmac; |
326 | | |
327 | 567 | hmac = OPENSSL_secure_zalloc(sizeof(*hmac)); |
328 | 567 | if (hmac == NULL) |
329 | 0 | return 0; |
330 | | |
331 | 567 | OSSL_FIPS_IND_INIT(drbg) |
332 | | |
333 | 567 | drbg->data = hmac; |
334 | | /* See SP800-57 Part1 Rev4 5.6.1 Table 3 */ |
335 | 567 | drbg->max_entropylen = DRBG_MAX_LENGTH; |
336 | 567 | drbg->max_noncelen = DRBG_MAX_LENGTH; |
337 | 567 | drbg->max_perslen = DRBG_MAX_LENGTH; |
338 | 567 | drbg->max_adinlen = DRBG_MAX_LENGTH; |
339 | | |
340 | | /* Maximum number of bits per request = 2^19 = 2^16 bytes */ |
341 | 567 | drbg->max_request = 1 << 16; |
342 | 567 | return 1; |
343 | 567 | } |
344 | | |
345 | | static void *drbg_hmac_new_wrapper(void *provctx, void *parent, |
346 | | const OSSL_DISPATCH *parent_dispatch) |
347 | 567 | { |
348 | 567 | return ossl_rand_drbg_new(provctx, parent, parent_dispatch, |
349 | 567 | &drbg_hmac_new, &drbg_hmac_free, |
350 | 567 | &drbg_hmac_instantiate, &drbg_hmac_uninstantiate, |
351 | 567 | &drbg_hmac_reseed, &drbg_hmac_generate); |
352 | 567 | } |
353 | | |
354 | | static void drbg_hmac_free(void *vdrbg) |
355 | 567 | { |
356 | 567 | PROV_DRBG *drbg = (PROV_DRBG *)vdrbg; |
357 | 567 | PROV_DRBG_HMAC *hmac; |
358 | | |
359 | 567 | if (drbg != NULL && (hmac = (PROV_DRBG_HMAC *)drbg->data) != NULL) { |
360 | 567 | EVP_MAC_CTX_free(hmac->ctx); |
361 | 567 | ossl_prov_digest_reset(&hmac->digest); |
362 | 567 | OPENSSL_secure_clear_free(hmac, sizeof(*hmac)); |
363 | 567 | } |
364 | 567 | ossl_rand_drbg_free(drbg); |
365 | 567 | } |
366 | | |
367 | | #define drbg_hmac_get_ctx_params_st drbg_get_ctx_params_st |
368 | | |
369 | | /* clang-format off */ |
370 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
371 | | #ifndef drbg_hmac_get_ctx_params_list |
372 | | static const OSSL_PARAM drbg_hmac_get_ctx_params_list[] = { |
373 | | OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_MAC, NULL, 0), |
374 | | OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_DIGEST, NULL, 0), |
375 | | OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL), |
376 | | OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL), |
377 | | OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL), |
378 | | OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MIN_ENTROPYLEN, NULL), |
379 | | OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_ENTROPYLEN, NULL), |
380 | | OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MIN_NONCELEN, NULL), |
381 | | OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_NONCELEN, NULL), |
382 | | OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_PERSLEN, NULL), |
383 | | OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_ADINLEN, NULL), |
384 | | OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_COUNTER, NULL), |
385 | | OSSL_PARAM_time_t(OSSL_DRBG_PARAM_RESEED_TIME, NULL), |
386 | | OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS, NULL), |
387 | | OSSL_PARAM_uint64(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL, NULL), |
388 | | # if defined(FIPS_MODULE) |
389 | | OSSL_PARAM_int(OSSL_KDF_PARAM_FIPS_APPROVED_INDICATOR, NULL), |
390 | | # endif |
391 | | OSSL_PARAM_END |
392 | | }; |
393 | | #endif |
394 | | |
395 | | #ifndef drbg_hmac_get_ctx_params_st |
396 | | struct drbg_hmac_get_ctx_params_st { |
397 | | OSSL_PARAM *digest; |
398 | | # if defined(FIPS_MODULE) |
399 | | OSSL_PARAM *ind; |
400 | | # endif |
401 | | OSSL_PARAM *mac; |
402 | | OSSL_PARAM *maxadlen; |
403 | | OSSL_PARAM *maxentlen; |
404 | | OSSL_PARAM *maxnonlen; |
405 | | OSSL_PARAM *maxperlen; |
406 | | OSSL_PARAM *maxreq; |
407 | | OSSL_PARAM *minentlen; |
408 | | OSSL_PARAM *minnonlen; |
409 | | OSSL_PARAM *reseed_cnt; |
410 | | OSSL_PARAM *reseed_int; |
411 | | OSSL_PARAM *reseed_req; |
412 | | OSSL_PARAM *reseed_time; |
413 | | OSSL_PARAM *state; |
414 | | OSSL_PARAM *str; |
415 | | }; |
416 | | #endif |
417 | | |
418 | | #ifndef drbg_hmac_get_ctx_params_decoder |
419 | | static int drbg_hmac_get_ctx_params_decoder |
420 | | (const OSSL_PARAM *p, struct drbg_hmac_get_ctx_params_st *r) |
421 | 61 | { |
422 | 61 | const char *s; |
423 | | |
424 | 61 | memset(r, 0, sizeof(*r)); |
425 | 61 | if (p != NULL) |
426 | 122 | for (; (s = p->key) != NULL; p++) |
427 | 61 | switch(s[0]) { |
428 | 0 | default: |
429 | 0 | break; |
430 | 0 | case 'd': |
431 | 0 | if (ossl_likely(strcmp("igest", s + 1) == 0)) { |
432 | | /* OSSL_DRBG_PARAM_DIGEST */ |
433 | 0 | if (ossl_unlikely(r->digest != NULL)) { |
434 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
435 | 0 | "param %s is repeated", s); |
436 | 0 | return 0; |
437 | 0 | } |
438 | 0 | r->digest = (OSSL_PARAM *)p; |
439 | 0 | } |
440 | 0 | break; |
441 | 0 | case 'f': |
442 | | # if defined(FIPS_MODULE) |
443 | | if (ossl_likely(strcmp("ips-indicator", s + 1) == 0)) { |
444 | | /* OSSL_KDF_PARAM_FIPS_APPROVED_INDICATOR */ |
445 | | if (ossl_unlikely(r->ind != NULL)) { |
446 | | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
447 | | "param %s is repeated", s); |
448 | | return 0; |
449 | | } |
450 | | r->ind = (OSSL_PARAM *)p; |
451 | | } |
452 | | # endif |
453 | 0 | break; |
454 | 61 | case 'm': |
455 | 61 | switch(s[1]) { |
456 | 0 | default: |
457 | 0 | break; |
458 | 61 | case 'a': |
459 | 61 | switch(s[2]) { |
460 | 0 | default: |
461 | 0 | break; |
462 | 0 | case 'c': |
463 | 0 | switch(s[3]) { |
464 | 0 | default: |
465 | 0 | break; |
466 | 0 | case '\0': |
467 | 0 | if (ossl_unlikely(r->mac != NULL)) { |
468 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
469 | 0 | "param %s is repeated", s); |
470 | 0 | return 0; |
471 | 0 | } |
472 | 0 | r->mac = (OSSL_PARAM *)p; |
473 | 0 | } |
474 | 0 | break; |
475 | 61 | case 'x': |
476 | 61 | switch(s[3]) { |
477 | 0 | default: |
478 | 0 | break; |
479 | 61 | case '_': |
480 | 61 | switch(s[4]) { |
481 | 0 | default: |
482 | 0 | break; |
483 | 0 | case 'a': |
484 | 0 | if (ossl_likely(strcmp("dinlen", s + 5) == 0)) { |
485 | | /* OSSL_DRBG_PARAM_MAX_ADINLEN */ |
486 | 0 | if (ossl_unlikely(r->maxadlen != NULL)) { |
487 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
488 | 0 | "param %s is repeated", s); |
489 | 0 | return 0; |
490 | 0 | } |
491 | 0 | r->maxadlen = (OSSL_PARAM *)p; |
492 | 0 | } |
493 | 0 | break; |
494 | 0 | case 'e': |
495 | 0 | if (ossl_likely(strcmp("ntropylen", s + 5) == 0)) { |
496 | | /* OSSL_DRBG_PARAM_MAX_ENTROPYLEN */ |
497 | 0 | if (ossl_unlikely(r->maxentlen != NULL)) { |
498 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
499 | 0 | "param %s is repeated", s); |
500 | 0 | return 0; |
501 | 0 | } |
502 | 0 | r->maxentlen = (OSSL_PARAM *)p; |
503 | 0 | } |
504 | 0 | break; |
505 | 0 | case 'n': |
506 | 0 | if (ossl_likely(strcmp("oncelen", s + 5) == 0)) { |
507 | | /* OSSL_DRBG_PARAM_MAX_NONCELEN */ |
508 | 0 | if (ossl_unlikely(r->maxnonlen != NULL)) { |
509 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
510 | 0 | "param %s is repeated", s); |
511 | 0 | return 0; |
512 | 0 | } |
513 | 0 | r->maxnonlen = (OSSL_PARAM *)p; |
514 | 0 | } |
515 | 0 | break; |
516 | 0 | case 'p': |
517 | 0 | if (ossl_likely(strcmp("erslen", s + 5) == 0)) { |
518 | | /* OSSL_DRBG_PARAM_MAX_PERSLEN */ |
519 | 0 | if (ossl_unlikely(r->maxperlen != NULL)) { |
520 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
521 | 0 | "param %s is repeated", s); |
522 | 0 | return 0; |
523 | 0 | } |
524 | 0 | r->maxperlen = (OSSL_PARAM *)p; |
525 | 0 | } |
526 | 0 | break; |
527 | 61 | case 'r': |
528 | 61 | if (ossl_likely(strcmp("equest", s + 5) == 0)) { |
529 | | /* OSSL_RAND_PARAM_MAX_REQUEST */ |
530 | 61 | if (ossl_unlikely(r->maxreq != NULL)) { |
531 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
532 | 0 | "param %s is repeated", s); |
533 | 0 | return 0; |
534 | 0 | } |
535 | 61 | r->maxreq = (OSSL_PARAM *)p; |
536 | 61 | } |
537 | 61 | } |
538 | 61 | } |
539 | 61 | } |
540 | 61 | break; |
541 | 61 | case 'i': |
542 | 0 | switch(s[2]) { |
543 | 0 | default: |
544 | 0 | break; |
545 | 0 | case 'n': |
546 | 0 | switch(s[3]) { |
547 | 0 | default: |
548 | 0 | break; |
549 | 0 | case '_': |
550 | 0 | switch(s[4]) { |
551 | 0 | default: |
552 | 0 | break; |
553 | 0 | case 'e': |
554 | 0 | if (ossl_likely(strcmp("ntropylen", s + 5) == 0)) { |
555 | | /* OSSL_DRBG_PARAM_MIN_ENTROPYLEN */ |
556 | 0 | if (ossl_unlikely(r->minentlen != NULL)) { |
557 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
558 | 0 | "param %s is repeated", s); |
559 | 0 | return 0; |
560 | 0 | } |
561 | 0 | r->minentlen = (OSSL_PARAM *)p; |
562 | 0 | } |
563 | 0 | break; |
564 | 0 | case 'n': |
565 | 0 | if (ossl_likely(strcmp("oncelen", s + 5) == 0)) { |
566 | | /* OSSL_DRBG_PARAM_MIN_NONCELEN */ |
567 | 0 | if (ossl_unlikely(r->minnonlen != NULL)) { |
568 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
569 | 0 | "param %s is repeated", s); |
570 | 0 | return 0; |
571 | 0 | } |
572 | 0 | r->minnonlen = (OSSL_PARAM *)p; |
573 | 0 | } |
574 | 0 | } |
575 | 0 | } |
576 | 0 | } |
577 | 61 | } |
578 | 61 | break; |
579 | 61 | case 'r': |
580 | 0 | switch(s[1]) { |
581 | 0 | default: |
582 | 0 | break; |
583 | 0 | case 'e': |
584 | 0 | switch(s[2]) { |
585 | 0 | default: |
586 | 0 | break; |
587 | 0 | case 's': |
588 | 0 | switch(s[3]) { |
589 | 0 | default: |
590 | 0 | break; |
591 | 0 | case 'e': |
592 | 0 | switch(s[4]) { |
593 | 0 | default: |
594 | 0 | break; |
595 | 0 | case 'e': |
596 | 0 | switch(s[5]) { |
597 | 0 | default: |
598 | 0 | break; |
599 | 0 | case 'd': |
600 | 0 | switch(s[6]) { |
601 | 0 | default: |
602 | 0 | break; |
603 | 0 | case '_': |
604 | 0 | switch(s[7]) { |
605 | 0 | default: |
606 | 0 | break; |
607 | 0 | case 'c': |
608 | 0 | if (ossl_likely(strcmp("ounter", s + 8) == 0)) { |
609 | | /* OSSL_DRBG_PARAM_RESEED_COUNTER */ |
610 | 0 | if (ossl_unlikely(r->reseed_cnt != NULL)) { |
611 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
612 | 0 | "param %s is repeated", s); |
613 | 0 | return 0; |
614 | 0 | } |
615 | 0 | r->reseed_cnt = (OSSL_PARAM *)p; |
616 | 0 | } |
617 | 0 | break; |
618 | 0 | case 'r': |
619 | 0 | if (ossl_likely(strcmp("equests", s + 8) == 0)) { |
620 | | /* OSSL_DRBG_PARAM_RESEED_REQUESTS */ |
621 | 0 | if (ossl_unlikely(r->reseed_req != NULL)) { |
622 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
623 | 0 | "param %s is repeated", s); |
624 | 0 | return 0; |
625 | 0 | } |
626 | 0 | r->reseed_req = (OSSL_PARAM *)p; |
627 | 0 | } |
628 | 0 | break; |
629 | 0 | case 't': |
630 | 0 | switch(s[8]) { |
631 | 0 | default: |
632 | 0 | break; |
633 | 0 | case 'i': |
634 | 0 | switch(s[9]) { |
635 | 0 | default: |
636 | 0 | break; |
637 | 0 | case 'm': |
638 | 0 | switch(s[10]) { |
639 | 0 | default: |
640 | 0 | break; |
641 | 0 | case 'e': |
642 | 0 | switch(s[11]) { |
643 | 0 | default: |
644 | 0 | break; |
645 | 0 | case '_': |
646 | 0 | if (ossl_likely(strcmp("interval", s + 12) == 0)) { |
647 | | /* OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL */ |
648 | 0 | if (ossl_unlikely(r->reseed_int != NULL)) { |
649 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
650 | 0 | "param %s is repeated", s); |
651 | 0 | return 0; |
652 | 0 | } |
653 | 0 | r->reseed_int = (OSSL_PARAM *)p; |
654 | 0 | } |
655 | 0 | break; |
656 | 0 | case '\0': |
657 | 0 | if (ossl_unlikely(r->reseed_time != NULL)) { |
658 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
659 | 0 | "param %s is repeated", s); |
660 | 0 | return 0; |
661 | 0 | } |
662 | 0 | r->reseed_time = (OSSL_PARAM *)p; |
663 | 0 | } |
664 | 0 | } |
665 | 0 | } |
666 | 0 | } |
667 | 0 | } |
668 | 0 | } |
669 | 0 | } |
670 | 0 | } |
671 | 0 | } |
672 | 0 | } |
673 | 0 | } |
674 | 0 | break; |
675 | 0 | case 's': |
676 | 0 | switch(s[1]) { |
677 | 0 | default: |
678 | 0 | break; |
679 | 0 | case 't': |
680 | 0 | switch(s[2]) { |
681 | 0 | default: |
682 | 0 | break; |
683 | 0 | case 'a': |
684 | 0 | if (ossl_likely(strcmp("te", s + 3) == 0)) { |
685 | | /* OSSL_RAND_PARAM_STATE */ |
686 | 0 | if (ossl_unlikely(r->state != NULL)) { |
687 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
688 | 0 | "param %s is repeated", s); |
689 | 0 | return 0; |
690 | 0 | } |
691 | 0 | r->state = (OSSL_PARAM *)p; |
692 | 0 | } |
693 | 0 | break; |
694 | 0 | case 'r': |
695 | 0 | if (ossl_likely(strcmp("ength", s + 3) == 0)) { |
696 | | /* OSSL_RAND_PARAM_STRENGTH */ |
697 | 0 | if (ossl_unlikely(r->str != NULL)) { |
698 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
699 | 0 | "param %s is repeated", s); |
700 | 0 | return 0; |
701 | 0 | } |
702 | 0 | r->str = (OSSL_PARAM *)p; |
703 | 0 | } |
704 | 0 | } |
705 | 0 | } |
706 | 61 | } |
707 | 61 | return 1; |
708 | 61 | } |
709 | | #endif |
710 | | /* End of machine generated */ |
711 | | /* clang-format on */ |
712 | | |
713 | | static int drbg_hmac_get_ctx_params(void *vdrbg, OSSL_PARAM params[]) |
714 | 61 | { |
715 | 61 | PROV_DRBG *drbg = (PROV_DRBG *)vdrbg; |
716 | 61 | PROV_DRBG_HMAC *hmac; |
717 | 61 | const char *name; |
718 | 61 | const EVP_MD *md; |
719 | 61 | struct drbg_get_ctx_params_st p; |
720 | 61 | int ret = 0, complete = 0; |
721 | | |
722 | 61 | if (drbg == NULL || !drbg_hmac_get_ctx_params_decoder(params, &p)) |
723 | 0 | return 0; |
724 | | |
725 | 61 | if (!ossl_drbg_get_ctx_params_no_lock(drbg, &p, params, &complete)) |
726 | 0 | return 0; |
727 | | |
728 | 61 | if (complete) |
729 | 61 | return 1; |
730 | | |
731 | 0 | hmac = (PROV_DRBG_HMAC *)drbg->data; |
732 | |
|
733 | 0 | if (drbg->lock != NULL && !CRYPTO_THREAD_read_lock(drbg->lock)) |
734 | 0 | return 0; |
735 | | |
736 | 0 | if (p.mac != NULL) { |
737 | 0 | if (hmac->ctx == NULL) |
738 | 0 | goto err; |
739 | 0 | name = EVP_MAC_get0_name(EVP_MAC_CTX_get0_mac(hmac->ctx)); |
740 | 0 | if (!OSSL_PARAM_set_utf8_string(p.mac, name)) |
741 | 0 | goto err; |
742 | 0 | } |
743 | | |
744 | 0 | if (p.digest != NULL) { |
745 | 0 | md = ossl_prov_digest_md(&hmac->digest); |
746 | 0 | if (md == NULL |
747 | 0 | || !OSSL_PARAM_set_utf8_string(p.digest, EVP_MD_get0_name(md))) |
748 | 0 | goto err; |
749 | 0 | } |
750 | | |
751 | 0 | ret = ossl_drbg_get_ctx_params(drbg, &p); |
752 | 0 | err: |
753 | 0 | if (drbg->lock != NULL) |
754 | 0 | CRYPTO_THREAD_unlock(drbg->lock); |
755 | |
|
756 | 0 | return ret; |
757 | 0 | } |
758 | | |
759 | | static const OSSL_PARAM *drbg_hmac_gettable_ctx_params(ossl_unused void *vctx, |
760 | | ossl_unused void *p_ctx) |
761 | 0 | { |
762 | 0 | return drbg_hmac_get_ctx_params_list; |
763 | 0 | } |
764 | | |
765 | | static int drbg_fetch_algs_from_prov(const struct drbg_set_ctx_params_st *p, |
766 | | OSSL_LIB_CTX *libctx, |
767 | | EVP_MAC_CTX **macctx, |
768 | | EVP_MD **digest, const char *propq) |
769 | 95 | { |
770 | 95 | EVP_MD *md = NULL; |
771 | 95 | EVP_MAC *mac = NULL; |
772 | 95 | int ret = 0; |
773 | 95 | const char *propquery = NULL; |
774 | | |
775 | 95 | #ifndef FIPS_MODULE |
776 | 95 | if (propq == NULL) |
777 | 0 | propquery = "provider=default"; |
778 | 95 | else |
779 | 95 | propquery = propq; |
780 | 95 | #endif |
781 | | |
782 | 95 | if (macctx == NULL || digest == NULL) |
783 | 0 | return 0; |
784 | | |
785 | 95 | if (p->digest != NULL) { |
786 | 95 | if (p->digest->data_type != OSSL_PARAM_UTF8_STRING) |
787 | 0 | goto done; |
788 | 95 | md = EVP_MD_fetch(libctx, p->digest->data, propquery); |
789 | 95 | if (md) { |
790 | 76 | EVP_MD_free(*digest); |
791 | 76 | *digest = md; |
792 | 76 | } else { |
793 | 19 | goto done; |
794 | 19 | } |
795 | 95 | } |
796 | | |
797 | 76 | if (p->mac == NULL) { |
798 | 0 | ret = 1; |
799 | 0 | goto done; |
800 | 0 | } |
801 | | |
802 | 76 | if (p->mac->data_type != OSSL_PARAM_UTF8_STRING) |
803 | 0 | goto done; |
804 | | |
805 | 76 | EVP_MAC_CTX_free(*macctx); |
806 | 76 | *macctx = NULL; |
807 | | |
808 | 76 | mac = EVP_MAC_fetch(libctx, p->mac->data, propquery); |
809 | 76 | if (mac) { |
810 | 64 | *macctx = EVP_MAC_CTX_new(mac); |
811 | | /* The context holds on to the MAC */ |
812 | 64 | EVP_MAC_free(mac); |
813 | 64 | ret = 1; |
814 | 64 | } |
815 | | |
816 | 95 | done: |
817 | 95 | return ret; |
818 | 76 | } |
819 | | |
820 | | static int drbg_hmac_set_ctx_params_locked(PROV_DRBG *ctx, const struct drbg_set_ctx_params_st *p) |
821 | 95 | { |
822 | 95 | PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)ctx->data; |
823 | 95 | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
824 | 95 | EVP_MD *prov_md = NULL; |
825 | 95 | const EVP_MD *md; |
826 | 95 | int md_size; |
827 | | |
828 | 95 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p->ind_d)) |
829 | 0 | return 0; |
830 | | |
831 | | /* try to fetch mac and digest from provider */ |
832 | 95 | (void)ERR_set_mark(); |
833 | 95 | if (!drbg_fetch_algs_from_prov(p, libctx, &hmac->ctx, &prov_md, |
834 | 95 | (p->propq != NULL && p->propq->data_type == OSSL_PARAM_UTF8_STRING) ? p->propq->data : NULL)) { |
835 | 31 | (void)ERR_pop_to_mark(); |
836 | | /* |
837 | | * Its possible for drbg_fetch_algs_from_prov to return 0 and set prov_md here |
838 | | * so we need to free prov_md to be leak free |
839 | | */ |
840 | 31 | EVP_MD_free(prov_md); |
841 | | /* fall back to full implementation search */ |
842 | 31 | if (!ossl_prov_digest_load(&hmac->digest, p->digest, p->propq, |
843 | 31 | p->engine, libctx)) |
844 | 19 | return 0; |
845 | | |
846 | 12 | if (!ossl_prov_macctx_load(&hmac->ctx, p->mac, NULL, p->digest, |
847 | 12 | p->propq, p->engine, |
848 | 12 | NULL, NULL, NULL, libctx)) |
849 | 12 | return 0; |
850 | 64 | } else { |
851 | 64 | (void)ERR_clear_last_mark(); |
852 | 64 | if (prov_md) |
853 | 64 | ossl_prov_digest_set_md(&hmac->digest, prov_md); |
854 | 64 | if (!ossl_prov_macctx_load(&hmac->ctx, p->mac, NULL, p->digest, |
855 | 64 | p->propq, p->engine, |
856 | 64 | NULL, NULL, NULL, libctx)) |
857 | 0 | return 0; |
858 | 64 | } |
859 | | |
860 | 64 | md = ossl_prov_digest_md(&hmac->digest); |
861 | 64 | if (md != NULL && !ossl_drbg_verify_digest(ctx, libctx, md)) |
862 | 2 | return 0; /* Error already raised for us */ |
863 | | |
864 | 62 | if (md != NULL && hmac->ctx != NULL) { |
865 | | /* These are taken from SP 800-90 10.1 Table 2 */ |
866 | 62 | md_size = EVP_MD_get_size(md); |
867 | 62 | if (md_size <= 0) |
868 | 1 | return 0; |
869 | 61 | hmac->blocklen = (size_t)md_size; |
870 | | /* See SP800-57 Part1 Rev4 5.6.1 Table 3 */ |
871 | 61 | ctx->strength = 64 * (int)(hmac->blocklen >> 3); |
872 | 61 | if (ctx->strength > 256) |
873 | 8 | ctx->strength = 256; |
874 | 61 | ctx->seedlen = hmac->blocklen; |
875 | 61 | ctx->min_entropylen = ctx->strength / 8; |
876 | 61 | ctx->min_noncelen = ctx->min_entropylen / 2; |
877 | 61 | } |
878 | | |
879 | 61 | return ossl_drbg_set_ctx_params(ctx, p); |
880 | 62 | } |
881 | | |
882 | | #define drbg_hmac_set_ctx_params_st drbg_set_ctx_params_st |
883 | | |
884 | | /* clang-format off */ |
885 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
886 | | #ifndef drbg_hmac_set_ctx_params_list |
887 | | static const OSSL_PARAM drbg_hmac_set_ctx_params_list[] = { |
888 | | OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_PROPERTIES, NULL, 0), |
889 | | OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_DIGEST, NULL, 0), |
890 | | OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_MAC, NULL, 0), |
891 | | OSSL_PARAM_utf8_string(OSSL_PROV_PARAM_CORE_PROV_NAME, NULL, 0), |
892 | | OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS, NULL), |
893 | | OSSL_PARAM_uint64(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL, NULL), |
894 | | # if defined(FIPS_MODULE) |
895 | | OSSL_PARAM_int(OSSL_KDF_PARAM_FIPS_DIGEST_CHECK, NULL), |
896 | | # endif |
897 | | OSSL_PARAM_END |
898 | | }; |
899 | | #endif |
900 | | |
901 | | #ifndef drbg_hmac_set_ctx_params_st |
902 | | struct drbg_hmac_set_ctx_params_st { |
903 | | OSSL_PARAM *digest; |
904 | | OSSL_PARAM *engine; |
905 | | # if defined(FIPS_MODULE) |
906 | | OSSL_PARAM *ind_d; |
907 | | # endif |
908 | | OSSL_PARAM *mac; |
909 | | OSSL_PARAM *propq; |
910 | | OSSL_PARAM *prov; |
911 | | OSSL_PARAM *reseed_req; |
912 | | OSSL_PARAM *reseed_time; |
913 | | }; |
914 | | #endif |
915 | | |
916 | | #ifndef drbg_hmac_set_ctx_params_decoder |
917 | | static int drbg_hmac_set_ctx_params_decoder |
918 | | (const OSSL_PARAM *p, struct drbg_hmac_set_ctx_params_st *r) |
919 | 95 | { |
920 | 95 | const char *s; |
921 | | |
922 | 95 | memset(r, 0, sizeof(*r)); |
923 | 95 | if (p != NULL) |
924 | 665 | for (; (s = p->key) != NULL; p++) |
925 | 570 | switch(s[0]) { |
926 | 0 | default: |
927 | 0 | break; |
928 | 95 | case 'd': |
929 | 95 | switch(s[1]) { |
930 | 0 | default: |
931 | 0 | break; |
932 | 95 | case 'i': |
933 | 95 | switch(s[2]) { |
934 | 0 | default: |
935 | 0 | break; |
936 | 95 | case 'g': |
937 | 95 | switch(s[3]) { |
938 | 0 | default: |
939 | 0 | break; |
940 | 95 | case 'e': |
941 | 95 | switch(s[4]) { |
942 | 0 | default: |
943 | 0 | break; |
944 | 95 | case 's': |
945 | 95 | switch(s[5]) { |
946 | 0 | default: |
947 | 0 | break; |
948 | 95 | case 't': |
949 | 95 | switch(s[6]) { |
950 | 0 | default: |
951 | 0 | break; |
952 | 0 | case '-': |
953 | | # if defined(FIPS_MODULE) |
954 | | if (ossl_likely(strcmp("check", s + 7) == 0)) { |
955 | | /* OSSL_KDF_PARAM_FIPS_DIGEST_CHECK */ |
956 | | if (ossl_unlikely(r->ind_d != NULL)) { |
957 | | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
958 | | "param %s is repeated", s); |
959 | | return 0; |
960 | | } |
961 | | r->ind_d = (OSSL_PARAM *)p; |
962 | | } |
963 | | # endif |
964 | 0 | break; |
965 | 95 | case '\0': |
966 | 95 | if (ossl_unlikely(r->digest != NULL)) { |
967 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
968 | 0 | "param %s is repeated", s); |
969 | 0 | return 0; |
970 | 0 | } |
971 | 95 | r->digest = (OSSL_PARAM *)p; |
972 | 95 | } |
973 | 95 | } |
974 | 95 | } |
975 | 95 | } |
976 | 95 | } |
977 | 95 | } |
978 | 95 | break; |
979 | 95 | case 'e': |
980 | 0 | if (ossl_likely(strcmp("ngine", s + 1) == 0)) { |
981 | | /* OSSL_ALG_PARAM_ENGINE */ |
982 | 0 | if (ossl_unlikely(r->engine != NULL)) { |
983 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
984 | 0 | "param %s is repeated", s); |
985 | 0 | return 0; |
986 | 0 | } |
987 | 0 | r->engine = (OSSL_PARAM *)p; |
988 | 0 | } |
989 | 0 | break; |
990 | 95 | case 'm': |
991 | 95 | if (ossl_likely(strcmp("ac", s + 1) == 0)) { |
992 | | /* OSSL_DRBG_PARAM_MAC */ |
993 | 95 | if (ossl_unlikely(r->mac != NULL)) { |
994 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
995 | 0 | "param %s is repeated", s); |
996 | 0 | return 0; |
997 | 0 | } |
998 | 95 | r->mac = (OSSL_PARAM *)p; |
999 | 95 | } |
1000 | 95 | break; |
1001 | 190 | case 'p': |
1002 | 190 | switch(s[1]) { |
1003 | 0 | default: |
1004 | 0 | break; |
1005 | 190 | case 'r': |
1006 | 190 | switch(s[2]) { |
1007 | 0 | default: |
1008 | 0 | break; |
1009 | 190 | case 'o': |
1010 | 190 | switch(s[3]) { |
1011 | 0 | default: |
1012 | 0 | break; |
1013 | 95 | case 'p': |
1014 | 95 | if (ossl_likely(strcmp("erties", s + 4) == 0)) { |
1015 | | /* OSSL_DRBG_PARAM_PROPERTIES */ |
1016 | 95 | if (ossl_unlikely(r->propq != NULL)) { |
1017 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
1018 | 0 | "param %s is repeated", s); |
1019 | 0 | return 0; |
1020 | 0 | } |
1021 | 95 | r->propq = (OSSL_PARAM *)p; |
1022 | 95 | } |
1023 | 95 | break; |
1024 | 95 | case 'v': |
1025 | 95 | if (ossl_likely(strcmp("ider-name", s + 4) == 0)) { |
1026 | | /* OSSL_PROV_PARAM_CORE_PROV_NAME */ |
1027 | 95 | if (ossl_unlikely(r->prov != NULL)) { |
1028 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
1029 | 0 | "param %s is repeated", s); |
1030 | 0 | return 0; |
1031 | 0 | } |
1032 | 95 | r->prov = (OSSL_PARAM *)p; |
1033 | 95 | } |
1034 | 190 | } |
1035 | 190 | } |
1036 | 190 | } |
1037 | 190 | break; |
1038 | 190 | case 'r': |
1039 | 190 | switch(s[1]) { |
1040 | 0 | default: |
1041 | 0 | break; |
1042 | 190 | case 'e': |
1043 | 190 | switch(s[2]) { |
1044 | 0 | default: |
1045 | 0 | break; |
1046 | 190 | case 's': |
1047 | 190 | switch(s[3]) { |
1048 | 0 | default: |
1049 | 0 | break; |
1050 | 190 | case 'e': |
1051 | 190 | switch(s[4]) { |
1052 | 0 | default: |
1053 | 0 | break; |
1054 | 190 | case 'e': |
1055 | 190 | switch(s[5]) { |
1056 | 0 | default: |
1057 | 0 | break; |
1058 | 190 | case 'd': |
1059 | 190 | switch(s[6]) { |
1060 | 0 | default: |
1061 | 0 | break; |
1062 | 190 | case '_': |
1063 | 190 | switch(s[7]) { |
1064 | 0 | default: |
1065 | 0 | break; |
1066 | 95 | case 'r': |
1067 | 95 | if (ossl_likely(strcmp("equests", s + 8) == 0)) { |
1068 | | /* OSSL_DRBG_PARAM_RESEED_REQUESTS */ |
1069 | 95 | if (ossl_unlikely(r->reseed_req != NULL)) { |
1070 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
1071 | 0 | "param %s is repeated", s); |
1072 | 0 | return 0; |
1073 | 0 | } |
1074 | 95 | r->reseed_req = (OSSL_PARAM *)p; |
1075 | 95 | } |
1076 | 95 | break; |
1077 | 95 | case 't': |
1078 | 95 | if (ossl_likely(strcmp("ime_interval", s + 8) == 0)) { |
1079 | | /* OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL */ |
1080 | 95 | if (ossl_unlikely(r->reseed_time != NULL)) { |
1081 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
1082 | 0 | "param %s is repeated", s); |
1083 | 0 | return 0; |
1084 | 0 | } |
1085 | 95 | r->reseed_time = (OSSL_PARAM *)p; |
1086 | 95 | } |
1087 | 190 | } |
1088 | 190 | } |
1089 | 190 | } |
1090 | 190 | } |
1091 | 190 | } |
1092 | 190 | } |
1093 | 190 | } |
1094 | 570 | } |
1095 | 95 | return 1; |
1096 | 95 | } |
1097 | | #endif |
1098 | | /* End of machine generated */ |
1099 | | /* clang-format on */ |
1100 | | |
1101 | | static int drbg_hmac_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
1102 | 395 | { |
1103 | 395 | PROV_DRBG *drbg = (PROV_DRBG *)vctx; |
1104 | 395 | struct drbg_set_ctx_params_st p; |
1105 | 395 | int ret; |
1106 | | |
1107 | 395 | if (drbg == NULL || !drbg_hmac_set_ctx_params_decoder(params, &p)) |
1108 | 0 | return 0; |
1109 | | |
1110 | 395 | if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock)) |
1111 | 0 | return 0; |
1112 | | |
1113 | 395 | ret = drbg_hmac_set_ctx_params_locked(drbg, &p); |
1114 | | |
1115 | 395 | if (drbg->lock != NULL) |
1116 | 0 | CRYPTO_THREAD_unlock(drbg->lock); |
1117 | | |
1118 | 395 | return ret; |
1119 | 395 | } |
1120 | | |
1121 | | static const OSSL_PARAM *drbg_hmac_settable_ctx_params(ossl_unused void *vctx, |
1122 | | ossl_unused void *p_ctx) |
1123 | 567 | { |
1124 | 567 | return drbg_hmac_set_ctx_params_list; |
1125 | 567 | } |
1126 | | |
1127 | | const OSSL_DISPATCH ossl_drbg_ossl_hmac_functions[] = { |
1128 | | { OSSL_FUNC_RAND_NEWCTX, (void (*)(void))drbg_hmac_new_wrapper }, |
1129 | | { OSSL_FUNC_RAND_FREECTX, (void (*)(void))drbg_hmac_free }, |
1130 | | { OSSL_FUNC_RAND_INSTANTIATE, |
1131 | | (void (*)(void))drbg_hmac_instantiate_wrapper }, |
1132 | | { OSSL_FUNC_RAND_UNINSTANTIATE, |
1133 | | (void (*)(void))drbg_hmac_uninstantiate_wrapper }, |
1134 | | { OSSL_FUNC_RAND_GENERATE, (void (*)(void))drbg_hmac_generate_wrapper }, |
1135 | | { OSSL_FUNC_RAND_RESEED, (void (*)(void))drbg_hmac_reseed_wrapper }, |
1136 | | { OSSL_FUNC_RAND_ENABLE_LOCKING, (void (*)(void))ossl_drbg_enable_locking }, |
1137 | | { OSSL_FUNC_RAND_LOCK, (void (*)(void))ossl_drbg_lock }, |
1138 | | { OSSL_FUNC_RAND_UNLOCK, (void (*)(void))ossl_drbg_unlock }, |
1139 | | { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS, |
1140 | | (void (*)(void))drbg_hmac_settable_ctx_params }, |
1141 | | { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void (*)(void))drbg_hmac_set_ctx_params }, |
1142 | | { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS, |
1143 | | (void (*)(void))drbg_hmac_gettable_ctx_params }, |
1144 | | { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void (*)(void))drbg_hmac_get_ctx_params }, |
1145 | | { OSSL_FUNC_RAND_VERIFY_ZEROIZATION, |
1146 | | (void (*)(void))drbg_hmac_verify_zeroization }, |
1147 | | { OSSL_FUNC_RAND_GET_SEED, (void (*)(void))ossl_drbg_get_seed }, |
1148 | | { OSSL_FUNC_RAND_CLEAR_SEED, (void (*)(void))ossl_drbg_clear_seed }, |
1149 | | OSSL_DISPATCH_END |
1150 | | }; |