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.
 
 
 
 
 
 

188 line
5.6 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 <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) {}