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_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_EccPublicKey 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_EccPublicKey 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_EccPublicKey p1;
128 struct GNUNET_CRYPTO_EccPublicKey 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)));
151 struct GNUNET_CRYPTO_EccPrivateKey *priv1;
152 struct GNUNET_CRYPTO_EccPrivateKey *priv2;
153 struct GNUNET_CRYPTO_EccPublicKey pub1;
154 struct GNUNET_CRYPTO_EccPublicKey pub2;
155 struct GNUNET_HashCode ecdh1;
156 struct GNUNET_HashCode ecdh2;
158 priv1 = GNUNET_CRYPTO_ecc_key_create ();
159 priv2 = GNUNET_CRYPTO_ecc_key_create ();
160 GNUNET_CRYPTO_ecc_key_get_public (priv1, &pub1);
161 GNUNET_CRYPTO_ecc_key_get_public (priv2, &pub2);
162 GNUNET_CRYPTO_ecc_ecdh (priv1, &pub2, &ecdh1);
163 GNUNET_CRYPTO_ecc_ecdh (priv2, &pub1, &ecdh2);
164 GNUNET_assert (0 == memcmp (&ecdh1, &ecdh2,
165 sizeof (struct GNUNET_HashCode)));
166 GNUNET_CRYPTO_ecc_key_free (priv1);
167 GNUNET_CRYPTO_ecc_key_free (priv2);
174 struct GNUNET_TIME_Absolute start;
175 struct GNUNET_CRYPTO_EccPrivateKey *pk;
178 start = GNUNET_TIME_absolute_get ();
181 fprintf (stderr, ".");
182 pk = GNUNET_CRYPTO_ecc_key_create ();
183 GNUNET_CRYPTO_ecc_key_free (pk);
185 fprintf (stderr, "\n");
186 printf ("Creating 10 ECC keys took %s\n",
187 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
192 main (int argc, char *argv[])
194 int failureCount = 0;
196 if (!gcry_check_version ("1.5.0"))
200 ("libgcrypt has not the expected version (version %s is required).\n"),
204 GNUNET_log_setup ("test-crypto-ecc", "WARNING", NULL);
205 if (GNUNET_OK != testCreateFromFile ())
208 if (GNUNET_OK != testSignPerformance ())
211 if (GNUNET_OK != testSignVerify ())
213 GNUNET_CRYPTO_ecc_key_free (key);
214 GNUNET_assert (0 == UNLINK (KEYFILE));
218 if (failureCount != 0)
220 printf ("\n\n%d TESTS FAILED!\n\n", failureCount);
226 /* end of test_crypto_ecc.c */