| 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 |
{ |
| 55 |
return 1; |
| 56 |
} |
| 57 |
|
| 58 |
/* Interpreted as a hint only and ignored as for ossl_drbg_lock() */ |
| 59 |
void ossl_drbg_unlock(void *vctx) |
| 60 |
{ |
| 61 |
} |
| 62 |
|
| 63 |
static int ossl_drbg_lock_parent(PROV_DRBG *drbg) |
| 64 |
{ |
| 65 |
void *parent = drbg->parent; |
| 66 |
|
| 67 |
if (parent != NULL |
| 68 |
&& drbg->parent_lock != NULL |
| 69 |
&& !drbg->parent_lock(parent)) { |
| 70 |
ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_LOCKING_NOT_ENABLED); |
| 71 |
return 0; |
| 72 |
} |
| 73 |
return 1; |
| 74 |
} |
| 75 |
|
| 76 |
static void ossl_drbg_unlock_parent(PROV_DRBG *drbg) |
| 77 |
{ |
| 78 |
void *parent = drbg->parent; |
| 79 |
|
| 80 |
if (parent != NULL && drbg->parent_unlock != NULL) |
| 81 |
drbg->parent_unlock(parent); |
| 82 |
} |
| 83 |
|
| 84 |
static int get_parent_strength(PROV_DRBG *drbg, unsigned int *str) |
| 85 |
{ |
| 86 |
OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
| 87 |
void *parent = drbg->parent; |
| 88 |
int res; |
| 89 |
|
| 90 |
if (drbg->parent_get_ctx_params == NULL) { |
| 91 |
ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PARENT_STRENGTH); |
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
*params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, str); |
| 96 |
if (!ossl_drbg_lock_parent(drbg)) { |
| 97 |
ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_LOCK_PARENT); |
| 98 |
return 0; |
| 99 |
} |
| 100 |
res = drbg->parent_get_ctx_params(parent, params); |
| 101 |
ossl_drbg_unlock_parent(drbg); |
| 102 |
if (!res) { |
| 103 |
ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PARENT_STRENGTH); |
| 104 |
return 0; |
| 105 |
} |
| 106 |
return 1; |
| 107 |
} |
| 108 |
|
| 109 |
static unsigned int get_parent_reseed_count(PROV_DRBG *drbg) |
| 110 |
{ |
| 111 |
OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
| 112 |
void *parent = drbg->parent; |
| 113 |
unsigned int r = 0; |
| 114 |
|
| 115 |
*params = OSSL_PARAM_construct_uint(OSSL_DRBG_PARAM_RESEED_COUNTER, &r); |
| 116 |
if (!ossl_drbg_lock_parent(drbg)) { |
| 117 |
ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_LOCK_PARENT); |
| 118 |
goto err; |
| 119 |
} |
| 120 |
if (!drbg->parent_get_ctx_params(parent, params)) |
| 121 |
r = 0; |
| 122 |
ossl_drbg_unlock_parent(drbg); |
| 123 |
return r; |
| 124 |
|
| 125 |
err: |
| 126 |
r = tsan_load(&drbg->reseed_counter) - 2; |
| 127 |
if (r == 0) |
| 128 |
r = UINT_MAX; |
| 129 |
return r; |
| 130 |
} |
| 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 |
{ |
| 149 |
PROV_DRBG *drbg = (PROV_DRBG *)vdrbg; |
| 150 |
size_t bytes_needed; |
| 151 |
unsigned char *buffer; |
| 152 |
|
| 153 |
/* Figure out how many bytes we need */ |
| 154 |
bytes_needed = entropy >= 0 ? (entropy + 7) / 8 : 0; |
| 155 |
if (bytes_needed < min_len) |
| 156 |
bytes_needed = min_len; |
| 157 |
if (bytes_needed > max_len) |
| 158 |
bytes_needed = max_len; |
| 159 |
|
| 160 |
/* Allocate storage */ |
| 161 |
buffer = OPENSSL_secure_malloc(bytes_needed); |
| 162 |
if (buffer == NULL) |
| 163 |
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 |
if (!ossl_prov_drbg_generate(drbg, buffer, bytes_needed, |
| 175 |
drbg->strength, prediction_resistance, |
| 176 |
(unsigned char *)&drbg, sizeof(drbg))) { |
| 177 |
OPENSSL_secure_clear_free(buffer, bytes_needed); |
| 178 |
ERR_raise(ERR_LIB_PROV, PROV_R_GENERATE_ERROR); |
| 179 |
return 0; |
| 180 |
} |
| 181 |
*pout = buffer; |
| 182 |
return bytes_needed; |
| 183 |
} |
| 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 |
{ |
| 189 |
OPENSSL_secure_clear_free(out, outlen); |
| 190 |
} |
| 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 |
{ |
| 196 |
size_t bytes; |
| 197 |
unsigned int p_str; |
| 198 |
|
| 199 |
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 |
return ossl_prov_get_entropy(drbg->provctx, pout, entropy, min_len, |
| 205 |
max_len); |
| 206 |
|
| 207 |
if (drbg->parent_get_seed == NULL) { |
| 208 |
ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_CANNOT_SUPPLY_ENTROPY_SEED); |
| 209 |
return 0; |
| 210 |
} |
| 211 |
if (!get_parent_strength(drbg, &p_str)) |
| 212 |
return 0; |
| 213 |
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 |
ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_STRENGTH_TOO_WEAK); |
| 219 |
return 0; |
| 220 |
} |
| 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 |
if (!ossl_drbg_lock_parent(drbg)) |
| 228 |
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 |
bytes = drbg->parent_get_seed(drbg->parent, pout, |
| 239 |
entropy > 0 ? entropy : (int)drbg->strength, |
| 240 |
min_len, max_len, prediction_resistance, |
| 241 |
(unsigned char *)&drbg, sizeof(drbg)); |
| 242 |
ossl_drbg_unlock_parent(drbg); |
| 243 |
return bytes; |
| 244 |
} |
| 245 |
|
| 246 |
static void cleanup_entropy(PROV_DRBG *drbg, unsigned char *out, size_t outlen) |
| 247 |
{ |
| 248 |
if (drbg->parent == NULL) { |
| 249 |
ossl_prov_cleanup_entropy(drbg->provctx, out, outlen); |
| 250 |
} else if (drbg->parent_clear_seed != NULL) { |
| 251 |
if (!ossl_drbg_lock_parent(drbg)) |
| 252 |
return; |
| 253 |
drbg->parent_clear_seed(drbg->parent, out, outlen); |
| 254 |
ossl_drbg_unlock_parent(drbg); |
| 255 |
} |
| 256 |
} |
| 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 |
{ |
| 273 |
PROV_DRBG_NONCE_GLOBAL *dngbl = OPENSSL_zalloc(sizeof(*dngbl)); |
| 274 |
|
| 275 |
if (dngbl == NULL) |
| 276 |
return NULL; |
| 277 |
|
| 278 |
dngbl->rand_nonce_lock = CRYPTO_THREAD_lock_new(); |
| 279 |
if (dngbl->rand_nonce_lock == NULL) { |
| 280 |
OPENSSL_free(dngbl); |
| 281 |
return NULL; |
| 282 |
} |
| 283 |
|
| 284 |
return dngbl; |
| 285 |
} |
| 286 |
|
| 287 |
void ossl_prov_drbg_nonce_ctx_free(void *vdngbl) |
| 288 |
{ |
| 289 |
PROV_DRBG_NONCE_GLOBAL *dngbl = vdngbl; |
| 290 |
|
| 291 |
if (dngbl == NULL) |
| 292 |
return; |
| 293 |
|
| 294 |
CRYPTO_THREAD_lock_free(dngbl->rand_nonce_lock); |
| 295 |
|
| 296 |
OPENSSL_free(dngbl); |
| 297 |
} |
| 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 |
{ |
| 303 |
size_t ret = 0, n; |
| 304 |
unsigned char *buf = NULL; |
| 305 |
OSSL_LIB_CTX *libctx = ossl_prov_ctx_get0_libctx(drbg->provctx); |
| 306 |
PROV_DRBG_NONCE_GLOBAL *dngbl |
| 307 |
= ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DRBG_NONCE_INDEX); |
| 308 |
struct { |
| 309 |
void *drbg; |
| 310 |
int count; |
| 311 |
} data; |
| 312 |
|
| 313 |
if (dngbl == NULL) |
| 314 |
return 0; |
| 315 |
|
| 316 |
if (drbg->parent != NULL && drbg->parent_nonce != NULL) { |
| 317 |
n = drbg->parent_nonce(drbg->parent, NULL, 0, drbg->min_noncelen, |
| 318 |
drbg->max_noncelen); |
| 319 |
if (n > 0 && (buf = OPENSSL_malloc(n)) != NULL) { |
| 320 |
ret = drbg->parent_nonce(drbg->parent, buf, 0, |
| 321 |
drbg->min_noncelen, drbg->max_noncelen); |
| 322 |
if (ret == n) { |
| 323 |
*pout = buf; |
| 324 |
return ret; |
| 325 |
} |
| 326 |
OPENSSL_free(buf); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
/* Use the built in nonce source plus some of our specifics */ |
| 331 |
memset(&data, 0, sizeof(data)); |
| 332 |
data.drbg = drbg; |
| 333 |
if (!CRYPTO_atomic_add(&dngbl->rand_nonce_count, 1, &data.count, |
| 334 |
dngbl->rand_nonce_lock)) |
| 335 |
return 0; |
| 336 |
return ossl_prov_get_nonce(drbg->provctx, pout, min_len, max_len, |
| 337 |
&data, sizeof(data)); |
| 338 |
} |
| 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 |
{ |
| 353 |
unsigned char *nonce = NULL, *entropy = NULL; |
| 354 |
size_t noncelen = 0, entropylen = 0; |
| 355 |
unsigned int min_entropy; |
| 356 |
size_t min_entropylen, max_entropylen; |
| 357 |
|
| 358 |
if (strength > drbg->strength) { |
| 359 |
ERR_raise(ERR_LIB_PROV, PROV_R_INSUFFICIENT_DRBG_STRENGTH); |
| 360 |
goto end; |
| 361 |
} |
| 362 |
min_entropy = drbg->strength; |
| 363 |
min_entropylen = drbg->min_entropylen; |
| 364 |
max_entropylen = drbg->max_entropylen; |
| 365 |
|
| 366 |
if (pers == NULL) { |
| 367 |
pers = (const unsigned char *)ossl_pers_string; |
| 368 |
perslen = sizeof(ossl_pers_string); |
| 369 |
} |
| 370 |
if (perslen > drbg->max_perslen) { |
| 371 |
ERR_raise(ERR_LIB_PROV, PROV_R_PERSONALISATION_STRING_TOO_LONG); |
| 372 |
goto end; |
| 373 |
} |
| 374 |
|
| 375 |
if (drbg->state != EVP_RAND_STATE_UNINITIALISED) { |
| 376 |
if (drbg->state == EVP_RAND_STATE_ERROR) |
| 377 |
ERR_raise(ERR_LIB_PROV, PROV_R_IN_ERROR_STATE); |
| 378 |
else |
| 379 |
ERR_raise(ERR_LIB_PROV, PROV_R_ALREADY_INSTANTIATED); |
| 380 |
goto end; |
| 381 |
} |
| 382 |
|
| 383 |
drbg->state = EVP_RAND_STATE_ERROR; |
| 384 |
|
| 385 |
if (drbg->min_noncelen > 0) { |
| 386 |
if (drbg->parent_nonce != NULL) { |
| 387 |
noncelen = drbg->parent_nonce(drbg->parent, NULL, drbg->strength, |
| 388 |
drbg->min_noncelen, |
| 389 |
drbg->max_noncelen); |
| 390 |
if (noncelen == 0) { |
| 391 |
ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE); |
| 392 |
goto end; |
| 393 |
} |
| 394 |
nonce = OPENSSL_malloc(noncelen); |
| 395 |
if (nonce == NULL) { |
| 396 |
ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE); |
| 397 |
goto end; |
| 398 |
} |
| 399 |
if (noncelen != drbg->parent_nonce(drbg->parent, nonce, drbg->strength, drbg->min_noncelen, drbg->max_noncelen)) { |
| 400 |
ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE); |
| 401 |
goto end; |
| 402 |
} |
| 403 |
#ifndef PROV_RAND_GET_RANDOM_NONCE |
| 404 |
} else if (drbg->parent != NULL) { |
| 405 |
#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 |
min_entropy += drbg->strength / 2; |
| 414 |
min_entropylen += drbg->min_noncelen; |
| 415 |
max_entropylen += drbg->max_noncelen; |
| 416 |
} |
| 417 |
#ifndef PROV_RAND_GET_RANDOM_NONCE |
| 418 |
else { /* parent == NULL */ |
| 419 |
noncelen = prov_drbg_get_nonce(drbg, &nonce, drbg->min_noncelen, |
| 420 |
drbg->max_noncelen); |
| 421 |
if (noncelen < drbg->min_noncelen |
| 422 |
|| noncelen > drbg->max_noncelen) { |
| 423 |
ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE); |
| 424 |
goto end; |
| 425 |
} |
| 426 |
} |
| 427 |
#endif |
| 428 |
} |
| 429 |
|
| 430 |
drbg->reseed_next_counter = tsan_load(&drbg->reseed_counter); |
| 431 |
if (drbg->reseed_next_counter) { |
| 432 |
drbg->reseed_next_counter++; |
| 433 |
if (!drbg->reseed_next_counter) |
| 434 |
drbg->reseed_next_counter = 1; |
| 435 |
} |
| 436 |
|
| 437 |
entropylen = get_entropy(drbg, &entropy, min_entropy, |
| 438 |
min_entropylen, max_entropylen, |
| 439 |
prediction_resistance); |
| 440 |
if (entropylen < min_entropylen |
| 441 |
|| entropylen > max_entropylen) { |
| 442 |
ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_ENTROPY); |
| 443 |
goto end; |
| 444 |
} |
| 445 |
|
| 446 |
if (!drbg->instantiate(drbg, entropy, entropylen, nonce, noncelen, |
| 447 |
pers, perslen)) { |
| 448 |
cleanup_entropy(drbg, entropy, entropylen); |
| 449 |
ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_INSTANTIATING_DRBG); |
| 450 |
goto end; |
| 451 |
} |
| 452 |
cleanup_entropy(drbg, entropy, entropylen); |
| 453 |
|
| 454 |
drbg->state = EVP_RAND_STATE_READY; |
| 455 |
drbg->generate_counter = 1; |
| 456 |
drbg->reseed_time = time(NULL); |
| 457 |
tsan_store(&drbg->reseed_counter, drbg->reseed_next_counter); |
| 458 |
|
| 459 |
end: |
| 460 |
if (nonce != NULL) |
| 461 |
ossl_prov_cleanup_nonce(drbg->provctx, nonce, noncelen); |
| 462 |
if (drbg->state == EVP_RAND_STATE_READY) |
| 463 |
return 1; |
| 464 |
return 0; |
| 465 |
} |
| 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 |
{ |
| 476 |
drbg->state = EVP_RAND_STATE_UNINITIALISED; |
| 477 |
return 1; |
| 478 |
} |
| 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 |
{ |
| 487 |
unsigned char *entropy = NULL; |
| 488 |
size_t entropylen = 0; |
| 489 |
|
| 490 |
if (!ossl_prov_is_running()) |
| 491 |
return 0; |
| 492 |
|
| 493 |
if (drbg->state != EVP_RAND_STATE_READY) { |
| 494 |
/* try to recover from previous errors */ |
| 495 |
rand_drbg_restart(drbg); |
| 496 |
|
| 497 |
if (drbg->state == EVP_RAND_STATE_ERROR) { |
| 498 |
ERR_raise(ERR_LIB_PROV, PROV_R_IN_ERROR_STATE); |
| 499 |
return 0; |
| 500 |
} |
| 501 |
if (drbg->state == EVP_RAND_STATE_UNINITIALISED) { |
| 502 |
ERR_raise(ERR_LIB_PROV, PROV_R_NOT_INSTANTIATED); |
| 503 |
return 0; |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
if (ent != NULL) { |
| 508 |
if (ent_len < drbg->min_entropylen) { |
| 509 |
ERR_raise(ERR_LIB_RAND, RAND_R_ENTROPY_OUT_OF_RANGE); |
| 510 |
drbg->state = EVP_RAND_STATE_ERROR; |
| 511 |
return 0; |
| 512 |
} |
| 513 |
if (ent_len > drbg->max_entropylen) { |
| 514 |
ERR_raise(ERR_LIB_RAND, RAND_R_ENTROPY_INPUT_TOO_LONG); |
| 515 |
drbg->state = EVP_RAND_STATE_ERROR; |
| 516 |
return 0; |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
if (adin == NULL) { |
| 521 |
adinlen = 0; |
| 522 |
} else if (adinlen > drbg->max_adinlen) { |
| 523 |
ERR_raise(ERR_LIB_PROV, PROV_R_ADDITIONAL_INPUT_TOO_LONG); |
| 524 |
return 0; |
| 525 |
} |
| 526 |
|
| 527 |
drbg->state = EVP_RAND_STATE_ERROR; |
| 528 |
|
| 529 |
drbg->reseed_next_counter = tsan_load(&drbg->reseed_counter); |
| 530 |
if (drbg->reseed_next_counter) { |
| 531 |
drbg->reseed_next_counter++; |
| 532 |
if (!drbg->reseed_next_counter) |
| 533 |
drbg->reseed_next_counter = 1; |
| 534 |
} |
| 535 |
|
| 536 |
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 |
if (!drbg->reseed(drbg, ent, ent_len, adin, adinlen)) { |
| 551 |
ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_RESEED); |
| 552 |
return 0; |
| 553 |
} |
| 554 |
/* There isn't much point adding the same additional input twice */ |
| 555 |
adin = NULL; |
| 556 |
adinlen = 0; |
| 557 |
#endif |
| 558 |
} |
| 559 |
|
| 560 |
/* Reseed using our sources in addition */ |
| 561 |
entropylen = get_entropy(drbg, &entropy, drbg->strength, |
| 562 |
drbg->min_entropylen, drbg->max_entropylen, |
| 563 |
prediction_resistance); |
| 564 |
if (entropylen < drbg->min_entropylen |
| 565 |
|| entropylen > drbg->max_entropylen) { |
| 566 |
ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_ENTROPY); |
| 567 |
goto end; |
| 568 |
} |
| 569 |
|
| 570 |
if (!drbg->reseed(drbg, entropy, entropylen, adin, adinlen)) |
| 571 |
goto end; |
| 572 |
|
| 573 |
drbg->state = EVP_RAND_STATE_READY; |
| 574 |
drbg->generate_counter = 1; |
| 575 |
drbg->reseed_time = time(NULL); |
| 576 |
tsan_store(&drbg->reseed_counter, drbg->reseed_next_counter); |
| 577 |
if (drbg->parent != NULL) |
| 578 |
drbg->parent_reseed_counter = get_parent_reseed_count(drbg); |
| 579 |
|
| 580 |
end: |
| 581 |
cleanup_entropy(drbg, entropy, entropylen); |
| 582 |
if (drbg->state == EVP_RAND_STATE_READY) |
| 583 |
return 1; |
| 584 |
return 0; |
| 585 |
} |
| 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 |
{ |
| 598 |
int ret; |
| 599 |
|
| 600 |
if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock)) |
| 601 |
return 0; |
| 602 |
|
| 603 |
ret = ossl_prov_drbg_reseed_unlocked(drbg, prediction_resistance, ent, |
| 604 |
ent_len, adin, adinlen); |
| 605 |
|
| 606 |
if (drbg->lock != NULL) |
| 607 |
CRYPTO_THREAD_unlock(drbg->lock); |
| 608 |
|
| 609 |
return ret; |
| 610 |
} |
| 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 |
{ |
| 626 |
int fork_id; |
| 627 |
int reseed_required = 0; |
| 628 |
int ret = 0; |
| 629 |
time_t reseed_time_interval = drbg->reseed_time_interval; |
| 630 |
time_t now = 0; |
| 631 |
|
| 632 |
if (!ossl_prov_is_running()) |
| 633 |
return 0; |
| 634 |
|
| 635 |
fork_id = openssl_get_fork_id(); |
| 636 |
|
| 637 |
if (reseed_time_interval > 0) |
| 638 |
now = time(NULL); |
| 639 |
|
| 640 |
if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock)) |
| 641 |
return 0; |
| 642 |
|
| 643 |
if (drbg->state != EVP_RAND_STATE_READY) { |
| 644 |
/* try to recover from previous errors */ |
| 645 |
rand_drbg_restart(drbg); |
| 646 |
|
| 647 |
if (drbg->state == EVP_RAND_STATE_ERROR) { |
| 648 |
ERR_raise(ERR_LIB_PROV, PROV_R_IN_ERROR_STATE); |
| 649 |
goto err; |
| 650 |
} |
| 651 |
if (drbg->state == EVP_RAND_STATE_UNINITIALISED) { |
| 652 |
ERR_raise(ERR_LIB_PROV, PROV_R_NOT_INSTANTIATED); |
| 653 |
goto err; |
| 654 |
} |
| 655 |
} |
| 656 |
if (strength > drbg->strength) { |
| 657 |
ERR_raise(ERR_LIB_PROV, PROV_R_INSUFFICIENT_DRBG_STRENGTH); |
| 658 |
goto err; |
| 659 |
} |
| 660 |
|
| 661 |
if (outlen > drbg->max_request) { |
| 662 |
ERR_raise(ERR_LIB_PROV, PROV_R_REQUEST_TOO_LARGE_FOR_DRBG); |
| 663 |
goto err; |
| 664 |
} |
| 665 |
if (adinlen > drbg->max_adinlen) { |
| 666 |
ERR_raise(ERR_LIB_PROV, PROV_R_ADDITIONAL_INPUT_TOO_LONG); |
| 667 |
goto err; |
| 668 |
} |
| 669 |
|
| 670 |
if (drbg->fork_id != fork_id) { |
| 671 |
drbg->fork_id = fork_id; |
| 672 |
reseed_required = 1; |
| 673 |
} |
| 674 |
|
| 675 |
if (drbg->reseed_interval > 0) { |
| 676 |
if (drbg->generate_counter >= drbg->reseed_interval) |
| 677 |
reseed_required = 1; |
| 678 |
} |
| 679 |
if (reseed_time_interval > 0) { |
| 680 |
if (now < drbg->reseed_time |
| 681 |
|| now - drbg->reseed_time >= reseed_time_interval) |
| 682 |
reseed_required = 1; |
| 683 |
} |
| 684 |
if (drbg->parent != NULL |
| 685 |
&& get_parent_reseed_count(drbg) != drbg->parent_reseed_counter) |
| 686 |
reseed_required = 1; |
| 687 |
|
| 688 |
if (reseed_required || prediction_resistance) { |
| 689 |
if (!ossl_prov_drbg_reseed_unlocked(drbg, prediction_resistance, NULL, |
| 690 |
0, adin, adinlen)) { |
| 691 |
ERR_raise(ERR_LIB_PROV, PROV_R_RESEED_ERROR); |
| 692 |
goto err; |
| 693 |
} |
| 694 |
adin = NULL; |
| 695 |
adinlen = 0; |
| 696 |
} |
| 697 |
|
| 698 |
if (!drbg->generate(drbg, out, outlen, adin, adinlen)) { |
| 699 |
drbg->state = EVP_RAND_STATE_ERROR; |
| 700 |
ERR_raise(ERR_LIB_PROV, PROV_R_GENERATE_ERROR); |
| 701 |
goto err; |
| 702 |
} |
| 703 |
|
| 704 |
drbg->generate_counter++; |
| 705 |
|
| 706 |
ret = 1; |
| 707 |
err: |
| 708 |
if (drbg->lock != NULL) |
| 709 |
CRYPTO_THREAD_unlock(drbg->lock); |
| 710 |
|
| 711 |
return ret; |
| 712 |
} |
| 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 |
{ |
| 733 |
/* repair error state */ |
| 734 |
if (drbg->state == EVP_RAND_STATE_ERROR) |
| 735 |
drbg->uninstantiate(drbg); |
| 736 |
|
| 737 |
/* repair uninitialized state */ |
| 738 |
if (drbg->state == EVP_RAND_STATE_UNINITIALISED) |
| 739 |
/* reinstantiate drbg */ |
| 740 |
ossl_prov_drbg_instantiate(drbg, drbg->strength, 0, NULL, 0); |
| 741 |
|
| 742 |
return drbg->state == EVP_RAND_STATE_READY; |
| 743 |
} |
| 744 |
|
| 745 |
/* Provider support from here down */ |
| 746 |
static const OSSL_DISPATCH *find_call(const OSSL_DISPATCH *dispatch, |
| 747 |
int function) |
| 748 |
{ |
| 749 |
if (dispatch != NULL) |
| 750 |
while (dispatch->function_id != 0) { |
| 751 |
if (dispatch->function_id == function) |
| 752 |
return dispatch; |
| 753 |
dispatch++; |
| 754 |
} |
| 755 |
return NULL; |
| 756 |
} |
| 757 |
|
| 758 |
int ossl_drbg_enable_locking(void *vctx) |
| 759 |
{ |
| 760 |
PROV_DRBG *drbg = vctx; |
| 761 |
|
| 762 |
if (drbg != NULL && drbg->lock == NULL) { |
| 763 |
if (drbg->parent_enable_locking != NULL) |
| 764 |
if (!drbg->parent_enable_locking(drbg->parent)) { |
| 765 |
ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_LOCKING_NOT_ENABLED); |
| 766 |
return 0; |
| 767 |
} |
| 768 |
drbg->lock = CRYPTO_THREAD_lock_new(); |
| 769 |
if (drbg->lock == NULL) { |
| 770 |
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_CREATE_LOCK); |
| 771 |
return 0; |
| 772 |
} |
| 773 |
} |
| 774 |
return 1; |
| 775 |
} |
| 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 |
{ |
| 798 |
PROV_DRBG *drbg; |
| 799 |
unsigned int p_str; |
| 800 |
const OSSL_DISPATCH *pfunc; |
| 801 |
|
| 802 |
if (!ossl_prov_is_running()) |
| 803 |
return NULL; |
| 804 |
|
| 805 |
drbg = OPENSSL_zalloc(sizeof(*drbg)); |
| 806 |
if (drbg == NULL) |
| 807 |
return NULL; |
| 808 |
|
| 809 |
drbg->provctx = provctx; |
| 810 |
drbg->instantiate = instantiate; |
| 811 |
drbg->uninstantiate = uninstantiate; |
| 812 |
drbg->reseed = reseed; |
| 813 |
drbg->generate = generate; |
| 814 |
drbg->fork_id = openssl_get_fork_id(); |
| 815 |
|
| 816 |
/* Extract parent's functions */ |
| 817 |
drbg->parent = parent; |
| 818 |
if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_ENABLE_LOCKING)) != NULL) |
| 819 |
drbg->parent_enable_locking = OSSL_FUNC_rand_enable_locking(pfunc); |
| 820 |
if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_LOCK)) != NULL) |
| 821 |
drbg->parent_lock = OSSL_FUNC_rand_lock(pfunc); |
| 822 |
if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_UNLOCK)) != NULL) |
| 823 |
drbg->parent_unlock = OSSL_FUNC_rand_unlock(pfunc); |
| 824 |
if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_GET_CTX_PARAMS)) != NULL) |
| 825 |
drbg->parent_get_ctx_params = OSSL_FUNC_rand_get_ctx_params(pfunc); |
| 826 |
if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_NONCE)) != NULL) |
| 827 |
drbg->parent_nonce = OSSL_FUNC_rand_nonce(pfunc); |
| 828 |
if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_GET_SEED)) != NULL) |
| 829 |
drbg->parent_get_seed = OSSL_FUNC_rand_get_seed(pfunc); |
| 830 |
if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_CLEAR_SEED)) != NULL) |
| 831 |
drbg->parent_clear_seed = OSSL_FUNC_rand_clear_seed(pfunc); |
| 832 |
|
| 833 |
/* Set some default maximums up */ |
| 834 |
drbg->max_entropylen = DRBG_MAX_LENGTH; |
| 835 |
drbg->max_noncelen = DRBG_MAX_LENGTH; |
| 836 |
drbg->max_perslen = DRBG_MAX_LENGTH; |
| 837 |
drbg->max_adinlen = DRBG_MAX_LENGTH; |
| 838 |
drbg->generate_counter = 1; |
| 839 |
drbg->reseed_counter = 1; |
| 840 |
drbg->reseed_interval = RESEED_INTERVAL; |
| 841 |
drbg->reseed_time_interval = TIME_INTERVAL; |
| 842 |
|
| 843 |
if (!dnew(drbg)) |
| 844 |
goto err; |
| 845 |
|
| 846 |
if (parent != NULL) { |
| 847 |
if (!get_parent_strength(drbg, &p_str)) |
| 848 |
goto err; |
| 849 |
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 |
ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_STRENGTH_TOO_WEAK); |
| 855 |
goto err; |
| 856 |
} |
| 857 |
} |
| 858 |
#ifdef TSAN_REQUIRES_LOCKING |
| 859 |
if (!ossl_drbg_enable_locking(drbg)) |
| 860 |
goto err; |
| 861 |
#endif |
| 862 |
return drbg; |
| 863 |
|
| 864 |
err: |
| 865 |
dfree(drbg); |
| 866 |
return NULL; |
| 867 |
} |
| 868 |
|
| 869 |
void ossl_rand_drbg_free(PROV_DRBG *drbg) |
| 870 |
{ |
| 871 |
if (drbg == NULL) |
| 872 |
return; |
| 873 |
|
| 874 |
CRYPTO_THREAD_lock_free(drbg->lock); |
| 875 |
OPENSSL_free(drbg); |
| 876 |
} |
| 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 |
{ |
| 885 |
if (p->state != NULL && !OSSL_PARAM_set_int(p->state, drbg->state)) |
| 886 |
return 0; |
| 887 |
|
| 888 |
if (p->str != NULL && !OSSL_PARAM_set_int(p->str, drbg->strength)) |
| 889 |
return 0; |
| 890 |
|
| 891 |
if (p->minentlen != NULL |
| 892 |
&& !OSSL_PARAM_set_size_t(p->minentlen, drbg->min_entropylen)) |
| 893 |
return 0; |
| 894 |
|
| 895 |
if (p->maxentlen != NULL |
| 896 |
&& !OSSL_PARAM_set_size_t(p->maxentlen, drbg->max_entropylen)) |
| 897 |
return 0; |
| 898 |
|
| 899 |
if (p->minnonlen != NULL |
| 900 |
&& !OSSL_PARAM_set_size_t(p->minnonlen, drbg->min_noncelen)) |
| 901 |
return 0; |
| 902 |
|
| 903 |
if (p->maxnonlen != NULL |
| 904 |
&& !OSSL_PARAM_set_size_t(p->maxnonlen, drbg->max_noncelen)) |
| 905 |
return 0; |
| 906 |
|
| 907 |
if (p->maxperlen != NULL |
| 908 |
&& !OSSL_PARAM_set_size_t(p->maxperlen, drbg->max_perslen)) |
| 909 |
return 0; |
| 910 |
|
| 911 |
if (p->maxadlen != NULL |
| 912 |
&& !OSSL_PARAM_set_size_t(p->maxadlen, drbg->max_adinlen)) |
| 913 |
return 0; |
| 914 |
|
| 915 |
if (p->reseed_req != NULL |
| 916 |
&& !OSSL_PARAM_set_uint(p->reseed_req, drbg->reseed_interval)) |
| 917 |
return 0; |
| 918 |
|
| 919 |
if (p->reseed_time != NULL |
| 920 |
&& !OSSL_PARAM_set_time_t(p->reseed_time, drbg->reseed_time)) |
| 921 |
return 0; |
| 922 |
|
| 923 |
if (p->reseed_int != NULL |
| 924 |
&& !OSSL_PARAM_set_time_t(p->reseed_int, drbg->reseed_time_interval)) |
| 925 |
return 0; |
| 926 |
|
| 927 |
if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(drbg, p->ind)) |
| 928 |
return 0; |
| 929 |
|
| 930 |
return 1; |
| 931 |
} |
| 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 |
{ |
| 941 |
size_t cnt = 0; |
| 942 |
|
| 943 |
/* This value never changes once set */ |
| 944 |
if (p->maxreq != NULL) { |
| 945 |
if (!OSSL_PARAM_set_size_t(p->maxreq, drbg->max_request)) |
| 946 |
return 0; |
| 947 |
cnt++; |
| 948 |
} |
| 949 |
|
| 950 |
/* |
| 951 |
* Can be changed by multiple threads, but we tolerate inaccuracies in this |
| 952 |
* value. |
| 953 |
*/ |
| 954 |
if (p->reseed_cnt != NULL) { |
| 955 |
if (!OSSL_PARAM_set_uint(p->reseed_cnt, tsan_load(&drbg->reseed_counter))) |
| 956 |
return 0; |
| 957 |
cnt++; |
| 958 |
} |
| 959 |
|
| 960 |
if (params[cnt].key == NULL) |
| 961 |
*complete = 1; |
| 962 |
else |
| 963 |
*complete = 0; |
| 964 |
|
| 965 |
return 1; |
| 966 |
} |
| 967 |
|
| 968 |
int ossl_drbg_set_ctx_params(PROV_DRBG *drbg, |
| 969 |
const struct drbg_set_ctx_params_st *p) |
| 970 |
{ |
| 971 |
if (p->reseed_req != NULL |
| 972 |
&& !OSSL_PARAM_get_uint(p->reseed_req, &drbg->reseed_interval)) |
| 973 |
return 0; |
| 974 |
|
| 975 |
if (p->reseed_time != NULL |
| 976 |
&& !OSSL_PARAM_get_time_t(p->reseed_time, &drbg->reseed_time_interval)) |
| 977 |
return 0; |
| 978 |
|
| 979 |
return 1; |
| 980 |
} |
| 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 |
{ |
| 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 |
if (EVP_MD_xof(md)) { |
| 1021 |
ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED); |
| 1022 |
return 0; |
| 1023 |
} |
| 1024 |
#endif /* FIPS_MODULE */ |
| 1025 |
return 1; |
| 1026 |
} |