-dce
[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
31 #define TESTSTRING "Hello World\0"
32 #define MAX_TESTVAL sizeof(struct GNUNET_CRYPTO_AesSessionKey)
33 #define ITER 25
34 #define KEYFILE "/tmp/test-gnunet-crypto-ecc.key"
35
36 #define PERF GNUNET_YES
37
38 static struct GNUNET_CRYPTO_EccPrivateKey *key;
39
40
41 static int
42 testSignVerify ()
43 {
44   struct GNUNET_CRYPTO_EccSignature sig;
45   struct GNUNET_CRYPTO_EccSignaturePurpose purp;
46   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pkey;
47   int i;
48   struct GNUNET_TIME_Absolute start;
49   int ok = GNUNET_OK;
50
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);
56
57   for (i = 0; i < ITER; i++)
58   {
59     FPRINTF (stderr, "%s",  ".");
60     if (GNUNET_SYSERR == GNUNET_CRYPTO_ecc_sign (key, &purp, &sig))
61     {
62       FPRINTF (stderr, "%s",  "GNUNET_CRYPTO_ecc_sign returned SYSERR\n");
63       ok = GNUNET_SYSERR;
64       continue;
65     }
66     if (GNUNET_SYSERR ==
67         GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
68                                   &pkey))
69     {
70       printf ("GNUNET_CRYPTO_ecc_verify failed!\n");
71       ok = GNUNET_SYSERR;
72       continue;
73     }
74     if (GNUNET_SYSERR !=
75         GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
76                                   &purp, &sig, &pkey))
77     {
78       printf ("GNUNET_CRYPTO_ecc_verify failed to fail!\n");
79       ok = GNUNET_SYSERR;
80       continue;
81     }
82   }
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));
85   return ok;
86 }
87
88
89 #if PERF
90 static int
91 testSignPerformance ()
92 {
93   struct GNUNET_CRYPTO_EccSignaturePurpose purp;
94   struct GNUNET_CRYPTO_EccSignature sig;
95   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pkey;
96   int i;
97   struct GNUNET_TIME_Absolute start;
98   int ok = GNUNET_OK;
99
100   purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
101   purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
102   FPRINTF (stderr, "%s",  "W");
103   GNUNET_CRYPTO_ecc_key_get_public (key, &pkey);
104   start = GNUNET_TIME_absolute_get ();
105   for (i = 0; i < ITER; i++)
106   {
107     FPRINTF (stderr, "%s",  ".");
108     if (GNUNET_SYSERR == GNUNET_CRYPTO_ecc_sign (key, &purp, &sig))
109     {
110       FPRINTF (stderr, "%s",  "GNUNET_CRYPTO_ecc_sign returned SYSERR\n");
111       ok = GNUNET_SYSERR;
112       continue;
113     }
114   }
115   printf ("%d ECC sign operations %llu ms\n", ITER,
116           (unsigned long long)
117           GNUNET_TIME_absolute_get_duration (start).rel_value);
118   return ok;
119 }
120 #endif
121
122
123 static int
124 testCreateFromFile ()
125 {
126   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded p1;
127   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded p2;
128
129   key = GNUNET_CRYPTO_ecc_key_create_from_file (KEYFILE);
130   GNUNET_assert (NULL != key);
131   GNUNET_CRYPTO_ecc_key_get_public (key, &p1);
132   GNUNET_CRYPTO_ecc_key_free (key);
133   key = GNUNET_CRYPTO_ecc_key_create_from_file (KEYFILE);
134   GNUNET_assert (NULL != key);
135   GNUNET_CRYPTO_ecc_key_get_public (key, &p2);
136   GNUNET_assert (0 == memcmp (&p1, &p2, sizeof (p1)));
137   GNUNET_CRYPTO_ecc_key_free (key);
138   GNUNET_assert (0 == UNLINK (KEYFILE));
139   key = GNUNET_CRYPTO_ecc_key_create_from_file (KEYFILE);
140   GNUNET_assert (NULL != key);
141   GNUNET_CRYPTO_ecc_key_get_public (key, &p2);
142   GNUNET_assert (0 != memcmp (&p1, &p2, sizeof (p1)));
143   return GNUNET_OK;
144 }
145
146
147 static void
148 key_cont (void *cls,
149           struct GNUNET_CRYPTO_EccPrivateKey *pk,
150           const char *emsg)
151 {
152   const char *txt = cls;
153   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub1;
154   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub2;
155
156   GNUNET_assert (0 == strcmp ("ok", txt));
157   GNUNET_CRYPTO_ecc_key_get_public (pk, &pub1);
158   GNUNET_CRYPTO_ecc_key_get_public (key, &pub2);
159   GNUNET_assert (0 == memcmp (&pub1, &pub2, 
160                               sizeof (pub1)));
161   GNUNET_CRYPTO_ecc_key_free (pk);
162 }
163
164
165 static void
166 test_async_creation (void *cls,
167                      const struct GNUNET_SCHEDULER_TaskContext *tc)
168 {
169   struct GNUNET_CRYPTO_EccKeyGenerationContext *gc;
170
171   gc = GNUNET_CRYPTO_ecc_key_create_start (KEYFILE,
172                                            &key_cont, 
173                                            (void*) "bug");
174   GNUNET_CRYPTO_ecc_key_create_stop (gc);
175   gc = GNUNET_CRYPTO_ecc_key_create_start (KEYFILE,
176                                            &key_cont, 
177                                            (void*) "ok");
178 }
179
180
181 int
182 main (int argc, char *argv[])
183 {
184   int failureCount = 0;
185
186   GNUNET_log_setup ("test-crypto-ecc", "WARNING", NULL);
187   GNUNET_CRYPTO_random_disable_entropy_gathering ();
188   if (GNUNET_OK != testCreateFromFile ())
189     failureCount++;
190   GNUNET_SCHEDULER_run (&test_async_creation, NULL);
191 #if PERF
192   if (GNUNET_OK != testSignPerformance ())
193     failureCount++;
194 #endif
195   if (GNUNET_OK != testSignVerify ())
196     failureCount++;
197   GNUNET_CRYPTO_ecc_key_free (key);
198   GNUNET_assert (0 == UNLINK (KEYFILE));
199
200   if (failureCount != 0)
201   {
202     printf ("\n\n%d TESTS FAILED!\n\n", failureCount);
203     return -1;
204   }
205   return 0;
206 }                               /* end of main */