md_rand.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*
  2. * Copyright 1995-2016 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 <string.h>
  11. #include "e_os.h"
  12. #if !(defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_DSPBIOS))
  13. # include <sys/time.h>
  14. #endif
  15. #if defined(OPENSSL_SYS_VXWORKS)
  16. # include <time.h>
  17. #endif
  18. #include <openssl/opensslconf.h>
  19. #include <openssl/crypto.h>
  20. #include <openssl/rand.h>
  21. #include <openssl/async.h>
  22. #include "rand_lcl.h"
  23. #include <openssl/err.h>
  24. #include <internal/thread_once.h>
  25. #if defined(BN_DEBUG) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
  26. # define PREDICT
  27. #endif
  28. /* #define PREDICT 1 */
  29. #define STATE_SIZE 1023
  30. static size_t state_num = 0, state_index = 0;
  31. static unsigned char state[STATE_SIZE + MD_DIGEST_LENGTH];
  32. static unsigned char md[MD_DIGEST_LENGTH];
  33. static long md_count[2] = { 0, 0 };
  34. static double entropy = 0;
  35. static int initialized = 0;
  36. static CRYPTO_RWLOCK *rand_lock = NULL;
  37. static CRYPTO_RWLOCK *rand_tmp_lock = NULL;
  38. static CRYPTO_ONCE rand_lock_init = CRYPTO_ONCE_STATIC_INIT;
  39. /* May be set only when a thread holds rand_lock (to prevent double locking) */
  40. static unsigned int crypto_lock_rand = 0;
  41. /* access to locking_threadid is synchronized by rand_tmp_lock */
  42. /* valid iff crypto_lock_rand is set */
  43. static CRYPTO_THREAD_ID locking_threadid;
  44. #ifdef PREDICT
  45. int rand_predictable = 0;
  46. #endif
  47. static int rand_hw_seed(EVP_MD_CTX *ctx);
  48. static void rand_cleanup(void);
  49. static int rand_seed(const void *buf, int num);
  50. static int rand_add(const void *buf, int num, double add_entropy);
  51. static int rand_bytes(unsigned char *buf, int num, int pseudo);
  52. static int rand_nopseudo_bytes(unsigned char *buf, int num);
  53. #if OPENSSL_API_COMPAT < 0x10100000L
  54. static int rand_pseudo_bytes(unsigned char *buf, int num);
  55. #endif
  56. static int rand_status(void);
  57. static RAND_METHOD rand_meth = {
  58. rand_seed,
  59. rand_nopseudo_bytes,
  60. rand_cleanup,
  61. rand_add,
  62. #if OPENSSL_API_COMPAT < 0x10100000L
  63. rand_pseudo_bytes,
  64. #else
  65. NULL,
  66. #endif
  67. rand_status
  68. };
  69. DEFINE_RUN_ONCE_STATIC(do_rand_lock_init)
  70. {
  71. OPENSSL_init_crypto(0, NULL);
  72. rand_lock = CRYPTO_THREAD_lock_new();
  73. rand_tmp_lock = CRYPTO_THREAD_lock_new();
  74. return rand_lock != NULL && rand_tmp_lock != NULL;
  75. }
  76. RAND_METHOD *RAND_OpenSSL(void)
  77. {
  78. return (&rand_meth);
  79. }
  80. static void rand_cleanup(void)
  81. {
  82. OPENSSL_cleanse(state, sizeof(state));
  83. state_num = 0;
  84. state_index = 0;
  85. OPENSSL_cleanse(md, MD_DIGEST_LENGTH);
  86. md_count[0] = 0;
  87. md_count[1] = 0;
  88. entropy = 0;
  89. initialized = 0;
  90. CRYPTO_THREAD_lock_free(rand_lock);
  91. CRYPTO_THREAD_lock_free(rand_tmp_lock);
  92. }
  93. static int rand_add(const void *buf, int num, double add)
  94. {
  95. int i, j, k, st_idx;
  96. long md_c[2];
  97. unsigned char local_md[MD_DIGEST_LENGTH];
  98. EVP_MD_CTX *m;
  99. int do_not_lock;
  100. int rv = 0;
  101. if (!num)
  102. return 1;
  103. #ifdef PREDICT
  104. if (rand_predictable)
  105. return 1;
  106. #endif
  107. /*
  108. * (Based on the rand(3) manpage)
  109. *
  110. * The input is chopped up into units of 20 bytes (or less for
  111. * the last block). Each of these blocks is run through the hash
  112. * function as follows: The data passed to the hash function
  113. * is the current 'md', the same number of bytes from the 'state'
  114. * (the location determined by in incremented looping index) as
  115. * the current 'block', the new key data 'block', and 'count'
  116. * (which is incremented after each use).
  117. * The result of this is kept in 'md' and also xored into the
  118. * 'state' at the same locations that were used as input into the
  119. * hash function.
  120. */
  121. m = EVP_MD_CTX_new();
  122. if (m == NULL)
  123. goto err;
  124. if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init))
  125. goto err;
  126. /* check if we already have the lock */
  127. if (crypto_lock_rand) {
  128. CRYPTO_THREAD_ID cur = CRYPTO_THREAD_get_current_id();
  129. CRYPTO_THREAD_read_lock(rand_tmp_lock);
  130. do_not_lock = CRYPTO_THREAD_compare_id(locking_threadid, cur);
  131. CRYPTO_THREAD_unlock(rand_tmp_lock);
  132. } else
  133. do_not_lock = 0;
  134. if (!do_not_lock)
  135. CRYPTO_THREAD_write_lock(rand_lock);
  136. st_idx = state_index;
  137. /*
  138. * use our own copies of the counters so that even if a concurrent thread
  139. * seeds with exactly the same data and uses the same subarray there's
  140. * _some_ difference
  141. */
  142. md_c[0] = md_count[0];
  143. md_c[1] = md_count[1];
  144. memcpy(local_md, md, sizeof md);
  145. /* state_index <= state_num <= STATE_SIZE */
  146. state_index += num;
  147. if (state_index >= STATE_SIZE) {
  148. state_index %= STATE_SIZE;
  149. state_num = STATE_SIZE;
  150. } else if (state_num < STATE_SIZE) {
  151. if (state_index > state_num)
  152. state_num = state_index;
  153. }
  154. /* state_index <= state_num <= STATE_SIZE */
  155. /*
  156. * state[st_idx], ..., state[(st_idx + num - 1) % STATE_SIZE] are what we
  157. * will use now, but other threads may use them as well
  158. */
  159. md_count[1] += (num / MD_DIGEST_LENGTH) + (num % MD_DIGEST_LENGTH > 0);
  160. if (!do_not_lock)
  161. CRYPTO_THREAD_unlock(rand_lock);
  162. for (i = 0; i < num; i += MD_DIGEST_LENGTH) {
  163. j = (num - i);
  164. j = (j > MD_DIGEST_LENGTH) ? MD_DIGEST_LENGTH : j;
  165. if (!MD_Init(m))
  166. goto err;
  167. if (!MD_Update(m, local_md, MD_DIGEST_LENGTH))
  168. goto err;
  169. k = (st_idx + j) - STATE_SIZE;
  170. if (k > 0) {
  171. if (!MD_Update(m, &(state[st_idx]), j - k))
  172. goto err;
  173. if (!MD_Update(m, &(state[0]), k))
  174. goto err;
  175. } else if (!MD_Update(m, &(state[st_idx]), j))
  176. goto err;
  177. /* DO NOT REMOVE THE FOLLOWING CALL TO MD_Update()! */
  178. if (!MD_Update(m, buf, j))
  179. goto err;
  180. /*
  181. * We know that line may cause programs such as purify and valgrind
  182. * to complain about use of uninitialized data. The problem is not,
  183. * it's with the caller. Removing that line will make sure you get
  184. * really bad randomness and thereby other problems such as very
  185. * insecure keys.
  186. */
  187. if (!MD_Update(m, (unsigned char *)&(md_c[0]), sizeof(md_c)))
  188. goto err;
  189. if (!MD_Final(m, local_md))
  190. goto err;
  191. md_c[1]++;
  192. buf = (const char *)buf + j;
  193. for (k = 0; k < j; k++) {
  194. /*
  195. * Parallel threads may interfere with this, but always each byte
  196. * of the new state is the XOR of some previous value of its and
  197. * local_md (intermediate values may be lost). Alway using locking
  198. * could hurt performance more than necessary given that
  199. * conflicts occur only when the total seeding is longer than the
  200. * random state.
  201. */
  202. state[st_idx++] ^= local_md[k];
  203. if (st_idx >= STATE_SIZE)
  204. st_idx = 0;
  205. }
  206. }
  207. if (!do_not_lock)
  208. CRYPTO_THREAD_write_lock(rand_lock);
  209. /*
  210. * Don't just copy back local_md into md -- this could mean that other
  211. * thread's seeding remains without effect (except for the incremented
  212. * counter). By XORing it we keep at least as much entropy as fits into
  213. * md.
  214. */
  215. for (k = 0; k < (int)sizeof(md); k++) {
  216. md[k] ^= local_md[k];
  217. }
  218. if (entropy < ENTROPY_NEEDED) /* stop counting when we have enough */
  219. entropy += add;
  220. if (!do_not_lock)
  221. CRYPTO_THREAD_unlock(rand_lock);
  222. rv = 1;
  223. err:
  224. EVP_MD_CTX_free(m);
  225. return rv;
  226. }
  227. static int rand_seed(const void *buf, int num)
  228. {
  229. return rand_add(buf, num, (double)num);
  230. }
  231. static int rand_bytes(unsigned char *buf, int num, int pseudo)
  232. {
  233. static volatile int stirred_pool = 0;
  234. int i, j, k;
  235. size_t num_ceil, st_idx, st_num;
  236. int ok;
  237. long md_c[2];
  238. unsigned char local_md[MD_DIGEST_LENGTH];
  239. EVP_MD_CTX *m;
  240. #ifndef GETPID_IS_MEANINGLESS
  241. pid_t curr_pid = getpid();
  242. #endif
  243. time_t curr_time = time(NULL);
  244. int do_stir_pool = 0;
  245. /* time value for various platforms */
  246. #ifdef OPENSSL_SYS_WIN32
  247. FILETIME tv;
  248. # ifdef _WIN32_WCE
  249. SYSTEMTIME t;
  250. GetSystemTime(&t);
  251. SystemTimeToFileTime(&t, &tv);
  252. # else
  253. GetSystemTimeAsFileTime(&tv);
  254. # endif
  255. #elif defined(OPENSSL_SYS_VXWORKS)
  256. struct timespec tv;
  257. clock_gettime(CLOCK_REALTIME, &ts);
  258. #elif defined(OPENSSL_SYS_DSPBIOS)
  259. unsigned long long tv, OPENSSL_rdtsc();
  260. tv = OPENSSL_rdtsc();
  261. #else
  262. struct timeval tv;
  263. gettimeofday(&tv, NULL);
  264. #endif
  265. #ifdef PREDICT
  266. if (rand_predictable) {
  267. unsigned char val = 1;
  268. for (i = 0; i < num; i++)
  269. buf[i] = val++;
  270. return (1);
  271. }
  272. #endif
  273. if (num <= 0)
  274. return 1;
  275. m = EVP_MD_CTX_new();
  276. if (m == NULL)
  277. goto err_mem;
  278. /* round upwards to multiple of MD_DIGEST_LENGTH/2 */
  279. num_ceil =
  280. (1 + (num - 1) / (MD_DIGEST_LENGTH / 2)) * (MD_DIGEST_LENGTH / 2);
  281. /*
  282. * (Based on the rand(3) manpage:)
  283. *
  284. * For each group of 10 bytes (or less), we do the following:
  285. *
  286. * Input into the hash function the local 'md' (which is initialized from
  287. * the global 'md' before any bytes are generated), the bytes that are to
  288. * be overwritten by the random bytes, and bytes from the 'state'
  289. * (incrementing looping index). From this digest output (which is kept
  290. * in 'md'), the top (up to) 10 bytes are returned to the caller and the
  291. * bottom 10 bytes are xored into the 'state'.
  292. *
  293. * Finally, after we have finished 'num' random bytes for the
  294. * caller, 'count' (which is incremented) and the local and global 'md'
  295. * are fed into the hash function and the results are kept in the
  296. * global 'md'.
  297. */
  298. if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init))
  299. goto err_mem;
  300. CRYPTO_THREAD_write_lock(rand_lock);
  301. /*
  302. * We could end up in an async engine while holding this lock so ensure
  303. * we don't pause and cause a deadlock
  304. */
  305. ASYNC_block_pause();
  306. /* prevent rand_bytes() from trying to obtain the lock again */
  307. CRYPTO_THREAD_write_lock(rand_tmp_lock);
  308. locking_threadid = CRYPTO_THREAD_get_current_id();
  309. CRYPTO_THREAD_unlock(rand_tmp_lock);
  310. crypto_lock_rand = 1;
  311. if (!initialized) {
  312. RAND_poll();
  313. initialized = 1;
  314. }
  315. if (!stirred_pool)
  316. do_stir_pool = 1;
  317. ok = (entropy >= ENTROPY_NEEDED);
  318. if (!ok) {
  319. /*
  320. * If the PRNG state is not yet unpredictable, then seeing the PRNG
  321. * output may help attackers to determine the new state; thus we have
  322. * to decrease the entropy estimate. Once we've had enough initial
  323. * seeding we don't bother to adjust the entropy count, though,
  324. * because we're not ambitious to provide *information-theoretic*
  325. * randomness. NOTE: This approach fails if the program forks before
  326. * we have enough entropy. Entropy should be collected in a separate
  327. * input pool and be transferred to the output pool only when the
  328. * entropy limit has been reached.
  329. */
  330. entropy -= num;
  331. if (entropy < 0)
  332. entropy = 0;
  333. }
  334. if (do_stir_pool) {
  335. /*
  336. * In the output function only half of 'md' remains secret, so we
  337. * better make sure that the required entropy gets 'evenly
  338. * distributed' through 'state', our randomness pool. The input
  339. * function (rand_add) chains all of 'md', which makes it more
  340. * suitable for this purpose.
  341. */
  342. int n = STATE_SIZE; /* so that the complete pool gets accessed */
  343. while (n > 0) {
  344. #if MD_DIGEST_LENGTH > 20
  345. # error "Please adjust DUMMY_SEED."
  346. #endif
  347. #define DUMMY_SEED "...................." /* at least MD_DIGEST_LENGTH */
  348. /*
  349. * Note that the seed does not matter, it's just that
  350. * rand_add expects to have something to hash.
  351. */
  352. rand_add(DUMMY_SEED, MD_DIGEST_LENGTH, 0.0);
  353. n -= MD_DIGEST_LENGTH;
  354. }
  355. if (ok)
  356. stirred_pool = 1;
  357. }
  358. st_idx = state_index;
  359. st_num = state_num;
  360. md_c[0] = md_count[0];
  361. md_c[1] = md_count[1];
  362. memcpy(local_md, md, sizeof md);
  363. state_index += num_ceil;
  364. if (state_index > state_num)
  365. state_index %= state_num;
  366. /*
  367. * state[st_idx], ..., state[(st_idx + num_ceil - 1) % st_num] are now
  368. * ours (but other threads may use them too)
  369. */
  370. md_count[0] += 1;
  371. /* before unlocking, we must clear 'crypto_lock_rand' */
  372. crypto_lock_rand = 0;
  373. ASYNC_unblock_pause();
  374. CRYPTO_THREAD_unlock(rand_lock);
  375. while (num > 0) {
  376. /* num_ceil -= MD_DIGEST_LENGTH/2 */
  377. j = (num >= MD_DIGEST_LENGTH / 2) ? MD_DIGEST_LENGTH / 2 : num;
  378. num -= j;
  379. if (!MD_Init(m))
  380. goto err;
  381. #ifndef GETPID_IS_MEANINGLESS
  382. if (curr_pid) { /* just in the first iteration to save time */
  383. if (!MD_Update(m, (unsigned char *)&curr_pid, sizeof curr_pid))
  384. goto err;
  385. curr_pid = 0;
  386. }
  387. #endif
  388. if (curr_time) { /* just in the first iteration to save time */
  389. if (!MD_Update(m, (unsigned char *)&curr_time, sizeof curr_time))
  390. goto err;
  391. if (!MD_Update(m, (unsigned char *)&tv, sizeof tv))
  392. goto err;
  393. curr_time = 0;
  394. if (!rand_hw_seed(m))
  395. goto err;
  396. }
  397. if (!MD_Update(m, local_md, MD_DIGEST_LENGTH))
  398. goto err;
  399. if (!MD_Update(m, (unsigned char *)&(md_c[0]), sizeof(md_c)))
  400. goto err;
  401. k = (st_idx + MD_DIGEST_LENGTH / 2) - st_num;
  402. if (k > 0) {
  403. if (!MD_Update(m, &(state[st_idx]), MD_DIGEST_LENGTH / 2 - k))
  404. goto err;
  405. if (!MD_Update(m, &(state[0]), k))
  406. goto err;
  407. } else if (!MD_Update(m, &(state[st_idx]), MD_DIGEST_LENGTH / 2))
  408. goto err;
  409. if (!MD_Final(m, local_md))
  410. goto err;
  411. for (i = 0; i < MD_DIGEST_LENGTH / 2; i++) {
  412. /* may compete with other threads */
  413. state[st_idx++] ^= local_md[i];
  414. if (st_idx >= st_num)
  415. st_idx = 0;
  416. if (i < j)
  417. *(buf++) = local_md[i + MD_DIGEST_LENGTH / 2];
  418. }
  419. }
  420. if (!MD_Init(m)
  421. || !MD_Update(m, (unsigned char *)&(md_c[0]), sizeof(md_c))
  422. || !MD_Update(m, local_md, MD_DIGEST_LENGTH))
  423. goto err;
  424. CRYPTO_THREAD_write_lock(rand_lock);
  425. /*
  426. * Prevent deadlocks if we end up in an async engine
  427. */
  428. ASYNC_block_pause();
  429. if (!MD_Update(m, md, MD_DIGEST_LENGTH) || !MD_Final(m, md)) {
  430. CRYPTO_THREAD_unlock(rand_lock);
  431. goto err;
  432. }
  433. ASYNC_unblock_pause();
  434. CRYPTO_THREAD_unlock(rand_lock);
  435. EVP_MD_CTX_free(m);
  436. if (ok)
  437. return (1);
  438. else if (pseudo)
  439. return 0;
  440. else {
  441. RANDerr(RAND_F_RAND_BYTES, RAND_R_PRNG_NOT_SEEDED);
  442. ERR_add_error_data(1, "You need to read the OpenSSL FAQ, "
  443. "https://www.openssl.org/docs/faq.html");
  444. return (0);
  445. }
  446. err:
  447. RANDerr(RAND_F_RAND_BYTES, ERR_R_EVP_LIB);
  448. EVP_MD_CTX_free(m);
  449. return 0;
  450. err_mem:
  451. RANDerr(RAND_F_RAND_BYTES, ERR_R_MALLOC_FAILURE);
  452. EVP_MD_CTX_free(m);
  453. return 0;
  454. }
  455. static int rand_nopseudo_bytes(unsigned char *buf, int num)
  456. {
  457. return rand_bytes(buf, num, 0);
  458. }
  459. #if OPENSSL_API_COMPAT < 0x10100000L
  460. /*
  461. * pseudo-random bytes that are guaranteed to be unique but not unpredictable
  462. */
  463. static int rand_pseudo_bytes(unsigned char *buf, int num)
  464. {
  465. return rand_bytes(buf, num, 1);
  466. }
  467. #endif
  468. static int rand_status(void)
  469. {
  470. CRYPTO_THREAD_ID cur;
  471. int ret;
  472. int do_not_lock;
  473. if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init))
  474. return 0;
  475. cur = CRYPTO_THREAD_get_current_id();
  476. /*
  477. * check if we already have the lock (could happen if a RAND_poll()
  478. * implementation calls RAND_status())
  479. */
  480. if (crypto_lock_rand) {
  481. CRYPTO_THREAD_read_lock(rand_tmp_lock);
  482. do_not_lock = CRYPTO_THREAD_compare_id(locking_threadid, cur);
  483. CRYPTO_THREAD_unlock(rand_tmp_lock);
  484. } else
  485. do_not_lock = 0;
  486. if (!do_not_lock) {
  487. CRYPTO_THREAD_write_lock(rand_lock);
  488. /*
  489. * Prevent deadlocks in case we end up in an async engine
  490. */
  491. ASYNC_block_pause();
  492. /*
  493. * prevent rand_bytes() from trying to obtain the lock again
  494. */
  495. CRYPTO_THREAD_write_lock(rand_tmp_lock);
  496. locking_threadid = cur;
  497. CRYPTO_THREAD_unlock(rand_tmp_lock);
  498. crypto_lock_rand = 1;
  499. }
  500. if (!initialized) {
  501. RAND_poll();
  502. initialized = 1;
  503. }
  504. ret = entropy >= ENTROPY_NEEDED;
  505. if (!do_not_lock) {
  506. /* before unlocking, we must clear 'crypto_lock_rand' */
  507. crypto_lock_rand = 0;
  508. ASYNC_unblock_pause();
  509. CRYPTO_THREAD_unlock(rand_lock);
  510. }
  511. return ret;
  512. }
  513. /*
  514. * rand_hw_seed: get seed data from any available hardware RNG. only
  515. * currently supports rdrand.
  516. */
  517. /* Adapted from eng_rdrand.c */
  518. #if (defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
  519. defined(__x86_64) || defined(__x86_64__) || \
  520. defined(_M_AMD64) || defined (_M_X64)) && defined(OPENSSL_CPUID_OBJ) \
  521. && !defined(OPENSSL_NO_RDRAND)
  522. # define RDRAND_CALLS 4
  523. size_t OPENSSL_ia32_rdrand(void);
  524. extern unsigned int OPENSSL_ia32cap_P[];
  525. static int rand_hw_seed(EVP_MD_CTX *ctx)
  526. {
  527. int i;
  528. if (!(OPENSSL_ia32cap_P[1] & (1 << (62 - 32))))
  529. return 1;
  530. for (i = 0; i < RDRAND_CALLS; i++) {
  531. size_t rnd;
  532. rnd = OPENSSL_ia32_rdrand();
  533. if (rnd == 0)
  534. return 1;
  535. if (!MD_Update(ctx, (unsigned char *)&rnd, sizeof(size_t)))
  536. return 0;
  537. }
  538. return 1;
  539. }
  540. /* XOR an existing buffer with random data */
  541. void rand_hw_xor(unsigned char *buf, size_t num)
  542. {
  543. size_t rnd;
  544. if (!(OPENSSL_ia32cap_P[1] & (1 << (62 - 32))))
  545. return;
  546. while (num >= sizeof(size_t)) {
  547. rnd = OPENSSL_ia32_rdrand();
  548. if (rnd == 0)
  549. return;
  550. *((size_t *)buf) ^= rnd;
  551. buf += sizeof(size_t);
  552. num -= sizeof(size_t);
  553. }
  554. if (num) {
  555. rnd = OPENSSL_ia32_rdrand();
  556. if (rnd == 0)
  557. return;
  558. while (num) {
  559. *buf ^= rnd & 0xff;
  560. rnd >>= 8;
  561. buf++;
  562. num--;
  563. }
  564. }
  565. }
  566. #else
  567. static int rand_hw_seed(EVP_MD_CTX *ctx)
  568. {
  569. return 1;
  570. }
  571. void rand_hw_xor(unsigned char *buf, size_t num)
  572. {
  573. return;
  574. }
  575. #endif