mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-08-02 20:24:39 +02:00
Merge pull request #823 from cconlon/ecc_adds
add ECC helpers to get size and id from curve name
This commit is contained in:
57
tests/api.c
57
tests/api.c
@@ -3075,6 +3075,59 @@ static void test_wc_GetPkcs8TraditionalOffset(void)
|
||||
}
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------*
|
||||
| wolfCrypt ECC
|
||||
*----------------------------------------------------------------------------*/
|
||||
|
||||
static void test_wc_ecc_get_curve_size_from_name(void)
|
||||
{
|
||||
#ifdef HAVE_ECC
|
||||
int ret;
|
||||
|
||||
printf(testingFmt, "wc_ecc_get_curve_size_from_name");
|
||||
|
||||
#if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
|
||||
ret = wc_ecc_get_curve_size_from_name("SECP256R1");
|
||||
AssertIntEQ(ret, 32);
|
||||
#endif
|
||||
|
||||
/* invalid case */
|
||||
ret = wc_ecc_get_curve_size_from_name("BADCURVE");
|
||||
AssertIntEQ(ret, -1);
|
||||
|
||||
/* NULL input */
|
||||
ret = wc_ecc_get_curve_size_from_name(NULL);
|
||||
AssertIntEQ(ret, BAD_FUNC_ARG);
|
||||
|
||||
printf(resultFmt, passed);
|
||||
#endif /* HAVE_ECC */
|
||||
}
|
||||
|
||||
static void test_wc_ecc_get_curve_id_from_name(void)
|
||||
{
|
||||
#ifdef HAVE_ECC
|
||||
int id;
|
||||
|
||||
printf(testingFmt, "wc_ecc_get_curve_id_from_name");
|
||||
|
||||
#if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
|
||||
id = wc_ecc_get_curve_id_from_name("SECP256R1");
|
||||
AssertIntEQ(id, ECC_SECP256R1);
|
||||
#endif
|
||||
|
||||
/* invalid case */
|
||||
id = wc_ecc_get_curve_id_from_name("BADCURVE");
|
||||
AssertIntEQ(id, -1);
|
||||
|
||||
/* NULL input */
|
||||
id = wc_ecc_get_curve_id_from_name(NULL);
|
||||
AssertIntEQ(id, BAD_FUNC_ARG);
|
||||
|
||||
printf(resultFmt, passed);
|
||||
#endif /* HAVE_ECC */
|
||||
}
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------*
|
||||
| Main
|
||||
*----------------------------------------------------------------------------*/
|
||||
@@ -3139,6 +3192,10 @@ void ApiTest(void)
|
||||
/* wolfCrypt ASN tests */
|
||||
test_wc_GetPkcs8TraditionalOffset();
|
||||
|
||||
/* wolfCrypt ECC tests */
|
||||
test_wc_ecc_get_curve_size_from_name();
|
||||
test_wc_ecc_get_curve_id_from_name();
|
||||
|
||||
printf(" End API Tests\n");
|
||||
|
||||
}
|
||||
|
@@ -2490,6 +2490,74 @@ int wc_ecc_get_curve_size_from_id(int curve_id)
|
||||
return ecc_sets[curve_idx].size;
|
||||
}
|
||||
|
||||
/* Returns the curve index that corresponds to a given curve name in
|
||||
* ecc_sets[] of ecc.c
|
||||
*
|
||||
* name curve name, from ecc_sets[].name in ecc.c
|
||||
* return curve index in ecc_sets[] on success, negative on error
|
||||
*/
|
||||
int wc_ecc_get_curve_idx_from_name(const char* curveName)
|
||||
{
|
||||
int curve_idx;
|
||||
word32 len;
|
||||
|
||||
if (curveName == NULL)
|
||||
return BAD_FUNC_ARG;
|
||||
|
||||
len = XSTRLEN(curveName);
|
||||
|
||||
for (curve_idx = 0; ecc_sets[curve_idx].size != 0; curve_idx++) {
|
||||
if (XSTRNCASECMP(ecc_sets[curve_idx].name, curveName, len) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ecc_sets[curve_idx].size == 0) {
|
||||
WOLFSSL_MSG("ecc_set curve name not found");
|
||||
return ECC_CURVE_INVALID;
|
||||
}
|
||||
return curve_idx;
|
||||
}
|
||||
|
||||
/* Returns the curve size that corresponds to a given curve name,
|
||||
* as listed in ecc_sets[] of ecc.c.
|
||||
*
|
||||
* name curve name, from ecc_sets[].name in ecc.c
|
||||
* return curve size, from ecc_sets[] on success, negative on error
|
||||
*/
|
||||
int wc_ecc_get_curve_size_from_name(const char* curveName)
|
||||
{
|
||||
int curve_idx;
|
||||
|
||||
if (curveName == NULL)
|
||||
return BAD_FUNC_ARG;
|
||||
|
||||
curve_idx = wc_ecc_get_curve_idx_from_name(curveName);
|
||||
if (curve_idx < 0)
|
||||
return curve_idx;
|
||||
|
||||
return ecc_sets[curve_idx].size;
|
||||
}
|
||||
|
||||
/* Returns the curve id that corresponds to a given curve name,
|
||||
* as listed in ecc_sets[] of ecc.c.
|
||||
*
|
||||
* name curve name, from ecc_sets[].name in ecc.c
|
||||
* return curve id, from ecc_sets[] on success, negative on error
|
||||
*/
|
||||
int wc_ecc_get_curve_id_from_name(const char* curveName)
|
||||
{
|
||||
int curve_idx;
|
||||
|
||||
if (curveName == NULL)
|
||||
return BAD_FUNC_ARG;
|
||||
|
||||
curve_idx = wc_ecc_get_curve_idx_from_name(curveName);
|
||||
if (curve_idx < 0)
|
||||
return curve_idx;
|
||||
|
||||
return ecc_sets[curve_idx].id;
|
||||
}
|
||||
|
||||
|
||||
#ifdef HAVE_ECC_DHE
|
||||
/**
|
||||
|
@@ -370,6 +370,13 @@ int wc_ecc_get_curve_id(int curve_idx);
|
||||
WOLFSSL_API
|
||||
int wc_ecc_get_curve_size_from_id(int curve_id);
|
||||
|
||||
WOLFSSL_API
|
||||
int wc_ecc_get_curve_idx_from_name(const char* curveName);
|
||||
WOLFSSL_API
|
||||
int wc_ecc_get_curve_size_from_name(const char* curveName);
|
||||
WOLFSSL_API
|
||||
int wc_ecc_get_curve_id_from_name(const char* curveName);
|
||||
|
||||
#ifndef WOLFSSL_ATECC508A
|
||||
|
||||
WOLFSSL_API
|
||||
|
Reference in New Issue
Block a user