evp_rand.c 22 KB

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