diff options
-rw-r--r-- | src/include/gnunet_crypto_lib.h | 10 | ||||
-rw-r--r-- | src/util/crypto_ecc.c | 45 | ||||
-rw-r--r-- | src/util/gnunet-ecc.c | 22 |
3 files changed, 68 insertions, 9 deletions
diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h index 5dc5b60d7..6ac2a67fa 100644 --- a/src/include/gnunet_crypto_lib.h +++ b/src/include/gnunet_crypto_lib.h | |||
@@ -1110,6 +1110,16 @@ GNUNET_CRYPTO_ecdsa_public_key_to_string (const struct GNUNET_CRYPTO_EcdsaPublic | |||
1110 | 1110 | ||
1111 | 1111 | ||
1112 | /** | 1112 | /** |
1113 | * Convert a private key to a string. | ||
1114 | * | ||
1115 | * @param priv key to convert | ||
1116 | * @return string representing @a pub | ||
1117 | */ | ||
1118 | char * | ||
1119 | GNUNET_CRYPTO_eddsa_private_key_to_string (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv); | ||
1120 | |||
1121 | |||
1122 | /** | ||
1113 | * Convert a public key to a string. | 1123 | * Convert a public key to a string. |
1114 | * | 1124 | * |
1115 | * @param pub key to convert | 1125 | * @param pub key to convert |
diff --git a/src/util/crypto_ecc.c b/src/util/crypto_ecc.c index eaa49a991..7845932ee 100644 --- a/src/util/crypto_ecc.c +++ b/src/util/crypto_ecc.c | |||
@@ -354,6 +354,37 @@ GNUNET_CRYPTO_eddsa_public_key_to_string (const struct GNUNET_CRYPTO_EddsaPublic | |||
354 | 354 | ||
355 | 355 | ||
356 | /** | 356 | /** |
357 | * Convert a private key to a string. | ||
358 | * | ||
359 | * @param priv key to convert | ||
360 | * @return string representing @a pub | ||
361 | */ | ||
362 | char * | ||
363 | GNUNET_CRYPTO_eddsa_private_key_to_string (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv) | ||
364 | { | ||
365 | char *privkeybuf; | ||
366 | size_t keylen = (sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)) * 8; | ||
367 | char *end; | ||
368 | |||
369 | if (keylen % 5 > 0) | ||
370 | keylen += 5 - keylen % 5; | ||
371 | keylen /= 5; | ||
372 | privkeybuf = GNUNET_malloc (keylen + 1); | ||
373 | end = GNUNET_STRINGS_data_to_string ((unsigned char *) priv, | ||
374 | sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey), | ||
375 | privkeybuf, | ||
376 | keylen); | ||
377 | if (NULL == end) | ||
378 | { | ||
379 | GNUNET_free (privkeybuf); | ||
380 | return NULL; | ||
381 | } | ||
382 | *end = '\0'; | ||
383 | return privkeybuf; | ||
384 | } | ||
385 | |||
386 | |||
387 | /** | ||
357 | * Convert a string representing a public key to a public key. | 388 | * Convert a string representing a public key to a public key. |
358 | * | 389 | * |
359 | * @param enc encoded public key | 390 | * @param enc encoded public key |
@@ -374,9 +405,10 @@ GNUNET_CRYPTO_ecdsa_public_key_from_string (const char *enc, | |||
374 | if (enclen != keylen) | 405 | if (enclen != keylen) |
375 | return GNUNET_SYSERR; | 406 | return GNUNET_SYSERR; |
376 | 407 | ||
377 | if (GNUNET_OK != GNUNET_STRINGS_string_to_data (enc, enclen, | 408 | if (GNUNET_OK != |
378 | pub, | 409 | GNUNET_STRINGS_string_to_data (enc, enclen, |
379 | sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))) | 410 | pub, |
411 | sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))) | ||
380 | return GNUNET_SYSERR; | 412 | return GNUNET_SYSERR; |
381 | return GNUNET_OK; | 413 | return GNUNET_OK; |
382 | } | 414 | } |
@@ -403,9 +435,10 @@ GNUNET_CRYPTO_eddsa_public_key_from_string (const char *enc, | |||
403 | if (enclen != keylen) | 435 | if (enclen != keylen) |
404 | return GNUNET_SYSERR; | 436 | return GNUNET_SYSERR; |
405 | 437 | ||
406 | if (GNUNET_OK != GNUNET_STRINGS_string_to_data (enc, enclen, | 438 | if (GNUNET_OK != |
407 | pub, | 439 | GNUNET_STRINGS_string_to_data (enc, enclen, |
408 | sizeof (struct GNUNET_CRYPTO_EddsaPublicKey))) | 440 | pub, |
441 | sizeof (struct GNUNET_CRYPTO_EddsaPublicKey))) | ||
409 | return GNUNET_SYSERR; | 442 | return GNUNET_SYSERR; |
410 | return GNUNET_OK; | 443 | return GNUNET_OK; |
411 | } | 444 | } |
diff --git a/src/util/gnunet-ecc.c b/src/util/gnunet-ecc.c index 42ecc2101..66a4bd3e9 100644 --- a/src/util/gnunet-ecc.c +++ b/src/util/gnunet-ecc.c | |||
@@ -49,6 +49,11 @@ static unsigned int list_keys_count; | |||
49 | static int print_public_key; | 49 | static int print_public_key; |
50 | 50 | ||
51 | /** | 51 | /** |
52 | * Flag for printing private key. | ||
53 | */ | ||
54 | static int print_private_key; | ||
55 | |||
56 | /** | ||
52 | * Flag for printing public key in hex. | 57 | * Flag for printing public key in hex. |
53 | */ | 58 | */ |
54 | static int print_public_key_hex; | 59 | static int print_public_key_hex; |
@@ -377,7 +382,7 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
377 | create_keys (args[0], args[1]); | 382 | create_keys (args[0], args[1]); |
378 | return; | 383 | return; |
379 | } | 384 | } |
380 | if (print_public_key || print_public_key_hex) | 385 | if (print_public_key || print_public_key_hex || print_private_key) |
381 | { | 386 | { |
382 | char *str; | 387 | char *str; |
383 | struct GNUNET_DISK_FileHandle *keyfile; | 388 | struct GNUNET_DISK_FileHandle *keyfile; |
@@ -388,19 +393,26 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
388 | GNUNET_DISK_PERM_NONE); | 393 | GNUNET_DISK_PERM_NONE); |
389 | if (NULL == keyfile) | 394 | if (NULL == keyfile) |
390 | return; | 395 | return; |
391 | while (sizeof (pk) == GNUNET_DISK_file_read (keyfile, &pk, sizeof (pk))) | 396 | while (sizeof (pk) == |
397 | GNUNET_DISK_file_read (keyfile, &pk, sizeof (pk))) | ||
392 | { | 398 | { |
393 | GNUNET_CRYPTO_eddsa_key_get_public (&pk, &pub); | 399 | GNUNET_CRYPTO_eddsa_key_get_public (&pk, &pub); |
394 | if (print_public_key_hex) | 400 | if (print_public_key_hex) |
395 | { | 401 | { |
396 | print_hex ("HEX:", &pub, sizeof (pub)); | 402 | print_hex ("HEX:", &pub, sizeof (pub)); |
397 | } | 403 | } |
398 | else | 404 | else if (print_public_key) |
399 | { | 405 | { |
400 | str = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub); | 406 | str = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub); |
401 | FPRINTF (stdout, "%s\n", str); | 407 | FPRINTF (stdout, "%s\n", str); |
402 | GNUNET_free (str); | 408 | GNUNET_free (str); |
403 | } | 409 | } |
410 | else if (print_private_key) | ||
411 | { | ||
412 | str = GNUNET_CRYPTO_eddsa_private_key_to_string (&pk); | ||
413 | FPRINTF (stdout, "%s\n", str); | ||
414 | GNUNET_free (str); | ||
415 | } | ||
404 | } | 416 | } |
405 | GNUNET_DISK_file_close (keyfile); | 417 | GNUNET_DISK_file_close (keyfile); |
406 | } | 418 | } |
@@ -438,6 +450,10 @@ main (int argc, | |||
438 | "print-public-key", | 450 | "print-public-key", |
439 | gettext_noop ("print the public key in ASCII format"), | 451 | gettext_noop ("print the public key in ASCII format"), |
440 | &print_public_key), | 452 | &print_public_key), |
453 | GNUNET_GETOPT_option_flag ('P', | ||
454 | "print-private-key", | ||
455 | gettext_noop ("print the private key in ASCII format"), | ||
456 | &print_private_key), | ||
441 | GNUNET_GETOPT_option_flag ('x', | 457 | GNUNET_GETOPT_option_flag ('x', |
442 | "print-hex", | 458 | "print-hex", |
443 | gettext_noop ("print the public key in HEX format"), | 459 | gettext_noop ("print the public key in HEX format"), |