rand_lib.c 24 KB

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