rand_lib.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. /*
  2. * Copyright 1995-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. /* We need to use some engine deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include <openssl/err.h>
  12. #include <openssl/opensslconf.h>
  13. #include <openssl/core_names.h>
  14. #include "internal/cryptlib.h"
  15. #include "internal/thread_once.h"
  16. #include "crypto/rand.h"
  17. #include "crypto/cryptlib.h"
  18. #include "rand_local.h"
  19. #include "crypto/context.h"
  20. #ifndef FIPS_MODULE
  21. # include <stdio.h>
  22. # include <time.h>
  23. # include <limits.h>
  24. # include <openssl/conf.h>
  25. # include <openssl/trace.h>
  26. # include <openssl/engine.h>
  27. # include "crypto/rand_pool.h"
  28. # include "prov/seeding.h"
  29. # include "internal/e_os.h"
  30. # include "internal/property.h"
  31. # ifndef OPENSSL_NO_ENGINE
  32. /* non-NULL if default_RAND_meth is ENGINE-provided */
  33. static ENGINE *funct_ref;
  34. static CRYPTO_RWLOCK *rand_engine_lock;
  35. # endif
  36. # ifndef OPENSSL_NO_DEPRECATED_3_0
  37. static CRYPTO_RWLOCK *rand_meth_lock;
  38. static const RAND_METHOD *default_RAND_meth;
  39. # endif
  40. static CRYPTO_ONCE rand_init = CRYPTO_ONCE_STATIC_INIT;
  41. static int rand_inited = 0;
  42. DEFINE_RUN_ONCE_STATIC(do_rand_init)
  43. {
  44. # ifndef OPENSSL_NO_ENGINE
  45. rand_engine_lock = CRYPTO_THREAD_lock_new();
  46. if (rand_engine_lock == NULL)
  47. return 0;
  48. # endif
  49. # ifndef OPENSSL_NO_DEPRECATED_3_0
  50. rand_meth_lock = CRYPTO_THREAD_lock_new();
  51. if (rand_meth_lock == NULL)
  52. goto err;
  53. # endif
  54. if (!ossl_rand_pool_init())
  55. goto err;
  56. rand_inited = 1;
  57. return 1;
  58. err:
  59. # ifndef OPENSSL_NO_DEPRECATED_3_0
  60. CRYPTO_THREAD_lock_free(rand_meth_lock);
  61. rand_meth_lock = NULL;
  62. # endif
  63. # ifndef OPENSSL_NO_ENGINE
  64. CRYPTO_THREAD_lock_free(rand_engine_lock);
  65. rand_engine_lock = NULL;
  66. # endif
  67. return 0;
  68. }
  69. void ossl_rand_cleanup_int(void)
  70. {
  71. # ifndef OPENSSL_NO_DEPRECATED_3_0
  72. const RAND_METHOD *meth = default_RAND_meth;
  73. if (!rand_inited)
  74. return;
  75. if (meth != NULL && meth->cleanup != NULL)
  76. meth->cleanup();
  77. RAND_set_rand_method(NULL);
  78. # endif
  79. ossl_rand_pool_cleanup();
  80. # ifndef OPENSSL_NO_ENGINE
  81. CRYPTO_THREAD_lock_free(rand_engine_lock);
  82. rand_engine_lock = NULL;
  83. # endif
  84. # ifndef OPENSSL_NO_DEPRECATED_3_0
  85. CRYPTO_THREAD_lock_free(rand_meth_lock);
  86. rand_meth_lock = NULL;
  87. # endif
  88. ossl_release_default_drbg_ctx();
  89. rand_inited = 0;
  90. }
  91. /*
  92. * RAND_close_seed_files() ensures that any seed file descriptors are
  93. * closed after use. This only applies to libcrypto/default provider,
  94. * it does not apply to other providers.
  95. */
  96. void RAND_keep_random_devices_open(int keep)
  97. {
  98. if (RUN_ONCE(&rand_init, do_rand_init))
  99. ossl_rand_pool_keep_random_devices_open(keep);
  100. }
  101. /*
  102. * RAND_poll() reseeds the default RNG using random input
  103. *
  104. * The random input is obtained from polling various entropy
  105. * sources which depend on the operating system and are
  106. * configurable via the --with-rand-seed configure option.
  107. */
  108. int RAND_poll(void)
  109. {
  110. static const char salt[] = "polling";
  111. # ifndef OPENSSL_NO_DEPRECATED_3_0
  112. const RAND_METHOD *meth = RAND_get_rand_method();
  113. int ret = meth == RAND_OpenSSL();
  114. if (meth == NULL)
  115. return 0;
  116. if (!ret) {
  117. /* fill random pool and seed the current legacy RNG */
  118. RAND_POOL *pool = ossl_rand_pool_new(RAND_DRBG_STRENGTH, 1,
  119. (RAND_DRBG_STRENGTH + 7) / 8,
  120. RAND_POOL_MAX_LENGTH);
  121. if (pool == NULL)
  122. return 0;
  123. if (ossl_pool_acquire_entropy(pool) == 0)
  124. goto err;
  125. if (meth->add == NULL
  126. || meth->add(ossl_rand_pool_buffer(pool),
  127. ossl_rand_pool_length(pool),
  128. (ossl_rand_pool_entropy(pool) / 8.0)) == 0)
  129. goto err;
  130. ret = 1;
  131. err:
  132. ossl_rand_pool_free(pool);
  133. return ret;
  134. }
  135. # endif
  136. RAND_seed(salt, sizeof(salt));
  137. return 1;
  138. }
  139. # ifndef OPENSSL_NO_DEPRECATED_3_0
  140. static int rand_set_rand_method_internal(const RAND_METHOD *meth,
  141. ossl_unused ENGINE *e)
  142. {
  143. if (!RUN_ONCE(&rand_init, do_rand_init))
  144. return 0;
  145. if (!CRYPTO_THREAD_write_lock(rand_meth_lock))
  146. return 0;
  147. # ifndef OPENSSL_NO_ENGINE
  148. ENGINE_finish(funct_ref);
  149. funct_ref = e;
  150. # endif
  151. default_RAND_meth = meth;
  152. CRYPTO_THREAD_unlock(rand_meth_lock);
  153. return 1;
  154. }
  155. int RAND_set_rand_method(const RAND_METHOD *meth)
  156. {
  157. return rand_set_rand_method_internal(meth, NULL);
  158. }
  159. const RAND_METHOD *RAND_get_rand_method(void)
  160. {
  161. const RAND_METHOD *tmp_meth = NULL;
  162. if (!RUN_ONCE(&rand_init, do_rand_init))
  163. return NULL;
  164. if (!CRYPTO_THREAD_read_lock(rand_meth_lock))
  165. return NULL;
  166. tmp_meth = default_RAND_meth;
  167. CRYPTO_THREAD_unlock(rand_meth_lock);
  168. if (tmp_meth != NULL)
  169. return tmp_meth;
  170. if (!CRYPTO_THREAD_write_lock(rand_meth_lock))
  171. return NULL;
  172. if (default_RAND_meth == NULL) {
  173. # ifndef OPENSSL_NO_ENGINE
  174. ENGINE *e;
  175. /* If we have an engine that can do RAND, use it. */
  176. if ((e = ENGINE_get_default_RAND()) != NULL
  177. && (tmp_meth = ENGINE_get_RAND(e)) != NULL) {
  178. funct_ref = e;
  179. default_RAND_meth = tmp_meth;
  180. } else {
  181. ENGINE_finish(e);
  182. default_RAND_meth = &ossl_rand_meth;
  183. }
  184. # else
  185. default_RAND_meth = &ossl_rand_meth;
  186. # endif
  187. }
  188. tmp_meth = default_RAND_meth;
  189. CRYPTO_THREAD_unlock(rand_meth_lock);
  190. return tmp_meth;
  191. }
  192. # if !defined(OPENSSL_NO_ENGINE)
  193. int RAND_set_rand_engine(ENGINE *engine)
  194. {
  195. const RAND_METHOD *tmp_meth = NULL;
  196. if (!RUN_ONCE(&rand_init, do_rand_init))
  197. return 0;
  198. if (engine != NULL) {
  199. if (!ENGINE_init(engine))
  200. return 0;
  201. tmp_meth = ENGINE_get_RAND(engine);
  202. if (tmp_meth == NULL) {
  203. ENGINE_finish(engine);
  204. return 0;
  205. }
  206. }
  207. if (!CRYPTO_THREAD_write_lock(rand_engine_lock)) {
  208. ENGINE_finish(engine);
  209. return 0;
  210. }
  211. /* This function releases any prior ENGINE so call it first */
  212. rand_set_rand_method_internal(tmp_meth, engine);
  213. CRYPTO_THREAD_unlock(rand_engine_lock);
  214. return 1;
  215. }
  216. # endif
  217. # endif /* OPENSSL_NO_DEPRECATED_3_0 */
  218. void RAND_seed(const void *buf, int num)
  219. {
  220. EVP_RAND_CTX *drbg;
  221. # ifndef OPENSSL_NO_DEPRECATED_3_0
  222. const RAND_METHOD *meth = RAND_get_rand_method();
  223. if (meth != NULL && meth->seed != NULL) {
  224. meth->seed(buf, num);
  225. return;
  226. }
  227. # endif
  228. drbg = RAND_get0_primary(NULL);
  229. if (drbg != NULL && num > 0)
  230. EVP_RAND_reseed(drbg, 0, NULL, 0, buf, num);
  231. }
  232. void RAND_add(const void *buf, int num, double randomness)
  233. {
  234. EVP_RAND_CTX *drbg;
  235. # ifndef OPENSSL_NO_DEPRECATED_3_0
  236. const RAND_METHOD *meth = RAND_get_rand_method();
  237. if (meth != NULL && meth->add != NULL) {
  238. meth->add(buf, num, randomness);
  239. return;
  240. }
  241. # endif
  242. drbg = RAND_get0_primary(NULL);
  243. if (drbg != NULL && num > 0)
  244. # ifdef OPENSSL_RAND_SEED_NONE
  245. /* Without an entropy source, we have to rely on the user */
  246. EVP_RAND_reseed(drbg, 0, buf, num, NULL, 0);
  247. # else
  248. /* With an entropy source, we downgrade this to additional input */
  249. EVP_RAND_reseed(drbg, 0, NULL, 0, buf, num);
  250. # endif
  251. }
  252. # if !defined(OPENSSL_NO_DEPRECATED_1_1_0)
  253. int RAND_pseudo_bytes(unsigned char *buf, int num)
  254. {
  255. const RAND_METHOD *meth = RAND_get_rand_method();
  256. if (meth != NULL && meth->pseudorand != NULL)
  257. return meth->pseudorand(buf, num);
  258. ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED);
  259. return -1;
  260. }
  261. # endif
  262. int RAND_status(void)
  263. {
  264. EVP_RAND_CTX *rand;
  265. # ifndef OPENSSL_NO_DEPRECATED_3_0
  266. const RAND_METHOD *meth = RAND_get_rand_method();
  267. if (meth != NULL && meth != RAND_OpenSSL())
  268. return meth->status != NULL ? meth->status() : 0;
  269. # endif
  270. if ((rand = RAND_get0_primary(NULL)) == NULL)
  271. return 0;
  272. return EVP_RAND_get_state(rand) == EVP_RAND_STATE_READY;
  273. }
  274. # else /* !FIPS_MODULE */
  275. # ifndef OPENSSL_NO_DEPRECATED_3_0
  276. const RAND_METHOD *RAND_get_rand_method(void)
  277. {
  278. return NULL;
  279. }
  280. # endif
  281. #endif /* !FIPS_MODULE */
  282. /*
  283. * This function is not part of RAND_METHOD, so if we're not using
  284. * the default method, then just call RAND_bytes(). Otherwise make
  285. * sure we're instantiated and use the private DRBG.
  286. */
  287. int RAND_priv_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num,
  288. unsigned int strength)
  289. {
  290. EVP_RAND_CTX *rand;
  291. #if !defined(OPENSSL_NO_DEPRECATED_3_0) && !defined(FIPS_MODULE)
  292. const RAND_METHOD *meth = RAND_get_rand_method();
  293. if (meth != NULL && meth != RAND_OpenSSL()) {
  294. if (meth->bytes != NULL)
  295. return meth->bytes(buf, num);
  296. ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED);
  297. return -1;
  298. }
  299. #endif
  300. rand = RAND_get0_private(ctx);
  301. if (rand != NULL)
  302. return EVP_RAND_generate(rand, buf, num, strength, 0, NULL, 0);
  303. return 0;
  304. }
  305. int RAND_priv_bytes(unsigned char *buf, int num)
  306. {
  307. if (num < 0)
  308. return 0;
  309. return RAND_priv_bytes_ex(NULL, buf, (size_t)num, 0);
  310. }
  311. int RAND_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num,
  312. unsigned int strength)
  313. {
  314. EVP_RAND_CTX *rand;
  315. #if !defined(OPENSSL_NO_DEPRECATED_3_0) && !defined(FIPS_MODULE)
  316. const RAND_METHOD *meth = RAND_get_rand_method();
  317. if (meth != NULL && meth != RAND_OpenSSL()) {
  318. if (meth->bytes != NULL)
  319. return meth->bytes(buf, num);
  320. ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED);
  321. return -1;
  322. }
  323. #endif
  324. rand = RAND_get0_public(ctx);
  325. if (rand != NULL)
  326. return EVP_RAND_generate(rand, buf, num, strength, 0, NULL, 0);
  327. return 0;
  328. }
  329. int RAND_bytes(unsigned char *buf, int num)
  330. {
  331. if (num < 0)
  332. return 0;
  333. return RAND_bytes_ex(NULL, buf, (size_t)num, 0);
  334. }
  335. typedef struct rand_global_st {
  336. /*
  337. * The three shared DRBG instances
  338. *
  339. * There are three shared DRBG instances: <primary>, <public>, and
  340. * <private>. The <public> and <private> DRBGs are secondary ones.
  341. * These are used for non-secret (e.g. nonces) and secret
  342. * (e.g. private keys) data respectively.
  343. */
  344. CRYPTO_RWLOCK *lock;
  345. EVP_RAND_CTX *seed;
  346. /*
  347. * The <primary> DRBG
  348. *
  349. * Not used directly by the application, only for reseeding the two other
  350. * DRBGs. It reseeds itself by pulling either randomness from os entropy
  351. * sources or by consuming randomness which was added by RAND_add().
  352. *
  353. * The <primary> DRBG is a global instance which is accessed concurrently by
  354. * all threads. The necessary locking is managed automatically by its child
  355. * DRBG instances during reseeding.
  356. */
  357. EVP_RAND_CTX *primary;
  358. /*
  359. * The <public> DRBG
  360. *
  361. * Used by default for generating random bytes using RAND_bytes().
  362. *
  363. * The <public> secondary DRBG is thread-local, i.e., there is one instance
  364. * per thread.
  365. */
  366. CRYPTO_THREAD_LOCAL public;
  367. /*
  368. * The <private> DRBG
  369. *
  370. * Used by default for generating private keys using RAND_priv_bytes()
  371. *
  372. * The <private> secondary DRBG is thread-local, i.e., there is one
  373. * instance per thread.
  374. */
  375. CRYPTO_THREAD_LOCAL private;
  376. /* Which RNG is being used by default and it's configuration settings */
  377. char *rng_name;
  378. char *rng_cipher;
  379. char *rng_digest;
  380. char *rng_propq;
  381. /* Allow the randomness source to be changed */
  382. char *seed_name;
  383. char *seed_propq;
  384. } RAND_GLOBAL;
  385. /*
  386. * Initialize the OSSL_LIB_CTX global DRBGs on first use.
  387. * Returns the allocated global data on success or NULL on failure.
  388. */
  389. void *ossl_rand_ctx_new(OSSL_LIB_CTX *libctx)
  390. {
  391. RAND_GLOBAL *dgbl = OPENSSL_zalloc(sizeof(*dgbl));
  392. if (dgbl == NULL)
  393. return NULL;
  394. #ifndef FIPS_MODULE
  395. /*
  396. * We need to ensure that base libcrypto thread handling has been
  397. * initialised.
  398. */
  399. OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL);
  400. #endif
  401. dgbl->lock = CRYPTO_THREAD_lock_new();
  402. if (dgbl->lock == NULL)
  403. goto err1;
  404. if (!CRYPTO_THREAD_init_local(&dgbl->private, NULL))
  405. goto err1;
  406. if (!CRYPTO_THREAD_init_local(&dgbl->public, NULL))
  407. goto err2;
  408. return dgbl;
  409. err2:
  410. CRYPTO_THREAD_cleanup_local(&dgbl->private);
  411. err1:
  412. CRYPTO_THREAD_lock_free(dgbl->lock);
  413. OPENSSL_free(dgbl);
  414. return NULL;
  415. }
  416. void ossl_rand_ctx_free(void *vdgbl)
  417. {
  418. RAND_GLOBAL *dgbl = vdgbl;
  419. if (dgbl == NULL)
  420. return;
  421. CRYPTO_THREAD_lock_free(dgbl->lock);
  422. CRYPTO_THREAD_cleanup_local(&dgbl->private);
  423. CRYPTO_THREAD_cleanup_local(&dgbl->public);
  424. EVP_RAND_CTX_free(dgbl->primary);
  425. EVP_RAND_CTX_free(dgbl->seed);
  426. OPENSSL_free(dgbl->rng_name);
  427. OPENSSL_free(dgbl->rng_cipher);
  428. OPENSSL_free(dgbl->rng_digest);
  429. OPENSSL_free(dgbl->rng_propq);
  430. OPENSSL_free(dgbl->seed_name);
  431. OPENSSL_free(dgbl->seed_propq);
  432. OPENSSL_free(dgbl);
  433. }
  434. static RAND_GLOBAL *rand_get_global(OSSL_LIB_CTX *libctx)
  435. {
  436. return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DRBG_INDEX);
  437. }
  438. static void rand_delete_thread_state(void *arg)
  439. {
  440. OSSL_LIB_CTX *ctx = arg;
  441. RAND_GLOBAL *dgbl = rand_get_global(ctx);
  442. EVP_RAND_CTX *rand;
  443. if (dgbl == NULL)
  444. return;
  445. rand = CRYPTO_THREAD_get_local(&dgbl->public);
  446. CRYPTO_THREAD_set_local(&dgbl->public, NULL);
  447. EVP_RAND_CTX_free(rand);
  448. rand = CRYPTO_THREAD_get_local(&dgbl->private);
  449. CRYPTO_THREAD_set_local(&dgbl->private, NULL);
  450. EVP_RAND_CTX_free(rand);
  451. }
  452. #ifndef FIPS_MODULE
  453. static EVP_RAND_CTX *rand_new_seed(OSSL_LIB_CTX *libctx)
  454. {
  455. EVP_RAND *rand;
  456. RAND_GLOBAL *dgbl = rand_get_global(libctx);
  457. EVP_RAND_CTX *ctx = NULL;
  458. const char *propq;
  459. char *name, *props = NULL;
  460. size_t props_len;
  461. OSSL_PROPERTY_LIST *pl1, *pl2, *pl3 = NULL;
  462. if (dgbl == NULL)
  463. return NULL;
  464. propq = dgbl->seed_propq;
  465. if (dgbl->seed_name != NULL) {
  466. name = dgbl->seed_name;
  467. } else {
  468. /*
  469. * Default to our internal seed source. This isn't part of the FIPS
  470. * provider so we need to override any FIPS properties.
  471. */
  472. if (propq == NULL || *propq == '\0') {
  473. propq = "-fips";
  474. } else {
  475. pl1 = ossl_parse_query(libctx, propq, 1);
  476. if (pl1 == NULL) {
  477. ERR_raise(ERR_LIB_RAND, RAND_R_INVALID_PROPERTY_QUERY);
  478. return NULL;
  479. }
  480. pl2 = ossl_parse_query(libctx, "-fips", 1);
  481. if (pl2 == NULL) {
  482. ossl_property_free(pl1);
  483. ERR_raise(ERR_LIB_RAND, ERR_R_INTERNAL_ERROR);
  484. return NULL;
  485. }
  486. pl3 = ossl_property_merge(pl2, pl1);
  487. ossl_property_free(pl1);
  488. ossl_property_free(pl2);
  489. if (pl3 == NULL) {
  490. ERR_raise(ERR_LIB_RAND, ERR_R_INTERNAL_ERROR);
  491. return NULL;
  492. }
  493. props_len = ossl_property_list_to_string(libctx, pl3, NULL, 0);
  494. if (props_len == 0) {
  495. /* Shouldn't happen since we added a query element */
  496. ERR_raise(ERR_LIB_RAND, ERR_R_INTERNAL_ERROR);
  497. goto err;
  498. } else {
  499. props = OPENSSL_malloc(props_len);
  500. if (props == NULL) {
  501. ERR_raise(ERR_LIB_RAND, ERR_R_MALLOC_FAILURE);
  502. goto err;
  503. }
  504. if (ossl_property_list_to_string(libctx, pl3,
  505. props, props_len) == 0) {
  506. ERR_raise(ERR_LIB_RAND, ERR_R_INTERNAL_ERROR);
  507. goto err;
  508. }
  509. ossl_property_free(pl3);
  510. pl3 = NULL;
  511. propq = props;
  512. }
  513. }
  514. name = "SEED-SRC";
  515. }
  516. rand = EVP_RAND_fetch(libctx, name, propq);
  517. if (rand == NULL) {
  518. ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG);
  519. goto err;
  520. }
  521. ctx = EVP_RAND_CTX_new(rand, NULL);
  522. EVP_RAND_free(rand);
  523. if (ctx == NULL) {
  524. ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG);
  525. goto err;
  526. }
  527. if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, NULL)) {
  528. ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG);
  529. goto err;
  530. }
  531. OPENSSL_free(props);
  532. return ctx;
  533. err:
  534. EVP_RAND_CTX_free(ctx);
  535. ossl_property_free(pl3);
  536. OPENSSL_free(props);
  537. return NULL;
  538. }
  539. EVP_RAND_CTX *ossl_rand_get0_seed_noncreating(OSSL_LIB_CTX *ctx)
  540. {
  541. RAND_GLOBAL *dgbl = rand_get_global(ctx);
  542. EVP_RAND_CTX *ret;
  543. if (dgbl == NULL)
  544. return NULL;
  545. if (!CRYPTO_THREAD_read_lock(dgbl->lock))
  546. return NULL;
  547. ret = dgbl->seed;
  548. CRYPTO_THREAD_unlock(dgbl->lock);
  549. return ret;
  550. }
  551. #endif
  552. static EVP_RAND_CTX *rand_new_drbg(OSSL_LIB_CTX *libctx, EVP_RAND_CTX *parent,
  553. unsigned int reseed_interval,
  554. time_t reseed_time_interval, int use_df)
  555. {
  556. EVP_RAND *rand;
  557. RAND_GLOBAL *dgbl = rand_get_global(libctx);
  558. EVP_RAND_CTX *ctx;
  559. OSSL_PARAM params[8], *p = params;
  560. const OSSL_PARAM *settables;
  561. char *name, *cipher;
  562. if (dgbl == NULL)
  563. return NULL;
  564. name = dgbl->rng_name != NULL ? dgbl->rng_name : "CTR-DRBG";
  565. rand = EVP_RAND_fetch(libctx, name, dgbl->rng_propq);
  566. if (rand == NULL) {
  567. ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG);
  568. return NULL;
  569. }
  570. ctx = EVP_RAND_CTX_new(rand, parent);
  571. EVP_RAND_free(rand);
  572. if (ctx == NULL) {
  573. ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG);
  574. return NULL;
  575. }
  576. settables = EVP_RAND_CTX_settable_params(ctx);
  577. if (OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_CIPHER)) {
  578. cipher = dgbl->rng_cipher != NULL ? dgbl->rng_cipher : "AES-256-CTR";
  579. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
  580. cipher, 0);
  581. }
  582. if (dgbl->rng_digest != NULL
  583. && OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_DIGEST))
  584. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
  585. dgbl->rng_digest, 0);
  586. if (dgbl->rng_propq != NULL)
  587. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_PROPERTIES,
  588. dgbl->rng_propq, 0);
  589. if (OSSL_PARAM_locate_const(settables, OSSL_ALG_PARAM_MAC))
  590. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_MAC, "HMAC", 0);
  591. if (OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_USE_DF))
  592. *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &use_df);
  593. *p++ = OSSL_PARAM_construct_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS,
  594. &reseed_interval);
  595. *p++ = OSSL_PARAM_construct_time_t(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL,
  596. &reseed_time_interval);
  597. *p = OSSL_PARAM_construct_end();
  598. if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, params)) {
  599. ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG);
  600. EVP_RAND_CTX_free(ctx);
  601. return NULL;
  602. }
  603. return ctx;
  604. }
  605. /*
  606. * Get the primary random generator.
  607. * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
  608. *
  609. */
  610. EVP_RAND_CTX *RAND_get0_primary(OSSL_LIB_CTX *ctx)
  611. {
  612. RAND_GLOBAL *dgbl = rand_get_global(ctx);
  613. EVP_RAND_CTX *ret;
  614. if (dgbl == NULL)
  615. return NULL;
  616. if (!CRYPTO_THREAD_read_lock(dgbl->lock))
  617. return NULL;
  618. ret = dgbl->primary;
  619. CRYPTO_THREAD_unlock(dgbl->lock);
  620. if (ret != NULL)
  621. return ret;
  622. if (!CRYPTO_THREAD_write_lock(dgbl->lock))
  623. return NULL;
  624. ret = dgbl->primary;
  625. if (ret != NULL) {
  626. CRYPTO_THREAD_unlock(dgbl->lock);
  627. return ret;
  628. }
  629. #ifndef FIPS_MODULE
  630. if (dgbl->seed == NULL) {
  631. ERR_set_mark();
  632. dgbl->seed = rand_new_seed(ctx);
  633. ERR_pop_to_mark();
  634. }
  635. #endif
  636. ret = dgbl->primary = rand_new_drbg(ctx, dgbl->seed,
  637. PRIMARY_RESEED_INTERVAL,
  638. PRIMARY_RESEED_TIME_INTERVAL, 1);
  639. /*
  640. * The primary DRBG may be shared between multiple threads so we must
  641. * enable locking.
  642. */
  643. if (ret != NULL && !EVP_RAND_enable_locking(ret)) {
  644. ERR_raise(ERR_LIB_EVP, EVP_R_UNABLE_TO_ENABLE_LOCKING);
  645. EVP_RAND_CTX_free(ret);
  646. ret = dgbl->primary = NULL;
  647. }
  648. CRYPTO_THREAD_unlock(dgbl->lock);
  649. return ret;
  650. }
  651. /*
  652. * Get the public random generator.
  653. * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
  654. */
  655. EVP_RAND_CTX *RAND_get0_public(OSSL_LIB_CTX *ctx)
  656. {
  657. RAND_GLOBAL *dgbl = rand_get_global(ctx);
  658. EVP_RAND_CTX *rand, *primary;
  659. if (dgbl == NULL)
  660. return NULL;
  661. rand = CRYPTO_THREAD_get_local(&dgbl->public);
  662. if (rand == NULL) {
  663. primary = RAND_get0_primary(ctx);
  664. if (primary == NULL)
  665. return NULL;
  666. ctx = ossl_lib_ctx_get_concrete(ctx);
  667. /*
  668. * If the private is also NULL then this is the first time we've
  669. * used this thread.
  670. */
  671. if (CRYPTO_THREAD_get_local(&dgbl->private) == NULL
  672. && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state))
  673. return NULL;
  674. rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL,
  675. SECONDARY_RESEED_TIME_INTERVAL, 0);
  676. CRYPTO_THREAD_set_local(&dgbl->public, rand);
  677. }
  678. return rand;
  679. }
  680. /*
  681. * Get the private random generator.
  682. * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
  683. */
  684. EVP_RAND_CTX *RAND_get0_private(OSSL_LIB_CTX *ctx)
  685. {
  686. RAND_GLOBAL *dgbl = rand_get_global(ctx);
  687. EVP_RAND_CTX *rand, *primary;
  688. if (dgbl == NULL)
  689. return NULL;
  690. rand = CRYPTO_THREAD_get_local(&dgbl->private);
  691. if (rand == NULL) {
  692. primary = RAND_get0_primary(ctx);
  693. if (primary == NULL)
  694. return NULL;
  695. ctx = ossl_lib_ctx_get_concrete(ctx);
  696. /*
  697. * If the public is also NULL then this is the first time we've
  698. * used this thread.
  699. */
  700. if (CRYPTO_THREAD_get_local(&dgbl->public) == NULL
  701. && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state))
  702. return NULL;
  703. rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL,
  704. SECONDARY_RESEED_TIME_INTERVAL, 0);
  705. CRYPTO_THREAD_set_local(&dgbl->private, rand);
  706. }
  707. return rand;
  708. }
  709. #ifdef FIPS_MODULE
  710. EVP_RAND_CTX *ossl_rand_get0_private_noncreating(OSSL_LIB_CTX *ctx)
  711. {
  712. RAND_GLOBAL *dgbl = rand_get_global(ctx);
  713. if (dgbl == NULL)
  714. return NULL;
  715. return CRYPTO_THREAD_get_local(&dgbl->private);
  716. }
  717. #endif
  718. int RAND_set0_public(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand)
  719. {
  720. RAND_GLOBAL *dgbl = rand_get_global(ctx);
  721. EVP_RAND_CTX *old;
  722. int r;
  723. if (dgbl == NULL)
  724. return 0;
  725. old = CRYPTO_THREAD_get_local(&dgbl->public);
  726. if ((r = CRYPTO_THREAD_set_local(&dgbl->public, rand)) > 0)
  727. EVP_RAND_CTX_free(old);
  728. return r;
  729. }
  730. int RAND_set0_private(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand)
  731. {
  732. RAND_GLOBAL *dgbl = rand_get_global(ctx);
  733. EVP_RAND_CTX *old;
  734. int r;
  735. if (dgbl == NULL)
  736. return 0;
  737. old = CRYPTO_THREAD_get_local(&dgbl->private);
  738. if ((r = CRYPTO_THREAD_set_local(&dgbl->private, rand)) > 0)
  739. EVP_RAND_CTX_free(old);
  740. return r;
  741. }
  742. #ifndef FIPS_MODULE
  743. static int random_set_string(char **p, const char *s)
  744. {
  745. char *d = NULL;
  746. if (s != NULL) {
  747. d = OPENSSL_strdup(s);
  748. if (d == NULL)
  749. return 0;
  750. }
  751. OPENSSL_free(*p);
  752. *p = d;
  753. return 1;
  754. }
  755. /*
  756. * Load the DRBG definitions from a configuration file.
  757. */
  758. static int random_conf_init(CONF_IMODULE *md, const CONF *cnf)
  759. {
  760. STACK_OF(CONF_VALUE) *elist;
  761. CONF_VALUE *cval;
  762. RAND_GLOBAL *dgbl = rand_get_global(NCONF_get0_libctx((CONF *)cnf));
  763. int i, r = 1;
  764. OSSL_TRACE1(CONF, "Loading random module: section %s\n",
  765. CONF_imodule_get_value(md));
  766. /* Value is a section containing RANDOM configuration */
  767. elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
  768. if (elist == NULL) {
  769. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_RANDOM_SECTION_ERROR);
  770. return 0;
  771. }
  772. if (dgbl == NULL)
  773. return 0;
  774. for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
  775. cval = sk_CONF_VALUE_value(elist, i);
  776. if (OPENSSL_strcasecmp(cval->name, "random") == 0) {
  777. if (!random_set_string(&dgbl->rng_name, cval->value))
  778. return 0;
  779. } else if (OPENSSL_strcasecmp(cval->name, "cipher") == 0) {
  780. if (!random_set_string(&dgbl->rng_cipher, cval->value))
  781. return 0;
  782. } else if (OPENSSL_strcasecmp(cval->name, "digest") == 0) {
  783. if (!random_set_string(&dgbl->rng_digest, cval->value))
  784. return 0;
  785. } else if (OPENSSL_strcasecmp(cval->name, "properties") == 0) {
  786. if (!random_set_string(&dgbl->rng_propq, cval->value))
  787. return 0;
  788. } else if (OPENSSL_strcasecmp(cval->name, "seed") == 0) {
  789. if (!random_set_string(&dgbl->seed_name, cval->value))
  790. return 0;
  791. } else if (OPENSSL_strcasecmp(cval->name, "seed_properties") == 0) {
  792. if (!random_set_string(&dgbl->seed_propq, cval->value))
  793. return 0;
  794. } else {
  795. ERR_raise_data(ERR_LIB_CRYPTO,
  796. CRYPTO_R_UNKNOWN_NAME_IN_RANDOM_SECTION,
  797. "name=%s, value=%s", cval->name, cval->value);
  798. r = 0;
  799. }
  800. }
  801. return r;
  802. }
  803. static void random_conf_deinit(CONF_IMODULE *md)
  804. {
  805. OSSL_TRACE(CONF, "Cleaned up random\n");
  806. }
  807. void ossl_random_add_conf_module(void)
  808. {
  809. OSSL_TRACE(CONF, "Adding config module 'random'\n");
  810. CONF_module_add("random", random_conf_init, random_conf_deinit);
  811. }
  812. int RAND_set_DRBG_type(OSSL_LIB_CTX *ctx, const char *drbg, const char *propq,
  813. const char *cipher, const char *digest)
  814. {
  815. RAND_GLOBAL *dgbl = rand_get_global(ctx);
  816. if (dgbl == NULL)
  817. return 0;
  818. if (dgbl->primary != NULL) {
  819. ERR_raise(ERR_LIB_CRYPTO, RAND_R_ALREADY_INSTANTIATED);
  820. return 0;
  821. }
  822. return random_set_string(&dgbl->rng_name, drbg)
  823. && random_set_string(&dgbl->rng_propq, propq)
  824. && random_set_string(&dgbl->rng_cipher, cipher)
  825. && random_set_string(&dgbl->rng_digest, digest);
  826. }
  827. int RAND_set_seed_source_type(OSSL_LIB_CTX *ctx, const char *seed,
  828. const char *propq)
  829. {
  830. RAND_GLOBAL *dgbl = rand_get_global(ctx);
  831. if (dgbl == NULL)
  832. return 0;
  833. if (dgbl->seed != NULL) {
  834. ERR_raise(ERR_LIB_CRYPTO, RAND_R_ALREADY_INSTANTIATED);
  835. return 0;
  836. }
  837. return random_set_string(&dgbl->seed_name, seed)
  838. && random_set_string(&dgbl->seed_propq, propq);
  839. }
  840. #endif