rand_lib.c 24 KB

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