test_crypto_eddsa.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 ()
  31. {
  32. struct GNUNET_CRYPTO_EddsaSignature sig;
  33. struct GNUNET_CRYPTO_EccSignaturePurpose purp;
  34. struct GNUNET_CRYPTO_EddsaPublicKey pkey;
  35. int i;
  36. struct GNUNET_TIME_Absolute start;
  37. int ok = GNUNET_OK;
  38. fprintf (stderr, "%s", "W");
  39. GNUNET_CRYPTO_eddsa_key_get_public (key, &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 (i = 0; i < ITER; i++)
  44. {
  45. fprintf (stderr, "%s", "."); fflush (stderr);
  46. if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign (key, &purp, &sig))
  47. {
  48. fprintf (stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
  49. ok = GNUNET_SYSERR;
  50. continue;
  51. }
  52. if (GNUNET_SYSERR ==
  53. GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
  54. &pkey))
  55. {
  56. printf ("GNUNET_CRYPTO_eddsa_verify failed!\n");
  57. ok = GNUNET_SYSERR;
  58. continue;
  59. }
  60. if (GNUNET_SYSERR !=
  61. GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
  62. &purp, &sig, &pkey))
  63. {
  64. printf ("GNUNET_CRYPTO_eddsa_verify failed to fail!\n");
  65. ok = GNUNET_SYSERR;
  66. continue;
  67. }
  68. }
  69. printf ("%d EdDSA sign/verify operations %s\n", ITER,
  70. GNUNET_STRINGS_relative_time_to_string (
  71. GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
  72. return ok;
  73. }
  74. #if PERF
  75. static int
  76. testSignPerformance ()
  77. {
  78. struct GNUNET_CRYPTO_EccSignaturePurpose purp;
  79. struct GNUNET_CRYPTO_EddsaSignature sig;
  80. struct GNUNET_CRYPTO_EddsaPublicKey pkey;
  81. int i;
  82. struct GNUNET_TIME_Absolute start;
  83. int ok = GNUNET_OK;
  84. purp.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
  85. purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
  86. fprintf (stderr, "%s", "W");
  87. GNUNET_CRYPTO_eddsa_key_get_public (key, &pkey);
  88. start = GNUNET_TIME_absolute_get ();
  89. for (i = 0; i < ITER; i++)
  90. {
  91. fprintf (stderr, "%s", "."); fflush (stderr);
  92. if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign (key, &purp, &sig))
  93. {
  94. fprintf (stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
  95. ok = GNUNET_SYSERR;
  96. continue;
  97. }
  98. }
  99. printf ("%d EdDSA sign operations %s\n", ITER,
  100. GNUNET_STRINGS_relative_time_to_string (
  101. GNUNET_TIME_absolute_get_duration (start),
  102. GNUNET_YES));
  103. return ok;
  104. }
  105. #endif
  106. static int
  107. testCreateFromFile ()
  108. {
  109. struct GNUNET_CRYPTO_EddsaPublicKey p1;
  110. struct GNUNET_CRYPTO_EddsaPublicKey p2;
  111. key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
  112. GNUNET_assert (NULL != key);
  113. GNUNET_CRYPTO_eddsa_key_get_public (key, &p1);
  114. GNUNET_free (key);
  115. key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
  116. GNUNET_assert (NULL != key);
  117. GNUNET_CRYPTO_eddsa_key_get_public (key, &p2);
  118. GNUNET_assert (0 == memcmp (&p1, &p2, sizeof(p1)));
  119. GNUNET_free (key);
  120. GNUNET_assert (0 == unlink (KEYFILE));
  121. key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
  122. GNUNET_assert (NULL != key);
  123. GNUNET_CRYPTO_eddsa_key_get_public (key, &p2);
  124. GNUNET_assert (0 != memcmp (&p1, &p2, sizeof(p1)));
  125. GNUNET_free (key);
  126. return GNUNET_OK;
  127. }
  128. static void
  129. perf_keygen ()
  130. {
  131. struct GNUNET_TIME_Absolute start;
  132. struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
  133. int i;
  134. fprintf (stderr, "%s", "W");
  135. start = GNUNET_TIME_absolute_get ();
  136. for (i = 0; i < 10; i++)
  137. {
  138. fprintf (stderr, "."); fflush (stderr);
  139. pk = GNUNET_CRYPTO_eddsa_key_create ();
  140. GNUNET_free (pk);
  141. }
  142. for (; i < 25; i++)
  143. fprintf (stderr, ".");
  144. fflush (stderr);
  145. printf ("10 EdDSA keys created in %s\n",
  146. GNUNET_STRINGS_relative_time_to_string (
  147. GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
  148. }
  149. int
  150. main (int argc, char *argv[])
  151. {
  152. int failure_count = 0;
  153. if (! gcry_check_version ("1.6.0"))
  154. {
  155. fprintf (stderr,
  156. _ (
  157. "libgcrypt has not the expected version (version %s is required).\n"),
  158. "1.6.0");
  159. return 0;
  160. }
  161. if (getenv ("GNUNET_GCRYPT_DEBUG"))
  162. gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
  163. GNUNET_log_setup ("test-crypto-eddsa", "WARNING", NULL);
  164. key = GNUNET_CRYPTO_eddsa_key_create ();
  165. #if PERF
  166. if (GNUNET_OK != testSignPerformance ())
  167. failure_count++;
  168. #endif
  169. if (GNUNET_OK != testSignVerify ())
  170. failure_count++;
  171. GNUNET_free (key);
  172. if (GNUNET_OK != testCreateFromFile ())
  173. failure_count++;
  174. GNUNET_assert (0 == unlink (KEYFILE));
  175. perf_keygen ();
  176. if (0 != failure_count)
  177. {
  178. fprintf (stderr,
  179. "\n\n%d TESTS FAILED!\n\n",
  180. failure_count);
  181. return -1;
  182. }
  183. return 0;
  184. }
  185. /* end of test_crypto_eddsa.c */