arc4random.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*-
  2. * Copyright (c) 2017 The FreeBSD Foundation
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer
  10. * in this position and unchanged.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. */
  27. #include <sys/cdefs.h>
  28. __FBSDID("$FreeBSD$");
  29. #include <sys/types.h>
  30. #include <sys/param.h>
  31. #include <sys/kernel.h>
  32. #include <sys/libkern.h>
  33. #include <sys/linker.h>
  34. #include <sys/lock.h>
  35. #include <sys/malloc.h>
  36. #include <sys/mutex.h>
  37. #include <sys/random.h>
  38. #include <sys/smp.h>
  39. #include <sys/time.h>
  40. #include <machine/cpu.h>
  41. #include <crypto/chacha20/chacha.h>
  42. #include <crypto/sha2/sha256.h>
  43. #include <dev/random/randomdev.h>
  44. #ifdef RANDOM_FENESTRASX
  45. #include <dev/random/fenestrasX/fx_pub.h>
  46. #endif
  47. #define CHACHA20_RESEED_BYTES 65536
  48. #define CHACHA20_RESEED_SECONDS 300
  49. #define CHACHA20_KEYBYTES 32
  50. #define CHACHA20_BUFFER_SIZE 64
  51. CTASSERT(CHACHA20_KEYBYTES*8 >= CHACHA_MINKEYLEN);
  52. #ifndef RANDOM_FENESTRASX
  53. int arc4rand_iniseed_state = ARC4_ENTR_NONE;
  54. #endif
  55. MALLOC_DEFINE(M_CHACHA20RANDOM, "chacha20random", "chacha20random structures");
  56. struct chacha20_s {
  57. struct mtx mtx;
  58. int numbytes;
  59. time_t t_reseed;
  60. u_int8_t m_buffer[CHACHA20_BUFFER_SIZE];
  61. struct chacha_ctx ctx;
  62. #ifdef RANDOM_FENESTRASX
  63. uint64_t seed_version;
  64. #endif
  65. } __aligned(CACHE_LINE_SIZE);
  66. static struct chacha20_s *chacha20inst = NULL;
  67. #define CHACHA20_FOREACH(_chacha20) \
  68. for (_chacha20 = &chacha20inst[0]; \
  69. _chacha20 <= &chacha20inst[mp_maxid]; \
  70. _chacha20++)
  71. /*
  72. * Mix up the current context.
  73. */
  74. static void
  75. chacha20_randomstir(struct chacha20_s *chacha20)
  76. {
  77. struct timeval tv_now;
  78. u_int8_t key[CHACHA20_KEYBYTES];
  79. #ifdef RANDOM_FENESTRASX
  80. uint64_t seed_version;
  81. #else
  82. if (__predict_false(random_bypass_before_seeding && !is_random_seeded())) {
  83. SHA256_CTX ctx;
  84. uint64_t cc;
  85. uint32_t fver;
  86. if (!arc4random_bypassed_before_seeding) {
  87. arc4random_bypassed_before_seeding = true;
  88. if (!random_bypass_disable_warnings)
  89. printf("arc4random: WARNING: initial seeding "
  90. "bypassed the cryptographic random device "
  91. "because it was not yet seeded and the "
  92. "knob 'bypass_before_seeding' was "
  93. "enabled.\n");
  94. }
  95. /* Last ditch effort to inject something in a bad condition. */
  96. cc = get_cyclecount();
  97. SHA256_Init(&ctx);
  98. SHA256_Update(&ctx, key, sizeof(key));
  99. SHA256_Update(&ctx, &cc, sizeof(cc));
  100. fver = __FreeBSD_version;
  101. SHA256_Update(&ctx, &fver, sizeof(fver));
  102. _Static_assert(sizeof(key) == SHA256_DIGEST_LENGTH,
  103. "make sure 256 bits is still 256 bits");
  104. SHA256_Final(key, &ctx);
  105. } else {
  106. #endif
  107. #ifdef RANDOM_FENESTRASX
  108. read_random_key(key, CHACHA20_KEYBYTES, &seed_version);
  109. #else
  110. /*
  111. * If the loader(8) did not have an entropy stash from the
  112. * previous shutdown to load, then we will block. The answer is
  113. * to make sure there is an entropy stash at shutdown time.
  114. *
  115. * On the other hand, if the random_bypass_before_seeding knob
  116. * was set and we landed in this branch, we know this won't
  117. * block because we know the random device is seeded.
  118. */
  119. read_random(key, CHACHA20_KEYBYTES);
  120. }
  121. #endif
  122. getmicrouptime(&tv_now);
  123. mtx_lock(&chacha20->mtx);
  124. chacha_keysetup(&chacha20->ctx, key, CHACHA20_KEYBYTES*8);
  125. chacha_ivsetup(&chacha20->ctx, (u_char *)&tv_now.tv_sec, (u_char *)&tv_now.tv_usec);
  126. /* Reset for next reseed cycle. */
  127. chacha20->t_reseed = tv_now.tv_sec + CHACHA20_RESEED_SECONDS;
  128. chacha20->numbytes = 0;
  129. #ifdef RANDOM_FENESTRASX
  130. chacha20->seed_version = seed_version;
  131. #endif
  132. mtx_unlock(&chacha20->mtx);
  133. }
  134. /*
  135. * Initialize the contexts.
  136. */
  137. static void
  138. chacha20_init(void)
  139. {
  140. struct chacha20_s *chacha20;
  141. chacha20inst = malloc((mp_maxid + 1) * sizeof(struct chacha20_s),
  142. M_CHACHA20RANDOM, M_NOWAIT | M_ZERO);
  143. KASSERT(chacha20inst != NULL, ("chacha20_init: memory allocation error"));
  144. CHACHA20_FOREACH(chacha20) {
  145. mtx_init(&chacha20->mtx, "chacha20_mtx", NULL, MTX_DEF);
  146. chacha20->t_reseed = -1;
  147. chacha20->numbytes = 0;
  148. explicit_bzero(chacha20->m_buffer, CHACHA20_BUFFER_SIZE);
  149. explicit_bzero(&chacha20->ctx, sizeof(chacha20->ctx));
  150. }
  151. }
  152. SYSINIT(chacha20, SI_SUB_LOCK, SI_ORDER_ANY, chacha20_init, NULL);
  153. static void
  154. chacha20_uninit(void)
  155. {
  156. struct chacha20_s *chacha20;
  157. CHACHA20_FOREACH(chacha20)
  158. mtx_destroy(&chacha20->mtx);
  159. free(chacha20inst, M_CHACHA20RANDOM);
  160. }
  161. SYSUNINIT(chacha20, SI_SUB_LOCK, SI_ORDER_ANY, chacha20_uninit, NULL);
  162. /*
  163. * MPSAFE
  164. */
  165. void
  166. arc4rand(void *ptr, u_int len, int reseed)
  167. {
  168. struct chacha20_s *chacha20;
  169. struct timeval tv;
  170. u_int length;
  171. u_int8_t *p;
  172. #ifdef RANDOM_FENESTRASX
  173. if (__predict_false(reseed))
  174. #else
  175. if (__predict_false(reseed ||
  176. (arc4rand_iniseed_state == ARC4_ENTR_HAVE &&
  177. atomic_cmpset_int(&arc4rand_iniseed_state, ARC4_ENTR_HAVE, ARC4_ENTR_SEED))))
  178. #endif
  179. CHACHA20_FOREACH(chacha20)
  180. chacha20_randomstir(chacha20);
  181. getmicrouptime(&tv);
  182. chacha20 = &chacha20inst[curcpu];
  183. /* We may get unlucky and be migrated off this CPU, but that is expected to be infrequent */
  184. if ((chacha20->numbytes > CHACHA20_RESEED_BYTES) || (tv.tv_sec > chacha20->t_reseed))
  185. chacha20_randomstir(chacha20);
  186. mtx_lock(&chacha20->mtx);
  187. #ifdef RANDOM_FENESTRASX
  188. if (__predict_false(
  189. atomic_load_acq_64(&fxrng_root_generation) != chacha20->seed_version
  190. )) {
  191. mtx_unlock(&chacha20->mtx);
  192. chacha20_randomstir(chacha20);
  193. mtx_lock(&chacha20->mtx);
  194. }
  195. #endif
  196. p = ptr;
  197. while (len) {
  198. length = MIN(CHACHA20_BUFFER_SIZE, len);
  199. chacha_encrypt_bytes(&chacha20->ctx, chacha20->m_buffer, p, length);
  200. p += length;
  201. len -= length;
  202. chacha20->numbytes += length;
  203. if (chacha20->numbytes > CHACHA20_RESEED_BYTES) {
  204. mtx_unlock(&chacha20->mtx);
  205. chacha20_randomstir(chacha20);
  206. mtx_lock(&chacha20->mtx);
  207. }
  208. }
  209. mtx_unlock(&chacha20->mtx);
  210. }
  211. uint32_t
  212. arc4random(void)
  213. {
  214. uint32_t ret;
  215. arc4rand(&ret, sizeof(ret), 0);
  216. return ret;
  217. }
  218. void
  219. arc4random_buf(void *ptr, size_t len)
  220. {
  221. arc4rand(ptr, len, 0);
  222. }