aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_crypto_rsa.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/test_crypto_rsa.c')
-rw-r--r--src/util/test_crypto_rsa.c335
1 files changed, 335 insertions, 0 deletions
diff --git a/src/util/test_crypto_rsa.c b/src/util/test_crypto_rsa.c
new file mode 100644
index 000000000..e71decca8
--- /dev/null
+++ b/src/util/test_crypto_rsa.c
@@ -0,0 +1,335 @@
1/*
2 This file is part of GNUnet.
3 (C) 2002, 2003, 2004, 2006, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19
20*/
21/**
22 * @file util/test_crypto_rsa.c
23 * @brief testcase for RSA public key crypto
24 * @author Christian Grothoff
25 */
26#include "platform.h"
27#include "gnunet_common.h"
28#include "gnunet_crypto_lib.h"
29#include "gnunet_signatures.h"
30#include "gnunet_time_lib.h"
31
32#define TESTSTRING "Hello World\0"
33#define MAX_TESTVAL sizeof(struct GNUNET_CRYPTO_AesSessionKey)
34#define ITER 25
35#define KEYFILE "/tmp/test-gnunet-crypto-rsa.key"
36
37#define PERF GNUNET_YES
38
39static int
40testEncryptDecrypt ()
41{
42 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
43 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
44 struct GNUNET_CRYPTO_RsaEncryptedData target;
45 char result[MAX_TESTVAL];
46 int i;
47 struct GNUNET_TIME_Absolute start;
48 int ok;
49
50 fprintf (stderr, "W");
51 hostkey = GNUNET_CRYPTO_rsa_key_create ();
52 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
53
54 ok = 0;
55 start = GNUNET_TIME_absolute_get ();
56 for (i = 0; i < ITER; i++)
57 {
58 fprintf (stderr, ".");
59 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_encrypt (TESTSTRING,
60 strlen (TESTSTRING) + 1,
61 &pkey, &target))
62 {
63 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
64 ok++;
65 continue;
66 }
67 if (-1 == GNUNET_CRYPTO_rsa_decrypt (hostkey,
68 &target, result,
69 strlen (TESTSTRING) + 1))
70 {
71 fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
72 ok++;
73 continue;
74
75 }
76 if (strncmp (TESTSTRING, result, strlen (TESTSTRING)) != 0)
77 {
78 printf ("%s != %.*s - testEncryptDecrypt failed!\n",
79 TESTSTRING, MAX_TESTVAL, result);
80 ok++;
81 continue;
82 }
83 }
84 printf ("%d RSA encrypt/decrypt operations %llums (%d failures)\n",
85 ITER,
86 (unsigned long long) GNUNET_TIME_absolute_get_duration (start).
87 value, ok);
88 GNUNET_CRYPTO_rsa_key_free (hostkey);
89 if (ok == 0)
90 return GNUNET_OK;
91 else
92 return GNUNET_SYSERR;
93}
94
95#if PERF
96static int
97testEncryptPerformance ()
98{
99 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
100 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
101 struct GNUNET_CRYPTO_RsaEncryptedData target;
102 int i;
103 struct GNUNET_TIME_Absolute start;
104 int ok;
105
106 fprintf (stderr, "W");
107 hostkey = GNUNET_CRYPTO_rsa_key_create ();
108 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
109
110 ok = 0;
111 start = GNUNET_TIME_absolute_get ();
112 for (i = 0; i < ITER; i++)
113 {
114 fprintf (stderr, ".");
115 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_encrypt (TESTSTRING,
116 strlen (TESTSTRING) + 1,
117 &pkey, &target))
118 {
119 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
120 ok++;
121 continue;
122 }
123 }
124 printf ("%d RSA encrypt operations %llu ms (%d failures)\n",
125 ITER,
126 (unsigned long long) GNUNET_TIME_absolute_get_duration (start).
127 value, ok);
128 GNUNET_CRYPTO_rsa_key_free (hostkey);
129 if (ok != 0)
130 return GNUNET_SYSERR;
131 return GNUNET_OK;
132}
133#endif
134
135static int
136testEncryptDecryptSK ()
137{
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;
143 int i;
144 struct GNUNET_TIME_Absolute start;
145 int ok;
146
147 fprintf (stderr, "W");
148 hostkey = GNUNET_CRYPTO_rsa_key_create ();
149 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
150
151 ok = 0;
152 start = GNUNET_TIME_absolute_get ();
153 for (i = 0; i < ITER; i++)
154 {
155 fprintf (stderr, ".");
156 GNUNET_CRYPTO_aes_create_session_key (&insk);
157 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_encrypt (&insk,
158 sizeof (struct
159 GNUNET_CRYPTO_AesSessionKey),
160 &pkey, &target))
161 {
162 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
163 ok++;
164 continue;
165 }
166 if (-1 == GNUNET_CRYPTO_rsa_decrypt (hostkey,
167 &target, &outsk,
168 sizeof (struct
169 GNUNET_CRYPTO_AesSessionKey)))
170 {
171 fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
172 ok++;
173 continue;
174 }
175 if (0 !=
176 memcmp (&insk, &outsk, sizeof (struct GNUNET_CRYPTO_AesSessionKey)))
177 {
178 printf ("testEncryptDecryptSK failed!\n");
179 ok++;
180 continue;
181 }
182 }
183 printf ("%d RSA encrypt/decrypt SK operations %llus (%d failures)\n",
184 ITER,
185 (unsigned long long) GNUNET_TIME_absolute_get_duration (start).
186 value, ok);
187 GNUNET_CRYPTO_rsa_key_free (hostkey);
188 if (ok != 0)
189 return GNUNET_SYSERR;
190 return GNUNET_OK;
191}
192
193
194static int
195testSignVerify ()
196{
197 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
198 struct GNUNET_CRYPTO_RsaSignature sig;
199 struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
200 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
201 int i;
202 struct GNUNET_TIME_Absolute start;
203 int ok = GNUNET_OK;
204
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);
211
212 for (i = 0; i < ITER; i++)
213 {
214 fprintf (stderr, ".");
215 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
216 {
217 fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
218 ok = GNUNET_SYSERR;
219 continue;
220 }
221 if (GNUNET_SYSERR ==
222 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
223 &purp, &sig, &pkey))
224 {
225 printf ("GNUNET_CRYPTO_rsa_verify failed!\n");
226 ok = GNUNET_SYSERR;
227 continue;
228 }
229 if (GNUNET_SYSERR !=
230 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_HELLO,
231 &purp, &sig, &pkey))
232 {
233 printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n");
234 ok = GNUNET_SYSERR;
235 continue;
236 }
237 }
238 printf ("%d RSA sign/verify operations %llums\n",
239 ITER,
240 (unsigned long long) GNUNET_TIME_absolute_get_duration (start).
241 value);
242 GNUNET_CRYPTO_rsa_key_free (hostkey);
243 return ok;
244}
245
246
247#if PERF
248static int
249testSignPerformance ()
250{
251 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
252 struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
253 struct GNUNET_CRYPTO_RsaSignature sig;
254 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
255 int i;
256 struct GNUNET_TIME_Absolute start;
257 int ok = GNUNET_OK;
258
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++)
266 {
267 fprintf (stderr, ".");
268 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
269 {
270 fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
271 ok = GNUNET_SYSERR;
272 continue;
273 }
274 }
275 printf ("%d RSA sign operations %llu ms\n", ITER,
276 GNUNET_TIME_absolute_get_duration (start).value);
277 GNUNET_CRYPTO_rsa_key_free (hostkey);
278 return ok;
279}
280#endif
281
282
283static int
284testCreateFromFile ()
285{
286 struct GNUNET_CRYPTO_RsaPrivateKey *key;
287 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p1;
288 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p2;
289
290 key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE);
291 GNUNET_CRYPTO_rsa_key_get_public (key, &p1);
292 GNUNET_CRYPTO_rsa_key_free (key);
293 key = GNUNET_CRYPTO_rsa_key_create_from_file (KEYFILE);
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_CRYPTO_rsa_key_get_public (key, &p2);
300 GNUNET_assert (0 != memcmp (&p1, &p2, sizeof (p1)));
301 GNUNET_CRYPTO_rsa_key_free (key);
302 GNUNET_assert (0 == UNLINK (KEYFILE));
303 return GNUNET_OK;
304}
305
306
307int
308main (int argc, char *argv[])
309{
310 int failureCount = 0;
311
312 GNUNET_log_setup ("test-crypto-rsa", "WARNING", NULL);
313 GNUNET_CRYPTO_random_disable_entropy_gathering ();
314 if (GNUNET_OK != testCreateFromFile ())
315 failureCount++;
316#if PERF
317 if (GNUNET_OK != testEncryptPerformance ())
318 failureCount++;
319 if (GNUNET_OK != testSignPerformance ())
320 failureCount++;
321#endif
322 if (GNUNET_OK != testEncryptDecryptSK ())
323 failureCount++;
324 if (GNUNET_OK != testEncryptDecrypt ())
325 failureCount++;
326 if (GNUNET_OK != testSignVerify ())
327 failureCount++;
328
329 if (failureCount != 0)
330 {
331 printf ("\n\n%d TESTS FAILED!\n\n", failureCount);
332 return -1;
333 }
334 return 0;
335} /* end of main */