1
0

Random_test.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 "io/FileWriter.h"
  18. #include "memory/Allocator.h"
  19. #include "memory/MallocAllocator.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 = Random_new(alloc, logger, NULL);
  70. struct Random* rand2 = Random_new(alloc, logger, NULL);
  71. Random_bytes(rand, buff, 32);
  72. Random_bytes(rand2, buff, 32);
  73. Assert_true(Bits_memcmp(buff, buff2, 32));
  74. }
  75. int main()
  76. {
  77. struct Allocator* alloc = MallocAllocator_new(1<<20);
  78. struct RandomSeed* seed = DeterminentRandomSeed_new(alloc, NULL);
  79. struct Writer* w = FileWriter_new(stdout, alloc);
  80. struct Log* logger = WriterLog_new(w, alloc);
  81. struct Random* rand = Random_newWithSeed(alloc, logger, seed, NULL);
  82. // < 8 length
  83. checkBytes(rand, 0, 3);
  84. checkBytes(rand, 3, 6);
  85. // unaligned beginning (length and align add up to a multiple of 8)
  86. checkBytes(rand, 3, 29); // 1 byte aligned
  87. checkBytes(rand, 6, 10); // 2 byte aligned
  88. checkBytes(rand, 4, 20); // 4 byte aligned
  89. // unaligned end
  90. checkBytes(rand, 0, 9); // 1 byte
  91. checkBytes(rand, 0, 14); // 2 byte
  92. checkBytes(rand, 0, 20); // 4 byte
  93. // both unaligned
  94. checkBytes(rand, 1, 28); // 1,1 byte aligned
  95. checkBytes(rand, 3, 19); // 1,2 byte aligned
  96. checkBytes(rand, 5, 23); // 1,4 byte aligned
  97. checkBytes(rand, 2, 9); // 2,1 byte aligned
  98. checkBytes(rand, 6, 12); // 2,2 byte aligned
  99. checkBytes(rand, 6, 14); // 2,4 byte aligned
  100. checkBytes(rand, 4, 19); // 4,1 byte aligned
  101. checkBytes(rand, 4, 10); // 4,2 byte aligned
  102. checkBytes(rand, 4, 32); // 4,4 byte aligned
  103. test179(alloc, logger);
  104. /* torture
  105. uint8_t selections[2];
  106. for (int i = 0; i < 100000; i++) {
  107. Random_bytes(rand, selections, 2);
  108. checkBytes(rand, selections[0] % 8, selections[1] % 128);
  109. }*/
  110. Allocator_free(alloc);
  111. return 0;
  112. }