test_crypto_hash.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2002, 2003, 2004, 2006, 2009 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @author Christian Grothoff
  18. * @file util/test_crypto_hash.c
  19. * @brief Test for crypto_hash.c
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. static char block[65536];
  24. #define FILENAME "testblock.dat"
  25. static int
  26. test (int number)
  27. {
  28. struct GNUNET_HashCode h1;
  29. struct GNUNET_HashCode h2;
  30. struct GNUNET_CRYPTO_HashAsciiEncoded enc;
  31. memset (&h1, number, sizeof(struct GNUNET_HashCode));
  32. GNUNET_CRYPTO_hash_to_enc (&h1, &enc);
  33. if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &h2))
  34. {
  35. printf ("enc2hash failed!\n");
  36. return 1;
  37. }
  38. if (0 != memcmp (&h1, &h2, sizeof(struct GNUNET_HashCode)))
  39. return 1;
  40. return 0;
  41. }
  42. static int
  43. testEncoding ()
  44. {
  45. int i;
  46. for (i = 0; i < 255; i++)
  47. if (0 != test (i))
  48. return 1;
  49. return 0;
  50. }
  51. static int
  52. testArithmetic ()
  53. {
  54. struct GNUNET_HashCode h1;
  55. struct GNUNET_HashCode h2;
  56. struct GNUNET_HashCode d;
  57. struct GNUNET_HashCode s;
  58. struct GNUNET_CRYPTO_SymmetricSessionKey skey;
  59. struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
  60. GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &h1);
  61. GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &h2);
  62. if (GNUNET_CRYPTO_hash_distance_u32 (&h1, &h2) !=
  63. GNUNET_CRYPTO_hash_distance_u32 (&h2, &h1))
  64. return 1;
  65. GNUNET_CRYPTO_hash_difference (&h1, &h2, &d);
  66. GNUNET_CRYPTO_hash_sum (&h1, &d, &s);
  67. if (0 != GNUNET_CRYPTO_hash_cmp (&s, &h2))
  68. return 1;
  69. GNUNET_CRYPTO_hash_xor (&h1, &h2, &d);
  70. GNUNET_CRYPTO_hash_xor (&h1, &d, &s);
  71. if (0 != GNUNET_CRYPTO_hash_cmp (&s, &h2))
  72. return 1;
  73. if (0 != GNUNET_CRYPTO_hash_xorcmp (&s, &h2, &h1))
  74. return 1;
  75. if (-1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h1))
  76. return 1;
  77. if (1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h2))
  78. return 1;
  79. memset (&d, 0xF0, sizeof(d));
  80. if (0 != GNUNET_CRYPTO_hash_get_bit (&d, 3))
  81. return 1;
  82. if (1 != GNUNET_CRYPTO_hash_get_bit (&d, 6))
  83. return 1;
  84. memset (&d, 0, sizeof(d));
  85. GNUNET_CRYPTO_hash_to_aes_key (&d, &skey, &iv);
  86. return 0;
  87. }
  88. static void
  89. finished_task (void *cls, const struct GNUNET_HashCode *res)
  90. {
  91. int *ret = cls;
  92. struct GNUNET_HashCode want;
  93. GNUNET_CRYPTO_hash (block, sizeof(block), &want);
  94. if (0 != memcmp (res, &want, sizeof(want)))
  95. *ret = 2;
  96. else
  97. *ret = 0;
  98. }
  99. static void
  100. file_hasher (void *cls)
  101. {
  102. GNUNET_assert (NULL !=
  103. GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
  104. FILENAME, 1024, &finished_task, cls));
  105. }
  106. static int
  107. testFileHash ()
  108. {
  109. int ret;
  110. FILE *f;
  111. memset (block, 42, sizeof(block) / 2);
  112. memset (&block[sizeof(block) / 2], 43, sizeof(block) / 2);
  113. GNUNET_assert (NULL != (f = fopen (FILENAME, "w+")));
  114. GNUNET_break (sizeof(block) == fwrite (block, 1, sizeof(block), f));
  115. GNUNET_break (0 == fclose (f));
  116. ret = 1;
  117. GNUNET_SCHEDULER_run (&file_hasher, &ret);
  118. GNUNET_break (0 == unlink (FILENAME));
  119. return ret;
  120. }
  121. int
  122. main (int argc, char *argv[])
  123. {
  124. int failureCount = 0;
  125. int i;
  126. GNUNET_log_setup ("test-crypto-hash", "WARNING", NULL);
  127. for (i = 0; i < 10; i++)
  128. failureCount += testEncoding ();
  129. failureCount += testArithmetic ();
  130. failureCount += testFileHash ();
  131. if (failureCount != 0)
  132. return 1;
  133. return 0;
  134. }
  135. /* end of hashingtest.c */