From 65cd8a930e7c1a537375606dd5e519b6211597a5 Mon Sep 17 00:00:00 2001 From: Sean Parkinson Date: Wed, 6 Nov 2019 15:24:12 +1000 Subject: [PATCH] SP Math and RSA Key Gen working again --- wolfcrypt/src/sp_int.c | 30 ++++++++++++++++++++++++------ wolfssl/wolfcrypt/sp_int.h | 2 ++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/wolfcrypt/src/sp_int.c b/wolfcrypt/src/sp_int.c index eced17dd0..4b9ab4eb1 100644 --- a/wolfcrypt/src/sp_int.c +++ b/wolfcrypt/src/sp_int.c @@ -1067,7 +1067,7 @@ static int sp_sqrmod(sp_int* a, sp_int* m, sp_int* r) return err; } -#if defined(WOLFSSL_HAVE_SP_DH) && defined(WOLFSSL_KEY_GEN) +#if defined(WOLFSSL_HAVE_SP_DH) || defined(WOLFSSL_KEY_GEN) /* Multiply a by b mod m and store in r: r = (a * b) mod m * * a SP integer to multiply. @@ -1077,14 +1077,32 @@ static int sp_sqrmod(sp_int* a, sp_int* m, sp_int* r) * returns MP_VAL when m is 0, MP_MEM when dynamic memory allocation fails and * MP_OKAY otherwise. */ -static int sp_mulmod(sp_int* a, sp_int* b, sp_int* m, sp_int* r) +int sp_mulmod(sp_int* a, sp_int* b, sp_int* m, sp_int* r) { - int err; + int err = MP_OKAY; +#ifdef WOLFSSL_SMALL_STACK + sp_int* t; +#else + sp_int t[1]; +#endif - err = sp_mul(a, b, r); - if (err == MP_OKAY) - err = sp_mod(r, m, r); +#ifdef WOLFSSL_SMALL_STACK + t = (sp_int*)XMALLOC(sizeof(sp_int), NULL, DYNAMIC_TYPE_BIGINT); + if (t == NULL) { + err = MP_MEM; + } +#endif + if (err == MP_OKAY) { + err = sp_mul(a, b, t); + } + if (err == MP_OKAY) { + err = sp_mod(t, m, r); + } +#ifdef WOLFSSL_SMALL_STACK + if (t != NULL) + XFREE(t, NULL, DYNAMIC_TYPE_BIGINT); +#endif return err; } #endif diff --git a/wolfssl/wolfcrypt/sp_int.h b/wolfssl/wolfcrypt/sp_int.h index 6552d7cf5..ed8e0fe1c 100644 --- a/wolfssl/wolfcrypt/sp_int.h +++ b/wolfssl/wolfcrypt/sp_int.h @@ -180,6 +180,7 @@ MP_API int sp_tohex(sp_int* a, char* str); MP_API int sp_2expt(sp_int* a, int e); MP_API int sp_rand_prime(sp_int* r, int len, WC_RNG* rng, void* heap); MP_API int sp_mul(sp_int* a, sp_int* b, sp_int* r); +MP_API int sp_mulmod(sp_int* a, sp_int* b, sp_int* m, sp_int* r); MP_API int sp_gcd(sp_int* a, sp_int* b, sp_int* r); MP_API int sp_invmod(sp_int* a, sp_int* m, sp_int* r); MP_API int sp_lcm(sp_int* a, sp_int* b, sp_int* r); @@ -246,6 +247,7 @@ MP_API void sp_rshb(sp_int* a, int n, sp_int* r); #define mp_2expt sp_2expt #define mp_rand_prime sp_rand_prime #define mp_mul sp_mul +#define mp_mulmod sp_mulmod #define mp_gcd sp_gcd #define mp_invmod sp_invmod #define mp_lcm sp_lcm