evp_rand.c 20 KB

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