From 393ca1b30cf889a7ab318e7e4a99d0fdb4d390c2 Mon Sep 17 00:00:00 2001 From: Tesfa Mael Date: Mon, 15 Apr 2019 17:54:23 -0700 Subject: [PATCH] Increased test suite ciphers buffer size (ticket #5000)) Enhancement to support ECC domain param HEX string or unsigned bin comparison (ticket #5035) --- testsuite/testsuite.c | 2 +- wolfcrypt/src/ecc.c | 68 +++++++++++++++++++++++++++++++++-------- wolfssl/wolfcrypt/ecc.h | 2 ++ 3 files changed, 59 insertions(+), 13 deletions(-) diff --git a/testsuite/testsuite.c b/testsuite/testsuite.c index e993297cf..ddfd8dd48 100644 --- a/testsuite/testsuite.c +++ b/testsuite/testsuite.c @@ -180,7 +180,7 @@ int testsuite_test(int argc, char** argv) /* show ciphers */ { - char ciphers[1024]; + char ciphers[1024*2]; XMEMSET(ciphers, 0, sizeof(ciphers)); wolfSSL_get_ciphers(ciphers, sizeof(ciphers)-1); printf("ciphers = %s\n", ciphers); diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index def11e8a3..623f78ba1 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -3173,11 +3173,11 @@ int wc_ecc_get_curve_id_from_name(const char* curveName) } /* Compares a curve parameter (hex, from ecc_sets[]) to given input - * parameter (byte array) for equality. - * + * parameter for equality. + * encType is WC_TYPE_UNSIGNED_BIN or WC_TYPE_HEX_STR * Returns MP_EQ on success, negative on error */ static int wc_ecc_cmp_param(const char* curveParam, - const byte* param, word32 paramSz) + const byte* param, word32 paramSz, int encType) { int err = MP_OKAY; #ifdef WOLFSSL_SMALL_STACK @@ -3209,9 +3209,12 @@ static int wc_ecc_cmp_param(const char* curveParam, return err; } - if (err == MP_OKAY) - err = mp_read_unsigned_bin(a, param, paramSz); - + if (err == MP_OKAY) { + if (encType == WC_TYPE_HEX_STR) + err = mp_read_radix(a, (char*) param, MP_RADIX_HEX); + else + err = mp_read_unsigned_bin(a, param, paramSz); + } if (err == MP_OKAY) err = mp_read_radix(b, curveParam, MP_RADIX_HEX); @@ -3270,13 +3273,17 @@ int wc_ecc_get_curve_id_from_params(int fieldSize, for (idx = 0; ecc_sets[idx].size != 0; idx++) { if (curveSz == ecc_sets[idx].size) { if ((wc_ecc_cmp_param(ecc_sets[idx].prime, prime, - primeSz) == MP_EQ) && - (wc_ecc_cmp_param(ecc_sets[idx].Af, Af, AfSz) == MP_EQ) && - (wc_ecc_cmp_param(ecc_sets[idx].Bf, Bf, BfSz) == MP_EQ) && + primeSz, WC_TYPE_UNSIGNED_BIN) == MP_EQ) && + (wc_ecc_cmp_param(ecc_sets[idx].Af, Af, AfSz, + WC_TYPE_UNSIGNED_BIN) == MP_EQ) && + (wc_ecc_cmp_param(ecc_sets[idx].Bf, Bf, BfSz, + WC_TYPE_UNSIGNED_BIN) == MP_EQ) && (wc_ecc_cmp_param(ecc_sets[idx].order, order, - orderSz) == MP_EQ) && - (wc_ecc_cmp_param(ecc_sets[idx].Gx, Gx, GxSz) == MP_EQ) && - (wc_ecc_cmp_param(ecc_sets[idx].Gy, Gy, GySz) == MP_EQ) && + orderSz, WC_TYPE_UNSIGNED_BIN) == MP_EQ) && + (wc_ecc_cmp_param(ecc_sets[idx].Gx, Gx, GxSz, + WC_TYPE_UNSIGNED_BIN) == MP_EQ) && + (wc_ecc_cmp_param(ecc_sets[idx].Gy, Gy, GySz, + WC_TYPE_UNSIGNED_BIN) == MP_EQ) && (cofactor == ecc_sets[idx].cofactor)) { break; } @@ -3289,6 +3296,43 @@ int wc_ecc_get_curve_id_from_params(int fieldSize, return ecc_sets[idx].id; } +int wc_ecc_get_curve_id_from_dp_params(const ecc_set_type* dp) +{ + int idx; + + if (dp == NULL) + return BAD_FUNC_ARG; + + if (dp == NULL || dp->prime == NULL || dp->Af == NULL || + dp->Bf == NULL || dp->order == NULL || dp->Gx == NULL || dp->Gy == NULL) + return BAD_FUNC_ARG; + + for (idx = 0; ecc_sets[idx].size != 0; idx++) { + if (dp->size == ecc_sets[idx].size) { + if ((wc_ecc_cmp_param(ecc_sets[idx].prime, (const byte*)dp->prime, + (word32)XSTRLEN(dp->prime), WC_TYPE_HEX_STR) == MP_EQ) && + (wc_ecc_cmp_param(ecc_sets[idx].Af, (const byte*)dp->Af, + (word32)XSTRLEN(dp->Af),WC_TYPE_HEX_STR) == MP_EQ) && + (wc_ecc_cmp_param(ecc_sets[idx].Bf, (const byte*)dp->Bf, + (word32)XSTRLEN(dp->Bf),WC_TYPE_HEX_STR) == MP_EQ) && + (wc_ecc_cmp_param(ecc_sets[idx].order, (const byte*)dp->order, + (word32)XSTRLEN(dp->order),WC_TYPE_HEX_STR) == MP_EQ) && + (wc_ecc_cmp_param(ecc_sets[idx].Gx, (const byte*)dp->Gx, + (word32)XSTRLEN(dp->Gx),WC_TYPE_HEX_STR) == MP_EQ) && + (wc_ecc_cmp_param(ecc_sets[idx].Gy, (const byte*)dp->Gy, + (word32)XSTRLEN(dp->Gy),WC_TYPE_HEX_STR) == MP_EQ) && + (dp->cofactor == ecc_sets[idx].cofactor)) { + break; + } + } + } + + if (ecc_sets[idx].size == 0) + return ECC_CURVE_INVALID; + + return ecc_sets[idx].id; +} + /* Returns the curve id that corresponds to a given OID, * as listed in ecc_sets[] of ecc.c. * diff --git a/wolfssl/wolfcrypt/ecc.h b/wolfssl/wolfcrypt/ecc.h index 49b6b1acb..3c543a0c4 100644 --- a/wolfssl/wolfcrypt/ecc.h +++ b/wolfssl/wolfcrypt/ecc.h @@ -501,6 +501,8 @@ int wc_ecc_get_curve_id_from_params(int fieldSize, const byte* prime, word32 primeSz, const byte* Af, word32 AfSz, const byte* Bf, word32 BfSz, const byte* order, word32 orderSz, const byte* Gx, word32 GxSz, const byte* Gy, word32 GySz, int cofactor); +WOLFSSL_API +int wc_ecc_get_curve_id_from_dp_params(const ecc_set_type* dp); WOLFSSL_API int wc_ecc_get_curve_id_from_oid(const byte* oid, word32 len);