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, "%s", "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, "%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 (hostkey, &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);
87 GNUNET_CRYPTO_rsa_key_free (hostkey);
96 testEncryptPerformance ()
98 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
99 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
100 struct GNUNET_CRYPTO_RsaEncryptedData target;
102 struct GNUNET_TIME_Absolute start;
105 FPRINTF (stderr, "%s", "W");
106 hostkey = GNUNET_CRYPTO_rsa_key_create ();
107 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
110 start = GNUNET_TIME_absolute_get ();
111 for (i = 0; i < ITER; i++)
113 FPRINTF (stderr, "%s", ".");
115 GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &pkey,
118 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
123 printf ("%d RSA encrypt operations %llu ms (%d failures)\n", ITER,
125 GNUNET_TIME_absolute_get_duration (start).rel_value, ok);
126 GNUNET_CRYPTO_rsa_key_free (hostkey);
128 return GNUNET_SYSERR;
134 testEncryptDecryptSK ()
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;
142 struct GNUNET_TIME_Absolute start;
145 FPRINTF (stderr, "%s", "W");
146 hostkey = GNUNET_CRYPTO_rsa_key_create ();
147 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
150 start = GNUNET_TIME_absolute_get ();
151 for (i = 0; i < ITER; i++)
153 FPRINTF (stderr, "%s", ".");
154 GNUNET_CRYPTO_aes_create_session_key (&insk);
156 GNUNET_CRYPTO_rsa_encrypt (&insk,
157 sizeof (struct GNUNET_CRYPTO_AesSessionKey),
160 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
165 GNUNET_CRYPTO_rsa_decrypt (hostkey, &target, &outsk,
166 sizeof (struct GNUNET_CRYPTO_AesSessionKey)))
168 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
173 memcmp (&insk, &outsk, sizeof (struct GNUNET_CRYPTO_AesSessionKey)))
175 printf ("testEncryptDecryptSK failed!\n");
180 printf ("%d RSA encrypt/decrypt SK operations %llums (%d failures)\n", ITER,
182 GNUNET_TIME_absolute_get_duration (start).rel_value, ok);
183 GNUNET_CRYPTO_rsa_key_free (hostkey);
185 return GNUNET_SYSERR;
193 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
194 struct GNUNET_CRYPTO_RsaSignature sig;
195 struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
196 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
198 struct GNUNET_TIME_Absolute start;
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);
208 for (i = 0; i < ITER; i++)
210 FPRINTF (stderr, "%s", ".");
211 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
213 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
218 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
221 printf ("GNUNET_CRYPTO_rsa_verify failed!\n");
226 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
229 printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n");
234 printf ("%d RSA sign/verify operations %llums\n", ITER,
236 GNUNET_TIME_absolute_get_duration (start).rel_value);
237 GNUNET_CRYPTO_rsa_key_free (hostkey);
244 testSignPerformance ()
246 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
247 struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
248 struct GNUNET_CRYPTO_RsaSignature sig;
249 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
251 struct GNUNET_TIME_Absolute start;
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++)
262 FPRINTF (stderr, "%s", ".");
263 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
265 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
270 printf ("%d RSA sign operations %llu ms\n", ITER,
272 GNUNET_TIME_absolute_get_duration (start).rel_value);
273 GNUNET_CRYPTO_rsa_key_free (hostkey);
280 testCreateFromFile ()
282 struct GNUNET_CRYPTO_RsaPrivateKey *key;
283 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p1;
284 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p2;
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));
307 main (int argc, char *argv[])
309 int failureCount = 0;
311 GNUNET_log_setup ("test-crypto-rsa", "WARNING", NULL);
312 GNUNET_CRYPTO_random_disable_entropy_gathering ();
313 if (GNUNET_OK != testCreateFromFile ())
316 if (GNUNET_OK != testEncryptPerformance ())
318 if (GNUNET_OK != testSignPerformance ())
321 if (GNUNET_OK != testEncryptDecryptSK ())
323 if (GNUNET_OK != testEncryptDecrypt ())
325 if (GNUNET_OK != testSignVerify ())
328 if (failureCount != 0)
330 printf ("\n\n%d TESTS FAILED!\n\n", failureCount);