From: Christian Grothoff Date: Wed, 1 Nov 2017 09:33:57 +0000 (+0100) Subject: add -P option to gnunet-ecc X-Git-Tag: gnunet-0.11.0rc0~99^2~10 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=d1a84861ce988b9cdc87e19ff140400833e616fd;p=oweals%2Fgnunet.git add -P option to gnunet-ecc --- 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 @@ -1109,6 +1109,16 @@ char * GNUNET_CRYPTO_ecdsa_public_key_to_string (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub); +/** + * Convert a private key to a string. + * + * @param priv key to convert + * @return string representing @a pub + */ +char * +GNUNET_CRYPTO_eddsa_private_key_to_string (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv); + + /** * Convert a public key to a string. * 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 @@ -353,6 +353,37 @@ GNUNET_CRYPTO_eddsa_public_key_to_string (const struct GNUNET_CRYPTO_EddsaPublic } +/** + * Convert a private key to a string. + * + * @param priv key to convert + * @return string representing @a pub + */ +char * +GNUNET_CRYPTO_eddsa_private_key_to_string (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv) +{ + char *privkeybuf; + size_t keylen = (sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)) * 8; + char *end; + + if (keylen % 5 > 0) + keylen += 5 - keylen % 5; + keylen /= 5; + privkeybuf = GNUNET_malloc (keylen + 1); + end = GNUNET_STRINGS_data_to_string ((unsigned char *) priv, + sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey), + privkeybuf, + keylen); + if (NULL == end) + { + GNUNET_free (privkeybuf); + return NULL; + } + *end = '\0'; + return privkeybuf; +} + + /** * Convert a string representing a public key to a public key. * @@ -374,9 +405,10 @@ GNUNET_CRYPTO_ecdsa_public_key_from_string (const char *enc, if (enclen != keylen) return GNUNET_SYSERR; - if (GNUNET_OK != GNUNET_STRINGS_string_to_data (enc, enclen, - pub, - sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))) + if (GNUNET_OK != + GNUNET_STRINGS_string_to_data (enc, enclen, + pub, + sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))) return GNUNET_SYSERR; return GNUNET_OK; } @@ -403,9 +435,10 @@ GNUNET_CRYPTO_eddsa_public_key_from_string (const char *enc, if (enclen != keylen) return GNUNET_SYSERR; - if (GNUNET_OK != GNUNET_STRINGS_string_to_data (enc, enclen, - pub, - sizeof (struct GNUNET_CRYPTO_EddsaPublicKey))) + if (GNUNET_OK != + GNUNET_STRINGS_string_to_data (enc, enclen, + pub, + sizeof (struct GNUNET_CRYPTO_EddsaPublicKey))) return GNUNET_SYSERR; return GNUNET_OK; } 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 @@ -48,6 +48,11 @@ static unsigned int list_keys_count; */ static int print_public_key; +/** + * Flag for printing private key. + */ +static int print_private_key; + /** * Flag for printing public key in hex. */ @@ -377,7 +382,7 @@ run (void *cls, char *const *args, const char *cfgfile, create_keys (args[0], args[1]); return; } - if (print_public_key || print_public_key_hex) + if (print_public_key || print_public_key_hex || print_private_key) { char *str; struct GNUNET_DISK_FileHandle *keyfile; @@ -388,19 +393,26 @@ run (void *cls, char *const *args, const char *cfgfile, GNUNET_DISK_PERM_NONE); if (NULL == keyfile) return; - while (sizeof (pk) == GNUNET_DISK_file_read (keyfile, &pk, sizeof (pk))) + while (sizeof (pk) == + GNUNET_DISK_file_read (keyfile, &pk, sizeof (pk))) { GNUNET_CRYPTO_eddsa_key_get_public (&pk, &pub); if (print_public_key_hex) { print_hex ("HEX:", &pub, sizeof (pub)); } - else + else if (print_public_key) { str = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub); FPRINTF (stdout, "%s\n", str); GNUNET_free (str); } + else if (print_private_key) + { + str = GNUNET_CRYPTO_eddsa_private_key_to_string (&pk); + FPRINTF (stdout, "%s\n", str); + GNUNET_free (str); + } } GNUNET_DISK_file_close (keyfile); } @@ -438,6 +450,10 @@ main (int argc, "print-public-key", gettext_noop ("print the public key in ASCII format"), &print_public_key), + GNUNET_GETOPT_option_flag ('P', + "print-private-key", + gettext_noop ("print the private key in ASCII format"), + &print_private_key), GNUNET_GETOPT_option_flag ('x', "print-hex", gettext_noop ("print the public key in HEX format"),