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