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 3, 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
39 static struct GNUNET_CRYPTO_RsaPrivateKey *key;
45 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
46 struct GNUNET_CRYPTO_RsaEncryptedData target;
47 char result[MAX_TESTVAL];
49 struct GNUNET_TIME_Absolute start;
52 FPRINTF (stderr, "%s", "W");
53 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
55 start = GNUNET_TIME_absolute_get ();
56 for (i = 0; i < ITER; i++)
58 FPRINTF (stderr, "%s", ".");
60 GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &pkey,
63 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
68 GNUNET_CRYPTO_rsa_decrypt (key, &target, result,
69 strlen (TESTSTRING) + 1))
71 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
76 if (strncmp (TESTSTRING, result, strlen (TESTSTRING)) != 0)
78 printf ("%s != %.*s - testEncryptDecrypt failed!\n", TESTSTRING,
79 (int) MAX_TESTVAL, result);
84 printf ("%d RSA encrypt/decrypt operations %llums (%d failures)\n", ITER,
86 GNUNET_TIME_absolute_get_duration (start).rel_value, ok);
95 testEncryptPerformance ()
97 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
98 struct GNUNET_CRYPTO_RsaEncryptedData target;
100 struct GNUNET_TIME_Absolute start;
103 FPRINTF (stderr, "%s", "W");
104 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
106 start = GNUNET_TIME_absolute_get ();
107 for (i = 0; i < ITER; i++)
109 FPRINTF (stderr, "%s", ".");
111 GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &pkey,
114 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
119 printf ("%d RSA encrypt operations %llu ms (%d failures)\n", ITER,
121 GNUNET_TIME_absolute_get_duration (start).rel_value, ok);
123 return GNUNET_SYSERR;
129 testEncryptDecryptSK ()
131 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
132 struct GNUNET_CRYPTO_RsaEncryptedData target;
133 struct GNUNET_CRYPTO_AesSessionKey insk;
134 struct GNUNET_CRYPTO_AesSessionKey outsk;
136 struct GNUNET_TIME_Absolute start;
139 FPRINTF (stderr, "%s", "W");
140 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
142 start = GNUNET_TIME_absolute_get ();
143 for (i = 0; i < ITER; i++)
145 FPRINTF (stderr, "%s", ".");
146 GNUNET_CRYPTO_aes_create_session_key (&insk);
148 GNUNET_CRYPTO_rsa_encrypt (&insk,
149 sizeof (struct GNUNET_CRYPTO_AesSessionKey),
152 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
157 GNUNET_CRYPTO_rsa_decrypt (key, &target, &outsk,
158 sizeof (struct GNUNET_CRYPTO_AesSessionKey)))
160 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
165 memcmp (&insk, &outsk, sizeof (struct GNUNET_CRYPTO_AesSessionKey)))
167 printf ("testEncryptDecryptSK failed!\n");
172 printf ("%d RSA encrypt/decrypt SK operations %llums (%d failures)\n", ITER,
174 GNUNET_TIME_absolute_get_duration (start).rel_value, ok);
176 return GNUNET_SYSERR;
184 struct GNUNET_CRYPTO_RsaSignature sig;
185 struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
186 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
188 struct GNUNET_TIME_Absolute start;
191 FPRINTF (stderr, "%s", "W");
192 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
193 start = GNUNET_TIME_absolute_get ();
194 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose));
195 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
197 for (i = 0; i < ITER; i++)
199 FPRINTF (stderr, "%s", ".");
200 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (key, &purp, &sig))
202 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
207 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
210 printf ("GNUNET_CRYPTO_rsa_verify failed!\n");
215 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
218 printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n");
223 printf ("%d RSA sign/verify operations %llums\n", ITER,
225 GNUNET_TIME_absolute_get_duration (start).rel_value);
232 testSignPerformance ()
234 struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
235 struct GNUNET_CRYPTO_RsaSignature sig;
236 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
238 struct GNUNET_TIME_Absolute start;
241 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose));
242 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
243 FPRINTF (stderr, "%s", "W");
244 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
245 start = GNUNET_TIME_absolute_get ();
246 for (i = 0; i < ITER; i++)
248 FPRINTF (stderr, "%s", ".");
249 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (key, &purp, &sig))
251 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
256 printf ("%d RSA sign operations %llu ms\n", ITER,
258 GNUNET_TIME_absolute_get_duration (start).rel_value);
265 testCreateFromFile ()
267 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p1;
268 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p2;
270 key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE);
271 GNUNET_assert (NULL != key);
272 GNUNET_CRYPTO_rsa_key_get_public (key, &p1);
273 GNUNET_CRYPTO_rsa_key_free (key);
274 key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE);
275 GNUNET_assert (NULL != key);
276 GNUNET_CRYPTO_rsa_key_get_public (key, &p2);
277 GNUNET_assert (0 == memcmp (&p1, &p2, sizeof (p1)));
278 GNUNET_CRYPTO_rsa_key_free (key);
279 GNUNET_assert (0 == UNLINK (KEYFILE));
280 key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE);
281 GNUNET_assert (NULL != key);
282 GNUNET_CRYPTO_rsa_key_get_public (key, &p2);
283 GNUNET_assert (0 != memcmp (&p1, &p2, sizeof (p1)));
290 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
293 const char *txt = cls;
294 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub1;
295 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub2;
297 GNUNET_assert (0 == strcmp ("ok", txt));
298 GNUNET_CRYPTO_rsa_key_get_public (pk, &pub1);
299 GNUNET_CRYPTO_rsa_key_get_public (key, &pub2);
300 GNUNET_assert (0 == memcmp (&pub1, &pub2,
302 GNUNET_CRYPTO_rsa_key_free (pk);
307 test_async_creation (void *cls,
308 const struct GNUNET_SCHEDULER_TaskContext *tc)
310 struct GNUNET_CRYPTO_RsaKeyGenerationContext *gc;
312 gc = GNUNET_CRYPTO_rsa_key_create_start (KEYFILE,
315 GNUNET_CRYPTO_rsa_key_create_stop (gc);
316 gc = GNUNET_CRYPTO_rsa_key_create_start (KEYFILE,
323 main (int argc, char *argv[])
325 int failureCount = 0;
327 GNUNET_log_setup ("test-crypto-rsa", "WARNING", NULL);
328 GNUNET_CRYPTO_random_disable_entropy_gathering ();
329 if (GNUNET_OK != testCreateFromFile ())
331 GNUNET_SCHEDULER_run (&test_async_creation, NULL);
333 if (GNUNET_OK != testEncryptPerformance ())
335 if (GNUNET_OK != testSignPerformance ())
338 if (GNUNET_OK != testEncryptDecryptSK ())
340 if (GNUNET_OK != testEncryptDecrypt ())
342 if (GNUNET_OK != testSignVerify ())
344 GNUNET_CRYPTO_rsa_key_free (key);
345 GNUNET_assert (0 == UNLINK (KEYFILE));
347 if (failureCount != 0)
349 printf ("\n\n%d TESTS FAILED!\n\n", failureCount);