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
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",
79 TESTSTRING, (int) MAX_TESTVAL, result);
84 printf ("%d RSA encrypt/decrypt operations %llums (%d failures)\n",
87 GNUNET_TIME_absolute_get_duration (start).rel_value, ok);
88 GNUNET_CRYPTO_rsa_key_free (hostkey);
97 testEncryptPerformance ()
99 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
100 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
101 struct GNUNET_CRYPTO_RsaEncryptedData target;
103 struct GNUNET_TIME_Absolute start;
106 fprintf (stderr, "W");
107 hostkey = GNUNET_CRYPTO_rsa_key_create ();
108 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
111 start = GNUNET_TIME_absolute_get ();
112 for (i = 0; i < ITER; i++)
114 fprintf (stderr, ".");
115 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_encrypt (TESTSTRING,
116 strlen (TESTSTRING) + 1,
119 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
124 printf ("%d RSA encrypt operations %llu ms (%d failures)\n",
127 GNUNET_TIME_absolute_get_duration (start).rel_value, ok);
128 GNUNET_CRYPTO_rsa_key_free (hostkey);
130 return GNUNET_SYSERR;
136 testEncryptDecryptSK ()
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;
144 struct GNUNET_TIME_Absolute start;
147 fprintf (stderr, "W");
148 hostkey = GNUNET_CRYPTO_rsa_key_create ();
149 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
152 start = GNUNET_TIME_absolute_get ();
153 for (i = 0; i < ITER; i++)
155 fprintf (stderr, ".");
156 GNUNET_CRYPTO_aes_create_session_key (&insk);
157 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_encrypt (&insk,
159 GNUNET_CRYPTO_AesSessionKey),
162 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
166 if (-1 == GNUNET_CRYPTO_rsa_decrypt (hostkey,
169 GNUNET_CRYPTO_AesSessionKey)))
171 fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
176 memcmp (&insk, &outsk, sizeof (struct GNUNET_CRYPTO_AesSessionKey)))
178 printf ("testEncryptDecryptSK failed!\n");
183 printf ("%d RSA encrypt/decrypt SK operations %llus (%d failures)\n",
186 GNUNET_TIME_absolute_get_duration (start).rel_value, ok);
187 GNUNET_CRYPTO_rsa_key_free (hostkey);
189 return GNUNET_SYSERR;
197 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
198 struct GNUNET_CRYPTO_RsaSignature sig;
199 struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
200 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
202 struct GNUNET_TIME_Absolute start;
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);
212 for (i = 0; i < ITER; i++)
214 fprintf (stderr, ".");
215 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
217 fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
222 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
225 printf ("GNUNET_CRYPTO_rsa_verify failed!\n");
230 GNUNET_CRYPTO_rsa_verify
231 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN, &purp, &sig, &pkey))
233 printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n");
238 printf ("%d RSA sign/verify operations %llums\n",
241 GNUNET_TIME_absolute_get_duration (start).rel_value);
242 GNUNET_CRYPTO_rsa_key_free (hostkey);
249 testSignPerformance ()
251 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
252 struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
253 struct GNUNET_CRYPTO_RsaSignature sig;
254 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
256 struct GNUNET_TIME_Absolute start;
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++)
267 fprintf (stderr, ".");
268 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
270 fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
275 printf ("%d RSA sign operations %llu ms\n", ITER,
277 GNUNET_TIME_absolute_get_duration (start).rel_value);
278 GNUNET_CRYPTO_rsa_key_free (hostkey);
285 testCreateFromFile ()
287 struct GNUNET_CRYPTO_RsaPrivateKey *key;
288 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p1;
289 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p2;
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));
312 main (int argc, char *argv[])
314 int failureCount = 0;
316 GNUNET_log_setup ("test-crypto-rsa", "WARNING", NULL);
317 GNUNET_CRYPTO_random_disable_entropy_gathering ();
318 if (GNUNET_OK != testCreateFromFile ())
321 if (GNUNET_OK != testEncryptPerformance ())
323 if (GNUNET_OK != testSignPerformance ())
326 if (GNUNET_OK != testEncryptDecryptSK ())
328 if (GNUNET_OK != testEncryptDecrypt ())
330 if (GNUNET_OK != testSignVerify ())
333 if (failureCount != 0)
335 printf ("\n\n%d TESTS FAILED!\n\n", failureCount);