2 This file is part of GNUnet.
3 (C) 2002, 2003, 2004, 2006, 2009 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_common.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_signatures.h"
32 #define TESTSTRING "Hello World\0"
33 #define MAX_TESTVAL sizeof(struct GNUNET_CRYPTO_AesSessionKey)
35 #define KEYFILE "/tmp/test-gnunet-crypto-ecc.key"
37 #define PERF GNUNET_YES
39 static struct GNUNET_CRYPTO_EccPrivateKey *key;
45 struct GNUNET_CRYPTO_EccSignature sig;
46 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
47 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pkey;
49 struct GNUNET_TIME_Absolute start;
52 FPRINTF (stderr, "%s", "W");
53 GNUNET_CRYPTO_ecc_key_get_public (key, &pkey);
54 start = GNUNET_TIME_absolute_get ();
55 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
56 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
58 for (i = 0; i < ITER; i++)
60 FPRINTF (stderr, "%s", ".");
61 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecc_sign (key, &purp, &sig))
63 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_ecc_sign returned SYSERR\n");
68 GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
71 printf ("GNUNET_CRYPTO_ecc_verify failed!\n");
76 GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
79 printf ("GNUNET_CRYPTO_ecc_verify failed to fail!\n");
84 printf ("%d ECC sign/verify operations %s\n", ITER,
85 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
92 testSignPerformance ()
94 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
95 struct GNUNET_CRYPTO_EccSignature sig;
96 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pkey;
98 struct GNUNET_TIME_Absolute start;
101 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
102 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
103 FPRINTF (stderr, "%s", "W");
104 GNUNET_CRYPTO_ecc_key_get_public (key, &pkey);
105 start = GNUNET_TIME_absolute_get ();
106 for (i = 0; i < ITER; i++)
108 FPRINTF (stderr, "%s", ".");
109 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecc_sign (key, &purp, &sig))
111 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_ecc_sign returned SYSERR\n");
116 printf ("%d ECC sign operations %llu ms\n", ITER,
118 GNUNET_TIME_absolute_get_duration (start).rel_value);
125 testCreateFromFile ()
127 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded p1;
128 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded p2;
130 key = GNUNET_CRYPTO_ecc_key_create_from_file (KEYFILE);
131 GNUNET_assert (NULL != key);
132 GNUNET_CRYPTO_ecc_key_get_public (key, &p1);
133 GNUNET_CRYPTO_ecc_key_free (key);
134 key = GNUNET_CRYPTO_ecc_key_create_from_file (KEYFILE);
135 GNUNET_assert (NULL != key);
136 GNUNET_CRYPTO_ecc_key_get_public (key, &p2);
137 GNUNET_assert (0 == memcmp (&p1, &p2, sizeof (p1)));
138 GNUNET_CRYPTO_ecc_key_free (key);
139 GNUNET_assert (0 == UNLINK (KEYFILE));
140 key = GNUNET_CRYPTO_ecc_key_create_from_file (KEYFILE);
141 GNUNET_assert (NULL != key);
142 GNUNET_CRYPTO_ecc_key_get_public (key, &p2);
143 GNUNET_assert (0 != memcmp (&p1, &p2, sizeof (p1)));
150 struct GNUNET_CRYPTO_EccPrivateKey *pk,
153 const char *txt = cls;
154 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub1;
155 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub2;
157 GNUNET_assert (0 == strcmp ("ok", txt));
158 GNUNET_CRYPTO_ecc_key_get_public (pk, &pub1);
159 GNUNET_CRYPTO_ecc_key_get_public (key, &pub2);
160 GNUNET_assert (0 == memcmp (&pub1, &pub2,
162 GNUNET_CRYPTO_ecc_key_free (pk);
167 test_async_creation (void *cls,
168 const struct GNUNET_SCHEDULER_TaskContext *tc)
170 struct GNUNET_CRYPTO_EccKeyGenerationContext *gc;
172 gc = GNUNET_CRYPTO_ecc_key_create_start (KEYFILE,
175 GNUNET_CRYPTO_ecc_key_create_stop (gc);
176 gc = GNUNET_CRYPTO_ecc_key_create_start (KEYFILE,
185 struct GNUNET_CRYPTO_EccPrivateKey *priv1;
186 struct GNUNET_CRYPTO_EccPrivateKey *priv2;
187 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub1;
188 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub2;
189 struct GNUNET_HashCode ecdh1;
190 struct GNUNET_HashCode ecdh2;
192 priv1 = GNUNET_CRYPTO_ecc_key_create ();
193 priv2 = GNUNET_CRYPTO_ecc_key_create ();
194 GNUNET_CRYPTO_ecc_key_get_public (priv1, &pub1);
195 GNUNET_CRYPTO_ecc_key_get_public (priv2, &pub2);
196 GNUNET_CRYPTO_ecc_ecdh (priv1, &pub2, &ecdh1);
197 GNUNET_CRYPTO_ecc_ecdh (priv2, &pub1, &ecdh2);
198 GNUNET_CRYPTO_ecc_key_free (priv1);
199 GNUNET_CRYPTO_ecc_key_free (priv2);
200 GNUNET_assert (0 == memcmp (&ecdh1, &ecdh2,
201 sizeof (struct GNUNET_HashCode)));
206 main (int argc, char *argv[])
208 int failureCount = 0;
210 if (!gcry_check_version ("1.5.0"))
214 ("libgcrypt has not the expected version (version %s is required).\n"),
218 GNUNET_log_setup ("test-crypto-ecc", "WARNING", NULL);
219 if (GNUNET_OK != testCreateFromFile ())
221 GNUNET_SCHEDULER_run (&test_async_creation, NULL);
223 if (GNUNET_OK != testSignPerformance ())
226 if (GNUNET_OK != testSignVerify ())
228 GNUNET_CRYPTO_ecc_key_free (key);
229 GNUNET_assert (0 == UNLINK (KEYFILE));
232 if (failureCount != 0)
234 printf ("\n\n%d TESTS FAILED!\n\n", failureCount);
240 /* end of test_crypto_ecc.c */