/src/openssl/providers/implementations/rands/drbg.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2011-2022 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 | | #include <string.h> |
11 | | #include <openssl/crypto.h> |
12 | | #include <openssl/err.h> |
13 | | #include <openssl/rand.h> |
14 | | #include <openssl/evp.h> |
15 | | #include "crypto/rand.h" |
16 | | #include <openssl/proverr.h> |
17 | | #include "drbg_local.h" |
18 | | #include "internal/thread_once.h" |
19 | | #include "crypto/cryptlib.h" |
20 | | #include "prov/seeding.h" |
21 | | #include "crypto/rand_pool.h" |
22 | | #include "prov/provider_ctx.h" |
23 | | #include "prov/providercommon.h" |
24 | | #include "prov/fipscommon.h" |
25 | | #include "crypto/context.h" |
26 | | |
27 | | /* |
28 | | * Support framework for NIST SP 800-90A DRBG |
29 | | * |
30 | | * See manual page PROV_DRBG(7) for a general overview. |
31 | | * |
32 | | * The OpenSSL model is to have new and free functions, and that new |
33 | | * does all initialization. That is not the NIST model, which has |
34 | | * instantiation and un-instantiate, and re-use within a new/free |
35 | | * lifecycle. (No doubt this comes from the desire to support hardware |
36 | | * DRBG, where allocation of resources on something like an HSM is |
37 | | * a much bigger deal than just re-setting an allocated resource.) |
38 | | */ |
39 | | |
40 | | /* NIST SP 800-90A DRBG recommends the use of a personalization string. */ |
41 | | static const char ossl_pers_string[] = DRBG_DEFAULT_PERS_STRING; |
42 | | |
43 | | static const OSSL_DISPATCH *find_call(const OSSL_DISPATCH *dispatch, |
44 | | int function); |
45 | | |
46 | | static int rand_drbg_restart(PROV_DRBG *drbg); |
47 | | |
48 | | int ossl_drbg_lock(void *vctx) |
49 | 16 | { |
50 | 16 | PROV_DRBG *drbg = vctx; |
51 | | |
52 | 16 | if (drbg == NULL || drbg->lock == NULL) |
53 | 4 | return 1; |
54 | 12 | return CRYPTO_THREAD_write_lock(drbg->lock); |
55 | 16 | } |
56 | | |
57 | | void ossl_drbg_unlock(void *vctx) |
58 | 16 | { |
59 | 16 | PROV_DRBG *drbg = vctx; |
60 | | |
61 | 16 | if (drbg != NULL && drbg->lock != NULL) |
62 | 12 | CRYPTO_THREAD_unlock(drbg->lock); |
63 | 16 | } |
64 | | |
65 | | static int ossl_drbg_lock_parent(PROV_DRBG *drbg) |
66 | 20 | { |
67 | 20 | void *parent = drbg->parent; |
68 | | |
69 | 20 | if (parent != NULL |
70 | 20 | && drbg->parent_lock != NULL |
71 | 20 | && !drbg->parent_lock(parent)) { |
72 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_LOCKING_NOT_ENABLED); |
73 | 0 | return 0; |
74 | 0 | } |
75 | 20 | return 1; |
76 | 20 | } |
77 | | |
78 | | static void ossl_drbg_unlock_parent(PROV_DRBG *drbg) |
79 | 20 | { |
80 | 20 | void *parent = drbg->parent; |
81 | | |
82 | 20 | if (parent != NULL && drbg->parent_unlock != NULL) |
83 | 20 | drbg->parent_unlock(parent); |
84 | 20 | } |
85 | | |
86 | | static int get_parent_strength(PROV_DRBG *drbg, unsigned int *str) |
87 | 6 | { |
88 | 6 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
89 | 6 | void *parent = drbg->parent; |
90 | 6 | int res; |
91 | | |
92 | 6 | if (drbg->parent_get_ctx_params == NULL) { |
93 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PARENT_STRENGTH); |
94 | 0 | return 0; |
95 | 0 | } |
96 | | |
97 | 6 | *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, str); |
98 | 6 | if (!ossl_drbg_lock_parent(drbg)) { |
99 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_LOCK_PARENT); |
100 | 0 | return 0; |
101 | 0 | } |
102 | 6 | res = drbg->parent_get_ctx_params(parent, params); |
103 | 6 | ossl_drbg_unlock_parent(drbg); |
104 | 6 | if (!res) { |
105 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PARENT_STRENGTH); |
106 | 0 | return 0; |
107 | 0 | } |
108 | 6 | return 1; |
109 | 6 | } |
110 | | |
111 | | static unsigned int get_parent_reseed_count(PROV_DRBG *drbg) |
112 | 6 | { |
113 | 6 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
114 | 6 | void *parent = drbg->parent; |
115 | 6 | unsigned int r = 0; |
116 | | |
117 | 6 | *params = OSSL_PARAM_construct_uint(OSSL_DRBG_PARAM_RESEED_COUNTER, &r); |
118 | 6 | if (!ossl_drbg_lock_parent(drbg)) { |
119 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_LOCK_PARENT); |
120 | 0 | goto err; |
121 | 0 | } |
122 | 6 | if (!drbg->parent_get_ctx_params(parent, params)) |
123 | 0 | r = 0; |
124 | 6 | ossl_drbg_unlock_parent(drbg); |
125 | 6 | return r; |
126 | | |
127 | 0 | err: |
128 | 0 | r = tsan_load(&drbg->reseed_counter) - 2; |
129 | 0 | if (r == 0) |
130 | 0 | r = UINT_MAX; |
131 | 0 | return r; |
132 | 6 | } |
133 | | |
134 | | /* |
135 | | * Implements the get_entropy() callback |
136 | | * |
137 | | * If the DRBG has a parent, then the required amount of entropy input |
138 | | * is fetched using the parent's ossl_prov_drbg_generate(). |
139 | | * |
140 | | * Otherwise, the entropy is polled from the system entropy sources |
141 | | * using ossl_pool_acquire_entropy(). |
142 | | * |
143 | | * If a random pool has been added to the DRBG using RAND_add(), then |
144 | | * its entropy will be used up first. |
145 | | */ |
146 | | size_t ossl_drbg_get_seed(void *vdrbg, unsigned char **pout, |
147 | | int entropy, size_t min_len, |
148 | | size_t max_len, int prediction_resistance, |
149 | | const unsigned char *adin, size_t adin_len) |
150 | 2 | { |
151 | 2 | PROV_DRBG *drbg = (PROV_DRBG *)vdrbg; |
152 | 2 | size_t bytes_needed; |
153 | 2 | unsigned char *buffer; |
154 | | |
155 | | /* Figure out how many bytes we need */ |
156 | 2 | bytes_needed = entropy >= 0 ? (entropy + 7) / 8 : 0; |
157 | 2 | if (bytes_needed < min_len) |
158 | 2 | bytes_needed = min_len; |
159 | 2 | if (bytes_needed > max_len) |
160 | 0 | bytes_needed = max_len; |
161 | | |
162 | | /* Allocate storage */ |
163 | 2 | buffer = OPENSSL_secure_malloc(bytes_needed); |
164 | 2 | if (buffer == NULL) |
165 | 0 | return 0; |
166 | | |
167 | | /* |
168 | | * Get random data. Include our DRBG address as |
169 | | * additional input, in order to provide a distinction between |
170 | | * different DRBG child instances. |
171 | | * |
172 | | * Note: using the sizeof() operator on a pointer triggers |
173 | | * a warning in some static code analyzers, but it's |
174 | | * intentional and correct here. |
175 | | */ |
176 | 2 | if (!ossl_prov_drbg_generate(drbg, buffer, bytes_needed, |
177 | 2 | drbg->strength, prediction_resistance, |
178 | 2 | (unsigned char *)&drbg, sizeof(drbg))) { |
179 | 0 | OPENSSL_secure_clear_free(buffer, bytes_needed); |
180 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_GENERATE_ERROR); |
181 | 0 | return 0; |
182 | 0 | } |
183 | 2 | *pout = buffer; |
184 | 2 | return bytes_needed; |
185 | 2 | } |
186 | | |
187 | | /* Implements the cleanup_entropy() callback */ |
188 | | void ossl_drbg_clear_seed(ossl_unused void *vdrbg, |
189 | | unsigned char *out, size_t outlen) |
190 | 2 | { |
191 | 2 | OPENSSL_secure_clear_free(out, outlen); |
192 | 2 | } |
193 | | |
194 | | static size_t get_entropy(PROV_DRBG *drbg, unsigned char **pout, int entropy, |
195 | | size_t min_len, size_t max_len, |
196 | | int prediction_resistance) |
197 | 4 | { |
198 | 4 | size_t bytes; |
199 | 4 | unsigned int p_str; |
200 | | |
201 | 4 | if (drbg->parent == NULL) |
202 | | #ifdef FIPS_MODULE |
203 | | return ossl_crngt_get_entropy(drbg, pout, entropy, min_len, max_len, |
204 | | prediction_resistance); |
205 | | #else |
206 | 0 | return ossl_prov_get_entropy(drbg->provctx, pout, entropy, min_len, |
207 | 0 | max_len); |
208 | 4 | #endif |
209 | | |
210 | 4 | if (drbg->parent_get_seed == NULL) { |
211 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_CANNOT_SUPPLY_ENTROPY_SEED); |
212 | 0 | return 0; |
213 | 0 | } |
214 | 4 | if (!get_parent_strength(drbg, &p_str)) |
215 | 0 | return 0; |
216 | 4 | if (drbg->strength > p_str) { |
217 | | /* |
218 | | * We currently don't support the algorithm from NIST SP 800-90C |
219 | | * 10.1.2 to use a weaker DRBG as source |
220 | | */ |
221 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_STRENGTH_TOO_WEAK); |
222 | 0 | return 0; |
223 | 0 | } |
224 | | |
225 | | /* |
226 | | * Our lock is already held, but we need to lock our parent before |
227 | | * generating bits from it. Note: taking the lock will be a no-op |
228 | | * if locking is not required (while drbg->parent->lock == NULL). |
229 | | */ |
230 | 4 | if (!ossl_drbg_lock_parent(drbg)) |
231 | 0 | return 0; |
232 | | /* |
233 | | * Get random data from parent. Include our DRBG address as |
234 | | * additional input, in order to provide a distinction between |
235 | | * different DRBG child instances. |
236 | | * |
237 | | * Note: using the sizeof() operator on a pointer triggers |
238 | | * a warning in some static code analyzers, but it's |
239 | | * intentional and correct here. |
240 | | */ |
241 | 4 | bytes = drbg->parent_get_seed(drbg->parent, pout, drbg->strength, |
242 | 4 | min_len, max_len, prediction_resistance, |
243 | 4 | (unsigned char *)&drbg, sizeof(drbg)); |
244 | 4 | ossl_drbg_unlock_parent(drbg); |
245 | 4 | return bytes; |
246 | 4 | } |
247 | | |
248 | | static void cleanup_entropy(PROV_DRBG *drbg, unsigned char *out, size_t outlen) |
249 | 4 | { |
250 | 4 | if (drbg->parent == NULL) { |
251 | | #ifdef FIPS_MODULE |
252 | | ossl_crngt_cleanup_entropy(drbg, out, outlen); |
253 | | #else |
254 | 0 | ossl_prov_cleanup_entropy(drbg->provctx, out, outlen); |
255 | 0 | #endif |
256 | 4 | } else if (drbg->parent_clear_seed != NULL) { |
257 | 4 | if (!ossl_drbg_lock_parent(drbg)) |
258 | 0 | return; |
259 | 4 | drbg->parent_clear_seed(drbg->parent, out, outlen); |
260 | 4 | ossl_drbg_unlock_parent(drbg); |
261 | 4 | } |
262 | 4 | } |
263 | | |
264 | | #ifndef PROV_RAND_GET_RANDOM_NONCE |
265 | | typedef struct prov_drbg_nonce_global_st { |
266 | | CRYPTO_RWLOCK *rand_nonce_lock; |
267 | | int rand_nonce_count; |
268 | | } PROV_DRBG_NONCE_GLOBAL; |
269 | | |
270 | | /* |
271 | | * drbg_ossl_ctx_new() calls drgb_setup() which calls rand_drbg_get_nonce() |
272 | | * which needs to get the rand_nonce_lock out of the OSSL_LIB_CTX...but since |
273 | | * drbg_ossl_ctx_new() hasn't finished running yet we need the rand_nonce_lock |
274 | | * to be in a different global data object. Otherwise we will go into an |
275 | | * infinite recursion loop. |
276 | | */ |
277 | | void *ossl_prov_drbg_nonce_ctx_new(OSSL_LIB_CTX *libctx) |
278 | 1 | { |
279 | 1 | PROV_DRBG_NONCE_GLOBAL *dngbl = OPENSSL_zalloc(sizeof(*dngbl)); |
280 | | |
281 | 1 | if (dngbl == NULL) |
282 | 0 | return NULL; |
283 | | |
284 | 1 | dngbl->rand_nonce_lock = CRYPTO_THREAD_lock_new(); |
285 | 1 | if (dngbl->rand_nonce_lock == NULL) { |
286 | 0 | OPENSSL_free(dngbl); |
287 | 0 | return NULL; |
288 | 0 | } |
289 | | |
290 | 1 | return dngbl; |
291 | 1 | } |
292 | | |
293 | | void ossl_prov_drbg_nonce_ctx_free(void *vdngbl) |
294 | 1 | { |
295 | 1 | PROV_DRBG_NONCE_GLOBAL *dngbl = vdngbl; |
296 | | |
297 | 1 | if (dngbl == NULL) |
298 | 0 | return; |
299 | | |
300 | 1 | CRYPTO_THREAD_lock_free(dngbl->rand_nonce_lock); |
301 | | |
302 | 1 | OPENSSL_free(dngbl); |
303 | 1 | } |
304 | | |
305 | | /* Get a nonce from the operating system */ |
306 | | static size_t prov_drbg_get_nonce(PROV_DRBG *drbg, unsigned char **pout, |
307 | | size_t min_len, size_t max_len) |
308 | 0 | { |
309 | 0 | size_t ret = 0, n; |
310 | 0 | unsigned char *buf = NULL; |
311 | 0 | OSSL_LIB_CTX *libctx = ossl_prov_ctx_get0_libctx(drbg->provctx); |
312 | 0 | PROV_DRBG_NONCE_GLOBAL *dngbl |
313 | 0 | = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DRBG_NONCE_INDEX); |
314 | 0 | struct { |
315 | 0 | void *drbg; |
316 | 0 | int count; |
317 | 0 | } data; |
318 | |
|
319 | 0 | if (dngbl == NULL) |
320 | 0 | return 0; |
321 | | |
322 | 0 | if (drbg->parent != NULL && drbg->parent_nonce != NULL) { |
323 | 0 | n = drbg->parent_nonce(drbg->parent, NULL, 0, drbg->min_noncelen, |
324 | 0 | drbg->max_noncelen); |
325 | 0 | if (n > 0 && (buf = OPENSSL_malloc(n)) != NULL) { |
326 | 0 | ret = drbg->parent_nonce(drbg->parent, buf, 0, |
327 | 0 | drbg->min_noncelen, drbg->max_noncelen); |
328 | 0 | if (ret == n) { |
329 | 0 | *pout = buf; |
330 | 0 | return ret; |
331 | 0 | } |
332 | 0 | OPENSSL_free(buf); |
333 | 0 | } |
334 | 0 | } |
335 | | |
336 | | /* Use the built in nonce source plus some of our specifics */ |
337 | 0 | memset(&data, 0, sizeof(data)); |
338 | 0 | data.drbg = drbg; |
339 | 0 | CRYPTO_atomic_add(&dngbl->rand_nonce_count, 1, &data.count, |
340 | 0 | dngbl->rand_nonce_lock); |
341 | 0 | return ossl_prov_get_nonce(drbg->provctx, pout, min_len, max_len, |
342 | 0 | &data, sizeof(data)); |
343 | 0 | } |
344 | | #endif /* PROV_RAND_GET_RANDOM_NONCE */ |
345 | | |
346 | | /* |
347 | | * Instantiate |drbg|, after it has been initialized. Use |pers| and |
348 | | * |perslen| as prediction-resistance input. |
349 | | * |
350 | | * Requires that drbg->lock is already locked for write, if non-null. |
351 | | * |
352 | | * Returns 1 on success, 0 on failure. |
353 | | */ |
354 | | int ossl_prov_drbg_instantiate(PROV_DRBG *drbg, unsigned int strength, |
355 | | int prediction_resistance, |
356 | | const unsigned char *pers, size_t perslen) |
357 | 2 | { |
358 | 2 | unsigned char *nonce = NULL, *entropy = NULL; |
359 | 2 | size_t noncelen = 0, entropylen = 0; |
360 | 2 | size_t min_entropy, min_entropylen, max_entropylen; |
361 | | |
362 | 2 | if (strength > drbg->strength) { |
363 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INSUFFICIENT_DRBG_STRENGTH); |
364 | 0 | goto end; |
365 | 0 | } |
366 | 2 | min_entropy = drbg->strength; |
367 | 2 | min_entropylen = drbg->min_entropylen; |
368 | 2 | max_entropylen = drbg->max_entropylen; |
369 | | |
370 | 2 | if (pers == NULL) { |
371 | 2 | pers = (const unsigned char *)ossl_pers_string; |
372 | 2 | perslen = sizeof(ossl_pers_string); |
373 | 2 | } |
374 | 2 | if (perslen > drbg->max_perslen) { |
375 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_PERSONALISATION_STRING_TOO_LONG); |
376 | 0 | goto end; |
377 | 0 | } |
378 | | |
379 | 2 | if (drbg->state != EVP_RAND_STATE_UNINITIALISED) { |
380 | 0 | if (drbg->state == EVP_RAND_STATE_ERROR) |
381 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_IN_ERROR_STATE); |
382 | 0 | else |
383 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_ALREADY_INSTANTIATED); |
384 | 0 | goto end; |
385 | 0 | } |
386 | | |
387 | 2 | drbg->state = EVP_RAND_STATE_ERROR; |
388 | | |
389 | 2 | if (drbg->min_noncelen > 0) { |
390 | 1 | if (drbg->parent_nonce != NULL) { |
391 | 0 | noncelen = drbg->parent_nonce(drbg->parent, NULL, drbg->strength, |
392 | 0 | drbg->min_noncelen, |
393 | 0 | drbg->max_noncelen); |
394 | 0 | if (noncelen == 0) { |
395 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE); |
396 | 0 | goto end; |
397 | 0 | } |
398 | 0 | nonce = OPENSSL_malloc(noncelen); |
399 | 0 | if (nonce == NULL) { |
400 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE); |
401 | 0 | goto end; |
402 | 0 | } |
403 | 0 | if (noncelen != drbg->parent_nonce(drbg->parent, nonce, |
404 | 0 | drbg->strength, |
405 | 0 | drbg->min_noncelen, |
406 | 0 | drbg->max_noncelen)) { |
407 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE); |
408 | 0 | goto end; |
409 | 0 | } |
410 | 0 | #ifndef PROV_RAND_GET_RANDOM_NONCE |
411 | 1 | } else if (drbg->parent != NULL) { |
412 | 1 | #endif |
413 | | /* |
414 | | * NIST SP800-90Ar1 section 9.1 says you can combine getting |
415 | | * the entropy and nonce in 1 call by increasing the entropy |
416 | | * with 50% and increasing the minimum length to accommodate |
417 | | * the length of the nonce. We do this in case a nonce is |
418 | | * required and there is no parental nonce capability. |
419 | | */ |
420 | 1 | min_entropy += drbg->strength / 2; |
421 | 1 | min_entropylen += drbg->min_noncelen; |
422 | 1 | max_entropylen += drbg->max_noncelen; |
423 | 1 | } |
424 | 0 | #ifndef PROV_RAND_GET_RANDOM_NONCE |
425 | 0 | else { /* parent == NULL */ |
426 | 0 | noncelen = prov_drbg_get_nonce(drbg, &nonce, drbg->min_noncelen, |
427 | 0 | drbg->max_noncelen); |
428 | 0 | if (noncelen < drbg->min_noncelen |
429 | 0 | || noncelen > drbg->max_noncelen) { |
430 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE); |
431 | 0 | goto end; |
432 | 0 | } |
433 | 0 | } |
434 | 1 | #endif |
435 | 1 | } |
436 | | |
437 | 2 | drbg->reseed_next_counter = tsan_load(&drbg->reseed_counter); |
438 | 2 | if (drbg->reseed_next_counter) { |
439 | 2 | drbg->reseed_next_counter++; |
440 | 2 | if (!drbg->reseed_next_counter) |
441 | 0 | drbg->reseed_next_counter = 1; |
442 | 2 | } |
443 | | |
444 | 2 | entropylen = get_entropy(drbg, &entropy, min_entropy, |
445 | 2 | min_entropylen, max_entropylen, |
446 | 2 | prediction_resistance); |
447 | 2 | if (entropylen < min_entropylen |
448 | 2 | || entropylen > max_entropylen) { |
449 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_ENTROPY); |
450 | 0 | goto end; |
451 | 0 | } |
452 | | |
453 | 2 | if (!drbg->instantiate(drbg, entropy, entropylen, nonce, noncelen, |
454 | 2 | pers, perslen)) { |
455 | 0 | cleanup_entropy(drbg, entropy, entropylen); |
456 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_INSTANTIATING_DRBG); |
457 | 0 | goto end; |
458 | 0 | } |
459 | 2 | cleanup_entropy(drbg, entropy, entropylen); |
460 | | |
461 | 2 | drbg->state = EVP_RAND_STATE_READY; |
462 | 2 | drbg->generate_counter = 1; |
463 | 2 | drbg->reseed_time = time(NULL); |
464 | 2 | tsan_store(&drbg->reseed_counter, drbg->reseed_next_counter); |
465 | | |
466 | 2 | end: |
467 | 2 | if (nonce != NULL) |
468 | 0 | ossl_prov_cleanup_nonce(drbg->provctx, nonce, noncelen); |
469 | 2 | if (drbg->state == EVP_RAND_STATE_READY) |
470 | 2 | return 1; |
471 | 0 | return 0; |
472 | 2 | } |
473 | | |
474 | | /* |
475 | | * Uninstantiate |drbg|. Must be instantiated before it can be used. |
476 | | * |
477 | | * Requires that drbg->lock is already locked for write, if non-null. |
478 | | * |
479 | | * Returns 1 on success, 0 on failure. |
480 | | */ |
481 | | int ossl_prov_drbg_uninstantiate(PROV_DRBG *drbg) |
482 | 0 | { |
483 | 0 | drbg->state = EVP_RAND_STATE_UNINITIALISED; |
484 | 0 | return 1; |
485 | 0 | } |
486 | | |
487 | | /* |
488 | | * Reseed |drbg|, mixing in the specified data |
489 | | * |
490 | | * Requires that drbg->lock is already locked for write, if non-null. |
491 | | * |
492 | | * Returns 1 on success, 0 on failure. |
493 | | */ |
494 | | int ossl_prov_drbg_reseed(PROV_DRBG *drbg, int prediction_resistance, |
495 | | const unsigned char *ent, size_t ent_len, |
496 | | const unsigned char *adin, size_t adinlen) |
497 | 2 | { |
498 | 2 | unsigned char *entropy = NULL; |
499 | 2 | size_t entropylen = 0; |
500 | | |
501 | 2 | if (!ossl_prov_is_running()) |
502 | 0 | return 0; |
503 | | |
504 | 2 | if (drbg->state != EVP_RAND_STATE_READY) { |
505 | | /* try to recover from previous errors */ |
506 | 0 | rand_drbg_restart(drbg); |
507 | |
|
508 | 0 | if (drbg->state == EVP_RAND_STATE_ERROR) { |
509 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_IN_ERROR_STATE); |
510 | 0 | return 0; |
511 | 0 | } |
512 | 0 | if (drbg->state == EVP_RAND_STATE_UNINITIALISED) { |
513 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NOT_INSTANTIATED); |
514 | 0 | return 0; |
515 | 0 | } |
516 | 0 | } |
517 | | |
518 | 2 | if (ent != NULL) { |
519 | 0 | if (ent_len < drbg->min_entropylen) { |
520 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_ENTROPY_OUT_OF_RANGE); |
521 | 0 | drbg->state = EVP_RAND_STATE_ERROR; |
522 | 0 | return 0; |
523 | 0 | } |
524 | 0 | if (ent_len > drbg->max_entropylen) { |
525 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_ENTROPY_INPUT_TOO_LONG); |
526 | 0 | drbg->state = EVP_RAND_STATE_ERROR; |
527 | 0 | return 0; |
528 | 0 | } |
529 | 0 | } |
530 | | |
531 | 2 | if (adin == NULL) { |
532 | 1 | adinlen = 0; |
533 | 1 | } else if (adinlen > drbg->max_adinlen) { |
534 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_ADDITIONAL_INPUT_TOO_LONG); |
535 | 0 | return 0; |
536 | 0 | } |
537 | | |
538 | 2 | drbg->state = EVP_RAND_STATE_ERROR; |
539 | | |
540 | 2 | drbg->reseed_next_counter = tsan_load(&drbg->reseed_counter); |
541 | 2 | if (drbg->reseed_next_counter) { |
542 | 2 | drbg->reseed_next_counter++; |
543 | 2 | if (!drbg->reseed_next_counter) |
544 | 0 | drbg->reseed_next_counter = 1; |
545 | 2 | } |
546 | | |
547 | 2 | if (ent != NULL) { |
548 | | #ifdef FIPS_MODULE |
549 | | /* |
550 | | * NIST SP-800-90A mandates that entropy *shall not* be provided |
551 | | * by the consuming application. Instead the data is added as additional |
552 | | * input. |
553 | | * |
554 | | * (NIST SP-800-90Ar1, Sections 9.1 and 9.2) |
555 | | */ |
556 | | if (!drbg->reseed(drbg, NULL, 0, ent, ent_len)) { |
557 | | ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_RESEED); |
558 | | return 0; |
559 | | } |
560 | | #else |
561 | 0 | if (!drbg->reseed(drbg, ent, ent_len, adin, adinlen)) { |
562 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_RESEED); |
563 | 0 | return 0; |
564 | 0 | } |
565 | | /* There isn't much point adding the same additional input twice */ |
566 | 0 | adin = NULL; |
567 | 0 | adinlen = 0; |
568 | 0 | #endif |
569 | 0 | } |
570 | | |
571 | | /* Reseed using our sources in addition */ |
572 | 2 | entropylen = get_entropy(drbg, &entropy, drbg->strength, |
573 | 2 | drbg->min_entropylen, drbg->max_entropylen, |
574 | 2 | prediction_resistance); |
575 | 2 | if (entropylen < drbg->min_entropylen |
576 | 2 | || entropylen > drbg->max_entropylen) { |
577 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_ENTROPY); |
578 | 0 | goto end; |
579 | 0 | } |
580 | | |
581 | 2 | if (!drbg->reseed(drbg, entropy, entropylen, adin, adinlen)) |
582 | 0 | goto end; |
583 | | |
584 | 2 | drbg->state = EVP_RAND_STATE_READY; |
585 | 2 | drbg->generate_counter = 1; |
586 | 2 | drbg->reseed_time = time(NULL); |
587 | 2 | tsan_store(&drbg->reseed_counter, drbg->reseed_next_counter); |
588 | 2 | if (drbg->parent != NULL) |
589 | 2 | drbg->parent_reseed_counter = get_parent_reseed_count(drbg); |
590 | | |
591 | 2 | end: |
592 | 2 | cleanup_entropy(drbg, entropy, entropylen); |
593 | 2 | if (drbg->state == EVP_RAND_STATE_READY) |
594 | 2 | return 1; |
595 | 0 | return 0; |
596 | 2 | } |
597 | | |
598 | | /* |
599 | | * Generate |outlen| bytes into the buffer at |out|. Reseed if we need |
600 | | * to or if |prediction_resistance| is set. Additional input can be |
601 | | * sent in |adin| and |adinlen|. |
602 | | * |
603 | | * Requires that drbg->lock is already locked for write, if non-null. |
604 | | * |
605 | | * Returns 1 on success, 0 on failure. |
606 | | * |
607 | | */ |
608 | | int ossl_prov_drbg_generate(PROV_DRBG *drbg, unsigned char *out, size_t outlen, |
609 | | unsigned int strength, int prediction_resistance, |
610 | | const unsigned char *adin, size_t adinlen) |
611 | 4 | { |
612 | 4 | int fork_id; |
613 | 4 | int reseed_required = 0; |
614 | | |
615 | 4 | if (!ossl_prov_is_running()) |
616 | 0 | return 0; |
617 | | |
618 | 4 | if (drbg->state != EVP_RAND_STATE_READY) { |
619 | | /* try to recover from previous errors */ |
620 | 0 | rand_drbg_restart(drbg); |
621 | |
|
622 | 0 | if (drbg->state == EVP_RAND_STATE_ERROR) { |
623 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_IN_ERROR_STATE); |
624 | 0 | return 0; |
625 | 0 | } |
626 | 0 | if (drbg->state == EVP_RAND_STATE_UNINITIALISED) { |
627 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NOT_INSTANTIATED); |
628 | 0 | return 0; |
629 | 0 | } |
630 | 0 | } |
631 | 4 | if (strength > drbg->strength) { |
632 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INSUFFICIENT_DRBG_STRENGTH); |
633 | 0 | return 0; |
634 | 0 | } |
635 | | |
636 | 4 | if (outlen > drbg->max_request) { |
637 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_REQUEST_TOO_LARGE_FOR_DRBG); |
638 | 0 | return 0; |
639 | 0 | } |
640 | 4 | if (adinlen > drbg->max_adinlen) { |
641 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_ADDITIONAL_INPUT_TOO_LONG); |
642 | 0 | return 0; |
643 | 0 | } |
644 | | |
645 | 4 | fork_id = openssl_get_fork_id(); |
646 | | |
647 | 4 | if (drbg->fork_id != fork_id) { |
648 | 0 | drbg->fork_id = fork_id; |
649 | 0 | reseed_required = 1; |
650 | 0 | } |
651 | | |
652 | 4 | if (drbg->reseed_interval > 0) { |
653 | 4 | if (drbg->generate_counter >= drbg->reseed_interval) |
654 | 0 | reseed_required = 1; |
655 | 4 | } |
656 | 4 | if (drbg->reseed_time_interval > 0) { |
657 | 4 | time_t now = time(NULL); |
658 | 4 | if (now < drbg->reseed_time |
659 | 4 | || now - drbg->reseed_time >= drbg->reseed_time_interval) |
660 | 0 | reseed_required = 1; |
661 | 4 | } |
662 | 4 | if (drbg->parent != NULL |
663 | 4 | && get_parent_reseed_count(drbg) != drbg->parent_reseed_counter) |
664 | 1 | reseed_required = 1; |
665 | | |
666 | 4 | if (reseed_required || prediction_resistance) { |
667 | 1 | if (!ossl_prov_drbg_reseed(drbg, prediction_resistance, NULL, 0, |
668 | 1 | adin, adinlen)) { |
669 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_RESEED_ERROR); |
670 | 0 | return 0; |
671 | 0 | } |
672 | 1 | adin = NULL; |
673 | 1 | adinlen = 0; |
674 | 1 | } |
675 | | |
676 | 4 | if (!drbg->generate(drbg, out, outlen, adin, adinlen)) { |
677 | 0 | drbg->state = EVP_RAND_STATE_ERROR; |
678 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_GENERATE_ERROR); |
679 | 0 | return 0; |
680 | 0 | } |
681 | | |
682 | 4 | drbg->generate_counter++; |
683 | | |
684 | 4 | return 1; |
685 | 4 | } |
686 | | |
687 | | /* |
688 | | * Restart |drbg|, using the specified entropy or additional input |
689 | | * |
690 | | * Tries its best to get the drbg instantiated by all means, |
691 | | * regardless of its current state. |
692 | | * |
693 | | * Optionally, a |buffer| of |len| random bytes can be passed, |
694 | | * which is assumed to contain at least |entropy| bits of entropy. |
695 | | * |
696 | | * If |entropy| > 0, the buffer content is used as entropy input. |
697 | | * |
698 | | * If |entropy| == 0, the buffer content is used as additional input |
699 | | * |
700 | | * Returns 1 on success, 0 on failure. |
701 | | * |
702 | | * This function is used internally only. |
703 | | */ |
704 | | static int rand_drbg_restart(PROV_DRBG *drbg) |
705 | 0 | { |
706 | | /* repair error state */ |
707 | 0 | if (drbg->state == EVP_RAND_STATE_ERROR) |
708 | 0 | drbg->uninstantiate(drbg); |
709 | | |
710 | | /* repair uninitialized state */ |
711 | 0 | if (drbg->state == EVP_RAND_STATE_UNINITIALISED) |
712 | | /* reinstantiate drbg */ |
713 | 0 | ossl_prov_drbg_instantiate(drbg, drbg->strength, 0, NULL, 0); |
714 | |
|
715 | 0 | return drbg->state == EVP_RAND_STATE_READY; |
716 | 0 | } |
717 | | |
718 | | /* Provider support from here down */ |
719 | | static const OSSL_DISPATCH *find_call(const OSSL_DISPATCH *dispatch, |
720 | | int function) |
721 | 14 | { |
722 | 14 | if (dispatch != NULL) |
723 | 162 | while (dispatch->function_id != 0) { |
724 | 160 | if (dispatch->function_id == function) |
725 | 12 | return dispatch; |
726 | 148 | dispatch++; |
727 | 148 | } |
728 | 2 | return NULL; |
729 | 14 | } |
730 | | |
731 | | int ossl_drbg_enable_locking(void *vctx) |
732 | 1 | { |
733 | 1 | PROV_DRBG *drbg = vctx; |
734 | | |
735 | 1 | if (drbg != NULL && drbg->lock == NULL) { |
736 | 1 | if (drbg->parent_enable_locking != NULL) |
737 | 1 | if (!drbg->parent_enable_locking(drbg->parent)) { |
738 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_LOCKING_NOT_ENABLED); |
739 | 0 | return 0; |
740 | 0 | } |
741 | 1 | drbg->lock = CRYPTO_THREAD_lock_new(); |
742 | 1 | if (drbg->lock == NULL) { |
743 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_CREATE_LOCK); |
744 | 0 | return 0; |
745 | 0 | } |
746 | 1 | } |
747 | 1 | return 1; |
748 | 1 | } |
749 | | |
750 | | /* |
751 | | * Allocate memory and initialize a new DRBG. The DRBG is allocated on |
752 | | * the secure heap if |secure| is nonzero and the secure heap is enabled. |
753 | | * The |parent|, if not NULL, will be used as random source for reseeding. |
754 | | * This also requires the parent's provider context and the parent's lock. |
755 | | * |
756 | | * Returns a pointer to the new DRBG instance on success, NULL on failure. |
757 | | */ |
758 | | PROV_DRBG *ossl_rand_drbg_new |
759 | | (void *provctx, void *parent, const OSSL_DISPATCH *p_dispatch, |
760 | | int (*dnew)(PROV_DRBG *ctx), |
761 | | int (*instantiate)(PROV_DRBG *drbg, |
762 | | const unsigned char *entropy, size_t entropylen, |
763 | | const unsigned char *nonce, size_t noncelen, |
764 | | const unsigned char *pers, size_t perslen), |
765 | | int (*uninstantiate)(PROV_DRBG *ctx), |
766 | | int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent, size_t ent_len, |
767 | | const unsigned char *adin, size_t adin_len), |
768 | | int (*generate)(PROV_DRBG *, unsigned char *out, size_t outlen, |
769 | | const unsigned char *adin, size_t adin_len)) |
770 | 2 | { |
771 | 2 | PROV_DRBG *drbg; |
772 | 2 | unsigned int p_str; |
773 | 2 | const OSSL_DISPATCH *pfunc; |
774 | | |
775 | 2 | if (!ossl_prov_is_running()) |
776 | 0 | return NULL; |
777 | | |
778 | 2 | drbg = OPENSSL_zalloc(sizeof(*drbg)); |
779 | 2 | if (drbg == NULL) |
780 | 0 | return NULL; |
781 | | |
782 | 2 | drbg->provctx = provctx; |
783 | 2 | drbg->instantiate = instantiate; |
784 | 2 | drbg->uninstantiate = uninstantiate; |
785 | 2 | drbg->reseed = reseed; |
786 | 2 | drbg->generate = generate; |
787 | 2 | drbg->fork_id = openssl_get_fork_id(); |
788 | | |
789 | | /* Extract parent's functions */ |
790 | 2 | drbg->parent = parent; |
791 | 2 | if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_ENABLE_LOCKING)) != NULL) |
792 | 2 | drbg->parent_enable_locking = OSSL_FUNC_rand_enable_locking(pfunc); |
793 | 2 | if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_LOCK)) != NULL) |
794 | 2 | drbg->parent_lock = OSSL_FUNC_rand_lock(pfunc); |
795 | 2 | if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_UNLOCK)) != NULL) |
796 | 2 | drbg->parent_unlock = OSSL_FUNC_rand_unlock(pfunc); |
797 | 2 | if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_GET_CTX_PARAMS)) != NULL) |
798 | 2 | drbg->parent_get_ctx_params = OSSL_FUNC_rand_get_ctx_params(pfunc); |
799 | 2 | if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_NONCE)) != NULL) |
800 | 0 | drbg->parent_nonce = OSSL_FUNC_rand_nonce(pfunc); |
801 | 2 | if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_GET_SEED)) != NULL) |
802 | 2 | drbg->parent_get_seed = OSSL_FUNC_rand_get_seed(pfunc); |
803 | 2 | if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_CLEAR_SEED)) != NULL) |
804 | 2 | drbg->parent_clear_seed = OSSL_FUNC_rand_clear_seed(pfunc); |
805 | | |
806 | | /* Set some default maximums up */ |
807 | 2 | drbg->max_entropylen = DRBG_MAX_LENGTH; |
808 | 2 | drbg->max_noncelen = DRBG_MAX_LENGTH; |
809 | 2 | drbg->max_perslen = DRBG_MAX_LENGTH; |
810 | 2 | drbg->max_adinlen = DRBG_MAX_LENGTH; |
811 | 2 | drbg->generate_counter = 1; |
812 | 2 | drbg->reseed_counter = 1; |
813 | 2 | drbg->reseed_interval = RESEED_INTERVAL; |
814 | 2 | drbg->reseed_time_interval = TIME_INTERVAL; |
815 | | |
816 | 2 | if (!dnew(drbg)) |
817 | 0 | goto err; |
818 | | |
819 | 2 | if (parent != NULL) { |
820 | 2 | if (!get_parent_strength(drbg, &p_str)) |
821 | 0 | goto err; |
822 | 2 | if (drbg->strength > p_str) { |
823 | | /* |
824 | | * We currently don't support the algorithm from NIST SP 800-90C |
825 | | * 10.1.2 to use a weaker DRBG as source |
826 | | */ |
827 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_STRENGTH_TOO_WEAK); |
828 | 0 | goto err; |
829 | 0 | } |
830 | 2 | } |
831 | | #ifdef TSAN_REQUIRES_LOCKING |
832 | | if (!ossl_drbg_enable_locking(drbg)) |
833 | | goto err; |
834 | | #endif |
835 | 2 | return drbg; |
836 | | |
837 | 0 | err: |
838 | 0 | ossl_rand_drbg_free(drbg); |
839 | 0 | return NULL; |
840 | 2 | } |
841 | | |
842 | | void ossl_rand_drbg_free(PROV_DRBG *drbg) |
843 | 2 | { |
844 | 2 | if (drbg == NULL) |
845 | 0 | return; |
846 | | |
847 | 2 | CRYPTO_THREAD_lock_free(drbg->lock); |
848 | 2 | OPENSSL_free(drbg); |
849 | 2 | } |
850 | | |
851 | | int ossl_drbg_get_ctx_params(PROV_DRBG *drbg, OSSL_PARAM params[]) |
852 | 9 | { |
853 | 9 | OSSL_PARAM *p; |
854 | | |
855 | 9 | p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE); |
856 | 9 | if (p != NULL && !OSSL_PARAM_set_int(p, drbg->state)) |
857 | 0 | return 0; |
858 | | |
859 | 9 | p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH); |
860 | 9 | if (p != NULL && !OSSL_PARAM_set_int(p, drbg->strength)) |
861 | 0 | return 0; |
862 | | |
863 | 9 | p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST); |
864 | 9 | if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_request)) |
865 | 0 | return 0; |
866 | | |
867 | 9 | p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MIN_ENTROPYLEN); |
868 | 9 | if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->min_entropylen)) |
869 | 0 | return 0; |
870 | | |
871 | 9 | p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_ENTROPYLEN); |
872 | 9 | if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_entropylen)) |
873 | 0 | return 0; |
874 | | |
875 | 9 | p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MIN_NONCELEN); |
876 | 9 | if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->min_noncelen)) |
877 | 0 | return 0; |
878 | | |
879 | 9 | p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_NONCELEN); |
880 | 9 | if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_noncelen)) |
881 | 0 | return 0; |
882 | | |
883 | 9 | p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_PERSLEN); |
884 | 9 | if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_perslen)) |
885 | 0 | return 0; |
886 | | |
887 | 9 | p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_ADINLEN); |
888 | 9 | if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_adinlen)) |
889 | 0 | return 0; |
890 | | |
891 | 9 | p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_REQUESTS); |
892 | 9 | if (p != NULL && !OSSL_PARAM_set_uint(p, drbg->reseed_interval)) |
893 | 0 | return 0; |
894 | | |
895 | 9 | p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_TIME); |
896 | 9 | if (p != NULL && !OSSL_PARAM_set_time_t(p, drbg->reseed_time)) |
897 | 0 | return 0; |
898 | | |
899 | 9 | p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL); |
900 | 9 | if (p != NULL && !OSSL_PARAM_set_time_t(p, drbg->reseed_time_interval)) |
901 | 0 | return 0; |
902 | | |
903 | 9 | p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_COUNTER); |
904 | 9 | if (p != NULL |
905 | 9 | && !OSSL_PARAM_set_uint(p, tsan_load(&drbg->reseed_counter))) |
906 | 0 | return 0; |
907 | 9 | return 1; |
908 | 9 | } |
909 | | |
910 | | int ossl_drbg_set_ctx_params(PROV_DRBG *drbg, const OSSL_PARAM params[]) |
911 | 2 | { |
912 | 2 | const OSSL_PARAM *p; |
913 | | |
914 | 2 | if (params == NULL) |
915 | 0 | return 1; |
916 | | |
917 | 2 | p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_RESEED_REQUESTS); |
918 | 2 | if (p != NULL && !OSSL_PARAM_get_uint(p, &drbg->reseed_interval)) |
919 | 0 | return 0; |
920 | | |
921 | 2 | p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL); |
922 | 2 | if (p != NULL && !OSSL_PARAM_get_time_t(p, &drbg->reseed_time_interval)) |
923 | 0 | return 0; |
924 | 2 | return 1; |
925 | 2 | } |
926 | | |
927 | | /* Confirm digest is allowed to be used with a DRBG */ |
928 | | int ossl_drbg_verify_digest(ossl_unused OSSL_LIB_CTX *libctx, const EVP_MD *md) |
929 | 0 | { |
930 | | #ifdef FIPS_MODULE |
931 | | /* FIPS 140-3 IG D.R limited DRBG digests to a specific set */ |
932 | | static const char *const allowed_digests[] = { |
933 | | "SHA1", /* SHA 1 allowed */ |
934 | | "SHA2-256", "SHA2-512", /* non-truncated SHA2 allowed */ |
935 | | "SHA3-256", "SHA3-512", /* non-truncated SHA3 allowed */ |
936 | | }; |
937 | | size_t i; |
938 | | |
939 | | if (FIPS_restricted_drbg_digests_enabled(libctx)) { |
940 | | for (i = 0; i < OSSL_NELEM(allowed_digests); i++) |
941 | | if (EVP_MD_is_a(md, allowed_digests[i])) |
942 | | return 1; |
943 | | ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED); |
944 | | return 0; |
945 | | } |
946 | | #endif |
947 | | /* Outside of FIPS, any digests that are not XOF are allowed */ |
948 | 0 | if ((EVP_MD_get_flags(md) & EVP_MD_FLAG_XOF) != 0) { |
949 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED); |
950 | 0 | return 0; |
951 | 0 | } |
952 | 0 | return 1; |
953 | 0 | } |