選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

173 行
5.5 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/mem.h>
  18. #include "internal.h"
  19. #include "../internal.h"
  20. /* It's assumed that the operating system always has an unfailing source of
  21. * entropy which is accessed via |CRYPTO_sysrand|. (If the operating system
  22. * entropy source fails, it's up to |CRYPTO_sysrand| to abort the process—we
  23. * don't try to handle it.)
  24. *
  25. * In addition, the hardware may provide a low-latency RNG. Intel's rdrand
  26. * instruction is the canonical example of this. When a hardware RNG is
  27. * available we don't need to worry about an RNG failure arising from fork()ing
  28. * the process or moving a VM, so we can keep thread-local RNG state and XOR
  29. * the hardware entropy in.
  30. *
  31. * (We assume that the OS entropy is safe from fork()ing and VM duplication.
  32. * This might be a bit of a leap of faith, esp on Windows, but there's nothing
  33. * that we can do about it.) */
  34. /* rand_thread_state contains the per-thread state for the RNG. This is only
  35. * used if the system has support for a hardware RNG. */
  36. struct rand_thread_state {
  37. uint8_t key[32];
  38. uint64_t calls_used;
  39. size_t bytes_used;
  40. uint8_t partial_block[64];
  41. unsigned partial_block_used;
  42. };
  43. /* kMaxCallsPerRefresh is the maximum number of |RAND_bytes| calls that we'll
  44. * serve before reading a new key from the operating system. This only applies
  45. * if we have a hardware RNG. */
  46. static const unsigned kMaxCallsPerRefresh = 1024;
  47. /* kMaxBytesPerRefresh is the maximum number of bytes that we'll return from
  48. * |RAND_bytes| before reading a new key from the operating system. This only
  49. * applies if we have a hardware RNG. */
  50. static const uint64_t kMaxBytesPerRefresh = 1024 * 1024;
  51. /* rand_thread_state_free frees a |rand_thread_state|. This is called when a
  52. * thread exits. */
  53. static void rand_thread_state_free(void *state) {
  54. if (state == NULL) {
  55. return;
  56. }
  57. OPENSSL_cleanse(state, sizeof(struct rand_thread_state));
  58. OPENSSL_free(state);
  59. }
  60. extern void CRYPTO_chacha_20(uint8_t *out, const uint8_t *in, size_t in_len,
  61. const uint8_t key[32], const uint8_t nonce[8],
  62. size_t counter);
  63. int RAND_bytes(uint8_t *buf, size_t len) {
  64. if (len == 0) {
  65. return 1;
  66. }
  67. if (!CRYPTO_have_hwrand()) {
  68. /* Without a hardware RNG to save us from address-space duplication, the OS
  69. * entropy is used directly. */
  70. CRYPTO_sysrand(buf, len);
  71. return 1;
  72. }
  73. struct rand_thread_state *state =
  74. CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_RAND);
  75. if (state == NULL) {
  76. state = OPENSSL_malloc(sizeof(struct rand_thread_state));
  77. if (state == NULL ||
  78. !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_RAND, state,
  79. rand_thread_state_free)) {
  80. CRYPTO_sysrand(buf, len);
  81. return 1;
  82. }
  83. memset(state->partial_block, 0, sizeof(state->partial_block));
  84. state->calls_used = kMaxCallsPerRefresh;
  85. }
  86. if (state->calls_used >= kMaxCallsPerRefresh ||
  87. state->bytes_used >= kMaxBytesPerRefresh) {
  88. CRYPTO_sysrand(state->key, sizeof(state->key));
  89. state->calls_used = 0;
  90. state->bytes_used = 0;
  91. state->partial_block_used = sizeof(state->partial_block);
  92. }
  93. CRYPTO_hwrand(buf, len);
  94. if (len >= sizeof(state->partial_block)) {
  95. size_t remaining = len;
  96. while (remaining > 0) {
  97. // kMaxBytesPerCall is only 2GB, while ChaCha can handle 256GB. But this
  98. // is sufficient and easier on 32-bit.
  99. static const size_t kMaxBytesPerCall = 0x80000000;
  100. size_t todo = remaining;
  101. if (todo > kMaxBytesPerCall) {
  102. todo = kMaxBytesPerCall;
  103. }
  104. CRYPTO_chacha_20(buf, buf, todo, state->key,
  105. (uint8_t *)&state->calls_used, 0);
  106. buf += todo;
  107. remaining -= todo;
  108. state->calls_used++;
  109. }
  110. } else {
  111. if (sizeof(state->partial_block) - state->partial_block_used < len) {
  112. CRYPTO_chacha_20(state->partial_block, state->partial_block,
  113. sizeof(state->partial_block), state->key,
  114. (uint8_t *)&state->calls_used, 0);
  115. state->partial_block_used = 0;
  116. }
  117. unsigned i;
  118. for (i = 0; i < len; i++) {
  119. buf[i] ^= state->partial_block[state->partial_block_used++];
  120. }
  121. state->calls_used++;
  122. }
  123. state->bytes_used += len;
  124. return 1;
  125. }
  126. int RAND_pseudo_bytes(uint8_t *buf, size_t len) {
  127. return RAND_bytes(buf, len);
  128. }
  129. void RAND_seed(const void *buf, int num) {}
  130. int RAND_load_file(const char *path, long num) {
  131. if (num < 0) { /* read the "whole file" */
  132. return 1;
  133. } else if (num <= INT_MAX) {
  134. return (int) num;
  135. } else {
  136. return INT_MAX;
  137. }
  138. }
  139. void RAND_add(const void *buf, int num, double entropy) {}
  140. int RAND_poll(void) {
  141. return 1;
  142. }
  143. int RAND_status(void) {
  144. return 1;
  145. }