test_crypto_rng.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. struct testcase_t crypto_rng_tests[] = {
  159. { "rng", test_crypto_rng, 0, NULL, NULL },
  160. { "rng_range", test_crypto_rng_range, 0, NULL, NULL },
  161. { "rng_strongest", test_crypto_rng_strongest, TT_FORK, NULL, NULL },
  162. { "rng_strongest_nosyscall", test_crypto_rng_strongest, TT_FORK,
  163. &passthrough_setup, (void*)"nosyscall" },
  164. { "rng_strongest_nofallback", test_crypto_rng_strongest, TT_FORK,
  165. &passthrough_setup, (void*)"nofallback" },
  166. { "rng_strongest_broken", test_crypto_rng_strongest, TT_FORK,
  167. &passthrough_setup, (void*)"broken" },
  168. END_OF_TESTCASES
  169. };