rand_lib.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. /*
  2. * Copyright 1995-2019 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_inited = 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 != NULL && 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->seed_pool != NULL) {
  132. pool = drbg->seed_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 != NULL) {
  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->seed_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->seed_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 = { NULL, 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(RAND_POOL *pool, unsigned char **pout)
  244. {
  245. size_t ret = 0;
  246. if (rand_pool_add_additional_data(pool) == 0)
  247. goto err;
  248. ret = rand_pool_length(pool);
  249. *pout = rand_pool_detach(pool);
  250. err:
  251. return ret;
  252. }
  253. void rand_drbg_cleanup_additional_data(RAND_POOL *pool, unsigned char *out)
  254. {
  255. rand_pool_reattach(pool, out);
  256. }
  257. void rand_fork(void)
  258. {
  259. rand_fork_count++;
  260. }
  261. DEFINE_RUN_ONCE_STATIC(do_rand_init)
  262. {
  263. #ifndef OPENSSL_NO_ENGINE
  264. rand_engine_lock = CRYPTO_THREAD_lock_new();
  265. if (rand_engine_lock == NULL)
  266. return 0;
  267. #endif
  268. rand_meth_lock = CRYPTO_THREAD_lock_new();
  269. if (rand_meth_lock == NULL)
  270. goto err1;
  271. rand_nonce_lock = CRYPTO_THREAD_lock_new();
  272. if (rand_nonce_lock == NULL)
  273. goto err2;
  274. if (!rand_pool_init())
  275. goto err3;
  276. rand_inited = 1;
  277. return 1;
  278. err3:
  279. CRYPTO_THREAD_lock_free(rand_nonce_lock);
  280. rand_nonce_lock = NULL;
  281. err2:
  282. CRYPTO_THREAD_lock_free(rand_meth_lock);
  283. rand_meth_lock = NULL;
  284. err1:
  285. #ifndef OPENSSL_NO_ENGINE
  286. CRYPTO_THREAD_lock_free(rand_engine_lock);
  287. rand_engine_lock = NULL;
  288. #endif
  289. return 0;
  290. }
  291. void rand_cleanup_int(void)
  292. {
  293. const RAND_METHOD *meth = default_RAND_meth;
  294. if (!rand_inited)
  295. return;
  296. if (meth != NULL && meth->cleanup != NULL)
  297. meth->cleanup();
  298. RAND_set_rand_method(NULL);
  299. rand_pool_cleanup();
  300. #ifndef OPENSSL_NO_ENGINE
  301. CRYPTO_THREAD_lock_free(rand_engine_lock);
  302. rand_engine_lock = NULL;
  303. #endif
  304. CRYPTO_THREAD_lock_free(rand_meth_lock);
  305. rand_meth_lock = NULL;
  306. CRYPTO_THREAD_lock_free(rand_nonce_lock);
  307. rand_nonce_lock = NULL;
  308. rand_inited = 0;
  309. }
  310. /*
  311. * RAND_close_seed_files() ensures that any seed file decriptors are
  312. * closed after use.
  313. */
  314. void RAND_keep_random_devices_open(int keep)
  315. {
  316. if (RUN_ONCE(&rand_init, do_rand_init))
  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 + 7) / 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() or to re-attach it
  452. * again to the pool using rand_pool_reattach().
  453. */
  454. unsigned char *rand_pool_detach(RAND_POOL *pool)
  455. {
  456. unsigned char *ret = pool->buffer;
  457. pool->buffer = NULL;
  458. pool->entropy = 0;
  459. return ret;
  460. }
  461. /*
  462. * Re-attach the |pool| buffer. It is only allowed to pass
  463. * the |buffer| which was previously detached from the same pool.
  464. */
  465. void rand_pool_reattach(RAND_POOL *pool, unsigned char *buffer)
  466. {
  467. pool->buffer = buffer;
  468. OPENSSL_cleanse(pool->buffer, pool->len);
  469. pool->len = 0;
  470. }
  471. /*
  472. * If |entropy_factor| bits contain 1 bit of entropy, how many bytes does one
  473. * need to obtain at least |bits| bits of entropy?
  474. */
  475. #define ENTROPY_TO_BYTES(bits, entropy_factor) \
  476. (((bits) * (entropy_factor) + 7) / 8)
  477. /*
  478. * Checks whether the |pool|'s entropy is available to the caller.
  479. * This is the case when entropy count and buffer length are high enough.
  480. * Returns
  481. *
  482. * |entropy| if the entropy count and buffer size is large enough
  483. * 0 otherwise
  484. */
  485. size_t rand_pool_entropy_available(RAND_POOL *pool)
  486. {
  487. if (pool->entropy < pool->entropy_requested)
  488. return 0;
  489. if (pool->len < pool->min_len)
  490. return 0;
  491. return pool->entropy;
  492. }
  493. /*
  494. * Returns the (remaining) amount of entropy needed to fill
  495. * the random pool.
  496. */
  497. size_t rand_pool_entropy_needed(RAND_POOL *pool)
  498. {
  499. if (pool->entropy < pool->entropy_requested)
  500. return pool->entropy_requested - pool->entropy;
  501. return 0;
  502. }
  503. /*
  504. * Returns the number of bytes needed to fill the pool, assuming
  505. * the input has 1 / |entropy_factor| entropy bits per data bit.
  506. * In case of an error, 0 is returned.
  507. */
  508. size_t rand_pool_bytes_needed(RAND_POOL *pool, unsigned int entropy_factor)
  509. {
  510. size_t bytes_needed;
  511. size_t entropy_needed = rand_pool_entropy_needed(pool);
  512. if (entropy_factor < 1) {
  513. RANDerr(RAND_F_RAND_POOL_BYTES_NEEDED, RAND_R_ARGUMENT_OUT_OF_RANGE);
  514. return 0;
  515. }
  516. bytes_needed = ENTROPY_TO_BYTES(entropy_needed, entropy_factor);
  517. if (bytes_needed > pool->max_len - pool->len) {
  518. /* not enough space left */
  519. RANDerr(RAND_F_RAND_POOL_BYTES_NEEDED, RAND_R_RANDOM_POOL_OVERFLOW);
  520. return 0;
  521. }
  522. if (pool->len < pool->min_len &&
  523. bytes_needed < pool->min_len - pool->len)
  524. /* to meet the min_len requirement */
  525. bytes_needed = pool->min_len - pool->len;
  526. return bytes_needed;
  527. }
  528. /* Returns the remaining number of bytes available */
  529. size_t rand_pool_bytes_remaining(RAND_POOL *pool)
  530. {
  531. return pool->max_len - pool->len;
  532. }
  533. /*
  534. * Add random bytes to the random pool.
  535. *
  536. * It is expected that the |buffer| contains |len| bytes of
  537. * random input which contains at least |entropy| bits of
  538. * randomness.
  539. *
  540. * Returns 1 if the added amount is adequate, otherwise 0
  541. */
  542. int rand_pool_add(RAND_POOL *pool,
  543. const unsigned char *buffer, size_t len, size_t entropy)
  544. {
  545. if (len > pool->max_len - pool->len) {
  546. RANDerr(RAND_F_RAND_POOL_ADD, RAND_R_ENTROPY_INPUT_TOO_LONG);
  547. return 0;
  548. }
  549. if (pool->buffer == NULL) {
  550. RANDerr(RAND_F_RAND_POOL_ADD, ERR_R_INTERNAL_ERROR);
  551. return 0;
  552. }
  553. if (len > 0) {
  554. memcpy(pool->buffer + pool->len, buffer, len);
  555. pool->len += len;
  556. pool->entropy += entropy;
  557. }
  558. return 1;
  559. }
  560. /*
  561. * Start to add random bytes to the random pool in-place.
  562. *
  563. * Reserves the next |len| bytes for adding random bytes in-place
  564. * and returns a pointer to the buffer.
  565. * The caller is allowed to copy up to |len| bytes into the buffer.
  566. * If |len| == 0 this is considered a no-op and a NULL pointer
  567. * is returned without producing an error message.
  568. *
  569. * After updating the buffer, rand_pool_add_end() needs to be called
  570. * to finish the udpate operation (see next comment).
  571. */
  572. unsigned char *rand_pool_add_begin(RAND_POOL *pool, size_t len)
  573. {
  574. if (len == 0)
  575. return NULL;
  576. if (len > pool->max_len - pool->len) {
  577. RANDerr(RAND_F_RAND_POOL_ADD_BEGIN, RAND_R_RANDOM_POOL_OVERFLOW);
  578. return NULL;
  579. }
  580. if (pool->buffer == NULL) {
  581. RANDerr(RAND_F_RAND_POOL_ADD_BEGIN, ERR_R_INTERNAL_ERROR);
  582. return NULL;
  583. }
  584. return pool->buffer + pool->len;
  585. }
  586. /*
  587. * Finish to add random bytes to the random pool in-place.
  588. *
  589. * Finishes an in-place update of the random pool started by
  590. * rand_pool_add_begin() (see previous comment).
  591. * It is expected that |len| bytes of random input have been added
  592. * to the buffer which contain at least |entropy| bits of randomness.
  593. * It is allowed to add less bytes than originally reserved.
  594. */
  595. int rand_pool_add_end(RAND_POOL *pool, size_t len, size_t entropy)
  596. {
  597. if (len > pool->max_len - pool->len) {
  598. RANDerr(RAND_F_RAND_POOL_ADD_END, RAND_R_RANDOM_POOL_OVERFLOW);
  599. return 0;
  600. }
  601. if (len > 0) {
  602. pool->len += len;
  603. pool->entropy += entropy;
  604. }
  605. return 1;
  606. }
  607. int RAND_set_rand_method(const RAND_METHOD *meth)
  608. {
  609. if (!RUN_ONCE(&rand_init, do_rand_init))
  610. return 0;
  611. CRYPTO_THREAD_write_lock(rand_meth_lock);
  612. #ifndef OPENSSL_NO_ENGINE
  613. ENGINE_finish(funct_ref);
  614. funct_ref = NULL;
  615. #endif
  616. default_RAND_meth = meth;
  617. CRYPTO_THREAD_unlock(rand_meth_lock);
  618. return 1;
  619. }
  620. const RAND_METHOD *RAND_get_rand_method(void)
  621. {
  622. const RAND_METHOD *tmp_meth = NULL;
  623. if (!RUN_ONCE(&rand_init, do_rand_init))
  624. return NULL;
  625. CRYPTO_THREAD_write_lock(rand_meth_lock);
  626. if (default_RAND_meth == NULL) {
  627. #ifndef OPENSSL_NO_ENGINE
  628. ENGINE *e;
  629. /* If we have an engine that can do RAND, use it. */
  630. if ((e = ENGINE_get_default_RAND()) != NULL
  631. && (tmp_meth = ENGINE_get_RAND(e)) != NULL) {
  632. funct_ref = e;
  633. default_RAND_meth = tmp_meth;
  634. } else {
  635. ENGINE_finish(e);
  636. default_RAND_meth = &rand_meth;
  637. }
  638. #else
  639. default_RAND_meth = &rand_meth;
  640. #endif
  641. }
  642. tmp_meth = default_RAND_meth;
  643. CRYPTO_THREAD_unlock(rand_meth_lock);
  644. return tmp_meth;
  645. }
  646. #ifndef OPENSSL_NO_ENGINE
  647. int RAND_set_rand_engine(ENGINE *engine)
  648. {
  649. const RAND_METHOD *tmp_meth = NULL;
  650. if (!RUN_ONCE(&rand_init, do_rand_init))
  651. return 0;
  652. if (engine != NULL) {
  653. if (!ENGINE_init(engine))
  654. return 0;
  655. tmp_meth = ENGINE_get_RAND(engine);
  656. if (tmp_meth == NULL) {
  657. ENGINE_finish(engine);
  658. return 0;
  659. }
  660. }
  661. CRYPTO_THREAD_write_lock(rand_engine_lock);
  662. /* This function releases any prior ENGINE so call it first */
  663. RAND_set_rand_method(tmp_meth);
  664. funct_ref = engine;
  665. CRYPTO_THREAD_unlock(rand_engine_lock);
  666. return 1;
  667. }
  668. #endif
  669. void RAND_seed(const void *buf, int num)
  670. {
  671. const RAND_METHOD *meth = RAND_get_rand_method();
  672. if (meth->seed != NULL)
  673. meth->seed(buf, num);
  674. }
  675. void RAND_add(const void *buf, int num, double randomness)
  676. {
  677. const RAND_METHOD *meth = RAND_get_rand_method();
  678. if (meth->add != NULL)
  679. meth->add(buf, num, randomness);
  680. }
  681. /*
  682. * This function is not part of RAND_METHOD, so if we're not using
  683. * the default method, then just call RAND_bytes(). Otherwise make
  684. * sure we're instantiated and use the private DRBG.
  685. */
  686. int RAND_priv_bytes(unsigned char *buf, int num)
  687. {
  688. const RAND_METHOD *meth = RAND_get_rand_method();
  689. RAND_DRBG *drbg;
  690. int ret;
  691. if (meth != RAND_OpenSSL())
  692. return RAND_bytes(buf, num);
  693. drbg = RAND_DRBG_get0_private();
  694. if (drbg == NULL)
  695. return 0;
  696. ret = RAND_DRBG_bytes(drbg, buf, num);
  697. return ret;
  698. }
  699. int RAND_bytes(unsigned char *buf, int num)
  700. {
  701. const RAND_METHOD *meth = RAND_get_rand_method();
  702. if (meth->bytes != NULL)
  703. return meth->bytes(buf, num);
  704. RANDerr(RAND_F_RAND_BYTES, RAND_R_FUNC_NOT_IMPLEMENTED);
  705. return -1;
  706. }
  707. #if OPENSSL_API_COMPAT < 0x10100000L
  708. int RAND_pseudo_bytes(unsigned char *buf, int num)
  709. {
  710. const RAND_METHOD *meth = RAND_get_rand_method();
  711. if (meth->pseudorand != NULL)
  712. return meth->pseudorand(buf, num);
  713. return -1;
  714. }
  715. #endif
  716. int RAND_status(void)
  717. {
  718. const RAND_METHOD *meth = RAND_get_rand_method();
  719. if (meth->status != NULL)
  720. return meth->status();
  721. return 0;
  722. }