2
0

rand_lib.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <time.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/opensslconf.h>
  13. #include "internal/rand_int.h"
  14. #include <openssl/engine.h>
  15. #include "internal/thread_once.h"
  16. #include "rand_lcl.h"
  17. #include "e_os.h"
  18. #ifndef OPENSSL_NO_ENGINE
  19. /* non-NULL if default_RAND_meth is ENGINE-provided */
  20. static ENGINE *funct_ref;
  21. static CRYPTO_RWLOCK *rand_engine_lock;
  22. #endif
  23. static CRYPTO_RWLOCK *rand_meth_lock;
  24. static const RAND_METHOD *default_RAND_meth;
  25. static CRYPTO_ONCE rand_init = CRYPTO_ONCE_STATIC_INIT;
  26. int rand_fork_count;
  27. static CRYPTO_RWLOCK *rand_nonce_lock;
  28. static int rand_nonce_count;
  29. static int rand_cleaning_up = 0;
  30. #ifdef OPENSSL_RAND_SEED_RDTSC
  31. /*
  32. * IMPORTANT NOTE: It is not currently possible to use this code
  33. * because we are not sure about the amount of randomness it provides.
  34. * Some SP900 tests have been run, but there is internal skepticism.
  35. * So for now this code is not used.
  36. */
  37. # error "RDTSC enabled? Should not be possible!"
  38. /*
  39. * Acquire entropy from high-speed clock
  40. *
  41. * Since we get some randomness from the low-order bits of the
  42. * high-speed clock, it can help.
  43. *
  44. * Returns the total entropy count, if it exceeds the requested
  45. * entropy count. Otherwise, returns an entropy count of 0.
  46. */
  47. size_t rand_acquire_entropy_from_tsc(RAND_POOL *pool)
  48. {
  49. unsigned char c;
  50. int i;
  51. if ((OPENSSL_ia32cap_P[0] & (1 << 4)) != 0) {
  52. for (i = 0; i < TSC_READ_COUNT; i++) {
  53. c = (unsigned char)(OPENSSL_rdtsc() & 0xFF);
  54. rand_pool_add(pool, &c, 1, 4);
  55. }
  56. }
  57. return rand_pool_entropy_available(pool);
  58. }
  59. #endif
  60. #ifdef OPENSSL_RAND_SEED_RDCPU
  61. size_t OPENSSL_ia32_rdseed_bytes(unsigned char *buf, size_t len);
  62. size_t OPENSSL_ia32_rdrand_bytes(unsigned char *buf, size_t len);
  63. extern unsigned int OPENSSL_ia32cap_P[];
  64. /*
  65. * Acquire entropy using Intel-specific cpu instructions
  66. *
  67. * Uses the RDSEED instruction if available, otherwise uses
  68. * RDRAND if available.
  69. *
  70. * For the differences between RDSEED and RDRAND, and why RDSEED
  71. * is the preferred choice, see https://goo.gl/oK3KcN
  72. *
  73. * Returns the total entropy count, if it exceeds the requested
  74. * entropy count. Otherwise, returns an entropy count of 0.
  75. */
  76. size_t rand_acquire_entropy_from_cpu(RAND_POOL *pool)
  77. {
  78. size_t bytes_needed;
  79. unsigned char *buffer;
  80. bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
  81. if (bytes_needed > 0) {
  82. buffer = rand_pool_add_begin(pool, bytes_needed);
  83. if (buffer != NULL) {
  84. /* Whichever comes first, use RDSEED, RDRAND or nothing */
  85. if ((OPENSSL_ia32cap_P[2] & (1 << 18)) != 0) {
  86. if (OPENSSL_ia32_rdseed_bytes(buffer, bytes_needed)
  87. == bytes_needed) {
  88. rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);
  89. }
  90. } else if ((OPENSSL_ia32cap_P[1] & (1 << (62 - 32))) != 0) {
  91. if (OPENSSL_ia32_rdrand_bytes(buffer, bytes_needed)
  92. == bytes_needed) {
  93. rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);
  94. }
  95. } else {
  96. rand_pool_add_end(pool, 0, 0);
  97. }
  98. }
  99. }
  100. return rand_pool_entropy_available(pool);
  101. }
  102. #endif
  103. /*
  104. * Implements the get_entropy() callback (see RAND_DRBG_set_callbacks())
  105. *
  106. * If the DRBG has a parent, then the required amount of entropy input
  107. * is fetched using the parent's RAND_DRBG_generate().
  108. *
  109. * Otherwise, the entropy is polled from the system entropy sources
  110. * using rand_pool_acquire_entropy().
  111. *
  112. * If a random pool has been added to the DRBG using RAND_add(), then
  113. * its entropy will be used up first.
  114. */
  115. size_t rand_drbg_get_entropy(RAND_DRBG *drbg,
  116. unsigned char **pout,
  117. int entropy, size_t min_len, size_t max_len,
  118. int prediction_resistance)
  119. {
  120. size_t ret = 0;
  121. size_t entropy_available = 0;
  122. RAND_POOL *pool;
  123. if (drbg->parent && drbg->strength > drbg->parent->strength) {
  124. /*
  125. * We currently don't support the algorithm from NIST SP 800-90C
  126. * 10.1.2 to use a weaker DRBG as source
  127. */
  128. RANDerr(RAND_F_RAND_DRBG_GET_ENTROPY, RAND_R_PARENT_STRENGTH_TOO_WEAK);
  129. return 0;
  130. }
  131. if (drbg->pool != NULL) {
  132. pool = drbg->pool;
  133. pool->entropy_requested = entropy;
  134. } else {
  135. pool = rand_pool_new(entropy, min_len, max_len);
  136. if (pool == NULL)
  137. return 0;
  138. }
  139. if (drbg->parent) {
  140. size_t bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
  141. unsigned char *buffer = rand_pool_add_begin(pool, bytes_needed);
  142. if (buffer != NULL) {
  143. size_t bytes = 0;
  144. /*
  145. * Get random from parent, include our state as additional input.
  146. * Our lock is already held, but we need to lock our parent before
  147. * generating bits from it. (Note: taking the lock will be a no-op
  148. * if locking if drbg->parent->lock == NULL.)
  149. */
  150. rand_drbg_lock(drbg->parent);
  151. if (RAND_DRBG_generate(drbg->parent,
  152. buffer, bytes_needed,
  153. prediction_resistance,
  154. NULL, 0) != 0)
  155. bytes = bytes_needed;
  156. drbg->reseed_next_counter
  157. = tsan_load(&drbg->parent->reseed_prop_counter);
  158. rand_drbg_unlock(drbg->parent);
  159. rand_pool_add_end(pool, bytes, 8 * bytes);
  160. entropy_available = rand_pool_entropy_available(pool);
  161. }
  162. } else {
  163. if (prediction_resistance) {
  164. /*
  165. * We don't have any entropy sources that comply with the NIST
  166. * standard to provide prediction resistance (see NIST SP 800-90C,
  167. * Section 5.4).
  168. */
  169. RANDerr(RAND_F_RAND_DRBG_GET_ENTROPY,
  170. RAND_R_PREDICTION_RESISTANCE_NOT_SUPPORTED);
  171. goto err;
  172. }
  173. /* Get entropy by polling system entropy sources. */
  174. entropy_available = rand_pool_acquire_entropy(pool);
  175. }
  176. if (entropy_available > 0) {
  177. ret = rand_pool_length(pool);
  178. *pout = rand_pool_detach(pool);
  179. }
  180. err:
  181. if (drbg->pool == NULL)
  182. rand_pool_free(pool);
  183. return ret;
  184. }
  185. /*
  186. * Implements the cleanup_entropy() callback (see RAND_DRBG_set_callbacks())
  187. *
  188. */
  189. void rand_drbg_cleanup_entropy(RAND_DRBG *drbg,
  190. unsigned char *out, size_t outlen)
  191. {
  192. if (drbg->pool == NULL)
  193. OPENSSL_secure_clear_free(out, outlen);
  194. }
  195. /*
  196. * Implements the get_nonce() callback (see RAND_DRBG_set_callbacks())
  197. *
  198. */
  199. size_t rand_drbg_get_nonce(RAND_DRBG *drbg,
  200. unsigned char **pout,
  201. int entropy, size_t min_len, size_t max_len)
  202. {
  203. size_t ret = 0;
  204. RAND_POOL *pool;
  205. struct {
  206. void * instance;
  207. int count;
  208. } data = { 0 };
  209. pool = rand_pool_new(0, min_len, max_len);
  210. if (pool == NULL)
  211. return 0;
  212. if (rand_pool_add_nonce_data(pool) == 0)
  213. goto err;
  214. data.instance = drbg;
  215. CRYPTO_atomic_add(&rand_nonce_count, 1, &data.count, rand_nonce_lock);
  216. if (rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0) == 0)
  217. goto err;
  218. ret = rand_pool_length(pool);
  219. *pout = rand_pool_detach(pool);
  220. err:
  221. rand_pool_free(pool);
  222. return ret;
  223. }
  224. /*
  225. * Implements the cleanup_nonce() callback (see RAND_DRBG_set_callbacks())
  226. *
  227. */
  228. void rand_drbg_cleanup_nonce(RAND_DRBG *drbg,
  229. unsigned char *out, size_t outlen)
  230. {
  231. OPENSSL_secure_clear_free(out, outlen);
  232. }
  233. /*
  234. * Generate additional data that can be used for the drbg. The data does
  235. * not need to contain entropy, but it's useful if it contains at least
  236. * some bits that are unpredictable.
  237. *
  238. * Returns 0 on failure.
  239. *
  240. * On success it allocates a buffer at |*pout| and returns the length of
  241. * the data. The buffer should get freed using OPENSSL_secure_clear_free().
  242. */
  243. size_t rand_drbg_get_additional_data(unsigned char **pout, size_t max_len)
  244. {
  245. size_t ret = 0;
  246. RAND_POOL *pool;
  247. pool = rand_pool_new(0, 0, max_len);
  248. if (pool == NULL)
  249. return 0;
  250. if (rand_pool_add_additional_data(pool) == 0)
  251. goto err;
  252. ret = rand_pool_length(pool);
  253. *pout = rand_pool_detach(pool);
  254. err:
  255. rand_pool_free(pool);
  256. return ret;
  257. }
  258. void rand_drbg_cleanup_additional_data(unsigned char *out, size_t outlen)
  259. {
  260. OPENSSL_secure_clear_free(out, outlen);
  261. }
  262. void rand_fork(void)
  263. {
  264. rand_fork_count++;
  265. }
  266. DEFINE_RUN_ONCE_STATIC(do_rand_init)
  267. {
  268. #ifndef OPENSSL_NO_ENGINE
  269. rand_engine_lock = CRYPTO_THREAD_lock_new();
  270. if (rand_engine_lock == NULL)
  271. return 0;
  272. #endif
  273. rand_meth_lock = CRYPTO_THREAD_lock_new();
  274. if (rand_meth_lock == NULL)
  275. goto err1;
  276. rand_nonce_lock = CRYPTO_THREAD_lock_new();
  277. if (rand_nonce_lock == NULL)
  278. goto err2;
  279. if (!rand_cleaning_up && !rand_pool_init())
  280. goto err3;
  281. return 1;
  282. err3:
  283. rand_pool_cleanup();
  284. err2:
  285. CRYPTO_THREAD_lock_free(rand_meth_lock);
  286. rand_meth_lock = NULL;
  287. err1:
  288. #ifndef OPENSSL_NO_ENGINE
  289. CRYPTO_THREAD_lock_free(rand_engine_lock);
  290. rand_engine_lock = NULL;
  291. #endif
  292. return 0;
  293. }
  294. void rand_cleanup_int(void)
  295. {
  296. const RAND_METHOD *meth = default_RAND_meth;
  297. rand_cleaning_up = 1;
  298. if (meth != NULL && meth->cleanup != NULL)
  299. meth->cleanup();
  300. RAND_set_rand_method(NULL);
  301. rand_pool_cleanup();
  302. #ifndef OPENSSL_NO_ENGINE
  303. CRYPTO_THREAD_lock_free(rand_engine_lock);
  304. rand_engine_lock = NULL;
  305. #endif
  306. CRYPTO_THREAD_lock_free(rand_meth_lock);
  307. rand_meth_lock = NULL;
  308. CRYPTO_THREAD_lock_free(rand_nonce_lock);
  309. rand_nonce_lock = NULL;
  310. }
  311. /*
  312. * RAND_close_seed_files() ensures that any seed file decriptors are
  313. * closed after use.
  314. */
  315. void RAND_keep_random_devices_open(int keep)
  316. {
  317. rand_pool_keep_random_devices_open(keep);
  318. }
  319. /*
  320. * RAND_poll() reseeds the default RNG using random input
  321. *
  322. * The random input is obtained from polling various entropy
  323. * sources which depend on the operating system and are
  324. * configurable via the --with-rand-seed configure option.
  325. */
  326. int RAND_poll(void)
  327. {
  328. int ret = 0;
  329. RAND_POOL *pool = NULL;
  330. const RAND_METHOD *meth = RAND_get_rand_method();
  331. if (meth == RAND_OpenSSL()) {
  332. /* fill random pool and seed the master DRBG */
  333. RAND_DRBG *drbg = RAND_DRBG_get0_master();
  334. if (drbg == NULL)
  335. return 0;
  336. rand_drbg_lock(drbg);
  337. ret = rand_drbg_restart(drbg, NULL, 0, 0);
  338. rand_drbg_unlock(drbg);
  339. return ret;
  340. } else {
  341. /* fill random pool and seed the current legacy RNG */
  342. pool = rand_pool_new(RAND_DRBG_STRENGTH,
  343. RAND_DRBG_STRENGTH / 8,
  344. RAND_POOL_MAX_LENGTH);
  345. if (pool == NULL)
  346. return 0;
  347. if (rand_pool_acquire_entropy(pool) == 0)
  348. goto err;
  349. if (meth->add == NULL
  350. || meth->add(rand_pool_buffer(pool),
  351. rand_pool_length(pool),
  352. (rand_pool_entropy(pool) / 8.0)) == 0)
  353. goto err;
  354. ret = 1;
  355. }
  356. err:
  357. rand_pool_free(pool);
  358. return ret;
  359. }
  360. /*
  361. * Allocate memory and initialize a new random pool
  362. */
  363. RAND_POOL *rand_pool_new(int entropy_requested, size_t min_len, size_t max_len)
  364. {
  365. RAND_POOL *pool = OPENSSL_zalloc(sizeof(*pool));
  366. if (pool == NULL) {
  367. RANDerr(RAND_F_RAND_POOL_NEW, ERR_R_MALLOC_FAILURE);
  368. return NULL;
  369. }
  370. pool->min_len = min_len;
  371. pool->max_len = (max_len > RAND_POOL_MAX_LENGTH) ?
  372. RAND_POOL_MAX_LENGTH : max_len;
  373. pool->buffer = OPENSSL_secure_zalloc(pool->max_len);
  374. if (pool->buffer == NULL) {
  375. RANDerr(RAND_F_RAND_POOL_NEW, ERR_R_MALLOC_FAILURE);
  376. goto err;
  377. }
  378. pool->entropy_requested = entropy_requested;
  379. return pool;
  380. err:
  381. OPENSSL_free(pool);
  382. return NULL;
  383. }
  384. /*
  385. * Attach new random pool to the given buffer
  386. *
  387. * This function is intended to be used only for feeding random data
  388. * provided by RAND_add() and RAND_seed() into the <master> DRBG.
  389. */
  390. RAND_POOL *rand_pool_attach(const unsigned char *buffer, size_t len,
  391. size_t entropy)
  392. {
  393. RAND_POOL *pool = OPENSSL_zalloc(sizeof(*pool));
  394. if (pool == NULL) {
  395. RANDerr(RAND_F_RAND_POOL_ATTACH, ERR_R_MALLOC_FAILURE);
  396. return NULL;
  397. }
  398. /*
  399. * The const needs to be cast away, but attached buffers will not be
  400. * modified (in contrary to allocated buffers which are zeroed and
  401. * freed in the end).
  402. */
  403. pool->buffer = (unsigned char *) buffer;
  404. pool->len = len;
  405. pool->attached = 1;
  406. pool->min_len = pool->max_len = pool->len;
  407. pool->entropy = entropy;
  408. return pool;
  409. }
  410. /*
  411. * Free |pool|, securely erasing its buffer.
  412. */
  413. void rand_pool_free(RAND_POOL *pool)
  414. {
  415. if (pool == NULL)
  416. return;
  417. /*
  418. * Although it would be advisable from a cryptographical viewpoint,
  419. * we are not allowed to clear attached buffers, since they are passed
  420. * to rand_pool_attach() as `const unsigned char*`.
  421. * (see corresponding comment in rand_pool_attach()).
  422. */
  423. if (!pool->attached)
  424. OPENSSL_secure_clear_free(pool->buffer, pool->max_len);
  425. OPENSSL_free(pool);
  426. }
  427. /*
  428. * Return the |pool|'s buffer to the caller (readonly).
  429. */
  430. const unsigned char *rand_pool_buffer(RAND_POOL *pool)
  431. {
  432. return pool->buffer;
  433. }
  434. /*
  435. * Return the |pool|'s entropy to the caller.
  436. */
  437. size_t rand_pool_entropy(RAND_POOL *pool)
  438. {
  439. return pool->entropy;
  440. }
  441. /*
  442. * Return the |pool|'s buffer length to the caller.
  443. */
  444. size_t rand_pool_length(RAND_POOL *pool)
  445. {
  446. return pool->len;
  447. }
  448. /*
  449. * Detach the |pool| buffer and return it to the caller.
  450. * It's the responsibility of the caller to free the buffer
  451. * using OPENSSL_secure_clear_free().
  452. */
  453. unsigned char *rand_pool_detach(RAND_POOL *pool)
  454. {
  455. unsigned char *ret = pool->buffer;
  456. pool->buffer = NULL;
  457. pool->len = 0;
  458. pool->entropy = 0;
  459. return ret;
  460. }
  461. /*
  462. * If |entropy_factor| bits contain 1 bit of entropy, how many bytes does one
  463. * need to obtain at least |bits| bits of entropy?
  464. */
  465. #define ENTROPY_TO_BYTES(bits, entropy_factor) \
  466. (((bits) * (entropy_factor) + 7) / 8)
  467. /*
  468. * Checks whether the |pool|'s entropy is available to the caller.
  469. * This is the case when entropy count and buffer length are high enough.
  470. * Returns
  471. *
  472. * |entropy| if the entropy count and buffer size is large enough
  473. * 0 otherwise
  474. */
  475. size_t rand_pool_entropy_available(RAND_POOL *pool)
  476. {
  477. if (pool->entropy < pool->entropy_requested)
  478. return 0;
  479. if (pool->len < pool->min_len)
  480. return 0;
  481. return pool->entropy;
  482. }
  483. /*
  484. * Returns the (remaining) amount of entropy needed to fill
  485. * the random pool.
  486. */
  487. size_t rand_pool_entropy_needed(RAND_POOL *pool)
  488. {
  489. if (pool->entropy < pool->entropy_requested)
  490. return pool->entropy_requested - pool->entropy;
  491. return 0;
  492. }
  493. /*
  494. * Returns the number of bytes needed to fill the pool, assuming
  495. * the input has 1 / |entropy_factor| entropy bits per data bit.
  496. * In case of an error, 0 is returned.
  497. */
  498. size_t rand_pool_bytes_needed(RAND_POOL *pool, unsigned int entropy_factor)
  499. {
  500. size_t bytes_needed;
  501. size_t entropy_needed = rand_pool_entropy_needed(pool);
  502. if (entropy_factor < 1) {
  503. RANDerr(RAND_F_RAND_POOL_BYTES_NEEDED, RAND_R_ARGUMENT_OUT_OF_RANGE);
  504. return 0;
  505. }
  506. bytes_needed = ENTROPY_TO_BYTES(entropy_needed, entropy_factor);
  507. if (bytes_needed > pool->max_len - pool->len) {
  508. /* not enough space left */
  509. RANDerr(RAND_F_RAND_POOL_BYTES_NEEDED, RAND_R_RANDOM_POOL_OVERFLOW);
  510. return 0;
  511. }
  512. if (pool->len < pool->min_len &&
  513. bytes_needed < pool->min_len - pool->len)
  514. /* to meet the min_len requirement */
  515. bytes_needed = pool->min_len - pool->len;
  516. return bytes_needed;
  517. }
  518. /* Returns the remaining number of bytes available */
  519. size_t rand_pool_bytes_remaining(RAND_POOL *pool)
  520. {
  521. return pool->max_len - pool->len;
  522. }
  523. /*
  524. * Add random bytes to the random pool.
  525. *
  526. * It is expected that the |buffer| contains |len| bytes of
  527. * random input which contains at least |entropy| bits of
  528. * randomness.
  529. *
  530. * Returns 1 if the added amount is adequate, otherwise 0
  531. */
  532. int rand_pool_add(RAND_POOL *pool,
  533. const unsigned char *buffer, size_t len, size_t entropy)
  534. {
  535. if (len > pool->max_len - pool->len) {
  536. RANDerr(RAND_F_RAND_POOL_ADD, RAND_R_ENTROPY_INPUT_TOO_LONG);
  537. return 0;
  538. }
  539. if (len > 0) {
  540. memcpy(pool->buffer + pool->len, buffer, len);
  541. pool->len += len;
  542. pool->entropy += entropy;
  543. }
  544. return 1;
  545. }
  546. /*
  547. * Start to add random bytes to the random pool in-place.
  548. *
  549. * Reserves the next |len| bytes for adding random bytes in-place
  550. * and returns a pointer to the buffer.
  551. * The caller is allowed to copy up to |len| bytes into the buffer.
  552. * If |len| == 0 this is considered a no-op and a NULL pointer
  553. * is returned without producing an error message.
  554. *
  555. * After updating the buffer, rand_pool_add_end() needs to be called
  556. * to finish the udpate operation (see next comment).
  557. */
  558. unsigned char *rand_pool_add_begin(RAND_POOL *pool, size_t len)
  559. {
  560. if (len == 0)
  561. return NULL;
  562. if (len > pool->max_len - pool->len) {
  563. RANDerr(RAND_F_RAND_POOL_ADD_BEGIN, RAND_R_RANDOM_POOL_OVERFLOW);
  564. return NULL;
  565. }
  566. return pool->buffer + pool->len;
  567. }
  568. /*
  569. * Finish to add random bytes to the random pool in-place.
  570. *
  571. * Finishes an in-place update of the random pool started by
  572. * rand_pool_add_begin() (see previous comment).
  573. * It is expected that |len| bytes of random input have been added
  574. * to the buffer which contain at least |entropy| bits of randomness.
  575. * It is allowed to add less bytes than originally reserved.
  576. */
  577. int rand_pool_add_end(RAND_POOL *pool, size_t len, size_t entropy)
  578. {
  579. if (len > pool->max_len - pool->len) {
  580. RANDerr(RAND_F_RAND_POOL_ADD_END, RAND_R_RANDOM_POOL_OVERFLOW);
  581. return 0;
  582. }
  583. if (len > 0) {
  584. pool->len += len;
  585. pool->entropy += entropy;
  586. }
  587. return 1;
  588. }
  589. int RAND_set_rand_method(const RAND_METHOD *meth)
  590. {
  591. if (!RUN_ONCE(&rand_init, do_rand_init))
  592. return 0;
  593. CRYPTO_THREAD_write_lock(rand_meth_lock);
  594. #ifndef OPENSSL_NO_ENGINE
  595. ENGINE_finish(funct_ref);
  596. funct_ref = NULL;
  597. #endif
  598. default_RAND_meth = meth;
  599. CRYPTO_THREAD_unlock(rand_meth_lock);
  600. return 1;
  601. }
  602. const RAND_METHOD *RAND_get_rand_method(void)
  603. {
  604. const RAND_METHOD *tmp_meth = NULL;
  605. if (!RUN_ONCE(&rand_init, do_rand_init))
  606. return NULL;
  607. CRYPTO_THREAD_write_lock(rand_meth_lock);
  608. if (default_RAND_meth == NULL) {
  609. #ifndef OPENSSL_NO_ENGINE
  610. ENGINE *e;
  611. /* If we have an engine that can do RAND, use it. */
  612. if ((e = ENGINE_get_default_RAND()) != NULL
  613. && (tmp_meth = ENGINE_get_RAND(e)) != NULL) {
  614. funct_ref = e;
  615. default_RAND_meth = tmp_meth;
  616. } else {
  617. ENGINE_finish(e);
  618. default_RAND_meth = &rand_meth;
  619. }
  620. #else
  621. default_RAND_meth = &rand_meth;
  622. #endif
  623. }
  624. tmp_meth = default_RAND_meth;
  625. CRYPTO_THREAD_unlock(rand_meth_lock);
  626. return tmp_meth;
  627. }
  628. #ifndef OPENSSL_NO_ENGINE
  629. int RAND_set_rand_engine(ENGINE *engine)
  630. {
  631. const RAND_METHOD *tmp_meth = NULL;
  632. if (!RUN_ONCE(&rand_init, do_rand_init))
  633. return 0;
  634. if (engine != NULL) {
  635. if (!ENGINE_init(engine))
  636. return 0;
  637. tmp_meth = ENGINE_get_RAND(engine);
  638. if (tmp_meth == NULL) {
  639. ENGINE_finish(engine);
  640. return 0;
  641. }
  642. }
  643. CRYPTO_THREAD_write_lock(rand_engine_lock);
  644. /* This function releases any prior ENGINE so call it first */
  645. RAND_set_rand_method(tmp_meth);
  646. funct_ref = engine;
  647. CRYPTO_THREAD_unlock(rand_engine_lock);
  648. return 1;
  649. }
  650. #endif
  651. void RAND_seed(const void *buf, int num)
  652. {
  653. const RAND_METHOD *meth = RAND_get_rand_method();
  654. if (meth->seed != NULL)
  655. meth->seed(buf, num);
  656. }
  657. void RAND_add(const void *buf, int num, double randomness)
  658. {
  659. const RAND_METHOD *meth = RAND_get_rand_method();
  660. if (meth->add != NULL)
  661. meth->add(buf, num, randomness);
  662. }
  663. /*
  664. * This function is not part of RAND_METHOD, so if we're not using
  665. * the default method, then just call RAND_bytes(). Otherwise make
  666. * sure we're instantiated and use the private DRBG.
  667. */
  668. int RAND_priv_bytes(unsigned char *buf, int num)
  669. {
  670. const RAND_METHOD *meth = RAND_get_rand_method();
  671. RAND_DRBG *drbg;
  672. int ret;
  673. if (meth != RAND_OpenSSL())
  674. return RAND_bytes(buf, num);
  675. drbg = RAND_DRBG_get0_private();
  676. if (drbg == NULL)
  677. return 0;
  678. ret = RAND_DRBG_bytes(drbg, buf, num);
  679. return ret;
  680. }
  681. int RAND_bytes(unsigned char *buf, int num)
  682. {
  683. const RAND_METHOD *meth = RAND_get_rand_method();
  684. if (meth->bytes != NULL)
  685. return meth->bytes(buf, num);
  686. RANDerr(RAND_F_RAND_BYTES, RAND_R_FUNC_NOT_IMPLEMENTED);
  687. return -1;
  688. }
  689. #if OPENSSL_API_COMPAT < 0x10100000L
  690. int RAND_pseudo_bytes(unsigned char *buf, int num)
  691. {
  692. const RAND_METHOD *meth = RAND_get_rand_method();
  693. if (meth->pseudorand != NULL)
  694. return meth->pseudorand(buf, num);
  695. return -1;
  696. }
  697. #endif
  698. int RAND_status(void)
  699. {
  700. const RAND_METHOD *meth = RAND_get_rand_method();
  701. if (meth->status != NULL)
  702. return meth->status();
  703. return 0;
  704. }