From 7ab6f8a970da90f53ebd8dfd5a3792c4978f78a7 Mon Sep 17 00:00:00 2001 From: Brian Gix Date: Mon, 7 May 2018 15:35:24 -0700 Subject: [PATCH] shared/ecc: Make ecc_make_key() a stand alone API And correct minor flaws in API descriptions Change-Id: I0fd146cbc0c4bc0637a2c0dc894c6fed1209d30a Signed-off-by: Amit Purwar --- src/shared/ecc.c | 23 ++++++++++++++++------- src/shared/ecc.h | 4 +++- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/shared/ecc.c b/src/shared/ecc.c index 15f6b8a..8149f1d 100755 --- a/src/shared/ecc.c +++ b/src/shared/ecc.c @@ -883,20 +883,29 @@ bool ecc_make_public_key(const uint8_t private_key[32], uint8_t public_key[64]) bool ecc_make_key(uint8_t public_key[64], uint8_t private_key[32]) { + struct ecc_point pk; uint64_t priv[NUM_ECC_DIGITS]; unsigned int tries = 0; - bool result = false; - for (tries = 0; !result && tries < MAX_TRIES; tries++) { - if (!get_random_number(priv)) + do { + if (!get_random_number(priv) || (tries++ >= MAX_TRIES)) + return false; + + if (vli_is_zero(priv)) continue; - ecc_native2bytes(priv, private_key); + /* Make sure the private key is in the range [1, n-1]. */ + if (vli_cmp(curve_n, priv) != 1) + continue; - result = ecc_make_public_key(private_key, public_key); - } + ecc_point_mult(&pk, &curve_g, priv, NULL, vli_num_bits(priv)); + } while (ecc_point_is_zero(&pk)); - return result; + ecc_native2bytes(priv, private_key); + ecc_native2bytes(pk.x, public_key); + ecc_native2bytes(pk.y, &public_key[32]); + + return true; } bool ecc_valid_public_key(const uint8_t public_key[64]) diff --git a/src/shared/ecc.h b/src/shared/ecc.h index a88e735..8c15e4e 100755 --- a/src/shared/ecc.h +++ b/src/shared/ecc.h @@ -29,8 +29,10 @@ /* Create a public key from a private key. * + * Inputs: + * private_key - Your private key. + * * Outputs: - * private_key - Const private key * public_key - Will be filled in with the public key. * * Returns true if the public key was generated successfully, false -- 2.7.4