randombytes.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #define TEST_NAME "randombytes"
  2. #include "cmptest.h"
  3. static unsigned char x[65536];
  4. static unsigned long long freq[256];
  5. static int
  6. compat_tests(void)
  7. {
  8. size_t i;
  9. memset(x, 0, sizeof x);
  10. randombytes(x, sizeof x);
  11. for (i = 0; i < 256; ++i) {
  12. freq[i] = 0;
  13. }
  14. for (i = 0; i < sizeof x; ++i) {
  15. ++freq[255 & (int) x[i]];
  16. }
  17. for (i = 0; i < 256; ++i) {
  18. if (!freq[i]) {
  19. printf("nacl_tests failed\n");
  20. }
  21. }
  22. return 0;
  23. }
  24. static int
  25. randombytes_tests(void)
  26. {
  27. static const unsigned char seed[randombytes_SEEDBYTES] = {
  28. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
  29. 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
  30. 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  31. };
  32. unsigned char out[100];
  33. unsigned int f = 0U;
  34. unsigned int i;
  35. uint32_t n;
  36. #ifndef BENCHMARKS
  37. # ifdef __EMSCRIPTEN__
  38. assert(strcmp(randombytes_implementation_name(), "js") == 0);
  39. # elif defined(__native_client__)
  40. assert(strcmp(randombytes_implementation_name(), "nativeclient") == 0);
  41. # else
  42. assert(strcmp(randombytes_implementation_name(), "sysrandom") == 0);
  43. # endif
  44. #endif
  45. randombytes(x, 1U);
  46. do {
  47. n = randombytes_random();
  48. f |= ((n >> 24) > 1);
  49. f |= ((n >> 16) > 1) << 1;
  50. f |= ((n >> 8) > 1) << 2;
  51. f |= ((n) > 1) << 3;
  52. f |= (n > 0x7fffffff) << 4;
  53. } while (f != 0x1f);
  54. randombytes_close();
  55. for (i = 0; i < 256; ++i) {
  56. freq[i] = 0;
  57. }
  58. for (i = 0; i < 65536; ++i) {
  59. ++freq[randombytes_uniform(256)];
  60. }
  61. for (i = 0; i < 256; ++i) {
  62. if (!freq[i]) {
  63. printf("randombytes_uniform() test failed\n");
  64. }
  65. }
  66. assert(randombytes_uniform(1U) == 0U);
  67. randombytes_close();
  68. #ifndef __EMSCRIPTEN__
  69. randombytes_set_implementation(&randombytes_salsa20_implementation);
  70. assert(strcmp(randombytes_implementation_name(), "salsa20") == 0);
  71. #endif
  72. randombytes_stir();
  73. for (i = 0; i < 256; ++i) {
  74. freq[i] = 0;
  75. }
  76. for (i = 0; i < 65536; ++i) {
  77. ++freq[randombytes_uniform(256)];
  78. }
  79. for (i = 0; i < 256; ++i) {
  80. if (!freq[i]) {
  81. printf("randombytes_uniform() test failed\n");
  82. }
  83. }
  84. memset(x, 0, sizeof x);
  85. randombytes_buf(x, sizeof x);
  86. for (i = 0; i < 256; ++i) {
  87. freq[i] = 0;
  88. }
  89. for (i = 0; i < sizeof x; ++i) {
  90. ++freq[255 & (int) x[i]];
  91. }
  92. for (i = 0; i < 256; ++i) {
  93. if (!freq[i]) {
  94. printf("randombytes_buf() test failed\n");
  95. }
  96. }
  97. assert(randombytes_uniform(1U) == 0U);
  98. randombytes_buf_deterministic(out, sizeof out, seed);
  99. for (i = 0; i < sizeof out; ++i) {
  100. printf("%02x", out[i]);
  101. }
  102. printf(" (deterministic)\n");
  103. randombytes_close();
  104. randombytes(x, 1U);
  105. randombytes_close();
  106. assert(randombytes_SEEDBYTES > 0);
  107. assert(randombytes_seedbytes() == randombytes_SEEDBYTES);
  108. return 0;
  109. }
  110. static uint32_t
  111. randombytes_uniform_impl(const uint32_t upper_bound)
  112. {
  113. return upper_bound;
  114. }
  115. static int
  116. impl_tests(void)
  117. {
  118. #ifndef __native_client__
  119. randombytes_implementation impl = randombytes_sysrandom_implementation;
  120. #else
  121. randombytes_implementation impl = randombytes_nativeclient_implementation;
  122. #endif
  123. uint32_t v = randombytes_random();
  124. impl.uniform = randombytes_uniform_impl;
  125. randombytes_close();
  126. randombytes_set_implementation(&impl);
  127. assert(randombytes_uniform(1) == 1);
  128. assert(randombytes_uniform(v) == v);
  129. assert(randombytes_uniform(v) == v);
  130. assert(randombytes_uniform(v) == v);
  131. assert(randombytes_uniform(v) == v);
  132. randombytes_close();
  133. impl.close = NULL;
  134. randombytes_close();
  135. return 0;
  136. }
  137. int
  138. main(void)
  139. {
  140. compat_tests();
  141. randombytes_tests();
  142. #ifndef __EMSCRIPTEN__
  143. impl_tests();
  144. #endif
  145. printf("OK\n");
  146. randombytes_set_implementation(&randombytes_salsa20_implementation);
  147. return 0;
  148. }