Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

rand.c 5.6 KiB

Handle RDRAND failures. I mistakenly believed that only RDSEED could fail. However, the Intel manuals state that RDRAND can fail too. I can't actually observe it failing, even with all cores running RDRAND in a tight loop. In any case, the ChaCha20 masking means that it wouldn't be a big deal anyway. Still, this change tests the carry flag after RDRAND and the code falls back to |CRYPTO_sysrand| if RDRAND has a hiccup. (The Intel manuals suggest[1] calling RDRAND in a loop, ten times, before considering it to have failed. But a single failure appears to be such a rare event that the complexity in the asm code doesn't seem worth it.) This change also adds an asm function to fill a buffer with random data. Otherwise the overhead of calling |CRYPTO_rdrand|, and bouncing the data in and out of memory starts to add up. Thanks to W. Mark Kubacki, who may have reported this. (There's some confusion in the bug report.) Before: Did 6148000 RNG (16 bytes) operations in 1000080us: 98.4 MB/s Did 649000 RNG (256 bytes) operations in 1000281us: 166.1 MB/s Did 22000 RNG (8192 bytes) operations in 1033538us: 174.4 MB/s After: Did 6573000 RNG (16 bytes) operations in 1000002us: 105.2 MB/s Did 693000 RNG (256 bytes) operations in 1000127us: 177.4 MB/s Did 24000 RNG (8192 bytes) operations in 1028466us: 191.2 MB/s [1] Intel Reference Manual, section 7.3.17.1. Change-Id: Iba7f82e844ebacef535472a31f2dd749aad1190a Reviewed-on: https://boringssl-review.googlesource.com/5180 Reviewed-by: Adam Langley <agl@google.com>
il y a 9 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/rand.h>
  15. #include <limits.h>
  16. #include <string.h>
  17. #include <openssl/chacha.h>
  18. #include <openssl/mem.h>
  19. #include "internal.h"
  20. #include "../internal.h"
  21. /* It's assumed that the operating system always has an unfailing source of
  22. * entropy which is accessed via |CRYPTO_sysrand|. (If the operating system
  23. * entropy source fails, it's up to |CRYPTO_sysrand| to abort the process—we
  24. * don't try to handle it.)
  25. *
  26. * In addition, the hardware may provide a low-latency RNG. Intel's rdrand
  27. * instruction is the canonical example of this. When a hardware RNG is
  28. * available we don't need to worry about an RNG failure arising from fork()ing
  29. * the process or moving a VM, so we can keep thread-local RNG state and XOR
  30. * the hardware entropy in.
  31. *
  32. * (We assume that the OS entropy is safe from fork()ing and VM duplication.
  33. * This might be a bit of a leap of faith, esp on Windows, but there's nothing
  34. * that we can do about it.) */
  35. /* rand_thread_state contains the per-thread state for the RNG. This is only
  36. * used if the system has support for a hardware RNG. */
  37. struct rand_thread_state {
  38. uint8_t key[32];
  39. uint64_t calls_used;
  40. size_t bytes_used;
  41. uint8_t partial_block[64];
  42. unsigned partial_block_used;
  43. };
  44. /* kMaxCallsPerRefresh is the maximum number of |RAND_bytes| calls that we'll
  45. * serve before reading a new key from the operating system. This only applies
  46. * if we have a hardware RNG. */
  47. static const unsigned kMaxCallsPerRefresh = 1024;
  48. /* kMaxBytesPerRefresh is the maximum number of bytes that we'll return from
  49. * |RAND_bytes| before reading a new key from the operating system. This only
  50. * applies if we have a hardware RNG. */
  51. static const uint64_t kMaxBytesPerRefresh = 1024 * 1024;
  52. /* rand_thread_state_free frees a |rand_thread_state|. This is called when a
  53. * thread exits. */
  54. static void rand_thread_state_free(void *state) {
  55. if (state == NULL) {
  56. return;
  57. }
  58. OPENSSL_cleanse(state, sizeof(struct rand_thread_state));
  59. OPENSSL_free(state);
  60. }
  61. int RAND_bytes(uint8_t *buf, size_t len) {
  62. if (len == 0) {
  63. return 1;
  64. }
  65. if (!CRYPTO_have_hwrand() ||
  66. !CRYPTO_hwrand(buf, len)) {
  67. /* Without a hardware RNG to save us from address-space duplication, the OS
  68. * entropy is used directly. */
  69. CRYPTO_sysrand(buf, len);
  70. return 1;
  71. }
  72. struct rand_thread_state *state =
  73. CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_RAND);
  74. if (state == NULL) {
  75. state = OPENSSL_malloc(sizeof(struct rand_thread_state));
  76. if (state == NULL ||
  77. !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_RAND, state,
  78. rand_thread_state_free)) {
  79. CRYPTO_sysrand(buf, len);
  80. return 1;
  81. }
  82. memset(state->partial_block, 0, sizeof(state->partial_block));
  83. state->calls_used = kMaxCallsPerRefresh;
  84. }
  85. if (state->calls_used >= kMaxCallsPerRefresh ||
  86. state->bytes_used >= kMaxBytesPerRefresh) {
  87. CRYPTO_sysrand(state->key, sizeof(state->key));
  88. state->calls_used = 0;
  89. state->bytes_used = 0;
  90. state->partial_block_used = sizeof(state->partial_block);
  91. }
  92. if (len >= sizeof(state->partial_block)) {
  93. size_t remaining = len;
  94. while (remaining > 0) {
  95. // kMaxBytesPerCall is only 2GB, while ChaCha can handle 256GB. But this
  96. // is sufficient and easier on 32-bit.
  97. static const size_t kMaxBytesPerCall = 0x80000000;
  98. size_t todo = remaining;
  99. if (todo > kMaxBytesPerCall) {
  100. todo = kMaxBytesPerCall;
  101. }
  102. CRYPTO_chacha_20(buf, buf, todo, state->key,
  103. (uint8_t *)&state->calls_used, 0);
  104. buf += todo;
  105. remaining -= todo;
  106. state->calls_used++;
  107. }
  108. } else {
  109. if (sizeof(state->partial_block) - state->partial_block_used < len) {
  110. CRYPTO_chacha_20(state->partial_block, state->partial_block,
  111. sizeof(state->partial_block), state->key,
  112. (uint8_t *)&state->calls_used, 0);
  113. state->partial_block_used = 0;
  114. }
  115. unsigned i;
  116. for (i = 0; i < len; i++) {
  117. buf[i] ^= state->partial_block[state->partial_block_used++];
  118. }
  119. state->calls_used++;
  120. }
  121. state->bytes_used += len;
  122. return 1;
  123. }
  124. int RAND_pseudo_bytes(uint8_t *buf, size_t len) {
  125. return RAND_bytes(buf, len);
  126. }
  127. void RAND_seed(const void *buf, int num) {}
  128. int RAND_load_file(const char *path, long num) {
  129. if (num < 0) { /* read the "whole file" */
  130. return 1;
  131. } else if (num <= INT_MAX) {
  132. return (int) num;
  133. } else {
  134. return INT_MAX;
  135. }
  136. }
  137. void RAND_add(const void *buf, int num, double entropy) {}
  138. int RAND_egd(const char *path) {
  139. return 255;
  140. }
  141. int RAND_poll(void) {
  142. return 1;
  143. }
  144. int RAND_status(void) {
  145. return 1;
  146. }
  147. static const struct rand_meth_st kSSLeayMethod = {
  148. RAND_seed,
  149. RAND_bytes,
  150. RAND_cleanup,
  151. RAND_add,
  152. RAND_pseudo_bytes,
  153. RAND_status,
  154. };
  155. RAND_METHOD *RAND_SSLeay(void) {
  156. return (RAND_METHOD*) &kSSLeayMethod;
  157. }
  158. void RAND_set_rand_method(const RAND_METHOD *method) {}