crypto_rand_fast.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. #define CRYPTO_PRIVATE
  34. #include "lib/crypt_ops/crypto_rand.h"
  35. #include "lib/crypt_ops/crypto_cipher.h"
  36. #include "lib/crypt_ops/crypto_digest.h"
  37. #include "lib/crypt_ops/crypto_util.h"
  38. #include "lib/intmath/cmp.h"
  39. #include "lib/cc/ctassert.h"
  40. #include "lib/malloc/map_anon.h"
  41. #include "lib/thread/threads.h"
  42. #include "lib/log/util_bug.h"
  43. #ifdef HAVE_SYS_TYPES_H
  44. #include <sys/types.h>
  45. #endif
  46. #ifdef HAVE_UNISTD_H
  47. #include <unistd.h>
  48. #endif
  49. #include <string.h>
  50. #ifdef NOINHERIT_CAN_FAIL
  51. #define CHECK_PID
  52. #endif
  53. #ifdef CHECK_PID
  54. #define PID_FIELD_LEN sizeof(pid_t)
  55. #else
  56. #define PID_FIELD_LEN 0
  57. #endif
  58. /* Alias for CRYPTO_FAST_RNG_SEED_LEN to make our code shorter.
  59. */
  60. #define SEED_LEN (CRYPTO_FAST_RNG_SEED_LEN)
  61. /* The amount of space that we mmap for a crypto_fast_rng_t.
  62. */
  63. #define MAPLEN 4096
  64. /* The number of random bytes that we can yield to the user after each
  65. * time we fill a crypto_fast_rng_t's buffer.
  66. */
  67. #define BUFLEN (MAPLEN - 2*sizeof(uint16_t) - SEED_LEN - PID_FIELD_LEN)
  68. /* The number of buffer refills after which we should fetch more
  69. * entropy from crypto_strongest_rand().
  70. */
  71. #define RESEED_AFTER 16
  72. /* The length of the stream cipher key we will use for the PRNG, in bytes.
  73. */
  74. #define KEY_LEN (CRYPTO_FAST_RNG_SEED_LEN - CIPHER_IV_LEN)
  75. /* The length of the stream cipher key we will use for the PRNG, in bits.
  76. */
  77. #define KEY_BITS (KEY_LEN * 8)
  78. /* Make sure that we have a key length we can actually use with AES. */
  79. CTASSERT(KEY_BITS == 128 || KEY_BITS == 192 || KEY_BITS == 256);
  80. struct crypto_fast_rng_t {
  81. /** How many more fills does this buffer have before we should mix
  82. * in the output of crypto_strongest_rand()?
  83. *
  84. * This value may be negative if unit tests are enabled. If so, it
  85. * indicates that we should never mix in extra data from
  86. * crypto_strongest_rand().
  87. */
  88. int16_t n_till_reseed;
  89. /** How many bytes are remaining in cbuf.bytes? */
  90. uint16_t bytes_left;
  91. #ifdef CHECK_PID
  92. /** Which process owns this fast_rng? If this value is zero, we do not
  93. * need to test the owner. */
  94. pid_t owner;
  95. #endif
  96. struct cbuf {
  97. /** The seed (key and IV) that we will use the next time that we refill
  98. * cbuf. */
  99. uint8_t seed[SEED_LEN];
  100. /**
  101. * Bytes that we are yielding to the user. The next byte to be
  102. * yielded is at bytes[BUFLEN-bytes_left]; all other bytes in this
  103. * array are set to zero.
  104. */
  105. uint8_t bytes[BUFLEN];
  106. } buf;
  107. };
  108. /* alignof(uint8_t) should be 1, so there shouldn't be any padding in cbuf.
  109. */
  110. CTASSERT(sizeof(struct cbuf) == BUFLEN+SEED_LEN);
  111. /* We're trying to fit all of the RNG state into a nice mmapable chunk.
  112. */
  113. CTASSERT(sizeof(crypto_fast_rng_t) <= MAPLEN);
  114. /**
  115. * Initialize and return a new fast PRNG, using a strong random seed.
  116. *
  117. * Note that this object is NOT thread-safe. If you need a thread-safe
  118. * prng, use crypto_rand(), or wrap this in a mutex.
  119. **/
  120. crypto_fast_rng_t *
  121. crypto_fast_rng_new(void)
  122. {
  123. uint8_t seed[SEED_LEN];
  124. crypto_strongest_rand(seed, sizeof(seed));
  125. crypto_fast_rng_t *result = crypto_fast_rng_new_from_seed(seed);
  126. memwipe(seed, 0, sizeof(seed));
  127. return result;
  128. }
  129. /**
  130. * Initialize and return a new fast PRNG, using a seed value specified
  131. * in <b>seed</b>. This value must be CRYPTO_FAST_RNG_SEED_LEN bytes
  132. * long.
  133. *
  134. * Note that this object is NOT thread-safe. If you need a thread-safe
  135. * prng, you should probably look at get_thread_fast_rng(). Alternatively,
  136. * use crypto_rand(), wrap this in a mutex.
  137. **/
  138. crypto_fast_rng_t *
  139. crypto_fast_rng_new_from_seed(const uint8_t *seed)
  140. {
  141. unsigned inherit = INHERIT_RES_KEEP;
  142. /* We try to allocate this object as securely as we can, to avoid
  143. * having it get dumped, swapped, or shared after fork.
  144. */
  145. crypto_fast_rng_t *result = tor_mmap_anonymous(sizeof(*result),
  146. ANONMAP_PRIVATE | ANONMAP_NOINHERIT,
  147. &inherit);
  148. memcpy(result->buf.seed, seed, SEED_LEN);
  149. /* Causes an immediate refill once the user asks for data. */
  150. result->bytes_left = 0;
  151. result->n_till_reseed = RESEED_AFTER;
  152. #ifdef CHECK_PID
  153. if (inherit == INHERIT_RES_KEEP) {
  154. /* This value will neither be dropped nor zeroed after fork, so we need to
  155. * check our pid to make sure we are not sharing it across a fork. This
  156. * can be expensive if the pid value isn't cached, sadly.
  157. */
  158. result->owner = getpid();
  159. }
  160. #elif defined(_WIN32)
  161. /* Windows can't fork(), so there's no need to noinherit. */
  162. #else
  163. /* We decided above that noinherit would always do _something_. Assert here
  164. * that we were correct. */
  165. tor_assert(inherit != INHERIT_RES_KEEP);
  166. #endif
  167. return result;
  168. }
  169. #ifdef TOR_UNIT_TESTS
  170. /**
  171. * Unit tests only: prevent a crypto_fast_rng_t from ever mixing in more
  172. * entropy.
  173. */
  174. void
  175. crypto_fast_rng_disable_reseed(crypto_fast_rng_t *rng)
  176. {
  177. rng->n_till_reseed = -1;
  178. }
  179. #endif
  180. /**
  181. * Helper: create a crypto_cipher_t object from SEED_LEN bytes of
  182. * input. The first KEY_LEN bytes are used as the stream cipher's key,
  183. * and the remaining CIPHER_IV_LEN bytes are used as its IV.
  184. **/
  185. static inline crypto_cipher_t *
  186. cipher_from_seed(const uint8_t *seed)
  187. {
  188. return crypto_cipher_new_with_iv_and_bits(seed, seed+KEY_LEN, KEY_BITS);
  189. }
  190. /**
  191. * Helper: mix additional entropy into <b>rng</b> by using our XOF to mix the
  192. * old value for the seed with some additional bytes from
  193. * crypto_strongest_rand().
  194. **/
  195. static void
  196. crypto_fast_rng_add_entopy(crypto_fast_rng_t *rng)
  197. {
  198. crypto_xof_t *xof = crypto_xof_new();
  199. crypto_xof_add_bytes(xof, rng->buf.seed, SEED_LEN);
  200. {
  201. uint8_t seedbuf[SEED_LEN];
  202. crypto_strongest_rand(seedbuf, SEED_LEN);
  203. crypto_xof_add_bytes(xof, seedbuf, SEED_LEN);
  204. memwipe(seedbuf, 0, SEED_LEN);
  205. }
  206. crypto_xof_squeeze_bytes(xof, rng->buf.seed, SEED_LEN);
  207. crypto_xof_free(xof);
  208. }
  209. /**
  210. * Helper: refill the seed bytes and output buffer of <b>rng</b>, using
  211. * the input seed bytes as input (key and IV) for the stream cipher.
  212. *
  213. * If the n_till_reseed counter has reached zero, mix more random bytes into
  214. * the seed before refilling the buffer.
  215. **/
  216. static void
  217. crypto_fast_rng_refill(crypto_fast_rng_t *rng)
  218. {
  219. rng->n_till_reseed--;
  220. if (rng->n_till_reseed == 0) {
  221. /* It's time to reseed the RNG. */
  222. crypto_fast_rng_add_entopy(rng);
  223. rng->n_till_reseed = RESEED_AFTER;
  224. } else if (rng->n_till_reseed < 0) {
  225. #ifdef TOR_UNIT_TESTS
  226. /* Reseeding is disabled for testing; never do it on this prng. */
  227. rng->n_till_reseed = -1;
  228. #else
  229. /* If testing is disabled, this shouldn't be able to become negative. */
  230. tor_assert_unreached();
  231. #endif
  232. }
  233. /* Now fill rng->buf with output from our stream cipher, initialized from
  234. * that seed value. */
  235. crypto_cipher_t *c = cipher_from_seed(rng->buf.seed);
  236. memset(&rng->buf, 0, sizeof(rng->buf));
  237. crypto_cipher_crypt_inplace(c, (char*)&rng->buf, sizeof(rng->buf));
  238. crypto_cipher_free(c);
  239. rng->bytes_left = sizeof(rng->buf.bytes);
  240. }
  241. /**
  242. * Release all storage held by <b>rng</b>.
  243. **/
  244. void
  245. crypto_fast_rng_free_(crypto_fast_rng_t *rng)
  246. {
  247. if (!rng)
  248. return;
  249. memwipe(rng, 0, sizeof(*rng));
  250. tor_munmap_anonymous(rng, sizeof(*rng));
  251. }
  252. /**
  253. * Helper: extract bytes from the PRNG, refilling it as necessary. Does not
  254. * optimize the case when the user has asked for a huge output.
  255. **/
  256. static void
  257. crypto_fast_rng_getbytes_impl(crypto_fast_rng_t *rng, uint8_t *out,
  258. const size_t n)
  259. {
  260. #ifdef CHECK_PID
  261. if (rng->owner) {
  262. /* Note that we only need to do this check when we have owner set: that
  263. * is, when our attempt to block inheriting failed, and the result was
  264. * INHERIT_RES_KEEP.
  265. *
  266. * If the result was INHERIT_RES_DROP, then any attempt to access the rng
  267. * memory after forking will crash.
  268. *
  269. * If the result was INHERIT_RES_ZERO, then forking will set the bytes_left
  270. * and n_till_reseed fields to zero. This function will call
  271. * crypto_fast_rng_refill(), which will in turn reseed the PRNG.
  272. *
  273. * So we only need to do this test in the case when mmap_anonymous()
  274. * returned INHERIT_KEEP. We avoid doing it needlessly, since getpid() is
  275. * often a system call, and that can be slow.
  276. */
  277. tor_assert(rng->owner == getpid());
  278. }
  279. #endif
  280. size_t bytes_to_yield = n;
  281. while (bytes_to_yield) {
  282. if (rng->bytes_left == 0)
  283. crypto_fast_rng_refill(rng);
  284. const size_t to_copy = MIN(rng->bytes_left, bytes_to_yield);
  285. tor_assert(sizeof(rng->buf.bytes) >= rng->bytes_left);
  286. uint8_t *copy_from = rng->buf.bytes +
  287. (sizeof(rng->buf.bytes) - rng->bytes_left);
  288. memcpy(out, copy_from, to_copy);
  289. memset(copy_from, 0, to_copy);
  290. out += to_copy;
  291. bytes_to_yield -= to_copy;
  292. rng->bytes_left -= to_copy;
  293. }
  294. }
  295. /**
  296. * Extract <b>n</b> bytes from <b>rng</b> into the buffer at <b>out</b>.
  297. **/
  298. void
  299. crypto_fast_rng_getbytes(crypto_fast_rng_t *rng, uint8_t *out, size_t n)
  300. {
  301. if (PREDICT_UNLIKELY(n > BUFLEN)) {
  302. /* The user has asked for a lot of output; generate it from a stream
  303. * cipher seeded by the PRNG rather than by pulling it out of the PRNG
  304. * directly.
  305. */
  306. uint8_t seed[SEED_LEN];
  307. crypto_fast_rng_getbytes_impl(rng, seed, SEED_LEN);
  308. crypto_cipher_t *c = cipher_from_seed(seed);
  309. memset(out, 0, n);
  310. crypto_cipher_crypt_inplace(c, (char*)out, n);
  311. crypto_cipher_free(c);
  312. memwipe(seed, 0, sizeof(seed));
  313. return;
  314. }
  315. crypto_fast_rng_getbytes_impl(rng, out, n);
  316. }
  317. #if defined(TOR_UNIT_TESTS)
  318. /** for white-box testing: return the number of bytes that are returned from
  319. * the user for each invocation of the stream cipher in this RNG. */
  320. size_t
  321. crypto_fast_rng_get_bytes_used_per_stream(void)
  322. {
  323. return BUFLEN;
  324. }
  325. #endif
  326. /**
  327. * Thread-local instance for our fast RNG.
  328. **/
  329. static tor_threadlocal_t thread_rng;
  330. /**
  331. * Return a per-thread fast RNG, initializing it if necessary.
  332. *
  333. * You do not need to free this yourself.
  334. *
  335. * It is NOT safe to share this value across threads.
  336. **/
  337. crypto_fast_rng_t *
  338. get_thread_fast_rng(void)
  339. {
  340. crypto_fast_rng_t *rng = tor_threadlocal_get(&thread_rng);
  341. if (PREDICT_UNLIKELY(rng == NULL)) {
  342. rng = crypto_fast_rng_new();
  343. tor_threadlocal_set(&thread_rng, rng);
  344. }
  345. return rng;
  346. }
  347. /**
  348. * Used when a thread is exiting: free the per-thread fast RNG if needed.
  349. * Invoked from the crypto subsystem's thread-cleanup code.
  350. **/
  351. void
  352. destroy_thread_fast_rng(void)
  353. {
  354. crypto_fast_rng_t *rng = tor_threadlocal_get(&thread_rng);
  355. if (!rng)
  356. return;
  357. crypto_fast_rng_free(rng);
  358. tor_threadlocal_set(&thread_rng, NULL);
  359. }
  360. #ifdef TOR_UNIT_TESTS
  361. /**
  362. * Replace the current thread's rng with <b>rng</b>. For use by the
  363. * unit tests only. Returns the previous thread rng.
  364. **/
  365. crypto_fast_rng_t *
  366. crypto_replace_thread_fast_rng(crypto_fast_rng_t *rng)
  367. {
  368. crypto_fast_rng_t *old_rng = tor_threadlocal_get(&thread_rng);
  369. tor_threadlocal_set(&thread_rng, rng);
  370. return old_rng;
  371. }
  372. #endif
  373. /**
  374. * Initialize the global thread-local key that will be used to keep track
  375. * of per-thread fast RNG instances. Called from the crypto subsystem's
  376. * initialization code.
  377. **/
  378. void
  379. crypto_rand_fast_init(void)
  380. {
  381. tor_threadlocal_init(&thread_rng);
  382. }
  383. /**
  384. * Initialize the global thread-local key that will be used to keep track
  385. * of per-thread fast RNG instances. Called from the crypto subsystem's
  386. * shutdown code.
  387. **/
  388. void
  389. crypto_rand_fast_shutdown(void)
  390. {
  391. destroy_thread_fast_rng();
  392. tor_threadlocal_destroy(&thread_rng);
  393. }