diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index 5b15258b6f..a34ef0cdd5 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -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 */ @@ -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 @@ -4660,10 +4661,13 @@ 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; @@ -4671,46 +4675,23 @@ static int wc_ecc_shared_secret_gen_async(ecc_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 /** @@ -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; } @@ -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; } @@ -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; diff --git a/wolfssl/wolfcrypt/ecc.h b/wolfssl/wolfcrypt/ecc.h index a5949d7918..97170eb4e3 100644 --- a/wolfssl/wolfcrypt/ecc.h +++ b/wolfssl/wolfcrypt/ecc.h @@ -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); @@ -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); @@ -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);