No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

245 líneas
6.9 KiB

  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 <assert.h>
  16. #include <limits.h>
  17. #include <string.h>
  18. #include <openssl/chacha.h>
  19. #include <openssl/cpu.h>
  20. #include <openssl/mem.h>
  21. #include "internal.h"
  22. #include "../internal.h"
  23. /* It's assumed that the operating system always has an unfailing source of
  24. * entropy which is accessed via |CRYPTO_sysrand|. (If the operating system
  25. * entropy source fails, it's up to |CRYPTO_sysrand| to abort the process—we
  26. * don't try to handle it.)
  27. *
  28. * In addition, the hardware may provide a low-latency RNG. Intel's rdrand
  29. * instruction is the canonical example of this. When a hardware RNG is
  30. * available we don't need to worry about an RNG failure arising from fork()ing
  31. * the process or moving a VM, so we can keep thread-local RNG state and XOR
  32. * the hardware entropy in.
  33. *
  34. * (We assume that the OS entropy is safe from fork()ing and VM duplication.
  35. * This might be a bit of a leap of faith, esp on Windows, but there's nothing
  36. * that we can do about it.) */
  37. /* rand_thread_state contains the per-thread state for the RNG. This is only
  38. * used if the system has support for a hardware RNG. */
  39. struct rand_thread_state {
  40. uint8_t key[32];
  41. uint64_t calls_used;
  42. size_t bytes_used;
  43. uint8_t partial_block[64];
  44. unsigned partial_block_used;
  45. };
  46. /* kMaxCallsPerRefresh is the maximum number of |RAND_bytes| calls that we'll
  47. * serve before reading a new key from the operating system. This only applies
  48. * if we have a hardware RNG. */
  49. static const unsigned kMaxCallsPerRefresh = 1024;
  50. /* kMaxBytesPerRefresh is the maximum number of bytes that we'll return from
  51. * |RAND_bytes| before reading a new key from the operating system. This only
  52. * applies if we have a hardware RNG. */
  53. static const uint64_t kMaxBytesPerRefresh = 1024 * 1024;
  54. /* rand_thread_state_free frees a |rand_thread_state|. This is called when a
  55. * thread exits. */
  56. static void rand_thread_state_free(void *state) {
  57. if (state == NULL) {
  58. return;
  59. }
  60. OPENSSL_cleanse(state, sizeof(struct rand_thread_state));
  61. OPENSSL_free(state);
  62. }
  63. #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \
  64. !defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
  65. /* These functions are defined in asm/rdrand-x86_64.pl */
  66. extern int CRYPTO_rdrand(uint8_t out[8]);
  67. extern int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len);
  68. static int have_rdrand(void) {
  69. return (OPENSSL_ia32cap_P[1] & (1u << 30)) != 0;
  70. }
  71. static int hwrand(uint8_t *buf, size_t len) {
  72. if (!have_rdrand()) {
  73. return 0;
  74. }
  75. const size_t len_multiple8 = len & ~7;
  76. if (!CRYPTO_rdrand_multiple8_buf(buf, len_multiple8)) {
  77. return 0;
  78. }
  79. len -= len_multiple8;
  80. if (len != 0) {
  81. assert(len < 8);
  82. uint8_t rand_buf[8];
  83. if (!CRYPTO_rdrand(rand_buf)) {
  84. return 0;
  85. }
  86. OPENSSL_memcpy(buf + len_multiple8, rand_buf, len);
  87. }
  88. return 1;
  89. }
  90. #else
  91. static int hwrand(uint8_t *buf, size_t len) {
  92. return 0;
  93. }
  94. #endif
  95. int RAND_bytes(uint8_t *buf, size_t len) {
  96. if (len == 0) {
  97. return 1;
  98. }
  99. if (!hwrand(buf, len)) {
  100. /* Without a hardware RNG to save us from address-space duplication, the OS
  101. * entropy is used directly. */
  102. CRYPTO_sysrand(buf, len);
  103. return 1;
  104. }
  105. struct rand_thread_state *state =
  106. CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_RAND);
  107. if (state == NULL) {
  108. state = OPENSSL_malloc(sizeof(struct rand_thread_state));
  109. if (state == NULL ||
  110. !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_RAND, state,
  111. rand_thread_state_free)) {
  112. CRYPTO_sysrand(buf, len);
  113. return 1;
  114. }
  115. OPENSSL_memset(state->partial_block, 0, sizeof(state->partial_block));
  116. state->calls_used = kMaxCallsPerRefresh;
  117. }
  118. if (state->calls_used >= kMaxCallsPerRefresh ||
  119. state->bytes_used >= kMaxBytesPerRefresh) {
  120. CRYPTO_sysrand(state->key, sizeof(state->key));
  121. state->calls_used = 0;
  122. state->bytes_used = 0;
  123. state->partial_block_used = sizeof(state->partial_block);
  124. }
  125. if (len >= sizeof(state->partial_block)) {
  126. size_t remaining = len;
  127. while (remaining > 0) {
  128. /* kMaxBytesPerCall is only 2GB, while ChaCha can handle 256GB. But this
  129. * is sufficient and easier on 32-bit. */
  130. static const size_t kMaxBytesPerCall = 0x80000000;
  131. size_t todo = remaining;
  132. if (todo > kMaxBytesPerCall) {
  133. todo = kMaxBytesPerCall;
  134. }
  135. uint8_t nonce[12];
  136. OPENSSL_memset(nonce, 0, 4);
  137. OPENSSL_memcpy(nonce + 4, &state->calls_used, sizeof(state->calls_used));
  138. CRYPTO_chacha_20(buf, buf, todo, state->key, nonce, 0);
  139. buf += todo;
  140. remaining -= todo;
  141. state->calls_used++;
  142. }
  143. } else {
  144. if (sizeof(state->partial_block) - state->partial_block_used < len) {
  145. uint8_t nonce[12];
  146. OPENSSL_memset(nonce, 0, 4);
  147. OPENSSL_memcpy(nonce + 4, &state->calls_used, sizeof(state->calls_used));
  148. CRYPTO_chacha_20(state->partial_block, state->partial_block,
  149. sizeof(state->partial_block), state->key, nonce, 0);
  150. state->partial_block_used = 0;
  151. }
  152. unsigned i;
  153. for (i = 0; i < len; i++) {
  154. buf[i] ^= state->partial_block[state->partial_block_used++];
  155. }
  156. state->calls_used++;
  157. }
  158. state->bytes_used += len;
  159. return 1;
  160. }
  161. int RAND_pseudo_bytes(uint8_t *buf, size_t len) {
  162. return RAND_bytes(buf, len);
  163. }
  164. void RAND_seed(const void *buf, int num) {
  165. /* OpenSSH calls |RAND_seed| before jailing on the assumption that any needed
  166. * file descriptors etc will be opened. */
  167. uint8_t unused;
  168. RAND_bytes(&unused, sizeof(unused));
  169. }
  170. int RAND_load_file(const char *path, long num) {
  171. if (num < 0) { /* read the "whole file" */
  172. return 1;
  173. } else if (num <= INT_MAX) {
  174. return (int) num;
  175. } else {
  176. return INT_MAX;
  177. }
  178. }
  179. const char *RAND_file_name(char *buf, size_t num) { return NULL; }
  180. void RAND_add(const void *buf, int num, double entropy) {}
  181. int RAND_egd(const char *path) {
  182. return 255;
  183. }
  184. int RAND_poll(void) {
  185. return 1;
  186. }
  187. int RAND_status(void) {
  188. return 1;
  189. }
  190. static const struct rand_meth_st kSSLeayMethod = {
  191. RAND_seed,
  192. RAND_bytes,
  193. RAND_cleanup,
  194. RAND_add,
  195. RAND_pseudo_bytes,
  196. RAND_status,
  197. };
  198. RAND_METHOD *RAND_SSLeay(void) {
  199. return (RAND_METHOD*) &kSSLeayMethod;
  200. }
  201. void RAND_set_rand_method(const RAND_METHOD *method) {}
  202. void RAND_cleanup(void) {}