2 This file is part of GNUnet.
3 (C) 2002-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/test_crypto_ecc.c
23 * @brief testcase for ECC public key crypto
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_signatures.h"
33 #define KEYFILE "/tmp/test-gnunet-crypto-ecc.key"
35 #define PERF GNUNET_YES
38 static struct GNUNET_CRYPTO_EccPrivateKey *key;
44 struct GNUNET_CRYPTO_EccSignature sig;
45 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
46 struct GNUNET_CRYPTO_EccPublicKey pkey;
48 struct GNUNET_TIME_Absolute start;
51 FPRINTF (stderr, "%s", "W");
52 GNUNET_CRYPTO_ecc_key_get_public (key, &pkey);
53 start = GNUNET_TIME_absolute_get ();
54 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
55 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
57 for (i = 0; i < ITER; i++)
59 FPRINTF (stderr, "%s", ".");
60 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecc_sign (key, &purp, &sig))
62 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_ecc_sign returned SYSERR\n");
67 GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
70 printf ("GNUNET_CRYPTO_ecc_verify failed!\n");
75 GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
78 printf ("GNUNET_CRYPTO_ecc_verify failed to fail!\n");
83 printf ("%d ECC 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_EccSignature sig;
93 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
94 struct GNUNET_CRYPTO_EccPrivateKey *dpriv;
95 struct GNUNET_CRYPTO_EccPublicKey pkey;
96 struct GNUNET_CRYPTO_EccPublicKey dpub;
98 dpriv = GNUNET_CRYPTO_ecc_key_derive (key, "test-derive", "test-CTX");
99 GNUNET_CRYPTO_ecc_key_get_public (key, &pkey);
100 GNUNET_CRYPTO_ecc_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_ecc_sign (dpriv, &purp, &sig))
106 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_ecc_sign returned SYSERR\n");
108 return GNUNET_SYSERR;
111 GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
115 printf ("GNUNET_CRYPTO_ecc_verify failed!\n");
117 return GNUNET_SYSERR;
120 GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
124 printf ("GNUNET_CRYPTO_ecc_verify failed to fail!\n");
126 return GNUNET_SYSERR;
129 GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
132 printf ("GNUNET_CRYPTO_ecc_verify failed to fail!\n");
134 return GNUNET_SYSERR;
145 testSignPerformance ()
147 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
148 struct GNUNET_CRYPTO_EccSignature sig;
149 struct GNUNET_CRYPTO_EccPublicKey pkey;
151 struct GNUNET_TIME_Absolute start;
154 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
155 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
156 FPRINTF (stderr, "%s", "W");
157 GNUNET_CRYPTO_ecc_key_get_public (key, &pkey);
158 start = GNUNET_TIME_absolute_get ();
159 for (i = 0; i < ITER; i++)
161 FPRINTF (stderr, "%s", ".");
162 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecc_sign (key, &purp, &sig))
164 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_ecc_sign returned SYSERR\n");
169 printf ("%d ECC sign operations %s\n", ITER,
170 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
178 testCreateFromFile ()
180 struct GNUNET_CRYPTO_EccPublicKey p1;
181 struct GNUNET_CRYPTO_EccPublicKey p2;
183 key = GNUNET_CRYPTO_ecc_key_create_from_file (KEYFILE);
184 GNUNET_assert (NULL != key);
185 GNUNET_CRYPTO_ecc_key_get_public (key, &p1);
187 key = GNUNET_CRYPTO_ecc_key_create_from_file (KEYFILE);
188 GNUNET_assert (NULL != key);
189 GNUNET_CRYPTO_ecc_key_get_public (key, &p2);
190 GNUNET_assert (0 == memcmp (&p1, &p2, sizeof (p1)));
192 GNUNET_assert (0 == UNLINK (KEYFILE));
193 key = GNUNET_CRYPTO_ecc_key_create_from_file (KEYFILE);
194 GNUNET_assert (NULL != key);
195 GNUNET_CRYPTO_ecc_key_get_public (key, &p2);
196 GNUNET_assert (0 != memcmp (&p1, &p2, sizeof (p1)));
205 struct GNUNET_CRYPTO_EccPrivateKey *priv1;
206 struct GNUNET_CRYPTO_EccPrivateKey *priv2;
207 struct GNUNET_CRYPTO_EccPublicKey pub1;
208 struct GNUNET_CRYPTO_EccPublicKey pub2;
209 struct GNUNET_HashCode ecdh1;
210 struct GNUNET_HashCode ecdh2;
212 priv1 = GNUNET_CRYPTO_ecc_key_create ();
213 priv2 = GNUNET_CRYPTO_ecc_key_create ();
214 GNUNET_CRYPTO_ecc_key_get_public (priv1, &pub1);
215 GNUNET_CRYPTO_ecc_key_get_public (priv2, &pub2);
216 GNUNET_CRYPTO_ecc_ecdh (priv1, &pub2, &ecdh1);
217 GNUNET_CRYPTO_ecc_ecdh (priv2, &pub1, &ecdh2);
218 GNUNET_assert (0 == memcmp (&ecdh1, &ecdh2,
219 sizeof (struct GNUNET_HashCode)));
228 struct GNUNET_TIME_Absolute start;
229 struct GNUNET_CRYPTO_EccPrivateKey *pk;
232 start = GNUNET_TIME_absolute_get ();
235 fprintf (stderr, ".");
236 pk = GNUNET_CRYPTO_ecc_key_create ();
239 fprintf (stderr, "\n");
240 printf ("Creating 10 ECC keys took %s\n",
241 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
246 main (int argc, char *argv[])
248 int failure_count = 0;
250 if (! gcry_check_version ("1.5.0"))
254 ("libgcrypt has not the expected version (version %s is required).\n"),
258 GNUNET_log_setup ("test-crypto-ecc", "WARNING", NULL);
259 key = GNUNET_CRYPTO_ecc_key_create ();
260 if (GNUNET_OK != testDeriveSignVerify ())
264 "\n\n%d TESTS FAILED!\n\n", failure_count);
268 if (GNUNET_OK != testSignPerformance ())
271 if (GNUNET_OK != testSignVerify ())
274 if (GNUNET_OK != testCreateFromFile ())
276 GNUNET_assert (0 == UNLINK (KEYFILE));
280 if (0 != failure_count)
283 "\n\n%d TESTS FAILED!\n\n",
290 /* end of test_crypto_ecc.c */