From f147b016742d8e989427270dcbbf9e66d51a538f Mon Sep 17 00:00:00 2001 From: David Garske Date: Fri, 21 Jul 2017 10:50:12 -0700 Subject: [PATCH] =?UTF-8?q?Fixes=20for=20using=20`WOLFSSL=5FCUSTOM=5FCURVE?= =?UTF-8?q?S`=20and=20`wc=5Fecc=5Fset=5Fcustom=5Fcurve`.=20Fixes=20resolve?= =?UTF-8?q?s=20issue=20with=20`->dp`=20and=20`->idx`=20getting=20reset=20w?= =?UTF-8?q?hich=20caused=20curve=20parameters=20to=20not=20be=20set=20corr?= =?UTF-8?q?ectly.=20Proper=20sequence=20for=20using=20custom=20curves=20is?= =?UTF-8?q?=20=E2=80=98wc=5Fecc=5Finit`,=20`wc=5Fecc=5Fset=5Fcustom=5Fcurv?= =?UTF-8?q?e`=20then=20`wc=5Fecc=5Fmake=5Fkey=5Fex(=E2=80=A6,=20ECC=5FCUST?= =?UTF-8?q?OM=5FIDX)=E2=80=99=20or=20`wc=5Fecc=5Fimport=5Fx963=5Fex(?= =?UTF-8?q?=E2=80=A6,=20ECC=5FCUSTOM=5FIDX)`.=20Test=20case=20and=20exampl?= =?UTF-8?q?e=20to=20follow=20shortly.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wolfcrypt/src/ecc.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index 3fc2f5038..bd3d117fa 100755 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -1245,6 +1245,10 @@ int wc_ecc_set_curve(ecc_key* key, int keysize, int curve_id) if (key->idx != ECC_CUSTOM_IDX) { int x; + /* default values */ + key->idx = 0; + key->dp = NULL; + /* find ecc_set based on curve_id or key size */ for (x = 0; ecc_sets[x].size != 0; x++) { if (curve_id > ECC_CURVE_DEF) { @@ -2969,6 +2973,12 @@ static int wc_ecc_gen_k(WC_RNG* rng, int size, mp_int* k, mp_int* order) } #endif /* !WOLFSSL_ATECC508A */ +static INLINE void wc_ecc_reset(ecc_key* key) +{ + /* make sure required key variables are reset */ + key->state = ECC_STATE_NONE; +} + int wc_ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key, int curve_id) { int err; @@ -2981,10 +2991,8 @@ int wc_ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key, int curve_id) return BAD_FUNC_ARG; } - /* make sure required key variables are reset */ - key->state = ECC_STATE_NONE; - key->idx = 0; - key->dp = NULL; + /* make sure required variables are reset */ + wc_ecc_reset(key); err = wc_ecc_set_curve(key, keysize, curve_id); if (err != 0) { @@ -4777,7 +4785,6 @@ int wc_ecc_import_x963_ex(const byte* in, word32 inLen, ecc_key* key, #ifndef WOLFSSL_ATECC508A int compressed = 0; #endif /* !WOLFSSL_ATECC508A */ - void* heap; if (in == NULL || key == NULL) return BAD_FUNC_ARG; @@ -4787,9 +4794,8 @@ int wc_ecc_import_x963_ex(const byte* in, word32 inLen, ecc_key* key, return ECC_BAD_ARG_E; } - heap = key->heap; /* save heap */ - XMEMSET(key, 0, sizeof(ecc_key)); - key->heap = heap; /* restore heap */ + /* make sure required variables are reset */ + wc_ecc_reset(key); #ifdef WOLFSSL_ATECC508A /* TODO: Implement equiv call to ATECC508A */ @@ -5086,18 +5092,14 @@ int wc_ecc_import_private_key_ex(const byte* priv, word32 privSz, /* public optional, NULL if only importing private */ if (pub != NULL) { - ret = wc_ecc_import_x963_ex(pub, pubSz, key, curve_id); - - } else { - + } + else { if (key == NULL || priv == NULL) return BAD_FUNC_ARG; - /* make sure required key variables are reset */ - key->state = ECC_STATE_NONE; - key->idx = 0; - key->dp = NULL; + /* make sure required variables are reset */ + wc_ecc_reset(key); /* set key size */ ret = wc_ecc_set_curve(key, privSz, curve_id); @@ -5236,16 +5238,14 @@ static int wc_ecc_import_raw_private(ecc_key* key, const char* qx, const char* qy, const char* d, int curve_id) { int err = MP_OKAY; - void* heap; /* if d is NULL, only import as public key using Qx,Qy */ if (key == NULL || qx == NULL || qy == NULL) { return BAD_FUNC_ARG; } - heap = key->heap; /* save heap */ - XMEMSET(key, 0, sizeof(ecc_key)); - key->heap = heap; /* restore heap */ + /* make sure required variables are reset */ + wc_ecc_reset(key); /* set curve type and index */ err = wc_ecc_set_curve(key, 0, curve_id);