add public function wc_curve25519() "compute the public key from an existing private key, using bare vectors."; rename existing _LOCAL functions wc_curve25519_GetBasePoint() and wc_curve25519() to nxp_ltc_curve25519_GetBasePoint() and nxp_ltc_curve25519() respectively; add const qualifiers opportunistically to existing _LOCAL function curve25519()

pull/3200/head
Daniel Pouzzner 2020-08-05 16:28:17 -05:00
parent d12b80abdf
commit ffa2cdd2d1
6 changed files with 46 additions and 10 deletions

View File

@ -51,12 +51,45 @@ const curve25519_set_type curve25519_sets[] = {
}
};
/* compute the public key from an existing private key, using bare vectors. */
int wc_curve25519(int public_size, byte* public, int private_size, const byte* private) {
int ret;
if ((public_size != CURVE25519_KEYSIZE) ||
(private_size != CURVE25519_KEYSIZE))
return ECC_BAD_ARG_E;
if ((public == NULL) || (private == NULL))
return ECC_BAD_ARG_E;
/* check clamping */
if ((private[0] & ~248) ||
(private[CURVE25519_KEYSIZE-1] & 128))
return ECC_BAD_ARG_E;
#ifdef FREESCALE_LTC_ECC
{
const ECPoint* basepoint = nxp_ltc_curve25519_GetBasePoint();
ECPoint wc_pub;
ret = nxp_ltc_curve25519(&wc_pub, private, basepoint, kLTC_Weierstrass); /* input basepoint on Weierstrass curve */
if (ret == 0)
XMEMCPY(public, wc_pub.point, CURVE25519_KEY_SIZE);
}
#else
{
static const unsigned char basepoint[CURVE25519_KEYSIZE] = {9};
ret = curve25519(public, private, basepoint);
}
#endif
return ret;
}
int wc_curve25519_make_key(WC_RNG* rng, int keysize, curve25519_key* key)
{
#ifdef FREESCALE_LTC_ECC
const ECPoint* basepoint = wc_curve25519_GetBasePoint();
const ECPoint* basepoint = nxp_ltc_curve25519_GetBasePoint();
#else
unsigned char basepoint[CURVE25519_KEYSIZE] = {9};
static const unsigned char basepoint[CURVE25519_KEYSIZE] = {9};
#endif
int ret;
@ -83,7 +116,7 @@ int wc_curve25519_make_key(WC_RNG* rng, int keysize, curve25519_key* key)
/* compute public key */
#ifdef FREESCALE_LTC_ECC
ret = wc_curve25519(&key->p, key->k.point, basepoint, kLTC_Weierstrass); /* input basepoint on Weierstrass curve */
ret = nxp_ltc_curve25519(&key->p, key->k.point, basepoint, kLTC_Weierstrass); /* input basepoint on Weierstrass curve */
#else
ret = curve25519(key->p.point, key->k.point, basepoint);
#endif
@ -127,7 +160,7 @@ int wc_curve25519_shared_secret_ex(curve25519_key* private_key,
return ECC_BAD_ARG_E;
#ifdef FREESCALE_LTC_ECC
ret = wc_curve25519(&o, private_key->k.point, &public_key->p, kLTC_Curve25519 /* input point P on Curve25519 */);
ret = nxp_ltc_curve25519(&o, private_key->k.point, &public_key->p, kLTC_Curve25519 /* input point P on Curve25519 */);
#else
ret = curve25519(o, private_key->k.point, public_key->p.point);
#endif

View File

@ -129,7 +129,7 @@ void fe_init(void)
#if defined(HAVE_CURVE25519) && !defined(CURVE25519_SMALL) && \
!defined(FREESCALE_LTC_ECC)
int curve25519(byte* q, byte* n, byte* p)
int curve25519(byte* q, const byte* n, const byte* p)
{
#if 0
unsigned char e[32];

View File

@ -974,7 +974,7 @@ static const ECPoint ecBasePoint = {
0x1e, 0xe0, 0xb4, 0x86, 0xa0, 0xb8, 0xa1, 0x19, 0xae, 0x20},
};
const ECPoint *wc_curve25519_GetBasePoint(void)
const ECPoint *nxp_ltc_curve25519_GetBasePoint(void)
{
return &ecBasePoint;
}
@ -1122,7 +1122,7 @@ status_t LTC_PKHA_Curve25519ComputeY(ltc_pkha_ecc_point_t *ltcPoint)
/* if type is set, the input point p is in Montgomery curve coordinates,
so there is a map to Weierstrass curve */
/* q output point is always in Montgomery curve coordinates */
int wc_curve25519(ECPoint *q, byte *n, const ECPoint *p, fsl_ltc_ecc_coordinate_system_t type)
int nxp_ltc_curve25519(ECPoint *q, byte *n, const ECPoint *p, fsl_ltc_ecc_coordinate_system_t type)
{
status_t status;
ltc_pkha_ecc_point_t ltcPoint;

View File

@ -86,6 +86,9 @@ enum {
EC25519_BIG_ENDIAN=1
};
WOLFSSL_API
int wc_curve25519(int public_size, byte* public, int private_size, const byte* private);
WOLFSSL_API
int wc_curve25519_make_key(WC_RNG* rng, int keysize, curve25519_key* key);

View File

@ -79,7 +79,7 @@ Bounds on each t[i] vary depending on context.
#if !defined(FREESCALE_LTC_ECC)
WOLFSSL_LOCAL void fe_init(void);
WOLFSSL_LOCAL int curve25519(byte * q, byte * n, byte * p);
WOLFSSL_LOCAL int curve25519(byte * q, const byte * n, const byte * p);
#endif
/* default to be faster but take more memory */

View File

@ -65,8 +65,8 @@ int ksdk_port_init(void);
int wc_ecc_point_add(ecc_point *mG, ecc_point *mQ, ecc_point *mR, mp_int *m);
#ifdef HAVE_CURVE25519
int wc_curve25519(ECPoint *q, byte *n, const ECPoint *p, fsl_ltc_ecc_coordinate_system_t type);
const ECPoint *wc_curve25519_GetBasePoint(void);
int nxp_ltc_curve25519(ECPoint *q, byte *n, const ECPoint *p, fsl_ltc_ecc_coordinate_system_t type);
const ECPoint *nxp_ltc_curve25519_GetBasePoint(void);
status_t LTC_PKHA_Curve25519ToWeierstrass(const ltc_pkha_ecc_point_t *ltcPointIn, ltc_pkha_ecc_point_t *ltcPointOut);
status_t LTC_PKHA_WeierstrassToCurve25519(const ltc_pkha_ecc_point_t *ltcPointIn, ltc_pkha_ecc_point_t *ltcPointOut);
status_t LTC_PKHA_Curve25519ComputeY(ltc_pkha_ecc_point_t *ltcPoint);