crypto_rand_fast.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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_assertf(inherit != INHERIT_RES_KEEP,
  166. "We failed to create a non-inheritable memory region, even "
  167. "though we believed such a failure to be impossible! This is "
  168. "probably a bug in Tor support for your platform; please report "
  169. "it.");
  170. #endif /* defined(CHECK_PID) || ... */
  171. return result;
  172. }
  173. #ifdef TOR_UNIT_TESTS
  174. /**
  175. * Unit tests only: prevent a crypto_fast_rng_t from ever mixing in more
  176. * entropy.
  177. */
  178. void
  179. crypto_fast_rng_disable_reseed(crypto_fast_rng_t *rng)
  180. {
  181. rng->n_till_reseed = -1;
  182. }
  183. #endif /* defined(TOR_UNIT_TESTS) */
  184. /**
  185. * Helper: create a crypto_cipher_t object from SEED_LEN bytes of
  186. * input. The first KEY_LEN bytes are used as the stream cipher's key,
  187. * and the remaining CIPHER_IV_LEN bytes are used as its IV.
  188. **/
  189. static inline crypto_cipher_t *
  190. cipher_from_seed(const uint8_t *seed)
  191. {
  192. return crypto_cipher_new_with_iv_and_bits(seed, seed+KEY_LEN, KEY_BITS);
  193. }
  194. /**
  195. * Helper: mix additional entropy into <b>rng</b> by using our XOF to mix the
  196. * old value for the seed with some additional bytes from
  197. * crypto_strongest_rand().
  198. **/
  199. static void
  200. crypto_fast_rng_add_entopy(crypto_fast_rng_t *rng)
  201. {
  202. crypto_xof_t *xof = crypto_xof_new();
  203. crypto_xof_add_bytes(xof, rng->buf.seed, SEED_LEN);
  204. {
  205. uint8_t seedbuf[SEED_LEN];
  206. crypto_strongest_rand(seedbuf, SEED_LEN);
  207. crypto_xof_add_bytes(xof, seedbuf, SEED_LEN);
  208. memwipe(seedbuf, 0, SEED_LEN);
  209. }
  210. crypto_xof_squeeze_bytes(xof, rng->buf.seed, SEED_LEN);
  211. crypto_xof_free(xof);
  212. }
  213. /**
  214. * Helper: refill the seed bytes and output buffer of <b>rng</b>, using
  215. * the input seed bytes as input (key and IV) for the stream cipher.
  216. *
  217. * If the n_till_reseed counter has reached zero, mix more random bytes into
  218. * the seed before refilling the buffer.
  219. **/
  220. static void
  221. crypto_fast_rng_refill(crypto_fast_rng_t *rng)
  222. {
  223. rng->n_till_reseed--;
  224. if (rng->n_till_reseed == 0) {
  225. /* It's time to reseed the RNG. */
  226. crypto_fast_rng_add_entopy(rng);
  227. rng->n_till_reseed = RESEED_AFTER;
  228. } else if (rng->n_till_reseed < 0) {
  229. #ifdef TOR_UNIT_TESTS
  230. /* Reseeding is disabled for testing; never do it on this prng. */
  231. rng->n_till_reseed = -1;
  232. #else
  233. /* If testing is disabled, this shouldn't be able to become negative. */
  234. tor_assert_unreached();
  235. #endif /* defined(TOR_UNIT_TESTS) */
  236. }
  237. /* Now fill rng->buf with output from our stream cipher, initialized from
  238. * that seed value. */
  239. crypto_cipher_t *c = cipher_from_seed(rng->buf.seed);
  240. memset(&rng->buf, 0, sizeof(rng->buf));
  241. crypto_cipher_crypt_inplace(c, (char*)&rng->buf, sizeof(rng->buf));
  242. crypto_cipher_free(c);
  243. rng->bytes_left = sizeof(rng->buf.bytes);
  244. }
  245. /**
  246. * Release all storage held by <b>rng</b>.
  247. **/
  248. void
  249. crypto_fast_rng_free_(crypto_fast_rng_t *rng)
  250. {
  251. if (!rng)
  252. return;
  253. memwipe(rng, 0, sizeof(*rng));
  254. tor_munmap_anonymous(rng, sizeof(*rng));
  255. }
  256. /**
  257. * Helper: extract bytes from the PRNG, refilling it as necessary. Does not
  258. * optimize the case when the user has asked for a huge output.
  259. **/
  260. static void
  261. crypto_fast_rng_getbytes_impl(crypto_fast_rng_t *rng, uint8_t *out,
  262. const size_t n)
  263. {
  264. #ifdef CHECK_PID
  265. if (rng->owner) {
  266. /* Note that we only need to do this check when we have owner set: that
  267. * is, when our attempt to block inheriting failed, and the result was
  268. * INHERIT_RES_KEEP.
  269. *
  270. * If the result was INHERIT_RES_DROP, then any attempt to access the rng
  271. * memory after forking will crash.
  272. *
  273. * If the result was INHERIT_RES_ZERO, then forking will set the bytes_left
  274. * and n_till_reseed fields to zero. This function will call
  275. * crypto_fast_rng_refill(), which will in turn reseed the PRNG.
  276. *
  277. * So we only need to do this test in the case when mmap_anonymous()
  278. * returned INHERIT_KEEP. We avoid doing it needlessly, since getpid() is
  279. * often a system call, and that can be slow.
  280. */
  281. tor_assert(rng->owner == getpid());
  282. }
  283. #endif /* defined(CHECK_PID) */
  284. size_t bytes_to_yield = n;
  285. while (bytes_to_yield) {
  286. if (rng->bytes_left == 0)
  287. crypto_fast_rng_refill(rng);
  288. const size_t to_copy = MIN(rng->bytes_left, bytes_to_yield);
  289. tor_assert(sizeof(rng->buf.bytes) >= rng->bytes_left);
  290. uint8_t *copy_from = rng->buf.bytes +
  291. (sizeof(rng->buf.bytes) - rng->bytes_left);
  292. memcpy(out, copy_from, to_copy);
  293. memset(copy_from, 0, to_copy);
  294. out += to_copy;
  295. bytes_to_yield -= to_copy;
  296. rng->bytes_left -= to_copy;
  297. }
  298. }
  299. /**
  300. * Extract <b>n</b> bytes from <b>rng</b> into the buffer at <b>out</b>.
  301. **/
  302. void
  303. crypto_fast_rng_getbytes(crypto_fast_rng_t *rng, uint8_t *out, size_t n)
  304. {
  305. if (PREDICT_UNLIKELY(n > BUFLEN)) {
  306. /* The user has asked for a lot of output; generate it from a stream
  307. * cipher seeded by the PRNG rather than by pulling it out of the PRNG
  308. * directly.
  309. */
  310. uint8_t seed[SEED_LEN];
  311. crypto_fast_rng_getbytes_impl(rng, seed, SEED_LEN);
  312. crypto_cipher_t *c = cipher_from_seed(seed);
  313. memset(out, 0, n);
  314. crypto_cipher_crypt_inplace(c, (char*)out, n);
  315. crypto_cipher_free(c);
  316. memwipe(seed, 0, sizeof(seed));
  317. return;
  318. }
  319. crypto_fast_rng_getbytes_impl(rng, out, n);
  320. }
  321. #if defined(TOR_UNIT_TESTS)
  322. /** for white-box testing: return the number of bytes that are returned from
  323. * the user for each invocation of the stream cipher in this RNG. */
  324. size_t
  325. crypto_fast_rng_get_bytes_used_per_stream(void)
  326. {
  327. return BUFLEN;
  328. }
  329. #endif /* defined(TOR_UNIT_TESTS) */
  330. /**
  331. * Thread-local instance for our fast RNG.
  332. **/
  333. static tor_threadlocal_t thread_rng;
  334. /**
  335. * Return a per-thread fast RNG, initializing it if necessary.
  336. *
  337. * You do not need to free this yourself.
  338. *
  339. * It is NOT safe to share this value across threads.
  340. **/
  341. crypto_fast_rng_t *
  342. get_thread_fast_rng(void)
  343. {
  344. crypto_fast_rng_t *rng = tor_threadlocal_get(&thread_rng);
  345. if (PREDICT_UNLIKELY(rng == NULL)) {
  346. rng = crypto_fast_rng_new();
  347. tor_threadlocal_set(&thread_rng, rng);
  348. }
  349. return rng;
  350. }
  351. /**
  352. * Used when a thread is exiting: free the per-thread fast RNG if needed.
  353. * Invoked from the crypto subsystem's thread-cleanup code.
  354. **/
  355. void
  356. destroy_thread_fast_rng(void)
  357. {
  358. crypto_fast_rng_t *rng = tor_threadlocal_get(&thread_rng);
  359. if (!rng)
  360. return;
  361. crypto_fast_rng_free(rng);
  362. tor_threadlocal_set(&thread_rng, NULL);
  363. }
  364. #ifdef TOR_UNIT_TESTS
  365. /**
  366. * Replace the current thread's rng with <b>rng</b>. For use by the
  367. * unit tests only. Returns the previous thread rng.
  368. **/
  369. crypto_fast_rng_t *
  370. crypto_replace_thread_fast_rng(crypto_fast_rng_t *rng)
  371. {
  372. crypto_fast_rng_t *old_rng = tor_threadlocal_get(&thread_rng);
  373. tor_threadlocal_set(&thread_rng, rng);
  374. return old_rng;
  375. }
  376. #endif /* defined(TOR_UNIT_TESTS) */
  377. /**
  378. * Initialize the global thread-local key that will be used to keep track
  379. * of per-thread fast RNG instances. Called from the crypto subsystem's
  380. * initialization code.
  381. **/
  382. void
  383. crypto_rand_fast_init(void)
  384. {
  385. tor_threadlocal_init(&thread_rng);
  386. }
  387. /**
  388. * Initialize the global thread-local key that will be used to keep track
  389. * of per-thread fast RNG instances. Called from the crypto subsystem's
  390. * shutdown code.
  391. **/
  392. void
  393. crypto_rand_fast_shutdown(void)
  394. {
  395. destroy_thread_fast_rng();
  396. tor_threadlocal_destroy(&thread_rng);
  397. }