leakfix
[oweals/gnunet.git] / src / util / test_crypto_rsa.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 2, 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_rsa.c
23  * @brief testcase for RSA public key crypto
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_common.h"
28 #include "gnunet_crypto_lib.h"
29 #include "gnunet_signatures.h"
30 #include "gnunet_time_lib.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-rsa.key"
36
37 #define PERF GNUNET_YES
38
39 static int
40 testEncryptDecrypt ()
41 {
42   struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
43   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
44   struct GNUNET_CRYPTO_RsaEncryptedData target;
45   char result[MAX_TESTVAL];
46   int i;
47   struct GNUNET_TIME_Absolute start;
48   int ok;
49
50   fprintf (stderr, "W");
51   hostkey = GNUNET_CRYPTO_rsa_key_create ();
52   GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
53
54   ok = 0;
55   start = GNUNET_TIME_absolute_get ();
56   for (i = 0; i < ITER; i++)
57     {
58       fprintf (stderr, ".");
59       if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_encrypt (TESTSTRING,
60                                                       strlen (TESTSTRING) + 1,
61                                                       &pkey, &target))
62         {
63           fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
64           ok++;
65           continue;
66         }
67       if (-1 == GNUNET_CRYPTO_rsa_decrypt (hostkey,
68                                            &target, result,
69                                            strlen (TESTSTRING) + 1))
70         {
71           fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
72           ok++;
73           continue;
74
75         }
76       if (strncmp (TESTSTRING, result, strlen (TESTSTRING)) != 0)
77         {
78           printf ("%s != %.*s - testEncryptDecrypt failed!\n",
79                   TESTSTRING, 
80                   (int) MAX_TESTVAL,
81                   result);
82           ok++;
83           continue;
84         }
85     }
86   printf ("%d RSA encrypt/decrypt operations %llums (%d failures)\n",
87           ITER,
88           (unsigned long long)
89           GNUNET_TIME_absolute_get_duration (start).value, ok);
90   GNUNET_CRYPTO_rsa_key_free (hostkey);
91   if (ok == 0)
92     return GNUNET_OK;
93   else
94     return GNUNET_SYSERR;
95 }
96
97 #if PERF
98 static int
99 testEncryptPerformance ()
100 {
101   struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
102   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
103   struct GNUNET_CRYPTO_RsaEncryptedData target;
104   int i;
105   struct GNUNET_TIME_Absolute start;
106   int ok;
107
108   fprintf (stderr, "W");
109   hostkey = GNUNET_CRYPTO_rsa_key_create ();
110   GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
111
112   ok = 0;
113   start = GNUNET_TIME_absolute_get ();
114   for (i = 0; i < ITER; i++)
115     {
116       fprintf (stderr, ".");
117       if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_encrypt (TESTSTRING,
118                                                       strlen (TESTSTRING) + 1,
119                                                       &pkey, &target))
120         {
121           fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
122           ok++;
123           continue;
124         }
125     }
126   printf ("%d RSA encrypt operations %llu ms (%d failures)\n",
127           ITER,
128           (unsigned long long)
129           GNUNET_TIME_absolute_get_duration (start).value, ok);
130   GNUNET_CRYPTO_rsa_key_free (hostkey);
131   if (ok != 0)
132     return GNUNET_SYSERR;
133   return GNUNET_OK;
134 }
135 #endif
136
137 static int
138 testEncryptDecryptSK ()
139 {
140   struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
141   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
142   struct GNUNET_CRYPTO_RsaEncryptedData target;
143   struct GNUNET_CRYPTO_AesSessionKey insk;
144   struct GNUNET_CRYPTO_AesSessionKey outsk;
145   int i;
146   struct GNUNET_TIME_Absolute start;
147   int ok;
148
149   fprintf (stderr, "W");
150   hostkey = GNUNET_CRYPTO_rsa_key_create ();
151   GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
152
153   ok = 0;
154   start = GNUNET_TIME_absolute_get ();
155   for (i = 0; i < ITER; i++)
156     {
157       fprintf (stderr, ".");
158       GNUNET_CRYPTO_aes_create_session_key (&insk);
159       if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_encrypt (&insk,
160                                                       sizeof (struct
161                                                               GNUNET_CRYPTO_AesSessionKey),
162                                                       &pkey, &target))
163         {
164           fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
165           ok++;
166           continue;
167         }
168       if (-1 == GNUNET_CRYPTO_rsa_decrypt (hostkey,
169                                            &target, &outsk,
170                                            sizeof (struct
171                                                    GNUNET_CRYPTO_AesSessionKey)))
172         {
173           fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
174           ok++;
175           continue;
176         }
177       if (0 !=
178           memcmp (&insk, &outsk, sizeof (struct GNUNET_CRYPTO_AesSessionKey)))
179         {
180           printf ("testEncryptDecryptSK failed!\n");
181           ok++;
182           continue;
183         }
184     }
185   printf ("%d RSA encrypt/decrypt SK operations %llus (%d failures)\n",
186           ITER,
187           (unsigned long long)
188           GNUNET_TIME_absolute_get_duration (start).value, ok);
189   GNUNET_CRYPTO_rsa_key_free (hostkey);
190   if (ok != 0)
191     return GNUNET_SYSERR;
192   return GNUNET_OK;
193 }
194
195
196 static int
197 testSignVerify ()
198 {
199   struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
200   struct GNUNET_CRYPTO_RsaSignature sig;
201   struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
202   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
203   int i;
204   struct GNUNET_TIME_Absolute start;
205   int ok = GNUNET_OK;
206
207   fprintf (stderr, "W");
208   hostkey = GNUNET_CRYPTO_rsa_key_create ();
209   GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
210   start = GNUNET_TIME_absolute_get ();
211   purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose));
212   purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
213
214   for (i = 0; i < ITER; i++)
215     {
216       fprintf (stderr, ".");
217       if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
218         {
219           fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
220           ok = GNUNET_SYSERR;
221           continue;
222         }
223       if (GNUNET_SYSERR ==
224           GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
225                                     &purp, &sig, &pkey))
226         {
227           printf ("GNUNET_CRYPTO_rsa_verify failed!\n");
228           ok = GNUNET_SYSERR;
229           continue;
230         }
231       if (GNUNET_SYSERR !=
232           GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_TCP_PING,
233                                     &purp, &sig, &pkey))
234         {
235           printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n");
236           ok = GNUNET_SYSERR;
237           continue;
238         }
239     }
240   printf ("%d RSA sign/verify operations %llums\n",
241           ITER,
242           (unsigned long long)
243           GNUNET_TIME_absolute_get_duration (start).value);
244   GNUNET_CRYPTO_rsa_key_free (hostkey);
245   return ok;
246 }
247
248
249 #if PERF
250 static int
251 testSignPerformance ()
252 {
253   struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
254   struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
255   struct GNUNET_CRYPTO_RsaSignature sig;
256   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
257   int i;
258   struct GNUNET_TIME_Absolute start;
259   int ok = GNUNET_OK;
260
261   purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose));
262   purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
263   fprintf (stderr, "W");
264   hostkey = GNUNET_CRYPTO_rsa_key_create ();
265   GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
266   start = GNUNET_TIME_absolute_get ();
267   for (i = 0; i < ITER; i++)
268     {
269       fprintf (stderr, ".");
270       if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
271         {
272           fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
273           ok = GNUNET_SYSERR;
274           continue;
275         }
276     }
277   printf ("%d RSA sign operations %llu ms\n", ITER,
278           (unsigned long long) GNUNET_TIME_absolute_get_duration (start).value);
279   GNUNET_CRYPTO_rsa_key_free (hostkey);
280   return ok;
281 }
282 #endif
283
284
285 static int
286 testCreateFromFile ()
287 {
288   struct GNUNET_CRYPTO_RsaPrivateKey *key;
289   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p1;
290   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p2;
291
292   key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE);
293   GNUNET_CRYPTO_rsa_key_get_public (key, &p1);
294   GNUNET_CRYPTO_rsa_key_free (key);
295   key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE);
296   GNUNET_CRYPTO_rsa_key_get_public (key, &p2);
297   GNUNET_assert (0 == memcmp (&p1, &p2, sizeof (p1)));
298   GNUNET_CRYPTO_rsa_key_free (key);
299   GNUNET_assert (0 == UNLINK (KEYFILE));
300   key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE);
301   GNUNET_CRYPTO_rsa_key_get_public (key, &p2);
302   GNUNET_assert (0 != memcmp (&p1, &p2, sizeof (p1)));
303   GNUNET_CRYPTO_rsa_key_free (key);
304   GNUNET_assert (0 == UNLINK (KEYFILE));
305   return GNUNET_OK;
306 }
307
308
309 int
310 main (int argc, char *argv[])
311 {
312   int failureCount = 0;
313
314   GNUNET_log_setup ("test-crypto-rsa", "WARNING", NULL);
315   GNUNET_CRYPTO_random_disable_entropy_gathering ();
316   if (GNUNET_OK != testCreateFromFile ())
317     failureCount++;
318 #if PERF
319   if (GNUNET_OK != testEncryptPerformance ())
320     failureCount++;
321   if (GNUNET_OK != testSignPerformance ())
322     failureCount++;
323 #endif
324   if (GNUNET_OK != testEncryptDecryptSK ())
325     failureCount++;
326   if (GNUNET_OK != testEncryptDecrypt ())
327     failureCount++;
328   if (GNUNET_OK != testSignVerify ())
329     failureCount++;
330
331   if (failureCount != 0)
332     {
333       printf ("\n\n%d TESTS FAILED!\n\n", failureCount);
334       return -1;
335     }
336   return 0;
337 }                               /* end of main */