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