test_crypto_rng.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #define CRYPTO_RAND_PRIVATE
  7. #include "core/or/or.h"
  8. #include "test/test.h"
  9. #include "lib/crypt_ops/aes.h"
  10. #include "lib/crypt_ops/crypto_format.h"
  11. #include "lib/crypt_ops/crypto_rand.h"
  12. /** Run unit tests for our random number generation function and its wrappers.
  13. */
  14. static void
  15. test_crypto_rng(void *arg)
  16. {
  17. int i, j, allok;
  18. char data1[100], data2[100];
  19. double d;
  20. char *h=NULL;
  21. /* Try out RNG. */
  22. (void)arg;
  23. tt_assert(! crypto_seed_rng());
  24. crypto_rand(data1, 100);
  25. crypto_rand(data2, 100);
  26. tt_mem_op(data1,OP_NE, data2,100);
  27. allok = 1;
  28. for (i = 0; i < 100; ++i) {
  29. uint64_t big;
  30. char *host;
  31. j = crypto_rand_int(100);
  32. if (j < 0 || j >= 100)
  33. allok = 0;
  34. big = crypto_rand_uint64(UINT64_C(1)<<40);
  35. if (big >= (UINT64_C(1)<<40))
  36. allok = 0;
  37. big = crypto_rand_uint64(UINT64_C(5));
  38. if (big >= 5)
  39. allok = 0;
  40. d = crypto_rand_double();
  41. tt_assert(d >= 0);
  42. tt_assert(d < 1.0);
  43. host = crypto_random_hostname(3,8,"www.",".onion");
  44. if (strcmpstart(host,"www.") ||
  45. strcmpend(host,".onion") ||
  46. strlen(host) < 13 ||
  47. strlen(host) > 18)
  48. allok = 0;
  49. tor_free(host);
  50. }
  51. /* Make sure crypto_random_hostname clips its inputs properly. */
  52. h = crypto_random_hostname(20000, 9000, "www.", ".onion");
  53. tt_assert(! strcmpstart(h,"www."));
  54. tt_assert(! strcmpend(h,".onion"));
  55. tt_int_op(63+4+6, OP_EQ, strlen(h));
  56. tt_assert(allok);
  57. done:
  58. tor_free(h);
  59. }
  60. static void
  61. test_crypto_rng_range(void *arg)
  62. {
  63. int got_smallest = 0, got_largest = 0;
  64. int i;
  65. (void)arg;
  66. for (i = 0; i < 1000; ++i) {
  67. int x = crypto_rand_int_range(5,9);
  68. tt_int_op(x, OP_GE, 5);
  69. tt_int_op(x, OP_LT, 9);
  70. if (x == 5)
  71. got_smallest = 1;
  72. if (x == 8)
  73. got_largest = 1;
  74. }
  75. /* These fail with probability 1/10^603. */
  76. tt_assert(got_smallest);
  77. tt_assert(got_largest);
  78. got_smallest = got_largest = 0;
  79. const uint64_t ten_billion = 10 * ((uint64_t)1000000000000);
  80. for (i = 0; i < 1000; ++i) {
  81. uint64_t x = crypto_rand_uint64_range(ten_billion, ten_billion+10);
  82. tt_u64_op(x, OP_GE, ten_billion);
  83. tt_u64_op(x, OP_LT, ten_billion+10);
  84. if (x == ten_billion)
  85. got_smallest = 1;
  86. if (x == ten_billion+9)
  87. got_largest = 1;
  88. }
  89. tt_assert(got_smallest);
  90. tt_assert(got_largest);
  91. const time_t now = time(NULL);
  92. for (i = 0; i < 2000; ++i) {
  93. time_t x = crypto_rand_time_range(now, now+60);
  94. tt_i64_op(x, OP_GE, now);
  95. tt_i64_op(x, OP_LT, now+60);
  96. if (x == now)
  97. got_smallest = 1;
  98. if (x == now+59)
  99. got_largest = 1;
  100. }
  101. tt_assert(got_smallest);
  102. tt_assert(got_largest);
  103. done:
  104. ;
  105. }
  106. static void
  107. test_crypto_rng_strongest(void *arg)
  108. {
  109. const char *how = arg;
  110. int broken = 0;
  111. if (how == NULL) {
  112. ;
  113. } else if (!strcmp(how, "nosyscall")) {
  114. break_strongest_rng_syscall = 1;
  115. } else if (!strcmp(how, "nofallback")) {
  116. break_strongest_rng_fallback = 1;
  117. } else if (!strcmp(how, "broken")) {
  118. broken = break_strongest_rng_syscall = break_strongest_rng_fallback = 1;
  119. }
  120. #define N 128
  121. uint8_t combine_and[N];
  122. uint8_t combine_or[N];
  123. int i, j;
  124. memset(combine_and, 0xff, N);
  125. memset(combine_or, 0, N);
  126. for (i = 0; i < 100; ++i) { /* 2^-100 chances just don't happen. */
  127. uint8_t output[N];
  128. memset(output, 0, N);
  129. if (how == NULL) {
  130. /* this one can't fail. */
  131. crypto_strongest_rand(output, sizeof(output));
  132. } else {
  133. int r = crypto_strongest_rand_raw(output, sizeof(output));
  134. if (r == -1) {
  135. if (broken) {
  136. goto done; /* we're fine. */
  137. }
  138. /* This function is allowed to break, but only if it always breaks. */
  139. tt_int_op(i, OP_EQ, 0);
  140. tt_skip();
  141. } else {
  142. tt_assert(! broken);
  143. }
  144. }
  145. for (j = 0; j < N; ++j) {
  146. combine_and[j] &= output[j];
  147. combine_or[j] |= output[j];
  148. }
  149. }
  150. for (j = 0; j < N; ++j) {
  151. tt_int_op(combine_and[j], OP_EQ, 0);
  152. tt_int_op(combine_or[j], OP_EQ, 0xff);
  153. }
  154. done:
  155. ;
  156. #undef N
  157. }
  158. static void
  159. test_crypto_rng_fast(void *arg)
  160. {
  161. (void)arg;
  162. crypto_fast_rng_t *rng = crypto_fast_rng_new();
  163. tt_assert(rng);
  164. /* Rudimentary black-block test to make sure that our prng outputs
  165. * have all bits sometimes on and all bits sometimes off. */
  166. uint64_t m1 = 0, m2 = ~(uint64_t)0;
  167. const int N = 128;
  168. for (int i=0; i < N; ++i) {
  169. uint64_t v;
  170. crypto_fast_rng_getbytes(rng, (void*)&v, sizeof(v));
  171. m1 |= v;
  172. m2 &= v;
  173. }
  174. tt_u64_op(m1, OP_EQ, ~(uint64_t)0);
  175. tt_u64_op(m2, OP_EQ, 0);
  176. /* Check range functions. */
  177. int counts[5];
  178. memset(counts, 0, sizeof(counts));
  179. for (int i=0; i < N; ++i) {
  180. unsigned u = crypto_fast_rng_get_uint(rng, 5);
  181. tt_int_op(u, OP_GE, 0);
  182. tt_int_op(u, OP_LT, 5);
  183. counts[u]++;
  184. uint64_t u64 = crypto_fast_rng_get_uint64(rng, UINT64_C(1)<<40);
  185. tt_u64_op(u64, OP_GE, 0);
  186. tt_u64_op(u64, OP_LT, UINT64_C(1)<<40);
  187. double d = crypto_fast_rng_get_double(rng);
  188. tt_assert(d >= 0.0);
  189. tt_assert(d < 1.0);
  190. }
  191. /* All values should have come up once. */
  192. for (int i=0; i<5; ++i) {
  193. tt_int_op(counts[i], OP_GT, 0);
  194. }
  195. /* per-thread rand_fast shouldn't crash or leak. */
  196. crypto_fast_rng_t *t_rng = get_thread_fast_rng();
  197. for (int i = 0; i < N; ++i) {
  198. uint64_t u64 = crypto_fast_rng_get_uint64(t_rng, UINT64_C(1)<<40);
  199. tt_u64_op(u64, OP_GE, 0);
  200. tt_u64_op(u64, OP_LT, UINT64_C(1)<<40);
  201. }
  202. done:
  203. crypto_fast_rng_free(rng);
  204. }
  205. static void
  206. test_crypto_rng_fast_whitebox(void *arg)
  207. {
  208. (void)arg;
  209. const size_t buflen = crypto_fast_rng_get_bytes_used_per_stream();
  210. char *buf = tor_malloc_zero(buflen);
  211. char *buf2 = tor_malloc_zero(buflen);
  212. char *buf3 = NULL, *buf4 = NULL;
  213. crypto_cipher_t *cipher = NULL, *cipher2 = NULL;
  214. uint8_t seed[CRYPTO_FAST_RNG_SEED_LEN];
  215. memset(seed, 0, sizeof(seed));
  216. /* Start with a prng with zero key and zero IV. */
  217. crypto_fast_rng_t *rng = crypto_fast_rng_new_from_seed(seed);
  218. tt_assert(rng);
  219. /* We'll use a stream cipher to keep in sync */
  220. cipher = crypto_cipher_new_with_iv_and_bits(seed, seed+32, 256);
  221. /* The first 48 bytes are used for the next seed -- let's make sure we have
  222. * them.
  223. */
  224. memset(seed, 0, sizeof(seed));
  225. crypto_cipher_crypt_inplace(cipher, (char*)seed, sizeof(seed));
  226. /* if we get 128 bytes, they should match the bytes from the aes256-counter
  227. * stream, starting at position 48.
  228. */
  229. crypto_fast_rng_getbytes(rng, (uint8_t*)buf, 128);
  230. memset(buf2, 0, 128);
  231. crypto_cipher_crypt_inplace(cipher, buf2, 128);
  232. tt_mem_op(buf, OP_EQ, buf2, 128);
  233. /* Try that again, with an odd number of bytes. */
  234. crypto_fast_rng_getbytes(rng, (uint8_t*)buf, 199);
  235. memset(buf2, 0, 199);
  236. crypto_cipher_crypt_inplace(cipher, buf2, 199);
  237. tt_mem_op(buf, OP_EQ, buf2, 199);
  238. /* Make sure that refilling works as expected: skip all but the last 5 bytes
  239. * of this steam. */
  240. size_t skip = buflen - (199+128) - 5;
  241. crypto_fast_rng_getbytes(rng, (uint8_t*)buf, skip);
  242. crypto_cipher_crypt_inplace(cipher, buf2, skip);
  243. /* Now get the next 128 bytes. The first 5 will come from this stream, and
  244. * the next 5 will come from the stream keyed by the new value of 'seed'. */
  245. crypto_fast_rng_getbytes(rng, (uint8_t*)buf, 128);
  246. memset(buf2, 0, 128);
  247. crypto_cipher_crypt_inplace(cipher, buf2, 5);
  248. crypto_cipher_free(cipher);
  249. cipher = crypto_cipher_new_with_iv_and_bits(seed, seed+32, 256);
  250. memset(seed, 0, sizeof(seed));
  251. crypto_cipher_crypt_inplace(cipher, (char*)seed, sizeof(seed));
  252. crypto_cipher_crypt_inplace(cipher, buf2+5, 128-5);
  253. tt_mem_op(buf, OP_EQ, buf2, 128);
  254. /* And check the next 7 bytes to make sure we didn't discard anything. */
  255. crypto_fast_rng_getbytes(rng, (uint8_t*)buf, 7);
  256. memset(buf2, 0, 7);
  257. crypto_cipher_crypt_inplace(cipher, buf2, 7);
  258. tt_mem_op(buf, OP_EQ, buf2, 7);
  259. /* Now try the optimization for long outputs. */
  260. buf3 = tor_malloc(65536);
  261. crypto_fast_rng_getbytes(rng, (uint8_t*)buf3, 65536);
  262. buf4 = tor_malloc_zero(65536);
  263. uint8_t seed2[CRYPTO_FAST_RNG_SEED_LEN];
  264. memset(seed2, 0, sizeof(seed2));
  265. crypto_cipher_crypt_inplace(cipher, (char*)seed2, sizeof(seed2));
  266. cipher2 = crypto_cipher_new_with_iv_and_bits(seed2, seed2+32, 256);
  267. crypto_cipher_crypt_inplace(cipher2, buf4, 65536);
  268. tt_mem_op(buf3, OP_EQ, buf4, 65536);
  269. done:
  270. crypto_fast_rng_free(rng);
  271. crypto_cipher_free(cipher);
  272. crypto_cipher_free(cipher2);
  273. tor_free(buf);
  274. tor_free(buf2);
  275. tor_free(buf3);
  276. tor_free(buf4);
  277. }
  278. struct testcase_t crypto_rng_tests[] = {
  279. { "rng", test_crypto_rng, 0, NULL, NULL },
  280. { "rng_range", test_crypto_rng_range, 0, NULL, NULL },
  281. { "rng_strongest", test_crypto_rng_strongest, TT_FORK, NULL, NULL },
  282. { "rng_strongest_nosyscall", test_crypto_rng_strongest, TT_FORK,
  283. &passthrough_setup, (void*)"nosyscall" },
  284. { "rng_strongest_nofallback", test_crypto_rng_strongest, TT_FORK,
  285. &passthrough_setup, (void*)"nofallback" },
  286. { "rng_strongest_broken", test_crypto_rng_strongest, TT_FORK,
  287. &passthrough_setup, (void*)"broken" },
  288. { "fast", test_crypto_rng_fast, 0, NULL, NULL },
  289. { "fast_whitebox", test_crypto_rng_fast_whitebox, 0, NULL, NULL },
  290. END_OF_TESTCASES
  291. };