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 817ac45f4..3c87ed0a7 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -10069,6 +10069,11 @@ static int ecc_point_test(void) 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(); @@ -10082,17 +10087,17 @@ static int ecc_point_test(void) /* 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; @@ -10102,23 +10107,23 @@ static int ecc_point_test(void) 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; @@ -10155,14 +10160,14 @@ static int ecc_point_test(void) } /* 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; @@ -10187,7 +10192,7 @@ static int ecc_point_test(void) 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; @@ -10200,13 +10205,13 @@ static int ecc_point_test(void) #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);