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.
 
 
 
 
 
 

161 lines
5.2 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 <string.h>
  16. #include <openssl/mem.h>
  17. #include "internal.h"
  18. #include "../internal.h"
  19. /* It's assumed that the operating system always has an unfailing source of
  20. * entropy which is accessed via |CRYPTO_sysrand|. (If the operating system
  21. * entropy source fails, it's up to |CRYPTO_sysrand| to abort the process—we
  22. * don't try to handle it.)
  23. *
  24. * In addition, the hardware may provide a low-latency RNG. Intel's rdrand
  25. * instruction is the canonical example of this. When a hardware RNG is
  26. * available we don't need to worry about an RNG failure arising from fork()ing
  27. * the process or moving a VM, so we can keep thread-local RNG state and XOR
  28. * the hardware entropy in.
  29. *
  30. * (We assume that the OS entropy is safe from fork()ing and VM duplication.
  31. * This might be a bit of a leap of faith, esp on Windows, but there's nothing
  32. * that we can do about it.) */
  33. /* rand_thread_state contains the per-thread state for the RNG. This is only
  34. * used if the system has support for a hardware RNG. */
  35. struct rand_thread_state {
  36. uint8_t key[32];
  37. uint64_t calls_used;
  38. size_t bytes_used;
  39. uint8_t partial_block[64];
  40. unsigned partial_block_used;
  41. };
  42. /* kMaxCallsPerRefresh is the maximum number of |RAND_bytes| calls that we'll
  43. * serve before reading a new key from the operating system. This only applies
  44. * if we have a hardware RNG. */
  45. static const unsigned kMaxCallsPerRefresh = 1024;
  46. /* kMaxBytesPerRefresh is the maximum number of bytes that we'll return from
  47. * |RAND_bytes| before reading a new key from the operating system. This only
  48. * applies if we have a hardware RNG. */
  49. static const uint64_t kMaxBytesPerRefresh = 1024 * 1024;
  50. /* rand_thread_state_free frees a |rand_thread_state|. This is called when a
  51. * thread exits. */
  52. static void rand_thread_state_free(void *state) {
  53. if (state == NULL) {
  54. return;
  55. }
  56. OPENSSL_cleanse(state, sizeof(struct rand_thread_state));
  57. OPENSSL_free(state);
  58. }
  59. extern void CRYPTO_chacha_20(uint8_t *out, const uint8_t *in, size_t in_len,
  60. const uint8_t key[32], const uint8_t nonce[8],
  61. size_t counter);
  62. int RAND_bytes(uint8_t *buf, size_t len) {
  63. if (len == 0) {
  64. return 1;
  65. }
  66. if (!CRYPTO_have_hwrand()) {
  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. state->calls_used = kMaxCallsPerRefresh;
  83. }
  84. if (state->calls_used >= kMaxCallsPerRefresh ||
  85. state->bytes_used >= kMaxBytesPerRefresh) {
  86. CRYPTO_sysrand(state->key, sizeof(state->key));
  87. state->calls_used = 0;
  88. state->bytes_used = 0;
  89. state->partial_block_used = sizeof(state->partial_block);
  90. }
  91. CRYPTO_hwrand(buf, len);
  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. void RAND_add(const void *buf, int num, double entropy) {}
  129. int RAND_poll(void) {
  130. return 1;
  131. }
  132. int RAND_status(void) {
  133. return 1;
  134. }