Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Expose more ECC math functions and improve async shared secret #5868

Merged
merged 1 commit into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 34 additions & 38 deletions wolfcrypt/src/ecc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4605,6 +4605,8 @@ static int wc_ecc_shared_secret_gen_async(ecc_key* private_key,
ecc_point* point, byte* out, word32 *outlen)
{
int err = 0;

#if defined(HAVE_CAVIUM_V) || defined(HAVE_INTEL_QA)
DECLARE_CURVE_SPECS(3);

/* load curve info */
Expand All @@ -4620,7 +4622,6 @@ static int wc_ecc_shared_secret_gen_async(ecc_key* private_key,
return err;
}

#if defined(HAVE_CAVIUM_V) || defined(HAVE_INTEL_QA)
if (private_key->dp
#ifdef WOLFSSL_CUSTOM_CURVES
&& private_key->dp->id != ECC_CURVE_CUSTOM
Expand Down Expand Up @@ -4660,57 +4661,37 @@ static int wc_ecc_shared_secret_gen_async(ecc_key* private_key,
&curve->Af->raw, &curve->Bf->raw, &curve->prime->raw,
private_key->dp->cofactor);
#endif
wc_ecc_curve_free(curve);
FREE_CURVE_SPECS();
return err;

if (err == WC_PENDING_E) {
/* advance state, next call will handle return code processing */
private_key->state++;
}
}
else
#elif defined(WOLFSSL_ASYNC_CRYPT_SW)
if (wc_AsyncSwInit(&private_key->asyncDev, ASYNC_SW_ECC_SHARED_SEC)) {
WC_ASYNC_SW* sw = &private_key->asyncDev.sw;
sw->eccSharedSec.private_key = private_key;
sw->eccSharedSec.public_point = point;
sw->eccSharedSec.out = out;
sw->eccSharedSec.outLen = outlen;
wc_ecc_curve_free(curve);
FREE_CURVE_SPECS();
return WC_PENDING_E;
err = WC_PENDING_E;
}
else
#endif
{
/* use sync in other cases */
err = wc_ecc_shared_secret_gen_sync(private_key, point, out, outlen);
}

/* use sync in other cases */
err = wc_ecc_shared_secret_gen_sync(private_key, point, out, outlen);

#if defined(HAVE_CAVIUM_V) || defined(HAVE_INTEL_QA)
wc_ecc_curve_free(curve);
FREE_CURVE_SPECS();

return err;
}
#endif /* WOLFSSL_ASYNC_CRYPT && WC_ASYNC_ENABLE_ECC */

int wc_ecc_shared_secret_gen(ecc_key* private_key, ecc_point* point,
byte* out, word32 *outlen)
{
int err = MP_OKAY;

if (private_key == NULL || point == NULL || out == NULL ||
outlen == NULL) {
return BAD_FUNC_ARG;
}

#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
if (private_key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) {
err = wc_ecc_shared_secret_gen_async(private_key, point,
out, outlen);
}
else
#endif
{
err = wc_ecc_shared_secret_gen_sync(private_key, point,
out, outlen);
}

return err;
}
#endif /* WOLFSSL_ASYNC_CRYPT && WC_ASYNC_ENABLE_ECC */

#ifndef WOLF_CRYPTO_CB_ONLY_ECC
/**
Expand Down Expand Up @@ -4752,7 +4733,23 @@ int wc_ecc_shared_secret_ex(ecc_key* private_key, ecc_point* point,
case ECC_STATE_SHARED_SEC_GEN:
private_key->state = ECC_STATE_SHARED_SEC_GEN;

err = wc_ecc_shared_secret_gen(private_key, point, out, outlen);
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
if (private_key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) {
err = wc_ecc_shared_secret_gen_async(private_key, point,
out, outlen);
if (err == 0) {
/* advance state and exit early */
private_key->state++;
RESTORE_VECTOR_REGISTERS();
return err;
}
}
else
#endif
{
err = wc_ecc_shared_secret_gen_sync(private_key, point,
out, outlen);
}
if (err < 0) {
break;
}
Expand Down Expand Up @@ -4783,7 +4780,6 @@ int wc_ecc_shared_secret_ex(ecc_key* private_key, ecc_point* point,

/* if async pending then return and skip done cleanup below */
if (err == WC_PENDING_E) {
private_key->state++;
return err;
}

Expand Down Expand Up @@ -5029,7 +5025,7 @@ static int ecc_make_pub_ex(ecc_key* key, ecc_curve_spec* curveIn,
#endif
}

#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC) && \
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC_KEYGEN) && \
defined(HAVE_INTEL_QA)
if (err == MP_OKAY && key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) {
word32 keySz = key->dp->size;
Expand Down
18 changes: 7 additions & 11 deletions wolfssl/wolfcrypt/ecc.h
Original file line number Diff line number Diff line change
Expand Up @@ -570,12 +570,10 @@ ECC_API int ecc_projective_add_point(ecc_point* P, ecc_point* Q, ecc_point* R,
ECC_API int ecc_projective_dbl_point(ecc_point* P, ecc_point* R, mp_int* a,
mp_int* modulus, mp_digit mp);

WOLFSSL_LOCAL
int ecc_projective_add_point_safe(ecc_point* A, ecc_point* B, ecc_point* R,
mp_int* a, mp_int* modulus, mp_digit mp, int* infinity);
WOLFSSL_LOCAL
int ecc_projective_dbl_point_safe(ecc_point* P, ecc_point* R, mp_int* a,
mp_int* modulus, mp_digit mp);
ECC_API int ecc_projective_add_point_safe(ecc_point* A, ecc_point* B,
ecc_point* R, mp_int* a, mp_int* modulus, mp_digit mp, int* infinity);
ECC_API int ecc_projective_dbl_point_safe(ecc_point* P, ecc_point* R, mp_int* a,
mp_int* modulus, mp_digit mp);

WOLFSSL_ABI WOLFSSL_API
int wc_ecc_make_key(WC_RNG* rng, int keysize, ecc_key* key);
Expand All @@ -599,9 +597,7 @@ int wc_ecc_get_generator(ecc_point* ecp, int curve_idx);
WOLFSSL_ABI WOLFSSL_API
int wc_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out,
word32* outlen);
WOLFSSL_LOCAL
int wc_ecc_shared_secret_gen(ecc_key* private_key, ecc_point* point,
byte* out, word32 *outlen);

WOLFSSL_API
int wc_ecc_shared_secret_ex(ecc_key* private_key, ecc_point* point,
byte* out, word32 *outlen);
Expand Down Expand Up @@ -727,10 +723,10 @@ int wc_ecc_point_is_on_curve(ecc_point *p, int curve_idx);
WOLFSSL_API
int wc_ecc_mulmod(const mp_int* k, ecc_point *G, ecc_point *R,
mp_int* a, mp_int* modulus, int map);
WOLFSSL_LOCAL
ECC_API
int wc_ecc_mulmod_ex(const mp_int* k, ecc_point *G, ecc_point *R,
mp_int* a, mp_int* modulus, int map, void* heap);
WOLFSSL_LOCAL
ECC_API
int wc_ecc_mulmod_ex2(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a,
mp_int* modulus, mp_int* order, WC_RNG* rng, int map,
void* heap);
Expand Down