1
0

Random_test.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "util/Assert.h"
  20. #include "util/Bits.h"
  21. #include "util/log/Log.h"
  22. #include "util/log/WriterLog.h"
  23. static void checkBytes(struct Random* rand, int alignment, int length)
  24. {
  25. Assert_true(length < 128 && alignment < 8);
  26. uint64_t buff64[20] = {0};
  27. uint8_t* buff = (uint8_t*) (&buff64[1]);
  28. buff += alignment;
  29. // Check for bytes which are always the same, a few \0s are ok
  30. // but if every cycle they are always zero then there's a bug.
  31. uint8_t oldBuff[128] = {0};
  32. // Preload into the output buff so alignment is same.
  33. Random_bytes(rand, buff, length);
  34. Bits_memcpy(oldBuff, buff, length);
  35. uint8_t sameAsOld[128];
  36. Bits_memset(sameAsOld, 0xff, 128);
  37. // Check for bytes which are the same as other bytes every time.
  38. // if buff[3] always equals buff[8] then there's a bug.
  39. uint8_t sameBytes[128+128];
  40. Bits_memset(sameBytes, 0xff, 128+128);
  41. for (int i = 0; i < 100; i++) {
  42. Random_bytes(rand, buff, length);
  43. for (int j = 0; j < length; j++) {
  44. for (int jj = j; jj < length; jj++) {
  45. sameBytes[j+jj] &= (jj != j && buff[j] == buff[jj]);
  46. }
  47. }
  48. for (int j = 0; j < length; j++) {
  49. sameAsOld[j] &= (oldBuff[i] == buff[i]);
  50. }
  51. // Check that the function did not write after or before the buffer.
  52. uint8_t* origBuff = (uint8_t*) (buff64);
  53. Assert_true(Bits_isZero(origBuff, 8+alignment));
  54. Assert_true(Bits_isZero(buff+length, 8));
  55. }
  56. for (int i = 0; i < length+length-1; i++) {
  57. Assert_true(!sameBytes[i]);
  58. }
  59. for (int i = 0; i < length; i++) {
  60. Assert_true(!sameAsOld[i]);
  61. }
  62. }
  63. /** https://github.com/cjdelisle/cjdns/issues/179 */
  64. static void test179(struct Allocator* alloc, struct Log* logger)
  65. {
  66. uint8_t buff[32] = {0};
  67. uint8_t buff2[32] = {0};
  68. struct Random* rand = Random_new(alloc, logger, NULL);
  69. struct Random* rand2 = Random_new(alloc, logger, NULL);
  70. Random_bytes(rand, buff, 32);
  71. Random_bytes(rand2, buff, 32);
  72. Assert_true(Bits_memcmp(buff, buff2, 32));
  73. }
  74. int main()
  75. {
  76. struct Allocator* alloc = Allocator_new(1<<20);
  77. RandomSeed_t* seed = DeterminentRandomSeed_new(alloc, NULL);
  78. struct Writer* w = FileWriter_new(stdout, alloc);
  79. struct Log* logger = WriterLog_new(w, alloc);
  80. struct Random* rand = Random_newWithSeed(alloc, logger, seed, NULL);
  81. // < 8 length
  82. checkBytes(rand, 0, 3);
  83. checkBytes(rand, 3, 6);
  84. // unaligned beginning (length and align add up to a multiple of 8)
  85. checkBytes(rand, 3, 29); // 1 byte aligned
  86. checkBytes(rand, 6, 10); // 2 byte aligned
  87. checkBytes(rand, 4, 20); // 4 byte aligned
  88. // unaligned end
  89. checkBytes(rand, 0, 9); // 1 byte
  90. checkBytes(rand, 0, 14); // 2 byte
  91. checkBytes(rand, 0, 20); // 4 byte
  92. // both unaligned
  93. checkBytes(rand, 1, 28); // 1,1 byte aligned
  94. checkBytes(rand, 3, 19); // 1,2 byte aligned
  95. checkBytes(rand, 5, 23); // 1,4 byte aligned
  96. checkBytes(rand, 2, 9); // 2,1 byte aligned
  97. checkBytes(rand, 6, 12); // 2,2 byte aligned
  98. checkBytes(rand, 6, 14); // 2,4 byte aligned
  99. checkBytes(rand, 4, 19); // 4,1 byte aligned
  100. checkBytes(rand, 4, 10); // 4,2 byte aligned
  101. checkBytes(rand, 4, 32); // 4,4 byte aligned
  102. test179(alloc, logger);
  103. /* torture
  104. uint8_t selections[2];
  105. for (int i = 0; i < 100000; i++) {
  106. Random_bytes(rand, selections, 2);
  107. checkBytes(rand, selections[0] % 8, selections[1] % 128);
  108. }*/
  109. Allocator_free(alloc);
  110. return 0;
  111. }