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