change default configurations on systems with UNIX domain sockets to NOT specify...
[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, "%s",  "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, "%s",  ".");
59     if (GNUNET_SYSERR ==
60         GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &pkey,
61                                    &target))
62     {
63       FPRINTF (stderr, "%s",  "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, "%s",  "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, "%s",  "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, "%s",  ".");
114     if (GNUNET_SYSERR ==
115         GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &pkey,
116                                    &target))
117     {
118       FPRINTF (stderr, "%s",  "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, "%s",  "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, "%s",  ".");
154     GNUNET_CRYPTO_aes_create_session_key (&insk);
155     if (GNUNET_SYSERR ==
156         GNUNET_CRYPTO_rsa_encrypt (&insk,
157                                    sizeof (struct GNUNET_CRYPTO_AesSessionKey),
158                                    &pkey, &target))
159     {
160       FPRINTF (stderr, "%s",  "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
161       ok++;
162       continue;
163     }
164     if (-1 ==
165         GNUNET_CRYPTO_rsa_decrypt (hostkey, &target, &outsk,
166                                    sizeof (struct GNUNET_CRYPTO_AesSessionKey)))
167     {
168       FPRINTF (stderr, "%s",  "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
169       ok++;
170       continue;
171     }
172     if (0 !=
173         memcmp (&insk, &outsk, sizeof (struct GNUNET_CRYPTO_AesSessionKey)))
174     {
175       printf ("testEncryptDecryptSK failed!\n");
176       ok++;
177       continue;
178     }
179   }
180   printf ("%d RSA encrypt/decrypt SK operations %llums (%d failures)\n", ITER,
181           (unsigned long long)
182           GNUNET_TIME_absolute_get_duration (start).rel_value, ok);
183   GNUNET_CRYPTO_rsa_key_free (hostkey);
184   if (ok != 0)
185     return GNUNET_SYSERR;
186   return GNUNET_OK;
187 }
188
189
190 static int
191 testSignVerify ()
192 {
193   struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
194   struct GNUNET_CRYPTO_RsaSignature sig;
195   struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
196   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
197   int i;
198   struct GNUNET_TIME_Absolute start;
199   int ok = GNUNET_OK;
200
201   FPRINTF (stderr, "%s",  "W");
202   hostkey = GNUNET_CRYPTO_rsa_key_create ();
203   GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
204   start = GNUNET_TIME_absolute_get ();
205   purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose));
206   purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
207
208   for (i = 0; i < ITER; i++)
209   {
210     FPRINTF (stderr, "%s",  ".");
211     if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
212     {
213       FPRINTF (stderr, "%s",  "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
214       ok = GNUNET_SYSERR;
215       continue;
216     }
217     if (GNUNET_SYSERR ==
218         GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
219                                   &pkey))
220     {
221       printf ("GNUNET_CRYPTO_rsa_verify failed!\n");
222       ok = GNUNET_SYSERR;
223       continue;
224     }
225     if (GNUNET_SYSERR !=
226         GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
227                                   &purp, &sig, &pkey))
228     {
229       printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n");
230       ok = GNUNET_SYSERR;
231       continue;
232     }
233   }
234   printf ("%d RSA sign/verify operations %llums\n", ITER,
235           (unsigned long long)
236           GNUNET_TIME_absolute_get_duration (start).rel_value);
237   GNUNET_CRYPTO_rsa_key_free (hostkey);
238   return ok;
239 }
240
241
242 #if PERF
243 static int
244 testSignPerformance ()
245 {
246   struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
247   struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
248   struct GNUNET_CRYPTO_RsaSignature sig;
249   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
250   int i;
251   struct GNUNET_TIME_Absolute start;
252   int ok = GNUNET_OK;
253
254   purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose));
255   purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
256   FPRINTF (stderr, "%s",  "W");
257   hostkey = GNUNET_CRYPTO_rsa_key_create ();
258   GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
259   start = GNUNET_TIME_absolute_get ();
260   for (i = 0; i < ITER; i++)
261   {
262     FPRINTF (stderr, "%s",  ".");
263     if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
264     {
265       FPRINTF (stderr, "%s",  "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
266       ok = GNUNET_SYSERR;
267       continue;
268     }
269   }
270   printf ("%d RSA sign operations %llu ms\n", ITER,
271           (unsigned long long)
272           GNUNET_TIME_absolute_get_duration (start).rel_value);
273   GNUNET_CRYPTO_rsa_key_free (hostkey);
274   return ok;
275 }
276 #endif
277
278
279 static int
280 testCreateFromFile ()
281 {
282   struct GNUNET_CRYPTO_RsaPrivateKey *key;
283   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p1;
284   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p2;
285
286   key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE);
287   GNUNET_assert (NULL != key);
288   GNUNET_CRYPTO_rsa_key_get_public (key, &p1);
289   GNUNET_CRYPTO_rsa_key_free (key);
290   key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE);
291   GNUNET_assert (NULL != key);
292   GNUNET_CRYPTO_rsa_key_get_public (key, &p2);
293   GNUNET_assert (0 == memcmp (&p1, &p2, sizeof (p1)));
294   GNUNET_CRYPTO_rsa_key_free (key);
295   GNUNET_assert (0 == UNLINK (KEYFILE));
296   key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE);
297   GNUNET_assert (NULL != key);
298   GNUNET_CRYPTO_rsa_key_get_public (key, &p2);
299   GNUNET_assert (0 != memcmp (&p1, &p2, sizeof (p1)));
300   GNUNET_CRYPTO_rsa_key_free (key);
301   GNUNET_assert (0 == UNLINK (KEYFILE));
302   return GNUNET_OK;
303 }
304
305
306 int
307 main (int argc, char *argv[])
308 {
309   int failureCount = 0;
310
311   GNUNET_log_setup ("test-crypto-rsa", "WARNING", NULL);
312   GNUNET_CRYPTO_random_disable_entropy_gathering ();
313   if (GNUNET_OK != testCreateFromFile ())
314     failureCount++;
315 #if PERF
316   if (GNUNET_OK != testEncryptPerformance ())
317     failureCount++;
318   if (GNUNET_OK != testSignPerformance ())
319     failureCount++;
320 #endif
321   if (GNUNET_OK != testEncryptDecryptSK ())
322     failureCount++;
323   if (GNUNET_OK != testEncryptDecrypt ())
324     failureCount++;
325   if (GNUNET_OK != testSignVerify ())
326     failureCount++;
327
328   if (failureCount != 0)
329   {
330     printf ("\n\n%d TESTS FAILED!\n\n", failureCount);
331     return -1;
332   }
333   return 0;
334 }                               /* end of main */