Merge pull request #834 from cconlon/ecc_name_from_params
add wc_ecc_get_curve_id_from_params()
This commit is contained in:
commit
338c70a1ab
97
tests/api.c
97
tests/api.c
@ -3127,6 +3127,102 @@ static void test_wc_ecc_get_curve_id_from_name(void)
|
||||
#endif /* HAVE_ECC */
|
||||
}
|
||||
|
||||
static void test_wc_ecc_get_curve_id_from_params(void)
|
||||
{
|
||||
#ifdef HAVE_ECC
|
||||
int id;
|
||||
|
||||
const byte prime[] =
|
||||
{
|
||||
0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
|
||||
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
|
||||
};
|
||||
|
||||
const byte primeInvalid[] =
|
||||
{
|
||||
0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
|
||||
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
|
||||
};
|
||||
|
||||
const byte Af[] =
|
||||
{
|
||||
0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
|
||||
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
|
||||
};
|
||||
|
||||
const byte Bf[] =
|
||||
{
|
||||
0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
|
||||
0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
|
||||
0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
|
||||
0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
|
||||
};
|
||||
|
||||
const byte order[] =
|
||||
{
|
||||
0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
|
||||
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
|
||||
0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
|
||||
0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
|
||||
};
|
||||
|
||||
const byte Gx[] =
|
||||
{
|
||||
0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
|
||||
0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
|
||||
0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
|
||||
0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
|
||||
};
|
||||
|
||||
const byte Gy[] =
|
||||
{
|
||||
0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
|
||||
0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
|
||||
0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
|
||||
0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
|
||||
};
|
||||
|
||||
int cofactor = 1;
|
||||
int fieldSize = 256;
|
||||
|
||||
printf(testingFmt, "wc_ecc_get_curve_id_from_params");
|
||||
|
||||
#if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
|
||||
id = wc_ecc_get_curve_id_from_params(fieldSize, prime, sizeof(prime),
|
||||
Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
|
||||
Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
|
||||
AssertIntEQ(id, ECC_SECP256R1);
|
||||
#endif
|
||||
|
||||
/* invalid case, fieldSize = 0 */
|
||||
id = wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
|
||||
Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
|
||||
Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
|
||||
AssertIntEQ(id, ECC_CURVE_INVALID);
|
||||
|
||||
/* invalid case, NULL prime */
|
||||
id = wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
|
||||
Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
|
||||
Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
|
||||
AssertIntEQ(id, BAD_FUNC_ARG);
|
||||
|
||||
/* invalid case, invalid prime */
|
||||
id = wc_ecc_get_curve_id_from_params(fieldSize,
|
||||
primeInvalid, sizeof(primeInvalid),
|
||||
Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
|
||||
Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
|
||||
AssertIntEQ(id, ECC_CURVE_INVALID);
|
||||
|
||||
printf(resultFmt, passed);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------*
|
||||
| Main
|
||||
@ -3195,6 +3291,7 @@ void ApiTest(void)
|
||||
/* wolfCrypt ECC tests */
|
||||
test_wc_ecc_get_curve_size_from_name();
|
||||
test_wc_ecc_get_curve_id_from_name();
|
||||
test_wc_ecc_get_curve_id_from_params();
|
||||
|
||||
printf(" End API Tests\n");
|
||||
|
||||
|
@ -2558,6 +2558,100 @@ int wc_ecc_get_curve_id_from_name(const char* curveName)
|
||||
return ecc_sets[curve_idx].id;
|
||||
}
|
||||
|
||||
/* Compares a curve parameter (hex, from ecc_sets[]) to given input
|
||||
* parameter (byte array) for equality.
|
||||
*
|
||||
* Returns MP_EQ on success, negative on error */
|
||||
static int wc_ecc_cmp_param(const char* curveParam,
|
||||
const byte* param, word32 paramSz)
|
||||
{
|
||||
int err = MP_OKAY;
|
||||
mp_int a, b;
|
||||
|
||||
if (param == NULL || curveParam == NULL)
|
||||
return BAD_FUNC_ARG;
|
||||
|
||||
if ((err = mp_init_multi(&a, &b, NULL, NULL, NULL, NULL)) != MP_OKAY)
|
||||
return err;
|
||||
|
||||
if (err == MP_OKAY)
|
||||
err = mp_read_unsigned_bin(&a, param, paramSz);
|
||||
|
||||
if (err == MP_OKAY)
|
||||
err = mp_read_radix(&b, curveParam, 16);
|
||||
|
||||
if (err == MP_OKAY) {
|
||||
if (mp_cmp(&a, &b) != MP_EQ) {
|
||||
err = -1;
|
||||
} else {
|
||||
err = MP_EQ;
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef USE_FAST_MATH
|
||||
mp_clear(&a);
|
||||
mp_clear(&b);
|
||||
#endif
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/* Returns the curve id in ecc_sets[] that corresponds to a given set of
|
||||
* curve parameters.
|
||||
*
|
||||
* fieldSize the field size in bits
|
||||
* prime prime of the finite field
|
||||
* primeSz size of prime in octets
|
||||
* Af first coefficient a of the curve
|
||||
* AfSz size of Af in octets
|
||||
* Bf second coefficient b of the curve
|
||||
* BfSz size of Bf in octets
|
||||
* order curve order
|
||||
* orderSz size of curve in octets
|
||||
* Gx affine x coordinate of base point
|
||||
* GxSz size of Gx in octets
|
||||
* Gy affine y coordinate of base point
|
||||
* GySz size of Gy in octets
|
||||
* cofactor curve cofactor
|
||||
*
|
||||
* return curve id, from ecc_sets[] on success, negative on error
|
||||
*/
|
||||
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)
|
||||
{
|
||||
int idx;
|
||||
int curveSz;
|
||||
|
||||
if (prime == NULL || Af == NULL || Bf == NULL || order == NULL ||
|
||||
Gx == NULL || Gy == NULL)
|
||||
return BAD_FUNC_ARG;
|
||||
|
||||
curveSz = (fieldSize + 1) / 8; /* round up */
|
||||
|
||||
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) &&
|
||||
(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) &&
|
||||
(cofactor == ecc_sets[idx].cofactor)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ecc_sets[idx].size == 0)
|
||||
return ECC_CURVE_INVALID;
|
||||
|
||||
return ecc_sets[idx].id;
|
||||
}
|
||||
|
||||
|
||||
#ifdef HAVE_ECC_DHE
|
||||
/**
|
||||
|
@ -376,6 +376,11 @@ 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);
|
||||
WOLFSSL_API
|
||||
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);
|
||||
|
||||
#ifndef WOLFSSL_ATECC508A
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user