From 0eb01698f4595fabde080b4d34717a6be81e1cc3 Mon Sep 17 00:00:00 2001 From: David Garske Date: Mon, 13 Mar 2017 19:58:15 -0700 Subject: [PATCH] =?UTF-8?q?Fix=20for=20wolfCrypt=20ECC=20import/export=20p?= =?UTF-8?q?oint=20test=20to=20not=20use=20const=20idx=20and=20instead=20lo?= =?UTF-8?q?okup=20using=20the=20=E2=80=9Cecc=5Fcurve=5Fid=E2=80=9D=20enum?= =?UTF-8?q?=20value.=20Added=20new=20=E2=80=9Cwc=5Fecc=5Fget=5Fcurve=5Fidx?= =?UTF-8?q?=E2=80=9D=20and=20=E2=80=9Cwc=5Fecc=5Fget=5Fcurve=5Fid=E2=80=9D?= =?UTF-8?q?=20API=E2=80=99s.=20Redirected=20duplicate=20ECC=20function=20?= =?UTF-8?q?=E2=80=9Cwc=5Fecc=5Fget=5Fcurve=5Fname=5Ffrom=5Fid=E2=80=9D=20t?= =?UTF-8?q?o=20=E2=80=9Cwc=5Fecc=5Fget=5Fname=E2=80=9D.=20Added=20?= =?UTF-8?q?=E2=80=9CECC=5FCURVE=5FINVALID=E2=80=9D=20to=20indicate=20inval?= =?UTF-8?q?id=20curve=5Fid.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wolfcrypt/src/ecc.c | 71 +++++++++++++++-------------------------- wolfcrypt/test/test.c | 31 ++++++++++-------- wolfssl/wolfcrypt/ecc.h | 8 +++-- 3 files changed, 49 insertions(+), 61 deletions(-) diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index 32dd99836..5f095f5a5 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -1102,12 +1102,8 @@ static int wc_ecc_curve_load(const ecc_set_type* dp, ecc_curve_spec** pCurve, return BAD_FUNC_ARG; #ifdef ECC_CACHE_CURVE - /* find ecc_set index based on curve_id */ - for (x = 0; ecc_sets[x].size != 0; x++) { - if (dp->id == ecc_sets[x].id) - break; /* found index */ - } - if (ecc_sets[x].size == 0) + x = wc_ecc_get_curve_idx(dp->id); + if (x == ECC_CURVE_INVALID) return ECC_BAD_ARG_E; /* make sure cache has been allocated */ @@ -1195,6 +1191,7 @@ void wc_ecc_curve_cache_free(void) #endif /* WOLFSSL_ATECC508A */ + /* Retrieve the curve name for the ECC curve id. * * curve_id The id of the curve. @@ -1202,14 +1199,10 @@ void wc_ecc_curve_cache_free(void) */ const char* wc_ecc_get_name(int curve_id) { - int x; - - for (x = 0; ecc_sets[x].size != 0; x++) { - if (curve_id == ecc_sets[x].id) - return ecc_sets[x].name; - } - - return NULL; + int curve_idx = wc_ecc_get_curve_idx(curve_id); + if (curve_idx == ECC_CURVE_INVALID) + return NULL; + return ecc_sets[curve_idx].name; } static int wc_ecc_set_curve(ecc_key* key, int keysize, int curve_id) @@ -2468,52 +2461,38 @@ int wc_ecc_is_valid_idx(int n) return 0; } - -/* - * Returns the curve name that corresponds to an ecc_curve_id identifier - * - * id curve id, from ecc_curve_id enum in ecc.h - * return const char* representing curve name, from ecc_sets[] on success, - * otherwise NULL if id not found. - */ -const char* wc_ecc_get_curve_name_from_id(int id) +int wc_ecc_get_curve_idx(int curve_id) { - int i; - - for (i = 0; ecc_sets[i].size != 0; i++) { - if (id == ecc_sets[i].id) + int curve_idx; + for (curve_idx = 0; ecc_sets[curve_idx].size != 0; curve_idx++) { + if (curve_id == ecc_sets[curve_idx].id) break; } - - if (ecc_sets[i].size == 0) { - WOLFSSL_MSG("ecc_set curve not found"); - return NULL; + if (ecc_sets[curve_idx].size == 0) { + return ECC_CURVE_INVALID; } - - return ecc_sets[i].name; + return curve_idx; } +int wc_ecc_get_curve_id(int curve_idx) +{ + if (wc_ecc_is_valid_idx(curve_idx)) { + return ecc_sets[curve_idx].id; + } + return ECC_CURVE_INVALID; +} /* Returns the curve size that corresponds to a given ecc_curve_id identifier * * id curve id, from ecc_curve_id enum in ecc.h * return curve size, from ecc_sets[] on success, negative on error */ -int wc_ecc_get_curve_size_from_id(int id) +int wc_ecc_get_curve_size_from_id(int curve_id) { - int i; - - for (i = 0; ecc_sets[i].size != 0; i++) { - if (id == ecc_sets[i].id) - break; - } - - if (ecc_sets[i].size == 0) { - WOLFSSL_MSG("ecc_set curve not found"); + int curve_idx = wc_ecc_get_curve_idx(curve_id); + if (curve_idx == ECC_CURVE_INVALID) return ECC_BAD_ARG_E; - } - - return ecc_sets[i].size; + return ecc_sets[curve_idx].size; } diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index ffbd6b552..e3fc7af2a 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -10020,6 +10020,11 @@ static int ecc_point_test() 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; + int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1); + + /* if curve P256 is not enabled then test should not fail */ + if (curve_idx == ECC_CURVE_INVALID) + return 0; outLen = sizeof(out); point = wc_ecc_new_point(); @@ -10033,17 +10038,17 @@ static int ecc_point_test() /* Parameter Validation testing. */ wc_ecc_del_point(NULL); - ret = wc_ecc_import_point_der(NULL, sizeof(der), 6, point); + ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point); if (ret != ECC_BAD_ARG_E) { ret = -1037; goto done; } - ret = wc_ecc_import_point_der(der, sizeof(der), -1, point); + ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point); if (ret != ECC_BAD_ARG_E) { ret = -1038; goto done; } - ret = wc_ecc_import_point_der(der, sizeof(der), 6, NULL); + ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL); if (ret != ECC_BAD_ARG_E) { ret = -1039; goto done; @@ -10053,23 +10058,23 @@ static int ecc_point_test() ret = -1040; goto done; } - ret = wc_ecc_export_point_der(6, NULL, out, &outLen); + ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen); if (ret != ECC_BAD_ARG_E) { ret = -1041; goto done; } - ret = wc_ecc_export_point_der(6, point, NULL, &outLen); + ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen); if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) { - ret = -1043; + ret = -1042; goto done; } - ret = wc_ecc_export_point_der(6, point, out, NULL); + ret = wc_ecc_export_point_der(curve_idx, point, out, NULL); if (ret != ECC_BAD_ARG_E) { ret = -1043; goto done; } outLen = 0; - ret = wc_ecc_export_point_der(6, point, out, &outLen); + ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen); if (ret != BUFFER_E) { ret = -1044; goto done; @@ -10106,14 +10111,14 @@ static int ecc_point_test() } /* Use API. */ - ret = wc_ecc_import_point_der(der, sizeof(der), 6, point); + ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point); if (ret != 0) { ret = -1051; goto done; } outLen = sizeof(out); - ret = wc_ecc_export_point_der(6, point, out, &outLen); + ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen); if (ret != 0) { ret = -1052; goto done; @@ -10138,7 +10143,7 @@ static int ecc_point_test() goto done; } - ret = wc_ecc_import_point_der(altDer, sizeof(altDer), 6, point2); + ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2); if (ret != 0) { ret = -1057; goto done; @@ -10151,13 +10156,13 @@ static int ecc_point_test() #ifdef HAVE_COMP_KEY /* TODO: Doesn't work. */ - ret = wc_ecc_import_point_der(derComp0, sizeof(der), 6, point); + ret = wc_ecc_import_point_der(derComp0, sizeof(der), curve_idx, point); if (ret != 0) { ret = -1059; goto done; } - ret = wc_ecc_import_point_der(derComp1, sizeof(der), 6, point); + ret = wc_ecc_import_point_der(derComp1, sizeof(der), curve_idx, point); if (ret != 0) { ret = -1060; goto done; diff --git a/wolfssl/wolfcrypt/ecc.h b/wolfssl/wolfcrypt/ecc.h index baf33637b..49203bc2f 100644 --- a/wolfssl/wolfcrypt/ecc.h +++ b/wolfssl/wolfcrypt/ecc.h @@ -110,7 +110,8 @@ enum { /* Curve Types */ typedef enum ecc_curve_id { - ECC_CURVE_DEF, /* NIST or SECP */ + ECC_CURVE_INVALID = -1, + ECC_CURVE_DEF = 0, /* NIST or SECP */ /* NIST Prime Curves */ ECC_SECP192R1, @@ -343,7 +344,10 @@ void wc_ecc_fp_free(void); WOLFSSL_API int wc_ecc_is_valid_idx(int n); WOLFSSL_API -const char* wc_ecc_get_curve_name_from_id(int curve_id); +int wc_ecc_get_curve_idx(int curve_id); +WOLFSSL_API +int wc_ecc_get_curve_id(int curve_idx); +#define wc_ecc_get_curve_name_from_id wc_ecc_get_name WOLFSSL_API int wc_ecc_get_curve_size_from_id(int curve_id);