evp_rand.c 20 KB

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