2 This file is part of GNUnet.
3 Copyright (C) 2002-2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file util/test_crypto_ecdsa.c
23 * @brief testcase for ECC ECDSA public key crypto
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_signatures.h"
33 #define PERF GNUNET_YES
36 static struct GNUNET_CRYPTO_EcdsaPrivateKey *key;
42 struct GNUNET_CRYPTO_EcdsaSignature sig;
43 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
44 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
46 struct GNUNET_TIME_Absolute start;
49 FPRINTF (stderr, "%s", "W");
50 GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey);
51 start = GNUNET_TIME_absolute_get ();
52 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
53 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
55 for (i = 0; i < ITER; i++)
57 FPRINTF (stderr, "%s", "."); fflush (stderr);
58 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (key, &purp, &sig))
62 "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
67 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
70 printf ("GNUNET_CRYPTO_ecdsa_verify failed!\n");
75 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
78 printf ("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n");
83 printf ("%d ECDSA sign/verify operations %s\n", ITER,
84 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
90 testDeriveSignVerify ()
92 struct GNUNET_CRYPTO_EcdsaSignature sig;
93 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
94 struct GNUNET_CRYPTO_EcdsaPrivateKey *dpriv;
95 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
96 struct GNUNET_CRYPTO_EcdsaPublicKey dpub;
98 dpriv = GNUNET_CRYPTO_ecdsa_private_key_derive (key, "test-derive", "test-CTX");
99 GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey);
100 GNUNET_CRYPTO_ecdsa_public_key_derive (&pkey, "test-derive", "test-CTX", &dpub);
101 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
102 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
104 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (dpriv, &purp, &sig))
106 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
108 return GNUNET_SYSERR;
111 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
115 printf ("GNUNET_CRYPTO_ecdsa_verify failed!\n");
117 return GNUNET_SYSERR;
120 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
124 printf ("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n");
126 return GNUNET_SYSERR;
129 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
132 printf ("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n");
134 return GNUNET_SYSERR;
143 testSignPerformance ()
145 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
146 struct GNUNET_CRYPTO_EcdsaSignature sig;
147 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
149 struct GNUNET_TIME_Absolute start;
152 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
153 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
154 FPRINTF (stderr, "%s", "W");
155 GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey);
156 start = GNUNET_TIME_absolute_get ();
157 for (i = 0; i < ITER; i++)
159 FPRINTF (stderr, "%s", "."); fflush (stderr);
160 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (key, &purp, &sig))
162 FPRINTF (stderr, "%s",
163 "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
168 printf ("%d ECC sign operations %s\n", ITER,
169 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
179 struct GNUNET_TIME_Absolute start;
180 struct GNUNET_CRYPTO_EcdsaPrivateKey *pk;
183 FPRINTF (stderr, "%s", "W");
184 start = GNUNET_TIME_absolute_get ();
187 fprintf (stderr, "."); fflush (stderr);
188 pk = GNUNET_CRYPTO_ecdsa_key_create ();
192 fprintf (stderr, ".");
194 printf ("10 ECDSA keys created in %s\n",
195 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
200 main (int argc, char *argv[])
202 int failure_count = 0;
204 if (! gcry_check_version ("1.6.0"))
208 ("libgcrypt has not the expected version (version %s is required).\n"),
212 if (getenv ("GNUNET_GCRYPT_DEBUG"))
213 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0);
214 GNUNET_log_setup ("test-crypto-ecc", "WARNING", NULL);
215 key = GNUNET_CRYPTO_ecdsa_key_create ();
216 if (GNUNET_OK != testDeriveSignVerify ())
220 "\n\n%d TESTS FAILED!\n\n", failure_count);
224 if (GNUNET_OK != testSignPerformance ())
227 if (GNUNET_OK != testSignVerify ())
232 if (0 != failure_count)
235 "\n\n%d TESTS FAILED!\n\n",
242 /* end of test_crypto_ecdsa.c */