rand_lib.c 23 KB

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