Skip to content

Commit c959d22

Browse files
authored
Merge pull request #5868 from dgarske/ecc_pub_math
Expose more ECC math functions and improve async shared secret
2 parents f8484fb + 9ad4e3f commit c959d22

2 files changed

Lines changed: 41 additions & 49 deletions

File tree

wolfcrypt/src/ecc.c

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4605,6 +4605,8 @@ static int wc_ecc_shared_secret_gen_async(ecc_key* private_key,
46054605
ecc_point* point, byte* out, word32 *outlen)
46064606
{
46074607
int err = 0;
4608+
4609+
#if defined(HAVE_CAVIUM_V) || defined(HAVE_INTEL_QA)
46084610
DECLARE_CURVE_SPECS(3);
46094611

46104612
/* load curve info */
@@ -4620,7 +4622,6 @@ static int wc_ecc_shared_secret_gen_async(ecc_key* private_key,
46204622
return err;
46214623
}
46224624

4623-
#if defined(HAVE_CAVIUM_V) || defined(HAVE_INTEL_QA)
46244625
if (private_key->dp
46254626
#ifdef WOLFSSL_CUSTOM_CURVES
46264627
&& private_key->dp->id != ECC_CURVE_CUSTOM
@@ -4660,57 +4661,37 @@ static int wc_ecc_shared_secret_gen_async(ecc_key* private_key,
46604661
&curve->Af->raw, &curve->Bf->raw, &curve->prime->raw,
46614662
private_key->dp->cofactor);
46624663
#endif
4663-
wc_ecc_curve_free(curve);
4664-
FREE_CURVE_SPECS();
4665-
return err;
4664+
4665+
if (err == WC_PENDING_E) {
4666+
/* advance state, next call will handle return code processing */
4667+
private_key->state++;
4668+
}
46664669
}
4670+
else
46674671
#elif defined(WOLFSSL_ASYNC_CRYPT_SW)
46684672
if (wc_AsyncSwInit(&private_key->asyncDev, ASYNC_SW_ECC_SHARED_SEC)) {
46694673
WC_ASYNC_SW* sw = &private_key->asyncDev.sw;
46704674
sw->eccSharedSec.private_key = private_key;
46714675
sw->eccSharedSec.public_point = point;
46724676
sw->eccSharedSec.out = out;
46734677
sw->eccSharedSec.outLen = outlen;
4674-
wc_ecc_curve_free(curve);
4675-
FREE_CURVE_SPECS();
4676-
return WC_PENDING_E;
4678+
err = WC_PENDING_E;
46774679
}
4680+
else
46784681
#endif
4682+
{
4683+
/* use sync in other cases */
4684+
err = wc_ecc_shared_secret_gen_sync(private_key, point, out, outlen);
4685+
}
46794686

4680-
/* use sync in other cases */
4681-
err = wc_ecc_shared_secret_gen_sync(private_key, point, out, outlen);
4682-
4687+
#if defined(HAVE_CAVIUM_V) || defined(HAVE_INTEL_QA)
46834688
wc_ecc_curve_free(curve);
46844689
FREE_CURVE_SPECS();
4685-
4686-
return err;
4687-
}
4688-
#endif /* WOLFSSL_ASYNC_CRYPT && WC_ASYNC_ENABLE_ECC */
4689-
4690-
int wc_ecc_shared_secret_gen(ecc_key* private_key, ecc_point* point,
4691-
byte* out, word32 *outlen)
4692-
{
4693-
int err = MP_OKAY;
4694-
4695-
if (private_key == NULL || point == NULL || out == NULL ||
4696-
outlen == NULL) {
4697-
return BAD_FUNC_ARG;
4698-
}
4699-
4700-
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
4701-
if (private_key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) {
4702-
err = wc_ecc_shared_secret_gen_async(private_key, point,
4703-
out, outlen);
4704-
}
4705-
else
47064690
#endif
4707-
{
4708-
err = wc_ecc_shared_secret_gen_sync(private_key, point,
4709-
out, outlen);
4710-
}
47114691

47124692
return err;
47134693
}
4694+
#endif /* WOLFSSL_ASYNC_CRYPT && WC_ASYNC_ENABLE_ECC */
47144695

47154696
#ifndef WOLF_CRYPTO_CB_ONLY_ECC
47164697
/**
@@ -4752,7 +4733,23 @@ int wc_ecc_shared_secret_ex(ecc_key* private_key, ecc_point* point,
47524733
case ECC_STATE_SHARED_SEC_GEN:
47534734
private_key->state = ECC_STATE_SHARED_SEC_GEN;
47544735

4755-
err = wc_ecc_shared_secret_gen(private_key, point, out, outlen);
4736+
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
4737+
if (private_key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) {
4738+
err = wc_ecc_shared_secret_gen_async(private_key, point,
4739+
out, outlen);
4740+
if (err == 0) {
4741+
/* advance state and exit early */
4742+
private_key->state++;
4743+
RESTORE_VECTOR_REGISTERS();
4744+
return err;
4745+
}
4746+
}
4747+
else
4748+
#endif
4749+
{
4750+
err = wc_ecc_shared_secret_gen_sync(private_key, point,
4751+
out, outlen);
4752+
}
47564753
if (err < 0) {
47574754
break;
47584755
}
@@ -4783,7 +4780,6 @@ int wc_ecc_shared_secret_ex(ecc_key* private_key, ecc_point* point,
47834780

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

@@ -5029,7 +5025,7 @@ static int ecc_make_pub_ex(ecc_key* key, ecc_curve_spec* curveIn,
50295025
#endif
50305026
}
50315027

5032-
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC) && \
5028+
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC_KEYGEN) && \
50335029
defined(HAVE_INTEL_QA)
50345030
if (err == MP_OKAY && key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) {
50355031
word32 keySz = key->dp->size;

wolfssl/wolfcrypt/ecc.h

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -570,12 +570,10 @@ ECC_API int ecc_projective_add_point(ecc_point* P, ecc_point* Q, ecc_point* R,
570570
ECC_API int ecc_projective_dbl_point(ecc_point* P, ecc_point* R, mp_int* a,
571571
mp_int* modulus, mp_digit mp);
572572

573-
WOLFSSL_LOCAL
574-
int ecc_projective_add_point_safe(ecc_point* A, ecc_point* B, ecc_point* R,
575-
mp_int* a, mp_int* modulus, mp_digit mp, int* infinity);
576-
WOLFSSL_LOCAL
577-
int ecc_projective_dbl_point_safe(ecc_point* P, ecc_point* R, mp_int* a,
578-
mp_int* modulus, mp_digit mp);
573+
ECC_API int ecc_projective_add_point_safe(ecc_point* A, ecc_point* B,
574+
ecc_point* R, mp_int* a, mp_int* modulus, mp_digit mp, int* infinity);
575+
ECC_API int ecc_projective_dbl_point_safe(ecc_point* P, ecc_point* R, mp_int* a,
576+
mp_int* modulus, mp_digit mp);
579577

580578
WOLFSSL_ABI WOLFSSL_API
581579
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);
599597
WOLFSSL_ABI WOLFSSL_API
600598
int wc_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out,
601599
word32* outlen);
602-
WOLFSSL_LOCAL
603-
int wc_ecc_shared_secret_gen(ecc_key* private_key, ecc_point* point,
604-
byte* out, word32 *outlen);
600+
605601
WOLFSSL_API
606602
int wc_ecc_shared_secret_ex(ecc_key* private_key, ecc_point* point,
607603
byte* out, word32 *outlen);
@@ -727,10 +723,10 @@ int wc_ecc_point_is_on_curve(ecc_point *p, int curve_idx);
727723
WOLFSSL_API
728724
int wc_ecc_mulmod(const mp_int* k, ecc_point *G, ecc_point *R,
729725
mp_int* a, mp_int* modulus, int map);
730-
WOLFSSL_LOCAL
726+
ECC_API
731727
int wc_ecc_mulmod_ex(const mp_int* k, ecc_point *G, ecc_point *R,
732728
mp_int* a, mp_int* modulus, int map, void* heap);
733-
WOLFSSL_LOCAL
729+
ECC_API
734730
int wc_ecc_mulmod_ex2(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a,
735731
mp_int* modulus, mp_int* order, WC_RNG* rng, int map,
736732
void* heap);

0 commit comments

Comments
 (0)