From 7a7ec54a324da4820202582126795a8ac6bbe94d Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 15 Feb 2017 16:55:25 +0100 Subject: [PATCH] introducing GNUNET_CRYPTO_ecdhe_create2() to avoid malloc nonsense --- src/include/gnunet_crypto_lib.h | 11 +++++++++++ src/util/crypto_ecc.c | 35 ++++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h index 43fd32a58..07cade0e3 100644 --- a/src/include/gnunet_crypto_lib.h +++ b/src/include/gnunet_crypto_lib.h @@ -1238,6 +1238,17 @@ struct GNUNET_CRYPTO_EddsaPrivateKey * GNUNET_CRYPTO_eddsa_key_create (void); +/** + * @ingroup crypto + * Create a new private key. Clear with #GNUNET_CRYPTO_ecdhe_key_clear(). + * + * @param[out] pk set to fresh private key; + * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure + */ +int +GNUNET_CRYPTO_ecdhe_key_create2 (struct GNUNET_CRYPTO_EcdhePrivateKey *pk); + + /** * @ingroup crypto * Create a new private key. Caller must free return value. diff --git a/src/util/crypto_ecc.c b/src/util/crypto_ecc.c index 4bba395b3..3f9150762 100644 --- a/src/util/crypto_ecc.c +++ b/src/util/crypto_ecc.c @@ -488,6 +488,28 @@ struct GNUNET_CRYPTO_EcdhePrivateKey * GNUNET_CRYPTO_ecdhe_key_create () { struct GNUNET_CRYPTO_EcdhePrivateKey *priv; + + priv = GNUNET_new (struct GNUNET_CRYPTO_EcdhePrivateKey); + if (GNUNET_OK != + GNUNET_CRYPTO_ecdhe_key_create2 (priv)) + { + GNUNET_free (priv); + return NULL; + } + return priv; +} + + +/** + * @ingroup crypto + * Create a new private key. Clear with #GNUNET_CRYPTO_ecdhe_key_clear(). + * + * @param[out] pk set to fresh private key; + * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure + */ +int +GNUNET_CRYPTO_ecdhe_key_create2 (struct GNUNET_CRYPTO_EcdhePrivateKey *pk) +{ gcry_sexp_t priv_sexp; gcry_sexp_t s_keyparam; gcry_mpi_t d; @@ -503,13 +525,13 @@ GNUNET_CRYPTO_ecdhe_key_create () "(flags eddsa no-keytest)))"))) { LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); - return NULL; + return GNUNET_SYSERR; } if (0 != (rc = gcry_pk_genkey (&priv_sexp, s_keyparam))) { LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_genkey", rc); gcry_sexp_release (s_keyparam); - return NULL; + return GNUNET_SYSERR; } gcry_sexp_release (s_keyparam); #if EXTRA_CHECKS @@ -517,20 +539,19 @@ GNUNET_CRYPTO_ecdhe_key_create () { LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc); gcry_sexp_release (priv_sexp); - return NULL; + return GNUNET_SYSERR; } #endif if (0 != (rc = key_from_sexp (&d, priv_sexp, "private-key", "d"))) { LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "key_from_sexp", rc); gcry_sexp_release (priv_sexp); - return NULL; + return GNUNET_SYSERR; } gcry_sexp_release (priv_sexp); - priv = GNUNET_new (struct GNUNET_CRYPTO_EcdhePrivateKey); - GNUNET_CRYPTO_mpi_print_unsigned (priv->d, sizeof (priv->d), d); + GNUNET_CRYPTO_mpi_print_unsigned (pk->d, sizeof (pk->d), d); gcry_mpi_release (d); - return priv; + return GNUNET_OK; } -- 2.25.1