From 174b4d515949778e5e547f4bfbb1ffec863fd5de Mon Sep 17 00:00:00 2001 From: David Garske Date: Mon, 4 May 2020 14:23:32 -0700 Subject: [PATCH] Cleanup of SP with small stack. Expand support for `WOLFSSL_SP_NO_MALLOC`. Fix for evp.c when included directly. --- wolfcrypt/src/evp.c | 12 +- wolfcrypt/src/sp_arm32.c | 274 +++++++++--------- wolfcrypt/src/sp_arm64.c | 274 +++++++++--------- wolfcrypt/src/sp_armthumb.c | 274 +++++++++--------- wolfcrypt/src/sp_c32.c | 558 +++++++++++++++++++++++------------- wolfcrypt/src/sp_c64.c | 558 +++++++++++++++++++++++------------- wolfcrypt/src/sp_cortexm.c | 274 +++++++++--------- wolfcrypt/src/sp_int.c | 3 +- wolfcrypt/src/sp_x86_64.c | 350 +++++++++++----------- 9 files changed, 1471 insertions(+), 1106 deletions(-) diff --git a/wolfcrypt/src/evp.c b/wolfcrypt/src/evp.c index bdc46f02f..4a0bb8103 100644 --- a/wolfcrypt/src/evp.c +++ b/wolfcrypt/src/evp.c @@ -19,6 +19,13 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA */ + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include + #if !defined(WOLFSSL_EVP_INCLUDED) #ifndef WOLFSSL_IGNORE_FILE_WARN #warning evp.c does not need to be compiled separately from ssl.c @@ -26,11 +33,6 @@ #elif defined(WOLFCRYPT_ONLY) #else -#ifdef HAVE_CONFIG_H - #include -#endif - -#include #include #include diff --git a/wolfcrypt/src/sp_arm32.c b/wolfcrypt/src/sp_arm32.c index b33143218..d500965f9 100644 --- a/wolfcrypt/src/sp_arm32.c +++ b/wolfcrypt/src/sp_arm32.c @@ -4830,12 +4830,12 @@ static WC_INLINE int sp_2048_mod_32(sp_digit* r, const sp_digit* a, const sp_dig static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[16][64]; -#else - sp_digit* t[16]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[16 * 64]; #endif + sp_digit* t[16]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -4844,8 +4844,8 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 64, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 64), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -4853,12 +4853,14 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<16; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 64; - } +#else + t[i] = &td[i * 64]; #endif - norm = t[0]; + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_32(norm, m); @@ -4941,7 +4943,7 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_2048_cond_sub_32(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -4962,12 +4964,12 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][64]; -#else - sp_digit* t[32]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[32 * 64]; #endif + sp_digit* t[32]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -4976,8 +4978,8 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 64, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 64), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -4985,12 +4987,14 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<32; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 64; - } +#else + t[i] = &td[i * 64]; #endif - norm = t[0]; + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_32(norm, m); @@ -5090,7 +5094,7 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_2048_cond_sub_32(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -7087,12 +7091,12 @@ static WC_INLINE int sp_2048_mod_64_cond(sp_digit* r, const sp_digit* a, const s static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[16][128]; -#else - sp_digit* t[16]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[16 * 128]; #endif + sp_digit* t[16]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -7101,8 +7105,8 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 128, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 128), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -7110,12 +7114,14 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<16; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 128; - } +#else + t[i] = &td[i * 128]; #endif - norm = t[0]; + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_64(norm, m); @@ -7198,7 +7204,7 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_2048_cond_sub_64(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -7219,12 +7225,12 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][128]; -#else - sp_digit* t[32]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[32 * 128]; #endif + sp_digit* t[32]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -7233,8 +7239,8 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 128, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 128), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -7242,12 +7248,14 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<32; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 128; - } +#else + t[i] = &td[i * 128]; #endif - norm = t[0]; + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_64(norm, m); @@ -7347,7 +7355,7 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_2048_cond_sub_64(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -8398,11 +8406,10 @@ static void sp_2048_lshift_64(sp_digit* r, sp_digit* a, byte n) static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[128]; - sp_digit td[65]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[193]; #endif sp_digit* norm; sp_digit* tmp; @@ -8413,7 +8420,7 @@ static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 193, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { @@ -8422,12 +8429,11 @@ static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; - tmp = td + 128; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + tmp = td + 128; #else - norm = nd; - tmp = td; + tmp = &td[128]; #endif sp_2048_mont_setup(m, &mp); @@ -8487,7 +8493,7 @@ static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, sp_2048_cond_sub_64(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -15575,12 +15581,12 @@ static WC_INLINE int sp_3072_mod_48(sp_digit* r, const sp_digit* a, const sp_dig static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[16][96]; -#else - sp_digit* t[16]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[16 * 96]; #endif + sp_digit* t[16]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -15589,8 +15595,8 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 96, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 96), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -15598,12 +15604,14 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<16; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 96; - } +#else + t[i] = &td[i * 96]; #endif - norm = t[0]; + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_48(norm, m); @@ -15686,7 +15694,7 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_3072_cond_sub_48(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -15707,12 +15715,12 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][96]; -#else - sp_digit* t[32]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[32 * 96]; #endif + sp_digit* t[32]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -15721,8 +15729,8 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 96, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 96), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -15730,12 +15738,14 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<32; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 96; - } +#else + t[i] = &td[i * 96]; #endif - norm = t[0]; + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_48(norm, m); @@ -15835,7 +15845,7 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_3072_cond_sub_48(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -18632,12 +18642,12 @@ static WC_INLINE int sp_3072_mod_96_cond(sp_digit* r, const sp_digit* a, const s static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[16][192]; -#else - sp_digit* t[16]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[16 * 192]; #endif + sp_digit* t[16]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -18646,8 +18656,8 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 192, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 192), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -18655,12 +18665,14 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<16; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 192; - } +#else + t[i] = &td[i * 192]; #endif - norm = t[0]; + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_96(norm, m); @@ -18743,7 +18755,7 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_3072_cond_sub_96(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -18764,12 +18776,12 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][192]; -#else - sp_digit* t[32]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[32 * 192]; #endif + sp_digit* t[32]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -18778,8 +18790,8 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 192, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 192), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -18787,12 +18799,14 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<32; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 192; - } +#else + t[i] = &td[i * 192]; #endif - norm = t[0]; + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_96(norm, m); @@ -18892,7 +18906,7 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_3072_cond_sub_96(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -20215,11 +20229,10 @@ static void sp_3072_lshift_96(sp_digit* r, sp_digit* a, byte n) static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[192]; - sp_digit td[97]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[289]; #endif sp_digit* norm; sp_digit* tmp; @@ -20230,7 +20243,7 @@ static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 289, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { @@ -20239,12 +20252,11 @@ static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; - tmp = td + 192; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + tmp = td + 192; #else - norm = nd; - tmp = td; + tmp = &td[192]; #endif sp_3072_mont_setup(m, &mp); @@ -20304,7 +20316,7 @@ static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, sp_3072_cond_sub_96(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -71251,12 +71263,12 @@ static WC_INLINE int sp_4096_mod_128_cond(sp_digit* r, const sp_digit* a, const static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[16][256]; -#else - sp_digit* t[16]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[16 * 256]; #endif + sp_digit* t[16]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -71265,8 +71277,8 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 256, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 256), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -71274,12 +71286,14 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<16; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 256; - } +#else + t[i] = &td[i * 256]; #endif - norm = t[0]; + } sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_128(norm, m); @@ -71362,7 +71376,7 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e sp_4096_cond_sub_128(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -71383,12 +71397,12 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][256]; -#else - sp_digit* t[32]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[32 * 256]; #endif + sp_digit* t[32]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -71397,8 +71411,8 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 256, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 256), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -71406,12 +71420,14 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<32; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 256; - } +#else + t[i] = &td[i * 256]; #endif - norm = t[0]; + } sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_128(norm, m); @@ -71511,7 +71527,7 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e sp_4096_cond_sub_128(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -73106,11 +73122,10 @@ static void sp_4096_lshift_128(sp_digit* r, sp_digit* a, byte n) static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[256]; - sp_digit td[129]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[385]; #endif sp_digit* norm; sp_digit* tmp; @@ -73121,7 +73136,7 @@ static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 385, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { @@ -73130,12 +73145,11 @@ static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; - tmp = td + 256; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + tmp = td + 256; #else - norm = nd; - tmp = td; + tmp = &td[256]; #endif sp_4096_mont_setup(m, &mp); @@ -73195,7 +73209,7 @@ static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, sp_4096_cond_sub_128(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } diff --git a/wolfcrypt/src/sp_arm64.c b/wolfcrypt/src/sp_arm64.c index 370a63dbe..3ea3c4ec1 100644 --- a/wolfcrypt/src/sp_arm64.c +++ b/wolfcrypt/src/sp_arm64.c @@ -3152,12 +3152,12 @@ static WC_INLINE int sp_2048_mod_16(sp_digit* r, const sp_digit* a, const sp_dig static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[16][32]; -#else - sp_digit* t[16]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[16 * 32]; #endif + sp_digit* t[16]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -3166,8 +3166,8 @@ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 32, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 32), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -3175,12 +3175,14 @@ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<16; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 32; - } +#else + t[i] = &td[i * 32]; #endif - norm = t[0]; + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_16(norm, m); @@ -3263,7 +3265,7 @@ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_2048_cond_sub_16(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -3284,12 +3286,12 @@ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][32]; -#else - sp_digit* t[32]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[32 * 32]; #endif + sp_digit* t[32]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -3298,8 +3300,8 @@ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 32, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 32), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -3307,12 +3309,14 @@ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<32; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 32; - } +#else + t[i] = &td[i * 32]; #endif - norm = t[0]; + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_16(norm, m); @@ -3412,7 +3416,7 @@ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_2048_cond_sub_16(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -4589,12 +4593,12 @@ static WC_INLINE int sp_2048_mod_32_cond(sp_digit* r, const sp_digit* a, const s static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[16][64]; -#else - sp_digit* t[16]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[16 * 64]; #endif + sp_digit* t[16]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -4603,8 +4607,8 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 64, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 64), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -4612,12 +4616,14 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<16; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 64; - } +#else + t[i] = &td[i * 64]; #endif - norm = t[0]; + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_32(norm, m); @@ -4700,7 +4706,7 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_2048_cond_sub_32(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -4721,12 +4727,12 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][64]; -#else - sp_digit* t[32]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[32 * 64]; #endif + sp_digit* t[32]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -4735,8 +4741,8 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 64, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 64), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -4744,12 +4750,14 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<32; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 64; - } +#else + t[i] = &td[i * 64]; #endif - norm = t[0]; + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_32(norm, m); @@ -4849,7 +4857,7 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_2048_cond_sub_32(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -5604,11 +5612,10 @@ static void sp_2048_lshift_32(sp_digit* r, sp_digit* a, byte n) static int sp_2048_mod_exp_2_32(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[64]; - sp_digit td[33]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[97]; #endif sp_digit* norm; sp_digit* tmp; @@ -5619,7 +5626,7 @@ static int sp_2048_mod_exp_2_32(sp_digit* r, const sp_digit* e, int bits, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 97, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { @@ -5628,12 +5635,11 @@ static int sp_2048_mod_exp_2_32(sp_digit* r, const sp_digit* e, int bits, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; - tmp = td + 64; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + tmp = td + 64; #else - norm = nd; - tmp = td; + tmp = &td[64]; #endif sp_2048_mont_setup(m, &mp); @@ -5694,7 +5700,7 @@ static int sp_2048_mod_exp_2_32(sp_digit* r, const sp_digit* e, int bits, sp_2048_cond_sub_32(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -10487,12 +10493,12 @@ static WC_INLINE int sp_3072_mod_24(sp_digit* r, const sp_digit* a, const sp_dig static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[16][48]; -#else - sp_digit* t[16]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[16 * 48]; #endif + sp_digit* t[16]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -10501,8 +10507,8 @@ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 48, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 48), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -10510,12 +10516,14 @@ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<16; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 48; - } +#else + t[i] = &td[i * 48]; #endif - norm = t[0]; + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_24(norm, m); @@ -10598,7 +10606,7 @@ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_3072_cond_sub_24(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -10619,12 +10627,12 @@ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][48]; -#else - sp_digit* t[32]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[32 * 48]; #endif + sp_digit* t[32]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -10633,8 +10641,8 @@ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 48, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 48), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -10642,12 +10650,14 @@ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<32; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 48; - } +#else + t[i] = &td[i * 48]; #endif - norm = t[0]; + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_24(norm, m); @@ -10747,7 +10757,7 @@ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_3072_cond_sub_24(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -12292,12 +12302,12 @@ static WC_INLINE int sp_3072_mod_48_cond(sp_digit* r, const sp_digit* a, const s static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[16][96]; -#else - sp_digit* t[16]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[16 * 96]; #endif + sp_digit* t[16]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -12306,8 +12316,8 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 96, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 96), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -12315,12 +12325,14 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<16; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 96; - } +#else + t[i] = &td[i * 96]; #endif - norm = t[0]; + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_48(norm, m); @@ -12403,7 +12415,7 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_3072_cond_sub_48(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -12424,12 +12436,12 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][96]; -#else - sp_digit* t[32]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[32 * 96]; #endif + sp_digit* t[32]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -12438,8 +12450,8 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 96, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 96), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -12447,12 +12459,14 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<32; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 96; - } +#else + t[i] = &td[i * 96]; #endif - norm = t[0]; + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_48(norm, m); @@ -12552,7 +12566,7 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_3072_cond_sub_48(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -13431,11 +13445,10 @@ static void sp_3072_lshift_48(sp_digit* r, sp_digit* a, byte n) static int sp_3072_mod_exp_2_48(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[96]; - sp_digit td[49]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[145]; #endif sp_digit* norm; sp_digit* tmp; @@ -13446,7 +13459,7 @@ static int sp_3072_mod_exp_2_48(sp_digit* r, const sp_digit* e, int bits, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 145, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { @@ -13455,12 +13468,11 @@ static int sp_3072_mod_exp_2_48(sp_digit* r, const sp_digit* e, int bits, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; - tmp = td + 96; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + tmp = td + 96; #else - norm = nd; - tmp = td; + tmp = &td[96]; #endif sp_3072_mont_setup(m, &mp); @@ -13521,7 +13533,7 @@ static int sp_3072_mod_exp_2_48(sp_digit* r, const sp_digit* e, int bits, sp_3072_cond_sub_48(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -17221,12 +17233,12 @@ static WC_INLINE int sp_4096_mod_64_cond(sp_digit* r, const sp_digit* a, const s static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[16][128]; -#else - sp_digit* t[16]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[16 * 128]; #endif + sp_digit* t[16]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -17235,8 +17247,8 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 128, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 128), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -17244,12 +17256,14 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<16; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 128; - } +#else + t[i] = &td[i * 128]; #endif - norm = t[0]; + } sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_64(norm, m); @@ -17332,7 +17346,7 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_4096_cond_sub_64(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -17353,12 +17367,12 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][128]; -#else - sp_digit* t[32]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[32 * 128]; #endif + sp_digit* t[32]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -17367,8 +17381,8 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 128, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 128), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -17376,12 +17390,14 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<32; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 128; - } +#else + t[i] = &td[i * 128]; #endif - norm = t[0]; + } sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_64(norm, m); @@ -17481,7 +17497,7 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_4096_cond_sub_64(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -18484,11 +18500,10 @@ static void sp_4096_lshift_64(sp_digit* r, sp_digit* a, byte n) static int sp_4096_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[128]; - sp_digit td[65]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[193]; #endif sp_digit* norm; sp_digit* tmp; @@ -18499,7 +18514,7 @@ static int sp_4096_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 193, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { @@ -18508,12 +18523,11 @@ static int sp_4096_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; - tmp = td + 128; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + tmp = td + 128; #else - norm = nd; - tmp = td; + tmp = &td[128]; #endif sp_4096_mont_setup(m, &mp); @@ -18574,7 +18588,7 @@ static int sp_4096_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, sp_4096_cond_sub_64(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } diff --git a/wolfcrypt/src/sp_armthumb.c b/wolfcrypt/src/sp_armthumb.c index 2275dba3f..be16eed05 100644 --- a/wolfcrypt/src/sp_armthumb.c +++ b/wolfcrypt/src/sp_armthumb.c @@ -3128,12 +3128,12 @@ static WC_INLINE int sp_2048_mod_32(sp_digit* r, const sp_digit* a, const sp_dig static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[16][64]; -#else - sp_digit* t[16]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[16 * 64]; #endif + sp_digit* t[16]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -3142,8 +3142,8 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 64, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 64), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -3151,12 +3151,14 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<16; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 64; - } +#else + t[i] = &td[i * 64]; #endif - norm = t[0]; + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_32(norm, m); @@ -3239,7 +3241,7 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_2048_cond_sub_32(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -3260,12 +3262,12 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][64]; -#else - sp_digit* t[32]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[32 * 64]; #endif + sp_digit* t[32]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -3274,8 +3276,8 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 64, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 64), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -3283,12 +3285,14 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<32; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 64; - } +#else + t[i] = &td[i * 64]; #endif - norm = t[0]; + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_32(norm, m); @@ -3388,7 +3392,7 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_2048_cond_sub_32(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -3960,12 +3964,12 @@ static WC_INLINE int sp_2048_mod_64_cond(sp_digit* r, const sp_digit* a, const s static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[16][128]; -#else - sp_digit* t[16]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[16 * 128]; #endif + sp_digit* t[16]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -3974,8 +3978,8 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 128, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 128), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -3983,12 +3987,14 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<16; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 128; - } +#else + t[i] = &td[i * 128]; #endif - norm = t[0]; + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_64(norm, m); @@ -4071,7 +4077,7 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_2048_cond_sub_64(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -4092,12 +4098,12 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][128]; -#else - sp_digit* t[32]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[32 * 128]; #endif + sp_digit* t[32]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -4106,8 +4112,8 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 128, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 128), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -4115,12 +4121,14 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<32; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 128; - } +#else + t[i] = &td[i * 128]; #endif - norm = t[0]; + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_64(norm, m); @@ -4220,7 +4228,7 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_2048_cond_sub_64(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -5112,11 +5120,10 @@ static void sp_2048_lshift_64(sp_digit* r, sp_digit* a, byte n) static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[128]; - sp_digit td[65]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[193]; #endif sp_digit* norm; sp_digit* tmp; @@ -5127,7 +5134,7 @@ static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 193, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { @@ -5136,12 +5143,11 @@ static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; - tmp = td + 128; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + tmp = td + 128; #else - norm = nd; - tmp = td; + tmp = &td[128]; #endif sp_2048_mont_setup(m, &mp); @@ -5201,7 +5207,7 @@ static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, sp_2048_cond_sub_64(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -8922,12 +8928,12 @@ static WC_INLINE int sp_3072_mod_48(sp_digit* r, const sp_digit* a, const sp_dig static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[16][96]; -#else - sp_digit* t[16]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[16 * 96]; #endif + sp_digit* t[16]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -8936,8 +8942,8 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 96, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 96), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -8945,12 +8951,14 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<16; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 96; - } +#else + t[i] = &td[i * 96]; #endif - norm = t[0]; + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_48(norm, m); @@ -9033,7 +9041,7 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_3072_cond_sub_48(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -9054,12 +9062,12 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][96]; -#else - sp_digit* t[32]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[32 * 96]; #endif + sp_digit* t[32]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -9068,8 +9076,8 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 96, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 96), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -9077,12 +9085,14 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<32; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 96; - } +#else + t[i] = &td[i * 96]; #endif - norm = t[0]; + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_48(norm, m); @@ -9182,7 +9192,7 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_3072_cond_sub_48(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -9760,12 +9770,12 @@ static WC_INLINE int sp_3072_mod_96_cond(sp_digit* r, const sp_digit* a, const s static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[16][192]; -#else - sp_digit* t[16]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[16 * 192]; #endif + sp_digit* t[16]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -9774,8 +9784,8 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 192, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 192), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -9783,12 +9793,14 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<16; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 192; - } +#else + t[i] = &td[i * 192]; #endif - norm = t[0]; + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_96(norm, m); @@ -9871,7 +9883,7 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_3072_cond_sub_96(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -9892,12 +9904,12 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][192]; -#else - sp_digit* t[32]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[32 * 192]; #endif + sp_digit* t[32]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -9906,8 +9918,8 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 192, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 192), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -9915,12 +9927,14 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<32; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 192; - } +#else + t[i] = &td[i * 192]; #endif - norm = t[0]; + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_96(norm, m); @@ -10020,7 +10034,7 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_3072_cond_sub_96(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -11110,11 +11124,10 @@ static void sp_3072_lshift_96(sp_digit* r, sp_digit* a, byte n) static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[192]; - sp_digit td[97]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[289]; #endif sp_digit* norm; sp_digit* tmp; @@ -11125,7 +11138,7 @@ static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 289, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { @@ -11134,12 +11147,11 @@ static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; - tmp = td + 192; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + tmp = td + 192; #else - norm = nd; - tmp = td; + tmp = &td[192]; #endif sp_3072_mont_setup(m, &mp); @@ -11199,7 +11211,7 @@ static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, sp_3072_cond_sub_96(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -14219,12 +14231,12 @@ static WC_INLINE int sp_4096_mod_128_cond(sp_digit* r, const sp_digit* a, const static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[16][256]; -#else - sp_digit* t[16]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[16 * 256]; #endif + sp_digit* t[16]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -14233,8 +14245,8 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 256, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 256), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -14242,12 +14254,14 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<16; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 256; - } +#else + t[i] = &td[i * 256]; #endif - norm = t[0]; + } sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_128(norm, m); @@ -14330,7 +14344,7 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e sp_4096_cond_sub_128(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -14351,12 +14365,12 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][256]; -#else - sp_digit* t[32]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[32 * 256]; #endif + sp_digit* t[32]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -14365,8 +14379,8 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 256, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 256), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -14374,12 +14388,14 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<32; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 256; - } +#else + t[i] = &td[i * 256]; #endif - norm = t[0]; + } sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_128(norm, m); @@ -14479,7 +14495,7 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e sp_4096_cond_sub_128(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -15766,11 +15782,10 @@ static void sp_4096_lshift_128(sp_digit* r, sp_digit* a, byte n) static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[256]; - sp_digit td[129]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[385]; #endif sp_digit* norm; sp_digit* tmp; @@ -15781,7 +15796,7 @@ static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 385, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { @@ -15790,12 +15805,11 @@ static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; - tmp = td + 256; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + tmp = td + 256; #else - norm = nd; - tmp = td; + tmp = &td[256]; #endif sp_4096_mont_setup(m, &mp); @@ -15855,7 +15869,7 @@ static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, sp_4096_cond_sub_128(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } diff --git a/wolfcrypt/src/sp_c32.c b/wolfcrypt/src/sp_c32.c index 72557cb35..a607adf15 100644 --- a/wolfcrypt/src/sp_c32.c +++ b/wolfcrypt/src/sp_c32.c @@ -1896,7 +1896,11 @@ static int sp_2048_mod_exp_45(sp_digit* r, const sp_digit* a, const sp_digit* e, const sp_digit* m, int reduceA) { #ifdef WOLFSSL_SP_SMALL +#if !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[3 * 90]; +#endif sp_digit* t[3]; sp_digit* norm; sp_digit mp = 1; @@ -1905,18 +1909,24 @@ static int sp_2048_mod_exp_45(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 45 * 2, NULL, +#if !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 45 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; } +#endif if (err == MP_OKAY) { - XMEMSET(td, 0, sizeof(*td) * 3U * 45U * 2U); - - norm = t[0] = td; - t[1] = &td[45 * 2]; - t[2] = &td[2 * 45 * 2]; + norm = td; + for (i=0; i<3; i++) { +#if !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 45 * 2); +#else + t[i] = &td[i * 45 * 2]; +#endif + XMEMSET(t[i], 0, sizeof(sp_digit) * 45U * 2U); + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_45(norm, m); @@ -1954,11 +1964,11 @@ static int sp_2048_mod_exp_45(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), - sizeof(*t[2]) * 45 * 2); + sizeof(*t[2]) * 45 * 2); sp_2048_mont_sqr_45(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), t[2], - sizeof(*t[2]) * 45 * 2); + sizeof(*t[2]) * 45 * 2); } sp_2048_mont_reduce_45(t[0], m, mp); @@ -1969,16 +1979,18 @@ static int sp_2048_mod_exp_45(sp_digit* r, const sp_digit* a, const sp_digit* e, } +#if !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } +#endif return err; #elif defined(WOLFSSL_SP_CACHE_RESISTANT) -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[90]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[90]; #endif sp_digit* t[3]; sp_digit* norm; @@ -1989,7 +2001,7 @@ static int sp_2048_mod_exp_45(sp_digit* r, const sp_digit* a, const sp_digit* e, int err = MP_OKAY; #ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 45 * 2, NULL, + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 45 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -1997,10 +2009,14 @@ static int sp_2048_mod_exp_45(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { - t[0] = td; - t[1] = &td[45 * 2]; - t[2] = &td[2 * 45 * 2]; - norm = t[0]; + norm = td; + for (i=0; i<3; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 45 * 2); +#else + t[i] = &td[i * 45 * 2)]; +#endif + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_45(norm, m); @@ -2038,17 +2054,19 @@ static int sp_2048_mod_exp_45(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_2048_mont_mul_45(t[y^1], t[0], t[1], m, mp); XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), + sizeof(*t[2]) * 45 * 2); sp_2048_mont_sqr_45(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), t[2], sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), t[2], + sizeof(*t[2]) * 45 * 2); } sp_2048_mont_reduce_45(t[0], m, mp); n = sp_2048_cmp_45(t[0], m); sp_2048_cond_sub_45(t[0], t[0], m, ((n < 0) ? - (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, t[0], sizeof(t[0])); + (sp_digit)1 : (sp_digit)0) - 1); + XMEMCPY(r, t[0], sizeof(*r) * 45 * 2); } #ifdef WOLFSSL_SMALL_STACK @@ -2059,22 +2077,22 @@ static int sp_2048_mod_exp_45(sp_digit* r, const sp_digit* a, const sp_digit* e, return err; #else -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[32][90]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[(32 * 90) + 90]; #endif sp_digit* t[32]; + sp_digit* rt; sp_digit* norm; - sp_digit rt[90]; sp_digit mp = 1; sp_digit n; int i; int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 90, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 90) + 90), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -2082,9 +2100,16 @@ static int sp_2048_mod_exp_45(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { + norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) for (i=0; i<32; i++) t[i] = td + i * 90; - norm = t[0]; + rt = td + 2880; +#else + for (i=0; i<32; i++) + t[i] = &td[i * 90]; + rt = &td[2880]; +#endif sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_45(norm, m); @@ -2154,7 +2179,7 @@ static int sp_2048_mod_exp_45(sp_digit* r, const sp_digit* a, const sp_digit* e, y = (n >> 27) & 0x1f; n <<= 5; c -= 5; - XMEMCPY(rt, t[y], sizeof(rt)); + XMEMCPY(rt, t[y], sizeof(sp_digit) * 90); for (; i>=0 || c>=5; ) { if (c < 5) { n |= e[i--] << (9 - c); @@ -2177,10 +2202,10 @@ static int sp_2048_mod_exp_45(sp_digit* r, const sp_digit* a, const sp_digit* e, n = sp_2048_cmp_45(rt, m); sp_2048_cond_sub_45(rt, rt, m, ((n < 0) ? (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, rt, sizeof(rt)); + XMEMCPY(r, rt, sizeof(sp_digit) * 90); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -2824,7 +2849,11 @@ static int sp_2048_mod_exp_90(sp_digit* r, const sp_digit* a, const sp_digit* e, const sp_digit* m, int reduceA) { #ifdef WOLFSSL_SP_SMALL +#if !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[3 * 180]; +#endif sp_digit* t[3]; sp_digit* norm; sp_digit mp = 1; @@ -2833,18 +2862,24 @@ static int sp_2048_mod_exp_90(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 90 * 2, NULL, +#if !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 90 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; } +#endif if (err == MP_OKAY) { - XMEMSET(td, 0, sizeof(*td) * 3U * 90U * 2U); - - norm = t[0] = td; - t[1] = &td[90 * 2]; - t[2] = &td[2 * 90 * 2]; + norm = td; + for (i=0; i<3; i++) { +#if !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 90 * 2); +#else + t[i] = &td[i * 90 * 2]; +#endif + XMEMSET(t[i], 0, sizeof(sp_digit) * 90U * 2U); + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_90(norm, m); @@ -2882,11 +2917,11 @@ static int sp_2048_mod_exp_90(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), - sizeof(*t[2]) * 90 * 2); + sizeof(*t[2]) * 90 * 2); sp_2048_mont_sqr_90(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), t[2], - sizeof(*t[2]) * 90 * 2); + sizeof(*t[2]) * 90 * 2); } sp_2048_mont_reduce_90(t[0], m, mp); @@ -2897,16 +2932,18 @@ static int sp_2048_mod_exp_90(sp_digit* r, const sp_digit* a, const sp_digit* e, } +#if !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } +#endif return err; #elif defined(WOLFSSL_SP_CACHE_RESISTANT) -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[180]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[180]; #endif sp_digit* t[3]; sp_digit* norm; @@ -2917,7 +2954,7 @@ static int sp_2048_mod_exp_90(sp_digit* r, const sp_digit* a, const sp_digit* e, int err = MP_OKAY; #ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 90 * 2, NULL, + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 90 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -2925,10 +2962,14 @@ static int sp_2048_mod_exp_90(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { - t[0] = td; - t[1] = &td[90 * 2]; - t[2] = &td[2 * 90 * 2]; - norm = t[0]; + norm = td; + for (i=0; i<3; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 90 * 2); +#else + t[i] = &td[i * 90 * 2)]; +#endif + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_90(norm, m); @@ -2966,17 +3007,19 @@ static int sp_2048_mod_exp_90(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_2048_mont_mul_90(t[y^1], t[0], t[1], m, mp); XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), + sizeof(*t[2]) * 90 * 2); sp_2048_mont_sqr_90(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), t[2], sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), t[2], + sizeof(*t[2]) * 90 * 2); } sp_2048_mont_reduce_90(t[0], m, mp); n = sp_2048_cmp_90(t[0], m); sp_2048_cond_sub_90(t[0], t[0], m, ((n < 0) ? - (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, t[0], sizeof(t[0])); + (sp_digit)1 : (sp_digit)0) - 1); + XMEMCPY(r, t[0], sizeof(*r) * 90 * 2); } #ifdef WOLFSSL_SMALL_STACK @@ -2987,22 +3030,22 @@ static int sp_2048_mod_exp_90(sp_digit* r, const sp_digit* a, const sp_digit* e, return err; #else -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[32][180]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[(32 * 180) + 180]; #endif sp_digit* t[32]; + sp_digit* rt; sp_digit* norm; - sp_digit rt[180]; sp_digit mp = 1; sp_digit n; int i; int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 180, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 180) + 180), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -3010,9 +3053,16 @@ static int sp_2048_mod_exp_90(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { + norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) for (i=0; i<32; i++) t[i] = td + i * 180; - norm = t[0]; + rt = td + 5760; +#else + for (i=0; i<32; i++) + t[i] = &td[i * 180]; + rt = &td[5760]; +#endif sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_90(norm, m); @@ -3082,7 +3132,7 @@ static int sp_2048_mod_exp_90(sp_digit* r, const sp_digit* a, const sp_digit* e, y = (n >> 27) & 0x1f; n <<= 5; c -= 5; - XMEMCPY(rt, t[y], sizeof(rt)); + XMEMCPY(rt, t[y], sizeof(sp_digit) * 180); for (; i>=0 || c>=5; ) { if (c < 5) { n |= e[i--] << (9 - c); @@ -3105,10 +3155,10 @@ static int sp_2048_mod_exp_90(sp_digit* r, const sp_digit* a, const sp_digit* e, n = sp_2048_cmp_90(rt, m); sp_2048_cond_sub_90(rt, rt, m, ((n < 0) ? (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, rt, sizeof(rt)); + XMEMCPY(r, rt, sizeof(sp_digit) * 180); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -4036,11 +4086,10 @@ SP_NOINLINE static void sp_2048_lshift_90(sp_digit* r, sp_digit* a, byte n) */ static int sp_2048_mod_exp_2_90(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[180]; - sp_digit td[91]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[271]; #endif sp_digit* norm; sp_digit* tmp; @@ -4050,7 +4099,7 @@ static int sp_2048_mod_exp_2_90(sp_digit* r, const sp_digit* e, int bits, const int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 271, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { @@ -4059,13 +4108,12 @@ static int sp_2048_mod_exp_2_90(sp_digit* r, const sp_digit* e, int bits, const #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) tmp = td + 180; XMEMSET(td, 0, sizeof(sp_digit) * 271); #else - norm = nd; - tmp = td; + tmp = &td[180]; XMEMSET(td, 0, sizeof(td)); #endif @@ -4124,7 +4172,7 @@ static int sp_2048_mod_exp_2_90(sp_digit* r, const sp_digit* e, int bits, const (sp_digit)1 : (sp_digit)0) - 1); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -5689,7 +5737,11 @@ static int sp_3072_mod_exp_67(sp_digit* r, const sp_digit* a, const sp_digit* e, const sp_digit* m, int reduceA) { #ifdef WOLFSSL_SP_SMALL +#if !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[3 * 134]; +#endif sp_digit* t[3]; sp_digit* norm; sp_digit mp = 1; @@ -5698,18 +5750,24 @@ static int sp_3072_mod_exp_67(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 67 * 2, NULL, +#if !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 67 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; } +#endif if (err == MP_OKAY) { - XMEMSET(td, 0, sizeof(*td) * 3U * 67U * 2U); - - norm = t[0] = td; - t[1] = &td[67 * 2]; - t[2] = &td[2 * 67 * 2]; + norm = td; + for (i=0; i<3; i++) { +#if !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 67 * 2); +#else + t[i] = &td[i * 67 * 2]; +#endif + XMEMSET(t[i], 0, sizeof(sp_digit) * 67U * 2U); + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_67(norm, m); @@ -5747,11 +5805,11 @@ static int sp_3072_mod_exp_67(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), - sizeof(*t[2]) * 67 * 2); + sizeof(*t[2]) * 67 * 2); sp_3072_mont_sqr_67(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), t[2], - sizeof(*t[2]) * 67 * 2); + sizeof(*t[2]) * 67 * 2); } sp_3072_mont_reduce_67(t[0], m, mp); @@ -5762,16 +5820,18 @@ static int sp_3072_mod_exp_67(sp_digit* r, const sp_digit* a, const sp_digit* e, } +#if !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } +#endif return err; #elif defined(WOLFSSL_SP_CACHE_RESISTANT) -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[134]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[134]; #endif sp_digit* t[3]; sp_digit* norm; @@ -5782,7 +5842,7 @@ static int sp_3072_mod_exp_67(sp_digit* r, const sp_digit* a, const sp_digit* e, int err = MP_OKAY; #ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 67 * 2, NULL, + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 67 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -5790,10 +5850,14 @@ static int sp_3072_mod_exp_67(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { - t[0] = td; - t[1] = &td[67 * 2]; - t[2] = &td[2 * 67 * 2]; - norm = t[0]; + norm = td; + for (i=0; i<3; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 67 * 2); +#else + t[i] = &td[i * 67 * 2)]; +#endif + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_67(norm, m); @@ -5831,17 +5895,19 @@ static int sp_3072_mod_exp_67(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_3072_mont_mul_67(t[y^1], t[0], t[1], m, mp); XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), + sizeof(*t[2]) * 67 * 2); sp_3072_mont_sqr_67(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), t[2], sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), t[2], + sizeof(*t[2]) * 67 * 2); } sp_3072_mont_reduce_67(t[0], m, mp); n = sp_3072_cmp_67(t[0], m); sp_3072_cond_sub_67(t[0], t[0], m, ((n < 0) ? - (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, t[0], sizeof(t[0])); + (sp_digit)1 : (sp_digit)0) - 1); + XMEMCPY(r, t[0], sizeof(*r) * 67 * 2); } #ifdef WOLFSSL_SMALL_STACK @@ -5852,22 +5918,22 @@ static int sp_3072_mod_exp_67(sp_digit* r, const sp_digit* a, const sp_digit* e, return err; #else -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[32][134]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[(32 * 134) + 134]; #endif sp_digit* t[32]; + sp_digit* rt; sp_digit* norm; - sp_digit rt[134]; sp_digit mp = 1; sp_digit n; int i; int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 134, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 134) + 134), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -5875,9 +5941,16 @@ static int sp_3072_mod_exp_67(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { + norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) for (i=0; i<32; i++) t[i] = td + i * 134; - norm = t[0]; + rt = td + 4288; +#else + for (i=0; i<32; i++) + t[i] = &td[i * 134]; + rt = &td[4288]; +#endif sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_67(norm, m); @@ -5947,7 +6020,7 @@ static int sp_3072_mod_exp_67(sp_digit* r, const sp_digit* a, const sp_digit* e, y = (n >> 27) & 0x1f; n <<= 5; c -= 5; - XMEMCPY(rt, t[y], sizeof(rt)); + XMEMCPY(rt, t[y], sizeof(sp_digit) * 134); for (; i>=0 || c>=5; ) { if (c < 5) { n |= e[i--] << (9 - c); @@ -5970,10 +6043,10 @@ static int sp_3072_mod_exp_67(sp_digit* r, const sp_digit* a, const sp_digit* e, n = sp_3072_cmp_67(rt, m); sp_3072_cond_sub_67(rt, rt, m, ((n < 0) ? (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, rt, sizeof(rt)); + XMEMCPY(r, rt, sizeof(sp_digit) * 134); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -6653,7 +6726,11 @@ static int sp_3072_mod_exp_134(sp_digit* r, const sp_digit* a, const sp_digit* e const sp_digit* m, int reduceA) { #ifdef WOLFSSL_SP_SMALL +#if !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[3 * 268]; +#endif sp_digit* t[3]; sp_digit* norm; sp_digit mp = 1; @@ -6662,18 +6739,24 @@ static int sp_3072_mod_exp_134(sp_digit* r, const sp_digit* a, const sp_digit* e int c, y; int err = MP_OKAY; - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 134 * 2, NULL, +#if !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 134 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; } +#endif if (err == MP_OKAY) { - XMEMSET(td, 0, sizeof(*td) * 3U * 134U * 2U); - - norm = t[0] = td; - t[1] = &td[134 * 2]; - t[2] = &td[2 * 134 * 2]; + norm = td; + for (i=0; i<3; i++) { +#if !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 134 * 2); +#else + t[i] = &td[i * 134 * 2]; +#endif + XMEMSET(t[i], 0, sizeof(sp_digit) * 134U * 2U); + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_134(norm, m); @@ -6711,11 +6794,11 @@ static int sp_3072_mod_exp_134(sp_digit* r, const sp_digit* a, const sp_digit* e XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), - sizeof(*t[2]) * 134 * 2); + sizeof(*t[2]) * 134 * 2); sp_3072_mont_sqr_134(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), t[2], - sizeof(*t[2]) * 134 * 2); + sizeof(*t[2]) * 134 * 2); } sp_3072_mont_reduce_134(t[0], m, mp); @@ -6726,16 +6809,18 @@ static int sp_3072_mod_exp_134(sp_digit* r, const sp_digit* a, const sp_digit* e } +#if !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } +#endif return err; #elif defined(WOLFSSL_SP_CACHE_RESISTANT) -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[268]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[268]; #endif sp_digit* t[3]; sp_digit* norm; @@ -6746,7 +6831,7 @@ static int sp_3072_mod_exp_134(sp_digit* r, const sp_digit* a, const sp_digit* e int err = MP_OKAY; #ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 134 * 2, NULL, + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 134 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -6754,10 +6839,14 @@ static int sp_3072_mod_exp_134(sp_digit* r, const sp_digit* a, const sp_digit* e #endif if (err == MP_OKAY) { - t[0] = td; - t[1] = &td[134 * 2]; - t[2] = &td[2 * 134 * 2]; - norm = t[0]; + norm = td; + for (i=0; i<3; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 134 * 2); +#else + t[i] = &td[i * 134 * 2)]; +#endif + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_134(norm, m); @@ -6795,17 +6884,19 @@ static int sp_3072_mod_exp_134(sp_digit* r, const sp_digit* a, const sp_digit* e sp_3072_mont_mul_134(t[y^1], t[0], t[1], m, mp); XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), + sizeof(*t[2]) * 134 * 2); sp_3072_mont_sqr_134(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), t[2], sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), t[2], + sizeof(*t[2]) * 134 * 2); } sp_3072_mont_reduce_134(t[0], m, mp); n = sp_3072_cmp_134(t[0], m); sp_3072_cond_sub_134(t[0], t[0], m, ((n < 0) ? - (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, t[0], sizeof(t[0])); + (sp_digit)1 : (sp_digit)0) - 1); + XMEMCPY(r, t[0], sizeof(*r) * 134 * 2); } #ifdef WOLFSSL_SMALL_STACK @@ -6816,22 +6907,22 @@ static int sp_3072_mod_exp_134(sp_digit* r, const sp_digit* a, const sp_digit* e return err; #else -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[32][268]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[(32 * 268) + 268]; #endif sp_digit* t[32]; + sp_digit* rt; sp_digit* norm; - sp_digit rt[268]; sp_digit mp = 1; sp_digit n; int i; int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 268, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 268) + 268), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -6839,9 +6930,16 @@ static int sp_3072_mod_exp_134(sp_digit* r, const sp_digit* a, const sp_digit* e #endif if (err == MP_OKAY) { + norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) for (i=0; i<32; i++) t[i] = td + i * 268; - norm = t[0]; + rt = td + 8576; +#else + for (i=0; i<32; i++) + t[i] = &td[i * 268]; + rt = &td[8576]; +#endif sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_134(norm, m); @@ -6911,7 +7009,7 @@ static int sp_3072_mod_exp_134(sp_digit* r, const sp_digit* a, const sp_digit* e y = (n >> 27) & 0x1f; n <<= 5; c -= 5; - XMEMCPY(rt, t[y], sizeof(rt)); + XMEMCPY(rt, t[y], sizeof(sp_digit) * 268); for (; i>=0 || c>=5; ) { if (c < 5) { n |= e[i--] << (9 - c); @@ -6934,10 +7032,10 @@ static int sp_3072_mod_exp_134(sp_digit* r, const sp_digit* a, const sp_digit* e n = sp_3072_cmp_134(rt, m); sp_3072_cond_sub_134(rt, rt, m, ((n < 0) ? (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, rt, sizeof(rt)); + XMEMCPY(r, rt, sizeof(sp_digit) * 268); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -7953,11 +8051,10 @@ SP_NOINLINE static void sp_3072_lshift_134(sp_digit* r, sp_digit* a, byte n) */ static int sp_3072_mod_exp_2_134(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[268]; - sp_digit td[135]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[403]; #endif sp_digit* norm; sp_digit* tmp; @@ -7967,7 +8064,7 @@ static int sp_3072_mod_exp_2_134(sp_digit* r, const sp_digit* e, int bits, const int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 403, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { @@ -7976,13 +8073,12 @@ static int sp_3072_mod_exp_2_134(sp_digit* r, const sp_digit* e, int bits, const #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) tmp = td + 268; XMEMSET(td, 0, sizeof(sp_digit) * 403); #else - norm = nd; - tmp = td; + tmp = &td[268]; XMEMSET(td, 0, sizeof(td)); #endif @@ -8041,7 +8137,7 @@ static int sp_3072_mod_exp_2_134(sp_digit* r, const sp_digit* e, int bits, const (sp_digit)1 : (sp_digit)0) - 1); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -9705,7 +9801,11 @@ static int sp_4096_mod_exp_98(sp_digit* r, const sp_digit* a, const sp_digit* e, const sp_digit* m, int reduceA) { #ifdef WOLFSSL_SP_SMALL +#if !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[3 * 196]; +#endif sp_digit* t[3]; sp_digit* norm; sp_digit mp = 1; @@ -9714,18 +9814,24 @@ static int sp_4096_mod_exp_98(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 98 * 2, NULL, +#if !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 98 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; } +#endif if (err == MP_OKAY) { - XMEMSET(td, 0, sizeof(*td) * 3U * 98U * 2U); - - norm = t[0] = td; - t[1] = &td[98 * 2]; - t[2] = &td[2 * 98 * 2]; + norm = td; + for (i=0; i<3; i++) { +#if !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 98 * 2); +#else + t[i] = &td[i * 98 * 2]; +#endif + XMEMSET(t[i], 0, sizeof(sp_digit) * 98U * 2U); + } sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_98(norm, m); @@ -9763,11 +9869,11 @@ static int sp_4096_mod_exp_98(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), - sizeof(*t[2]) * 98 * 2); + sizeof(*t[2]) * 98 * 2); sp_4096_mont_sqr_98(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), t[2], - sizeof(*t[2]) * 98 * 2); + sizeof(*t[2]) * 98 * 2); } sp_4096_mont_reduce_98(t[0], m, mp); @@ -9778,16 +9884,18 @@ static int sp_4096_mod_exp_98(sp_digit* r, const sp_digit* a, const sp_digit* e, } +#if !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } +#endif return err; #elif defined(WOLFSSL_SP_CACHE_RESISTANT) -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[196]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[196]; #endif sp_digit* t[3]; sp_digit* norm; @@ -9798,7 +9906,7 @@ static int sp_4096_mod_exp_98(sp_digit* r, const sp_digit* a, const sp_digit* e, int err = MP_OKAY; #ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 98 * 2, NULL, + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 98 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -9806,10 +9914,14 @@ static int sp_4096_mod_exp_98(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { - t[0] = td; - t[1] = &td[98 * 2]; - t[2] = &td[2 * 98 * 2]; - norm = t[0]; + norm = td; + for (i=0; i<3; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 98 * 2); +#else + t[i] = &td[i * 98 * 2)]; +#endif + } sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_98(norm, m); @@ -9847,17 +9959,19 @@ static int sp_4096_mod_exp_98(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_4096_mont_mul_98(t[y^1], t[0], t[1], m, mp); XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), + sizeof(*t[2]) * 98 * 2); sp_4096_mont_sqr_98(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), t[2], sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), t[2], + sizeof(*t[2]) * 98 * 2); } sp_4096_mont_reduce_98(t[0], m, mp); n = sp_4096_cmp_98(t[0], m); sp_4096_cond_sub_98(t[0], t[0], m, ((n < 0) ? - (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, t[0], sizeof(t[0])); + (sp_digit)1 : (sp_digit)0) - 1); + XMEMCPY(r, t[0], sizeof(*r) * 98 * 2); } #ifdef WOLFSSL_SMALL_STACK @@ -9868,22 +9982,22 @@ static int sp_4096_mod_exp_98(sp_digit* r, const sp_digit* a, const sp_digit* e, return err; #else -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[32][196]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[(32 * 196) + 196]; #endif sp_digit* t[32]; + sp_digit* rt; sp_digit* norm; - sp_digit rt[196]; sp_digit mp = 1; sp_digit n; int i; int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 196, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 196) + 196), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -9891,9 +10005,16 @@ static int sp_4096_mod_exp_98(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { + norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) for (i=0; i<32; i++) t[i] = td + i * 196; - norm = t[0]; + rt = td + 6272; +#else + for (i=0; i<32; i++) + t[i] = &td[i * 196]; + rt = &td[6272]; +#endif sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_98(norm, m); @@ -9963,7 +10084,7 @@ static int sp_4096_mod_exp_98(sp_digit* r, const sp_digit* a, const sp_digit* e, y = (n >> 27) & 0x1f; n <<= 5; c -= 5; - XMEMCPY(rt, t[y], sizeof(rt)); + XMEMCPY(rt, t[y], sizeof(sp_digit) * 196); for (; i>=0 || c>=5; ) { if (c < 5) { n |= e[i--] << (11 - c); @@ -9986,10 +10107,10 @@ static int sp_4096_mod_exp_98(sp_digit* r, const sp_digit* a, const sp_digit* e, n = sp_4096_cmp_98(rt, m); sp_4096_cond_sub_98(rt, rt, m, ((n < 0) ? (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, rt, sizeof(rt)); + XMEMCPY(r, rt, sizeof(sp_digit) * 196); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -10646,7 +10767,11 @@ static int sp_4096_mod_exp_196(sp_digit* r, const sp_digit* a, const sp_digit* e const sp_digit* m, int reduceA) { #ifdef WOLFSSL_SP_SMALL +#if !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[3 * 392]; +#endif sp_digit* t[3]; sp_digit* norm; sp_digit mp = 1; @@ -10655,18 +10780,24 @@ static int sp_4096_mod_exp_196(sp_digit* r, const sp_digit* a, const sp_digit* e int c, y; int err = MP_OKAY; - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 196 * 2, NULL, +#if !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 196 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; } +#endif if (err == MP_OKAY) { - XMEMSET(td, 0, sizeof(*td) * 3U * 196U * 2U); - - norm = t[0] = td; - t[1] = &td[196 * 2]; - t[2] = &td[2 * 196 * 2]; + norm = td; + for (i=0; i<3; i++) { +#if !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 196 * 2); +#else + t[i] = &td[i * 196 * 2]; +#endif + XMEMSET(t[i], 0, sizeof(sp_digit) * 196U * 2U); + } sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_196(norm, m); @@ -10704,11 +10835,11 @@ static int sp_4096_mod_exp_196(sp_digit* r, const sp_digit* a, const sp_digit* e XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), - sizeof(*t[2]) * 196 * 2); + sizeof(*t[2]) * 196 * 2); sp_4096_mont_sqr_196(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), t[2], - sizeof(*t[2]) * 196 * 2); + sizeof(*t[2]) * 196 * 2); } sp_4096_mont_reduce_196(t[0], m, mp); @@ -10719,16 +10850,18 @@ static int sp_4096_mod_exp_196(sp_digit* r, const sp_digit* a, const sp_digit* e } +#if !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } +#endif return err; #elif defined(WOLFSSL_SP_CACHE_RESISTANT) -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[392]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[392]; #endif sp_digit* t[3]; sp_digit* norm; @@ -10739,7 +10872,7 @@ static int sp_4096_mod_exp_196(sp_digit* r, const sp_digit* a, const sp_digit* e int err = MP_OKAY; #ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 196 * 2, NULL, + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 196 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -10747,10 +10880,14 @@ static int sp_4096_mod_exp_196(sp_digit* r, const sp_digit* a, const sp_digit* e #endif if (err == MP_OKAY) { - t[0] = td; - t[1] = &td[196 * 2]; - t[2] = &td[2 * 196 * 2]; - norm = t[0]; + norm = td; + for (i=0; i<3; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 196 * 2); +#else + t[i] = &td[i * 196 * 2)]; +#endif + } sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_196(norm, m); @@ -10788,17 +10925,19 @@ static int sp_4096_mod_exp_196(sp_digit* r, const sp_digit* a, const sp_digit* e sp_4096_mont_mul_196(t[y^1], t[0], t[1], m, mp); XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), + sizeof(*t[2]) * 196 * 2); sp_4096_mont_sqr_196(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), t[2], sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), t[2], + sizeof(*t[2]) * 196 * 2); } sp_4096_mont_reduce_196(t[0], m, mp); n = sp_4096_cmp_196(t[0], m); sp_4096_cond_sub_196(t[0], t[0], m, ((n < 0) ? - (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, t[0], sizeof(t[0])); + (sp_digit)1 : (sp_digit)0) - 1); + XMEMCPY(r, t[0], sizeof(*r) * 196 * 2); } #ifdef WOLFSSL_SMALL_STACK @@ -10809,22 +10948,22 @@ static int sp_4096_mod_exp_196(sp_digit* r, const sp_digit* a, const sp_digit* e return err; #else -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[32][392]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[(32 * 392) + 392]; #endif sp_digit* t[32]; + sp_digit* rt; sp_digit* norm; - sp_digit rt[392]; sp_digit mp = 1; sp_digit n; int i; int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 392, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 392) + 392), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -10832,9 +10971,16 @@ static int sp_4096_mod_exp_196(sp_digit* r, const sp_digit* a, const sp_digit* e #endif if (err == MP_OKAY) { + norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) for (i=0; i<32; i++) t[i] = td + i * 392; - norm = t[0]; + rt = td + 12544; +#else + for (i=0; i<32; i++) + t[i] = &td[i * 392]; + rt = &td[12544]; +#endif sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_196(norm, m); @@ -10904,7 +11050,7 @@ static int sp_4096_mod_exp_196(sp_digit* r, const sp_digit* a, const sp_digit* e y = (n >> 27) & 0x1f; n <<= 5; c -= 5; - XMEMCPY(rt, t[y], sizeof(rt)); + XMEMCPY(rt, t[y], sizeof(sp_digit) * 392); for (; i>=0 || c>=5; ) { if (c < 5) { n |= e[i--] << (11 - c); @@ -10927,10 +11073,10 @@ static int sp_4096_mod_exp_196(sp_digit* r, const sp_digit* a, const sp_digit* e n = sp_4096_cmp_196(rt, m); sp_4096_cond_sub_196(rt, rt, m, ((n < 0) ? (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, rt, sizeof(rt)); + XMEMCPY(r, rt, sizeof(sp_digit) * 392); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -12070,11 +12216,10 @@ SP_NOINLINE static void sp_4096_lshift_196(sp_digit* r, sp_digit* a, byte n) */ static int sp_4096_mod_exp_2_196(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[392]; - sp_digit td[197]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[589]; #endif sp_digit* norm; sp_digit* tmp; @@ -12084,7 +12229,7 @@ static int sp_4096_mod_exp_2_196(sp_digit* r, const sp_digit* e, int bits, const int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 589, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { @@ -12093,13 +12238,12 @@ static int sp_4096_mod_exp_2_196(sp_digit* r, const sp_digit* e, int bits, const #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) tmp = td + 392; XMEMSET(td, 0, sizeof(sp_digit) * 589); #else - norm = nd; - tmp = td; + tmp = &td[392]; XMEMSET(td, 0, sizeof(td)); #endif @@ -12158,7 +12302,7 @@ static int sp_4096_mod_exp_2_196(sp_digit* r, const sp_digit* e, int bits, const (sp_digit)1 : (sp_digit)0) - 1); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } diff --git a/wolfcrypt/src/sp_c64.c b/wolfcrypt/src/sp_c64.c index 1e49826ce..f956f86ed 100644 --- a/wolfcrypt/src/sp_c64.c +++ b/wolfcrypt/src/sp_c64.c @@ -1544,7 +1544,11 @@ static int sp_2048_mod_exp_18(sp_digit* r, const sp_digit* a, const sp_digit* e, const sp_digit* m, int reduceA) { #ifdef WOLFSSL_SP_SMALL +#if !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[3 * 36]; +#endif sp_digit* t[3]; sp_digit* norm; sp_digit mp = 1; @@ -1553,18 +1557,24 @@ static int sp_2048_mod_exp_18(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 18 * 2, NULL, +#if !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 18 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; } +#endif if (err == MP_OKAY) { - XMEMSET(td, 0, sizeof(*td) * 3U * 18U * 2U); - - norm = t[0] = td; - t[1] = &td[18 * 2]; - t[2] = &td[2 * 18 * 2]; + norm = td; + for (i=0; i<3; i++) { +#if !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 18 * 2); +#else + t[i] = &td[i * 18 * 2]; +#endif + XMEMSET(t[i], 0, sizeof(sp_digit) * 18U * 2U); + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_18(norm, m); @@ -1602,11 +1612,11 @@ static int sp_2048_mod_exp_18(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), - sizeof(*t[2]) * 18 * 2); + sizeof(*t[2]) * 18 * 2); sp_2048_mont_sqr_18(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), t[2], - sizeof(*t[2]) * 18 * 2); + sizeof(*t[2]) * 18 * 2); } sp_2048_mont_reduce_18(t[0], m, mp); @@ -1617,16 +1627,18 @@ static int sp_2048_mod_exp_18(sp_digit* r, const sp_digit* a, const sp_digit* e, } +#if !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } +#endif return err; #elif defined(WOLFSSL_SP_CACHE_RESISTANT) -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[36]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[36]; #endif sp_digit* t[3]; sp_digit* norm; @@ -1637,7 +1649,7 @@ static int sp_2048_mod_exp_18(sp_digit* r, const sp_digit* a, const sp_digit* e, int err = MP_OKAY; #ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 18 * 2, NULL, + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 18 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -1645,10 +1657,14 @@ static int sp_2048_mod_exp_18(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { - t[0] = td; - t[1] = &td[18 * 2]; - t[2] = &td[2 * 18 * 2]; - norm = t[0]; + norm = td; + for (i=0; i<3; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 18 * 2); +#else + t[i] = &td[i * 18 * 2)]; +#endif + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_18(norm, m); @@ -1686,17 +1702,19 @@ static int sp_2048_mod_exp_18(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_2048_mont_mul_18(t[y^1], t[0], t[1], m, mp); XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), + sizeof(*t[2]) * 18 * 2); sp_2048_mont_sqr_18(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), t[2], sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), t[2], + sizeof(*t[2]) * 18 * 2); } sp_2048_mont_reduce_18(t[0], m, mp); n = sp_2048_cmp_18(t[0], m); sp_2048_cond_sub_18(t[0], t[0], m, ((n < 0) ? - (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, t[0], sizeof(t[0])); + (sp_digit)1 : (sp_digit)0) - 1); + XMEMCPY(r, t[0], sizeof(*r) * 18 * 2); } #ifdef WOLFSSL_SMALL_STACK @@ -1707,22 +1725,22 @@ static int sp_2048_mod_exp_18(sp_digit* r, const sp_digit* a, const sp_digit* e, return err; #else -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[32][36]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[(32 * 36) + 36]; #endif sp_digit* t[32]; + sp_digit* rt; sp_digit* norm; - sp_digit rt[36]; sp_digit mp = 1; sp_digit n; int i; int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 36, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 36) + 36), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -1730,9 +1748,16 @@ static int sp_2048_mod_exp_18(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { + norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) for (i=0; i<32; i++) t[i] = td + i * 36; - norm = t[0]; + rt = td + 1152; +#else + for (i=0; i<32; i++) + t[i] = &td[i * 36]; + rt = &td[1152]; +#endif sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_18(norm, m); @@ -1802,7 +1827,7 @@ static int sp_2048_mod_exp_18(sp_digit* r, const sp_digit* a, const sp_digit* e, y = (n >> 59) & 0x1f; n <<= 5; c -= 5; - XMEMCPY(rt, t[y], sizeof(rt)); + XMEMCPY(rt, t[y], sizeof(sp_digit) * 36); for (; i>=0 || c>=5; ) { if (c < 5) { n |= e[i--] << (7 - c); @@ -1825,10 +1850,10 @@ static int sp_2048_mod_exp_18(sp_digit* r, const sp_digit* a, const sp_digit* e, n = sp_2048_cmp_18(rt, m); sp_2048_cond_sub_18(rt, rt, m, ((n < 0) ? (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, rt, sizeof(rt)); + XMEMCPY(r, rt, sizeof(sp_digit) * 36); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -2464,7 +2489,11 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, const sp_digit* m, int reduceA) { #ifdef WOLFSSL_SP_SMALL +#if !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[3 * 72]; +#endif sp_digit* t[3]; sp_digit* norm; sp_digit mp = 1; @@ -2473,18 +2502,24 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 36 * 2, NULL, +#if !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 36 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; } +#endif if (err == MP_OKAY) { - XMEMSET(td, 0, sizeof(*td) * 3U * 36U * 2U); - - norm = t[0] = td; - t[1] = &td[36 * 2]; - t[2] = &td[2 * 36 * 2]; + norm = td; + for (i=0; i<3; i++) { +#if !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 36 * 2); +#else + t[i] = &td[i * 36 * 2]; +#endif + XMEMSET(t[i], 0, sizeof(sp_digit) * 36U * 2U); + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_36(norm, m); @@ -2522,11 +2557,11 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), - sizeof(*t[2]) * 36 * 2); + sizeof(*t[2]) * 36 * 2); sp_2048_mont_sqr_36(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), t[2], - sizeof(*t[2]) * 36 * 2); + sizeof(*t[2]) * 36 * 2); } sp_2048_mont_reduce_36(t[0], m, mp); @@ -2537,16 +2572,18 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, } +#if !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } +#endif return err; #elif defined(WOLFSSL_SP_CACHE_RESISTANT) -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[72]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[72]; #endif sp_digit* t[3]; sp_digit* norm; @@ -2557,7 +2594,7 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, int err = MP_OKAY; #ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 36 * 2, NULL, + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 36 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -2565,10 +2602,14 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { - t[0] = td; - t[1] = &td[36 * 2]; - t[2] = &td[2 * 36 * 2]; - norm = t[0]; + norm = td; + for (i=0; i<3; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 36 * 2); +#else + t[i] = &td[i * 36 * 2)]; +#endif + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_36(norm, m); @@ -2606,17 +2647,19 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_2048_mont_mul_36(t[y^1], t[0], t[1], m, mp); XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), + sizeof(*t[2]) * 36 * 2); sp_2048_mont_sqr_36(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), t[2], sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), t[2], + sizeof(*t[2]) * 36 * 2); } sp_2048_mont_reduce_36(t[0], m, mp); n = sp_2048_cmp_36(t[0], m); sp_2048_cond_sub_36(t[0], t[0], m, ((n < 0) ? - (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, t[0], sizeof(t[0])); + (sp_digit)1 : (sp_digit)0) - 1); + XMEMCPY(r, t[0], sizeof(*r) * 36 * 2); } #ifdef WOLFSSL_SMALL_STACK @@ -2627,22 +2670,22 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, return err; #else -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[32][72]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[(32 * 72) + 72]; #endif sp_digit* t[32]; + sp_digit* rt; sp_digit* norm; - sp_digit rt[72]; sp_digit mp = 1; sp_digit n; int i; int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 72, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 72) + 72), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -2650,9 +2693,16 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { + norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) for (i=0; i<32; i++) t[i] = td + i * 72; - norm = t[0]; + rt = td + 2304; +#else + for (i=0; i<32; i++) + t[i] = &td[i * 72]; + rt = &td[2304]; +#endif sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_36(norm, m); @@ -2722,7 +2772,7 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, y = (n >> 59) & 0x1f; n <<= 5; c -= 5; - XMEMCPY(rt, t[y], sizeof(rt)); + XMEMCPY(rt, t[y], sizeof(sp_digit) * 72); for (; i>=0 || c>=5; ) { if (c < 5) { n |= e[i--] << (7 - c); @@ -2745,10 +2795,10 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, n = sp_2048_cmp_36(rt, m); sp_2048_cond_sub_36(rt, rt, m, ((n < 0) ? (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, rt, sizeof(rt)); + XMEMCPY(r, rt, sizeof(sp_digit) * 72); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -3568,11 +3618,10 @@ SP_NOINLINE static void sp_2048_lshift_36(sp_digit* r, sp_digit* a, byte n) */ static int sp_2048_mod_exp_2_36(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[72]; - sp_digit td[37]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[109]; #endif sp_digit* norm; sp_digit* tmp; @@ -3582,7 +3631,7 @@ static int sp_2048_mod_exp_2_36(sp_digit* r, const sp_digit* e, int bits, const int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 109, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { @@ -3591,13 +3640,12 @@ static int sp_2048_mod_exp_2_36(sp_digit* r, const sp_digit* e, int bits, const #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) tmp = td + 72; XMEMSET(td, 0, sizeof(sp_digit) * 109); #else - norm = nd; - tmp = td; + tmp = &td[72]; XMEMSET(td, 0, sizeof(td)); #endif @@ -3657,7 +3705,7 @@ static int sp_2048_mod_exp_2_36(sp_digit* r, const sp_digit* e, int bits, const (sp_digit)1 : (sp_digit)0) - 1); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -5677,7 +5725,11 @@ static int sp_3072_mod_exp_27(sp_digit* r, const sp_digit* a, const sp_digit* e, const sp_digit* m, int reduceA) { #ifdef WOLFSSL_SP_SMALL +#if !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[3 * 54]; +#endif sp_digit* t[3]; sp_digit* norm; sp_digit mp = 1; @@ -5686,18 +5738,24 @@ static int sp_3072_mod_exp_27(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 27 * 2, NULL, +#if !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 27 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; } +#endif if (err == MP_OKAY) { - XMEMSET(td, 0, sizeof(*td) * 3U * 27U * 2U); - - norm = t[0] = td; - t[1] = &td[27 * 2]; - t[2] = &td[2 * 27 * 2]; + norm = td; + for (i=0; i<3; i++) { +#if !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 27 * 2); +#else + t[i] = &td[i * 27 * 2]; +#endif + XMEMSET(t[i], 0, sizeof(sp_digit) * 27U * 2U); + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_27(norm, m); @@ -5735,11 +5793,11 @@ static int sp_3072_mod_exp_27(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), - sizeof(*t[2]) * 27 * 2); + sizeof(*t[2]) * 27 * 2); sp_3072_mont_sqr_27(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), t[2], - sizeof(*t[2]) * 27 * 2); + sizeof(*t[2]) * 27 * 2); } sp_3072_mont_reduce_27(t[0], m, mp); @@ -5750,16 +5808,18 @@ static int sp_3072_mod_exp_27(sp_digit* r, const sp_digit* a, const sp_digit* e, } +#if !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } +#endif return err; #elif defined(WOLFSSL_SP_CACHE_RESISTANT) -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[54]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[54]; #endif sp_digit* t[3]; sp_digit* norm; @@ -5770,7 +5830,7 @@ static int sp_3072_mod_exp_27(sp_digit* r, const sp_digit* a, const sp_digit* e, int err = MP_OKAY; #ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 27 * 2, NULL, + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 27 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -5778,10 +5838,14 @@ static int sp_3072_mod_exp_27(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { - t[0] = td; - t[1] = &td[27 * 2]; - t[2] = &td[2 * 27 * 2]; - norm = t[0]; + norm = td; + for (i=0; i<3; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 27 * 2); +#else + t[i] = &td[i * 27 * 2)]; +#endif + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_27(norm, m); @@ -5819,17 +5883,19 @@ static int sp_3072_mod_exp_27(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_3072_mont_mul_27(t[y^1], t[0], t[1], m, mp); XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), + sizeof(*t[2]) * 27 * 2); sp_3072_mont_sqr_27(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), t[2], sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), t[2], + sizeof(*t[2]) * 27 * 2); } sp_3072_mont_reduce_27(t[0], m, mp); n = sp_3072_cmp_27(t[0], m); sp_3072_cond_sub_27(t[0], t[0], m, ((n < 0) ? - (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, t[0], sizeof(t[0])); + (sp_digit)1 : (sp_digit)0) - 1); + XMEMCPY(r, t[0], sizeof(*r) * 27 * 2); } #ifdef WOLFSSL_SMALL_STACK @@ -5840,22 +5906,22 @@ static int sp_3072_mod_exp_27(sp_digit* r, const sp_digit* a, const sp_digit* e, return err; #else -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[32][54]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[(32 * 54) + 54]; #endif sp_digit* t[32]; + sp_digit* rt; sp_digit* norm; - sp_digit rt[54]; sp_digit mp = 1; sp_digit n; int i; int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 54, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 54) + 54), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -5863,9 +5929,16 @@ static int sp_3072_mod_exp_27(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { + norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) for (i=0; i<32; i++) t[i] = td + i * 54; - norm = t[0]; + rt = td + 1728; +#else + for (i=0; i<32; i++) + t[i] = &td[i * 54]; + rt = &td[1728]; +#endif sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_27(norm, m); @@ -5935,7 +6008,7 @@ static int sp_3072_mod_exp_27(sp_digit* r, const sp_digit* a, const sp_digit* e, y = (n >> 59) & 0x1f; n <<= 5; c -= 5; - XMEMCPY(rt, t[y], sizeof(rt)); + XMEMCPY(rt, t[y], sizeof(sp_digit) * 54); for (; i>=0 || c>=5; ) { if (c < 5) { n |= e[i--] << (7 - c); @@ -5958,10 +6031,10 @@ static int sp_3072_mod_exp_27(sp_digit* r, const sp_digit* a, const sp_digit* e, n = sp_3072_cmp_27(rt, m); sp_3072_cond_sub_27(rt, rt, m, ((n < 0) ? (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, rt, sizeof(rt)); + XMEMCPY(r, rt, sizeof(sp_digit) * 54); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -6567,7 +6640,11 @@ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e, const sp_digit* m, int reduceA) { #ifdef WOLFSSL_SP_SMALL +#if !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[3 * 108]; +#endif sp_digit* t[3]; sp_digit* norm; sp_digit mp = 1; @@ -6576,18 +6653,24 @@ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 54 * 2, NULL, +#if !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 54 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; } +#endif if (err == MP_OKAY) { - XMEMSET(td, 0, sizeof(*td) * 3U * 54U * 2U); - - norm = t[0] = td; - t[1] = &td[54 * 2]; - t[2] = &td[2 * 54 * 2]; + norm = td; + for (i=0; i<3; i++) { +#if !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 54 * 2); +#else + t[i] = &td[i * 54 * 2]; +#endif + XMEMSET(t[i], 0, sizeof(sp_digit) * 54U * 2U); + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_54(norm, m); @@ -6625,11 +6708,11 @@ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), - sizeof(*t[2]) * 54 * 2); + sizeof(*t[2]) * 54 * 2); sp_3072_mont_sqr_54(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), t[2], - sizeof(*t[2]) * 54 * 2); + sizeof(*t[2]) * 54 * 2); } sp_3072_mont_reduce_54(t[0], m, mp); @@ -6640,16 +6723,18 @@ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e, } +#if !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } +#endif return err; #elif defined(WOLFSSL_SP_CACHE_RESISTANT) -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[108]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[108]; #endif sp_digit* t[3]; sp_digit* norm; @@ -6660,7 +6745,7 @@ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e, int err = MP_OKAY; #ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 54 * 2, NULL, + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 54 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -6668,10 +6753,14 @@ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { - t[0] = td; - t[1] = &td[54 * 2]; - t[2] = &td[2 * 54 * 2]; - norm = t[0]; + norm = td; + for (i=0; i<3; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 54 * 2); +#else + t[i] = &td[i * 54 * 2)]; +#endif + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_54(norm, m); @@ -6709,17 +6798,19 @@ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_3072_mont_mul_54(t[y^1], t[0], t[1], m, mp); XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), + sizeof(*t[2]) * 54 * 2); sp_3072_mont_sqr_54(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), t[2], sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), t[2], + sizeof(*t[2]) * 54 * 2); } sp_3072_mont_reduce_54(t[0], m, mp); n = sp_3072_cmp_54(t[0], m); sp_3072_cond_sub_54(t[0], t[0], m, ((n < 0) ? - (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, t[0], sizeof(t[0])); + (sp_digit)1 : (sp_digit)0) - 1); + XMEMCPY(r, t[0], sizeof(*r) * 54 * 2); } #ifdef WOLFSSL_SMALL_STACK @@ -6730,22 +6821,22 @@ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e, return err; #else -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[32][108]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[(32 * 108) + 108]; #endif sp_digit* t[32]; + sp_digit* rt; sp_digit* norm; - sp_digit rt[108]; sp_digit mp = 1; sp_digit n; int i; int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 108, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 108) + 108), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -6753,9 +6844,16 @@ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { + norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) for (i=0; i<32; i++) t[i] = td + i * 108; - norm = t[0]; + rt = td + 3456; +#else + for (i=0; i<32; i++) + t[i] = &td[i * 108]; + rt = &td[3456]; +#endif sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_54(norm, m); @@ -6825,7 +6923,7 @@ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e, y = (n >> 59) & 0x1f; n <<= 5; c -= 5; - XMEMCPY(rt, t[y], sizeof(rt)); + XMEMCPY(rt, t[y], sizeof(sp_digit) * 108); for (; i>=0 || c>=5; ) { if (c < 5) { n |= e[i--] << (7 - c); @@ -6848,10 +6946,10 @@ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e, n = sp_3072_cmp_54(rt, m); sp_3072_cond_sub_54(rt, rt, m, ((n < 0) ? (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, rt, sizeof(rt)); + XMEMCPY(r, rt, sizeof(sp_digit) * 108); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -7707,11 +7805,10 @@ SP_NOINLINE static void sp_3072_lshift_54(sp_digit* r, sp_digit* a, byte n) */ static int sp_3072_mod_exp_2_54(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[108]; - sp_digit td[55]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[163]; #endif sp_digit* norm; sp_digit* tmp; @@ -7721,7 +7818,7 @@ static int sp_3072_mod_exp_2_54(sp_digit* r, const sp_digit* e, int bits, const int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 163, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { @@ -7730,13 +7827,12 @@ static int sp_3072_mod_exp_2_54(sp_digit* r, const sp_digit* e, int bits, const #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) tmp = td + 108; XMEMSET(td, 0, sizeof(sp_digit) * 163); #else - norm = nd; - tmp = td; + tmp = &td[108]; XMEMSET(td, 0, sizeof(td)); #endif @@ -7796,7 +7892,7 @@ static int sp_3072_mod_exp_2_54(sp_digit* r, const sp_digit* e, int bits, const (sp_digit)1 : (sp_digit)0) - 1); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -9926,7 +10022,11 @@ static int sp_4096_mod_exp_39(sp_digit* r, const sp_digit* a, const sp_digit* e, const sp_digit* m, int reduceA) { #ifdef WOLFSSL_SP_SMALL +#if !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[3 * 78]; +#endif sp_digit* t[3]; sp_digit* norm; sp_digit mp = 1; @@ -9935,18 +10035,24 @@ static int sp_4096_mod_exp_39(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 39 * 2, NULL, +#if !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 39 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; } +#endif if (err == MP_OKAY) { - XMEMSET(td, 0, sizeof(*td) * 3U * 39U * 2U); - - norm = t[0] = td; - t[1] = &td[39 * 2]; - t[2] = &td[2 * 39 * 2]; + norm = td; + for (i=0; i<3; i++) { +#if !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 39 * 2); +#else + t[i] = &td[i * 39 * 2]; +#endif + XMEMSET(t[i], 0, sizeof(sp_digit) * 39U * 2U); + } sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_39(norm, m); @@ -9984,11 +10090,11 @@ static int sp_4096_mod_exp_39(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), - sizeof(*t[2]) * 39 * 2); + sizeof(*t[2]) * 39 * 2); sp_4096_mont_sqr_39(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), t[2], - sizeof(*t[2]) * 39 * 2); + sizeof(*t[2]) * 39 * 2); } sp_4096_mont_reduce_39(t[0], m, mp); @@ -9999,16 +10105,18 @@ static int sp_4096_mod_exp_39(sp_digit* r, const sp_digit* a, const sp_digit* e, } +#if !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } +#endif return err; #elif defined(WOLFSSL_SP_CACHE_RESISTANT) -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[78]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[78]; #endif sp_digit* t[3]; sp_digit* norm; @@ -10019,7 +10127,7 @@ static int sp_4096_mod_exp_39(sp_digit* r, const sp_digit* a, const sp_digit* e, int err = MP_OKAY; #ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 39 * 2, NULL, + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 39 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -10027,10 +10135,14 @@ static int sp_4096_mod_exp_39(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { - t[0] = td; - t[1] = &td[39 * 2]; - t[2] = &td[2 * 39 * 2]; - norm = t[0]; + norm = td; + for (i=0; i<3; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 39 * 2); +#else + t[i] = &td[i * 39 * 2)]; +#endif + } sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_39(norm, m); @@ -10068,17 +10180,19 @@ static int sp_4096_mod_exp_39(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_4096_mont_mul_39(t[y^1], t[0], t[1], m, mp); XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), + sizeof(*t[2]) * 39 * 2); sp_4096_mont_sqr_39(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), t[2], sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), t[2], + sizeof(*t[2]) * 39 * 2); } sp_4096_mont_reduce_39(t[0], m, mp); n = sp_4096_cmp_39(t[0], m); sp_4096_cond_sub_39(t[0], t[0], m, ((n < 0) ? - (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, t[0], sizeof(t[0])); + (sp_digit)1 : (sp_digit)0) - 1); + XMEMCPY(r, t[0], sizeof(*r) * 39 * 2); } #ifdef WOLFSSL_SMALL_STACK @@ -10089,22 +10203,22 @@ static int sp_4096_mod_exp_39(sp_digit* r, const sp_digit* a, const sp_digit* e, return err; #else -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[32][78]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[(32 * 78) + 78]; #endif sp_digit* t[32]; + sp_digit* rt; sp_digit* norm; - sp_digit rt[78]; sp_digit mp = 1; sp_digit n; int i; int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 78, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 78) + 78), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -10112,9 +10226,16 @@ static int sp_4096_mod_exp_39(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { + norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) for (i=0; i<32; i++) t[i] = td + i * 78; - norm = t[0]; + rt = td + 2496; +#else + for (i=0; i<32; i++) + t[i] = &td[i * 78]; + rt = &td[2496]; +#endif sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_39(norm, m); @@ -10184,7 +10305,7 @@ static int sp_4096_mod_exp_39(sp_digit* r, const sp_digit* a, const sp_digit* e, y = (n >> 59) & 0x1f; n <<= 5; c -= 5; - XMEMCPY(rt, t[y], sizeof(rt)); + XMEMCPY(rt, t[y], sizeof(sp_digit) * 78); for (; i>=0 || c>=5; ) { if (c < 5) { n |= e[i--] << (11 - c); @@ -10207,10 +10328,10 @@ static int sp_4096_mod_exp_39(sp_digit* r, const sp_digit* a, const sp_digit* e, n = sp_4096_cmp_39(rt, m); sp_4096_cond_sub_39(rt, rt, m, ((n < 0) ? (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, rt, sizeof(rt)); + XMEMCPY(r, rt, sizeof(sp_digit) * 78); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -10915,7 +11036,11 @@ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e, const sp_digit* m, int reduceA) { #ifdef WOLFSSL_SP_SMALL +#if !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[3 * 156]; +#endif sp_digit* t[3]; sp_digit* norm; sp_digit mp = 1; @@ -10924,18 +11049,24 @@ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 78 * 2, NULL, +#if !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 78 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; } +#endif if (err == MP_OKAY) { - XMEMSET(td, 0, sizeof(*td) * 3U * 78U * 2U); - - norm = t[0] = td; - t[1] = &td[78 * 2]; - t[2] = &td[2 * 78 * 2]; + norm = td; + for (i=0; i<3; i++) { +#if !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 78 * 2); +#else + t[i] = &td[i * 78 * 2]; +#endif + XMEMSET(t[i], 0, sizeof(sp_digit) * 78U * 2U); + } sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_78(norm, m); @@ -10973,11 +11104,11 @@ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), - sizeof(*t[2]) * 78 * 2); + sizeof(*t[2]) * 78 * 2); sp_4096_mont_sqr_78(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + ((size_t)t[1] & addr_mask[y])), t[2], - sizeof(*t[2]) * 78 * 2); + sizeof(*t[2]) * 78 * 2); } sp_4096_mont_reduce_78(t[0], m, mp); @@ -10988,16 +11119,18 @@ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e, } +#if !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } +#endif return err; #elif defined(WOLFSSL_SP_CACHE_RESISTANT) -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[156]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[156]; #endif sp_digit* t[3]; sp_digit* norm; @@ -11008,7 +11141,7 @@ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e, int err = MP_OKAY; #ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(*td) * 3 * 78 * 2, NULL, + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 78 * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -11016,10 +11149,14 @@ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { - t[0] = td; - t[1] = &td[78 * 2]; - t[2] = &td[2 * 78 * 2]; - norm = t[0]; + norm = td; + for (i=0; i<3; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + t[i] = td + (i * 78 * 2); +#else + t[i] = &td[i * 78 * 2)]; +#endif + } sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_78(norm, m); @@ -11057,17 +11194,19 @@ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_4096_mont_mul_78(t[y^1], t[0], t[1], m, mp); XMEMCPY(t[2], (void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), + sizeof(*t[2]) * 78 * 2); sp_4096_mont_sqr_78(t[2], t[2], m, mp); XMEMCPY((void*)(((size_t)t[0] & addr_mask[y^1]) + - ((size_t)t[1] & addr_mask[y])), t[2], sizeof(t[2])); + ((size_t)t[1] & addr_mask[y])), t[2], + sizeof(*t[2]) * 78 * 2); } sp_4096_mont_reduce_78(t[0], m, mp); n = sp_4096_cmp_78(t[0], m); sp_4096_cond_sub_78(t[0], t[0], m, ((n < 0) ? - (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, t[0], sizeof(t[0])); + (sp_digit)1 : (sp_digit)0) - 1); + XMEMCPY(r, t[0], sizeof(*r) * 78 * 2); } #ifdef WOLFSSL_SMALL_STACK @@ -11078,22 +11217,22 @@ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e, return err; #else -#ifndef WOLFSSL_SMALL_STACK - sp_digit td[32][156]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[(32 * 156) + 156]; #endif sp_digit* t[32]; + sp_digit* rt; sp_digit* norm; - sp_digit rt[156]; sp_digit mp = 1; sp_digit n; int i; int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 156, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 156) + 156), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -11101,9 +11240,16 @@ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { + norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) for (i=0; i<32; i++) t[i] = td + i * 156; - norm = t[0]; + rt = td + 4992; +#else + for (i=0; i<32; i++) + t[i] = &td[i * 156]; + rt = &td[4992]; +#endif sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_78(norm, m); @@ -11173,7 +11319,7 @@ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e, y = (n >> 59) & 0x1f; n <<= 5; c -= 5; - XMEMCPY(rt, t[y], sizeof(rt)); + XMEMCPY(rt, t[y], sizeof(sp_digit) * 156); for (; i>=0 || c>=5; ) { if (c < 5) { n |= e[i--] << (11 - c); @@ -11196,10 +11342,10 @@ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e, n = sp_4096_cmp_78(rt, m); sp_4096_cond_sub_78(rt, rt, m, ((n < 0) ? (sp_digit)1 : (sp_digit)0) - 1); - XMEMCPY(r, rt, sizeof(rt)); + XMEMCPY(r, rt, sizeof(sp_digit) * 156); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -12103,11 +12249,10 @@ SP_NOINLINE static void sp_4096_lshift_78(sp_digit* r, sp_digit* a, byte n) */ static int sp_4096_mod_exp_2_78(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[156]; - sp_digit td[79]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[235]; #endif sp_digit* norm; sp_digit* tmp; @@ -12117,7 +12262,7 @@ static int sp_4096_mod_exp_2_78(sp_digit* r, const sp_digit* e, int bits, const int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 235, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { @@ -12126,13 +12271,12 @@ static int sp_4096_mod_exp_2_78(sp_digit* r, const sp_digit* e, int bits, const #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) tmp = td + 156; XMEMSET(td, 0, sizeof(sp_digit) * 235); #else - norm = nd; - tmp = td; + tmp = &td[156]; XMEMSET(td, 0, sizeof(td)); #endif @@ -12192,7 +12336,7 @@ static int sp_4096_mod_exp_2_78(sp_digit* r, const sp_digit* e, int bits, const (sp_digit)1 : (sp_digit)0) - 1); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } diff --git a/wolfcrypt/src/sp_cortexm.c b/wolfcrypt/src/sp_cortexm.c index 6d8ebf0cc..ec2a5003b 100644 --- a/wolfcrypt/src/sp_cortexm.c +++ b/wolfcrypt/src/sp_cortexm.c @@ -3023,12 +3023,12 @@ static WC_INLINE int sp_2048_mod_32(sp_digit* r, const sp_digit* a, const sp_dig static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[16][64]; -#else - sp_digit* t[16]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[16 * 64]; #endif + sp_digit* t[16]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -3037,8 +3037,8 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 64, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 64), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -3046,12 +3046,14 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<16; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 64; - } +#else + t[i] = &td[i * 64]; #endif - norm = t[0]; + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_32(norm, m); @@ -3134,7 +3136,7 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_2048_cond_sub_32(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -3155,12 +3157,12 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][64]; -#else - sp_digit* t[32]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[32 * 64]; #endif + sp_digit* t[32]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -3169,8 +3171,8 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 64, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 64), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -3178,12 +3180,14 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<32; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 64; - } +#else + t[i] = &td[i * 64]; #endif - norm = t[0]; + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_32(norm, m); @@ -3283,7 +3287,7 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_2048_cond_sub_32(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -3714,12 +3718,12 @@ static WC_INLINE int sp_2048_mod_64_cond(sp_digit* r, const sp_digit* a, const s static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[16][128]; -#else - sp_digit* t[16]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[16 * 128]; #endif + sp_digit* t[16]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -3728,8 +3732,8 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 128, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 128), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -3737,12 +3741,14 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<16; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 128; - } +#else + t[i] = &td[i * 128]; #endif - norm = t[0]; + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_64(norm, m); @@ -3825,7 +3831,7 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_2048_cond_sub_64(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -3846,12 +3852,12 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][128]; -#else - sp_digit* t[32]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[32 * 128]; #endif + sp_digit* t[32]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -3860,8 +3866,8 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 128, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 128), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -3869,12 +3875,14 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<32; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 128; - } +#else + t[i] = &td[i * 128]; #endif - norm = t[0]; + } sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_64(norm, m); @@ -3974,7 +3982,7 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_2048_cond_sub_64(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -4864,11 +4872,10 @@ static void sp_2048_lshift_64(sp_digit* r, sp_digit* a, byte n) static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[128]; - sp_digit td[65]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[193]; #endif sp_digit* norm; sp_digit* tmp; @@ -4879,7 +4886,7 @@ static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 193, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { @@ -4888,12 +4895,11 @@ static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; - tmp = td + 128; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + tmp = td + 128; #else - norm = nd; - tmp = td; + tmp = &td[128]; #endif sp_2048_mont_setup(m, &mp); @@ -4953,7 +4959,7 @@ static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, sp_2048_cond_sub_64(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -7669,12 +7675,12 @@ static WC_INLINE int sp_3072_mod_48(sp_digit* r, const sp_digit* a, const sp_dig static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[16][96]; -#else - sp_digit* t[16]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[16 * 96]; #endif + sp_digit* t[16]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -7683,8 +7689,8 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 96, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 96), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -7692,12 +7698,14 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<16; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 96; - } +#else + t[i] = &td[i * 96]; #endif - norm = t[0]; + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_48(norm, m); @@ -7780,7 +7788,7 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_3072_cond_sub_48(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -7801,12 +7809,12 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][96]; -#else - sp_digit* t[32]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[32 * 96]; #endif + sp_digit* t[32]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -7815,8 +7823,8 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 96, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 96), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -7824,12 +7832,14 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<32; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 96; - } +#else + t[i] = &td[i * 96]; #endif - norm = t[0]; + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_48(norm, m); @@ -7929,7 +7939,7 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_3072_cond_sub_48(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -8363,12 +8373,12 @@ static WC_INLINE int sp_3072_mod_96_cond(sp_digit* r, const sp_digit* a, const s static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[16][192]; -#else - sp_digit* t[16]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[16 * 192]; #endif + sp_digit* t[16]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -8377,8 +8387,8 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 192, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 192), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -8386,12 +8396,14 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<16; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 192; - } +#else + t[i] = &td[i * 192]; #endif - norm = t[0]; + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_96(norm, m); @@ -8474,7 +8486,7 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_3072_cond_sub_96(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -8495,12 +8507,12 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][192]; -#else - sp_digit* t[32]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[32 * 192]; #endif + sp_digit* t[32]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -8509,8 +8521,8 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 192, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 192), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -8518,12 +8530,14 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<32; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 192; - } +#else + t[i] = &td[i * 192]; #endif - norm = t[0]; + } sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_96(norm, m); @@ -8623,7 +8637,7 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_3072_cond_sub_96(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -9709,11 +9723,10 @@ static void sp_3072_lshift_96(sp_digit* r, sp_digit* a, byte n) static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[192]; - sp_digit td[97]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[289]; #endif sp_digit* norm; sp_digit* tmp; @@ -9724,7 +9737,7 @@ static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 289, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { @@ -9733,12 +9746,11 @@ static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; - tmp = td + 192; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + tmp = td + 192; #else - norm = nd; - tmp = td; + tmp = &td[192]; #endif sp_3072_mont_setup(m, &mp); @@ -9798,7 +9810,7 @@ static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, sp_3072_cond_sub_96(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -11958,12 +11970,12 @@ static WC_INLINE int sp_4096_mod_128_cond(sp_digit* r, const sp_digit* a, const static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[16][256]; -#else - sp_digit* t[16]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[16 * 256]; #endif + sp_digit* t[16]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -11972,8 +11984,8 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 256, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 256), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -11981,12 +11993,14 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<16; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 256; - } +#else + t[i] = &td[i * 256]; #endif - norm = t[0]; + } sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_128(norm, m); @@ -12069,7 +12083,7 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e sp_4096_cond_sub_128(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -12090,12 +12104,12 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][256]; -#else - sp_digit* t[32]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[32 * 256]; #endif + sp_digit* t[32]; sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -12104,8 +12118,8 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 256, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 256), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -12113,12 +12127,14 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; for (i=0; i<32; i++) { +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) t[i] = td + i * 256; - } +#else + t[i] = &td[i * 256]; #endif - norm = t[0]; + } sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_128(norm, m); @@ -12218,7 +12234,7 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e sp_4096_cond_sub_128(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } @@ -13501,11 +13517,10 @@ static void sp_4096_lshift_128(sp_digit* r, sp_digit* a, byte n) static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[256]; - sp_digit td[129]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[385]; #endif sp_digit* norm; sp_digit* tmp; @@ -13516,7 +13531,7 @@ static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 385, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { @@ -13525,12 +13540,11 @@ static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; - tmp = td + 256; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + tmp = td + 256; #else - norm = nd; - tmp = td; + tmp = &td[256]; #endif sp_4096_mont_setup(m, &mp); @@ -13590,7 +13604,7 @@ static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, sp_4096_cond_sub_128(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) { XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); } diff --git a/wolfcrypt/src/sp_int.c b/wolfcrypt/src/sp_int.c index 36f1165db..88ba48c1c 100644 --- a/wolfcrypt/src/sp_int.c +++ b/wolfcrypt/src/sp_int.c @@ -41,8 +41,9 @@ * WOLFSSL_SP_MATH: Use only single precision math and algorithms it supports (no fastmath tfm.c or normal integer.c) * WOLFSSL_SP_SMALL: Use smaller version of code and avoid large stack variables * WOLFSSL_SP_NO_MALLOC: Always use stack, no heap XMALLOC/XFREE allowed - * WOLFSSL_SP_NO_3072: Disable RSA/DH 3072-bit support * WOLFSSL_SP_NO_2048: Disable RSA/DH 2048-bit support + * WOLFSSL_SP_NO_3072: Disable RSA/DH 3072-bit support + * WOLFSSL_SP_NO_4096: Disable RSA/DH 4096-bit support * WOLFSSL_SP_4096: Enable RSA/RH 4096-bit support * WOLFSSL_SP_384 Enable ECC 384-bit SECP384R1 support * WOLFSSL_SP_NO_256 Disable ECC 256-bit SECP256R1 support diff --git a/wolfcrypt/src/sp_x86_64.c b/wolfcrypt/src/sp_x86_64.c index f94612136..b3af97702 100644 --- a/wolfcrypt/src/sp_x86_64.c +++ b/wolfcrypt/src/sp_x86_64.c @@ -399,14 +399,13 @@ static WC_INLINE int sp_2048_mod_16(sp_digit* r, const sp_digit* a, const sp_dig static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][32]; - sp_digit rt[32]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + sp_digit* td; #else + sp_digit td[(33 * 32) + 32]; +#endif sp_digit* t[32]; sp_digit* rt; - sp_digit* td; -#endif sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -415,8 +414,8 @@ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 33 * 32, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 * 32) + 32, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -424,12 +423,16 @@ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) for (i=0; i<32; i++) t[i] = td + i * 32; rt = td + 1024; +#else + for (i=0; i<32; i++) + t[i] = &td[i * 32]; + rt = &td[1024]; #endif - norm = t[0]; sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_16(norm, m); @@ -536,7 +539,7 @@ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_2048_cond_sub_16(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); #endif @@ -592,14 +595,13 @@ static void sp_2048_mont_sqr_avx2_16(sp_digit* r, const sp_digit* a, const sp_di static int sp_2048_mod_exp_avx2_16(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][32]; - sp_digit rt[32]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + sp_digit* td; #else + sp_digit td[(33 * 32) + 32]; +#endif sp_digit* t[32]; sp_digit* rt; - sp_digit* td; -#endif sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -608,8 +610,8 @@ static int sp_2048_mod_exp_avx2_16(sp_digit* r, const sp_digit* a, const sp_digi int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 33 * 32, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 * 32) + 32, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -617,12 +619,16 @@ static int sp_2048_mod_exp_avx2_16(sp_digit* r, const sp_digit* a, const sp_digi #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) for (i=0; i<32; i++) t[i] = td + i * 32; rt = td + 1024; +#else + for (i=0; i<32; i++) + t[i] = &td[i * 32]; + rt = &td[1024]; #endif - norm = t[0]; sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_16(norm, m); @@ -729,7 +735,7 @@ static int sp_2048_mod_exp_avx2_16(sp_digit* r, const sp_digit* a, const sp_digi sp_2048_cond_sub_avx2_16(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); #endif @@ -1000,14 +1006,13 @@ static WC_INLINE int sp_2048_mod_32_cond(sp_digit* r, const sp_digit* a, const s static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][64]; - sp_digit rt[64]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + sp_digit* td; #else + sp_digit td[(33 * 64) + 64]; +#endif sp_digit* t[32]; sp_digit* rt; - sp_digit* td; -#endif sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -1016,8 +1021,8 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 33 * 64, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 * 64) + 64, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -1025,12 +1030,16 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) for (i=0; i<32; i++) t[i] = td + i * 64; rt = td + 2048; +#else + for (i=0; i<32; i++) + t[i] = &td[i * 64]; + rt = &td[2048]; #endif - norm = t[0]; sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_32(norm, m); @@ -1137,7 +1146,7 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_2048_cond_sub_32(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); #endif @@ -1195,14 +1204,13 @@ static void sp_2048_mont_sqr_avx2_32(sp_digit* r, const sp_digit* a, const sp_di static int sp_2048_mod_exp_avx2_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][64]; - sp_digit rt[64]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + sp_digit* td; #else + sp_digit td[(33 * 64) + 64]; +#endif sp_digit* t[32]; sp_digit* rt; - sp_digit* td; -#endif sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -1211,8 +1219,8 @@ static int sp_2048_mod_exp_avx2_32(sp_digit* r, const sp_digit* a, const sp_digi int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 33 * 64, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 * 64) + 64, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -1220,12 +1228,16 @@ static int sp_2048_mod_exp_avx2_32(sp_digit* r, const sp_digit* a, const sp_digi #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) for (i=0; i<32; i++) t[i] = td + i * 64; rt = td + 2048; +#else + for (i=0; i<32; i++) + t[i] = &td[i * 64]; + rt = &td[2048]; #endif - norm = t[0]; sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_32(norm, m); @@ -1332,7 +1344,7 @@ static int sp_2048_mod_exp_avx2_32(sp_digit* r, const sp_digit* a, const sp_digi sp_2048_cond_sub_avx2_32(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); #endif @@ -1889,11 +1901,10 @@ extern void sp_2048_lshift_32(sp_digit* r, const sp_digit* a, int n); static int sp_2048_mod_exp_2_avx2_32(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[64]; - sp_digit td[33]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[33 + 64]; #endif sp_digit* norm; sp_digit* tmp; @@ -1904,8 +1915,8 @@ static int sp_2048_mod_exp_2_avx2_32(sp_digit* r, const sp_digit* e, int bits, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 97, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 + 64), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -1913,12 +1924,11 @@ static int sp_2048_mod_exp_2_avx2_32(sp_digit* r, const sp_digit* e, int bits, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; - tmp = td + 64; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + tmp = td + 64; #else - norm = nd; - tmp = td; + tmp = &td[64]; #endif sp_2048_mont_setup(m, &mp); @@ -1981,7 +1991,7 @@ static int sp_2048_mod_exp_2_avx2_32(sp_digit* r, const sp_digit* e, int bits, sp_2048_cond_sub_avx2_32(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); #endif @@ -2001,11 +2011,10 @@ static int sp_2048_mod_exp_2_avx2_32(sp_digit* r, const sp_digit* e, int bits, static int sp_2048_mod_exp_2_32(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[64]; - sp_digit td[33]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[33 + 64]; #endif sp_digit* norm; sp_digit* tmp; @@ -2016,8 +2025,8 @@ static int sp_2048_mod_exp_2_32(sp_digit* r, const sp_digit* e, int bits, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 97, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 + 64), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -2025,12 +2034,11 @@ static int sp_2048_mod_exp_2_32(sp_digit* r, const sp_digit* e, int bits, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; - tmp = td + 64; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + tmp = td + 64; #else - norm = nd; - tmp = td; + tmp = &td[64]; #endif sp_2048_mont_setup(m, &mp); @@ -2093,7 +2101,7 @@ static int sp_2048_mod_exp_2_32(sp_digit* r, const sp_digit* e, int bits, sp_2048_cond_sub_32(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); #endif @@ -2587,14 +2595,13 @@ static WC_INLINE int sp_3072_mod_24(sp_digit* r, const sp_digit* a, const sp_dig static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][48]; - sp_digit rt[48]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + sp_digit* td; #else + sp_digit td[(33 * 48) + 48]; +#endif sp_digit* t[32]; sp_digit* rt; - sp_digit* td; -#endif sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -2603,8 +2610,8 @@ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 33 * 48, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 * 48) + 48, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -2612,12 +2619,16 @@ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) for (i=0; i<32; i++) t[i] = td + i * 48; rt = td + 1536; +#else + for (i=0; i<32; i++) + t[i] = &td[i * 48]; + rt = &td[1536]; #endif - norm = t[0]; sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_24(norm, m); @@ -2724,7 +2735,7 @@ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_3072_cond_sub_24(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); #endif @@ -2780,14 +2791,13 @@ static void sp_3072_mont_sqr_avx2_24(sp_digit* r, const sp_digit* a, const sp_di static int sp_3072_mod_exp_avx2_24(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][48]; - sp_digit rt[48]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + sp_digit* td; #else + sp_digit td[(33 * 48) + 48]; +#endif sp_digit* t[32]; sp_digit* rt; - sp_digit* td; -#endif sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -2796,8 +2806,8 @@ static int sp_3072_mod_exp_avx2_24(sp_digit* r, const sp_digit* a, const sp_digi int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 33 * 48, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 * 48) + 48, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -2805,12 +2815,16 @@ static int sp_3072_mod_exp_avx2_24(sp_digit* r, const sp_digit* a, const sp_digi #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) for (i=0; i<32; i++) t[i] = td + i * 48; rt = td + 1536; +#else + for (i=0; i<32; i++) + t[i] = &td[i * 48]; + rt = &td[1536]; #endif - norm = t[0]; sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_24(norm, m); @@ -2917,7 +2931,7 @@ static int sp_3072_mod_exp_avx2_24(sp_digit* r, const sp_digit* a, const sp_digi sp_3072_cond_sub_avx2_24(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); #endif @@ -3188,14 +3202,13 @@ static WC_INLINE int sp_3072_mod_48_cond(sp_digit* r, const sp_digit* a, const s static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][96]; - sp_digit rt[96]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + sp_digit* td; #else + sp_digit td[(33 * 96) + 96]; +#endif sp_digit* t[32]; sp_digit* rt; - sp_digit* td; -#endif sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -3204,8 +3217,8 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 33 * 96, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 * 96) + 96, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -3213,12 +3226,16 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) for (i=0; i<32; i++) t[i] = td + i * 96; rt = td + 3072; +#else + for (i=0; i<32; i++) + t[i] = &td[i * 96]; + rt = &td[3072]; #endif - norm = t[0]; sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_48(norm, m); @@ -3325,7 +3342,7 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_3072_cond_sub_48(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); #endif @@ -3383,14 +3400,13 @@ static void sp_3072_mont_sqr_avx2_48(sp_digit* r, const sp_digit* a, const sp_di static int sp_3072_mod_exp_avx2_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][96]; - sp_digit rt[96]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + sp_digit* td; #else + sp_digit td[(33 * 96) + 96]; +#endif sp_digit* t[32]; sp_digit* rt; - sp_digit* td; -#endif sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -3399,8 +3415,8 @@ static int sp_3072_mod_exp_avx2_48(sp_digit* r, const sp_digit* a, const sp_digi int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 33 * 96, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 * 96) + 96, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -3408,12 +3424,16 @@ static int sp_3072_mod_exp_avx2_48(sp_digit* r, const sp_digit* a, const sp_digi #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) for (i=0; i<32; i++) t[i] = td + i * 96; rt = td + 3072; +#else + for (i=0; i<32; i++) + t[i] = &td[i * 96]; + rt = &td[3072]; #endif - norm = t[0]; sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_48(norm, m); @@ -3520,7 +3540,7 @@ static int sp_3072_mod_exp_avx2_48(sp_digit* r, const sp_digit* a, const sp_digi sp_3072_cond_sub_avx2_48(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); #endif @@ -4077,11 +4097,10 @@ extern void sp_3072_lshift_48(sp_digit* r, const sp_digit* a, int n); static int sp_3072_mod_exp_2_avx2_48(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[96]; - sp_digit td[49]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[49 + 96]; #endif sp_digit* norm; sp_digit* tmp; @@ -4092,8 +4111,8 @@ static int sp_3072_mod_exp_2_avx2_48(sp_digit* r, const sp_digit* e, int bits, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 145, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (49 + 96), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -4101,12 +4120,11 @@ static int sp_3072_mod_exp_2_avx2_48(sp_digit* r, const sp_digit* e, int bits, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; - tmp = td + 96; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + tmp = td + 96; #else - norm = nd; - tmp = td; + tmp = &td[96]; #endif sp_3072_mont_setup(m, &mp); @@ -4169,7 +4187,7 @@ static int sp_3072_mod_exp_2_avx2_48(sp_digit* r, const sp_digit* e, int bits, sp_3072_cond_sub_avx2_48(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); #endif @@ -4189,11 +4207,10 @@ static int sp_3072_mod_exp_2_avx2_48(sp_digit* r, const sp_digit* e, int bits, static int sp_3072_mod_exp_2_48(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[96]; - sp_digit td[49]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[49 + 96]; #endif sp_digit* norm; sp_digit* tmp; @@ -4204,8 +4221,8 @@ static int sp_3072_mod_exp_2_48(sp_digit* r, const sp_digit* e, int bits, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 145, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (49 + 96), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -4213,12 +4230,11 @@ static int sp_3072_mod_exp_2_48(sp_digit* r, const sp_digit* e, int bits, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; - tmp = td + 96; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + tmp = td + 96; #else - norm = nd; - tmp = td; + tmp = &td[96]; #endif sp_3072_mont_setup(m, &mp); @@ -4281,7 +4297,7 @@ static int sp_3072_mod_exp_2_48(sp_digit* r, const sp_digit* e, int bits, sp_3072_cond_sub_48(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); #endif @@ -4830,14 +4846,13 @@ static WC_INLINE int sp_4096_mod_64_cond(sp_digit* r, const sp_digit* a, const s static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][128]; - sp_digit rt[128]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + sp_digit* td; #else + sp_digit td[(33 * 128) + 128]; +#endif sp_digit* t[32]; sp_digit* rt; - sp_digit* td; -#endif sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -4846,8 +4861,8 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 33 * 128, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 * 128) + 128, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -4855,12 +4870,16 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) for (i=0; i<32; i++) t[i] = td + i * 128; rt = td + 4096; +#else + for (i=0; i<32; i++) + t[i] = &td[i * 128]; + rt = &td[4096]; #endif - norm = t[0]; sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_64(norm, m); @@ -4967,7 +4986,7 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, sp_4096_cond_sub_64(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); #endif @@ -5025,14 +5044,13 @@ static void sp_4096_mont_sqr_avx2_64(sp_digit* r, const sp_digit* a, const sp_di static int sp_4096_mod_exp_avx2_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit t[32][128]; - sp_digit rt[128]; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + sp_digit* td; #else + sp_digit td[(33 * 128) + 128]; +#endif sp_digit* t[32]; sp_digit* rt; - sp_digit* td; -#endif sp_digit* norm; sp_digit mp = 1; sp_digit n; @@ -5041,8 +5059,8 @@ static int sp_4096_mod_exp_avx2_64(sp_digit* r, const sp_digit* a, const sp_digi int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 33 * 128, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 * 128) + 128, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -5050,12 +5068,16 @@ static int sp_4096_mod_exp_avx2_64(sp_digit* r, const sp_digit* a, const sp_digi #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK + norm = td; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) for (i=0; i<32; i++) t[i] = td + i * 128; rt = td + 4096; +#else + for (i=0; i<32; i++) + t[i] = &td[i * 128]; + rt = &td[4096]; #endif - norm = t[0]; sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_64(norm, m); @@ -5162,7 +5184,7 @@ static int sp_4096_mod_exp_avx2_64(sp_digit* r, const sp_digit* a, const sp_digi sp_4096_cond_sub_avx2_64(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); #endif @@ -5719,11 +5741,10 @@ extern void sp_4096_lshift_64(sp_digit* r, const sp_digit* a, int n); static int sp_4096_mod_exp_2_avx2_64(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[128]; - sp_digit td[65]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[65 + 128]; #endif sp_digit* norm; sp_digit* tmp; @@ -5734,8 +5755,8 @@ static int sp_4096_mod_exp_2_avx2_64(sp_digit* r, const sp_digit* e, int bits, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 193, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (65 + 128), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -5743,12 +5764,11 @@ static int sp_4096_mod_exp_2_avx2_64(sp_digit* r, const sp_digit* e, int bits, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; - tmp = td + 128; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + tmp = td + 128; #else - norm = nd; - tmp = td; + tmp = &td[128]; #endif sp_4096_mont_setup(m, &mp); @@ -5811,7 +5831,7 @@ static int sp_4096_mod_exp_2_avx2_64(sp_digit* r, const sp_digit* e, int bits, sp_4096_cond_sub_avx2_64(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); #endif @@ -5831,11 +5851,10 @@ static int sp_4096_mod_exp_2_avx2_64(sp_digit* r, const sp_digit* e, int bits, static int sp_4096_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifndef WOLFSSL_SMALL_STACK - sp_digit nd[128]; - sp_digit td[65]; -#else +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) sp_digit* td; +#else + sp_digit td[65 + 128]; #endif sp_digit* norm; sp_digit* tmp; @@ -5846,8 +5865,8 @@ static int sp_4096_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, int c, y; int err = MP_OKAY; -#ifdef WOLFSSL_SMALL_STACK - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 193, NULL, +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (65 + 128), NULL, DYNAMIC_TYPE_TMP_BUFFER); if (td == NULL) { err = MEMORY_E; @@ -5855,12 +5874,11 @@ static int sp_4096_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, #endif if (err == MP_OKAY) { -#ifdef WOLFSSL_SMALL_STACK norm = td; - tmp = td + 128; +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) + tmp = td + 128; #else - norm = nd; - tmp = td; + tmp = &td[128]; #endif sp_4096_mont_setup(m, &mp); @@ -5923,7 +5941,7 @@ static int sp_4096_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, sp_4096_cond_sub_64(r, r, m, mask); } -#ifdef WOLFSSL_SMALL_STACK +#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC) if (td != NULL) XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); #endif