- fix 2699
[oweals/gnunet.git] / src / util / test_crypto_ecc.c
1 /*
2      This file is part of GNUnet.
3      (C) 2002, 2003, 2004, 2006, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19
20 */
21 /**
22  * @file util/test_crypto_ecc.c
23  * @brief testcase for ECC public key crypto
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_common.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_signatures.h"
30 #include <gcrypt.h>
31
32 #define TESTSTRING "Hello World\0"
33 #define MAX_TESTVAL sizeof(struct GNUNET_CRYPTO_AesSessionKey)
34 #define ITER 25
35 #define KEYFILE "/tmp/test-gnunet-crypto-ecc.key"
36
37 #define PERF GNUNET_YES
38
39 static struct GNUNET_CRYPTO_EccPrivateKey *key;
40
41
42 static int
43 testSignVerify ()
44 {
45   struct GNUNET_CRYPTO_EccSignature sig;
46   struct GNUNET_CRYPTO_EccSignaturePurpose purp;
47   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pkey;
48   int i;
49   struct GNUNET_TIME_Absolute start;
50   int ok = GNUNET_OK;
51
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);
57
58   for (i = 0; i < ITER; i++)
59   {
60     FPRINTF (stderr, "%s",  ".");
61     if (GNUNET_SYSERR == GNUNET_CRYPTO_ecc_sign (key, &purp, &sig))
62     {
63       FPRINTF (stderr, "%s",  "GNUNET_CRYPTO_ecc_sign returned SYSERR\n");
64       ok = GNUNET_SYSERR;
65       continue;
66     }
67     if (GNUNET_SYSERR ==
68         GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
69                                   &pkey))
70     {
71       printf ("GNUNET_CRYPTO_ecc_verify failed!\n");
72       ok = GNUNET_SYSERR;
73       continue;
74     }
75     if (GNUNET_SYSERR !=
76         GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
77                                   &purp, &sig, &pkey))
78     {
79       printf ("GNUNET_CRYPTO_ecc_verify failed to fail!\n");
80       ok = GNUNET_SYSERR;
81       continue;
82     }
83   }
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));
86   return ok;
87 }
88
89
90 #if PERF
91 static int
92 testSignPerformance ()
93 {
94   struct GNUNET_CRYPTO_EccSignaturePurpose purp;
95   struct GNUNET_CRYPTO_EccSignature sig;
96   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pkey;
97   int i;
98   struct GNUNET_TIME_Absolute start;
99   int ok = GNUNET_OK;
100
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++)
107   {
108     FPRINTF (stderr, "%s",  ".");
109     if (GNUNET_SYSERR == GNUNET_CRYPTO_ecc_sign (key, &purp, &sig))
110     {
111       FPRINTF (stderr, "%s",  "GNUNET_CRYPTO_ecc_sign returned SYSERR\n");
112       ok = GNUNET_SYSERR;
113       continue;
114     }
115   }
116   printf ("%d ECC sign operations %llu ms\n", ITER,
117           (unsigned long long)
118           GNUNET_TIME_absolute_get_duration (start).rel_value);
119   return ok;
120 }
121 #endif
122
123
124 static int
125 testCreateFromFile ()
126 {
127   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded p1;
128   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded p2;
129
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)));
144   return GNUNET_OK;
145 }
146
147
148 static void
149 key_cont (void *cls,
150           struct GNUNET_CRYPTO_EccPrivateKey *pk,
151           const char *emsg)
152 {
153   const char *txt = cls;
154   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub1;
155   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub2;
156
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, 
161                               sizeof (pub1)));
162   GNUNET_CRYPTO_ecc_key_free (pk);
163 }
164
165
166 static void
167 test_async_creation (void *cls,
168                      const struct GNUNET_SCHEDULER_TaskContext *tc)
169 {
170   struct GNUNET_CRYPTO_EccKeyGenerationContext *gc;
171
172   gc = GNUNET_CRYPTO_ecc_key_create_start (KEYFILE,
173                                            &key_cont, 
174                                            (void*) "bug");
175   GNUNET_CRYPTO_ecc_key_create_stop (gc);
176   gc = GNUNET_CRYPTO_ecc_key_create_start (KEYFILE,
177                                            &key_cont, 
178                                            (void*) "ok");
179 }
180
181
182 static void
183 test_ecdh ()
184 {
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;
191
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)));
202 }
203
204 static void
205 perf_keygen ()
206 {
207   struct GNUNET_TIME_Absolute start;
208   struct GNUNET_CRYPTO_EccPrivateKey *pk;
209   int i;
210
211   start = GNUNET_TIME_absolute_get ();
212   for (i=0;i<10;i++)
213   {
214     fprintf (stderr, ".");
215     pk = GNUNET_CRYPTO_ecc_key_create ();
216     GNUNET_CRYPTO_ecc_key_free (pk);
217   }
218   fprintf (stderr, "\n");
219   printf ("Creating 10 ECC keys took %llu ms\n",
220           (unsigned long long)
221           GNUNET_TIME_absolute_get_duration (start).rel_value);
222 }
223
224
225
226 int
227 main (int argc, char *argv[])
228 {
229   int failureCount = 0;
230
231   if (!gcry_check_version ("1.5.0"))
232   {
233     FPRINTF (stderr,
234              _
235              ("libgcrypt has not the expected version (version %s is required).\n"),
236              "1.5.0");
237     return 0;
238   }
239   GNUNET_log_setup ("test-crypto-ecc", "WARNING", NULL);
240   if (GNUNET_OK != testCreateFromFile ())
241     failureCount++;
242   GNUNET_SCHEDULER_run (&test_async_creation, NULL);
243 #if PERF
244   if (GNUNET_OK != testSignPerformance ())
245     failureCount++;
246 #endif
247   if (GNUNET_OK != testSignVerify ())
248     failureCount++;
249   GNUNET_CRYPTO_ecc_key_free (key);
250   GNUNET_assert (0 == UNLINK (KEYFILE));
251   test_ecdh ();
252   perf_keygen ();
253
254   if (failureCount != 0)
255   {
256     printf ("\n\n%d TESTS FAILED!\n\n", failureCount);
257     return -1;
258   }
259   return 0;
260 }
261
262 /* end of test_crypto_ecc.c */