diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-07-14 21:19:22 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-07-14 21:19:22 +0000 |
commit | a299afa8af5593c2114ca8242099d8f7971c428e (patch) | |
tree | d6474062280de16f92cb7f2239393094dcf472cd /src/util/test_crypto_rsa.c | |
parent | 7958f822ccba4fe2dc372c62aaaa2b2d8fe56acf (diff) | |
download | gnunet-a299afa8af5593c2114ca8242099d8f7971c428e.tar.gz gnunet-a299afa8af5593c2114ca8242099d8f7971c428e.zip |
-new API for asyncronous generation of private RSA keys
Diffstat (limited to 'src/util/test_crypto_rsa.c')
-rw-r--r-- | src/util/test_crypto_rsa.c | 83 |
1 files changed, 51 insertions, 32 deletions
diff --git a/src/util/test_crypto_rsa.c b/src/util/test_crypto_rsa.c index f6800afcd..0113b7ad0 100644 --- a/src/util/test_crypto_rsa.c +++ b/src/util/test_crypto_rsa.c | |||
@@ -36,10 +36,12 @@ | |||
36 | 36 | ||
37 | #define PERF GNUNET_YES | 37 | #define PERF GNUNET_YES |
38 | 38 | ||
39 | static struct GNUNET_CRYPTO_RsaPrivateKey *key; | ||
40 | |||
41 | |||
39 | static int | 42 | static int |
40 | testEncryptDecrypt () | 43 | testEncryptDecrypt () |
41 | { | 44 | { |
42 | struct GNUNET_CRYPTO_RsaPrivateKey *hostkey; | ||
43 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey; | 45 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey; |
44 | struct GNUNET_CRYPTO_RsaEncryptedData target; | 46 | struct GNUNET_CRYPTO_RsaEncryptedData target; |
45 | char result[MAX_TESTVAL]; | 47 | char result[MAX_TESTVAL]; |
@@ -48,9 +50,7 @@ testEncryptDecrypt () | |||
48 | int ok; | 50 | int ok; |
49 | 51 | ||
50 | FPRINTF (stderr, "%s", "W"); | 52 | FPRINTF (stderr, "%s", "W"); |
51 | hostkey = GNUNET_CRYPTO_rsa_key_create (); | 53 | GNUNET_CRYPTO_rsa_key_get_public (key, &pkey); |
52 | GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey); | ||
53 | |||
54 | ok = 0; | 54 | ok = 0; |
55 | start = GNUNET_TIME_absolute_get (); | 55 | start = GNUNET_TIME_absolute_get (); |
56 | for (i = 0; i < ITER; i++) | 56 | for (i = 0; i < ITER; i++) |
@@ -65,7 +65,7 @@ testEncryptDecrypt () | |||
65 | continue; | 65 | continue; |
66 | } | 66 | } |
67 | if (-1 == | 67 | if (-1 == |
68 | GNUNET_CRYPTO_rsa_decrypt (hostkey, &target, result, | 68 | GNUNET_CRYPTO_rsa_decrypt (key, &target, result, |
69 | strlen (TESTSTRING) + 1)) | 69 | strlen (TESTSTRING) + 1)) |
70 | { | 70 | { |
71 | FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n"); | 71 | FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n"); |
@@ -84,18 +84,16 @@ testEncryptDecrypt () | |||
84 | printf ("%d RSA encrypt/decrypt operations %llums (%d failures)\n", ITER, | 84 | printf ("%d RSA encrypt/decrypt operations %llums (%d failures)\n", ITER, |
85 | (unsigned long long) | 85 | (unsigned long long) |
86 | GNUNET_TIME_absolute_get_duration (start).rel_value, ok); | 86 | GNUNET_TIME_absolute_get_duration (start).rel_value, ok); |
87 | GNUNET_CRYPTO_rsa_key_free (hostkey); | ||
88 | if (ok == 0) | 87 | if (ok == 0) |
89 | return GNUNET_OK; | 88 | return GNUNET_OK; |
90 | else | 89 | return GNUNET_SYSERR; |
91 | return GNUNET_SYSERR; | ||
92 | } | 90 | } |
93 | 91 | ||
92 | |||
94 | #if PERF | 93 | #if PERF |
95 | static int | 94 | static int |
96 | testEncryptPerformance () | 95 | testEncryptPerformance () |
97 | { | 96 | { |
98 | struct GNUNET_CRYPTO_RsaPrivateKey *hostkey; | ||
99 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey; | 97 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey; |
100 | struct GNUNET_CRYPTO_RsaEncryptedData target; | 98 | struct GNUNET_CRYPTO_RsaEncryptedData target; |
101 | int i; | 99 | int i; |
@@ -103,9 +101,7 @@ testEncryptPerformance () | |||
103 | int ok; | 101 | int ok; |
104 | 102 | ||
105 | FPRINTF (stderr, "%s", "W"); | 103 | FPRINTF (stderr, "%s", "W"); |
106 | hostkey = GNUNET_CRYPTO_rsa_key_create (); | 104 | GNUNET_CRYPTO_rsa_key_get_public (key, &pkey); |
107 | GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey); | ||
108 | |||
109 | ok = 0; | 105 | ok = 0; |
110 | start = GNUNET_TIME_absolute_get (); | 106 | start = GNUNET_TIME_absolute_get (); |
111 | for (i = 0; i < ITER; i++) | 107 | for (i = 0; i < ITER; i++) |
@@ -123,7 +119,6 @@ testEncryptPerformance () | |||
123 | printf ("%d RSA encrypt operations %llu ms (%d failures)\n", ITER, | 119 | printf ("%d RSA encrypt operations %llu ms (%d failures)\n", ITER, |
124 | (unsigned long long) | 120 | (unsigned long long) |
125 | GNUNET_TIME_absolute_get_duration (start).rel_value, ok); | 121 | GNUNET_TIME_absolute_get_duration (start).rel_value, ok); |
126 | GNUNET_CRYPTO_rsa_key_free (hostkey); | ||
127 | if (ok != 0) | 122 | if (ok != 0) |
128 | return GNUNET_SYSERR; | 123 | return GNUNET_SYSERR; |
129 | return GNUNET_OK; | 124 | return GNUNET_OK; |
@@ -133,7 +128,6 @@ testEncryptPerformance () | |||
133 | static int | 128 | static int |
134 | testEncryptDecryptSK () | 129 | testEncryptDecryptSK () |
135 | { | 130 | { |
136 | struct GNUNET_CRYPTO_RsaPrivateKey *hostkey; | ||
137 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey; | 131 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey; |
138 | struct GNUNET_CRYPTO_RsaEncryptedData target; | 132 | struct GNUNET_CRYPTO_RsaEncryptedData target; |
139 | struct GNUNET_CRYPTO_AesSessionKey insk; | 133 | struct GNUNET_CRYPTO_AesSessionKey insk; |
@@ -143,9 +137,7 @@ testEncryptDecryptSK () | |||
143 | int ok; | 137 | int ok; |
144 | 138 | ||
145 | FPRINTF (stderr, "%s", "W"); | 139 | FPRINTF (stderr, "%s", "W"); |
146 | hostkey = GNUNET_CRYPTO_rsa_key_create (); | 140 | GNUNET_CRYPTO_rsa_key_get_public (key, &pkey); |
147 | GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey); | ||
148 | |||
149 | ok = 0; | 141 | ok = 0; |
150 | start = GNUNET_TIME_absolute_get (); | 142 | start = GNUNET_TIME_absolute_get (); |
151 | for (i = 0; i < ITER; i++) | 143 | for (i = 0; i < ITER; i++) |
@@ -162,7 +154,7 @@ testEncryptDecryptSK () | |||
162 | continue; | 154 | continue; |
163 | } | 155 | } |
164 | if (-1 == | 156 | if (-1 == |
165 | GNUNET_CRYPTO_rsa_decrypt (hostkey, &target, &outsk, | 157 | GNUNET_CRYPTO_rsa_decrypt (key, &target, &outsk, |
166 | sizeof (struct GNUNET_CRYPTO_AesSessionKey))) | 158 | sizeof (struct GNUNET_CRYPTO_AesSessionKey))) |
167 | { | 159 | { |
168 | FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n"); | 160 | FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n"); |
@@ -180,7 +172,6 @@ testEncryptDecryptSK () | |||
180 | printf ("%d RSA encrypt/decrypt SK operations %llums (%d failures)\n", ITER, | 172 | printf ("%d RSA encrypt/decrypt SK operations %llums (%d failures)\n", ITER, |
181 | (unsigned long long) | 173 | (unsigned long long) |
182 | GNUNET_TIME_absolute_get_duration (start).rel_value, ok); | 174 | GNUNET_TIME_absolute_get_duration (start).rel_value, ok); |
183 | GNUNET_CRYPTO_rsa_key_free (hostkey); | ||
184 | if (ok != 0) | 175 | if (ok != 0) |
185 | return GNUNET_SYSERR; | 176 | return GNUNET_SYSERR; |
186 | return GNUNET_OK; | 177 | return GNUNET_OK; |
@@ -190,7 +181,6 @@ testEncryptDecryptSK () | |||
190 | static int | 181 | static int |
191 | testSignVerify () | 182 | testSignVerify () |
192 | { | 183 | { |
193 | struct GNUNET_CRYPTO_RsaPrivateKey *hostkey; | ||
194 | struct GNUNET_CRYPTO_RsaSignature sig; | 184 | struct GNUNET_CRYPTO_RsaSignature sig; |
195 | struct GNUNET_CRYPTO_RsaSignaturePurpose purp; | 185 | struct GNUNET_CRYPTO_RsaSignaturePurpose purp; |
196 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey; | 186 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey; |
@@ -199,8 +189,7 @@ testSignVerify () | |||
199 | int ok = GNUNET_OK; | 189 | int ok = GNUNET_OK; |
200 | 190 | ||
201 | FPRINTF (stderr, "%s", "W"); | 191 | FPRINTF (stderr, "%s", "W"); |
202 | hostkey = GNUNET_CRYPTO_rsa_key_create (); | 192 | GNUNET_CRYPTO_rsa_key_get_public (key, &pkey); |
203 | GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey); | ||
204 | start = GNUNET_TIME_absolute_get (); | 193 | start = GNUNET_TIME_absolute_get (); |
205 | purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)); | 194 | purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)); |
206 | purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST); | 195 | purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST); |
@@ -208,7 +197,7 @@ testSignVerify () | |||
208 | for (i = 0; i < ITER; i++) | 197 | for (i = 0; i < ITER; i++) |
209 | { | 198 | { |
210 | FPRINTF (stderr, "%s", "."); | 199 | FPRINTF (stderr, "%s", "."); |
211 | if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig)) | 200 | if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (key, &purp, &sig)) |
212 | { | 201 | { |
213 | FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_sign returned SYSERR\n"); | 202 | FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_sign returned SYSERR\n"); |
214 | ok = GNUNET_SYSERR; | 203 | ok = GNUNET_SYSERR; |
@@ -234,7 +223,6 @@ testSignVerify () | |||
234 | printf ("%d RSA sign/verify operations %llums\n", ITER, | 223 | printf ("%d RSA sign/verify operations %llums\n", ITER, |
235 | (unsigned long long) | 224 | (unsigned long long) |
236 | GNUNET_TIME_absolute_get_duration (start).rel_value); | 225 | GNUNET_TIME_absolute_get_duration (start).rel_value); |
237 | GNUNET_CRYPTO_rsa_key_free (hostkey); | ||
238 | return ok; | 226 | return ok; |
239 | } | 227 | } |
240 | 228 | ||
@@ -243,7 +231,6 @@ testSignVerify () | |||
243 | static int | 231 | static int |
244 | testSignPerformance () | 232 | testSignPerformance () |
245 | { | 233 | { |
246 | struct GNUNET_CRYPTO_RsaPrivateKey *hostkey; | ||
247 | struct GNUNET_CRYPTO_RsaSignaturePurpose purp; | 234 | struct GNUNET_CRYPTO_RsaSignaturePurpose purp; |
248 | struct GNUNET_CRYPTO_RsaSignature sig; | 235 | struct GNUNET_CRYPTO_RsaSignature sig; |
249 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey; | 236 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey; |
@@ -254,13 +241,12 @@ testSignPerformance () | |||
254 | purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)); | 241 | purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)); |
255 | purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST); | 242 | purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST); |
256 | FPRINTF (stderr, "%s", "W"); | 243 | FPRINTF (stderr, "%s", "W"); |
257 | hostkey = GNUNET_CRYPTO_rsa_key_create (); | 244 | GNUNET_CRYPTO_rsa_key_get_public (key, &pkey); |
258 | GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey); | ||
259 | start = GNUNET_TIME_absolute_get (); | 245 | start = GNUNET_TIME_absolute_get (); |
260 | for (i = 0; i < ITER; i++) | 246 | for (i = 0; i < ITER; i++) |
261 | { | 247 | { |
262 | FPRINTF (stderr, "%s", "."); | 248 | FPRINTF (stderr, "%s", "."); |
263 | if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig)) | 249 | if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (key, &purp, &sig)) |
264 | { | 250 | { |
265 | FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_sign returned SYSERR\n"); | 251 | FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_sign returned SYSERR\n"); |
266 | ok = GNUNET_SYSERR; | 252 | ok = GNUNET_SYSERR; |
@@ -270,7 +256,6 @@ testSignPerformance () | |||
270 | printf ("%d RSA sign operations %llu ms\n", ITER, | 256 | printf ("%d RSA sign operations %llu ms\n", ITER, |
271 | (unsigned long long) | 257 | (unsigned long long) |
272 | GNUNET_TIME_absolute_get_duration (start).rel_value); | 258 | GNUNET_TIME_absolute_get_duration (start).rel_value); |
273 | GNUNET_CRYPTO_rsa_key_free (hostkey); | ||
274 | return ok; | 259 | return ok; |
275 | } | 260 | } |
276 | #endif | 261 | #endif |
@@ -279,7 +264,6 @@ testSignPerformance () | |||
279 | static int | 264 | static int |
280 | testCreateFromFile () | 265 | testCreateFromFile () |
281 | { | 266 | { |
282 | struct GNUNET_CRYPTO_RsaPrivateKey *key; | ||
283 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p1; | 267 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p1; |
284 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p2; | 268 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p2; |
285 | 269 | ||
@@ -297,12 +281,44 @@ testCreateFromFile () | |||
297 | GNUNET_assert (NULL != key); | 281 | GNUNET_assert (NULL != key); |
298 | GNUNET_CRYPTO_rsa_key_get_public (key, &p2); | 282 | GNUNET_CRYPTO_rsa_key_get_public (key, &p2); |
299 | GNUNET_assert (0 != memcmp (&p1, &p2, sizeof (p1))); | 283 | GNUNET_assert (0 != memcmp (&p1, &p2, sizeof (p1))); |
300 | GNUNET_CRYPTO_rsa_key_free (key); | ||
301 | GNUNET_assert (0 == UNLINK (KEYFILE)); | ||
302 | return GNUNET_OK; | 284 | return GNUNET_OK; |
303 | } | 285 | } |
304 | 286 | ||
305 | 287 | ||
288 | static void | ||
289 | key_cont (void *cls, | ||
290 | struct GNUNET_CRYPTO_RsaPrivateKey *pk, | ||
291 | const char *emsg) | ||
292 | { | ||
293 | const char *txt = cls; | ||
294 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub1; | ||
295 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub2; | ||
296 | |||
297 | GNUNET_assert (0 == strcmp ("ok", txt)); | ||
298 | GNUNET_CRYPTO_rsa_key_get_public (pk, &pub1); | ||
299 | GNUNET_CRYPTO_rsa_key_get_public (key, &pub2); | ||
300 | GNUNET_assert (0 == memcmp (&pub1, &pub2, | ||
301 | sizeof (pub1))); | ||
302 | GNUNET_CRYPTO_rsa_key_free (pk); | ||
303 | } | ||
304 | |||
305 | |||
306 | static void | ||
307 | test_async_creation (void *cls, | ||
308 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
309 | { | ||
310 | struct GNUNET_CRYPTO_RsaKeyGenerationContext *gc; | ||
311 | |||
312 | gc = GNUNET_CRYPTO_rsa_key_create_start (KEYFILE, | ||
313 | &key_cont, | ||
314 | (void*) "bug"); | ||
315 | GNUNET_CRYPTO_rsa_key_create_stop (gc); | ||
316 | gc = GNUNET_CRYPTO_rsa_key_create_start (KEYFILE, | ||
317 | &key_cont, | ||
318 | (void*) "ok"); | ||
319 | } | ||
320 | |||
321 | |||
306 | int | 322 | int |
307 | main (int argc, char *argv[]) | 323 | main (int argc, char *argv[]) |
308 | { | 324 | { |
@@ -312,6 +328,7 @@ main (int argc, char *argv[]) | |||
312 | GNUNET_CRYPTO_random_disable_entropy_gathering (); | 328 | GNUNET_CRYPTO_random_disable_entropy_gathering (); |
313 | if (GNUNET_OK != testCreateFromFile ()) | 329 | if (GNUNET_OK != testCreateFromFile ()) |
314 | failureCount++; | 330 | failureCount++; |
331 | GNUNET_SCHEDULER_run (&test_async_creation, NULL); | ||
315 | #if PERF | 332 | #if PERF |
316 | if (GNUNET_OK != testEncryptPerformance ()) | 333 | if (GNUNET_OK != testEncryptPerformance ()) |
317 | failureCount++; | 334 | failureCount++; |
@@ -324,6 +341,8 @@ main (int argc, char *argv[]) | |||
324 | failureCount++; | 341 | failureCount++; |
325 | if (GNUNET_OK != testSignVerify ()) | 342 | if (GNUNET_OK != testSignVerify ()) |
326 | failureCount++; | 343 | failureCount++; |
344 | GNUNET_CRYPTO_rsa_key_free (key); | ||
345 | GNUNET_assert (0 == UNLINK (KEYFILE)); | ||
327 | 346 | ||
328 | if (failureCount != 0) | 347 | if (failureCount != 0) |
329 | { | 348 | { |