urandom.c 5.7 KB

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