Random_test.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/random/Random.h"
  16. #include "crypto/random/test/DeterminentRandomSeed.h"
  17. #include "exception/Err.h"
  18. #include "io/FileWriter.h"
  19. #include "memory/Allocator.h"
  20. #include "util/Assert.h"
  21. #include "util/Bits.h"
  22. #include "util/log/Log.h"
  23. #include "util/log/WriterLog.h"
  24. static void checkBytes(struct Random* rand, int alignment, int length)
  25. {
  26. Assert_true(length < 128 && alignment < 8);
  27. uint64_t buff64[20] = {0};
  28. uint8_t* buff = (uint8_t*) (&buff64[1]);
  29. buff += alignment;
  30. // Check for bytes which are always the same, a few \0s are ok
  31. // but if every cycle they are always zero then there's a bug.
  32. uint8_t oldBuff[128] = {0};
  33. // Preload into the output buff so alignment is same.
  34. Random_bytes(rand, buff, length);
  35. Bits_memcpy(oldBuff, buff, length);
  36. uint8_t sameAsOld[128];
  37. Bits_memset(sameAsOld, 0xff, 128);
  38. // Check for bytes which are the same as other bytes every time.
  39. // if buff[3] always equals buff[8] then there's a bug.
  40. uint8_t sameBytes[128+128];
  41. Bits_memset(sameBytes, 0xff, 128+128);
  42. for (int i = 0; i < 100; i++) {
  43. Random_bytes(rand, buff, length);
  44. for (int j = 0; j < length; j++) {
  45. for (int jj = j; jj < length; jj++) {
  46. sameBytes[j+jj] &= (jj != j && buff[j] == buff[jj]);
  47. }
  48. }
  49. for (int j = 0; j < length; j++) {
  50. sameAsOld[j] &= (oldBuff[i] == buff[i]);
  51. }
  52. // Check that the function did not write after or before the buffer.
  53. uint8_t* origBuff = (uint8_t*) (buff64);
  54. Assert_true(Bits_isZero(origBuff, 8+alignment));
  55. Assert_true(Bits_isZero(buff+length, 8));
  56. }
  57. for (int i = 0; i < length+length-1; i++) {
  58. Assert_true(!sameBytes[i]);
  59. }
  60. for (int i = 0; i < length; i++) {
  61. Assert_true(!sameAsOld[i]);
  62. }
  63. }
  64. /** https://github.com/cjdelisle/cjdns/issues/179 */
  65. static void test179(struct Allocator* alloc, struct Log* logger)
  66. {
  67. uint8_t buff[32] = {0};
  68. uint8_t buff2[32] = {0};
  69. struct Random* rand = NULL;
  70. Err_assert(Random_new(&rand, alloc, logger));
  71. struct Random* rand2 = NULL;
  72. Err_assert(Random_new(&rand2, alloc, logger));
  73. Random_bytes(rand, buff, 32);
  74. Random_bytes(rand2, buff, 32);
  75. Assert_true(Bits_memcmp(buff, buff2, 32));
  76. }
  77. int main()
  78. {
  79. struct Allocator* alloc = Allocator_new(1<<20);
  80. RandomSeed_t* seed = DeterminentRandomSeed_new(alloc, NULL);
  81. struct Writer* w = FileWriter_new(stdout, alloc);
  82. struct Log* logger = WriterLog_new(w, alloc);
  83. struct Random* rand = NULL;
  84. Err_assert(Random_newWithSeed(&rand, alloc, logger, seed));
  85. // < 8 length
  86. checkBytes(rand, 0, 3);
  87. checkBytes(rand, 3, 6);
  88. // unaligned beginning (length and align add up to a multiple of 8)
  89. checkBytes(rand, 3, 29); // 1 byte aligned
  90. checkBytes(rand, 6, 10); // 2 byte aligned
  91. checkBytes(rand, 4, 20); // 4 byte aligned
  92. // unaligned end
  93. checkBytes(rand, 0, 9); // 1 byte
  94. checkBytes(rand, 0, 14); // 2 byte
  95. checkBytes(rand, 0, 20); // 4 byte
  96. // both unaligned
  97. checkBytes(rand, 1, 28); // 1,1 byte aligned
  98. checkBytes(rand, 3, 19); // 1,2 byte aligned
  99. checkBytes(rand, 5, 23); // 1,4 byte aligned
  100. checkBytes(rand, 2, 9); // 2,1 byte aligned
  101. checkBytes(rand, 6, 12); // 2,2 byte aligned
  102. checkBytes(rand, 6, 14); // 2,4 byte aligned
  103. checkBytes(rand, 4, 19); // 4,1 byte aligned
  104. checkBytes(rand, 4, 10); // 4,2 byte aligned
  105. checkBytes(rand, 4, 32); // 4,4 byte aligned
  106. test179(alloc, logger);
  107. /* torture
  108. uint8_t selections[2];
  109. for (int i = 0; i < 100000; i++) {
  110. Random_bytes(rand, selections, 2);
  111. checkBytes(rand, selections[0] % 8, selections[1] % 128);
  112. }*/
  113. Allocator_free(alloc);
  114. return 0;
  115. }