crypto_rand_fast.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* Copyright (c) 2001, Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file crypto_rand_fast.c
  8. *
  9. * \brief A fast strong PRNG for use when our underlying cryptographic
  10. * library's PRNG isn't fast enough.
  11. **/
  12. /* This library is currently implemented to use the same implementation
  13. * technique as libottery, using AES-CTR-256 as our underlying stream cipher.
  14. * It's backtracking-resistant immediately, and prediction-resistant after
  15. * a while.
  16. *
  17. * Here's how it works:
  18. *
  19. * We generate pseudorandom bytes using AES-CTR-256. We generate BUFLEN bytes
  20. * at a time. When we do this, we keep the first SEED_LEN bytes as the key
  21. * and the IV for our next invocation of AES_CTR, and yield the remaining
  22. * BUFLEN - SEED_LEN bytes to the user as they invoke the PRNG. As we yield
  23. * bytes to the user, we clear them from the buffer.
  24. *
  25. * After we have refilled the buffer RESEED_AFTER times, we mix in an
  26. * additional SEED_LEN bytes from our strong PRNG into the seed.
  27. *
  28. * If the user ever asks for a huge number of bytes at once, we pull SEED_LEN
  29. * bytes from the PRNG and use them with our stream cipher to fill the user's
  30. * request.
  31. */
  32. #define CRYPTO_RAND_FAST_PRIVATE
  33. #include "lib/crypt_ops/crypto_rand.h"
  34. #include "lib/crypt_ops/crypto_cipher.h"
  35. #include "lib/crypt_ops/crypto_digest.h"
  36. #include "lib/crypt_ops/crypto_util.h"
  37. #include "lib/intmath/cmp.h"
  38. #include "lib/cc/ctassert.h"
  39. #include "lib/malloc/map_anon.h"
  40. #include "lib/log/util_bug.h"
  41. #include <string.h>
  42. /* Alias for CRYPTO_FAST_RNG_SEED_LEN to make our code shorter.
  43. */
  44. #define SEED_LEN (CRYPTO_FAST_RNG_SEED_LEN)
  45. /* The amount of space that we mmap for a crypto_fast_rng_t.
  46. */
  47. #define MAPLEN 4096
  48. /* The number of random bytes that we can yield to the user after each
  49. * time we fill a crypto_fast_rng_t's buffer.
  50. */
  51. #define BUFLEN (MAPLEN - 2*sizeof(uint16_t) - SEED_LEN)
  52. /* The number of buffer refills after which we should fetch more
  53. * entropy from crypto_strongest_rand().
  54. */
  55. #define RESEED_AFTER 16
  56. /* The length of the stream cipher key we will use for the PRNG, in bytes.
  57. */
  58. #define KEY_LEN (CRYPTO_FAST_RNG_SEED_LEN - CIPHER_IV_LEN)
  59. /* The length of the stream cipher key we will use for the PRNG, in bits.
  60. */
  61. #define KEY_BITS (KEY_LEN * 8)
  62. /* Make sure that we have a key length we can actually use with AES. */
  63. CTASSERT(KEY_BITS == 128 || KEY_BITS == 192 || KEY_BITS == 256);
  64. struct crypto_fast_rng_t {
  65. /** How many more fills does this buffer have before we should mix
  66. * in the output of crypto_rand()? */
  67. uint16_t n_till_reseed;
  68. /** How many bytes are remaining in cbuf.bytes? */
  69. uint16_t bytes_left;
  70. struct cbuf {
  71. /** The seed (key and IV) that we will use the next time that we refill
  72. * cbuf. */
  73. uint8_t seed[SEED_LEN];
  74. /**
  75. * Bytes that we are yielding to the user. The next byte to be
  76. * yielded is at bytes[BUFLEN-bytes_left]; all other bytes in this
  77. * array are set to zero.
  78. */
  79. uint8_t bytes[BUFLEN];
  80. } buf;
  81. };
  82. /* alignof(uint8_t) should be 1, so there shouldn't be any padding in cbuf.
  83. */
  84. CTASSERT(sizeof(struct cbuf) == BUFLEN+SEED_LEN);
  85. /* We're trying to fit all of the RNG state into a nice mmapable chunk.
  86. */
  87. CTASSERT(sizeof(crypto_fast_rng_t) <= MAPLEN);
  88. /**
  89. * Initialize and return a new fast PRNG, using a strong random seed.
  90. *
  91. * Note that this object is NOT thread-safe. If you need a thread-safe
  92. * prng, use crypto_rand(), or wrap this in a mutex.
  93. **/
  94. crypto_fast_rng_t *
  95. crypto_fast_rng_new(void)
  96. {
  97. uint8_t seed[SEED_LEN];
  98. crypto_strongest_rand(seed, sizeof(seed));
  99. crypto_fast_rng_t *result = crypto_fast_rng_new_from_seed(seed);
  100. memwipe(seed, 0, sizeof(seed));
  101. return result;
  102. }
  103. /**
  104. * Initialize and return a new fast PRNG, using a seed value specified
  105. * in <b>seed</b>. This value must be CRYPTO_FAST_RNG_SEED_LEN bytes
  106. * long.
  107. *
  108. * Note that this object is NOT thread-safe. If you need a thread-safe
  109. * prng, use crypto_rand(), or wrap this in a mutex.
  110. **/
  111. crypto_fast_rng_t *
  112. crypto_fast_rng_new_from_seed(const uint8_t *seed)
  113. {
  114. /* We try to allocate this object as securely as we can, to avoid
  115. * having it get dumped, swapped, or shared after fork.
  116. */
  117. crypto_fast_rng_t *result = tor_mmap_anonymous(sizeof(*result),
  118. ANONMAP_PRIVATE | ANONMAP_NOINHERIT);
  119. memcpy(result->buf.seed, seed, SEED_LEN);
  120. /* Causes an immediate refill once the user asks for data. */
  121. result->bytes_left = 0;
  122. result->n_till_reseed = RESEED_AFTER;
  123. return result;
  124. }
  125. /**
  126. * Helper: create a crypto_cipher_t object from SEED_LEN bytes of
  127. * input. The first KEY_LEN bytes are used as the stream cipher's key,
  128. * and the remaining CIPHER_IV_LEN bytes are used as its IV.
  129. **/
  130. static inline crypto_cipher_t *
  131. cipher_from_seed(const uint8_t *seed)
  132. {
  133. return crypto_cipher_new_with_iv_and_bits(seed, seed+KEY_LEN, KEY_BITS);
  134. }
  135. /**
  136. * Helper: refill the seed bytes and output buffer of <b>rng</b>, using
  137. * the input seed bytes as input (key and IV) for the stream cipher.
  138. *
  139. * If the n_till_reseed counter has reached zero, mix more random bytes into
  140. * the seed before refilling the buffer.
  141. **/
  142. static void
  143. crypto_fast_rng_refill(crypto_fast_rng_t *rng)
  144. {
  145. if (rng->n_till_reseed-- == 0) {
  146. /* It's time to reseed the RNG. We'll do this by using our XOF to mix the
  147. * old value for the seed with some additional bytes from
  148. * crypto_strongest_rand(). */
  149. crypto_xof_t *xof = crypto_xof_new();
  150. crypto_xof_add_bytes(xof, rng->buf.seed, SEED_LEN);
  151. {
  152. uint8_t seedbuf[SEED_LEN];
  153. crypto_strongest_rand(seedbuf, SEED_LEN);
  154. crypto_xof_add_bytes(xof, seedbuf, SEED_LEN);
  155. memwipe(seedbuf, 0, SEED_LEN);
  156. }
  157. crypto_xof_squeeze_bytes(xof, rng->buf.seed, SEED_LEN);
  158. crypto_xof_free(xof);
  159. rng->n_till_reseed = RESEED_AFTER;
  160. }
  161. /* Now fill rng->buf with output from our stream cipher, initialized from
  162. * that seed value. */
  163. crypto_cipher_t *c = cipher_from_seed(rng->buf.seed);
  164. memset(&rng->buf, 0, sizeof(rng->buf));
  165. crypto_cipher_crypt_inplace(c, (char*)&rng->buf, sizeof(rng->buf));
  166. crypto_cipher_free(c);
  167. rng->bytes_left = sizeof(rng->buf.bytes);
  168. }
  169. /**
  170. * Release all storage held by <b>rng</b>.
  171. **/
  172. void
  173. crypto_fast_rng_free_(crypto_fast_rng_t *rng)
  174. {
  175. if (!rng)
  176. return;
  177. memwipe(rng, 0, sizeof(*rng));
  178. tor_munmap_anonymous(rng, sizeof(*rng));
  179. }
  180. /**
  181. * Helper: extract bytes from the PRNG, refilling it as necessary. Does not
  182. * optimize the case when the user has asked for a huge output.
  183. **/
  184. static void
  185. crypto_fast_rng_getbytes_impl(crypto_fast_rng_t *rng, uint8_t *out,
  186. const size_t n)
  187. {
  188. size_t bytes_to_yield = n;
  189. while (bytes_to_yield) {
  190. if (rng->bytes_left == 0)
  191. crypto_fast_rng_refill(rng);
  192. const size_t to_copy = MIN(rng->bytes_left, bytes_to_yield);
  193. tor_assert(sizeof(rng->buf.bytes) >= rng->bytes_left);
  194. uint8_t *copy_from = rng->buf.bytes +
  195. (sizeof(rng->buf.bytes) - rng->bytes_left);
  196. memcpy(out, copy_from, to_copy);
  197. memset(copy_from, 0, to_copy);
  198. out += to_copy;
  199. bytes_to_yield -= to_copy;
  200. rng->bytes_left -= to_copy;
  201. }
  202. }
  203. /**
  204. * Extract <b>n</b> bytes from <b>rng</b> into the buffer at <b>out</b>.
  205. **/
  206. void
  207. crypto_fast_rng_getbytes(crypto_fast_rng_t *rng, uint8_t *out, size_t n)
  208. {
  209. if (PREDICT_UNLIKELY(n > BUFLEN)) {
  210. /* The user has asked for a lot of output; generate it from a stream
  211. * cipher seeded by the PRNG rather than by pulling it out of the PRNG
  212. * directly.
  213. */
  214. uint8_t seed[SEED_LEN];
  215. crypto_fast_rng_getbytes_impl(rng, seed, SEED_LEN);
  216. crypto_cipher_t *c = cipher_from_seed(seed);
  217. memset(out, 0, n);
  218. crypto_cipher_crypt_inplace(c, (char*)out, n);
  219. crypto_cipher_free(c);
  220. memwipe(seed, 0, sizeof(seed));
  221. return;
  222. }
  223. crypto_fast_rng_getbytes_impl(rng, out, n);
  224. }
  225. #if defined(TOR_UNIT_TESTS)
  226. /** for white-box testing: return the number of bytes that are returned from
  227. * the user for each invocation of the stream cipher in this RNG. */
  228. size_t
  229. crypto_fast_rng_get_bytes_used_per_stream(void)
  230. {
  231. return BUFLEN;
  232. }
  233. #endif