test_crypto_eddsa.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2002-2013 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. * @file util/test_crypto_eddsa.c
  18. * @brief testcase for ECC public key crypto
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_signatures.h"
  24. #include <gcrypt.h>
  25. #define ITER 25
  26. #define KEYFILE "/tmp/test-gnunet-crypto-eddsa.key"
  27. #define PERF GNUNET_YES
  28. static struct GNUNET_CRYPTO_EddsaPrivateKey key;
  29. static int
  30. testSignVerify (void)
  31. {
  32. struct GNUNET_CRYPTO_EddsaSignature sig;
  33. struct GNUNET_CRYPTO_EccSignaturePurpose purp;
  34. struct GNUNET_CRYPTO_EddsaPublicKey pkey;
  35. struct GNUNET_TIME_Absolute start;
  36. int ok = GNUNET_OK;
  37. fprintf (stderr, "%s", "W");
  38. GNUNET_CRYPTO_eddsa_key_get_public (&key,
  39. &pkey);
  40. start = GNUNET_TIME_absolute_get ();
  41. purp.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
  42. purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
  43. for (unsigned int i = 0; i < ITER; i++)
  44. {
  45. fprintf (stderr, "%s", "."); fflush (stderr);
  46. if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign_ (&key,
  47. &purp,
  48. &sig))
  49. {
  50. fprintf (stderr,
  51. "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
  52. ok = GNUNET_SYSERR;
  53. continue;
  54. }
  55. if (GNUNET_SYSERR ==
  56. GNUNET_CRYPTO_eddsa_verify_ (GNUNET_SIGNATURE_PURPOSE_TEST,
  57. &purp,
  58. &sig,
  59. &pkey))
  60. {
  61. fprintf (stderr,
  62. "GNUNET_CRYPTO_eddsa_verify failed!\n");
  63. ok = GNUNET_SYSERR;
  64. continue;
  65. }
  66. if (GNUNET_SYSERR !=
  67. GNUNET_CRYPTO_eddsa_verify_ (
  68. GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
  69. &purp,
  70. &sig,
  71. &pkey))
  72. {
  73. fprintf (stderr,
  74. "GNUNET_CRYPTO_eddsa_verify failed to fail!\n");
  75. ok = GNUNET_SYSERR;
  76. continue;
  77. }
  78. }
  79. fprintf (stderr, "\n");
  80. printf ("%d EdDSA sign/verify operations %s\n",
  81. ITER,
  82. GNUNET_STRINGS_relative_time_to_string (
  83. GNUNET_TIME_absolute_get_duration (start),
  84. GNUNET_YES));
  85. return ok;
  86. }
  87. #if PERF
  88. static int
  89. testSignPerformance ()
  90. {
  91. struct GNUNET_CRYPTO_EccSignaturePurpose purp;
  92. struct GNUNET_CRYPTO_EddsaSignature sig;
  93. struct GNUNET_CRYPTO_EddsaPublicKey pkey;
  94. struct GNUNET_TIME_Absolute start;
  95. int ok = GNUNET_OK;
  96. purp.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
  97. purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
  98. fprintf (stderr, "%s", "W");
  99. GNUNET_CRYPTO_eddsa_key_get_public (&key,
  100. &pkey);
  101. start = GNUNET_TIME_absolute_get ();
  102. for (unsigned int i = 0; i < ITER; i++)
  103. {
  104. fprintf (stderr, "%s", ".");
  105. fflush (stderr);
  106. if (GNUNET_SYSERR ==
  107. GNUNET_CRYPTO_eddsa_sign_ (&key,
  108. &purp,
  109. &sig))
  110. {
  111. fprintf (stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
  112. ok = GNUNET_SYSERR;
  113. continue;
  114. }
  115. }
  116. fprintf (stderr, "\n");
  117. printf ("%d EdDSA sign operations %s\n",
  118. ITER,
  119. GNUNET_STRINGS_relative_time_to_string (
  120. GNUNET_TIME_absolute_get_duration (start),
  121. GNUNET_YES));
  122. return ok;
  123. }
  124. #endif
  125. static int
  126. testCreateFromFile (void)
  127. {
  128. struct GNUNET_CRYPTO_EddsaPublicKey p1;
  129. struct GNUNET_CRYPTO_EddsaPublicKey p2;
  130. GNUNET_assert (0 <=
  131. GNUNET_CRYPTO_eddsa_key_from_file (KEYFILE,
  132. GNUNET_YES,
  133. &key));
  134. GNUNET_CRYPTO_eddsa_key_get_public (&key,
  135. &p1);
  136. GNUNET_assert (GNUNET_NO ==
  137. GNUNET_CRYPTO_eddsa_key_from_file (KEYFILE,
  138. GNUNET_YES,
  139. &key));
  140. GNUNET_CRYPTO_eddsa_key_get_public (&key,
  141. &p2);
  142. GNUNET_assert (0 ==
  143. GNUNET_memcmp (&p1,
  144. &p2));
  145. GNUNET_assert (0 == unlink (KEYFILE));
  146. GNUNET_assert (GNUNET_OK ==
  147. GNUNET_CRYPTO_eddsa_key_from_file (KEYFILE,
  148. GNUNET_NO,
  149. &key));
  150. GNUNET_CRYPTO_eddsa_key_get_public (&key,
  151. &p2);
  152. GNUNET_assert (0 !=
  153. GNUNET_memcmp (&p1,
  154. &p2));
  155. return GNUNET_OK;
  156. }
  157. static void
  158. perf_keygen (void)
  159. {
  160. struct GNUNET_TIME_Absolute start;
  161. struct GNUNET_CRYPTO_EddsaPrivateKey pk;
  162. fprintf (stderr, "%s", "W");
  163. start = GNUNET_TIME_absolute_get ();
  164. for (unsigned int i = 0; i < 10; i++)
  165. {
  166. fprintf (stderr, ".");
  167. fflush (stderr);
  168. GNUNET_CRYPTO_eddsa_key_create (&pk);
  169. }
  170. fprintf (stderr, "\n");
  171. printf ("10 EdDSA keys created in %s\n",
  172. GNUNET_STRINGS_relative_time_to_string (
  173. GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
  174. }
  175. int
  176. main (int argc, char *argv[])
  177. {
  178. int failure_count = 0;
  179. if (! gcry_check_version ("1.6.0"))
  180. {
  181. fprintf (stderr,
  182. "libgcrypt has not the expected version (version %s is required).\n",
  183. "1.6.0");
  184. return 0;
  185. }
  186. if (getenv ("GNUNET_GCRYPT_DEBUG"))
  187. gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
  188. GNUNET_log_setup ("test-crypto-eddsa",
  189. "WARNING",
  190. NULL);
  191. GNUNET_CRYPTO_eddsa_key_create (&key);
  192. #if PERF
  193. if (GNUNET_OK != testSignPerformance ())
  194. failure_count++;
  195. #endif
  196. if (GNUNET_OK != testSignVerify ())
  197. failure_count++;
  198. if (GNUNET_OK != testCreateFromFile ())
  199. failure_count++;
  200. GNUNET_assert (0 == unlink (KEYFILE));
  201. perf_keygen ();
  202. if (0 != failure_count)
  203. {
  204. fprintf (stderr,
  205. "\n\n%d TESTS FAILED!\n\n",
  206. failure_count);
  207. return -1;
  208. }
  209. return 0;
  210. }
  211. /* end of test_crypto_eddsa.c */