aboutsummaryrefslogtreecommitdiff
path: root/src/util/gnunet-ecc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/gnunet-ecc.c')
-rw-r--r--src/util/gnunet-ecc.c208
1 files changed, 93 insertions, 115 deletions
diff --git a/src/util/gnunet-ecc.c b/src/util/gnunet-ecc.c
index d2253337c..e7a170b81 100644
--- a/src/util/gnunet-ecc.c
+++ b/src/util/gnunet-ecc.c
@@ -31,7 +31,7 @@
31/** 31/**
32 * Number of characters a Base32-encoded public key requires. 32 * Number of characters a Base32-encoded public key requires.
33 */ 33 */
34#define KEY_STR_LEN sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)*8/5+1 34#define KEY_STR_LEN sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) * 8 / 5 + 1
35 35
36/** 36/**
37 * Flag for listing public key. 37 * Flag for listing public key.
@@ -91,7 +91,7 @@ create_keys (const char *fn, const char *prefix)
91 91
92 if (NULL == (f = fopen (fn, "w+"))) 92 if (NULL == (f = fopen (fn, "w+")))
93 { 93 {
94 fprintf (stderr, _("Failed to open `%s': %s\n"), fn, STRERROR (errno)); 94 fprintf (stderr, _ ("Failed to open `%s': %s\n"), fn, strerror (errno));
95 return; 95 return;
96 } 96 }
97 if (NULL != prefix) 97 if (NULL != prefix)
@@ -116,7 +116,7 @@ create_keys (const char *fn, const char *prefix)
116 * rest = 5%8 = 5 (bits) 116 * rest = 5%8 = 5 (bits)
117 * mask = ~(2**(8 - 5) - 1) = ~(2**3 - 1) = ~(8 - 1) = ~b111 = b11111000 117 * mask = ~(2**(8 - 5) - 1) = ~(2**3 - 1) = ~(8 - 1) = ~b111 = b11111000
118 */ 118 */
119 mask = ~ ((int)pow (2, 8 - rest) - 1); 119 mask = ~((int) pow (2, 8 - rest) - 1);
120 target_byte = ((unsigned char *) &target_pub)[n] & mask; 120 target_byte = ((unsigned char *) &target_pub)[n] & mask;
121 } 121 }
122 else 122 else
@@ -126,21 +126,15 @@ create_keys (const char *fn, const char *prefix)
126 } 126 }
127 s = GNUNET_CRYPTO_eddsa_public_key_to_string (&target_pub); 127 s = GNUNET_CRYPTO_eddsa_public_key_to_string (&target_pub);
128 fprintf (stderr, 128 fprintf (stderr,
129 _("Generating %u keys like %s, please wait"), 129 _ ("Generating %u keys like %s, please wait"),
130 make_keys, 130 make_keys,
131 s); 131 s);
132 GNUNET_free (s); 132 GNUNET_free (s);
133 fprintf (stderr, 133 fprintf (stderr, "\nattempt %s [%u, %X]\n", vanity, (unsigned int) n, mask);
134 "\nattempt %s [%u, %X]\n",
135 vanity,
136 (unsigned int) n,
137 mask);
138 } 134 }
139 else 135 else
140 { 136 {
141 fprintf (stderr, 137 fprintf (stderr, _ ("Generating %u keys, please wait"), make_keys);
142 _("Generating %u keys, please wait"),
143 make_keys);
144 /* Just so old (debian) versions of GCC calm down with the warnings. */ 138 /* Just so old (debian) versions of GCC calm down with the warnings. */
145 n = rest = target_byte = mask = 0; 139 n = rest = target_byte = mask = 0;
146 } 140 }
@@ -150,8 +144,8 @@ create_keys (const char *fn, const char *prefix)
150 fprintf (stderr, "."); 144 fprintf (stderr, ".");
151 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create ())) 145 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create ()))
152 { 146 {
153 GNUNET_break (0); 147 GNUNET_break (0);
154 break; 148 break;
155 } 149 }
156 if (NULL != prefix) 150 if (NULL != prefix)
157 { 151 {
@@ -176,38 +170,32 @@ create_keys (const char *fn, const char *prefix)
176 } 170 }
177 } 171 }
178 if (GNUNET_TESTING_HOSTKEYFILESIZE != 172 if (GNUNET_TESTING_HOSTKEYFILESIZE !=
179 fwrite (pk, 1, 173 fwrite (pk, 1, GNUNET_TESTING_HOSTKEYFILESIZE, f))
180 GNUNET_TESTING_HOSTKEYFILESIZE, f))
181 { 174 {
182 fprintf (stderr, 175 fprintf (stderr,
183 _("\nFailed to write to `%s': %s\n"), 176 _ ("\nFailed to write to `%s': %s\n"),
184 fn, 177 fn,
185 STRERROR (errno)); 178 strerror (errno));
186 GNUNET_free (pk); 179 GNUNET_free (pk);
187 break; 180 break;
188 } 181 }
189 GNUNET_free (pk); 182 GNUNET_free (pk);
190 } 183 }
191 if (UINT_MAX == make_keys) 184 if (UINT_MAX == make_keys)
192 fprintf (stderr, 185 fprintf (stderr, _ ("\nFinished!\n"));
193 _("\nFinished!\n"));
194 else 186 else
195 fprintf (stderr, 187 fprintf (stderr, _ ("\nError, %u keys not generated\n"), make_keys);
196 _("\nError, %u keys not generated\n"),
197 make_keys);
198 fclose (f); 188 fclose (f);
199} 189}
200 190
201 191
202static void 192static void
203print_hex (const char *msg, 193print_hex (const char *msg, const void *buf, size_t size)
204 const void *buf,
205 size_t size)
206{ 194{
207 printf ("%s: ", msg); 195 printf ("%s: ", msg);
208 for (size_t i = 0; i < size; i++) 196 for (size_t i = 0; i < size; i++)
209 { 197 {
210 printf ("%02hhx", ((const uint8_t *)buf)[i]); 198 printf ("%02hhx", ((const uint8_t *) buf)[i]);
211 } 199 }
212 printf ("\n"); 200 printf ("\n");
213} 201}
@@ -230,7 +218,8 @@ print_examples_ecdh ()
230 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv1, dh_pub1); 218 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv1, dh_pub1);
231 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv2, dh_pub2); 219 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv2, dh_pub2);
232 220
233 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_priv1, 32, buf, 128)); 221 GNUNET_assert (NULL !=
222 GNUNET_STRINGS_data_to_string (dh_priv1, 32, buf, 128));
234 printf ("ECDHE key 1:\n"); 223 printf ("ECDHE key 1:\n");
235 printf ("private: %s\n", buf); 224 printf ("private: %s\n", buf);
236 print_hex ("private(hex)", dh_priv1, sizeof *dh_priv1); 225 print_hex ("private(hex)", dh_priv1, sizeof *dh_priv1);
@@ -238,7 +227,8 @@ print_examples_ecdh ()
238 printf ("public: %s\n", buf); 227 printf ("public: %s\n", buf);
239 print_hex ("public(hex)", dh_pub1, sizeof *dh_pub1); 228 print_hex ("public(hex)", dh_pub1, sizeof *dh_pub1);
240 229
241 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_priv2, 32, buf, 128)); 230 GNUNET_assert (NULL !=
231 GNUNET_STRINGS_data_to_string (dh_priv2, 32, buf, 128));
242 printf ("ECDHE key 2:\n"); 232 printf ("ECDHE key 2:\n");
243 printf ("private: %s\n", buf); 233 printf ("private: %s\n", buf);
244 print_hex ("private(hex)", dh_priv2, sizeof *dh_priv2); 234 print_hex ("private(hex)", dh_priv2, sizeof *dh_priv2);
@@ -246,7 +236,8 @@ print_examples_ecdh ()
246 printf ("public: %s\n", buf); 236 printf ("public: %s\n", buf);
247 print_hex ("public(hex)", dh_pub2, sizeof *dh_pub2); 237 print_hex ("public(hex)", dh_pub2, sizeof *dh_pub2);
248 238
249 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_ecc_ecdh (dh_priv1, dh_pub2, &hash)); 239 GNUNET_assert (GNUNET_OK ==
240 GNUNET_CRYPTO_ecc_ecdh (dh_priv1, dh_pub2, &hash));
250 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (&hash, 64, buf, 128)); 241 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (&hash, 64, buf, 128));
251 printf ("ECDH shared secret: %s\n", buf); 242 printf ("ECDH shared secret: %s\n", buf);
252 243
@@ -282,56 +273,39 @@ print_key (const char *filename)
282 unsigned int c; 273 unsigned int c;
283 ssize_t sret; 274 ssize_t sret;
284 275
285 if (GNUNET_YES != 276 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
286 GNUNET_DISK_file_test (filename))
287 { 277 {
288 fprintf (stderr, 278 fprintf (stderr, _ ("Hostkeys file `%s' not found\n"), filename);
289 _("Hostkeys file `%s' not found\n"),
290 filename);
291 return; 279 return;
292 } 280 }
293 281
294 /* Check hostkey file size, read entire thing into memory */ 282 /* Check hostkey file size, read entire thing into memory */
295 if (GNUNET_OK != 283 if (GNUNET_OK !=
296 GNUNET_DISK_file_size (filename, 284 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
297 &fs,
298 GNUNET_YES,
299 GNUNET_YES))
300 fs = 0; 285 fs = 0;
301 if (0 == fs) 286 if (0 == fs)
302 { 287 {
303 fprintf (stderr, 288 fprintf (stderr, _ ("Hostkeys file `%s' is empty\n"), filename);
304 _("Hostkeys file `%s' is empty\n"), 289 return; /* File is empty */
305 filename);
306 return; /* File is empty */
307 } 290 }
308 if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE)) 291 if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE))
309 { 292 {
310 fprintf (stderr, 293 fprintf (stderr, _ ("Incorrect hostkey file format: %s\n"), filename);
311 _("Incorrect hostkey file format: %s\n"),
312 filename);
313 return; 294 return;
314 } 295 }
315 fd = GNUNET_DISK_file_open (filename, 296 fd = GNUNET_DISK_file_open (filename,
316 GNUNET_DISK_OPEN_READ, 297 GNUNET_DISK_OPEN_READ,
317 GNUNET_DISK_PERM_NONE); 298 GNUNET_DISK_PERM_NONE);
318 if (NULL == fd) 299 if (NULL == fd)
319 { 300 {
320 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, 301 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename);
321 "open",
322 filename);
323 return; 302 return;
324 } 303 }
325 hostkeys_data = GNUNET_malloc (fs); 304 hostkeys_data = GNUNET_malloc (fs);
326 sret = GNUNET_DISK_file_read (fd, 305 sret = GNUNET_DISK_file_read (fd, hostkeys_data, fs);
327 hostkeys_data, 306 if ((sret < 0) || (fs != (size_t) sret))
328 fs);
329 if ( (sret < 0) ||
330 (fs != (size_t) sret) )
331 { 307 {
332 fprintf (stderr, 308 fprintf (stderr, _ ("Could not read hostkey file: %s\n"), filename);
333 _("Could not read hostkey file: %s\n"),
334 filename);
335 GNUNET_free (hostkeys_data); 309 GNUNET_free (hostkeys_data);
336 GNUNET_DISK_file_close (fd); 310 GNUNET_DISK_file_close (fd);
337 return; 311 return;
@@ -344,8 +318,8 @@ print_key (const char *filename)
344 for (c = 0; (c < total_hostkeys) && (c < list_keys_count); c++) 318 for (c = 0; (c < total_hostkeys) && (c < list_keys_count); c++)
345 { 319 {
346 GNUNET_memcpy (&private_key, 320 GNUNET_memcpy (&private_key,
347 hostkeys_data + (c * GNUNET_TESTING_HOSTKEYFILESIZE), 321 hostkeys_data + (c * GNUNET_TESTING_HOSTKEYFILESIZE),
348 GNUNET_TESTING_HOSTKEYFILESIZE); 322 GNUNET_TESTING_HOSTKEYFILESIZE);
349 GNUNET_CRYPTO_eddsa_key_get_public (&private_key, &public_key); 323 GNUNET_CRYPTO_eddsa_key_get_public (&private_key, &public_key);
350 hostkey_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&public_key); 324 hostkey_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&public_key);
351 if (NULL != hostkey_str) 325 if (NULL != hostkey_str)
@@ -377,7 +351,7 @@ run (void *cls,
377 (void) cls; 351 (void) cls;
378 (void) cfgfile; 352 (void) cfgfile;
379 (void) cfg; 353 (void) cfg;
380 354
381 if (print_examples_flag) 355 if (print_examples_flag)
382 { 356 {
383 print_examples (); 357 print_examples ();
@@ -385,9 +359,7 @@ run (void *cls,
385 } 359 }
386 if (NULL == args[0]) 360 if (NULL == args[0])
387 { 361 {
388 FPRINTF (stderr, 362 fprintf (stderr, "%s", _ ("No hostkey file specified on command line\n"));
389 "%s",
390 _("No hostkey file specified on command line\n"));
391 return; 363 return;
392 } 364 }
393 if (list_keys) 365 if (list_keys)
@@ -407,12 +379,12 @@ run (void *cls,
407 struct GNUNET_CRYPTO_EddsaPrivateKey pk; 379 struct GNUNET_CRYPTO_EddsaPrivateKey pk;
408 struct GNUNET_CRYPTO_EddsaPublicKey pub; 380 struct GNUNET_CRYPTO_EddsaPublicKey pub;
409 381
410 keyfile = GNUNET_DISK_file_open (args[0], GNUNET_DISK_OPEN_READ, 382 keyfile = GNUNET_DISK_file_open (args[0],
383 GNUNET_DISK_OPEN_READ,
411 GNUNET_DISK_PERM_NONE); 384 GNUNET_DISK_PERM_NONE);
412 if (NULL == keyfile) 385 if (NULL == keyfile)
413 return; 386 return;
414 while (sizeof (pk) == 387 while (sizeof (pk) == GNUNET_DISK_file_read (keyfile, &pk, sizeof (pk)))
415 GNUNET_DISK_file_read (keyfile, &pk, sizeof (pk)))
416 { 388 {
417 GNUNET_CRYPTO_eddsa_key_get_public (&pk, &pub); 389 GNUNET_CRYPTO_eddsa_key_get_public (&pk, &pub);
418 if (print_public_key_hex) 390 if (print_public_key_hex)
@@ -422,19 +394,18 @@ run (void *cls,
422 else if (print_public_key) 394 else if (print_public_key)
423 { 395 {
424 str = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub); 396 str = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
425 FPRINTF (stdout, "%s\n", str); 397 fprintf (stdout, "%s\n", str);
426 GNUNET_free (str); 398 GNUNET_free (str);
427 } 399 }
428 else if (print_private_key) 400 else if (print_private_key)
429 { 401 {
430 str = GNUNET_CRYPTO_eddsa_private_key_to_string (&pk); 402 str = GNUNET_CRYPTO_eddsa_private_key_to_string (&pk);
431 FPRINTF (stdout, "%s\n", str); 403 fprintf (stdout, "%s\n", str);
432 GNUNET_free (str); 404 GNUNET_free (str);
433 } 405 }
434 } 406 }
435 GNUNET_DISK_file_close (keyfile); 407 GNUNET_DISK_file_close (keyfile);
436 } 408 }
437
438} 409}
439 410
440 411
@@ -446,59 +417,66 @@ run (void *cls,
446 * @return 0 ok, 1 on error 417 * @return 0 ok, 1 on error
447 */ 418 */
448int 419int
449main (int argc, 420main (int argc, char *const *argv)
450 char *const *argv)
451{ 421{
452 struct GNUNET_GETOPT_CommandLineOption options[] = { 422 struct GNUNET_GETOPT_CommandLineOption options[] =
453 GNUNET_GETOPT_option_flag ('i', 423 {GNUNET_GETOPT_option_flag ('i',
454 "iterate", 424 "iterate",
455 gettext_noop ("list keys included in a file (for testing)"), 425 gettext_noop (
456 &list_keys), 426 "list keys included in a file (for testing)"),
457 GNUNET_GETOPT_option_uint ('e', 427 &list_keys),
458 "end=", 428 GNUNET_GETOPT_option_uint (
459 "COUNT", 429 'e',
460 gettext_noop ("number of keys to list included in a file (for testing)"), 430 "end=",
461 &list_keys_count), 431 "COUNT",
462 GNUNET_GETOPT_option_uint ('g', 432 gettext_noop ("number of keys to list included in a file (for testing)"),
463 "generate-keys", 433 &list_keys_count),
464 "COUNT", 434 GNUNET_GETOPT_option_uint (
465 gettext_noop ("create COUNT public-private key pairs (for testing)"), 435 'g',
466 &make_keys), 436 "generate-keys",
467 GNUNET_GETOPT_option_flag ('p', 437 "COUNT",
468 "print-public-key", 438 gettext_noop ("create COUNT public-private key pairs (for testing)"),
469 gettext_noop ("print the public key in ASCII format"), 439 &make_keys),
470 &print_public_key), 440 GNUNET_GETOPT_option_flag ('p',
471 GNUNET_GETOPT_option_flag ('P', 441 "print-public-key",
472 "print-private-key", 442 gettext_noop (
473 gettext_noop ("print the private key in ASCII format"), 443 "print the public key in ASCII format"),
474 &print_private_key), 444 &print_public_key),
475 GNUNET_GETOPT_option_flag ('x', 445 GNUNET_GETOPT_option_flag ('P',
476 "print-hex", 446 "print-private-key",
477 gettext_noop ("print the public key in HEX format"), 447 gettext_noop (
478 &print_public_key_hex), 448 "print the private key in ASCII format"),
479 GNUNET_GETOPT_option_flag ('E', 449 &print_private_key),
480 "examples", 450 GNUNET_GETOPT_option_flag ('x',
481 gettext_noop ("print examples of ECC operations (used for compatibility testing)"), 451 "print-hex",
482 &print_examples_flag), 452 gettext_noop (
483 GNUNET_GETOPT_OPTION_END 453 "print the public key in HEX format"),
484 }; 454 &print_public_key_hex),
455 GNUNET_GETOPT_option_flag (
456 'E',
457 "examples",
458 gettext_noop (
459 "print examples of ECC operations (used for compatibility testing)"),
460 &print_examples_flag),
461 GNUNET_GETOPT_OPTION_END};
485 int ret; 462 int ret;
486 463
487 list_keys_count = UINT32_MAX; 464 list_keys_count = UINT32_MAX;
488 if (GNUNET_OK != 465 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
489 GNUNET_STRINGS_get_utf8_args (argc, argv,
490 &argc, &argv))
491 return 2; 466 return 2;
492 467
493 ret = (GNUNET_OK == 468 ret = (GNUNET_OK ==
494 GNUNET_PROGRAM_run (argc, 469 GNUNET_PROGRAM_run (argc,
495 argv, 470 argv,
496 "gnunet-ecc [OPTIONS] keyfile [VANITY_PREFIX]", 471 "gnunet-ecc [OPTIONS] keyfile [VANITY_PREFIX]",
497 gettext_noop ("Manipulate GNUnet private ECC key files"), 472 gettext_noop (
498 options, 473 "Manipulate GNUnet private ECC key files"),
474 options,
499 &run, 475 &run,
500 NULL)) ? 0 : 1; 476 NULL))
501 GNUNET_free ((void*) argv); 477 ? 0
478 : 1;
479 GNUNET_free ((void *) argv);
502 return ret; 480 return ret;
503} 481}
504 482