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, ".");
60 GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1,
63 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
68 GNUNET_CRYPTO_rsa_decrypt (hostkey, &target, result,
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", 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, "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, ".");
115 GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1,
118 fprintf (stderr, "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, "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, ".");
154 GNUNET_CRYPTO_aes_create_session_key (&insk);
156 GNUNET_CRYPTO_rsa_encrypt (&insk,
158 GNUNET_CRYPTO_AesSessionKey),
161 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
166 GNUNET_CRYPTO_rsa_decrypt (hostkey, &target, &outsk,
168 GNUNET_CRYPTO_AesSessionKey)))
170 fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
175 memcmp (&insk, &outsk, sizeof (struct GNUNET_CRYPTO_AesSessionKey)))
177 printf ("testEncryptDecryptSK failed!\n");
182 printf ("%d RSA encrypt/decrypt SK operations %llums (%d failures)\n", ITER,
184 GNUNET_TIME_absolute_get_duration (start).rel_value, ok);
185 GNUNET_CRYPTO_rsa_key_free (hostkey);
187 return GNUNET_SYSERR;
195 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
196 struct GNUNET_CRYPTO_RsaSignature sig;
197 struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
198 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
200 struct GNUNET_TIME_Absolute start;
203 fprintf (stderr, "W");
204 hostkey = GNUNET_CRYPTO_rsa_key_create ();
205 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
206 start = GNUNET_TIME_absolute_get ();
207 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose));
208 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
210 for (i = 0; i < ITER; i++)
212 fprintf (stderr, ".");
213 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
215 fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
220 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp,
223 printf ("GNUNET_CRYPTO_rsa_verify failed!\n");
228 GNUNET_CRYPTO_rsa_verify
229 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN, &purp, &sig, &pkey))
231 printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n");
236 printf ("%d RSA sign/verify operations %llums\n", ITER,
238 GNUNET_TIME_absolute_get_duration (start).rel_value);
239 GNUNET_CRYPTO_rsa_key_free (hostkey);
246 testSignPerformance ()
248 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
249 struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
250 struct GNUNET_CRYPTO_RsaSignature sig;
251 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
253 struct GNUNET_TIME_Absolute start;
256 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose));
257 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
258 fprintf (stderr, "W");
259 hostkey = GNUNET_CRYPTO_rsa_key_create ();
260 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
261 start = GNUNET_TIME_absolute_get ();
262 for (i = 0; i < ITER; i++)
264 fprintf (stderr, ".");
265 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
267 fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
272 printf ("%d RSA sign operations %llu ms\n", ITER,
274 GNUNET_TIME_absolute_get_duration (start).rel_value);
275 GNUNET_CRYPTO_rsa_key_free (hostkey);
282 testCreateFromFile ()
284 struct GNUNET_CRYPTO_RsaPrivateKey *key;
285 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p1;
286 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p2;
288 key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE);
289 GNUNET_assert (NULL != key);
290 GNUNET_CRYPTO_rsa_key_get_public (key, &p1);
291 GNUNET_CRYPTO_rsa_key_free (key);
292 key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE);
293 GNUNET_assert (NULL != key);
294 GNUNET_CRYPTO_rsa_key_get_public (key, &p2);
295 GNUNET_assert (0 == memcmp (&p1, &p2, sizeof (p1)));
296 GNUNET_CRYPTO_rsa_key_free (key);
297 GNUNET_assert (0 == UNLINK (KEYFILE));
298 key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE);
299 GNUNET_assert (NULL != key);
300 GNUNET_CRYPTO_rsa_key_get_public (key, &p2);
301 GNUNET_assert (0 != memcmp (&p1, &p2, sizeof (p1)));
302 GNUNET_CRYPTO_rsa_key_free (key);
303 GNUNET_assert (0 == UNLINK (KEYFILE));
309 main (int argc, char *argv[])
311 int failureCount = 0;
313 GNUNET_log_setup ("test-crypto-rsa", "WARNING", NULL);
314 GNUNET_CRYPTO_random_disable_entropy_gathering ();
315 if (GNUNET_OK != testCreateFromFile ())
318 if (GNUNET_OK != testEncryptPerformance ())
320 if (GNUNET_OK != testSignPerformance ())
323 if (GNUNET_OK != testEncryptDecryptSK ())
325 if (GNUNET_OK != testEncryptDecrypt ())
327 if (GNUNET_OK != testSignVerify ())
330 if (failureCount != 0)
332 printf ("\n\n%d TESTS FAILED!\n\n", failureCount);