From 26780d92f3a5da7784949eae3bd59d763053dc89 Mon Sep 17 00:00:00 2001 From: Jouni Malinen Date: Thu, 26 May 2022 14:23:46 +0300 Subject: [PATCH] crypto: Convert crypto_ec_key_get_private_key() to return new bignum This makes it easier for the crypto library/wrapper to avoid direct references to internal EC key parameters. Signed-off-by: Jouni Malinen --- src/common/dpp_crypto.c | 22 ++++++++++++++-------- src/crypto/crypto.h | 4 +++- src/crypto/crypto_openssl.c | 11 ++++++++--- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/common/dpp_crypto.c b/src/common/dpp_crypto.c index 8d3f3abfa..2696dd226 100644 --- a/src/common/dpp_crypto.c +++ b/src/common/dpp_crypto.c @@ -1037,8 +1037,8 @@ int dpp_auth_derive_l_responder(struct dpp_authentication *auth) struct crypto_ec *ec; struct crypto_ec_point *L = NULL; const struct crypto_ec_point *BI; - const struct crypto_bignum *bR, *pR, *q; - struct crypto_bignum *sum = NULL, *lx = NULL; + const struct crypto_bignum *q; + struct crypto_bignum *sum = NULL, *lx = NULL, *bR = NULL, *pR = NULL; int ret = -1; /* L = ((bR + pR) modulo q) * BI */ @@ -1068,6 +1068,8 @@ int dpp_auth_derive_l_responder(struct dpp_authentication *auth) fail: crypto_bignum_deinit(lx, 1); crypto_bignum_deinit(sum, 1); + crypto_bignum_deinit(bR, 1); + crypto_bignum_deinit(pR, 1); crypto_ec_point_deinit(L, 1); crypto_ec_deinit(ec); return ret; @@ -1079,8 +1081,7 @@ int dpp_auth_derive_l_initiator(struct dpp_authentication *auth) struct crypto_ec *ec; struct crypto_ec_point *L = NULL, *sum = NULL; const struct crypto_ec_point *BR, *PR; - const struct crypto_bignum *bI; - struct crypto_bignum *lx = NULL; + struct crypto_bignum *lx = NULL, *bI = NULL; int ret = -1; /* L = bI * (BR + PR) */ @@ -1108,6 +1109,7 @@ int dpp_auth_derive_l_initiator(struct dpp_authentication *auth) ret = 0; fail: crypto_bignum_deinit(lx, 1); + crypto_bignum_deinit(bI, 1); crypto_ec_point_deinit(sum, 1); crypto_ec_point_deinit(L, 1); crypto_ec_deinit(ec); @@ -1661,8 +1663,8 @@ int dpp_reconfig_derive_ke_responder(struct dpp_authentication *auth, struct json_token *peer_net_access_key) { struct crypto_ec_key *own_key = NULL, *peer_key = NULL; - struct crypto_bignum *sum = NULL; - const struct crypto_bignum *q, *cR, *pR; + struct crypto_bignum *sum = NULL, *cR = NULL, *pR = NULL; + const struct crypto_bignum *q; struct crypto_ec *ec = NULL; struct crypto_ec_point *M = NULL; const struct crypto_ec_point *CI; @@ -1749,6 +1751,8 @@ fail: forced_memzero(Mx, sizeof(Mx)); crypto_ec_point_deinit(M, 1); crypto_bignum_deinit(sum, 1); + crypto_bignum_deinit(cR, 1); + crypto_bignum_deinit(pR, 1); crypto_ec_key_deinit(own_key); crypto_ec_key_deinit(peer_key); crypto_ec_deinit(ec); @@ -1762,7 +1766,7 @@ int dpp_reconfig_derive_ke_initiator(struct dpp_authentication *auth, { struct crypto_ec_key *pr = NULL, *peer_key = NULL; const struct crypto_ec_point *CR, *PR; - const struct crypto_bignum *cI; + struct crypto_bignum *cI = NULL; struct crypto_ec *ec = NULL; struct crypto_ec_point *sum = NULL, *M = NULL; u8 Mx[DPP_MAX_SHARED_SECRET_LEN]; @@ -1835,6 +1839,7 @@ int dpp_reconfig_derive_ke_initiator(struct dpp_authentication *auth, fail: forced_memzero(prk, sizeof(prk)); forced_memzero(Mx, sizeof(Mx)); + crypto_bignum_deinit(cI, 1); crypto_ec_key_deinit(pr); crypto_ec_key_deinit(peer_key); crypto_ec_point_deinit(sum, 1); @@ -2321,7 +2326,7 @@ struct crypto_ec_point * dpp_decrypt_e_id(struct crypto_ec_key *ppkey, struct crypto_ec_key *e_prime_id) { struct crypto_ec *ec; - const struct crypto_bignum *pp; + struct crypto_bignum *pp = NULL; struct crypto_ec_point *e_id = NULL; const struct crypto_ec_point *a_nonce_point, *e_prime_id_point; @@ -2348,6 +2353,7 @@ struct crypto_ec_point * dpp_decrypt_e_id(struct crypto_ec_key *ppkey, crypto_ec_point_debug_print(ec, e_id, "DPP: Decrypted E-id"); fail: + crypto_bignum_deinit(pp, 1); crypto_ec_deinit(ec); return e_id; } diff --git a/src/crypto/crypto.h b/src/crypto/crypto.h index e4f3eb3e6..730ad0d58 100644 --- a/src/crypto/crypto.h +++ b/src/crypto/crypto.h @@ -1094,8 +1094,10 @@ crypto_ec_key_get_public_key(struct crypto_ec_key *key); * crypto_ec_key_get_private_key - Get EC private key as a bignum * @key: EC key from crypto_ec_key_parse/set_pub() or crypto_ec_key_parse_priv() * Returns: Private key as a bignum or %NULL on failure + * + * The caller needs to free the returned value with crypto_bignum_deinit(). */ -const struct crypto_bignum * +struct crypto_bignum * crypto_ec_key_get_private_key(struct crypto_ec_key *key); /** diff --git a/src/crypto/crypto_openssl.c b/src/crypto/crypto_openssl.c index 212e5adad..3a6b87682 100644 --- a/src/crypto/crypto_openssl.c +++ b/src/crypto/crypto_openssl.c @@ -3425,15 +3425,20 @@ crypto_ec_key_get_public_key(struct crypto_ec_key *key) } -const struct crypto_bignum * +struct crypto_bignum * crypto_ec_key_get_private_key(struct crypto_ec_key *key) { + EVP_PKEY *pkey = (EVP_PKEY *) key; const EC_KEY *eckey; + const BIGNUM *bn; - eckey = EVP_PKEY_get0_EC_KEY((EVP_PKEY *) key); + eckey = EVP_PKEY_get0_EC_KEY(pkey); if (!eckey) return NULL; - return (const struct crypto_bignum *) EC_KEY_get0_private_key(eckey); + bn = EC_KEY_get0_private_key(eckey); + if (!bn) + return NULL; + return (struct crypto_bignum *) BN_dup(bn); }