test_crypto_ecdsa.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_ecdsa.c
  18. * @brief testcase for ECC ECDSA 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 PERF GNUNET_YES
  27. static struct GNUNET_CRYPTO_EcdsaPrivateKey *key;
  28. static int
  29. testSignVerify ()
  30. {
  31. struct GNUNET_CRYPTO_EcdsaSignature sig;
  32. struct GNUNET_CRYPTO_EccSignaturePurpose purp;
  33. struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
  34. int i;
  35. struct GNUNET_TIME_Absolute start;
  36. int ok = GNUNET_OK;
  37. FPRINTF (stderr, "%s", "W");
  38. GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey);
  39. start = GNUNET_TIME_absolute_get ();
  40. purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
  41. purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
  42. for (i = 0; i < ITER; i++)
  43. {
  44. FPRINTF (stderr, "%s", "."); fflush (stderr);
  45. if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (key, &purp, &sig))
  46. {
  47. FPRINTF (stderr,
  48. "%s",
  49. "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
  50. ok = GNUNET_SYSERR;
  51. continue;
  52. }
  53. if (GNUNET_SYSERR ==
  54. GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
  55. &pkey))
  56. {
  57. printf ("GNUNET_CRYPTO_ecdsa_verify failed!\n");
  58. ok = GNUNET_SYSERR;
  59. continue;
  60. }
  61. if (GNUNET_SYSERR !=
  62. GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
  63. &purp, &sig, &pkey))
  64. {
  65. printf ("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n");
  66. ok = GNUNET_SYSERR;
  67. continue;
  68. }
  69. }
  70. printf ("%d ECDSA sign/verify operations %s\n", ITER,
  71. GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
  72. return ok;
  73. }
  74. static int
  75. testDeriveSignVerify ()
  76. {
  77. struct GNUNET_CRYPTO_EcdsaSignature sig;
  78. struct GNUNET_CRYPTO_EccSignaturePurpose purp;
  79. struct GNUNET_CRYPTO_EcdsaPrivateKey *dpriv;
  80. struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
  81. struct GNUNET_CRYPTO_EcdsaPublicKey dpub;
  82. dpriv = GNUNET_CRYPTO_ecdsa_private_key_derive (key, "test-derive", "test-CTX");
  83. GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey);
  84. GNUNET_CRYPTO_ecdsa_public_key_derive (&pkey, "test-derive", "test-CTX", &dpub);
  85. purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
  86. purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
  87. if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (dpriv, &purp, &sig))
  88. {
  89. FPRINTF (stderr, "%s", "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
  90. GNUNET_free (dpriv);
  91. return GNUNET_SYSERR;
  92. }
  93. if (GNUNET_SYSERR ==
  94. GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
  95. &purp, &sig,
  96. &dpub))
  97. {
  98. printf ("GNUNET_CRYPTO_ecdsa_verify failed!\n");
  99. GNUNET_free (dpriv);
  100. return GNUNET_SYSERR;
  101. }
  102. if (GNUNET_SYSERR !=
  103. GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
  104. &purp, &sig,
  105. &pkey))
  106. {
  107. printf ("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n");
  108. GNUNET_free (dpriv);
  109. return GNUNET_SYSERR;
  110. }
  111. if (GNUNET_SYSERR !=
  112. GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
  113. &purp, &sig, &dpub))
  114. {
  115. printf ("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n");
  116. GNUNET_free (dpriv);
  117. return GNUNET_SYSERR;
  118. }
  119. GNUNET_free (dpriv);
  120. return GNUNET_OK;
  121. }
  122. #if PERF
  123. static int
  124. testSignPerformance ()
  125. {
  126. struct GNUNET_CRYPTO_EccSignaturePurpose purp;
  127. struct GNUNET_CRYPTO_EcdsaSignature sig;
  128. struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
  129. int i;
  130. struct GNUNET_TIME_Absolute start;
  131. int ok = GNUNET_OK;
  132. purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
  133. purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
  134. FPRINTF (stderr, "%s", "W");
  135. GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey);
  136. start = GNUNET_TIME_absolute_get ();
  137. for (i = 0; i < ITER; i++)
  138. {
  139. FPRINTF (stderr, "%s", "."); fflush (stderr);
  140. if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (key, &purp, &sig))
  141. {
  142. FPRINTF (stderr, "%s",
  143. "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
  144. ok = GNUNET_SYSERR;
  145. continue;
  146. }
  147. }
  148. printf ("%d ECC sign operations %s\n", ITER,
  149. GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
  150. GNUNET_YES));
  151. return ok;
  152. }
  153. #endif
  154. static void
  155. perf_keygen ()
  156. {
  157. struct GNUNET_TIME_Absolute start;
  158. struct GNUNET_CRYPTO_EcdsaPrivateKey *pk;
  159. int i;
  160. FPRINTF (stderr, "%s", "W");
  161. start = GNUNET_TIME_absolute_get ();
  162. for (i=0;i<10;i++)
  163. {
  164. fprintf (stderr, "."); fflush (stderr);
  165. pk = GNUNET_CRYPTO_ecdsa_key_create ();
  166. GNUNET_free (pk);
  167. }
  168. for (;i<25;i++)
  169. fprintf (stderr, ".");
  170. fflush (stderr);
  171. printf ("10 ECDSA keys created in %s\n",
  172. GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
  173. }
  174. int
  175. main (int argc, char *argv[])
  176. {
  177. int failure_count = 0;
  178. if (! gcry_check_version ("1.6.0"))
  179. {
  180. FPRINTF (stderr,
  181. _
  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-ecc", "WARNING", NULL);
  189. key = GNUNET_CRYPTO_ecdsa_key_create ();
  190. if (GNUNET_OK != testDeriveSignVerify ())
  191. {
  192. failure_count++;
  193. fprintf (stderr,
  194. "\n\n%d TESTS FAILED!\n\n", failure_count);
  195. return -1;
  196. }
  197. #if PERF
  198. if (GNUNET_OK != testSignPerformance ())
  199. failure_count++;
  200. #endif
  201. if (GNUNET_OK != testSignVerify ())
  202. failure_count++;
  203. GNUNET_free (key);
  204. perf_keygen ();
  205. if (0 != failure_count)
  206. {
  207. fprintf (stderr,
  208. "\n\n%d TESTS FAILED!\n\n",
  209. failure_count);
  210. return -1;
  211. }
  212. return 0;
  213. }
  214. /* end of test_crypto_ecdsa.c */