rand_lib.c 25 KB

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