2 This file is part of GNUnet.
3 (C) 2002, 2003, 2004, 2006, 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file util/test_crypto_rsa.c
23 * @brief testcase for RSA public key crypto
24 * @author Christian Grothoff
27 #include "gnunet_common.h"
28 #include "gnunet_crypto_lib.h"
29 #include "gnunet_signatures.h"
30 #include "gnunet_time_lib.h"
32 #define TESTSTRING "Hello World\0"
33 #define MAX_TESTVAL sizeof(struct GNUNET_CRYPTO_AesSessionKey)
35 #define KEYFILE "/tmp/test-gnunet-crypto-rsa.key"
37 #define PERF GNUNET_YES
42 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
43 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
44 struct GNUNET_CRYPTO_RsaEncryptedData target;
45 char result[MAX_TESTVAL];
47 struct GNUNET_TIME_Absolute start;
50 fprintf (stderr, "W");
51 hostkey = GNUNET_CRYPTO_rsa_key_create ();
52 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
55 start = GNUNET_TIME_absolute_get ();
56 for (i = 0; i < ITER; i++)
58 fprintf (stderr, ".");
59 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_encrypt (TESTSTRING,
60 strlen (TESTSTRING) + 1,
63 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
67 if (-1 == GNUNET_CRYPTO_rsa_decrypt (hostkey,
69 strlen (TESTSTRING) + 1))
71 fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
76 if (strncmp (TESTSTRING, result, strlen (TESTSTRING)) != 0)
78 printf ("%s != %.*s - testEncryptDecrypt failed!\n",
86 printf ("%d RSA encrypt/decrypt operations %llums (%d failures)\n",
89 GNUNET_TIME_absolute_get_duration (start).value, ok);
90 GNUNET_CRYPTO_rsa_key_free (hostkey);
99 testEncryptPerformance ()
101 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
102 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
103 struct GNUNET_CRYPTO_RsaEncryptedData target;
105 struct GNUNET_TIME_Absolute start;
108 fprintf (stderr, "W");
109 hostkey = GNUNET_CRYPTO_rsa_key_create ();
110 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
113 start = GNUNET_TIME_absolute_get ();
114 for (i = 0; i < ITER; i++)
116 fprintf (stderr, ".");
117 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_encrypt (TESTSTRING,
118 strlen (TESTSTRING) + 1,
121 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
126 printf ("%d RSA encrypt operations %llu ms (%d failures)\n",
129 GNUNET_TIME_absolute_get_duration (start).value, ok);
130 GNUNET_CRYPTO_rsa_key_free (hostkey);
132 return GNUNET_SYSERR;
138 testEncryptDecryptSK ()
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;
146 struct GNUNET_TIME_Absolute start;
149 fprintf (stderr, "W");
150 hostkey = GNUNET_CRYPTO_rsa_key_create ();
151 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
154 start = GNUNET_TIME_absolute_get ();
155 for (i = 0; i < ITER; i++)
157 fprintf (stderr, ".");
158 GNUNET_CRYPTO_aes_create_session_key (&insk);
159 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_encrypt (&insk,
161 GNUNET_CRYPTO_AesSessionKey),
164 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
168 if (-1 == GNUNET_CRYPTO_rsa_decrypt (hostkey,
171 GNUNET_CRYPTO_AesSessionKey)))
173 fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
178 memcmp (&insk, &outsk, sizeof (struct GNUNET_CRYPTO_AesSessionKey)))
180 printf ("testEncryptDecryptSK failed!\n");
185 printf ("%d RSA encrypt/decrypt SK operations %llus (%d failures)\n",
188 GNUNET_TIME_absolute_get_duration (start).value, ok);
189 GNUNET_CRYPTO_rsa_key_free (hostkey);
191 return GNUNET_SYSERR;
199 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
200 struct GNUNET_CRYPTO_RsaSignature sig;
201 struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
202 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
204 struct GNUNET_TIME_Absolute start;
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);
214 for (i = 0; i < ITER; i++)
216 fprintf (stderr, ".");
217 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
219 fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
224 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
227 printf ("GNUNET_CRYPTO_rsa_verify failed!\n");
232 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_TCP_PING,
235 printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n");
240 printf ("%d RSA sign/verify operations %llums\n",
243 GNUNET_TIME_absolute_get_duration (start).value);
244 GNUNET_CRYPTO_rsa_key_free (hostkey);
251 testSignPerformance ()
253 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
254 struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
255 struct GNUNET_CRYPTO_RsaSignature sig;
256 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
258 struct GNUNET_TIME_Absolute start;
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++)
269 fprintf (stderr, ".");
270 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
272 fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
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);
286 testCreateFromFile ()
288 struct GNUNET_CRYPTO_RsaPrivateKey *key;
289 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p1;
290 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p2;
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));
310 main (int argc, char *argv[])
312 int failureCount = 0;
314 GNUNET_log_setup ("test-crypto-rsa", "WARNING", NULL);
315 GNUNET_CRYPTO_random_disable_entropy_gathering ();
316 if (GNUNET_OK != testCreateFromFile ())
319 if (GNUNET_OK != testEncryptPerformance ())
321 if (GNUNET_OK != testSignPerformance ())
324 if (GNUNET_OK != testEncryptDecryptSK ())
326 if (GNUNET_OK != testEncryptDecrypt ())
328 if (GNUNET_OK != testSignVerify ())
331 if (failureCount != 0)
333 printf ("\n\n%d TESTS FAILED!\n\n", failureCount);