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

271 行
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. #define _GNU_SOURCE /* needed for syscall() on Linux. */
  15. #include <openssl/rand.h>
  16. #if !defined(OPENSSL_WINDOWS) && !defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
  17. #include <assert.h>
  18. #include <errno.h>
  19. #include <fcntl.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #if defined(OPENSSL_LINUX)
  23. #include <sys/syscall.h>
  24. #endif
  25. #include <openssl/thread.h>
  26. #include <openssl/mem.h>
  27. #include "internal.h"
  28. #include "../internal.h"
  29. #if defined(OPENSSL_LINUX)
  30. #if defined(OPENSSL_X86_64)
  31. #define EXPECTED_SYS_getrandom 318
  32. #elif defined(OPENSSL_X86)
  33. #define EXPECTED_SYS_getrandom 355
  34. #elif defined(OPENSSL_AARCH64)
  35. #define EXPECTED_SYS_getrandom 278
  36. #elif defined(OPENSSL_ARM)
  37. #define EXPECTED_SYS_getrandom 384
  38. #elif defined(OPENSSL_PPC64LE)
  39. #define EXPECTED_SYS_getrandom 359
  40. #endif
  41. #if defined(EXPECTED_SYS_getrandom)
  42. #define USE_SYS_getrandom
  43. #if defined(SYS_getrandom)
  44. #if SYS_getrandom != EXPECTED_SYS_getrandom
  45. #error "system call number for getrandom is not the expected value"
  46. #endif
  47. #else /* SYS_getrandom */
  48. #define SYS_getrandom EXPECTED_SYS_getrandom
  49. #endif /* SYS_getrandom */
  50. #endif /* EXPECTED_SYS_getrandom */
  51. #if !defined(GRND_NONBLOCK)
  52. #define GRND_NONBLOCK 1
  53. #endif
  54. #endif /* OPENSSL_LINUX */
  55. /* This file implements a PRNG by reading from /dev/urandom, optionally with a
  56. * buffer, which is unsafe across |fork|. */
  57. #define BUF_SIZE 4096
  58. /* rand_buffer contains unused, random bytes, some of which may have been
  59. * consumed already. */
  60. struct rand_buffer {
  61. size_t used;
  62. uint8_t rand[BUF_SIZE];
  63. };
  64. /* requested_lock is used to protect the |*_requested| variables. */
  65. static struct CRYPTO_STATIC_MUTEX requested_lock = CRYPTO_STATIC_MUTEX_INIT;
  66. /* urandom_fd_requested is set by |RAND_set_urandom_fd|. It's protected by
  67. * |requested_lock|. */
  68. static int urandom_fd_requested = -2;
  69. /* urandom_fd is a file descriptor to /dev/urandom. It's protected by |once|. */
  70. static int urandom_fd = -2;
  71. /* urandom_buffering_requested is set by |RAND_enable_fork_unsafe_buffering|.
  72. * It's protected by |requested_lock|. */
  73. static int urandom_buffering_requested = 0;
  74. /* urandom_buffering controls whether buffering is enabled (1) or not (0). This
  75. * is protected by |once|. */
  76. static int urandom_buffering = 0;
  77. static CRYPTO_once_t once = CRYPTO_ONCE_INIT;
  78. /* init_once initializes the state of this module to values previously
  79. * requested. This is the only function that modifies |urandom_fd| and
  80. * |urandom_buffering|, whose values may be read safely after calling the
  81. * once. */
  82. static void init_once(void) {
  83. CRYPTO_STATIC_MUTEX_lock_read(&requested_lock);
  84. urandom_buffering = urandom_buffering_requested;
  85. int fd = urandom_fd_requested;
  86. CRYPTO_STATIC_MUTEX_unlock_read(&requested_lock);
  87. #if defined(USE_SYS_getrandom)
  88. /* Initial test of getrandom to find any unexpected behavior. */
  89. uint8_t dummy;
  90. syscall(SYS_getrandom, &dummy, sizeof(dummy), GRND_NONBLOCK);
  91. #endif
  92. if (fd == -2) {
  93. do {
  94. fd = open("/dev/urandom", O_RDONLY);
  95. } while (fd == -1 && errno == EINTR);
  96. }
  97. if (fd < 0) {
  98. abort();
  99. }
  100. int flags = fcntl(fd, F_GETFD);
  101. if (flags == -1) {
  102. /* Native Client doesn't implement |fcntl|. */
  103. if (errno != ENOSYS) {
  104. abort();
  105. }
  106. } else {
  107. flags |= FD_CLOEXEC;
  108. if (fcntl(fd, F_SETFD, flags) == -1) {
  109. abort();
  110. }
  111. }
  112. urandom_fd = fd;
  113. }
  114. void RAND_set_urandom_fd(int fd) {
  115. fd = dup(fd);
  116. if (fd < 0) {
  117. abort();
  118. }
  119. CRYPTO_STATIC_MUTEX_lock_write(&requested_lock);
  120. urandom_fd_requested = fd;
  121. CRYPTO_STATIC_MUTEX_unlock_write(&requested_lock);
  122. CRYPTO_once(&once, init_once);
  123. if (urandom_fd != fd) {
  124. abort(); // Already initialized.
  125. }
  126. }
  127. void RAND_enable_fork_unsafe_buffering(int fd) {
  128. if (fd >= 0) {
  129. fd = dup(fd);
  130. if (fd < 0) {
  131. abort();
  132. }
  133. } else {
  134. fd = -2;
  135. }
  136. CRYPTO_STATIC_MUTEX_lock_write(&requested_lock);
  137. urandom_buffering_requested = 1;
  138. urandom_fd_requested = fd;
  139. CRYPTO_STATIC_MUTEX_unlock_write(&requested_lock);
  140. CRYPTO_once(&once, init_once);
  141. if (urandom_buffering != 1 || (fd >= 0 && urandom_fd != fd)) {
  142. abort(); // Already initialized.
  143. }
  144. }
  145. static struct rand_buffer *get_thread_local_buffer(void) {
  146. struct rand_buffer *buf =
  147. CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_URANDOM_BUF);
  148. if (buf != NULL) {
  149. return buf;
  150. }
  151. buf = OPENSSL_malloc(sizeof(struct rand_buffer));
  152. if (buf == NULL) {
  153. return NULL;
  154. }
  155. buf->used = BUF_SIZE; /* To trigger a |fill_with_entropy| on first use. */
  156. if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_URANDOM_BUF, buf,
  157. OPENSSL_free)) {
  158. OPENSSL_free(buf);
  159. return NULL;
  160. }
  161. return buf;
  162. }
  163. /* fill_with_entropy writes |len| bytes of entropy into |out|. It returns one
  164. * on success and zero on error. */
  165. static char fill_with_entropy(uint8_t *out, size_t len) {
  166. ssize_t r;
  167. while (len > 0) {
  168. do {
  169. r = read(urandom_fd, out, len);
  170. } while (r == -1 && errno == EINTR);
  171. if (r <= 0) {
  172. return 0;
  173. }
  174. out += r;
  175. len -= r;
  176. }
  177. return 1;
  178. }
  179. /* read_from_buffer reads |requested| random bytes from the buffer into |out|,
  180. * refilling it if necessary to satisfy the request. */
  181. static void read_from_buffer(struct rand_buffer *buf,
  182. uint8_t *out, size_t requested) {
  183. size_t remaining = BUF_SIZE - buf->used;
  184. while (requested > remaining) {
  185. memcpy(out, &buf->rand[buf->used], remaining);
  186. buf->used += remaining;
  187. out += remaining;
  188. requested -= remaining;
  189. if (!fill_with_entropy(buf->rand, BUF_SIZE)) {
  190. abort();
  191. return;
  192. }
  193. buf->used = 0;
  194. remaining = BUF_SIZE;
  195. }
  196. memcpy(out, &buf->rand[buf->used], requested);
  197. buf->used += requested;
  198. }
  199. /* CRYPTO_sysrand puts |requested| random bytes into |out|. */
  200. void CRYPTO_sysrand(uint8_t *out, size_t requested) {
  201. if (requested == 0) {
  202. return;
  203. }
  204. CRYPTO_once(&once, init_once);
  205. if (urandom_buffering && requested < BUF_SIZE) {
  206. struct rand_buffer *buf = get_thread_local_buffer();
  207. if (buf != NULL) {
  208. read_from_buffer(buf, out, requested);
  209. return;
  210. }
  211. }
  212. if (!fill_with_entropy(out, requested)) {
  213. abort();
  214. }
  215. }
  216. #endif /* !OPENSSL_WINDOWS && !BORINGSSL_UNSAFE_DETERMINISTIC_MODE */