rand_lib.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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. #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. extern unsigned int OPENSSL_ia32cap_P[];
  63. /*
  64. * Acquire entropy using Intel-specific cpu instructions
  65. *
  66. * Uses the RDSEED instruction if available, otherwise uses
  67. * RDRAND if available.
  68. *
  69. * For the differences between RDSEED and RDRAND, and why RDSEED
  70. * is the preferred choice, see https://goo.gl/oK3KcN
  71. *
  72. * Returns the total entropy count, if it exceeds the requested
  73. * entropy count. Otherwise, returns an entropy count of 0.
  74. */
  75. size_t rand_acquire_entropy_from_cpu(RAND_POOL *pool)
  76. {
  77. size_t bytes_needed;
  78. unsigned char *buffer;
  79. bytes_needed = rand_pool_bytes_needed(pool, 8 /*entropy_per_byte*/);
  80. if (bytes_needed > 0) {
  81. buffer = rand_pool_add_begin(pool, bytes_needed);
  82. if (buffer != NULL) {
  83. /* Whichever comes first, use RDSEED, RDRAND or nothing */
  84. if ((OPENSSL_ia32cap_P[2] & (1 << 18)) != 0) {
  85. if (OPENSSL_ia32_rdseed_bytes(buffer, bytes_needed)
  86. == bytes_needed) {
  87. rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);
  88. }
  89. } else if ((OPENSSL_ia32cap_P[1] & (1 << (62 - 32))) != 0) {
  90. if (OPENSSL_ia32_rdrand_bytes(buffer, bytes_needed)
  91. == bytes_needed) {
  92. rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);
  93. }
  94. } else {
  95. rand_pool_add_end(pool, 0, 0);
  96. }
  97. }
  98. }
  99. return rand_pool_entropy_available(pool);
  100. }
  101. #endif
  102. /*
  103. * Implements the get_entropy() callback (see RAND_DRBG_set_callbacks())
  104. *
  105. * If the DRBG has a parent, then the required amount of entropy input
  106. * is fetched using the parent's RAND_DRBG_generate().
  107. *
  108. * Otherwise, the entropy is polled from the system entropy sources
  109. * using rand_pool_acquire_entropy().
  110. *
  111. * If a random pool has been added to the DRBG using RAND_add(), then
  112. * its entropy will be used up first.
  113. */
  114. size_t rand_drbg_get_entropy(RAND_DRBG *drbg,
  115. unsigned char **pout,
  116. int entropy, size_t min_len, size_t max_len,
  117. int prediction_resistance)
  118. {
  119. size_t ret = 0;
  120. size_t entropy_available = 0;
  121. RAND_POOL *pool;
  122. if (drbg->parent && drbg->strength > drbg->parent->strength) {
  123. /*
  124. * We currently don't support the algorithm from NIST SP 800-90C
  125. * 10.1.2 to use a weaker DRBG as source
  126. */
  127. RANDerr(RAND_F_RAND_DRBG_GET_ENTROPY, RAND_R_PARENT_STRENGTH_TOO_WEAK);
  128. return 0;
  129. }
  130. pool = rand_pool_new(entropy, min_len, max_len);
  131. if (pool == NULL)
  132. return 0;
  133. if (drbg->pool) {
  134. rand_pool_add(pool,
  135. rand_pool_buffer(drbg->pool),
  136. rand_pool_length(drbg->pool),
  137. rand_pool_entropy(drbg->pool));
  138. rand_pool_free(drbg->pool);
  139. drbg->pool = NULL;
  140. }
  141. if (drbg->parent) {
  142. size_t bytes_needed = rand_pool_bytes_needed(pool, 8);
  143. unsigned char *buffer = rand_pool_add_begin(pool, bytes_needed);
  144. if (buffer != NULL) {
  145. size_t bytes = 0;
  146. /*
  147. * Get random from parent, include our state as additional input.
  148. * Our lock is already held, but we need to lock our parent before
  149. * generating bits from it. (Note: taking the lock will be a no-op
  150. * if locking if drbg->parent->lock == NULL.)
  151. */
  152. rand_drbg_lock(drbg->parent);
  153. if (RAND_DRBG_generate(drbg->parent,
  154. buffer, bytes_needed,
  155. prediction_resistance,
  156. (unsigned char *)drbg, sizeof(*drbg)) != 0)
  157. bytes = bytes_needed;
  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. rand_pool_free(pool);
  182. return ret;
  183. }
  184. /*
  185. * Implements the cleanup_entropy() callback (see RAND_DRBG_set_callbacks())
  186. *
  187. */
  188. void rand_drbg_cleanup_entropy(RAND_DRBG *drbg,
  189. unsigned char *out, size_t outlen)
  190. {
  191. OPENSSL_secure_clear_free(out, outlen);
  192. }
  193. /*
  194. * Implements the get_nonce() callback (see RAND_DRBG_set_callbacks())
  195. *
  196. */
  197. size_t rand_drbg_get_nonce(RAND_DRBG *drbg,
  198. unsigned char **pout,
  199. int entropy, size_t min_len, size_t max_len)
  200. {
  201. size_t ret = 0;
  202. RAND_POOL *pool;
  203. struct {
  204. void * instance;
  205. int count;
  206. } data = { 0 };
  207. pool = rand_pool_new(0, min_len, max_len);
  208. if (pool == NULL)
  209. return 0;
  210. if (rand_pool_add_nonce_data(pool) == 0)
  211. goto err;
  212. data.instance = drbg;
  213. CRYPTO_atomic_add(&rand_nonce_count, 1, &data.count, rand_nonce_lock);
  214. if (rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0) == 0)
  215. goto err;
  216. ret = rand_pool_length(pool);
  217. *pout = rand_pool_detach(pool);
  218. err:
  219. rand_pool_free(pool);
  220. return ret;
  221. }
  222. /*
  223. * Implements the cleanup_nonce() callback (see RAND_DRBG_set_callbacks())
  224. *
  225. */
  226. void rand_drbg_cleanup_nonce(RAND_DRBG *drbg,
  227. unsigned char *out, size_t outlen)
  228. {
  229. OPENSSL_secure_clear_free(out, outlen);
  230. }
  231. /*
  232. * Generate additional data that can be used for the drbg. The data does
  233. * not need to contain entropy, but it's useful if it contains at least
  234. * some bits that are unpredictable.
  235. *
  236. * Returns 0 on failure.
  237. *
  238. * On success it allocates a buffer at |*pout| and returns the length of
  239. * the data. The buffer should get freed using OPENSSL_secure_clear_free().
  240. */
  241. size_t rand_drbg_get_additional_data(unsigned char **pout, size_t max_len)
  242. {
  243. size_t ret = 0;
  244. RAND_POOL *pool;
  245. pool = rand_pool_new(0, 0, max_len);
  246. if (pool == NULL)
  247. return 0;
  248. if (rand_pool_add_additional_data(pool) == 0)
  249. goto err;
  250. ret = rand_pool_length(pool);
  251. *pout = rand_pool_detach(pool);
  252. err:
  253. rand_pool_free(pool);
  254. return ret;
  255. }
  256. void rand_drbg_cleanup_additional_data(unsigned char *out, size_t outlen)
  257. {
  258. OPENSSL_secure_clear_free(out, outlen);
  259. }
  260. void rand_fork()
  261. {
  262. rand_fork_count++;
  263. }
  264. DEFINE_RUN_ONCE_STATIC(do_rand_init)
  265. {
  266. int ret = 1;
  267. #ifndef OPENSSL_NO_ENGINE
  268. rand_engine_lock = CRYPTO_THREAD_lock_new();
  269. ret &= rand_engine_lock != NULL;
  270. #endif
  271. rand_meth_lock = CRYPTO_THREAD_lock_new();
  272. ret &= rand_meth_lock != NULL;
  273. rand_nonce_lock = CRYPTO_THREAD_lock_new();
  274. ret &= rand_meth_lock != NULL;
  275. return ret;
  276. }
  277. void rand_cleanup_int(void)
  278. {
  279. const RAND_METHOD *meth = default_RAND_meth;
  280. if (meth != NULL && meth->cleanup != NULL)
  281. meth->cleanup();
  282. RAND_set_rand_method(NULL);
  283. #ifndef OPENSSL_NO_ENGINE
  284. CRYPTO_THREAD_lock_free(rand_engine_lock);
  285. #endif
  286. CRYPTO_THREAD_lock_free(rand_meth_lock);
  287. CRYPTO_THREAD_lock_free(rand_nonce_lock);
  288. }
  289. /*
  290. * RAND_poll() reseeds the default RNG using random input
  291. *
  292. * The random input is obtained from polling various entropy
  293. * sources which depend on the operating system and are
  294. * configurable via the --with-rand-seed configure option.
  295. */
  296. int RAND_poll(void)
  297. {
  298. int ret = 0;
  299. RAND_POOL *pool = NULL;
  300. const RAND_METHOD *meth = RAND_get_rand_method();
  301. if (meth == RAND_OpenSSL()) {
  302. /* fill random pool and seed the master DRBG */
  303. RAND_DRBG *drbg = RAND_DRBG_get0_master();
  304. if (drbg == NULL)
  305. return 0;
  306. rand_drbg_lock(drbg);
  307. ret = rand_drbg_restart(drbg, NULL, 0, 0);
  308. rand_drbg_unlock(drbg);
  309. return ret;
  310. } else {
  311. /* fill random pool and seed the current legacy RNG */
  312. pool = rand_pool_new(RAND_DRBG_STRENGTH,
  313. RAND_DRBG_STRENGTH / 8,
  314. DRBG_MINMAX_FACTOR * (RAND_DRBG_STRENGTH / 8));
  315. if (pool == NULL)
  316. return 0;
  317. if (rand_pool_acquire_entropy(pool) == 0)
  318. goto err;
  319. if (meth->add == NULL
  320. || meth->add(rand_pool_buffer(pool),
  321. rand_pool_length(pool),
  322. (rand_pool_entropy(pool) / 8.0)) == 0)
  323. goto err;
  324. ret = 1;
  325. }
  326. err:
  327. rand_pool_free(pool);
  328. return ret;
  329. }
  330. /*
  331. * Allocate memory and initialize a new random pool
  332. */
  333. RAND_POOL *rand_pool_new(int entropy, size_t min_len, size_t max_len)
  334. {
  335. RAND_POOL *pool = OPENSSL_zalloc(sizeof(*pool));
  336. if (pool == NULL) {
  337. RANDerr(RAND_F_RAND_POOL_NEW, ERR_R_MALLOC_FAILURE);
  338. goto err;
  339. }
  340. pool->min_len = min_len;
  341. pool->max_len = max_len;
  342. pool->buffer = OPENSSL_secure_zalloc(pool->max_len);
  343. if (pool->buffer == NULL) {
  344. RANDerr(RAND_F_RAND_POOL_NEW, ERR_R_MALLOC_FAILURE);
  345. goto err;
  346. }
  347. pool->requested_entropy = entropy;
  348. return pool;
  349. err:
  350. OPENSSL_free(pool);
  351. return NULL;
  352. }
  353. /*
  354. * Free |pool|, securely erasing its buffer.
  355. */
  356. void rand_pool_free(RAND_POOL *pool)
  357. {
  358. if (pool == NULL)
  359. return;
  360. OPENSSL_secure_clear_free(pool->buffer, pool->max_len);
  361. OPENSSL_free(pool);
  362. }
  363. /*
  364. * Return the |pool|'s buffer to the caller (readonly).
  365. */
  366. const unsigned char *rand_pool_buffer(RAND_POOL *pool)
  367. {
  368. return pool->buffer;
  369. }
  370. /*
  371. * Return the |pool|'s entropy to the caller.
  372. */
  373. size_t rand_pool_entropy(RAND_POOL *pool)
  374. {
  375. return pool->entropy;
  376. }
  377. /*
  378. * Return the |pool|'s buffer length to the caller.
  379. */
  380. size_t rand_pool_length(RAND_POOL *pool)
  381. {
  382. return pool->len;
  383. }
  384. /*
  385. * Detach the |pool| buffer and return it to the caller.
  386. * It's the responsibility of the caller to free the buffer
  387. * using OPENSSL_secure_clear_free().
  388. */
  389. unsigned char *rand_pool_detach(RAND_POOL *pool)
  390. {
  391. unsigned char *ret = pool->buffer;
  392. pool->buffer = NULL;
  393. return ret;
  394. }
  395. /*
  396. * If every byte of the input contains |entropy_per_bytes| bits of entropy,
  397. * how many bytes does one need to obtain at least |bits| bits of entropy?
  398. */
  399. #define ENTROPY_TO_BYTES(bits, entropy_per_bytes) \
  400. (((bits) + ((entropy_per_bytes) - 1))/(entropy_per_bytes))
  401. /*
  402. * Checks whether the |pool|'s entropy is available to the caller.
  403. * This is the case when entropy count and buffer length are high enough.
  404. * Returns
  405. *
  406. * |entropy| if the entropy count and buffer size is large enough
  407. * 0 otherwise
  408. */
  409. size_t rand_pool_entropy_available(RAND_POOL *pool)
  410. {
  411. if (pool->entropy < pool->requested_entropy)
  412. return 0;
  413. if (pool->len < pool->min_len)
  414. return 0;
  415. return pool->entropy;
  416. }
  417. /*
  418. * Returns the (remaining) amount of entropy needed to fill
  419. * the random pool.
  420. */
  421. size_t rand_pool_entropy_needed(RAND_POOL *pool)
  422. {
  423. if (pool->entropy < pool->requested_entropy)
  424. return pool->requested_entropy - pool->entropy;
  425. return 0;
  426. }
  427. /*
  428. * Returns the number of bytes needed to fill the pool, assuming
  429. * the input has 'entropy_per_byte' entropy bits per byte.
  430. * In case of an error, 0 is returned.
  431. */
  432. size_t rand_pool_bytes_needed(RAND_POOL *pool, unsigned int entropy_per_byte)
  433. {
  434. size_t bytes_needed;
  435. size_t entropy_needed = rand_pool_entropy_needed(pool);
  436. if (entropy_per_byte < 1 || entropy_per_byte > 8) {
  437. RANDerr(RAND_F_RAND_POOL_BYTES_NEEDED, RAND_R_ARGUMENT_OUT_OF_RANGE);
  438. return 0;
  439. }
  440. bytes_needed = ENTROPY_TO_BYTES(entropy_needed, entropy_per_byte);
  441. if (bytes_needed > pool->max_len - pool->len) {
  442. /* not enough space left */
  443. RANDerr(RAND_F_RAND_POOL_BYTES_NEEDED, RAND_R_RANDOM_POOL_OVERFLOW);
  444. return 0;
  445. }
  446. if (pool->len < pool->min_len &&
  447. bytes_needed < pool->min_len - pool->len)
  448. /* to meet the min_len requirement */
  449. bytes_needed = pool->min_len - pool->len;
  450. return bytes_needed;
  451. }
  452. /* Returns the remaining number of bytes available */
  453. size_t rand_pool_bytes_remaining(RAND_POOL *pool)
  454. {
  455. return pool->max_len - pool->len;
  456. }
  457. /*
  458. * Add random bytes to the random pool.
  459. *
  460. * It is expected that the |buffer| contains |len| bytes of
  461. * random input which contains at least |entropy| bits of
  462. * randomness.
  463. *
  464. * Returns 1 if the added amount is adequate, otherwise 0
  465. */
  466. int rand_pool_add(RAND_POOL *pool,
  467. const unsigned char *buffer, size_t len, size_t entropy)
  468. {
  469. if (len > pool->max_len - pool->len) {
  470. RANDerr(RAND_F_RAND_POOL_ADD, RAND_R_ENTROPY_INPUT_TOO_LONG);
  471. return 0;
  472. }
  473. if (len > 0) {
  474. memcpy(pool->buffer + pool->len, buffer, len);
  475. pool->len += len;
  476. pool->entropy += entropy;
  477. }
  478. return 1;
  479. }
  480. /*
  481. * Start to add random bytes to the random pool in-place.
  482. *
  483. * Reserves the next |len| bytes for adding random bytes in-place
  484. * and returns a pointer to the buffer.
  485. * The caller is allowed to copy up to |len| bytes into the buffer.
  486. * If |len| == 0 this is considered a no-op and a NULL pointer
  487. * is returned without producing an error message.
  488. *
  489. * After updating the buffer, rand_pool_add_end() needs to be called
  490. * to finish the udpate operation (see next comment).
  491. */
  492. unsigned char *rand_pool_add_begin(RAND_POOL *pool, size_t len)
  493. {
  494. if (len == 0)
  495. return NULL;
  496. if (len > pool->max_len - pool->len) {
  497. RANDerr(RAND_F_RAND_POOL_ADD_BEGIN, RAND_R_RANDOM_POOL_OVERFLOW);
  498. return NULL;
  499. }
  500. return pool->buffer + pool->len;
  501. }
  502. /*
  503. * Finish to add random bytes to the random pool in-place.
  504. *
  505. * Finishes an in-place update of the random pool started by
  506. * rand_pool_add_begin() (see previous comment).
  507. * It is expected that |len| bytes of random input have been added
  508. * to the buffer which contain at least |entropy| bits of randomness.
  509. * It is allowed to add less bytes than originally reserved.
  510. */
  511. int rand_pool_add_end(RAND_POOL *pool, size_t len, size_t entropy)
  512. {
  513. if (len > pool->max_len - pool->len) {
  514. RANDerr(RAND_F_RAND_POOL_ADD_END, RAND_R_RANDOM_POOL_OVERFLOW);
  515. return 0;
  516. }
  517. if (len > 0) {
  518. pool->len += len;
  519. pool->entropy += entropy;
  520. }
  521. return 1;
  522. }
  523. int RAND_set_rand_method(const RAND_METHOD *meth)
  524. {
  525. if (!RUN_ONCE(&rand_init, do_rand_init))
  526. return 0;
  527. CRYPTO_THREAD_write_lock(rand_meth_lock);
  528. #ifndef OPENSSL_NO_ENGINE
  529. ENGINE_finish(funct_ref);
  530. funct_ref = NULL;
  531. #endif
  532. default_RAND_meth = meth;
  533. CRYPTO_THREAD_unlock(rand_meth_lock);
  534. return 1;
  535. }
  536. const RAND_METHOD *RAND_get_rand_method(void)
  537. {
  538. const RAND_METHOD *tmp_meth = NULL;
  539. if (!RUN_ONCE(&rand_init, do_rand_init))
  540. return NULL;
  541. CRYPTO_THREAD_write_lock(rand_meth_lock);
  542. if (default_RAND_meth == NULL) {
  543. #ifndef OPENSSL_NO_ENGINE
  544. ENGINE *e;
  545. /* If we have an engine that can do RAND, use it. */
  546. if ((e = ENGINE_get_default_RAND()) != NULL
  547. && (tmp_meth = ENGINE_get_RAND(e)) != NULL) {
  548. funct_ref = e;
  549. default_RAND_meth = tmp_meth;
  550. } else {
  551. ENGINE_finish(e);
  552. default_RAND_meth = &rand_meth;
  553. }
  554. #else
  555. default_RAND_meth = &rand_meth;
  556. #endif
  557. }
  558. tmp_meth = default_RAND_meth;
  559. CRYPTO_THREAD_unlock(rand_meth_lock);
  560. return tmp_meth;
  561. }
  562. #ifndef OPENSSL_NO_ENGINE
  563. int RAND_set_rand_engine(ENGINE *engine)
  564. {
  565. const RAND_METHOD *tmp_meth = NULL;
  566. if (!RUN_ONCE(&rand_init, do_rand_init))
  567. return 0;
  568. if (engine != NULL) {
  569. if (!ENGINE_init(engine))
  570. return 0;
  571. tmp_meth = ENGINE_get_RAND(engine);
  572. if (tmp_meth == NULL) {
  573. ENGINE_finish(engine);
  574. return 0;
  575. }
  576. }
  577. CRYPTO_THREAD_write_lock(rand_engine_lock);
  578. /* This function releases any prior ENGINE so call it first */
  579. RAND_set_rand_method(tmp_meth);
  580. funct_ref = engine;
  581. CRYPTO_THREAD_unlock(rand_engine_lock);
  582. return 1;
  583. }
  584. #endif
  585. void RAND_seed(const void *buf, int num)
  586. {
  587. const RAND_METHOD *meth = RAND_get_rand_method();
  588. if (meth->seed != NULL)
  589. meth->seed(buf, num);
  590. }
  591. void RAND_add(const void *buf, int num, double randomness)
  592. {
  593. const RAND_METHOD *meth = RAND_get_rand_method();
  594. if (meth->add != NULL)
  595. meth->add(buf, num, randomness);
  596. }
  597. /*
  598. * This function is not part of RAND_METHOD, so if we're not using
  599. * the default method, then just call RAND_bytes(). Otherwise make
  600. * sure we're instantiated and use the private DRBG.
  601. */
  602. int RAND_priv_bytes(unsigned char *buf, int num)
  603. {
  604. const RAND_METHOD *meth = RAND_get_rand_method();
  605. RAND_DRBG *drbg;
  606. int ret;
  607. if (meth != RAND_OpenSSL())
  608. return RAND_bytes(buf, num);
  609. drbg = RAND_DRBG_get0_private();
  610. if (drbg == NULL)
  611. return 0;
  612. ret = RAND_DRBG_bytes(drbg, buf, num);
  613. return ret;
  614. }
  615. int RAND_bytes(unsigned char *buf, int num)
  616. {
  617. const RAND_METHOD *meth = RAND_get_rand_method();
  618. if (meth->bytes != NULL)
  619. return meth->bytes(buf, num);
  620. RANDerr(RAND_F_RAND_BYTES, RAND_R_FUNC_NOT_IMPLEMENTED);
  621. return -1;
  622. }
  623. #if OPENSSL_API_COMPAT < 0x10100000L
  624. int RAND_pseudo_bytes(unsigned char *buf, int num)
  625. {
  626. const RAND_METHOD *meth = RAND_get_rand_method();
  627. if (meth->pseudorand != NULL)
  628. return meth->pseudorand(buf, num);
  629. return -1;
  630. }
  631. #endif
  632. int RAND_status(void)
  633. {
  634. const RAND_METHOD *meth = RAND_get_rand_method();
  635. if (meth->status != NULL)
  636. return meth->status();
  637. return 0;
  638. }