From 24cd46f1f117a1d387292ba2e65c749fb7a13e54 Mon Sep 17 00:00:00 2001 From: Sean Parkinson Date: Fri, 17 Feb 2017 11:05:29 -0800 Subject: [PATCH] Fixes from code review --- wolfcrypt/src/coding.c | 3 ++- wolfcrypt/src/ecc.c | 7 +++---- wolfcrypt/src/tfm.c | 6 ++++++ wolfcrypt/test/test.c | 13 ++++++------- wolfssl/wolfcrypt/coding.h | 3 ++- wolfssl/wolfcrypt/tfm.h | 1 + 6 files changed, 20 insertions(+), 13 deletions(-) diff --git a/wolfcrypt/src/coding.c b/wolfcrypt/src/coding.c index f144b1130..43cb45d06 100644 --- a/wolfcrypt/src/coding.c +++ b/wolfcrypt/src/coding.c @@ -347,7 +347,8 @@ int Base64_Encode_NoNl(const byte* in, word32 inLen, byte* out, word32* outLen) #endif /* defined(WOLFSSL_BASE64_ENCODE) */ -#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || defined(HAVE_FIPS) +#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || defined(HAVE_FIPS) \ + || defined(HAVE_ECC_CDH) static const byte hexDecode[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index 8d0e697be..51d310a7a 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -2591,10 +2591,8 @@ static int wc_ecc_shared_secret_gen_sync(ecc_key* private_key, ecc_point* point, k = &k_lcl; if (mp_init(k) != MP_OKAY) return MEMORY_E; - /* multiple cofactor times private key "k" */ - err = mp_set_int(k, cofactor); - if (err == MP_OKAY) - err = mp_mul(k, &private_key->k, k); + /* multiply cofactor times private key "k" */ + err = mp_mul_d(&private_key->k, cofactor, k); if (err != MP_OKAY) { mp_clear(k); return err; @@ -2606,6 +2604,7 @@ static int wc_ecc_shared_secret_gen_sync(ecc_key* private_key, ecc_point* point, /* make new point */ result = wc_ecc_new_point_h(private_key->heap); if (result == NULL) { + mp_clear(k); return MEMORY_E; } diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index cfb647fb1..0656f760c 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -2254,6 +2254,12 @@ int mp_mul (mp_int * a, mp_int * b, mp_int * c) return MP_OKAY; } +int mp_mul_d (mp_int * a, mp_digit b, mp_int * c) +{ + fp_mul_d(a, b, c); + return MP_OKAY; +} + /* d = a * b (mod c) */ int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d) { diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index d109d549a..216b317b2 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -8204,8 +8204,7 @@ static int ecc_test_cdh_vectors(void) int ret; ecc_key pub_key, priv_key; byte sharedA[32] = {0}, sharedB[32] = {0}; - word32 x; - mp_int z; + word32 x, z; const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287"; const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac"; @@ -8234,13 +8233,13 @@ static int ecc_test_cdh_vectors(void) } /* read in expected Z */ - mp_init(&z); - mp_read_radix(&z, ZIUT, 16); - mp_to_unsigned_bin(&z, sharedB); - mp_clear(&z); + z = sizeof(sharedB); + ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z); + if (ret != 0) + goto done; /* compare results */ - if (XMEMCMP(sharedA, sharedB, x)) { + if (x != z || XMEMCMP(sharedA, sharedB, x)) { ERROR_OUT(-1007, done); } diff --git a/wolfssl/wolfcrypt/coding.h b/wolfssl/wolfcrypt/coding.h index 5395cc2f4..427f3a6cb 100644 --- a/wolfssl/wolfcrypt/coding.h +++ b/wolfssl/wolfcrypt/coding.h @@ -61,7 +61,8 @@ WOLFSSL_API int Base64_Decode(const byte* in, word32 inLen, byte* out, word32* outLen); #endif -#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || defined(HAVE_FIPS) +#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || defined(HAVE_FIPS) \ + || defined(HAVE_ECC_CDH) WOLFSSL_API int Base16_Decode(const byte* in, word32 inLen, byte* out, word32* outLen); WOLFSSL_API diff --git a/wolfssl/wolfcrypt/tfm.h b/wolfssl/wolfcrypt/tfm.h index fc88149d7..f5df3bf12 100644 --- a/wolfssl/wolfcrypt/tfm.h +++ b/wolfssl/wolfcrypt/tfm.h @@ -622,6 +622,7 @@ int mp_sub (mp_int * a, mp_int * b, mp_int * c); int mp_add_d (mp_int * a, mp_digit b, mp_int * c); int mp_mul (mp_int * a, mp_int * b, mp_int * c); +int mp_mul_d (mp_int * a, mp_digit b, mp_int * c); int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d); int mp_submod (mp_int* a, mp_int* b, mp_int* c, mp_int* d); int mp_addmod (mp_int* a, mp_int* b, mp_int* c, mp_int* d);