You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

242 lines
6.8 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. /* These functions are defined in asm/rdrand-x86_64.pl */
  65. extern int CRYPTO_rdrand(uint8_t out[8]);
  66. extern int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len);
  67. static int have_rdrand(void) {
  68. return (OPENSSL_ia32cap_P[1] & (1u << 30)) != 0;
  69. }
  70. static int hwrand(uint8_t *buf, size_t len) {
  71. if (!have_rdrand()) {
  72. return 0;
  73. }
  74. const size_t len_multiple8 = len & ~7;
  75. if (!CRYPTO_rdrand_multiple8_buf(buf, len_multiple8)) {
  76. return 0;
  77. }
  78. len -= len_multiple8;
  79. if (len != 0) {
  80. assert(len < 8);
  81. uint8_t rand_buf[8];
  82. if (!CRYPTO_rdrand(rand_buf)) {
  83. return 0;
  84. }
  85. memcpy(buf + len_multiple8, rand_buf, len);
  86. }
  87. return 1;
  88. }
  89. #else
  90. static int hwrand(uint8_t *buf, size_t len) {
  91. return 0;
  92. }
  93. #endif
  94. int RAND_bytes(uint8_t *buf, size_t len) {
  95. if (len == 0) {
  96. return 1;
  97. }
  98. if (!hwrand(buf, len)) {
  99. /* Without a hardware RNG to save us from address-space duplication, the OS
  100. * entropy is used directly. */
  101. CRYPTO_sysrand(buf, len);
  102. return 1;
  103. }
  104. struct rand_thread_state *state =
  105. CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_RAND);
  106. if (state == NULL) {
  107. state = OPENSSL_malloc(sizeof(struct rand_thread_state));
  108. if (state == NULL ||
  109. !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_RAND, state,
  110. rand_thread_state_free)) {
  111. CRYPTO_sysrand(buf, len);
  112. return 1;
  113. }
  114. memset(state->partial_block, 0, sizeof(state->partial_block));
  115. state->calls_used = kMaxCallsPerRefresh;
  116. }
  117. if (state->calls_used >= kMaxCallsPerRefresh ||
  118. state->bytes_used >= kMaxBytesPerRefresh) {
  119. CRYPTO_sysrand(state->key, sizeof(state->key));
  120. state->calls_used = 0;
  121. state->bytes_used = 0;
  122. state->partial_block_used = sizeof(state->partial_block);
  123. }
  124. if (len >= sizeof(state->partial_block)) {
  125. size_t remaining = len;
  126. while (remaining > 0) {
  127. /* kMaxBytesPerCall is only 2GB, while ChaCha can handle 256GB. But this
  128. * is sufficient and easier on 32-bit. */
  129. static const size_t kMaxBytesPerCall = 0x80000000;
  130. size_t todo = remaining;
  131. if (todo > kMaxBytesPerCall) {
  132. todo = kMaxBytesPerCall;
  133. }
  134. uint8_t nonce[12];
  135. memset(nonce, 0, 4);
  136. memcpy(nonce + 4, &state->calls_used, sizeof(state->calls_used));
  137. CRYPTO_chacha_20(buf, buf, todo, state->key, nonce, 0);
  138. buf += todo;
  139. remaining -= todo;
  140. state->calls_used++;
  141. }
  142. } else {
  143. if (sizeof(state->partial_block) - state->partial_block_used < len) {
  144. uint8_t nonce[12];
  145. memset(nonce, 0, 4);
  146. memcpy(nonce + 4, &state->calls_used, sizeof(state->calls_used));
  147. CRYPTO_chacha_20(state->partial_block, state->partial_block,
  148. sizeof(state->partial_block), state->key, nonce, 0);
  149. state->partial_block_used = 0;
  150. }
  151. unsigned i;
  152. for (i = 0; i < len; i++) {
  153. buf[i] ^= state->partial_block[state->partial_block_used++];
  154. }
  155. state->calls_used++;
  156. }
  157. state->bytes_used += len;
  158. return 1;
  159. }
  160. int RAND_pseudo_bytes(uint8_t *buf, size_t len) {
  161. return RAND_bytes(buf, len);
  162. }
  163. void RAND_seed(const void *buf, int num) {
  164. /* OpenSSH calls |RAND_seed| before jailing on the assumption that any needed
  165. * file descriptors etc will be opened. */
  166. uint8_t unused;
  167. RAND_bytes(&unused, sizeof(unused));
  168. }
  169. int RAND_load_file(const char *path, long num) {
  170. if (num < 0) { /* read the "whole file" */
  171. return 1;
  172. } else if (num <= INT_MAX) {
  173. return (int) num;
  174. } else {
  175. return INT_MAX;
  176. }
  177. }
  178. const char *RAND_file_name(char *buf, size_t num) { return NULL; }
  179. void RAND_add(const void *buf, int num, double entropy) {}
  180. int RAND_egd(const char *path) {
  181. return 255;
  182. }
  183. int RAND_poll(void) {
  184. return 1;
  185. }
  186. int RAND_status(void) {
  187. return 1;
  188. }
  189. static const struct rand_meth_st kSSLeayMethod = {
  190. RAND_seed,
  191. RAND_bytes,
  192. RAND_cleanup,
  193. RAND_add,
  194. RAND_pseudo_bytes,
  195. RAND_status,
  196. };
  197. RAND_METHOD *RAND_SSLeay(void) {
  198. return (RAND_METHOD*) &kSSLeayMethod;
  199. }
  200. void RAND_set_rand_method(const RAND_METHOD *method) {}