/src/openssl30/crypto/evp/evp_rand.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved. | 
| 3 |  |  * | 
| 4 |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
| 5 |  |  * this file except in compliance with the License.  You can obtain a copy | 
| 6 |  |  * in the file LICENSE in the source distribution or at | 
| 7 |  |  * https://www.openssl.org/source/license.html | 
| 8 |  |  */ | 
| 9 |  |  | 
| 10 |  | #include <stdio.h> | 
| 11 |  | #include <stdlib.h> | 
| 12 |  | #include <openssl/evp.h> | 
| 13 |  | #include <openssl/rand.h> | 
| 14 |  | #include <openssl/core.h> | 
| 15 |  | #include <openssl/core_names.h> | 
| 16 |  | #include <openssl/crypto.h> | 
| 17 |  | #include "internal/cryptlib.h" | 
| 18 |  | #include "internal/numbers.h" | 
| 19 |  | #include "internal/provider.h" | 
| 20 |  | #include "internal/core.h" | 
| 21 |  | #include "crypto/evp.h" | 
| 22 |  | #include "evp_local.h" | 
| 23 |  |  | 
| 24 |  | struct evp_rand_st { | 
| 25 |  |     OSSL_PROVIDER *prov; | 
| 26 |  |     int name_id; | 
| 27 |  |     char *type_name; | 
| 28 |  |     const char *description; | 
| 29 |  |     CRYPTO_REF_COUNT refcnt; | 
| 30 |  |     CRYPTO_RWLOCK *refcnt_lock; | 
| 31 |  |  | 
| 32 |  |     const OSSL_DISPATCH *dispatch; | 
| 33 |  |     OSSL_FUNC_rand_newctx_fn *newctx; | 
| 34 |  |     OSSL_FUNC_rand_freectx_fn *freectx; | 
| 35 |  |     OSSL_FUNC_rand_instantiate_fn *instantiate; | 
| 36 |  |     OSSL_FUNC_rand_uninstantiate_fn *uninstantiate; | 
| 37 |  |     OSSL_FUNC_rand_generate_fn *generate; | 
| 38 |  |     OSSL_FUNC_rand_reseed_fn *reseed; | 
| 39 |  |     OSSL_FUNC_rand_nonce_fn *nonce; | 
| 40 |  |     OSSL_FUNC_rand_enable_locking_fn *enable_locking; | 
| 41 |  |     OSSL_FUNC_rand_lock_fn *lock; | 
| 42 |  |     OSSL_FUNC_rand_unlock_fn *unlock; | 
| 43 |  |     OSSL_FUNC_rand_gettable_params_fn *gettable_params; | 
| 44 |  |     OSSL_FUNC_rand_gettable_ctx_params_fn *gettable_ctx_params; | 
| 45 |  |     OSSL_FUNC_rand_settable_ctx_params_fn *settable_ctx_params; | 
| 46 |  |     OSSL_FUNC_rand_get_params_fn *get_params; | 
| 47 |  |     OSSL_FUNC_rand_get_ctx_params_fn *get_ctx_params; | 
| 48 |  |     OSSL_FUNC_rand_set_ctx_params_fn *set_ctx_params; | 
| 49 |  |     OSSL_FUNC_rand_verify_zeroization_fn *verify_zeroization; | 
| 50 |  | } /* EVP_RAND */ ; | 
| 51 |  |  | 
| 52 |  | static int evp_rand_up_ref(void *vrand) | 
| 53 | 192 | { | 
| 54 | 192 |     EVP_RAND *rand = (EVP_RAND *)vrand; | 
| 55 | 192 |     int ref = 0; | 
| 56 |  |  | 
| 57 | 192 |     if (rand != NULL) | 
| 58 | 192 |         return CRYPTO_UP_REF(&rand->refcnt, &ref, rand->refcnt_lock); | 
| 59 | 0 |     return 1; | 
| 60 | 192 | } | 
| 61 |  |  | 
| 62 |  | static void evp_rand_free(void *vrand) | 
| 63 | 268 | { | 
| 64 | 268 |     EVP_RAND *rand = (EVP_RAND *)vrand; | 
| 65 | 268 |     int ref = 0; | 
| 66 |  |  | 
| 67 | 268 |     if (rand == NULL) | 
| 68 | 0 |         return; | 
| 69 | 268 |     CRYPTO_DOWN_REF(&rand->refcnt, &ref, rand->refcnt_lock); | 
| 70 | 268 |     if (ref > 0) | 
| 71 | 192 |         return; | 
| 72 | 76 |     OPENSSL_free(rand->type_name); | 
| 73 | 76 |     ossl_provider_free(rand->prov); | 
| 74 | 76 |     CRYPTO_THREAD_lock_free(rand->refcnt_lock); | 
| 75 | 76 |     OPENSSL_free(rand); | 
| 76 | 76 | } | 
| 77 |  |  | 
| 78 |  | static void *evp_rand_new(void) | 
| 79 | 35 | { | 
| 80 | 35 |     EVP_RAND *rand = OPENSSL_zalloc(sizeof(*rand)); | 
| 81 |  |  | 
| 82 | 35 |     if (rand == NULL | 
| 83 | 35 |             || (rand->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL) { | 
| 84 | 0 |         OPENSSL_free(rand); | 
| 85 | 0 |         return NULL; | 
| 86 | 0 |     } | 
| 87 | 35 |     rand->refcnt = 1; | 
| 88 | 35 |     return rand; | 
| 89 | 35 | } | 
| 90 |  |  | 
| 91 |  | /* Enable locking of the underlying DRBG/RAND if available */ | 
| 92 |  | int EVP_RAND_enable_locking(EVP_RAND_CTX *rand) | 
| 93 | 13 | { | 
| 94 | 13 |     if (rand->meth->enable_locking != NULL) | 
| 95 | 13 |         return rand->meth->enable_locking(rand->algctx); | 
| 96 | 13 |     ERR_raise(ERR_LIB_EVP, EVP_R_LOCKING_NOT_SUPPORTED); | 
| 97 | 0 |     return 0; | 
| 98 | 13 | } | 
| 99 |  |  | 
| 100 |  | /* Lock the underlying DRBG/RAND if available */ | 
| 101 |  | static int evp_rand_lock(EVP_RAND_CTX *rand) | 
| 102 | 395k | { | 
| 103 | 395k |     if (rand->meth->lock != NULL) | 
| 104 | 16.0k |         return rand->meth->lock(rand->algctx); | 
| 105 | 379k |     return 1; | 
| 106 | 395k | } | 
| 107 |  |  | 
| 108 |  | /* Unlock the underlying DRBG/RAND if available */ | 
| 109 |  | static void evp_rand_unlock(EVP_RAND_CTX *rand) | 
| 110 | 395k | { | 
| 111 | 395k |     if (rand->meth->unlock != NULL) | 
| 112 | 16.0k |         rand->meth->unlock(rand->algctx); | 
| 113 | 395k | } | 
| 114 |  |  | 
| 115 |  | static void *evp_rand_from_algorithm(int name_id, | 
| 116 |  |                                      const OSSL_ALGORITHM *algodef, | 
| 117 |  |                                      OSSL_PROVIDER *prov) | 
| 118 | 76 | { | 
| 119 | 76 |     const OSSL_DISPATCH *fns = algodef->implementation; | 
| 120 | 76 |     EVP_RAND *rand = NULL; | 
| 121 | 76 |     int fnrandcnt = 0, fnctxcnt = 0, fnlockcnt = 0, fnenablelockcnt = 0; | 
| 122 |  | #ifdef FIPS_MODULE | 
| 123 |  |     int fnzeroizecnt = 0; | 
| 124 |  | #endif | 
| 125 |  |  | 
| 126 | 76 |     if ((rand = evp_rand_new()) == NULL) { | 
| 127 | 0 |         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); | 
| 128 | 0 |         return NULL; | 
| 129 | 0 |     } | 
| 130 | 76 |     rand->name_id = name_id; | 
| 131 | 76 |     if ((rand->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) { | 
| 132 | 0 |         evp_rand_free(rand); | 
| 133 | 0 |         return NULL; | 
| 134 | 0 |     } | 
| 135 | 76 |     rand->description = algodef->algorithm_description; | 
| 136 | 76 |     rand->dispatch = fns; | 
| 137 | 1.17k |     for (; fns->function_id != 0; fns++) { | 
| 138 | 1.10k |         switch (fns->function_id) { | 
| 139 | 76 |         case OSSL_FUNC_RAND_NEWCTX: | 
| 140 | 76 |             if (rand->newctx != NULL) | 
| 141 | 0 |                 break; | 
| 142 | 76 |             rand->newctx = OSSL_FUNC_rand_newctx(fns); | 
| 143 | 76 |             fnctxcnt++; | 
| 144 | 76 |             break; | 
| 145 | 76 |         case OSSL_FUNC_RAND_FREECTX: | 
| 146 | 76 |             if (rand->freectx != NULL) | 
| 147 | 0 |                 break; | 
| 148 | 76 |             rand->freectx = OSSL_FUNC_rand_freectx(fns); | 
| 149 | 76 |             fnctxcnt++; | 
| 150 | 76 |             break; | 
| 151 | 76 |         case OSSL_FUNC_RAND_INSTANTIATE: | 
| 152 | 76 |             if (rand->instantiate != NULL) | 
| 153 | 0 |                 break; | 
| 154 | 76 |             rand->instantiate = OSSL_FUNC_rand_instantiate(fns); | 
| 155 | 76 |             fnrandcnt++; | 
| 156 | 76 |             break; | 
| 157 | 76 |         case OSSL_FUNC_RAND_UNINSTANTIATE: | 
| 158 | 76 |              if (rand->uninstantiate != NULL) | 
| 159 | 0 |                 break; | 
| 160 | 76 |             rand->uninstantiate = OSSL_FUNC_rand_uninstantiate(fns); | 
| 161 | 76 |             fnrandcnt++; | 
| 162 | 76 |             break; | 
| 163 | 76 |         case OSSL_FUNC_RAND_GENERATE: | 
| 164 | 76 |             if (rand->generate != NULL) | 
| 165 | 0 |                 break; | 
| 166 | 76 |             rand->generate = OSSL_FUNC_rand_generate(fns); | 
| 167 | 76 |             fnrandcnt++; | 
| 168 | 76 |             break; | 
| 169 | 65 |         case OSSL_FUNC_RAND_RESEED: | 
| 170 | 65 |             if (rand->reseed != NULL) | 
| 171 | 0 |                 break; | 
| 172 | 65 |             rand->reseed = OSSL_FUNC_rand_reseed(fns); | 
| 173 | 65 |             break; | 
| 174 | 13 |         case OSSL_FUNC_RAND_NONCE: | 
| 175 | 13 |             if (rand->nonce != NULL) | 
| 176 | 0 |                 break; | 
| 177 | 13 |             rand->nonce = OSSL_FUNC_rand_nonce(fns); | 
| 178 | 13 |             break; | 
| 179 | 76 |         case OSSL_FUNC_RAND_ENABLE_LOCKING: | 
| 180 | 76 |             if (rand->enable_locking != NULL) | 
| 181 | 0 |                 break; | 
| 182 | 76 |             rand->enable_locking = OSSL_FUNC_rand_enable_locking(fns); | 
| 183 | 76 |             fnenablelockcnt++; | 
| 184 | 76 |             break; | 
| 185 | 65 |         case OSSL_FUNC_RAND_LOCK: | 
| 186 | 65 |             if (rand->lock != NULL) | 
| 187 | 0 |                 break; | 
| 188 | 65 |             rand->lock = OSSL_FUNC_rand_lock(fns); | 
| 189 | 65 |             fnlockcnt++; | 
| 190 | 65 |             break; | 
| 191 | 65 |         case OSSL_FUNC_RAND_UNLOCK: | 
| 192 | 65 |             if (rand->unlock != NULL) | 
| 193 | 0 |                 break; | 
| 194 | 65 |             rand->unlock = OSSL_FUNC_rand_unlock(fns); | 
| 195 | 65 |             fnlockcnt++; | 
| 196 | 65 |             break; | 
| 197 | 0 |         case OSSL_FUNC_RAND_GETTABLE_PARAMS: | 
| 198 | 0 |             if (rand->gettable_params != NULL) | 
| 199 | 0 |                 break; | 
| 200 | 0 |             rand->gettable_params = | 
| 201 | 0 |                 OSSL_FUNC_rand_gettable_params(fns); | 
| 202 | 0 |             break; | 
| 203 | 76 |         case OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS: | 
| 204 | 76 |             if (rand->gettable_ctx_params != NULL) | 
| 205 | 0 |                 break; | 
| 206 | 76 |             rand->gettable_ctx_params = | 
| 207 | 76 |                 OSSL_FUNC_rand_gettable_ctx_params(fns); | 
| 208 | 76 |             break; | 
| 209 | 52 |         case OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS: | 
| 210 | 52 |             if (rand->settable_ctx_params != NULL) | 
| 211 | 0 |                 break; | 
| 212 | 52 |             rand->settable_ctx_params = | 
| 213 | 52 |                 OSSL_FUNC_rand_settable_ctx_params(fns); | 
| 214 | 52 |             break; | 
| 215 | 0 |         case OSSL_FUNC_RAND_GET_PARAMS: | 
| 216 | 0 |             if (rand->get_params != NULL) | 
| 217 | 0 |                 break; | 
| 218 | 0 |             rand->get_params = OSSL_FUNC_rand_get_params(fns); | 
| 219 | 0 |             break; | 
| 220 | 76 |         case OSSL_FUNC_RAND_GET_CTX_PARAMS: | 
| 221 | 76 |             if (rand->get_ctx_params != NULL) | 
| 222 | 0 |                 break; | 
| 223 | 76 |             rand->get_ctx_params = OSSL_FUNC_rand_get_ctx_params(fns); | 
| 224 | 76 |             fnctxcnt++; | 
| 225 | 76 |             break; | 
| 226 | 52 |         case OSSL_FUNC_RAND_SET_CTX_PARAMS: | 
| 227 | 52 |             if (rand->set_ctx_params != NULL) | 
| 228 | 0 |                 break; | 
| 229 | 52 |             rand->set_ctx_params = OSSL_FUNC_rand_set_ctx_params(fns); | 
| 230 | 52 |             break; | 
| 231 | 65 |         case OSSL_FUNC_RAND_VERIFY_ZEROIZATION: | 
| 232 | 65 |             if (rand->verify_zeroization != NULL) | 
| 233 | 0 |                 break; | 
| 234 | 65 |             rand->verify_zeroization = OSSL_FUNC_rand_verify_zeroization(fns); | 
| 235 |  | #ifdef FIPS_MODULE | 
| 236 |  |             fnzeroizecnt++; | 
| 237 |  | #endif | 
| 238 | 65 |             break; | 
| 239 | 1.10k |         } | 
| 240 | 1.10k |     } | 
| 241 |  |     /* | 
| 242 |  |      * In order to be a consistent set of functions we must have at least | 
| 243 |  |      * a complete set of "rand" functions and a complete set of context | 
| 244 |  |      * management functions.  In FIPS mode, we also require the zeroization | 
| 245 |  |      * verification function. | 
| 246 |  |      * | 
| 247 |  |      * In addition, if locking can be enabled, we need a complete set of | 
| 248 |  |      * locking functions. | 
| 249 |  |      */ | 
| 250 | 76 |     if (fnrandcnt != 3 | 
| 251 | 76 |             || fnctxcnt != 3 | 
| 252 | 76 |             || (fnenablelockcnt != 0 && fnenablelockcnt != 1) | 
| 253 | 76 |             || (fnlockcnt != 0 && fnlockcnt != 2) | 
| 254 |  | #ifdef FIPS_MODULE | 
| 255 |  |             || fnzeroizecnt != 1 | 
| 256 |  | #endif | 
| 257 | 76 |        ) { | 
| 258 | 0 |         evp_rand_free(rand); | 
| 259 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS); | 
| 260 | 0 |         return NULL; | 
| 261 | 0 |     } | 
| 262 |  |  | 
| 263 | 76 |     if (prov != NULL && !ossl_provider_up_ref(prov)) { | 
| 264 | 0 |         evp_rand_free(rand); | 
| 265 | 0 |         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); | 
| 266 | 0 |         return NULL; | 
| 267 | 0 |     } | 
| 268 | 76 |     rand->prov = prov; | 
| 269 |  |  | 
| 270 | 76 |     return rand; | 
| 271 | 76 | } | 
| 272 |  |  | 
| 273 |  | EVP_RAND *EVP_RAND_fetch(OSSL_LIB_CTX *libctx, const char *algorithm, | 
| 274 |  |                          const char *properties) | 
| 275 | 45 | { | 
| 276 | 45 |     return evp_generic_fetch(libctx, OSSL_OP_RAND, algorithm, properties, | 
| 277 | 45 |                              evp_rand_from_algorithm, evp_rand_up_ref, | 
| 278 | 45 |                              evp_rand_free); | 
| 279 | 45 | } | 
| 280 |  |  | 
| 281 |  | int EVP_RAND_up_ref(EVP_RAND *rand) | 
| 282 | 45 | { | 
| 283 | 45 |     return evp_rand_up_ref(rand); | 
| 284 | 45 | } | 
| 285 |  |  | 
| 286 |  | void EVP_RAND_free(EVP_RAND *rand) | 
| 287 | 90 | { | 
| 288 | 90 |     evp_rand_free(rand); | 
| 289 | 90 | } | 
| 290 |  |  | 
| 291 |  | int evp_rand_get_number(const EVP_RAND *rand) | 
| 292 | 0 | { | 
| 293 | 0 |     return rand->name_id; | 
| 294 | 0 | } | 
| 295 |  |  | 
| 296 |  | const char *EVP_RAND_get0_name(const EVP_RAND *rand) | 
| 297 | 0 | { | 
| 298 | 0 |     return rand->type_name; | 
| 299 | 0 | } | 
| 300 |  |  | 
| 301 |  | const char *EVP_RAND_get0_description(const EVP_RAND *rand) | 
| 302 | 0 | { | 
| 303 | 0 |     return rand->description; | 
| 304 | 0 | } | 
| 305 |  |  | 
| 306 |  | int EVP_RAND_is_a(const EVP_RAND *rand, const char *name) | 
| 307 | 0 | { | 
| 308 | 0 |     return rand != NULL && evp_is_a(rand->prov, rand->name_id, NULL, name); | 
| 309 | 0 | } | 
| 310 |  |  | 
| 311 |  | const OSSL_PROVIDER *EVP_RAND_get0_provider(const EVP_RAND *rand) | 
| 312 | 2 | { | 
| 313 | 2 |     return rand->prov; | 
| 314 | 2 | } | 
| 315 |  |  | 
| 316 |  | int EVP_RAND_get_params(EVP_RAND *rand, OSSL_PARAM params[]) | 
| 317 | 0 | { | 
| 318 | 0 |     if (rand->get_params != NULL) | 
| 319 | 0 |         return rand->get_params(params); | 
| 320 | 0 |     return 1; | 
| 321 | 0 | } | 
| 322 |  |  | 
| 323 |  | static int evp_rand_ctx_up_ref(EVP_RAND_CTX *ctx) | 
| 324 | 15 | { | 
| 325 | 15 |     int ref = 0; | 
| 326 |  |  | 
| 327 | 15 |     return CRYPTO_UP_REF(&ctx->refcnt, &ref, ctx->refcnt_lock); | 
| 328 | 15 | } | 
| 329 |  |  | 
| 330 |  | EVP_RAND_CTX *EVP_RAND_CTX_new(EVP_RAND *rand, EVP_RAND_CTX *parent) | 
| 331 | 21 | { | 
| 332 | 21 |     EVP_RAND_CTX *ctx; | 
| 333 | 21 |     void *parent_ctx = NULL; | 
| 334 | 21 |     const OSSL_DISPATCH *parent_dispatch = NULL; | 
| 335 |  |  | 
| 336 | 21 |     if (rand == NULL) { | 
| 337 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM); | 
| 338 | 0 |         return NULL; | 
| 339 | 0 |     } | 
| 340 |  |  | 
| 341 | 21 |     ctx = OPENSSL_zalloc(sizeof(*ctx)); | 
| 342 | 21 |     if (ctx == NULL || (ctx->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL) { | 
| 343 | 0 |         OPENSSL_free(ctx); | 
| 344 | 0 |         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); | 
| 345 | 0 |         return NULL; | 
| 346 | 0 |     } | 
| 347 | 21 |     if (parent != NULL) { | 
| 348 | 15 |         if (!evp_rand_ctx_up_ref(parent)) { | 
| 349 | 0 |             ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); | 
| 350 | 0 |             CRYPTO_THREAD_lock_free(ctx->refcnt_lock); | 
| 351 | 0 |             OPENSSL_free(ctx); | 
| 352 | 0 |             return NULL; | 
| 353 | 0 |         } | 
| 354 | 15 |         parent_ctx = parent->algctx; | 
| 355 | 15 |         parent_dispatch = parent->meth->dispatch; | 
| 356 | 15 |     } | 
| 357 | 21 |     if ((ctx->algctx = rand->newctx(ossl_provider_ctx(rand->prov), parent_ctx, | 
| 358 | 21 |                                     parent_dispatch)) == NULL | 
| 359 | 21 |             || !EVP_RAND_up_ref(rand)) { | 
| 360 | 0 |         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); | 
| 361 | 0 |         rand->freectx(ctx->algctx); | 
| 362 | 0 |         CRYPTO_THREAD_lock_free(ctx->refcnt_lock); | 
| 363 | 0 |         OPENSSL_free(ctx); | 
| 364 | 0 |         EVP_RAND_CTX_free(parent); | 
| 365 | 0 |         return NULL; | 
| 366 | 0 |     } | 
| 367 | 21 |     ctx->meth = rand; | 
| 368 | 21 |     ctx->parent = parent; | 
| 369 | 21 |     ctx->refcnt = 1; | 
| 370 | 21 |     return ctx; | 
| 371 | 21 | } | 
| 372 |  |  | 
| 373 |  | void EVP_RAND_CTX_free(EVP_RAND_CTX *ctx) | 
| 374 | 143 | { | 
| 375 | 143 |     int ref = 0; | 
| 376 | 143 |     EVP_RAND_CTX *parent; | 
| 377 |  |  | 
| 378 | 143 |     if (ctx == NULL) | 
| 379 | 66 |         return; | 
| 380 |  |  | 
| 381 | 77 |     CRYPTO_DOWN_REF(&ctx->refcnt, &ref, ctx->refcnt_lock); | 
| 382 | 77 |     if (ref > 0) | 
| 383 | 32 |         return; | 
| 384 | 45 |     parent = ctx->parent; | 
| 385 | 45 |     ctx->meth->freectx(ctx->algctx); | 
| 386 | 45 |     ctx->algctx = NULL; | 
| 387 | 45 |     EVP_RAND_free(ctx->meth); | 
| 388 | 45 |     CRYPTO_THREAD_lock_free(ctx->refcnt_lock); | 
| 389 | 45 |     OPENSSL_free(ctx); | 
| 390 | 45 |     EVP_RAND_CTX_free(parent); | 
| 391 | 45 | } | 
| 392 |  |  | 
| 393 |  | EVP_RAND *EVP_RAND_CTX_get0_rand(EVP_RAND_CTX *ctx) | 
| 394 | 0 | { | 
| 395 | 0 |     return ctx->meth; | 
| 396 | 0 | } | 
| 397 |  |  | 
| 398 |  | static int evp_rand_get_ctx_params_locked(EVP_RAND_CTX *ctx, | 
| 399 |  |                                           OSSL_PARAM params[]) | 
| 400 | 395k | { | 
| 401 | 395k |     return ctx->meth->get_ctx_params(ctx->algctx, params); | 
| 402 | 395k | } | 
| 403 |  |  | 
| 404 |  | int EVP_RAND_CTX_get_params(EVP_RAND_CTX *ctx, OSSL_PARAM params[]) | 
| 405 | 0 | { | 
| 406 | 0 |     int res; | 
| 407 |  | 
 | 
| 408 | 0 |     if (!evp_rand_lock(ctx)) | 
| 409 | 0 |         return 0; | 
| 410 | 0 |     res = evp_rand_get_ctx_params_locked(ctx, params); | 
| 411 | 0 |     evp_rand_unlock(ctx); | 
| 412 | 0 |     return res; | 
| 413 | 0 | } | 
| 414 |  |  | 
| 415 |  | static int evp_rand_set_ctx_params_locked(EVP_RAND_CTX *ctx, | 
| 416 |  |                                           const OSSL_PARAM params[]) | 
| 417 | 0 | { | 
| 418 | 0 |     if (ctx->meth->set_ctx_params != NULL) | 
| 419 | 0 |         return ctx->meth->set_ctx_params(ctx->algctx, params); | 
| 420 | 0 |     return 1; | 
| 421 | 0 | } | 
| 422 |  |  | 
| 423 |  | int EVP_RAND_CTX_set_params(EVP_RAND_CTX *ctx, const OSSL_PARAM params[]) | 
| 424 | 0 | { | 
| 425 | 0 |     int res; | 
| 426 |  | 
 | 
| 427 | 0 |     if (!evp_rand_lock(ctx)) | 
| 428 | 0 |         return 0; | 
| 429 | 0 |     res = evp_rand_set_ctx_params_locked(ctx, params); | 
| 430 | 0 |     evp_rand_unlock(ctx); | 
| 431 | 0 |     return res; | 
| 432 | 0 | } | 
| 433 |  |  | 
| 434 |  | const OSSL_PARAM *EVP_RAND_gettable_params(const EVP_RAND *rand) | 
| 435 | 0 | { | 
| 436 | 0 |     if (rand->gettable_params == NULL) | 
| 437 | 0 |         return NULL; | 
| 438 | 0 |     return rand->gettable_params(ossl_provider_ctx(EVP_RAND_get0_provider(rand))); | 
| 439 | 0 | } | 
| 440 |  |  | 
| 441 |  | const OSSL_PARAM *EVP_RAND_gettable_ctx_params(const EVP_RAND *rand) | 
| 442 | 0 | { | 
| 443 | 0 |     void *provctx; | 
| 444 |  | 
 | 
| 445 | 0 |     if (rand->gettable_ctx_params == NULL) | 
| 446 | 0 |         return NULL; | 
| 447 | 0 |     provctx = ossl_provider_ctx(EVP_RAND_get0_provider(rand)); | 
| 448 | 0 |     return rand->gettable_ctx_params(NULL, provctx); | 
| 449 | 0 | } | 
| 450 |  |  | 
| 451 |  | const OSSL_PARAM *EVP_RAND_settable_ctx_params(const EVP_RAND *rand) | 
| 452 | 0 | { | 
| 453 | 0 |     void *provctx; | 
| 454 |  | 
 | 
| 455 | 0 |     if (rand->settable_ctx_params == NULL) | 
| 456 | 0 |         return NULL; | 
| 457 | 0 |     provctx = ossl_provider_ctx(EVP_RAND_get0_provider(rand)); | 
| 458 | 0 |     return rand->settable_ctx_params(NULL, provctx); | 
| 459 | 0 | } | 
| 460 |  |  | 
| 461 |  | const OSSL_PARAM *EVP_RAND_CTX_gettable_params(EVP_RAND_CTX *ctx) | 
| 462 | 0 | { | 
| 463 | 0 |     void *provctx; | 
| 464 |  | 
 | 
| 465 | 0 |     if (ctx->meth->gettable_ctx_params == NULL) | 
| 466 | 0 |         return NULL; | 
| 467 | 0 |     provctx = ossl_provider_ctx(EVP_RAND_get0_provider(ctx->meth)); | 
| 468 | 0 |     return ctx->meth->gettable_ctx_params(ctx->algctx, provctx); | 
| 469 | 0 | } | 
| 470 |  |  | 
| 471 |  | const OSSL_PARAM *EVP_RAND_CTX_settable_params(EVP_RAND_CTX *ctx) | 
| 472 | 17 | { | 
| 473 | 17 |     void *provctx; | 
| 474 |  |  | 
| 475 | 17 |     if (ctx->meth->settable_ctx_params == NULL) | 
| 476 | 15 |         return NULL; | 
| 477 | 2 |     provctx = ossl_provider_ctx(EVP_RAND_get0_provider(ctx->meth)); | 
| 478 | 2 |     return ctx->meth->settable_ctx_params(ctx->algctx, provctx); | 
| 479 | 17 | } | 
| 480 |  |  | 
| 481 |  | void EVP_RAND_do_all_provided(OSSL_LIB_CTX *libctx, | 
| 482 |  |                               void (*fn)(EVP_RAND *rand, void *arg), | 
| 483 |  |                               void *arg) | 
| 484 | 0 | { | 
| 485 | 0 |     evp_generic_do_all(libctx, OSSL_OP_RAND, | 
| 486 | 0 |                        (void (*)(void *, void *))fn, arg, | 
| 487 | 0 |                        evp_rand_from_algorithm, evp_rand_up_ref, | 
| 488 | 0 |                        evp_rand_free); | 
| 489 | 0 | } | 
| 490 |  |  | 
| 491 |  | int EVP_RAND_names_do_all(const EVP_RAND *rand, | 
| 492 |  |                           void (*fn)(const char *name, void *data), | 
| 493 |  |                           void *data) | 
| 494 | 0 | { | 
| 495 | 0 |     if (rand->prov != NULL) | 
| 496 | 0 |         return evp_names_do_all(rand->prov, rand->name_id, fn, data); | 
| 497 |  |  | 
| 498 | 0 |     return 1; | 
| 499 | 0 | } | 
| 500 |  |  | 
| 501 |  | static int evp_rand_instantiate_locked | 
| 502 |  |     (EVP_RAND_CTX *ctx, unsigned int strength, int prediction_resistance, | 
| 503 |  |      const unsigned char *pstr, size_t pstr_len, const OSSL_PARAM params[]) | 
| 504 | 45 | { | 
| 505 | 45 |     return ctx->meth->instantiate(ctx->algctx, strength, prediction_resistance, | 
| 506 | 45 |                                   pstr, pstr_len, params); | 
| 507 | 45 | } | 
| 508 |  |  | 
| 509 |  | int EVP_RAND_instantiate(EVP_RAND_CTX *ctx, unsigned int strength, | 
| 510 |  |                          int prediction_resistance, | 
| 511 |  |                          const unsigned char *pstr, size_t pstr_len, | 
| 512 |  |                          const OSSL_PARAM params[]) | 
| 513 | 45 | { | 
| 514 | 45 |     int res; | 
| 515 |  |  | 
| 516 | 45 |     if (!evp_rand_lock(ctx)) | 
| 517 | 0 |         return 0; | 
| 518 | 45 |     res = evp_rand_instantiate_locked(ctx, strength, prediction_resistance, | 
| 519 | 45 |                                       pstr, pstr_len, params); | 
| 520 | 45 |     evp_rand_unlock(ctx); | 
| 521 | 45 |     return res; | 
| 522 | 45 | } | 
| 523 |  |  | 
| 524 |  | static int evp_rand_uninstantiate_locked(EVP_RAND_CTX *ctx) | 
| 525 | 0 | { | 
| 526 | 0 |     return ctx->meth->uninstantiate(ctx->algctx); | 
| 527 | 0 | } | 
| 528 |  |  | 
| 529 |  | int EVP_RAND_uninstantiate(EVP_RAND_CTX *ctx) | 
| 530 | 0 | { | 
| 531 | 0 |     int res; | 
| 532 |  | 
 | 
| 533 | 0 |     if (!evp_rand_lock(ctx)) | 
| 534 | 0 |         return 0; | 
| 535 | 0 |     res = evp_rand_uninstantiate_locked(ctx); | 
| 536 | 0 |     evp_rand_unlock(ctx); | 
| 537 | 0 |     return res; | 
| 538 | 0 | } | 
| 539 |  |  | 
| 540 |  | static int evp_rand_generate_locked(EVP_RAND_CTX *ctx, unsigned char *out, | 
| 541 |  |                                     size_t outlen, unsigned int strength, | 
| 542 |  |                                     int prediction_resistance, | 
| 543 |  |                                     const unsigned char *addin, | 
| 544 |  |                                     size_t addin_len) | 
| 545 | 395k | { | 
| 546 | 395k |     size_t chunk, max_request = 0; | 
| 547 | 395k |     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | 
| 548 |  |  | 
| 549 | 395k |     params[0] = OSSL_PARAM_construct_size_t(OSSL_RAND_PARAM_MAX_REQUEST, | 
| 550 | 395k |                                             &max_request); | 
| 551 | 395k |     if (!evp_rand_get_ctx_params_locked(ctx, params) | 
| 552 | 395k |             || max_request == 0) { | 
| 553 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_UNABLE_TO_GET_MAXIMUM_REQUEST_SIZE); | 
| 554 | 0 |         return 0; | 
| 555 | 0 |     } | 
| 556 | 790k |     for (; outlen > 0; outlen -= chunk, out += chunk) { | 
| 557 | 395k |         chunk = outlen > max_request ? max_request : outlen; | 
| 558 | 395k |         if (!ctx->meth->generate(ctx->algctx, out, chunk, strength, | 
| 559 | 395k |                                  prediction_resistance, addin, addin_len)) { | 
| 560 | 0 |             ERR_raise(ERR_LIB_EVP, EVP_R_GENERATE_ERROR); | 
| 561 | 0 |             return 0; | 
| 562 | 0 |         } | 
| 563 |  |         /* | 
| 564 |  |          * Prediction resistance is only relevant the first time around, | 
| 565 |  |          * subsequently, the DRBG has already been properly reseeded. | 
| 566 |  |          */ | 
| 567 | 395k |         prediction_resistance = 0; | 
| 568 | 395k |     } | 
| 569 | 395k |     return 1; | 
| 570 | 395k | } | 
| 571 |  |  | 
| 572 |  | int EVP_RAND_generate(EVP_RAND_CTX *ctx, unsigned char *out, size_t outlen, | 
| 573 |  |                       unsigned int strength, int prediction_resistance, | 
| 574 |  |                       const unsigned char *addin, size_t addin_len) | 
| 575 | 395k | { | 
| 576 | 395k |     int res; | 
| 577 |  |  | 
| 578 | 395k |     if (!evp_rand_lock(ctx)) | 
| 579 | 0 |         return 0; | 
| 580 | 395k |     res = evp_rand_generate_locked(ctx, out, outlen, strength, | 
| 581 | 395k |                                    prediction_resistance, addin, addin_len); | 
| 582 | 395k |     evp_rand_unlock(ctx); | 
| 583 | 395k |     return res; | 
| 584 | 395k | } | 
| 585 |  |  | 
| 586 |  | static int evp_rand_reseed_locked(EVP_RAND_CTX *ctx, int prediction_resistance, | 
| 587 |  |                                   const unsigned char *ent, size_t ent_len, | 
| 588 |  |                                   const unsigned char *addin, size_t addin_len) | 
| 589 | 0 | { | 
| 590 | 0 |     if (ctx->meth->reseed != NULL) | 
| 591 | 0 |         return ctx->meth->reseed(ctx->algctx, prediction_resistance, | 
| 592 | 0 |                                  ent, ent_len, addin, addin_len); | 
| 593 | 0 |     return 1; | 
| 594 | 0 | } | 
| 595 |  |  | 
| 596 |  | int EVP_RAND_reseed(EVP_RAND_CTX *ctx, int prediction_resistance, | 
| 597 |  |                     const unsigned char *ent, size_t ent_len, | 
| 598 |  |                     const unsigned char *addin, size_t addin_len) | 
| 599 | 0 | { | 
| 600 | 0 |     int res; | 
| 601 |  | 
 | 
| 602 | 0 |     if (!evp_rand_lock(ctx)) | 
| 603 | 0 |         return 0; | 
| 604 | 0 |     res = evp_rand_reseed_locked(ctx, prediction_resistance, | 
| 605 | 0 |                                  ent, ent_len, addin, addin_len); | 
| 606 | 0 |     evp_rand_unlock(ctx); | 
| 607 | 0 |     return res; | 
| 608 | 0 | } | 
| 609 |  |  | 
| 610 |  | static unsigned int evp_rand_strength_locked(EVP_RAND_CTX *ctx) | 
| 611 | 0 | { | 
| 612 | 0 |     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | 
| 613 | 0 |     unsigned int strength = 0; | 
| 614 |  | 
 | 
| 615 | 0 |     params[0] = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength); | 
| 616 | 0 |     if (!evp_rand_get_ctx_params_locked(ctx, params)) | 
| 617 | 0 |         return 0; | 
| 618 | 0 |     return strength; | 
| 619 | 0 | } | 
| 620 |  |  | 
| 621 |  | unsigned int EVP_RAND_get_strength(EVP_RAND_CTX *ctx) | 
| 622 | 0 | { | 
| 623 | 0 |     unsigned int res; | 
| 624 |  | 
 | 
| 625 | 0 |     if (!evp_rand_lock(ctx)) | 
| 626 | 0 |         return 0; | 
| 627 | 0 |     res = evp_rand_strength_locked(ctx); | 
| 628 | 0 |     evp_rand_unlock(ctx); | 
| 629 | 0 |     return res; | 
| 630 | 0 | } | 
| 631 |  |  | 
| 632 |  | static int evp_rand_nonce_locked(EVP_RAND_CTX *ctx, unsigned char *out, | 
| 633 |  |                                  size_t outlen) | 
| 634 | 0 | { | 
| 635 | 0 |     unsigned int str = evp_rand_strength_locked(ctx); | 
| 636 |  | 
 | 
| 637 | 0 |     if (ctx->meth->nonce == NULL) | 
| 638 | 0 |         return 0; | 
| 639 | 0 |     if (ctx->meth->nonce(ctx->algctx, out, str, outlen, outlen)) | 
| 640 | 0 |         return 1; | 
| 641 | 0 |     return evp_rand_generate_locked(ctx, out, outlen, str, 0, NULL, 0); | 
| 642 | 0 | } | 
| 643 |  |  | 
| 644 |  | int EVP_RAND_nonce(EVP_RAND_CTX *ctx, unsigned char *out, size_t outlen) | 
| 645 | 0 | { | 
| 646 | 0 |     int res; | 
| 647 |  | 
 | 
| 648 | 0 |     if (!evp_rand_lock(ctx)) | 
| 649 | 0 |         return 0; | 
| 650 | 0 |     res = evp_rand_nonce_locked(ctx, out, outlen); | 
| 651 | 0 |     evp_rand_unlock(ctx); | 
| 652 | 0 |     return res; | 
| 653 | 0 | } | 
| 654 |  |  | 
| 655 |  | int EVP_RAND_get_state(EVP_RAND_CTX *ctx) | 
| 656 | 0 | { | 
| 657 | 0 |     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | 
| 658 | 0 |     int state; | 
| 659 |  | 
 | 
| 660 | 0 |     params[0] = OSSL_PARAM_construct_int(OSSL_RAND_PARAM_STATE, &state); | 
| 661 | 0 |     if (!EVP_RAND_CTX_get_params(ctx, params)) | 
| 662 | 0 |         state = EVP_RAND_STATE_ERROR; | 
| 663 | 0 |     return state; | 
| 664 | 0 | } | 
| 665 |  |  | 
| 666 |  | static int evp_rand_verify_zeroization_locked(EVP_RAND_CTX *ctx) | 
| 667 | 0 | { | 
| 668 | 0 |     if (ctx->meth->verify_zeroization != NULL) | 
| 669 | 0 |         return ctx->meth->verify_zeroization(ctx->algctx); | 
| 670 | 0 |     return 0; | 
| 671 | 0 | } | 
| 672 |  |  | 
| 673 |  | int EVP_RAND_verify_zeroization(EVP_RAND_CTX *ctx) | 
| 674 | 0 | { | 
| 675 | 0 |     int res; | 
| 676 |  | 
 | 
| 677 | 0 |     if (!evp_rand_lock(ctx)) | 
| 678 | 0 |         return 0; | 
| 679 | 0 |     res = evp_rand_verify_zeroization_locked(ctx); | 
| 680 | 0 |     evp_rand_unlock(ctx); | 
| 681 | 0 |     return res; | 
| 682 | 0 | } |