SP ASM improvements

Change Karatsuba implementations for x86_64.
Fix ECC code to better handle corner cases.
Add 'lower' versions of functions wehn an input is known to be less than m.
Add mont_add/dbl/tpl/sub for P384.
Change ECC point add to be cache-attack resistant.
Change mod_exp to be cache-attack resistant.
This commit is contained in:
Sean Parkinson
2022-03-24 12:11:13 +10:00
parent f71a85d5f9
commit fd66f6bcec
11 changed files with 52668 additions and 17870 deletions

View File

@ -5837,12 +5837,12 @@ void bench_eccMakeKey(int doAsync, int curveId)
bench_stats_start(&count, &start);
do {
/* while free pending slots in queue, submit ops */
for (times = 0; times < genTimes || pending > 0; ) {
for (times = 0; times < agreeTimes || pending > 0; ) {
bench_async_poll(&pending);
for (i = 0; i < BENCH_MAX_PENDING; i++) {
if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0,
&times, genTimes, &pending)) {
&times, agreeTimes, &pending)) {
wc_ecc_free(&genKey[i]);
ret = wc_ecc_init_ex(&genKey[i], HEAP_HINT, deviceID);

View File

@ -4258,16 +4258,9 @@ int wc_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out,
!defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_KCAPI_ECC)
static int wc_ecc_shared_secret_gen_sync(ecc_key* private_key, ecc_point* point,
byte* out, word32* outlen, ecc_curve_spec* curve)
byte* out, word32* outlen)
{
int err = MP_OKAY;
#if !defined(WOLFSSL_SP_MATH)
ecc_point* result = NULL;
#ifdef WOLFSSL_NO_MALLOC
ecc_point lcl_result;
#endif
word32 x = 0;
#endif
mp_int* k = &private_key->k;
#ifdef HAVE_ECC_CDH
#ifdef WOLFSSL_SMALL_STACK
@ -4333,20 +4326,41 @@ static int wc_ecc_shared_secret_gen_sync(ecc_key* private_key, ecc_point* point,
#if defined(WOLFSSL_SP_MATH)
{
err = WC_KEY_SIZE_E;
(void)curve;
goto errout;
}
#else
{
ecc_point* result = NULL;
#ifdef WOLFSSL_NO_MALLOC
ecc_point lcl_result;
#endif
word32 x = 0;
mp_digit mp = 0;
DECLARE_CURVE_SPECS(3);
/* load curve info */
ALLOC_CURVE_SPECS(3, err);
if (err == MP_OKAY) {
err = wc_ecc_curve_load(private_key->dp, &curve,
(ECC_CURVE_FIELD_PRIME | ECC_CURVE_FIELD_AF |
ECC_CURVE_FIELD_ORDER));
}
if (err != MP_OKAY) {
FREE_CURVE_SPECS();
goto errout;
}
/* make new point */
#ifdef WOLFSSL_NO_MALLOC
result = &lcl_result;
#endif
err = wc_ecc_new_point_ex(&result, private_key->heap);
if (err != MP_OKAY)
if (err != MP_OKAY) {
wc_ecc_curve_free(curve);
FREE_CURVE_SPECS();
goto errout;
}
#ifdef ECC_TIMING_RESISTANT
if (private_key->rng == NULL) {
@ -4387,6 +4401,9 @@ static int wc_ecc_shared_secret_gen_sync(ecc_key* private_key, ecc_point* point,
*outlen = x;
wc_ecc_del_point_ex(result, private_key->heap);
wc_ecc_curve_free(curve);
FREE_CURVE_SPECS();
}
#endif
@ -4408,10 +4425,23 @@ static int wc_ecc_shared_secret_gen_sync(ecc_key* private_key, ecc_point* point,
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
static int wc_ecc_shared_secret_gen_async(ecc_key* private_key,
ecc_point* point, byte* out, word32 *outlen,
ecc_curve_spec* curve)
ecc_point* point, byte* out, word32 *outlen)
{
int err;
DECLARE_CURVE_SPECS(3);
/* load curve info */
ALLOC_CURVE_SPECS(3, err);
if (err == MP_OKAY) {
err = wc_ecc_curve_load(private_key->dp, &curve,
(ECC_CURVE_FIELD_PRIME | ECC_CURVE_FIELD_AF |
ECC_CURVE_FIELD_ORDER));
}
if (err != MP_OKAY) {
FREE_CURVE_SPECS();
return err;
}
#if defined(HAVE_CAVIUM_V) || defined(HAVE_INTEL_QA)
if (private_key->dp
@ -4453,6 +4483,8 @@ 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;
}
#elif defined(WOLFSSL_ASYNC_CRYPT_TEST)
@ -4462,6 +4494,8 @@ static int wc_ecc_shared_secret_gen_async(ecc_key* private_key,
testDev->eccSharedSec.public_point = point;
testDev->eccSharedSec.out = out;
testDev->eccSharedSec.outLen = outlen;
wc_ecc_curve_free(curve);
FREE_CURVE_SPECS();
return WC_PENDING_E;
}
#endif
@ -4469,6 +4503,9 @@ static int wc_ecc_shared_secret_gen_async(ecc_key* private_key,
/* use sync in other cases */
err = wc_ecc_shared_secret_gen_sync(private_key, point, out, outlen, curve);
wc_ecc_curve_free(curve);
FREE_CURVE_SPECS();
return err;
}
#endif /* WOLFSSL_ASYNC_CRYPT && WC_ASYNC_ENABLE_ECC */
@ -4477,40 +4514,24 @@ int wc_ecc_shared_secret_gen(ecc_key* private_key, ecc_point* point,
byte* out, word32 *outlen)
{
int err = MP_OKAY;
DECLARE_CURVE_SPECS(3);
if (private_key == NULL || point == NULL || out == NULL ||
outlen == NULL) {
return BAD_FUNC_ARG;
}
/* load curve info */
ALLOC_CURVE_SPECS(3, err);
if (err == MP_OKAY) {
err = wc_ecc_curve_load(private_key->dp, &curve,
(ECC_CURVE_FIELD_PRIME | ECC_CURVE_FIELD_AF | ECC_CURVE_FIELD_ORDER));
}
if (err != MP_OKAY) {
FREE_CURVE_SPECS();
return err;
}
#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, curve);
out, outlen);
}
else
#endif
{
err = wc_ecc_shared_secret_gen_sync(private_key, point,
out, outlen, curve);
out, outlen);
}
wc_ecc_curve_free(curve);
FREE_CURVE_SPECS();
return err;
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff