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/>.
20 * @file util/test_crypto_ecdsa.c
21 * @brief testcase for ECC ECDSA public key crypto
22 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
26 #include "gnunet_signatures.h"
31 #define PERF GNUNET_YES
34 static struct GNUNET_CRYPTO_EcdsaPrivateKey *key;
40 struct GNUNET_CRYPTO_EcdsaSignature sig;
41 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
42 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
44 struct GNUNET_TIME_Absolute start;
47 FPRINTF (stderr, "%s", "W");
48 GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey);
49 start = GNUNET_TIME_absolute_get ();
50 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
51 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
53 for (i = 0; i < ITER; i++)
55 FPRINTF (stderr, "%s", "."); fflush (stderr);
56 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (key, &purp, &sig))
60 "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
65 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
68 printf ("GNUNET_CRYPTO_ecdsa_verify failed!\n");
73 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
76 printf ("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n");
81 printf ("%d ECDSA sign/verify operations %s\n", ITER,
82 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
88 testDeriveSignVerify ()
90 struct GNUNET_CRYPTO_EcdsaSignature sig;
91 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
92 struct GNUNET_CRYPTO_EcdsaPrivateKey *dpriv;
93 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
94 struct GNUNET_CRYPTO_EcdsaPublicKey dpub;
96 dpriv = GNUNET_CRYPTO_ecdsa_private_key_derive (key, "test-derive", "test-CTX");
97 GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey);
98 GNUNET_CRYPTO_ecdsa_public_key_derive (&pkey, "test-derive", "test-CTX", &dpub);
99 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
100 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
102 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (dpriv, &purp, &sig))
104 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
106 return GNUNET_SYSERR;
109 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
113 printf ("GNUNET_CRYPTO_ecdsa_verify failed!\n");
115 return GNUNET_SYSERR;
118 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
122 printf ("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n");
124 return GNUNET_SYSERR;
127 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
130 printf ("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n");
132 return GNUNET_SYSERR;
141 testSignPerformance ()
143 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
144 struct GNUNET_CRYPTO_EcdsaSignature sig;
145 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
147 struct GNUNET_TIME_Absolute start;
150 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
151 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
152 FPRINTF (stderr, "%s", "W");
153 GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey);
154 start = GNUNET_TIME_absolute_get ();
155 for (i = 0; i < ITER; i++)
157 FPRINTF (stderr, "%s", "."); fflush (stderr);
158 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (key, &purp, &sig))
160 FPRINTF (stderr, "%s",
161 "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
166 printf ("%d ECC sign operations %s\n", ITER,
167 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
177 struct GNUNET_TIME_Absolute start;
178 struct GNUNET_CRYPTO_EcdsaPrivateKey *pk;
181 FPRINTF (stderr, "%s", "W");
182 start = GNUNET_TIME_absolute_get ();
185 fprintf (stderr, "."); fflush (stderr);
186 pk = GNUNET_CRYPTO_ecdsa_key_create ();
190 fprintf (stderr, ".");
192 printf ("10 ECDSA keys created in %s\n",
193 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
198 main (int argc, char *argv[])
200 int failure_count = 0;
202 if (! gcry_check_version ("1.6.0"))
206 ("libgcrypt has not the expected version (version %s is required).\n"),
210 if (getenv ("GNUNET_GCRYPT_DEBUG"))
211 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0);
212 GNUNET_log_setup ("test-crypto-ecc", "WARNING", NULL);
213 key = GNUNET_CRYPTO_ecdsa_key_create ();
214 if (GNUNET_OK != testDeriveSignVerify ())
218 "\n\n%d TESTS FAILED!\n\n", failure_count);
222 if (GNUNET_OK != testSignPerformance ())
225 if (GNUNET_OK != testSignVerify ())
230 if (0 != failure_count)
233 "\n\n%d TESTS FAILED!\n\n",
240 /* end of test_crypto_ecdsa.c */