You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.0 KiB

  1. #define TEST_NAME "randombytes"
  2. #include "cmptest.h"
  3. unsigned char x[65536];
  4. unsigned long long freq[256];
  5. static int compat_tests(void)
  6. {
  7. size_t i;
  8. memset(x, 0, sizeof x);
  9. randombytes(x, sizeof x);
  10. for (i = 0; i < 256; ++i) {
  11. freq[i] = 0;
  12. }
  13. for (i = 0; i < sizeof x; ++i) {
  14. ++freq[255 & (int)x[i]];
  15. }
  16. for (i = 0; i < 256; ++i) {
  17. if (!freq[i]) {
  18. printf("nacl_tests failed\n");
  19. }
  20. }
  21. return 0;
  22. }
  23. static int randombytes_tests(void)
  24. {
  25. unsigned int i;
  26. assert(strcmp(randombytes_implementation_name(), "sysrandom") == 0);
  27. randombytes(x, 1U);
  28. randombytes_close();
  29. for (i = 0; i < 256; ++i) {
  30. freq[i] = 0;
  31. }
  32. for (i = 0; i < 65536; ++i) {
  33. ++freq[randombytes_uniform(256)];
  34. }
  35. for (i = 0; i < 256; ++i) {
  36. if (!freq[i]) {
  37. printf("randombytes_uniform() test failed\n");
  38. }
  39. }
  40. assert(randombytes_uniform(1U) == 0U);
  41. randombytes_close();
  42. randombytes_set_implementation(&randombytes_salsa20_implementation);
  43. assert(strcmp(randombytes_implementation_name(), "salsa20") == 0);
  44. randombytes_stir();
  45. for (i = 0; i < 256; ++i) {
  46. freq[i] = 0;
  47. }
  48. for (i = 0; i < 65536; ++i) {
  49. ++freq[randombytes_uniform(256)];
  50. }
  51. for (i = 0; i < 256; ++i) {
  52. if (!freq[i]) {
  53. printf("randombytes_uniform() test failed\n");
  54. }
  55. }
  56. memset(x, 0, sizeof x);
  57. randombytes_buf(x, sizeof x);
  58. for (i = 0; i < 256; ++i) {
  59. freq[i] = 0;
  60. }
  61. for (i = 0; i < sizeof x; ++i) {
  62. ++freq[255 & (int)x[i]];
  63. }
  64. for (i = 0; i < 256; ++i) {
  65. if (!freq[i]) {
  66. printf("randombytes_buf() test failed\n");
  67. }
  68. }
  69. assert(randombytes_uniform(1U) == 0U);
  70. randombytes_close();
  71. randombytes(x, 1U);
  72. randombytes_close();
  73. return 0;
  74. }
  75. int main(void)
  76. {
  77. compat_tests();
  78. randombytes_tests();
  79. printf("OK\n");
  80. return 0;
  81. }