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