Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

359 Zeilen
11 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 <assert.h>
  16. #include <limits.h>
  17. #include <string.h>
  18. #if defined(BORINGSSL_FIPS)
  19. #include <unistd.h>
  20. #endif
  21. #include <openssl/chacha.h>
  22. #include <openssl/cpu.h>
  23. #include <openssl/mem.h>
  24. #include "internal.h"
  25. #include "../../internal.h"
  26. #include "../delocate.h"
  27. // It's assumed that the operating system always has an unfailing source of
  28. // entropy which is accessed via |CRYPTO_sysrand|. (If the operating system
  29. // entropy source fails, it's up to |CRYPTO_sysrand| to abort the process—we
  30. // don't try to handle it.)
  31. //
  32. // In addition, the hardware may provide a low-latency RNG. Intel's rdrand
  33. // instruction is the canonical example of this. When a hardware RNG is
  34. // available we don't need to worry about an RNG failure arising from fork()ing
  35. // the process or moving a VM, so we can keep thread-local RNG state and use it
  36. // as an additional-data input to CTR-DRBG.
  37. //
  38. // (We assume that the OS entropy is safe from fork()ing and VM duplication.
  39. // This might be a bit of a leap of faith, esp on Windows, but there's nothing
  40. // that we can do about it.)
  41. // kReseedInterval is the number of generate calls made to CTR-DRBG before
  42. // reseeding.
  43. static const unsigned kReseedInterval = 4096;
  44. // CRNGT_BLOCK_SIZE is the number of bytes in a “block” for the purposes of the
  45. // continuous random number generator test in FIPS 140-2, section 4.9.2.
  46. #define CRNGT_BLOCK_SIZE 16
  47. // rand_thread_state contains the per-thread state for the RNG.
  48. struct rand_thread_state {
  49. CTR_DRBG_STATE drbg;
  50. // calls is the number of generate calls made on |drbg| since it was last
  51. // (re)seeded. This is bound by |kReseedInterval|.
  52. unsigned calls;
  53. // last_block_valid is non-zero iff |last_block| contains data from
  54. // |CRYPTO_sysrand|.
  55. int last_block_valid;
  56. #if defined(BORINGSSL_FIPS)
  57. // last_block contains the previous block from |CRYPTO_sysrand|.
  58. uint8_t last_block[CRNGT_BLOCK_SIZE];
  59. // next and prev form a NULL-terminated, double-linked list of all states in
  60. // a process.
  61. struct rand_thread_state *next, *prev;
  62. #endif
  63. };
  64. #if defined(BORINGSSL_FIPS)
  65. // thread_states_list is the head of a linked-list of all |rand_thread_state|
  66. // objects in the process, one per thread. This is needed because FIPS requires
  67. // that they be zeroed on process exit, but thread-local destructors aren't
  68. // called when the whole process is exiting.
  69. DEFINE_BSS_GET(struct rand_thread_state *, thread_states_list);
  70. DEFINE_STATIC_MUTEX(thread_states_list_lock);
  71. static void rand_thread_state_clear_all(void) __attribute__((destructor));
  72. static void rand_thread_state_clear_all(void) {
  73. CRYPTO_STATIC_MUTEX_lock_write(thread_states_list_lock_bss_get());
  74. for (struct rand_thread_state *cur = *thread_states_list_bss_get();
  75. cur != NULL; cur = cur->next) {
  76. CTR_DRBG_clear(&cur->drbg);
  77. }
  78. // |thread_states_list_lock is deliberately left locked so that any threads
  79. // that are still running will hang if they try to call |RAND_bytes|.
  80. }
  81. #endif
  82. // rand_thread_state_free frees a |rand_thread_state|. This is called when a
  83. // thread exits.
  84. static void rand_thread_state_free(void *state_in) {
  85. struct rand_thread_state *state = state_in;
  86. if (state_in == NULL) {
  87. return;
  88. }
  89. #if defined(BORINGSSL_FIPS)
  90. CRYPTO_STATIC_MUTEX_lock_write(thread_states_list_lock_bss_get());
  91. if (state->prev != NULL) {
  92. state->prev->next = state->next;
  93. } else {
  94. *thread_states_list_bss_get() = state->next;
  95. }
  96. if (state->next != NULL) {
  97. state->next->prev = state->prev;
  98. }
  99. CRYPTO_STATIC_MUTEX_unlock_write(thread_states_list_lock_bss_get());
  100. CTR_DRBG_clear(&state->drbg);
  101. #endif
  102. OPENSSL_free(state);
  103. }
  104. #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \
  105. !defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
  106. // These functions are defined in asm/rdrand-x86_64.pl
  107. extern int CRYPTO_rdrand(uint8_t out[8]);
  108. extern int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len);
  109. static int have_rdrand(void) {
  110. return (OPENSSL_ia32cap_get()[1] & (1u << 30)) != 0;
  111. }
  112. static int hwrand(uint8_t *buf, const size_t len) {
  113. if (!have_rdrand()) {
  114. return 0;
  115. }
  116. const size_t len_multiple8 = len & ~7;
  117. if (!CRYPTO_rdrand_multiple8_buf(buf, len_multiple8)) {
  118. return 0;
  119. }
  120. const size_t remainder = len - len_multiple8;
  121. if (remainder != 0) {
  122. assert(remainder < 8);
  123. uint8_t rand_buf[8];
  124. if (!CRYPTO_rdrand(rand_buf)) {
  125. return 0;
  126. }
  127. OPENSSL_memcpy(buf + len_multiple8, rand_buf, remainder);
  128. }
  129. #if defined(BORINGSSL_FIPS_BREAK_CRNG)
  130. // This breaks the "continuous random number generator test" defined in FIPS
  131. // 140-2, section 4.9.2, and implemented in rand_get_seed().
  132. OPENSSL_memset(buf, 0, len);
  133. #endif
  134. return 1;
  135. }
  136. #else
  137. static int hwrand(uint8_t *buf, size_t len) {
  138. return 0;
  139. }
  140. #endif
  141. #if defined(BORINGSSL_FIPS)
  142. static void rand_get_seed(struct rand_thread_state *state,
  143. uint8_t seed[CTR_DRBG_ENTROPY_LEN]) {
  144. if (!state->last_block_valid) {
  145. if (!hwrand(state->last_block, sizeof(state->last_block))) {
  146. CRYPTO_sysrand(state->last_block, sizeof(state->last_block));
  147. }
  148. state->last_block_valid = 1;
  149. }
  150. // We overread from /dev/urandom or RDRAND by a factor of 10 and XOR to
  151. // whiten.
  152. #define FIPS_OVERREAD 10
  153. uint8_t entropy[CTR_DRBG_ENTROPY_LEN * FIPS_OVERREAD];
  154. if (!hwrand(entropy, sizeof(entropy))) {
  155. CRYPTO_sysrand(entropy, sizeof(entropy));
  156. }
  157. // See FIPS 140-2, section 4.9.2. This is the “continuous random number
  158. // generator test” which causes the program to randomly abort. Hopefully the
  159. // rate of failure is small enough not to be a problem in practice.
  160. if (CRYPTO_memcmp(state->last_block, entropy, CRNGT_BLOCK_SIZE) == 0) {
  161. printf("CRNGT failed.\n");
  162. BORINGSSL_FIPS_abort();
  163. }
  164. for (size_t i = CRNGT_BLOCK_SIZE; i < sizeof(entropy);
  165. i += CRNGT_BLOCK_SIZE) {
  166. if (CRYPTO_memcmp(entropy + i - CRNGT_BLOCK_SIZE, entropy + i,
  167. CRNGT_BLOCK_SIZE) == 0) {
  168. printf("CRNGT failed.\n");
  169. BORINGSSL_FIPS_abort();
  170. }
  171. }
  172. OPENSSL_memcpy(state->last_block,
  173. entropy + sizeof(entropy) - CRNGT_BLOCK_SIZE,
  174. CRNGT_BLOCK_SIZE);
  175. OPENSSL_memcpy(seed, entropy, CTR_DRBG_ENTROPY_LEN);
  176. for (size_t i = 1; i < FIPS_OVERREAD; i++) {
  177. for (size_t j = 0; j < CTR_DRBG_ENTROPY_LEN; j++) {
  178. seed[j] ^= entropy[CTR_DRBG_ENTROPY_LEN * i + j];
  179. }
  180. }
  181. }
  182. #else
  183. static void rand_get_seed(struct rand_thread_state *state,
  184. uint8_t seed[CTR_DRBG_ENTROPY_LEN]) {
  185. // If not in FIPS mode, we don't overread from the system entropy source and
  186. // we don't depend only on the hardware RDRAND.
  187. CRYPTO_sysrand(seed, CTR_DRBG_ENTROPY_LEN);
  188. }
  189. #endif
  190. void RAND_bytes_with_additional_data(uint8_t *out, size_t out_len,
  191. const uint8_t user_additional_data[32]) {
  192. if (out_len == 0) {
  193. return;
  194. }
  195. // Additional data is mixed into every CTR-DRBG call to protect, as best we
  196. // can, against forks & VM clones. We do not over-read this information and
  197. // don't reseed with it so, from the point of view of FIPS, this doesn't
  198. // provide “prediction resistance”. But, in practice, it does.
  199. uint8_t additional_data[32];
  200. if (!hwrand(additional_data, sizeof(additional_data))) {
  201. // Without a hardware RNG to save us from address-space duplication, the OS
  202. // entropy is used. This can be expensive (one read per |RAND_bytes| call)
  203. // and so can be disabled by applications that we have ensured don't fork
  204. // and aren't at risk of VM cloning.
  205. if (!rand_fork_unsafe_buffering_enabled()) {
  206. CRYPTO_sysrand(additional_data, sizeof(additional_data));
  207. } else {
  208. OPENSSL_memset(additional_data, 0, sizeof(additional_data));
  209. }
  210. }
  211. for (size_t i = 0; i < sizeof(additional_data); i++) {
  212. additional_data[i] ^= user_additional_data[i];
  213. }
  214. struct rand_thread_state stack_state;
  215. struct rand_thread_state *state =
  216. CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_RAND);
  217. if (state == NULL) {
  218. state = OPENSSL_malloc(sizeof(struct rand_thread_state));
  219. if (state == NULL ||
  220. !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_RAND, state,
  221. rand_thread_state_free)) {
  222. // If the system is out of memory, use an ephemeral state on the
  223. // stack.
  224. state = &stack_state;
  225. }
  226. state->last_block_valid = 0;
  227. uint8_t seed[CTR_DRBG_ENTROPY_LEN];
  228. rand_get_seed(state, seed);
  229. if (!CTR_DRBG_init(&state->drbg, seed, NULL, 0)) {
  230. abort();
  231. }
  232. state->calls = 0;
  233. #if defined(BORINGSSL_FIPS)
  234. if (state != &stack_state) {
  235. CRYPTO_STATIC_MUTEX_lock_write(thread_states_list_lock_bss_get());
  236. struct rand_thread_state **states_list = thread_states_list_bss_get();
  237. state->next = *states_list;
  238. if (state->next != NULL) {
  239. state->next->prev = state;
  240. }
  241. state->prev = NULL;
  242. *states_list = state;
  243. CRYPTO_STATIC_MUTEX_unlock_write(thread_states_list_lock_bss_get());
  244. }
  245. #endif
  246. }
  247. if (state->calls >= kReseedInterval) {
  248. uint8_t seed[CTR_DRBG_ENTROPY_LEN];
  249. rand_get_seed(state, seed);
  250. #if defined(BORINGSSL_FIPS)
  251. // Take a read lock around accesses to |state->drbg|. This is needed to
  252. // avoid returning bad entropy if we race with
  253. // |rand_thread_state_clear_all|.
  254. //
  255. // This lock must be taken after any calls to |CRYPTO_sysrand| to avoid a
  256. // bug on ppc64le. glibc may implement pthread locks by wrapping user code
  257. // in a hardware transaction, but, on some older versions of glibc and the
  258. // kernel, syscalls made with |syscall| did not abort the transaction.
  259. CRYPTO_STATIC_MUTEX_lock_read(thread_states_list_lock_bss_get());
  260. #endif
  261. if (!CTR_DRBG_reseed(&state->drbg, seed, NULL, 0)) {
  262. abort();
  263. }
  264. state->calls = 0;
  265. } else {
  266. #if defined(BORINGSSL_FIPS)
  267. CRYPTO_STATIC_MUTEX_lock_read(thread_states_list_lock_bss_get());
  268. #endif
  269. }
  270. int first_call = 1;
  271. while (out_len > 0) {
  272. size_t todo = out_len;
  273. if (todo > CTR_DRBG_MAX_GENERATE_LENGTH) {
  274. todo = CTR_DRBG_MAX_GENERATE_LENGTH;
  275. }
  276. if (!CTR_DRBG_generate(&state->drbg, out, todo, additional_data,
  277. first_call ? sizeof(additional_data) : 0)) {
  278. abort();
  279. }
  280. out += todo;
  281. out_len -= todo;
  282. state->calls++;
  283. first_call = 0;
  284. }
  285. if (state == &stack_state) {
  286. CTR_DRBG_clear(&state->drbg);
  287. }
  288. #if defined(BORINGSSL_FIPS)
  289. CRYPTO_STATIC_MUTEX_unlock_read(thread_states_list_lock_bss_get());
  290. #endif
  291. }
  292. int RAND_bytes(uint8_t *out, size_t out_len) {
  293. static const uint8_t kZeroAdditionalData[32] = {0};
  294. RAND_bytes_with_additional_data(out, out_len, kZeroAdditionalData);
  295. return 1;
  296. }
  297. int RAND_pseudo_bytes(uint8_t *buf, size_t len) {
  298. return RAND_bytes(buf, len);
  299. }