29
29
#include "platform.h"
31
31
#define TESTSTRING "Hello World\0"
32
#define MAX_TESTVAL sizeof(SESSIONKEY)
32
#define MAX_TESTVAL sizeof(GNUNET_AES_SessionKey)
35
#define PERF GNUNET_NO
38
38
testEncryptDecrypt ()
40
struct PrivateKey *hostkey;
42
RSAEncryptedData target;
40
struct GNUNET_RSA_PrivateKey *hostkey;
41
GNUNET_RSA_PublicKey pkey;
42
GNUNET_RSA_EncryptedData target;
43
43
char result[MAX_TESTVAL];
45
GNUNET_Int32Time start;
48
48
fprintf (stderr, "W");
49
hostkey = makePrivateKey ();
50
getPublicKey (hostkey, &pkey);
49
hostkey = GNUNET_RSA_create_key ();
50
GNUNET_RSA_get_public_key (hostkey, &pkey);
53
GNUNET_get_time_int32 (&start);
54
54
for (i = 0; i < ITER; i++)
56
56
fprintf (stderr, ".");
57
if (SYSERR == encryptPrivateKey (TESTSTRING,
58
strlen (TESTSTRING) + 1,
57
if (GNUNET_SYSERR == GNUNET_RSA_encrypt (TESTSTRING,
58
strlen (TESTSTRING) + 1,
61
61
fprintf (stderr, "encryptPrivateKey returned SYSERR\n");
65
if (-1 == decryptPrivateKey (hostkey,
66
&target, result, strlen (TESTSTRING) + 1))
65
if (-1 == GNUNET_RSA_decrypt (hostkey,
66
&target, result, strlen (TESTSTRING) + 1))
68
68
fprintf (stderr, "decryptPrivateKey returned SYSERR\n");
81
81
printf ("%d RSA encrypt/decrypt operations %ds (%d failures)\n",
82
ITER, (int) (TIME (NULL) - start), ok);
83
freePrivateKey (hostkey);
82
ITER, (int) (GNUNET_get_time_int32 (NULL) - start), ok);
83
GNUNET_RSA_free_key (hostkey);
92
92
testEncryptPerformance ()
94
struct PrivateKey *hostkey;
96
RSAEncryptedData target;
94
struct GNUNET_RSA_PrivateKey *hostkey;
95
GNUNET_RSA_PublicKey pkey;
96
GNUNET_RSA_EncryptedData target;
98
GNUNET_CronTime start;
101
101
fprintf (stderr, "W");
102
hostkey = makePrivateKey ();
103
getPublicKey (hostkey, &pkey);
102
hostkey = GNUNET_RSA_create_key ();
103
GNUNET_RSA_get_public_key (hostkey, &pkey);
106
start = GNUNET_get_time ();
107
107
for (i = 0; i < ITER; i++)
109
109
fprintf (stderr, ".");
110
if (SYSERR == encryptPrivateKey (TESTSTRING,
111
strlen (TESTSTRING) + 1,
110
if (GNUNET_SYSERR == GNUNET_RSA_encrypt (TESTSTRING,
111
strlen (TESTSTRING) + 1,
114
114
fprintf (stderr, "encryptPrivateKey returned SYSERR\n");
119
119
printf ("%d RSA encrypt operations %llu ms (%d failures)\n",
120
ITER, get_time () - start, ok);
121
freePrivateKey (hostkey);
120
ITER, GNUNET_get_time () - start, ok);
121
GNUNET_RSA_free_key (hostkey);
123
return GNUNET_SYSERR;
129
129
testEncryptDecryptSK ()
131
struct PrivateKey *hostkey;
133
RSAEncryptedData target;
131
struct GNUNET_RSA_PrivateKey *hostkey;
132
GNUNET_RSA_PublicKey pkey;
133
GNUNET_RSA_EncryptedData target;
134
GNUNET_AES_SessionKey insk;
135
GNUNET_AES_SessionKey outsk;
137
GNUNET_Int32Time start;
140
140
fprintf (stderr, "W");
141
hostkey = makePrivateKey ();
142
getPublicKey (hostkey, &pkey);
141
hostkey = GNUNET_RSA_create_key ();
142
GNUNET_RSA_get_public_key (hostkey, &pkey);
145
GNUNET_get_time_int32 (&start);
146
146
for (i = 0; i < ITER; i++)
148
148
fprintf (stderr, ".");
149
makeSessionkey (&insk);
150
if (SYSERR == encryptPrivateKey (&insk,
151
sizeof (SESSIONKEY), &pkey, &target))
149
GNUNET_AES_create_session_key (&insk);
150
if (GNUNET_SYSERR == GNUNET_RSA_encrypt (&insk,
151
sizeof (GNUNET_AES_SessionKey),
153
154
fprintf (stderr, "encryptPrivateKey returned SYSERR\n");
157
if (-1 == decryptPrivateKey (hostkey,
158
&target, &outsk, sizeof (SESSIONKEY)))
158
if (-1 == GNUNET_RSA_decrypt (hostkey,
160
sizeof (GNUNET_AES_SessionKey)))
160
162
fprintf (stderr, "decryptPrivateKey returned SYSERR\n");
164
if (0 != memcmp (&insk, &outsk, sizeof (SESSIONKEY)))
166
if (0 != memcmp (&insk, &outsk, sizeof (GNUNET_AES_SessionKey)))
166
168
printf ("testEncryptDecryptSK failed!\n");
171
173
printf ("%d RSA encrypt/decrypt SK operations %ds (%d failures)\n",
172
ITER, (int) (TIME (NULL) - start), ok);
173
freePrivateKey (hostkey);
174
ITER, (int) (GNUNET_get_time_int32 (NULL) - start), ok);
175
GNUNET_RSA_free_key (hostkey);
177
return GNUNET_SYSERR;
180
182
testSignVerify ()
182
struct PrivateKey *hostkey;
184
struct GNUNET_RSA_PrivateKey *hostkey;
185
GNUNET_RSA_Signature sig;
186
GNUNET_RSA_PublicKey pkey;
188
GNUNET_Int32Time start;
189
191
fprintf (stderr, "W");
190
hostkey = makePrivateKey ();
191
getPublicKey (hostkey, &pkey);
192
hostkey = GNUNET_RSA_create_key ();
193
GNUNET_RSA_get_public_key (hostkey, &pkey);
194
GNUNET_get_time_int32 (&start);
193
195
for (i = 0; i < ITER; i++)
195
197
fprintf (stderr, ".");
196
if (SYSERR == sign (hostkey, strlen (TESTSTRING), TESTSTRING, &sig))
199
GNUNET_RSA_sign (hostkey, strlen (TESTSTRING), TESTSTRING, &sig))
198
201
fprintf (stderr, "sign returned SYSERR\n");
202
if (SYSERR == verifySig (TESTSTRING, strlen (TESTSTRING), &sig, &pkey))
206
GNUNET_RSA_verify (TESTSTRING, strlen (TESTSTRING), &sig, &pkey))
204
208
printf ("testSignVerify failed!\n");
209
213
printf ("%d RSA sign/verify operations %ds\n",
210
ITER, (int) (TIME (NULL) - start));
211
freePrivateKey (hostkey);
214
ITER, (int) (GNUNET_get_time_int32 (NULL) - start));
215
GNUNET_RSA_free_key (hostkey);
217
221
testSignPerformance ()
219
struct PrivateKey *hostkey;
223
struct GNUNET_RSA_PrivateKey *hostkey;
224
GNUNET_RSA_Signature sig;
225
GNUNET_RSA_PublicKey pkey;
227
GNUNET_CronTime start;
226
230
fprintf (stderr, "W");
227
hostkey = makePrivateKey ();
228
getPublicKey (hostkey, &pkey);
231
hostkey = GNUNET_RSA_create_key ();
232
GNUNET_RSA_get_public_key (hostkey, &pkey);
233
start = GNUNET_get_time ();
230
234
for (i = 0; i < ITER; i++)
232
236
fprintf (stderr, ".");
233
if (SYSERR == sign (hostkey, strlen (TESTSTRING), TESTSTRING, &sig))
238
GNUNET_RSA_sign (hostkey, strlen (TESTSTRING), TESTSTRING, &sig))
235
240
fprintf (stderr, "sign returned SYSERR\n");
240
printf ("%d RSA sign operations %llu ms\n", ITER, get_time () - start);
241
freePrivateKey (hostkey);
245
printf ("%d RSA sign operations %llu ms\n", ITER,
246
GNUNET_get_time () - start);
247
GNUNET_RSA_free_key (hostkey);
247
253
testPrivateKeyEncoding ()
249
struct PrivateKey *hostkey;
250
PrivateKeyEncoded *encoding;
252
RSAEncryptedData target;
255
struct GNUNET_RSA_PrivateKey *hostkey;
256
GNUNET_RSA_PrivateKeyEncoded *encoding;
257
GNUNET_RSA_PublicKey pkey;
258
GNUNET_RSA_EncryptedData target;
253
259
char result[MAX_TESTVAL];
261
GNUNET_Int32Time start;
258
264
fprintf (stderr, "W");
259
hostkey = makePrivateKey ();
265
hostkey = GNUNET_RSA_create_key ();
267
GNUNET_get_time_int32 (&start);
262
268
for (i = 0; i < ITER; i++)
264
270
fprintf (stderr, ".");
265
getPublicKey (hostkey, &pkey);
266
if (SYSERR == encryptPrivateKey (TESTSTRING,
267
strlen (TESTSTRING) + 1,
271
GNUNET_RSA_get_public_key (hostkey, &pkey);
272
if (GNUNET_SYSERR == GNUNET_RSA_encrypt (TESTSTRING,
273
strlen (TESTSTRING) + 1,
270
276
fprintf (stderr, "encryptPrivateKey returned SYSERR\n");
274
encoding = encodePrivateKey (hostkey);
275
freePrivateKey (hostkey);
280
encoding = GNUNET_RSA_encode_key (hostkey);
281
GNUNET_RSA_free_key (hostkey);
276
282
if (encoding == NULL)
278
284
fprintf (stderr, "encodePrivateKey returned NULL\n");
282
hostkey = decodePrivateKey (encoding);
284
if (SYSERR == decryptPrivateKey (hostkey,
286
result, strlen (TESTSTRING) + 1))
288
hostkey = GNUNET_RSA_decode_key (encoding);
289
GNUNET_free (encoding);
290
if (GNUNET_SYSERR == GNUNET_RSA_decrypt (hostkey,
293
strlen (TESTSTRING) + 1))
288
295
fprintf (stderr, "decryptPrivateKey returned SYSERR\n");
292
299
if (strncmp (TESTSTRING, result, strlen (TESTSTRING)) != 0)
294
301
printf ("%s != %.*s - testEncryptDecrypt failed!\n",
295
302
TESTSTRING, (int) strlen (TESTSTRING), result);
300
freePrivateKey (hostkey);
307
GNUNET_RSA_free_key (hostkey);
301
308
printf ("%d RSA encrypt/encode/decode/decrypt operations %ds\n",
302
ITER, (int) (TIME (NULL) - start));
309
ITER, (int) (GNUNET_get_time_int32 (NULL) - start));
309
316
int failureCount = 0;
312
if (OK != testEncryptPerformance ())
319
if (GNUNET_OK != testEncryptPerformance ())
314
if (OK != testSignPerformance ())
321
if (GNUNET_OK != testSignPerformance ())
317
if (OK != testEncryptDecryptSK ())
319
if (OK != testEncryptDecrypt ())
321
if (OK != testSignVerify ())
323
if (OK != testPrivateKeyEncoding ())
324
if (GNUNET_OK != testEncryptDecryptSK ())
326
if (GNUNET_OK != testEncryptDecrypt ())
328
if (GNUNET_OK != testSignVerify ())
330
if (GNUNET_OK != testPrivateKeyEncoding ())
326
333
if (failureCount != 0)