diff --git a/wolfcrypt/src/curve25519.c b/wolfcrypt/src/curve25519.c index 39e1216a0..8568a9894 100644 --- a/wolfcrypt/src/curve25519.c +++ b/wolfcrypt/src/curve25519.c @@ -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 diff --git a/wolfcrypt/src/fe_operations.c b/wolfcrypt/src/fe_operations.c index 1e1c92bf2..691b344e8 100644 --- a/wolfcrypt/src/fe_operations.c +++ b/wolfcrypt/src/fe_operations.c @@ -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]; diff --git a/wolfcrypt/src/port/nxp/ksdk_port.c b/wolfcrypt/src/port/nxp/ksdk_port.c index a5cc737d7..fdbd95764 100644 --- a/wolfcrypt/src/port/nxp/ksdk_port.c +++ b/wolfcrypt/src/port/nxp/ksdk_port.c @@ -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; diff --git a/wolfssl/wolfcrypt/curve25519.h b/wolfssl/wolfcrypt/curve25519.h index 2b122e7a2..9ac83c539 100644 --- a/wolfssl/wolfcrypt/curve25519.h +++ b/wolfssl/wolfcrypt/curve25519.h @@ -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); diff --git a/wolfssl/wolfcrypt/fe_operations.h b/wolfssl/wolfcrypt/fe_operations.h index 336da81c6..5e01eb83b 100644 --- a/wolfssl/wolfcrypt/fe_operations.h +++ b/wolfssl/wolfcrypt/fe_operations.h @@ -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 */ diff --git a/wolfssl/wolfcrypt/port/nxp/ksdk_port.h b/wolfssl/wolfcrypt/port/nxp/ksdk_port.h index 749a3eeb5..9c52bedaf 100644 --- a/wolfssl/wolfcrypt/port/nxp/ksdk_port.h +++ b/wolfssl/wolfcrypt/port/nxp/ksdk_port.h @@ -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);