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