diff --git a/wolfcrypt/src/sp_arm32.c b/wolfcrypt/src/sp_arm32.c index ed50bffc9..efa3decee 100644 --- a/wolfcrypt/src/sp_arm32.c +++ b/wolfcrypt/src/sp_arm32.c @@ -57,6 +57,57 @@ #define __volatile__ volatile #endif +#ifdef WOLFSSL_SP_SMALL_STACK + #define SP_DECL_VAR(TYPE, NAME, CNT) \ + TYPE* NAME = NULL + #define SP_ALLOC_VAR(TYPE, NAME, CNT, HEAP, DT) \ + if (err == MP_OKAY) { \ + (NAME) = (TYPE*)XMALLOC(sizeof(TYPE) * (CNT), (HEAP), DT); \ + if ((NAME) == NULL) { \ + err = MEMORY_E; \ + } \ + } + + #define SP_VAR_OK(NAME) ((NAME) != NULL) + + #define SP_FREE_VAR(NAME, HEAP, DT) \ + XFREE(NAME, (HEAP), DT) + #define SP_ZEROFREE_VAR(TYPE, NAME, CNT, HEAP, DT) \ + do { \ + if ((NAME) != NULL) { \ + ForceZero(NAME, sizeof(TYPE) * (CNT)); \ + } \ + SP_FREE_VAR(NAME, HEAP, DT); \ + } while (0) + #define SP_ZEROFREE_VAR_ALT(TYPE, NAME, FZ_NAME, CNT, HEAP, DT) \ + do { \ + if ((FZ_NAME) != NULL) { \ + ForceZero(FZ_NAME, sizeof(TYPE) * (CNT)); \ + } \ + SP_FREE_VAR(NAME, HEAP, DT); \ + } while (0) +#else + #define SP_DECL_VAR(TYPE, NAME, CNT) \ + TYPE NAME[CNT] + #define SP_ALLOC_VAR(TYPE, NAME, CNT, HEAP, DT) \ + WC_DO_NOTHING + #define SP_VAR_OK(NAME) (1) + #define SP_FREE_VAR(NAME, HEAP, DT) \ + WC_DO_NOTHING + #define SP_ZEROFREE_VAR(TYPE, NAME, CNT, HEAP, DT) \ + do { \ + if ((NAME) != NULL) { \ + ForceZero(NAME, sizeof(TYPE) * (CNT)); \ + } \ + } while (0) + #define SP_ZEROFREE_VAR_ALT(TYPE, NAME, FZ_NAME, CNT, HEAP, DT) \ + do { \ + if ((FZ_NAME) != NULL) { \ + ForceZero(FZ_NAME, sizeof(TYPE) * (CNT)); \ + } \ + } while (0) +#endif + #ifdef WOLFSSL_SP_ARM32_ASM #define SP_PRINT_NUM(var, name, total, words, bits) \ do { \ @@ -12380,11 +12431,7 @@ static WC_INLINE int sp_2048_mod_32(sp_digit* r, const sp_digit* a, 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[16 * 64]; -#endif + SP_DECL_VAR(sp_digit, td, 16 * 64); sp_digit* t[16]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -12399,15 +12446,7 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 64), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 16 * 64, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) { @@ -12510,9 +12549,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -12531,11 +12568,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[32 * 64]; -#endif + SP_DECL_VAR(sp_digit, td, 32 * 64); sp_digit* t[32]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -12550,15 +12583,7 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 64), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 32 * 64, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) { @@ -12678,9 +12703,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -17282,11 +17305,7 @@ static WC_INLINE int sp_2048_mod_64(sp_digit* r, const sp_digit* a, 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[8 * 128]; -#endif + SP_DECL_VAR(sp_digit, td, 8 * 128); sp_digit* t[8]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -17301,15 +17320,7 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 128), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 8 * 128, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<8; i++) { @@ -17403,9 +17414,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -17424,11 +17433,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[16 * 128]; -#endif + SP_DECL_VAR(sp_digit, td, 16 * 128); sp_digit* t[16]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -17443,15 +17448,7 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 128), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 16 * 128, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) { @@ -17554,9 +17551,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -17580,11 +17575,7 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[64 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 64 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit *ah = NULL; @@ -17602,15 +17593,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 64 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 64 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { ah = a + 64; r = a + 64 * 2; @@ -17717,9 +17700,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; } @@ -17952,11 +17933,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, const mp_int* qim, const mp_int* mm, byte* out, word32* outLen) { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[64 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 64 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -17986,15 +17963,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 64 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 64 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 64; m = a + 128; @@ -18011,25 +17980,11 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 64); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 64, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[32 * 11]; -#endif + SP_DECL_VAR(sp_digit, a, 32 * 11); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -18060,15 +18015,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 11, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 32 * 11, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 64 * 2; q = p + 32; @@ -18108,15 +18055,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 32 * 11); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 32 * 11, NULL, DYNAMIC_TYPE_RSA); #endif /* SP_RSA_PRIVATE_EXP_D || RSA_LOW_MEM */ return err; } @@ -18668,11 +18607,7 @@ WC_OMIT_FRAME_POINTER static void sp_2048_lshift_64(sp_digit* r, static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[193]; -#endif + SP_DECL_VAR(sp_digit, td, 193); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -18688,15 +18623,7 @@ static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 193, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 193, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 128; @@ -18773,9 +18700,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -38359,11 +38284,7 @@ static WC_INLINE int sp_3072_mod_48(sp_digit* r, const sp_digit* a, 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[16 * 96]; -#endif + SP_DECL_VAR(sp_digit, td, 16 * 96); sp_digit* t[16]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -38378,15 +38299,7 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 96), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 16 * 96, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) { @@ -38489,9 +38402,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -38510,11 +38421,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[32 * 96]; -#endif + SP_DECL_VAR(sp_digit, td, 32 * 96); sp_digit* t[32]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -38529,15 +38436,7 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 96), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 32 * 96, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) { @@ -38657,9 +38556,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -45130,11 +45027,7 @@ static WC_INLINE int sp_3072_mod_96(sp_digit* r, const sp_digit* a, 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[8 * 192]; -#endif + SP_DECL_VAR(sp_digit, td, 8 * 192); sp_digit* t[8]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -45149,15 +45042,7 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 192), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 8 * 192, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<8; i++) { @@ -45251,9 +45136,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -45272,11 +45155,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[16 * 192]; -#endif + SP_DECL_VAR(sp_digit, td, 16 * 192); sp_digit* t[16]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -45291,15 +45170,7 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 192), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 16 * 192, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) { @@ -45402,9 +45273,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -45428,11 +45297,7 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[96 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 96 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit *ah = NULL; @@ -45450,15 +45315,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 96 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 96 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { ah = a + 96; r = a + 96 * 2; @@ -45565,9 +45422,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; } @@ -45856,11 +45711,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, const mp_int* qim, const mp_int* mm, byte* out, word32* outLen) { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[96 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 96 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -45890,15 +45741,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 96 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 96 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 96; m = a + 192; @@ -45915,25 +45758,11 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 96); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 96, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[48 * 11]; -#endif + SP_DECL_VAR(sp_digit, a, 48 * 11); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -45964,15 +45793,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 48 * 11, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 48 * 11, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 96 * 2; q = p + 48; @@ -46012,15 +45833,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 48 * 11); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 48 * 11, NULL, DYNAMIC_TYPE_RSA); #endif /* SP_RSA_PRIVATE_EXP_D || RSA_LOW_MEM */ return err; } @@ -46764,11 +46577,7 @@ WC_OMIT_FRAME_POINTER static void sp_3072_lshift_96(sp_digit* r, static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[289]; -#endif + SP_DECL_VAR(sp_digit, td, 289); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -46784,15 +46593,7 @@ static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 289, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 289, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 192; @@ -46869,9 +46670,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -60834,11 +60633,7 @@ static WC_INLINE int sp_4096_mod_128(sp_digit* r, const sp_digit* a, 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[8 * 256]; -#endif + SP_DECL_VAR(sp_digit, td, 8 * 256); sp_digit* t[8]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -60853,15 +60648,7 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 256), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 8 * 256, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<8; i++) { @@ -60955,9 +60742,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -60976,11 +60761,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[16 * 256]; -#endif + SP_DECL_VAR(sp_digit, td, 16 * 256); sp_digit* t[16]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -60995,15 +60776,7 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 256), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 16 * 256, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) { @@ -61106,9 +60879,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -61132,11 +60903,7 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[128 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 128 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit *ah = NULL; @@ -61154,15 +60921,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 128 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 128 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { ah = a + 128; r = a + 128 * 2; @@ -61269,9 +61028,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; } @@ -61616,11 +61373,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, const mp_int* qim, const mp_int* mm, byte* out, word32* outLen) { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[128 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 128 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -61650,15 +61403,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 128 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 128 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 128; m = a + 256; @@ -61675,25 +61420,11 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 128); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 128, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[64 * 11]; -#endif + SP_DECL_VAR(sp_digit, a, 64 * 11); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -61724,15 +61455,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 64 * 11, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 64 * 11, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 128 * 2; q = p + 64; @@ -61772,15 +61495,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 64 * 11); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 64 * 11, NULL, DYNAMIC_TYPE_RSA); #endif /* SP_RSA_PRIVATE_EXP_D || RSA_LOW_MEM */ return err; } @@ -62716,11 +62431,7 @@ WC_OMIT_FRAME_POINTER static void sp_4096_lshift_128(sp_digit* r, static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[385]; -#endif + SP_DECL_VAR(sp_digit, td, 385); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -62736,15 +62447,7 @@ static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 385, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 385, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 256; @@ -62821,9 +62524,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -75476,20 +75177,11 @@ static void sp_256_get_point_16_8(sp_point_256* r, const sp_point_256* table, static int sp_256_ecc_mulmod_fast_8(sp_point_256* r, const sp_point_256* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_256 t[16 + 1]; - sp_digit tmp[2 * 8 * 6]; -#endif + SP_DECL_VAR(sp_point_256, t, 16 + 1); + SP_DECL_VAR(sp_digit, tmp, 2 * 8 * 6); sp_point_256* rt = NULL; #ifndef WC_NO_CACHE_RESISTANT -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* p = NULL; -#else - sp_point_256 p[1]; -#endif + SP_DECL_VAR(sp_point_256, p, 1); #endif /* !WC_NO_CACHE_RESISTANT */ sp_digit n; int i; @@ -75501,26 +75193,11 @@ static int sp_256_ecc_mulmod_fast_8(sp_point_256* r, const sp_point_256* g, cons (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * (16 + 1), - heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - #ifndef WC_NO_CACHE_RESISTANT - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256), - heap, DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - #endif - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 6, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } + SP_ALLOC_VAR(sp_point_256, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); +#ifndef WC_NO_CACHE_RESISTANT + SP_ALLOC_VAR(sp_point_256, p, 1, heap, DYNAMIC_TYPE_ECC); #endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 8 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 16; @@ -75612,35 +75289,12 @@ static int sp_256_ecc_mulmod_fast_8(sp_point_256* r, const sp_point_256* g, cons } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 8 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 8 * 6, heap, + DYNAMIC_TYPE_ECC); #ifndef WC_NO_CACHE_RESISTANT -#ifdef WOLFSSL_SP_SMALL_STACK - if (p != NULL) -#endif - { - ForceZero(p, sizeof(sp_point_256)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(p, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_256, p, 1, heap, DYNAMIC_TYPE_ECC); #endif /* !WC_NO_CACHE_RESISTANT */ -#ifdef WOLFSSL_SP_SMALL_STACK - if (t != NULL) -#endif - { - ForceZero(t, sizeof(sp_point_256) * 17); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_256, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -75865,11 +75519,7 @@ static void sp_256_proj_point_add_qz1_8(sp_point_256* r, static int sp_256_gen_stripe_table_8(const sp_point_256* a, sp_table_entry_256* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* t = NULL; -#else - sp_point_256 t[3]; -#endif + SP_DECL_VAR(sp_point_256, t, 3); sp_point_256* s1 = NULL; sp_point_256* s2 = NULL; int i; @@ -75878,13 +75528,7 @@ static int sp_256_gen_stripe_table_8(const sp_point_256* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_256, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -75933,9 +75577,7 @@ static int sp_256_gen_stripe_table_8(const sp_point_256* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -76011,13 +75653,8 @@ static int sp_256_ecc_mulmod_stripe_8(sp_point_256* r, const sp_point_256* g, const sp_table_entry_256* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_256 rt[2]; - sp_digit t[2 * 8 * 6]; -#endif + SP_DECL_VAR(sp_point_256, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 8 * 6); sp_point_256* p = NULL; int i; int j; @@ -76031,19 +75668,8 @@ static int sp_256_ecc_mulmod_stripe_8(sp_point_256* r, const sp_point_256* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 8 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -76097,10 +75723,8 @@ static int sp_256_ecc_mulmod_stripe_8(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -76217,20 +75841,11 @@ static int sp_256_ecc_mulmod_8(sp_point_256* r, const sp_point_256* g, #ifndef FP_ECC return sp_256_ecc_mulmod_fast_8(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 8 * 6]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 8 * 6); sp_cache_256_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 6, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 8 * 6, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -76263,9 +75878,7 @@ static int sp_256_ecc_mulmod_8(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -76286,11 +75899,7 @@ static int sp_256_ecc_mulmod_8(sp_point_256* r, const sp_point_256* g, static int sp_256_gen_stripe_table_8(const sp_point_256* a, sp_table_entry_256* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* t = NULL; -#else - sp_point_256 t[3]; -#endif + SP_DECL_VAR(sp_point_256, t, 3); sp_point_256* s1 = NULL; sp_point_256* s2 = NULL; int i; @@ -76299,13 +75908,7 @@ static int sp_256_gen_stripe_table_8(const sp_point_256* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_256, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -76354,9 +75957,7 @@ static int sp_256_gen_stripe_table_8(const sp_point_256* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -76432,13 +76033,8 @@ static int sp_256_ecc_mulmod_stripe_8(sp_point_256* r, const sp_point_256* g, const sp_table_entry_256* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_256 rt[2]; - sp_digit t[2 * 8 * 6]; -#endif + SP_DECL_VAR(sp_point_256, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 8 * 6); sp_point_256* p = NULL; int i; int j; @@ -76452,19 +76048,8 @@ static int sp_256_ecc_mulmod_stripe_8(sp_point_256* r, const sp_point_256* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 8 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -76518,10 +76103,8 @@ static int sp_256_ecc_mulmod_stripe_8(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -76638,20 +76221,11 @@ static int sp_256_ecc_mulmod_8(sp_point_256* r, const sp_point_256* g, #ifndef FP_ECC return sp_256_ecc_mulmod_fast_8(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 8 * 6]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 8 * 6); sp_cache_256_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 6, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 8 * 6, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -76684,9 +76258,7 @@ static int sp_256_ecc_mulmod_8(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -76705,28 +76277,12 @@ static int sp_256_ecc_mulmod_8(sp_point_256* r, const sp_point_256* g, int sp_ecc_mulmod_256(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[1]; - sp_digit k[8]; -#endif + SP_DECL_VAR(sp_point_256, point, 1); + SP_DECL_VAR(sp_digit, k, 8); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 8, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 8, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(k, 8, km); sp_256_point_from_ecc_point_8(point, gm); @@ -76737,10 +76293,8 @@ int sp_ecc_mulmod_256(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_256_point_to_ecc_point_8(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -76760,31 +76314,14 @@ int sp_ecc_mulmod_256(const mp_int* km, const ecc_point* gm, ecc_point* r, int sp_ecc_mulmod_add_256(const mp_int* km, const ecc_point* gm, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[2]; - sp_digit k[8 + 8 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_256, point, 2); + SP_DECL_VAR(sp_digit, k, 8 + 8 * 2 * 6); sp_point_256* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (8 + 8 * 2 * 6), heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 8 + 8 * 2 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 8; @@ -76815,10 +76352,8 @@ int sp_ecc_mulmod_add_256(const mp_int* km, const ecc_point* gm, err = sp_256_point_to_ecc_point_8(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -78252,28 +77787,12 @@ static int sp_256_ecc_mulmod_base_8(sp_point_256* r, const sp_digit* k, */ int sp_ecc_mulmod_base_256(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[1]; - sp_digit k[8]; -#endif + SP_DECL_VAR(sp_point_256, point, 1); + SP_DECL_VAR(sp_digit, k, 8); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 8, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 8, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(k, 8, km); @@ -78283,10 +77802,8 @@ int sp_ecc_mulmod_base_256(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_256_point_to_ecc_point_8(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -78305,31 +77822,14 @@ int sp_ecc_mulmod_base_256(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_256(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[2]; - sp_digit k[8 + 8 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_256, point, 2); + SP_DECL_VAR(sp_digit, k, 8 + 8 * 2 * 6); sp_point_256* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (8 + 8 * 2 * 6), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 8 + 8 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 8; @@ -78359,10 +77859,8 @@ int sp_ecc_mulmod_base_add_256(const mp_int* km, const ecc_point* am, err = sp_256_point_to_ecc_point_8(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -78501,17 +77999,12 @@ static int sp_256_ecc_gen_k_8(WC_RNG* rng, sp_digit* k) */ int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_DECL_VAR(sp_point_256, point, 2); #else - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - sp_point_256 point[2]; - #else - sp_point_256 point[1]; - #endif - sp_digit k[8]; + SP_DECL_VAR(sp_point_256, point, 1); #endif + SP_DECL_VAR(sp_digit, k, 8); #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN sp_point_256* infinity = NULL; #endif @@ -78520,22 +78013,12 @@ int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, DYNAMIC_TYPE_ECC); - #else - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, DYNAMIC_TYPE_ECC); - #endif - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 8, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_ALLOC_VAR(sp_point_256, point, 2, heap, DYNAMIC_TYPE_ECC); +#else + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); #endif - + SP_ALLOC_VAR(sp_digit, k, 8, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN infinity = point + 1; @@ -78565,11 +78048,9 @@ int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) err = sp_256_point_to_ecc_point_8(point, pub); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); /* point is not sensitive, so no need to zeroize */ - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -78687,34 +78168,16 @@ static void sp_256_to_bin_8(sp_digit* r, byte* a) int sp_ecc_secret_gen_256(const mp_int* priv, const ecc_point* pub, byte* out, word32* outLen, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[1]; - sp_digit k[8]; -#endif + SP_DECL_VAR(sp_point_256, point, 1); + SP_DECL_VAR(sp_digit, k, 8); int err = MP_OKAY; if (*outLen < 32U) { err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 8, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 8, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(k, 8, priv); sp_256_point_from_ecc_point_8(point, pub); @@ -78725,10 +78188,8 @@ int sp_ecc_secret_gen_256(const mp_int* priv, const ecc_point* pub, byte* out, *outLen = 32; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -79870,13 +79331,8 @@ static int sp_256_calc_s_8(sp_digit* s, const sp_digit* r, sp_digit* k, int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* e = NULL; - sp_point_256* point = NULL; -#else - sp_digit e[7 * 2 * 8]; - sp_point_256 point[1]; -#endif + SP_DECL_VAR(sp_digit, e, 7 * 2 * 8); + SP_DECL_VAR(sp_point_256, point, 1); sp_digit* x = NULL; sp_digit* k = NULL; sp_digit* r = NULL; @@ -79888,21 +79344,8 @@ int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - e = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7 * 2 * 8, heap, - DYNAMIC_TYPE_ECC); - if (e == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, e, 7 * 2 * 8, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { x = e + 2 * 8; k = e + 4 * 8; @@ -79967,24 +79410,8 @@ int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, err = sp_256_to_mp(s, sm); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (e != NULL) -#endif - { - ForceZero(e, sizeof(sp_digit) * 7 * 2 * 8); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(e, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (point != NULL) -#endif - { - ForceZero(point, sizeof(sp_point_256)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(point, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_digit, e, 7 * 2 * 8, heap, DYNAMIC_TYPE_ECC); return err; } @@ -81112,13 +80539,8 @@ int sp_ecc_verify_256(const byte* hash, word32 hashLen, const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* rm, const mp_int* sm, int* res, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u1 = NULL; - sp_point_256* p1 = NULL; -#else - sp_digit u1[18 * 8]; - sp_point_256 p1[2]; -#endif + SP_DECL_VAR(sp_digit, u1, 18 * 8); + SP_DECL_VAR(sp_point_256, p1, 2); sp_digit* u2 = NULL; sp_digit* s = NULL; sp_digit* tmp = NULL; @@ -81127,21 +80549,8 @@ int sp_ecc_verify_256(const byte* hash, word32 hashLen, const mp_int* pX, sp_int32 c = 0; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p1 = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (p1 == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - u1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 8, heap, - DYNAMIC_TYPE_ECC); - if (u1 == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, u1, 18 * 8, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p1, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { u2 = u1 + 2 * 8; s = u1 + 4 * 8; @@ -81198,11 +80607,8 @@ int sp_ecc_verify_256(const byte* hash, word32 hashLen, const mp_int* pX, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u1, heap, DYNAMIC_TYPE_ECC); - XFREE(p1, heap, DYNAMIC_TYPE_ECC); -#endif - + SP_FREE_VAR(p1, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(u1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -81368,21 +80774,13 @@ int sp_ecc_verify_256_nb(sp_ecc_ctx_t* sp_ctx, const byte* hash, static int sp_256_ecc_is_point_8(const sp_point_256* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[8 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 8 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 8 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 8 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 8; @@ -81406,9 +80804,7 @@ static int sp_256_ecc_is_point_8(const sp_point_256* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -81422,21 +80818,11 @@ static int sp_256_ecc_is_point_8(const sp_point_256* point, */ int sp_ecc_is_point_256(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* pub = NULL; -#else - sp_point_256 pub[1]; -#endif + SP_DECL_VAR(sp_point_256, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_256*)XMALLOC(sizeof(sp_point_256), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_256, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(pub->x, 8, pX); sp_256_from_mp(pub->y, 8, pY); @@ -81445,9 +80831,7 @@ int sp_ecc_is_point_256(const mp_int* pX, const mp_int* pY) err = sp_256_ecc_is_point_8(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -81466,13 +80850,8 @@ int sp_ecc_is_point_256(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_256(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_256* pub = NULL; -#else - sp_digit priv[8]; - sp_point_256 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 8); + SP_DECL_VAR(sp_point_256, pub, 2); sp_point_256* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -81487,21 +80866,8 @@ int sp_ecc_check_key_256(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 8, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 8, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -81553,10 +80919,8 @@ int sp_ecc_check_key_256(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } @@ -81580,32 +80944,13 @@ int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_256* p = NULL; -#else - sp_digit tmp[2 * 8 * 6]; - sp_point_256 p[2]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 8 * 6); + SP_DECL_VAR(sp_point_256, p, 2); sp_point_256* q = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 8 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p, 2, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { q = p + 1; @@ -81633,10 +80978,8 @@ int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_256_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -81655,30 +80998,12 @@ int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_256* p = NULL; -#else - sp_digit tmp[2 * 8 * 2]; - sp_point_256 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 8 * 2); + SP_DECL_VAR(sp_point_256, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 2, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 8 * 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(p->x, 8, pX); sp_256_from_mp(p->y, 8, pY); @@ -81699,10 +81024,8 @@ int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_256_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -81717,30 +81040,13 @@ int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, */ int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_256* p = NULL; -#else - sp_digit tmp[2 * 8 * 4]; - sp_point_256 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 8 * 4); + SP_DECL_VAR(sp_point_256, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 4, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 8 * 4, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(p->x, 8, pX); sp_256_from_mp(p->y, 8, pY); @@ -81761,10 +81067,8 @@ int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ) err = sp_256_to_mp(p->z, pZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -81777,21 +81081,11 @@ int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ) */ static int sp_256_mont_sqrt_8(sp_digit* y) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 8]; -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 8); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 8, NULL, DYNAMIC_TYPE_ECC); - if (t1 == NULL) { - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, t1, 4 * 8, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 8; @@ -81828,9 +81122,7 @@ static int sp_256_mont_sqrt_8(sp_digit* y) } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -81845,20 +81137,11 @@ static int sp_256_mont_sqrt_8(sp_digit* y) */ int sp_ecc_uncompress_256(mp_int* xm, int odd, mp_int* ym) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* x = NULL; -#else - sp_digit x[4 * 8]; -#endif + SP_DECL_VAR(sp_digit, x, 4 * 8); sp_digit* y = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - x = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 8, NULL, DYNAMIC_TYPE_ECC); - if (x == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, x, 4 * 8, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { y = x + 2 * 8; @@ -81893,9 +81176,7 @@ int sp_ecc_uncompress_256(mp_int* xm, int odd, mp_int* ym) err = sp_256_to_mp(y, ym); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(x, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(x, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -91083,23 +90364,13 @@ WC_OMIT_FRAME_POINTER static sp_digit sp_384_add_12(sp_digit* r, */ static int sp_384_mod_mul_norm_12(sp_digit* r, const sp_digit* a, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - int64_t* t = NULL; -#else - int64_t t[12]; -#endif + SP_DECL_VAR(int64_t, t, 12); int64_t o; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (int64_t*)XMALLOC(sizeof(int64_t) * 12, NULL, DYNAMIC_TYPE_ECC); - if (t == NULL) { - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(int64_t, t, 12, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { /* 1 0 0 0 0 0 0 0 1 1 0 -1 */ t[0] = 0 + (int64_t)a[0] + (int64_t)a[8] + (int64_t)a[9] - (int64_t)a[11]; @@ -91168,9 +90439,7 @@ static int sp_384_mod_mul_norm_12(sp_digit* r, const sp_digit* a, const sp_digit r[11] = (sp_digit)t[11]; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -93641,20 +92910,11 @@ static void sp_384_get_point_16_12(sp_point_384* r, const sp_point_384* table, static int sp_384_ecc_mulmod_fast_12(sp_point_384* r, const sp_point_384* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_384 t[16 + 1]; - sp_digit tmp[2 * 12 * 6]; -#endif + SP_DECL_VAR(sp_point_384, t, 16 + 1); + SP_DECL_VAR(sp_digit, tmp, 2 * 12 * 6); sp_point_384* rt = NULL; #ifndef WC_NO_CACHE_RESISTANT -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* p = NULL; -#else - sp_point_384 p[1]; -#endif + SP_DECL_VAR(sp_point_384, p, 1); #endif /* !WC_NO_CACHE_RESISTANT */ sp_digit n; int i; @@ -93666,26 +92926,11 @@ static int sp_384_ecc_mulmod_fast_12(sp_point_384* r, const sp_point_384* g, con (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * (16 + 1), - heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - #ifndef WC_NO_CACHE_RESISTANT - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384), - heap, DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - #endif - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 6, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } + SP_ALLOC_VAR(sp_point_384, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); +#ifndef WC_NO_CACHE_RESISTANT + SP_ALLOC_VAR(sp_point_384, p, 1, heap, DYNAMIC_TYPE_ECC); #endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 12 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 16; @@ -93777,35 +93022,12 @@ static int sp_384_ecc_mulmod_fast_12(sp_point_384* r, const sp_point_384* g, con } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 12 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 12 * 6, heap, + DYNAMIC_TYPE_ECC); #ifndef WC_NO_CACHE_RESISTANT -#ifdef WOLFSSL_SP_SMALL_STACK - if (p != NULL) -#endif - { - ForceZero(p, sizeof(sp_point_384)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(p, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_384, p, 1, heap, DYNAMIC_TYPE_ECC); #endif /* !WC_NO_CACHE_RESISTANT */ -#ifdef WOLFSSL_SP_SMALL_STACK - if (t != NULL) -#endif - { - ForceZero(t, sizeof(sp_point_384) * 17); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_384, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -94030,11 +93252,7 @@ static void sp_384_proj_point_add_qz1_12(sp_point_384* r, static int sp_384_gen_stripe_table_12(const sp_point_384* a, sp_table_entry_384* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* t = NULL; -#else - sp_point_384 t[3]; -#endif + SP_DECL_VAR(sp_point_384, t, 3); sp_point_384* s1 = NULL; sp_point_384* s2 = NULL; int i; @@ -94043,13 +93261,7 @@ static int sp_384_gen_stripe_table_12(const sp_point_384* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_384, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -94098,9 +93310,7 @@ static int sp_384_gen_stripe_table_12(const sp_point_384* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -94192,13 +93402,8 @@ static int sp_384_ecc_mulmod_stripe_12(sp_point_384* r, const sp_point_384* g, const sp_table_entry_384* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_384 rt[2]; - sp_digit t[2 * 12 * 6]; -#endif + SP_DECL_VAR(sp_point_384, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 12 * 6); sp_point_384* p = NULL; int i; int j; @@ -94212,19 +93417,8 @@ static int sp_384_ecc_mulmod_stripe_12(sp_point_384* r, const sp_point_384* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 12 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -94278,10 +93472,8 @@ static int sp_384_ecc_mulmod_stripe_12(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -94398,20 +93590,11 @@ static int sp_384_ecc_mulmod_12(sp_point_384* r, const sp_point_384* g, #ifndef FP_ECC return sp_384_ecc_mulmod_fast_12(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 12 * 7]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 12 * 7); sp_cache_384_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 7, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 12 * 7, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -94444,9 +93627,7 @@ static int sp_384_ecc_mulmod_12(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -94467,11 +93648,7 @@ static int sp_384_ecc_mulmod_12(sp_point_384* r, const sp_point_384* g, static int sp_384_gen_stripe_table_12(const sp_point_384* a, sp_table_entry_384* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* t = NULL; -#else - sp_point_384 t[3]; -#endif + SP_DECL_VAR(sp_point_384, t, 3); sp_point_384* s1 = NULL; sp_point_384* s2 = NULL; int i; @@ -94480,13 +93657,7 @@ static int sp_384_gen_stripe_table_12(const sp_point_384* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_384, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -94535,9 +93706,7 @@ static int sp_384_gen_stripe_table_12(const sp_point_384* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -94629,13 +93798,8 @@ static int sp_384_ecc_mulmod_stripe_12(sp_point_384* r, const sp_point_384* g, const sp_table_entry_384* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_384 rt[2]; - sp_digit t[2 * 12 * 6]; -#endif + SP_DECL_VAR(sp_point_384, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 12 * 6); sp_point_384* p = NULL; int i; int j; @@ -94649,19 +93813,8 @@ static int sp_384_ecc_mulmod_stripe_12(sp_point_384* r, const sp_point_384* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 12 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -94715,10 +93868,8 @@ static int sp_384_ecc_mulmod_stripe_12(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -94835,20 +93986,11 @@ static int sp_384_ecc_mulmod_12(sp_point_384* r, const sp_point_384* g, #ifndef FP_ECC return sp_384_ecc_mulmod_fast_12(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 12 * 7]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 12 * 7); sp_cache_384_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 7, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 12 * 7, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -94881,9 +94023,7 @@ static int sp_384_ecc_mulmod_12(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -94902,28 +94042,12 @@ static int sp_384_ecc_mulmod_12(sp_point_384* r, const sp_point_384* g, int sp_ecc_mulmod_384(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[1]; - sp_digit k[12]; -#endif + SP_DECL_VAR(sp_point_384, point, 1); + SP_DECL_VAR(sp_digit, k, 12); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 12, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 12, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(k, 12, km); sp_384_point_from_ecc_point_12(point, gm); @@ -94934,10 +94058,8 @@ int sp_ecc_mulmod_384(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_384_point_to_ecc_point_12(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -94957,31 +94079,14 @@ int sp_ecc_mulmod_384(const mp_int* km, const ecc_point* gm, ecc_point* r, int sp_ecc_mulmod_add_384(const mp_int* km, const ecc_point* gm, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[2]; - sp_digit k[12 + 12 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_384, point, 2); + SP_DECL_VAR(sp_digit, k, 12 + 12 * 2 * 6); sp_point_384* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (12 + 12 * 2 * 6), heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 12 + 12 * 2 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 12; @@ -95012,10 +94117,8 @@ int sp_ecc_mulmod_add_384(const mp_int* km, const ecc_point* gm, err = sp_384_point_to_ecc_point_12(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -96449,28 +95552,12 @@ static int sp_384_ecc_mulmod_base_12(sp_point_384* r, const sp_digit* k, */ int sp_ecc_mulmod_base_384(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[1]; - sp_digit k[12]; -#endif + SP_DECL_VAR(sp_point_384, point, 1); + SP_DECL_VAR(sp_digit, k, 12); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 12, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 12, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(k, 12, km); @@ -96480,10 +95567,8 @@ int sp_ecc_mulmod_base_384(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_384_point_to_ecc_point_12(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -96502,31 +95587,14 @@ int sp_ecc_mulmod_base_384(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_384(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[2]; - sp_digit k[12 + 12 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_384, point, 2); + SP_DECL_VAR(sp_digit, k, 12 + 12 * 2 * 6); sp_point_384* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (12 + 12 * 2 * 6), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 12 + 12 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 12; @@ -96556,10 +95624,8 @@ int sp_ecc_mulmod_base_add_384(const mp_int* km, const ecc_point* am, err = sp_384_point_to_ecc_point_12(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -96704,17 +95770,12 @@ static int sp_384_ecc_gen_k_12(WC_RNG* rng, sp_digit* k) */ int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_DECL_VAR(sp_point_384, point, 2); #else - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - sp_point_384 point[2]; - #else - sp_point_384 point[1]; - #endif - sp_digit k[12]; + SP_DECL_VAR(sp_point_384, point, 1); #endif + SP_DECL_VAR(sp_digit, k, 12); #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN sp_point_384* infinity = NULL; #endif @@ -96723,22 +95784,12 @@ int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, DYNAMIC_TYPE_ECC); - #else - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, DYNAMIC_TYPE_ECC); - #endif - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 12, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_ALLOC_VAR(sp_point_384, point, 2, heap, DYNAMIC_TYPE_ECC); +#else + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); #endif - + SP_ALLOC_VAR(sp_digit, k, 12, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN infinity = point + 1; @@ -96768,11 +95819,9 @@ int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) err = sp_384_point_to_ecc_point_12(point, pub); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); /* point is not sensitive, so no need to zeroize */ - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -96890,34 +95939,16 @@ static void sp_384_to_bin_12(sp_digit* r, byte* a) int sp_ecc_secret_gen_384(const mp_int* priv, const ecc_point* pub, byte* out, word32* outLen, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[1]; - sp_digit k[12]; -#endif + SP_DECL_VAR(sp_point_384, point, 1); + SP_DECL_VAR(sp_digit, k, 12); int err = MP_OKAY; if (*outLen < 48U) { err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 12, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 12, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(k, 12, priv); sp_384_point_from_ecc_point_12(point, pub); @@ -96928,10 +95959,8 @@ int sp_ecc_secret_gen_384(const mp_int* priv, const ecc_point* pub, byte* out, *outLen = 48; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -98179,13 +97208,8 @@ static int sp_384_calc_s_12(sp_digit* s, const sp_digit* r, sp_digit* k, int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng, const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* e = NULL; - sp_point_384* point = NULL; -#else - sp_digit e[7 * 2 * 12]; - sp_point_384 point[1]; -#endif + SP_DECL_VAR(sp_digit, e, 7 * 2 * 12); + SP_DECL_VAR(sp_point_384, point, 1); sp_digit* x = NULL; sp_digit* k = NULL; sp_digit* r = NULL; @@ -98197,21 +97221,8 @@ int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - e = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7 * 2 * 12, heap, - DYNAMIC_TYPE_ECC); - if (e == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, e, 7 * 2 * 12, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { x = e + 2 * 12; k = e + 4 * 12; @@ -98276,24 +97287,8 @@ int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng, err = sp_384_to_mp(s, sm); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (e != NULL) -#endif - { - ForceZero(e, sizeof(sp_digit) * 7 * 2 * 12); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(e, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (point != NULL) -#endif - { - ForceZero(point, sizeof(sp_point_384)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(point, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_digit, e, 7 * 2 * 12, heap, DYNAMIC_TYPE_ECC); return err; } @@ -99543,13 +98538,8 @@ int sp_ecc_verify_384(const byte* hash, word32 hashLen, const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* rm, const mp_int* sm, int* res, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u1 = NULL; - sp_point_384* p1 = NULL; -#else - sp_digit u1[18 * 12]; - sp_point_384 p1[2]; -#endif + SP_DECL_VAR(sp_digit, u1, 18 * 12); + SP_DECL_VAR(sp_point_384, p1, 2); sp_digit* u2 = NULL; sp_digit* s = NULL; sp_digit* tmp = NULL; @@ -99558,21 +98548,8 @@ int sp_ecc_verify_384(const byte* hash, word32 hashLen, const mp_int* pX, sp_int32 c = 0; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p1 = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (p1 == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - u1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 12, heap, - DYNAMIC_TYPE_ECC); - if (u1 == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, u1, 18 * 12, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p1, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { u2 = u1 + 2 * 12; s = u1 + 4 * 12; @@ -99629,11 +98606,8 @@ int sp_ecc_verify_384(const byte* hash, word32 hashLen, const mp_int* pX, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u1, heap, DYNAMIC_TYPE_ECC); - XFREE(p1, heap, DYNAMIC_TYPE_ECC); -#endif - + SP_FREE_VAR(p1, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(u1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -99799,21 +98773,13 @@ int sp_ecc_verify_384_nb(sp_ecc_ctx_t* sp_ctx, const byte* hash, static int sp_384_ecc_is_point_12(const sp_point_384* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[12 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 12 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 12 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 12 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 12; @@ -99837,9 +98803,7 @@ static int sp_384_ecc_is_point_12(const sp_point_384* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -99853,21 +98817,11 @@ static int sp_384_ecc_is_point_12(const sp_point_384* point, */ int sp_ecc_is_point_384(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* pub = NULL; -#else - sp_point_384 pub[1]; -#endif + SP_DECL_VAR(sp_point_384, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_384*)XMALLOC(sizeof(sp_point_384), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_384, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(pub->x, 12, pX); sp_384_from_mp(pub->y, 12, pY); @@ -99876,9 +98830,7 @@ int sp_ecc_is_point_384(const mp_int* pX, const mp_int* pY) err = sp_384_ecc_is_point_12(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -99897,13 +98849,8 @@ int sp_ecc_is_point_384(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_384(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_384* pub = NULL; -#else - sp_digit priv[12]; - sp_point_384 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 12); + SP_DECL_VAR(sp_point_384, pub, 2); sp_point_384* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -99918,21 +98865,8 @@ int sp_ecc_check_key_384(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 12, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 12, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -99984,10 +98918,8 @@ int sp_ecc_check_key_384(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } @@ -100011,32 +98943,13 @@ int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_384* p = NULL; -#else - sp_digit tmp[2 * 12 * 6]; - sp_point_384 p[2]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 12 * 6); + SP_DECL_VAR(sp_point_384, p, 2); sp_point_384* q = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 12 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p, 2, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { q = p + 1; @@ -100064,10 +98977,8 @@ int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_384_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -100086,30 +98997,12 @@ int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_384* p = NULL; -#else - sp_digit tmp[2 * 12 * 2]; - sp_point_384 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 12 * 2); + SP_DECL_VAR(sp_point_384, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 2, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 12 * 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(p->x, 12, pX); sp_384_from_mp(p->y, 12, pY); @@ -100130,10 +99023,8 @@ int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_384_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -100148,30 +99039,13 @@ int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, */ int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_384* p = NULL; -#else - sp_digit tmp[2 * 12 * 6]; - sp_point_384 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 12 * 6); + SP_DECL_VAR(sp_point_384, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 12 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(p->x, 12, pX); sp_384_from_mp(p->y, 12, pY); @@ -100192,10 +99066,8 @@ int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ) err = sp_384_to_mp(p->z, pZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -100208,23 +99080,14 @@ int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ) */ static int sp_384_mont_sqrt_12(sp_digit* y) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[5 * 2 * 12]; -#endif + SP_DECL_VAR(sp_digit, t1, 5 * 2 * 12); sp_digit* t2 = NULL; sp_digit* t3 = NULL; sp_digit* t4 = NULL; sp_digit* t5 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 5 * 2 * 12, NULL, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, t1, 5 * 2 * 12, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 12; t3 = t1 + 4 * 12; @@ -100289,9 +99152,7 @@ static int sp_384_mont_sqrt_12(sp_digit* y) } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -100306,20 +99167,11 @@ static int sp_384_mont_sqrt_12(sp_digit* y) */ int sp_ecc_uncompress_384(mp_int* xm, int odd, mp_int* ym) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* x = NULL; -#else - sp_digit x[4 * 12]; -#endif + SP_DECL_VAR(sp_digit, x, 4 * 12); sp_digit* y = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - x = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 12, NULL, DYNAMIC_TYPE_ECC); - if (x == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, x, 4 * 12, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { y = x + 2 * 12; @@ -100354,9 +99206,7 @@ int sp_ecc_uncompress_384(mp_int* xm, int odd, mp_int* ym) err = sp_384_to_mp(y, ym); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(x, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(x, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -121169,20 +120019,11 @@ static void sp_521_get_point_16_17(sp_point_521* r, const sp_point_521* table, static int sp_521_ecc_mulmod_fast_17(sp_point_521* r, const sp_point_521* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_521 t[16 + 1]; - sp_digit tmp[2 * 17 * 6]; -#endif + SP_DECL_VAR(sp_point_521, t, 16 + 1); + SP_DECL_VAR(sp_digit, tmp, 2 * 17 * 6); sp_point_521* rt = NULL; #ifndef WC_NO_CACHE_RESISTANT -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* p = NULL; -#else - sp_point_521 p[1]; -#endif + SP_DECL_VAR(sp_point_521, p, 1); #endif /* !WC_NO_CACHE_RESISTANT */ sp_digit n; int i; @@ -121194,26 +120035,11 @@ static int sp_521_ecc_mulmod_fast_17(sp_point_521* r, const sp_point_521* g, con (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * (16 + 1), - heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - #ifndef WC_NO_CACHE_RESISTANT - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521), - heap, DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - #endif - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 6, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } + SP_ALLOC_VAR(sp_point_521, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); +#ifndef WC_NO_CACHE_RESISTANT + SP_ALLOC_VAR(sp_point_521, p, 1, heap, DYNAMIC_TYPE_ECC); #endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 17 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 16; @@ -121309,35 +120135,12 @@ static int sp_521_ecc_mulmod_fast_17(sp_point_521* r, const sp_point_521* g, con } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 17 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 17 * 6, heap, + DYNAMIC_TYPE_ECC); #ifndef WC_NO_CACHE_RESISTANT -#ifdef WOLFSSL_SP_SMALL_STACK - if (p != NULL) -#endif - { - ForceZero(p, sizeof(sp_point_521)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(p, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_521, p, 1, heap, DYNAMIC_TYPE_ECC); #endif /* !WC_NO_CACHE_RESISTANT */ -#ifdef WOLFSSL_SP_SMALL_STACK - if (t != NULL) -#endif - { - ForceZero(t, sizeof(sp_point_521) * 17); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_521, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -121562,11 +120365,7 @@ static void sp_521_proj_point_add_qz1_17(sp_point_521* r, static int sp_521_gen_stripe_table_17(const sp_point_521* a, sp_table_entry_521* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* t = NULL; -#else - sp_point_521 t[3]; -#endif + SP_DECL_VAR(sp_point_521, t, 3); sp_point_521* s1 = NULL; sp_point_521* s2 = NULL; int i; @@ -121575,13 +120374,7 @@ static int sp_521_gen_stripe_table_17(const sp_point_521* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_521, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -121630,9 +120423,7 @@ static int sp_521_gen_stripe_table_17(const sp_point_521* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -121744,13 +120535,8 @@ static int sp_521_ecc_mulmod_stripe_17(sp_point_521* r, const sp_point_521* g, const sp_table_entry_521* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_521 rt[2]; - sp_digit t[2 * 17 * 6]; -#endif + SP_DECL_VAR(sp_point_521, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 17 * 6); sp_point_521* p = NULL; int i; int j; @@ -121764,19 +120550,8 @@ static int sp_521_ecc_mulmod_stripe_17(sp_point_521* r, const sp_point_521* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 17 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -121830,10 +120605,8 @@ static int sp_521_ecc_mulmod_stripe_17(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -121950,20 +120723,11 @@ static int sp_521_ecc_mulmod_17(sp_point_521* r, const sp_point_521* g, #ifndef FP_ECC return sp_521_ecc_mulmod_fast_17(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 17 * 6]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 17 * 6); sp_cache_521_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 6, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 17 * 6, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -121996,9 +120760,7 @@ static int sp_521_ecc_mulmod_17(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -122019,11 +120781,7 @@ static int sp_521_ecc_mulmod_17(sp_point_521* r, const sp_point_521* g, static int sp_521_gen_stripe_table_17(const sp_point_521* a, sp_table_entry_521* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* t = NULL; -#else - sp_point_521 t[3]; -#endif + SP_DECL_VAR(sp_point_521, t, 3); sp_point_521* s1 = NULL; sp_point_521* s2 = NULL; int i; @@ -122032,13 +120790,7 @@ static int sp_521_gen_stripe_table_17(const sp_point_521* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_521, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -122087,9 +120839,7 @@ static int sp_521_gen_stripe_table_17(const sp_point_521* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -122201,13 +120951,8 @@ static int sp_521_ecc_mulmod_stripe_17(sp_point_521* r, const sp_point_521* g, const sp_table_entry_521* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_521 rt[2]; - sp_digit t[2 * 17 * 6]; -#endif + SP_DECL_VAR(sp_point_521, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 17 * 6); sp_point_521* p = NULL; int i; int j; @@ -122221,19 +120966,8 @@ static int sp_521_ecc_mulmod_stripe_17(sp_point_521* r, const sp_point_521* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 17 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -122287,10 +121021,8 @@ static int sp_521_ecc_mulmod_stripe_17(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -122407,20 +121139,11 @@ static int sp_521_ecc_mulmod_17(sp_point_521* r, const sp_point_521* g, #ifndef FP_ECC return sp_521_ecc_mulmod_fast_17(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 17 * 6]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 17 * 6); sp_cache_521_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 6, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 17 * 6, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -122453,9 +121176,7 @@ static int sp_521_ecc_mulmod_17(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -122474,28 +121195,12 @@ static int sp_521_ecc_mulmod_17(sp_point_521* r, const sp_point_521* g, int sp_ecc_mulmod_521(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[1]; - sp_digit k[17]; -#endif + SP_DECL_VAR(sp_point_521, point, 1); + SP_DECL_VAR(sp_digit, k, 17); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 17, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 17, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(k, 17, km); sp_521_point_from_ecc_point_17(point, gm); @@ -122506,10 +121211,8 @@ int sp_ecc_mulmod_521(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_521_point_to_ecc_point_17(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -122529,31 +121232,14 @@ int sp_ecc_mulmod_521(const mp_int* km, const ecc_point* gm, ecc_point* r, int sp_ecc_mulmod_add_521(const mp_int* km, const ecc_point* gm, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[2]; - sp_digit k[17 + 17 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_521, point, 2); + SP_DECL_VAR(sp_digit, k, 17 + 17 * 2 * 6); sp_point_521* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (17 + 17 * 2 * 6), heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 17 + 17 * 2 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 17; @@ -122584,10 +121270,8 @@ int sp_ecc_mulmod_add_521(const mp_int* km, const ecc_point* gm, err = sp_521_point_to_ecc_point_17(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -124565,28 +123249,12 @@ static int sp_521_ecc_mulmod_base_17(sp_point_521* r, const sp_digit* k, */ int sp_ecc_mulmod_base_521(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[1]; - sp_digit k[17]; -#endif + SP_DECL_VAR(sp_point_521, point, 1); + SP_DECL_VAR(sp_digit, k, 17); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 17, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 17, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(k, 17, km); @@ -124596,10 +123264,8 @@ int sp_ecc_mulmod_base_521(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_521_point_to_ecc_point_17(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -124618,31 +123284,14 @@ int sp_ecc_mulmod_base_521(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_521(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[2]; - sp_digit k[17 + 17 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_521, point, 2); + SP_DECL_VAR(sp_digit, k, 17 + 17 * 2 * 6); sp_point_521* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (17 + 17 * 2 * 6), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 17 + 17 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 17; @@ -124672,10 +123321,8 @@ int sp_ecc_mulmod_base_add_521(const mp_int* km, const ecc_point* am, err = sp_521_point_to_ecc_point_17(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -124830,17 +123477,12 @@ static int sp_521_ecc_gen_k_17(WC_RNG* rng, sp_digit* k) */ int sp_ecc_make_key_521(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_DECL_VAR(sp_point_521, point, 2); #else - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - sp_point_521 point[2]; - #else - sp_point_521 point[1]; - #endif - sp_digit k[17]; + SP_DECL_VAR(sp_point_521, point, 1); #endif + SP_DECL_VAR(sp_digit, k, 17); #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN sp_point_521* infinity = NULL; #endif @@ -124849,22 +123491,12 @@ int sp_ecc_make_key_521(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, DYNAMIC_TYPE_ECC); - #else - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, DYNAMIC_TYPE_ECC); - #endif - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 17, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_ALLOC_VAR(sp_point_521, point, 2, heap, DYNAMIC_TYPE_ECC); +#else + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); #endif - + SP_ALLOC_VAR(sp_digit, k, 17, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN infinity = point + 1; @@ -124894,11 +123526,9 @@ int sp_ecc_make_key_521(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) err = sp_521_point_to_ecc_point_17(point, pub); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); /* point is not sensitive, so no need to zeroize */ - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -125018,34 +123648,16 @@ static void sp_521_to_bin_17(sp_digit* r, byte* a) int sp_ecc_secret_gen_521(const mp_int* priv, const ecc_point* pub, byte* out, word32* outLen, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[1]; - sp_digit k[17]; -#endif + SP_DECL_VAR(sp_point_521, point, 1); + SP_DECL_VAR(sp_digit, k, 17); int err = MP_OKAY; if (*outLen < 65U) { err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 17, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 17, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(k, 17, priv); sp_521_point_from_ecc_point_17(point, pub); @@ -125056,10 +123668,8 @@ int sp_ecc_secret_gen_521(const mp_int* priv, const ecc_point* pub, byte* out, *outLen = 66; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -126982,13 +125592,8 @@ static int sp_521_calc_s_17(sp_digit* s, const sp_digit* r, sp_digit* k, int sp_ecc_sign_521(const byte* hash, word32 hashLen, WC_RNG* rng, const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* e = NULL; - sp_point_521* point = NULL; -#else - sp_digit e[7 * 2 * 17]; - sp_point_521 point[1]; -#endif + SP_DECL_VAR(sp_digit, e, 7 * 2 * 17); + SP_DECL_VAR(sp_point_521, point, 1); sp_digit* x = NULL; sp_digit* k = NULL; sp_digit* r = NULL; @@ -127000,21 +125605,8 @@ int sp_ecc_sign_521(const byte* hash, word32 hashLen, WC_RNG* rng, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - e = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7 * 2 * 17, heap, - DYNAMIC_TYPE_ECC); - if (e == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, e, 7 * 2 * 17, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { x = e + 2 * 17; k = e + 4 * 17; @@ -127084,24 +125676,8 @@ int sp_ecc_sign_521(const byte* hash, word32 hashLen, WC_RNG* rng, err = sp_521_to_mp(s, sm); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (e != NULL) -#endif - { - ForceZero(e, sizeof(sp_digit) * 7 * 2 * 17); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(e, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (point != NULL) -#endif - { - ForceZero(point, sizeof(sp_point_521)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(point, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_digit, e, 7 * 2 * 17, heap, DYNAMIC_TYPE_ECC); return err; } @@ -128873,13 +127449,8 @@ int sp_ecc_verify_521(const byte* hash, word32 hashLen, const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* rm, const mp_int* sm, int* res, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u1 = NULL; - sp_point_521* p1 = NULL; -#else - sp_digit u1[18 * 17]; - sp_point_521 p1[2]; -#endif + SP_DECL_VAR(sp_digit, u1, 18 * 17); + SP_DECL_VAR(sp_point_521, p1, 2); sp_digit* u2 = NULL; sp_digit* s = NULL; sp_digit* tmp = NULL; @@ -128888,21 +127459,8 @@ int sp_ecc_verify_521(const byte* hash, word32 hashLen, const mp_int* pX, sp_int32 c = 0; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p1 = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (p1 == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - u1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 17, heap, - DYNAMIC_TYPE_ECC); - if (u1 == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, u1, 18 * 17, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p1, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { u2 = u1 + 2 * 17; s = u1 + 4 * 17; @@ -128963,11 +127521,8 @@ int sp_ecc_verify_521(const byte* hash, word32 hashLen, const mp_int* pX, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u1, heap, DYNAMIC_TYPE_ECC); - XFREE(p1, heap, DYNAMIC_TYPE_ECC); -#endif - + SP_FREE_VAR(p1, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(u1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -129136,21 +127691,13 @@ int sp_ecc_verify_521_nb(sp_ecc_ctx_t* sp_ctx, const byte* hash, static int sp_521_ecc_is_point_17(const sp_point_521* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[17 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 17 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 17 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 17 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 17; @@ -129174,9 +127721,7 @@ static int sp_521_ecc_is_point_17(const sp_point_521* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -129190,21 +127735,11 @@ static int sp_521_ecc_is_point_17(const sp_point_521* point, */ int sp_ecc_is_point_521(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* pub = NULL; -#else - sp_point_521 pub[1]; -#endif + SP_DECL_VAR(sp_point_521, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_521*)XMALLOC(sizeof(sp_point_521), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_521, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(pub->x, 17, pX); sp_521_from_mp(pub->y, 17, pY); @@ -129213,9 +127748,7 @@ int sp_ecc_is_point_521(const mp_int* pX, const mp_int* pY) err = sp_521_ecc_is_point_17(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -129234,13 +127767,8 @@ int sp_ecc_is_point_521(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_521(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_521* pub = NULL; -#else - sp_digit priv[17]; - sp_point_521 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 17); + SP_DECL_VAR(sp_point_521, pub, 2); sp_point_521* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -129255,21 +127783,8 @@ int sp_ecc_check_key_521(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 17, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 17, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -129321,10 +127836,8 @@ int sp_ecc_check_key_521(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } @@ -129348,32 +127861,13 @@ int sp_ecc_proj_add_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_521* p = NULL; -#else - sp_digit tmp[2 * 17 * 6]; - sp_point_521 p[2]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 17 * 6); + SP_DECL_VAR(sp_point_521, p, 2); sp_point_521* q = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 17 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p, 2, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { q = p + 1; @@ -129401,10 +127895,8 @@ int sp_ecc_proj_add_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_521_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -129423,30 +127915,12 @@ int sp_ecc_proj_add_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, int sp_ecc_proj_dbl_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_521* p = NULL; -#else - sp_digit tmp[2 * 17 * 2]; - sp_point_521 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 17 * 2); + SP_DECL_VAR(sp_point_521, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 2, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 17 * 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(p->x, 17, pX); sp_521_from_mp(p->y, 17, pY); @@ -129467,10 +127941,8 @@ int sp_ecc_proj_dbl_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_521_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -129485,30 +127957,13 @@ int sp_ecc_proj_dbl_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, */ int sp_ecc_map_521(mp_int* pX, mp_int* pY, mp_int* pZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_521* p = NULL; -#else - sp_digit tmp[2 * 17 * 5]; - sp_point_521 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 17 * 5); + SP_DECL_VAR(sp_point_521, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 5, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 17 * 5, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(p->x, 17, pX); sp_521_from_mp(p->y, 17, pY); @@ -129529,10 +127984,8 @@ int sp_ecc_map_521(mp_int* pX, mp_int* pY, mp_int* pZ) err = sp_521_to_mp(p->z, pZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -129552,19 +128005,10 @@ static const word32 p521_sqrt_power[17] = { */ static int sp_521_mont_sqrt_17(sp_digit* y) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t = NULL; -#else - sp_digit t[2 * 17]; -#endif + SP_DECL_VAR(sp_digit, t, 2 * 17); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17, NULL, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, t, 2 * 17, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { { @@ -129580,9 +128024,7 @@ static int sp_521_mont_sqrt_17(sp_digit* y) } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -129597,20 +128039,11 @@ static int sp_521_mont_sqrt_17(sp_digit* y) */ int sp_ecc_uncompress_521(mp_int* xm, int odd, mp_int* ym) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* x = NULL; -#else - sp_digit x[4 * 17]; -#endif + SP_DECL_VAR(sp_digit, x, 4 * 17); sp_digit* y = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - x = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 17, NULL, DYNAMIC_TYPE_ECC); - if (x == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, x, 4 * 17, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { y = x + 2 * 17; @@ -129645,9 +128078,7 @@ int sp_ecc_uncompress_521(mp_int* xm, int odd, mp_int* ym) err = sp_521_to_mp(y, ym); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(x, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(x, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -151489,13 +149920,8 @@ static int sp_1024_proj_point_add_32_nb(sp_ecc_ctx_t* sp_ctx, sp_point_1024* r, static int sp_1024_ecc_mulmod_fast_32(sp_point_1024* r, const sp_point_1024* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_1024 t[16 + 1]; - sp_digit tmp[2 * 32 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, t, 16 + 1); + SP_DECL_VAR(sp_digit, tmp, 2 * 32 * 37); sp_point_1024* rt = NULL; sp_digit n; int i; @@ -151507,18 +149933,8 @@ static int sp_1024_ecc_mulmod_fast_32(sp_point_1024* r, const sp_point_1024* g, (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * (16 + 1), - heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 32 * 37, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_point_1024, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 32 * 37, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 16; @@ -151590,24 +150006,9 @@ static int sp_1024_ecc_mulmod_fast_32(sp_point_1024* r, const sp_point_1024* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 32 * 37); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (t != NULL) -#endif - { - ForceZero(t, sizeof(sp_point_1024) * 17); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 32 * 37, heap, + DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_point_1024, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -151832,11 +150233,7 @@ static void sp_1024_proj_point_add_qz1_32(sp_point_1024* r, static int sp_1024_gen_stripe_table_32(const sp_point_1024* a, sp_table_entry_1024* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* t = NULL; -#else - sp_point_1024 t[3]; -#endif + SP_DECL_VAR(sp_point_1024, t, 3); sp_point_1024* s1 = NULL; sp_point_1024* s2 = NULL; int i; @@ -151845,13 +150242,7 @@ static int sp_1024_gen_stripe_table_32(const sp_point_1024* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_1024, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -151900,9 +150291,7 @@ static int sp_1024_gen_stripe_table_32(const sp_point_1024* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -151928,13 +150317,8 @@ static int sp_1024_ecc_mulmod_stripe_32(sp_point_1024* r, const sp_point_1024* g const sp_table_entry_1024* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_1024 rt[2]; - sp_digit t[2 * 32 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 32 * 37); sp_point_1024* p = NULL; int i; int j; @@ -151948,19 +150332,8 @@ static int sp_1024_ecc_mulmod_stripe_32(sp_point_1024* r, const sp_point_1024* g (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 32 * 37, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 32 * 37, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -151999,10 +150372,8 @@ static int sp_1024_ecc_mulmod_stripe_32(sp_point_1024* r, const sp_point_1024* g } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -152119,20 +150490,11 @@ static int sp_1024_ecc_mulmod_32(sp_point_1024* r, const sp_point_1024* g, #ifndef FP_ECC return sp_1024_ecc_mulmod_fast_32(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 32 * 38]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 32 * 38); sp_cache_1024_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 32 * 38, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 32 * 38, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -152165,9 +150527,7 @@ static int sp_1024_ecc_mulmod_32(sp_point_1024* r, const sp_point_1024* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -152188,11 +150548,7 @@ static int sp_1024_ecc_mulmod_32(sp_point_1024* r, const sp_point_1024* g, static int sp_1024_gen_stripe_table_32(const sp_point_1024* a, sp_table_entry_1024* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* t = NULL; -#else - sp_point_1024 t[3]; -#endif + SP_DECL_VAR(sp_point_1024, t, 3); sp_point_1024* s1 = NULL; sp_point_1024* s2 = NULL; int i; @@ -152201,13 +150557,7 @@ static int sp_1024_gen_stripe_table_32(const sp_point_1024* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_1024, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -152256,9 +150606,7 @@ static int sp_1024_gen_stripe_table_32(const sp_point_1024* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -152284,13 +150632,8 @@ static int sp_1024_ecc_mulmod_stripe_32(sp_point_1024* r, const sp_point_1024* g const sp_table_entry_1024* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_1024 rt[2]; - sp_digit t[2 * 32 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 32 * 37); sp_point_1024* p = NULL; int i; int j; @@ -152304,19 +150647,8 @@ static int sp_1024_ecc_mulmod_stripe_32(sp_point_1024* r, const sp_point_1024* g (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 32 * 37, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 32 * 37, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -152355,10 +150687,8 @@ static int sp_1024_ecc_mulmod_stripe_32(sp_point_1024* r, const sp_point_1024* g } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -152475,20 +150805,11 @@ static int sp_1024_ecc_mulmod_32(sp_point_1024* r, const sp_point_1024* g, #ifndef FP_ECC return sp_1024_ecc_mulmod_fast_32(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 32 * 38]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 32 * 38); sp_cache_1024_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 32 * 38, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 32 * 38, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -152521,9 +150842,7 @@ static int sp_1024_ecc_mulmod_32(sp_point_1024* r, const sp_point_1024* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -152542,28 +150861,12 @@ static int sp_1024_ecc_mulmod_32(sp_point_1024* r, const sp_point_1024* g, int sp_ecc_mulmod_1024(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[1]; - sp_digit k[32]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, k, 32); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 32, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(k, 32, km); sp_1024_point_from_ecc_point_32(point, gm); @@ -152574,10 +150877,8 @@ int sp_ecc_mulmod_1024(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_1024_point_to_ecc_point_32(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -156179,28 +154480,12 @@ static int sp_1024_ecc_mulmod_base_32(sp_point_1024* r, const sp_digit* k, */ int sp_ecc_mulmod_base_1024(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[1]; - sp_digit k[32]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, k, 32); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 32, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(k, 32, km); @@ -156210,10 +154495,8 @@ int sp_ecc_mulmod_base_1024(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_1024_point_to_ecc_point_32(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -156232,31 +154515,14 @@ int sp_ecc_mulmod_base_1024(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[2]; - sp_digit k[32 + 32 * 2 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, point, 2); + SP_DECL_VAR(sp_digit, k, 32 + 32 * 2 * 37); sp_point_1024* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (32 + 32 * 2 * 37), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 32 + 32 * 2 * 37, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 32; @@ -156286,10 +154552,8 @@ int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am, err = sp_1024_point_to_ecc_point_32(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -156307,13 +154571,8 @@ int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am, int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* t = NULL; -#else - sp_point_1024 point[1]; - sp_digit t[38 * 2 * 32]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, t, 38 * 2 * 32); int err = MP_OKAY; if ((gm == NULL) || (len == NULL)) { @@ -156328,21 +154587,8 @@ int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 38 * 2 * 32, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 38 * 2 * 32, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_point_from_ecc_point_32(point, gm); err = sp_1024_gen_stripe_table_32(point, @@ -156352,10 +154598,8 @@ int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, *len = sizeof(sp_table_entry_1024) * 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -156408,28 +154652,12 @@ int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, int sp_ecc_mulmod_table_1024(const mp_int* km, const ecc_point* gm, byte* table, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[1]; - sp_digit k[32]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, k, 32); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) { - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32, heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 32, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(k, 32, km); sp_1024_point_from_ecc_point_32(point, gm); @@ -156446,10 +154674,8 @@ int sp_ecc_mulmod_table_1024(const mp_int* km, const ecc_point* gm, byte* table, err = sp_1024_point_to_ecc_point_32(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); return err; } @@ -159910,22 +158136,14 @@ static void sp_1024_from_bin(sp_digit* r, int size, const byte* a, int n) static int sp_1024_ecc_is_point_32(const sp_point_1024* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[32 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 32 * 4); sp_digit* t2 = NULL; sp_int32 n; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 32 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 32; @@ -159952,9 +158170,7 @@ static int sp_1024_ecc_is_point_32(const sp_point_1024* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -159968,21 +158184,11 @@ static int sp_1024_ecc_is_point_32(const sp_point_1024* point, */ int sp_ecc_is_point_1024(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* pub = NULL; -#else - sp_point_1024 pub[1]; -#endif + SP_DECL_VAR(sp_point_1024, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_1024, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(pub->x, 32, pX); sp_1024_from_mp(pub->y, 32, pY); @@ -159991,9 +158197,7 @@ int sp_ecc_is_point_1024(const mp_int* pX, const mp_int* pY) err = sp_1024_ecc_is_point_32(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -160012,13 +158216,8 @@ int sp_ecc_is_point_1024(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_1024(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_1024* pub = NULL; -#else - sp_digit priv[32]; - sp_point_1024 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 32); + SP_DECL_VAR(sp_point_1024, pub, 2); sp_point_1024* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -160033,21 +158232,8 @@ int sp_ecc_check_key_1024(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 32, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_1024, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -160099,10 +158285,8 @@ int sp_ecc_check_key_1024(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } diff --git a/wolfcrypt/src/sp_arm64.c b/wolfcrypt/src/sp_arm64.c index f34b59882..617b78bb6 100644 --- a/wolfcrypt/src/sp_arm64.c +++ b/wolfcrypt/src/sp_arm64.c @@ -57,6 +57,57 @@ #define __volatile__ volatile #endif +#ifdef WOLFSSL_SP_SMALL_STACK + #define SP_DECL_VAR(TYPE, NAME, CNT) \ + TYPE* NAME = NULL + #define SP_ALLOC_VAR(TYPE, NAME, CNT, HEAP, DT) \ + if (err == MP_OKAY) { \ + (NAME) = (TYPE*)XMALLOC(sizeof(TYPE) * (CNT), (HEAP), DT); \ + if ((NAME) == NULL) { \ + err = MEMORY_E; \ + } \ + } + + #define SP_VAR_OK(NAME) ((NAME) != NULL) + + #define SP_FREE_VAR(NAME, HEAP, DT) \ + XFREE(NAME, (HEAP), DT) + #define SP_ZEROFREE_VAR(TYPE, NAME, CNT, HEAP, DT) \ + do { \ + if ((NAME) != NULL) { \ + ForceZero(NAME, sizeof(TYPE) * (CNT)); \ + } \ + SP_FREE_VAR(NAME, HEAP, DT); \ + } while (0) + #define SP_ZEROFREE_VAR_ALT(TYPE, NAME, FZ_NAME, CNT, HEAP, DT) \ + do { \ + if ((FZ_NAME) != NULL) { \ + ForceZero(FZ_NAME, sizeof(TYPE) * (CNT)); \ + } \ + SP_FREE_VAR(NAME, HEAP, DT); \ + } while (0) +#else + #define SP_DECL_VAR(TYPE, NAME, CNT) \ + TYPE NAME[CNT] + #define SP_ALLOC_VAR(TYPE, NAME, CNT, HEAP, DT) \ + WC_DO_NOTHING + #define SP_VAR_OK(NAME) (1) + #define SP_FREE_VAR(NAME, HEAP, DT) \ + WC_DO_NOTHING + #define SP_ZEROFREE_VAR(TYPE, NAME, CNT, HEAP, DT) \ + do { \ + if ((NAME) != NULL) { \ + ForceZero(NAME, sizeof(TYPE) * (CNT)); \ + } \ + } while (0) + #define SP_ZEROFREE_VAR_ALT(TYPE, NAME, FZ_NAME, CNT, HEAP, DT) \ + do { \ + if ((FZ_NAME) != NULL) { \ + ForceZero(FZ_NAME, sizeof(TYPE) * (CNT)); \ + } \ + } while (0) +#endif + #ifdef WOLFSSL_SP_ARM64_ASM #define SP_PRINT_NUM(var, name, total, words, bits) \ do { \ @@ -4033,11 +4084,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[16 * 32]; -#endif + SP_DECL_VAR(sp_digit, td, 16 * 32); sp_digit* t[16]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -4052,15 +4099,7 @@ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 32), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 16 * 32, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) { @@ -4163,9 +4202,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -4184,11 +4221,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[32 * 32]; -#endif + SP_DECL_VAR(sp_digit, td, 32 * 32); sp_digit* t[32]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -4203,15 +4236,7 @@ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 32), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 32 * 32, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) { @@ -4331,9 +4356,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -5640,11 +5663,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[32 * 64]; -#endif + SP_DECL_VAR(sp_digit, td, 32 * 64); sp_digit* t[32]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -5659,15 +5678,7 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 64), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 32 * 64, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) { @@ -5787,9 +5798,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -5808,11 +5817,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[64 * 64]; -#endif + SP_DECL_VAR(sp_digit, td, 64 * 64); sp_digit* t[64]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -5827,15 +5832,7 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (64 * 64), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 64 * 64, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<64; i++) { @@ -5988,9 +5985,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -6014,11 +6009,7 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[32 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 32 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit *ah = NULL; @@ -6036,15 +6027,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 32 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { ah = a + 32; r = a + 32 * 2; @@ -6151,9 +6134,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; } @@ -6217,11 +6198,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, const mp_int* qim, const mp_int* mm, byte* out, word32* outLen) { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[32 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 32 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -6251,15 +6228,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 32 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 32; m = a + 64; @@ -6276,25 +6245,11 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 32); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 32, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[16 * 11]; -#endif + SP_DECL_VAR(sp_digit, a, 16 * 11); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -6325,15 +6280,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 11, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 16 * 11, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 32 * 2; q = p + 16; @@ -6373,15 +6320,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 16 * 11); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 16 * 11, NULL, DYNAMIC_TYPE_RSA); #endif /* SP_RSA_PRIVATE_EXP_D || RSA_LOW_MEM */ return err; } @@ -6725,11 +6664,7 @@ static void sp_2048_lshift_32(sp_digit* r, const 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[97]; -#endif + SP_DECL_VAR(sp_digit, td, 97); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -6745,15 +6680,7 @@ static int sp_2048_mod_exp_2_32(sp_digit* r, const sp_digit* e, int bits, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 97, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 97, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 64; @@ -6831,9 +6758,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -13218,11 +13143,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[16 * 48]; -#endif + SP_DECL_VAR(sp_digit, td, 16 * 48); sp_digit* t[16]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -13237,15 +13158,7 @@ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 48), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 16 * 48, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) { @@ -13348,9 +13261,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -13369,11 +13280,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[32 * 48]; -#endif + SP_DECL_VAR(sp_digit, td, 32 * 48); sp_digit* t[32]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -13388,15 +13295,7 @@ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 48), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 32 * 48, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) { @@ -13516,9 +13415,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -15217,11 +15114,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[8 * 96]; -#endif + SP_DECL_VAR(sp_digit, td, 8 * 96); sp_digit* t[8]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -15236,15 +15129,7 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 96), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 8 * 96, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<8; i++) { @@ -15338,9 +15223,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -15359,11 +15242,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[16 * 96]; -#endif + SP_DECL_VAR(sp_digit, td, 16 * 96); sp_digit* t[16]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -15378,15 +15257,7 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 96), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 16 * 96, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) { @@ -15489,9 +15360,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -15515,11 +15384,7 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[48 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 48 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit *ah = NULL; @@ -15537,15 +15402,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 48 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 48 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { ah = a + 48; r = a + 48 * 2; @@ -15652,9 +15509,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; } @@ -15718,11 +15573,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, const mp_int* qim, const mp_int* mm, byte* out, word32* outLen) { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[48 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 48 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -15752,15 +15603,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 48 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 48 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 48; m = a + 96; @@ -15777,25 +15620,11 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 48); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 48, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[24 * 11]; -#endif + SP_DECL_VAR(sp_digit, a, 24 * 11); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -15826,15 +15655,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 24 * 11, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 24 * 11, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 48 * 2; q = p + 24; @@ -15874,15 +15695,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 24 * 11); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 24 * 11, NULL, DYNAMIC_TYPE_RSA); #endif /* SP_RSA_PRIVATE_EXP_D || RSA_LOW_MEM */ return err; } @@ -16322,11 +16135,7 @@ static void sp_3072_lshift_48(sp_digit* r, const 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[145]; -#endif + SP_DECL_VAR(sp_digit, td, 145); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -16342,15 +16151,7 @@ static int sp_3072_mod_exp_2_48(sp_digit* r, const sp_digit* e, int bits, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 145, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 145, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 96; @@ -16428,9 +16229,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -20324,11 +20123,7 @@ static WC_INLINE int sp_4096_mod_64(sp_digit* r, const sp_digit* a, const sp_dig 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[8 * 128]; -#endif + SP_DECL_VAR(sp_digit, td, 8 * 128); sp_digit* t[8]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -20343,15 +20138,7 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 128), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 8 * 128, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<8; i++) { @@ -20445,9 +20232,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -20466,11 +20251,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[16 * 128]; -#endif + SP_DECL_VAR(sp_digit, td, 16 * 128); sp_digit* t[16]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -20485,15 +20266,7 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 128), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 16 * 128, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) { @@ -20596,9 +20369,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -20622,11 +20393,7 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[64 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 64 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit *ah = NULL; @@ -20644,15 +20411,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 64 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 64 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { ah = a + 64; r = a + 64 * 2; @@ -20759,9 +20518,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; } @@ -20825,11 +20582,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, const mp_int* qim, const mp_int* mm, byte* out, word32* outLen) { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[64 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 64 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -20859,15 +20612,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 64 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 64 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 64; m = a + 128; @@ -20884,25 +20629,11 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 64); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 64, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[32 * 11]; -#endif + SP_DECL_VAR(sp_digit, a, 32 * 11); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -20933,15 +20664,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 11, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 32 * 11, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 64 * 2; q = p + 32; @@ -20981,15 +20704,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 32 * 11); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 32 * 11, NULL, DYNAMIC_TYPE_RSA); #endif /* SP_RSA_PRIVATE_EXP_D || RSA_LOW_MEM */ return err; } @@ -21525,11 +21240,7 @@ static void sp_4096_lshift_64(sp_digit* r, const 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[193]; -#endif + SP_DECL_VAR(sp_digit, td, 193); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -21545,15 +21256,7 @@ static int sp_4096_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 193, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 193, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 128; @@ -21631,9 +21334,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -24353,13 +24054,8 @@ SP_NOINLINE static void sp_256_get_point_33_4(sp_point_256* r, static int sp_256_ecc_mulmod_win_add_sub_4(sp_point_256* r, const sp_point_256* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_256 t[33+2]; - sp_digit tmp[2 * 4 * 6]; -#endif + SP_DECL_VAR(sp_point_256, t, 33+2); + SP_DECL_VAR(sp_digit, tmp, 2 * 4 * 6); sp_point_256* rt = NULL; sp_point_256* p = NULL; sp_digit* negy; @@ -24371,19 +24067,8 @@ static int sp_256_ecc_mulmod_win_add_sub_4(sp_point_256* r, const sp_point_256* (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * - (33+2), heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 4 * 6, - heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, t, 33+2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 4 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 33; p = t + 33+1; @@ -24467,10 +24152,8 @@ static int sp_256_ecc_mulmod_win_add_sub_4(sp_point_256* r, const sp_point_256* } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; } @@ -24642,11 +24325,7 @@ static void sp_256_proj_to_affine_4(sp_point_256* a, sp_digit* t) static int sp_256_gen_stripe_table_4(const sp_point_256* a, sp_table_entry_256* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* t = NULL; -#else - sp_point_256 t[3]; -#endif + SP_DECL_VAR(sp_point_256, t, 3); sp_point_256* s1 = NULL; sp_point_256* s2 = NULL; int i; @@ -24655,13 +24334,7 @@ static int sp_256_gen_stripe_table_4(const sp_point_256* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_256, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -24710,9 +24383,7 @@ static int sp_256_gen_stripe_table_4(const sp_point_256* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -24794,13 +24465,8 @@ static int sp_256_ecc_mulmod_stripe_4(sp_point_256* r, const sp_point_256* g, const sp_table_entry_256* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_256 rt[2]; - sp_digit t[2 * 4 * 5]; -#endif + SP_DECL_VAR(sp_point_256, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 4 * 5); sp_point_256* p = NULL; int i; int j; @@ -24814,19 +24480,8 @@ static int sp_256_ecc_mulmod_stripe_4(sp_point_256* r, const sp_point_256* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 4 * 5, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 4 * 5, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -24880,10 +24535,8 @@ static int sp_256_ecc_mulmod_stripe_4(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -25001,20 +24654,11 @@ static int sp_256_ecc_mulmod_4(sp_point_256* r, const sp_point_256* g, #ifndef FP_ECC return sp_256_ecc_mulmod_win_add_sub_4(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 4 * 5]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 4 * 5); sp_cache_256_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 4 * 5, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 4 * 5, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -25047,9 +24691,7 @@ static int sp_256_ecc_mulmod_4(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -25072,11 +24714,7 @@ static int sp_256_ecc_mulmod_4(sp_point_256* r, const sp_point_256* g, static int sp_256_gen_stripe_table_4(const sp_point_256* a, sp_table_entry_256* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* t = NULL; -#else - sp_point_256 t[3]; -#endif + SP_DECL_VAR(sp_point_256, t, 3); sp_point_256* s1 = NULL; sp_point_256* s2 = NULL; int i; @@ -25085,13 +24723,7 @@ static int sp_256_gen_stripe_table_4(const sp_point_256* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_256, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -25140,9 +24772,7 @@ static int sp_256_gen_stripe_table_4(const sp_point_256* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -25224,13 +24854,8 @@ static int sp_256_ecc_mulmod_stripe_4(sp_point_256* r, const sp_point_256* g, const sp_table_entry_256* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_256 rt[2]; - sp_digit t[2 * 4 * 5]; -#endif + SP_DECL_VAR(sp_point_256, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 4 * 5); sp_point_256* p = NULL; int i; int j; @@ -25244,19 +24869,8 @@ static int sp_256_ecc_mulmod_stripe_4(sp_point_256* r, const sp_point_256* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 4 * 5, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 4 * 5, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -25310,10 +24924,8 @@ static int sp_256_ecc_mulmod_stripe_4(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -25431,20 +25043,11 @@ static int sp_256_ecc_mulmod_4(sp_point_256* r, const sp_point_256* g, #ifndef FP_ECC return sp_256_ecc_mulmod_win_add_sub_4(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 4 * 5]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 4 * 5); sp_cache_256_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 4 * 5, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 4 * 5, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -25477,9 +25080,7 @@ static int sp_256_ecc_mulmod_4(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -25498,28 +25099,12 @@ static int sp_256_ecc_mulmod_4(sp_point_256* r, const sp_point_256* g, int sp_ecc_mulmod_256(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[1]; - sp_digit k[4]; -#endif + SP_DECL_VAR(sp_point_256, point, 1); + SP_DECL_VAR(sp_digit, k, 4); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(k, 4, km); sp_256_point_from_ecc_point_4(point, gm); @@ -25530,10 +25115,8 @@ int sp_ecc_mulmod_256(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_256_point_to_ecc_point_4(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -25553,31 +25136,14 @@ int sp_ecc_mulmod_256(const mp_int* km, const ecc_point* gm, ecc_point* r, int sp_ecc_mulmod_add_256(const mp_int* km, const ecc_point* gm, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[2]; - sp_digit k[4 + 4 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_256, point, 2); + SP_DECL_VAR(sp_digit, k, 4 + 4 * 2 * 6); sp_point_256* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (4 + 4 * 2 * 6), heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 4 + 4 * 2 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 4; @@ -25608,10 +25174,8 @@ int sp_ecc_mulmod_add_256(const mp_int* km, const ecc_point* gm, err = sp_256_point_to_ecc_point_4(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -39375,13 +38939,8 @@ static int sp_256_ecc_mulmod_add_only_4(sp_point_256* r, const sp_point_256* g, const sp_table_entry_256* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* rt = NULL; - sp_digit* tmp = NULL; -#else - sp_point_256 rt[2]; - sp_digit tmp[2 * 4 * 5]; -#endif + SP_DECL_VAR(sp_point_256, rt, 2); + SP_DECL_VAR(sp_digit, tmp, 2 * 4 * 5); sp_point_256* p = NULL; sp_digit* negy = NULL; int i; @@ -39393,19 +38952,8 @@ static int sp_256_ecc_mulmod_add_only_4(sp_point_256* r, const sp_point_256* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 4 * 5, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 4 * 5, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { negy = tmp; p = rt + 1; @@ -39454,18 +39002,8 @@ static int sp_256_ecc_mulmod_add_only_4(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 4 * 5); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 4 * 5, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); return err; } @@ -39499,28 +39037,12 @@ static int sp_256_ecc_mulmod_base_4(sp_point_256* r, const sp_digit* k, */ int sp_ecc_mulmod_base_256(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[1]; - sp_digit k[4]; -#endif + SP_DECL_VAR(sp_point_256, point, 1); + SP_DECL_VAR(sp_digit, k, 4); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(k, 4, km); @@ -39530,10 +39052,8 @@ int sp_ecc_mulmod_base_256(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_256_point_to_ecc_point_4(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -39552,31 +39072,14 @@ int sp_ecc_mulmod_base_256(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_256(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[2]; - sp_digit k[4 + 4 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_256, point, 2); + SP_DECL_VAR(sp_digit, k, 4 + 4 * 2 * 6); sp_point_256* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (4 + 4 * 2 * 6), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 4 + 4 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 4; @@ -39606,10 +39109,8 @@ int sp_ecc_mulmod_base_add_256(const mp_int* km, const ecc_point* am, err = sp_256_point_to_ecc_point_4(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -39798,17 +39299,12 @@ static int sp_256_ecc_gen_k_4(WC_RNG* rng, sp_digit* k) */ int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_DECL_VAR(sp_point_256, point, 2); #else - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - sp_point_256 point[2]; - #else - sp_point_256 point[1]; - #endif - sp_digit k[4]; + SP_DECL_VAR(sp_point_256, point, 1); #endif + SP_DECL_VAR(sp_digit, k, 4); #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN sp_point_256* infinity = NULL; #endif @@ -39817,22 +39313,12 @@ int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, DYNAMIC_TYPE_ECC); - #else - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, DYNAMIC_TYPE_ECC); - #endif - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_ALLOC_VAR(sp_point_256, point, 2, heap, DYNAMIC_TYPE_ECC); +#else + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); #endif - + SP_ALLOC_VAR(sp_digit, k, 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN infinity = point + 1; @@ -39862,11 +39348,9 @@ int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) err = sp_256_point_to_ecc_point_4(point, pub); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); /* point is not sensitive, so no need to zeroize */ - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -39990,34 +39474,16 @@ static void sp_256_to_bin_4(sp_digit* r, byte* a) int sp_ecc_secret_gen_256(const mp_int* priv, const ecc_point* pub, byte* out, word32* outLen, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[1]; - sp_digit k[4]; -#endif + SP_DECL_VAR(sp_point_256, point, 1); + SP_DECL_VAR(sp_digit, k, 4); int err = MP_OKAY; if (*outLen < 32U) { err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(k, 4, priv); sp_256_point_from_ecc_point_4(point, pub); @@ -40028,10 +39494,8 @@ int sp_ecc_secret_gen_256(const mp_int* priv, const ecc_point* pub, byte* out, *outLen = 32; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -41153,13 +40617,8 @@ static int sp_256_calc_s_4(sp_digit* s, const sp_digit* r, sp_digit* k, int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* e = NULL; - sp_point_256* point = NULL; -#else - sp_digit e[10 * 2 * 4]; - sp_point_256 point[1]; -#endif + SP_DECL_VAR(sp_digit, e, 10 * 2 * 4); + SP_DECL_VAR(sp_point_256, point, 1); sp_digit* x = NULL; sp_digit* k = NULL; sp_digit* r = NULL; @@ -41171,21 +40630,8 @@ int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - e = (sp_digit*)XMALLOC(sizeof(sp_digit) * 10 * 2 * 4, heap, - DYNAMIC_TYPE_ECC); - if (e == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, e, 10 * 2 * 4, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { x = e + 2 * 4; k = e + 4 * 4; @@ -41250,24 +40696,8 @@ int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, err = sp_256_to_mp(s, sm); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (e != NULL) -#endif - { - ForceZero(e, sizeof(sp_digit) * 10 * 2 * 4); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(e, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (point != NULL) -#endif - { - ForceZero(point, sizeof(sp_point_256)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(point, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_digit, e, 10 * 2 * 4, heap, DYNAMIC_TYPE_ECC); return err; } @@ -41811,13 +41241,8 @@ int sp_ecc_verify_256(const byte* hash, word32 hashLen, const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* rm, const mp_int* sm, int* res, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u1 = NULL; - sp_point_256* p1 = NULL; -#else - sp_digit u1[18 * 4]; - sp_point_256 p1[2]; -#endif + SP_DECL_VAR(sp_digit, u1, 18 * 4); + SP_DECL_VAR(sp_point_256, p1, 2); sp_digit* u2 = NULL; sp_digit* s = NULL; sp_digit* tmp = NULL; @@ -41826,21 +41251,8 @@ int sp_ecc_verify_256(const byte* hash, word32 hashLen, const mp_int* pX, sp_int64 c = 0; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p1 = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (p1 == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - u1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 4, heap, - DYNAMIC_TYPE_ECC); - if (u1 == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, u1, 18 * 4, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p1, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { u2 = u1 + 2 * 4; s = u1 + 4 * 4; @@ -41897,11 +41309,8 @@ int sp_ecc_verify_256(const byte* hash, word32 hashLen, const mp_int* pX, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u1, heap, DYNAMIC_TYPE_ECC); - XFREE(p1, heap, DYNAMIC_TYPE_ECC); -#endif - + SP_FREE_VAR(p1, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(u1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -42111,21 +41520,13 @@ SP_NOINLINE static void sp_256_mont_add_4(sp_digit* r, const sp_digit* a, static int sp_256_ecc_is_point_4(const sp_point_256* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 4 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 4; @@ -42149,9 +41550,7 @@ static int sp_256_ecc_is_point_4(const sp_point_256* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -42165,21 +41564,11 @@ static int sp_256_ecc_is_point_4(const sp_point_256* point, */ int sp_ecc_is_point_256(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* pub = NULL; -#else - sp_point_256 pub[1]; -#endif + SP_DECL_VAR(sp_point_256, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_256*)XMALLOC(sizeof(sp_point_256), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_256, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(pub->x, 4, pX); sp_256_from_mp(pub->y, 4, pY); @@ -42188,9 +41577,7 @@ int sp_ecc_is_point_256(const mp_int* pX, const mp_int* pY) err = sp_256_ecc_is_point_4(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -42209,13 +41596,8 @@ int sp_ecc_is_point_256(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_256(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_256* pub = NULL; -#else - sp_digit priv[4]; - sp_point_256 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 4); + SP_DECL_VAR(sp_point_256, pub, 2); sp_point_256* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -42230,21 +41612,8 @@ int sp_ecc_check_key_256(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 4, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -42296,10 +41665,8 @@ int sp_ecc_check_key_256(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } @@ -42323,32 +41690,13 @@ int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_256* p = NULL; -#else - sp_digit tmp[2 * 4 * 6]; - sp_point_256 p[2]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 4 * 6); + SP_DECL_VAR(sp_point_256, p, 2); sp_point_256* q = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 4 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 4 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p, 2, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { q = p + 1; @@ -42376,10 +41724,8 @@ int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_256_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -42398,30 +41744,12 @@ int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_256* p = NULL; -#else - sp_digit tmp[2 * 4 * 2]; - sp_point_256 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 4 * 2); + SP_DECL_VAR(sp_point_256, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 4 * 2, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 4 * 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(p->x, 4, pX); sp_256_from_mp(p->y, 4, pY); @@ -42442,10 +41770,8 @@ int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_256_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -42460,30 +41786,13 @@ int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, */ int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_256* p = NULL; -#else - sp_digit tmp[2 * 4 * 4]; - sp_point_256 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 4 * 4); + SP_DECL_VAR(sp_point_256, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 4 * 4, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 4 * 4, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(p->x, 4, pX); sp_256_from_mp(p->y, 4, pY); @@ -42504,10 +41813,8 @@ int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ) err = sp_256_to_mp(p->z, pZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -42520,21 +41827,11 @@ int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ) */ static int sp_256_mont_sqrt_4(sp_digit* y) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 4, NULL, DYNAMIC_TYPE_ECC); - if (t1 == NULL) { - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, t1, 4 * 4, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 4; @@ -42571,9 +41868,7 @@ static int sp_256_mont_sqrt_4(sp_digit* y) } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -42588,20 +41883,11 @@ static int sp_256_mont_sqrt_4(sp_digit* y) */ int sp_ecc_uncompress_256(mp_int* xm, int odd, mp_int* ym) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* x = NULL; -#else - sp_digit x[4 * 4]; -#endif + SP_DECL_VAR(sp_digit, x, 4 * 4); sp_digit* y = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - x = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 4, NULL, DYNAMIC_TYPE_ECC); - if (x == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, x, 4 * 4, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { y = x + 2 * 4; @@ -42636,9 +41922,7 @@ int sp_ecc_uncompress_256(mp_int* xm, int odd, mp_int* ym) err = sp_256_to_mp(y, ym); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(x, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(x, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -43327,23 +42611,14 @@ static sp_digit sp_384_sub_6(sp_digit* r, const sp_digit* a, */ static int sp_384_mod_mul_norm_6(sp_digit* r, const sp_digit* a, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - int64_t* t = NULL; -#else - int64_t t[2 * 12]; -#endif + SP_DECL_VAR(int64_t, t, 2 * 12); int64_t* a32 = NULL; int64_t o; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (int64_t*)XMALLOC(sizeof(int64_t) * 2 * 12, NULL, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(int64_t, t, 2 * 12, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { a32 = t + 12; @@ -43421,9 +42696,7 @@ static int sp_384_mod_mul_norm_6(sp_digit* r, const sp_digit* a, const sp_digit* r[5] = (sp_digit)((t[11] << 32) | t[10]); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -45324,13 +44597,8 @@ SP_NOINLINE static void sp_384_get_point_33_6(sp_point_384* r, static int sp_384_ecc_mulmod_win_add_sub_6(sp_point_384* r, const sp_point_384* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_384 t[33+2]; - sp_digit tmp[2 * 6 * 6]; -#endif + SP_DECL_VAR(sp_point_384, t, 33+2); + SP_DECL_VAR(sp_digit, tmp, 2 * 6 * 6); sp_point_384* rt = NULL; sp_point_384* p = NULL; sp_digit* negy; @@ -45342,19 +44610,8 @@ static int sp_384_ecc_mulmod_win_add_sub_6(sp_point_384* r, const sp_point_384* (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * - (33+2), heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 6 * 6, - heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, t, 33+2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 6 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 33; p = t + 33+1; @@ -45438,10 +44695,8 @@ static int sp_384_ecc_mulmod_win_add_sub_6(sp_point_384* r, const sp_point_384* } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; } @@ -45573,11 +44828,7 @@ static void sp_384_proj_to_affine_6(sp_point_384* a, sp_digit* t) static int sp_384_gen_stripe_table_6(const sp_point_384* a, sp_table_entry_384* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* t = NULL; -#else - sp_point_384 t[3]; -#endif + SP_DECL_VAR(sp_point_384, t, 3); sp_point_384* s1 = NULL; sp_point_384* s2 = NULL; int i; @@ -45586,13 +44837,7 @@ static int sp_384_gen_stripe_table_6(const sp_point_384* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_384, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -45641,9 +44886,7 @@ static int sp_384_gen_stripe_table_6(const sp_point_384* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -45725,13 +44968,8 @@ static int sp_384_ecc_mulmod_stripe_6(sp_point_384* r, const sp_point_384* g, const sp_table_entry_384* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_384 rt[2]; - sp_digit t[2 * 6 * 6]; -#endif + SP_DECL_VAR(sp_point_384, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 6 * 6); sp_point_384* p = NULL; int i; int j; @@ -45745,19 +44983,8 @@ static int sp_384_ecc_mulmod_stripe_6(sp_point_384* r, const sp_point_384* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 6 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 6 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -45811,10 +45038,8 @@ static int sp_384_ecc_mulmod_stripe_6(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -45932,20 +45157,11 @@ static int sp_384_ecc_mulmod_6(sp_point_384* r, const sp_point_384* g, #ifndef FP_ECC return sp_384_ecc_mulmod_win_add_sub_6(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 6 * 7]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 6 * 7); sp_cache_384_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 6 * 7, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 6 * 7, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -45978,9 +45194,7 @@ static int sp_384_ecc_mulmod_6(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -46003,11 +45217,7 @@ static int sp_384_ecc_mulmod_6(sp_point_384* r, const sp_point_384* g, static int sp_384_gen_stripe_table_6(const sp_point_384* a, sp_table_entry_384* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* t = NULL; -#else - sp_point_384 t[3]; -#endif + SP_DECL_VAR(sp_point_384, t, 3); sp_point_384* s1 = NULL; sp_point_384* s2 = NULL; int i; @@ -46016,13 +45226,7 @@ static int sp_384_gen_stripe_table_6(const sp_point_384* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_384, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -46071,9 +45275,7 @@ static int sp_384_gen_stripe_table_6(const sp_point_384* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -46155,13 +45357,8 @@ static int sp_384_ecc_mulmod_stripe_6(sp_point_384* r, const sp_point_384* g, const sp_table_entry_384* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_384 rt[2]; - sp_digit t[2 * 6 * 6]; -#endif + SP_DECL_VAR(sp_point_384, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 6 * 6); sp_point_384* p = NULL; int i; int j; @@ -46175,19 +45372,8 @@ static int sp_384_ecc_mulmod_stripe_6(sp_point_384* r, const sp_point_384* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 6 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 6 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -46241,10 +45427,8 @@ static int sp_384_ecc_mulmod_stripe_6(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -46362,20 +45546,11 @@ static int sp_384_ecc_mulmod_6(sp_point_384* r, const sp_point_384* g, #ifndef FP_ECC return sp_384_ecc_mulmod_win_add_sub_6(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 6 * 7]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 6 * 7); sp_cache_384_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 6 * 7, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 6 * 7, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -46408,9 +45583,7 @@ static int sp_384_ecc_mulmod_6(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -46429,28 +45602,12 @@ static int sp_384_ecc_mulmod_6(sp_point_384* r, const sp_point_384* g, int sp_ecc_mulmod_384(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[1]; - sp_digit k[6]; -#endif + SP_DECL_VAR(sp_point_384, point, 1); + SP_DECL_VAR(sp_digit, k, 6); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 6, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(k, 6, km); sp_384_point_from_ecc_point_6(point, gm); @@ -46461,10 +45618,8 @@ int sp_ecc_mulmod_384(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_384_point_to_ecc_point_6(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -46484,31 +45639,14 @@ int sp_ecc_mulmod_384(const mp_int* km, const ecc_point* gm, ecc_point* r, int sp_ecc_mulmod_add_384(const mp_int* km, const ecc_point* gm, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[2]; - sp_digit k[6 + 6 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_384, point, 2); + SP_DECL_VAR(sp_digit, k, 6 + 6 * 2 * 6); sp_point_384* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (6 + 6 * 2 * 6), heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 6 + 6 * 2 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 6; @@ -46539,10 +45677,8 @@ int sp_ecc_mulmod_add_384(const mp_int* km, const ecc_point* gm, err = sp_384_point_to_ecc_point_6(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -66120,13 +65256,8 @@ static int sp_384_ecc_mulmod_add_only_6(sp_point_384* r, const sp_point_384* g, const sp_table_entry_384* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* rt = NULL; - sp_digit* tmp = NULL; -#else - sp_point_384 rt[2]; - sp_digit tmp[2 * 6 * 6]; -#endif + SP_DECL_VAR(sp_point_384, rt, 2); + SP_DECL_VAR(sp_digit, tmp, 2 * 6 * 6); sp_point_384* p = NULL; sp_digit* negy = NULL; int i; @@ -66138,19 +65269,8 @@ static int sp_384_ecc_mulmod_add_only_6(sp_point_384* r, const sp_point_384* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 6 * 6, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 6 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { negy = tmp; p = rt + 1; @@ -66199,18 +65319,8 @@ static int sp_384_ecc_mulmod_add_only_6(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 6 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 6 * 6, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); return err; } @@ -66244,28 +65354,12 @@ static int sp_384_ecc_mulmod_base_6(sp_point_384* r, const sp_digit* k, */ int sp_ecc_mulmod_base_384(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[1]; - sp_digit k[6]; -#endif + SP_DECL_VAR(sp_point_384, point, 1); + SP_DECL_VAR(sp_digit, k, 6); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 6, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(k, 6, km); @@ -66275,10 +65369,8 @@ int sp_ecc_mulmod_base_384(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_384_point_to_ecc_point_6(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -66297,31 +65389,14 @@ int sp_ecc_mulmod_base_384(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_384(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[2]; - sp_digit k[6 + 6 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_384, point, 2); + SP_DECL_VAR(sp_digit, k, 6 + 6 * 2 * 6); sp_point_384* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (6 + 6 * 2 * 6), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 6 + 6 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 6; @@ -66351,10 +65426,8 @@ int sp_ecc_mulmod_base_add_384(const mp_int* km, const ecc_point* am, err = sp_384_point_to_ecc_point_6(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -66547,17 +65620,12 @@ static int sp_384_ecc_gen_k_6(WC_RNG* rng, sp_digit* k) */ int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_DECL_VAR(sp_point_384, point, 2); #else - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - sp_point_384 point[2]; - #else - sp_point_384 point[1]; - #endif - sp_digit k[6]; + SP_DECL_VAR(sp_point_384, point, 1); #endif + SP_DECL_VAR(sp_digit, k, 6); #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN sp_point_384* infinity = NULL; #endif @@ -66566,22 +65634,12 @@ int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, DYNAMIC_TYPE_ECC); - #else - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, DYNAMIC_TYPE_ECC); - #endif - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 6, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_ALLOC_VAR(sp_point_384, point, 2, heap, DYNAMIC_TYPE_ECC); +#else + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); #endif - + SP_ALLOC_VAR(sp_digit, k, 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN infinity = point + 1; @@ -66611,11 +65669,9 @@ int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) err = sp_384_point_to_ecc_point_6(point, pub); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); /* point is not sensitive, so no need to zeroize */ - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -66739,34 +65795,16 @@ static void sp_384_to_bin_6(sp_digit* r, byte* a) int sp_ecc_secret_gen_384(const mp_int* priv, const ecc_point* pub, byte* out, word32* outLen, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[1]; - sp_digit k[6]; -#endif + SP_DECL_VAR(sp_point_384, point, 1); + SP_DECL_VAR(sp_digit, k, 6); int err = MP_OKAY; if (*outLen < 48U) { err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 6, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(k, 6, priv); sp_384_point_from_ecc_point_6(point, pub); @@ -66777,10 +65815,8 @@ int sp_ecc_secret_gen_384(const mp_int* priv, const ecc_point* pub, byte* out, *outLen = 48; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -67357,13 +66393,8 @@ static int sp_384_calc_s_6(sp_digit* s, const sp_digit* r, sp_digit* k, int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng, const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* e = NULL; - sp_point_384* point = NULL; -#else - sp_digit e[7 * 2 * 6]; - sp_point_384 point[1]; -#endif + SP_DECL_VAR(sp_digit, e, 7 * 2 * 6); + SP_DECL_VAR(sp_point_384, point, 1); sp_digit* x = NULL; sp_digit* k = NULL; sp_digit* r = NULL; @@ -67375,21 +66406,8 @@ int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - e = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7 * 2 * 6, heap, - DYNAMIC_TYPE_ECC); - if (e == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, e, 7 * 2 * 6, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { x = e + 2 * 6; k = e + 4 * 6; @@ -67454,24 +66472,8 @@ int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng, err = sp_384_to_mp(s, sm); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (e != NULL) -#endif - { - ForceZero(e, sizeof(sp_digit) * 7 * 2 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(e, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (point != NULL) -#endif - { - ForceZero(point, sizeof(sp_point_384)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(point, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_digit, e, 7 * 2 * 6, heap, DYNAMIC_TYPE_ECC); return err; } @@ -67917,13 +66919,8 @@ int sp_ecc_verify_384(const byte* hash, word32 hashLen, const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* rm, const mp_int* sm, int* res, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u1 = NULL; - sp_point_384* p1 = NULL; -#else - sp_digit u1[18 * 6]; - sp_point_384 p1[2]; -#endif + SP_DECL_VAR(sp_digit, u1, 18 * 6); + SP_DECL_VAR(sp_point_384, p1, 2); sp_digit* u2 = NULL; sp_digit* s = NULL; sp_digit* tmp = NULL; @@ -67932,21 +66929,8 @@ int sp_ecc_verify_384(const byte* hash, word32 hashLen, const mp_int* pX, sp_int64 c = 0; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p1 = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (p1 == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - u1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 6, heap, - DYNAMIC_TYPE_ECC); - if (u1 == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, u1, 18 * 6, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p1, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { u2 = u1 + 2 * 6; s = u1 + 4 * 6; @@ -68003,11 +66987,8 @@ int sp_ecc_verify_384(const byte* hash, word32 hashLen, const mp_int* pX, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u1, heap, DYNAMIC_TYPE_ECC); - XFREE(p1, heap, DYNAMIC_TYPE_ECC); -#endif - + SP_FREE_VAR(p1, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(u1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -68173,21 +67154,13 @@ int sp_ecc_verify_384_nb(sp_ecc_ctx_t* sp_ctx, const byte* hash, static int sp_384_ecc_is_point_6(const sp_point_384* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[6 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 6 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 6 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 6 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 6; @@ -68211,9 +67184,7 @@ static int sp_384_ecc_is_point_6(const sp_point_384* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -68227,21 +67198,11 @@ static int sp_384_ecc_is_point_6(const sp_point_384* point, */ int sp_ecc_is_point_384(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* pub = NULL; -#else - sp_point_384 pub[1]; -#endif + SP_DECL_VAR(sp_point_384, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_384*)XMALLOC(sizeof(sp_point_384), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_384, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(pub->x, 6, pX); sp_384_from_mp(pub->y, 6, pY); @@ -68250,9 +67211,7 @@ int sp_ecc_is_point_384(const mp_int* pX, const mp_int* pY) err = sp_384_ecc_is_point_6(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -68271,13 +67230,8 @@ int sp_ecc_is_point_384(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_384(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_384* pub = NULL; -#else - sp_digit priv[6]; - sp_point_384 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 6); + SP_DECL_VAR(sp_point_384, pub, 2); sp_point_384* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -68292,21 +67246,8 @@ int sp_ecc_check_key_384(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 6, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 6, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -68358,10 +67299,8 @@ int sp_ecc_check_key_384(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } @@ -68385,32 +67324,13 @@ int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_384* p = NULL; -#else - sp_digit tmp[2 * 6 * 6]; - sp_point_384 p[2]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 6 * 6); + SP_DECL_VAR(sp_point_384, p, 2); sp_point_384* q = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 6 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 6 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p, 2, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { q = p + 1; @@ -68438,10 +67358,8 @@ int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_384_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -68460,30 +67378,12 @@ int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_384* p = NULL; -#else - sp_digit tmp[2 * 6 * 2]; - sp_point_384 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 6 * 2); + SP_DECL_VAR(sp_point_384, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 6 * 2, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 6 * 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(p->x, 6, pX); sp_384_from_mp(p->y, 6, pY); @@ -68504,10 +67404,8 @@ int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_384_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -68522,30 +67420,13 @@ int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, */ int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_384* p = NULL; -#else - sp_digit tmp[2 * 6 * 6]; - sp_point_384 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 6 * 6); + SP_DECL_VAR(sp_point_384, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 6 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 6 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(p->x, 6, pX); sp_384_from_mp(p->y, 6, pY); @@ -68566,10 +67447,8 @@ int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ) err = sp_384_to_mp(p->z, pZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -68582,23 +67461,14 @@ int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ) */ static int sp_384_mont_sqrt_6(sp_digit* y) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[5 * 2 * 6]; -#endif + SP_DECL_VAR(sp_digit, t1, 5 * 2 * 6); sp_digit* t2 = NULL; sp_digit* t3 = NULL; sp_digit* t4 = NULL; sp_digit* t5 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 5 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, t1, 5 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 6; t3 = t1 + 4 * 6; @@ -68663,9 +67533,7 @@ static int sp_384_mont_sqrt_6(sp_digit* y) } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -68680,20 +67548,11 @@ static int sp_384_mont_sqrt_6(sp_digit* y) */ int sp_ecc_uncompress_384(mp_int* xm, int odd, mp_int* ym) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* x = NULL; -#else - sp_digit x[4 * 6]; -#endif + SP_DECL_VAR(sp_digit, x, 4 * 6); sp_digit* y = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - x = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 6, NULL, DYNAMIC_TYPE_ECC); - if (x == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, x, 4 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { y = x + 2 * 6; @@ -68728,9 +67587,7 @@ int sp_ecc_uncompress_384(mp_int* xm, int odd, mp_int* ym) err = sp_384_to_mp(y, ym); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(x, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(x, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -73713,13 +72570,8 @@ SP_NOINLINE static void sp_521_get_point_33_9(sp_point_521* r, static int sp_521_ecc_mulmod_win_add_sub_9(sp_point_521* r, const sp_point_521* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_521 t[33+2]; - sp_digit tmp[2 * 9 * 6]; -#endif + SP_DECL_VAR(sp_point_521, t, 33+2); + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 6); sp_point_521* rt = NULL; sp_point_521* p = NULL; sp_digit* negy; @@ -73731,19 +72583,8 @@ static int sp_521_ecc_mulmod_win_add_sub_9(sp_point_521* r, const sp_point_521* (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * - (33+2), heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, - heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, t, 33+2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 33; p = t + 33+1; @@ -73827,10 +72668,8 @@ static int sp_521_ecc_mulmod_win_add_sub_9(sp_point_521* r, const sp_point_521* } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; } @@ -73962,11 +72801,7 @@ static void sp_521_proj_to_affine_9(sp_point_521* a, sp_digit* t) static int sp_521_gen_stripe_table_9(const sp_point_521* a, sp_table_entry_521* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* t = NULL; -#else - sp_point_521 t[3]; -#endif + SP_DECL_VAR(sp_point_521, t, 3); sp_point_521* s1 = NULL; sp_point_521* s2 = NULL; int i; @@ -73975,13 +72810,7 @@ static int sp_521_gen_stripe_table_9(const sp_point_521* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_521, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -74030,9 +72859,7 @@ static int sp_521_gen_stripe_table_9(const sp_point_521* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -74132,13 +72959,8 @@ static int sp_521_ecc_mulmod_stripe_9(sp_point_521* r, const sp_point_521* g, const sp_table_entry_521* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_521 rt[2]; - sp_digit t[2 * 9 * 6]; -#endif + SP_DECL_VAR(sp_point_521, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 9 * 6); sp_point_521* p = NULL; int i; int j; @@ -74152,19 +72974,8 @@ static int sp_521_ecc_mulmod_stripe_9(sp_point_521* r, const sp_point_521* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -74218,10 +73029,8 @@ static int sp_521_ecc_mulmod_stripe_9(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -74339,20 +73148,11 @@ static int sp_521_ecc_mulmod_9(sp_point_521* r, const sp_point_521* g, #ifndef FP_ECC return sp_521_ecc_mulmod_win_add_sub_9(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 9 * 6]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 6); sp_cache_521_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -74385,9 +73185,7 @@ static int sp_521_ecc_mulmod_9(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -74410,11 +73208,7 @@ static int sp_521_ecc_mulmod_9(sp_point_521* r, const sp_point_521* g, static int sp_521_gen_stripe_table_9(const sp_point_521* a, sp_table_entry_521* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* t = NULL; -#else - sp_point_521 t[3]; -#endif + SP_DECL_VAR(sp_point_521, t, 3); sp_point_521* s1 = NULL; sp_point_521* s2 = NULL; int i; @@ -74423,13 +73217,7 @@ static int sp_521_gen_stripe_table_9(const sp_point_521* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_521, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -74478,9 +73266,7 @@ static int sp_521_gen_stripe_table_9(const sp_point_521* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -74580,13 +73366,8 @@ static int sp_521_ecc_mulmod_stripe_9(sp_point_521* r, const sp_point_521* g, const sp_table_entry_521* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_521 rt[2]; - sp_digit t[2 * 9 * 6]; -#endif + SP_DECL_VAR(sp_point_521, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 9 * 6); sp_point_521* p = NULL; int i; int j; @@ -74600,19 +73381,8 @@ static int sp_521_ecc_mulmod_stripe_9(sp_point_521* r, const sp_point_521* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -74666,10 +73436,8 @@ static int sp_521_ecc_mulmod_stripe_9(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -74787,20 +73555,11 @@ static int sp_521_ecc_mulmod_9(sp_point_521* r, const sp_point_521* g, #ifndef FP_ECC return sp_521_ecc_mulmod_win_add_sub_9(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 9 * 6]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 6); sp_cache_521_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -74833,9 +73592,7 @@ static int sp_521_ecc_mulmod_9(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -74854,28 +73611,12 @@ static int sp_521_ecc_mulmod_9(sp_point_521* r, const sp_point_521* g, int sp_ecc_mulmod_521(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[1]; - sp_digit k[9]; -#endif + SP_DECL_VAR(sp_point_521, point, 1); + SP_DECL_VAR(sp_digit, k, 9); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 9, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(k, 9, km); sp_521_point_from_ecc_point_9(point, gm); @@ -74886,10 +73627,8 @@ int sp_ecc_mulmod_521(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_521_point_to_ecc_point_9(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -74909,31 +73648,14 @@ int sp_ecc_mulmod_521(const mp_int* km, const ecc_point* gm, ecc_point* r, int sp_ecc_mulmod_add_521(const mp_int* km, const ecc_point* gm, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[2]; - sp_digit k[9 + 9 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_521, point, 2); + SP_DECL_VAR(sp_digit, k, 9 + 9 * 2 * 6); sp_point_521* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (9 + 9 * 2 * 6), heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 9 + 9 * 2 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 9; @@ -74964,10 +73686,8 @@ int sp_ecc_mulmod_add_521(const mp_int* km, const ecc_point* gm, err = sp_521_point_to_ecc_point_9(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -111259,13 +109979,8 @@ static int sp_521_ecc_mulmod_add_only_9(sp_point_521* r, const sp_point_521* g, const sp_table_entry_521* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* rt = NULL; - sp_digit* tmp = NULL; -#else - sp_point_521 rt[2]; - sp_digit tmp[2 * 9 * 6]; -#endif + SP_DECL_VAR(sp_point_521, rt, 2); + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 6); sp_point_521* p = NULL; sp_digit* negy = NULL; int i; @@ -111277,19 +109992,8 @@ static int sp_521_ecc_mulmod_add_only_9(sp_point_521* r, const sp_point_521* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { negy = tmp; p = rt + 1; @@ -111338,18 +110042,8 @@ static int sp_521_ecc_mulmod_add_only_9(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 9 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); return err; } @@ -111383,28 +110077,12 @@ static int sp_521_ecc_mulmod_base_9(sp_point_521* r, const sp_digit* k, */ int sp_ecc_mulmod_base_521(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[1]; - sp_digit k[9]; -#endif + SP_DECL_VAR(sp_point_521, point, 1); + SP_DECL_VAR(sp_digit, k, 9); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 9, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(k, 9, km); @@ -111414,10 +110092,8 @@ int sp_ecc_mulmod_base_521(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_521_point_to_ecc_point_9(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -111436,31 +110112,14 @@ int sp_ecc_mulmod_base_521(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_521(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[2]; - sp_digit k[9 + 9 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_521, point, 2); + SP_DECL_VAR(sp_digit, k, 9 + 9 * 2 * 6); sp_point_521* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (9 + 9 * 2 * 6), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 9 + 9 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 9; @@ -111490,10 +110149,8 @@ int sp_ecc_mulmod_base_add_521(const mp_int* km, const ecc_point* am, err = sp_521_point_to_ecc_point_9(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -111695,17 +110352,12 @@ static int sp_521_ecc_gen_k_9(WC_RNG* rng, sp_digit* k) */ int sp_ecc_make_key_521(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_DECL_VAR(sp_point_521, point, 2); #else - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - sp_point_521 point[2]; - #else - sp_point_521 point[1]; - #endif - sp_digit k[9]; + SP_DECL_VAR(sp_point_521, point, 1); #endif + SP_DECL_VAR(sp_digit, k, 9); #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN sp_point_521* infinity = NULL; #endif @@ -111714,22 +110366,12 @@ int sp_ecc_make_key_521(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, DYNAMIC_TYPE_ECC); - #else - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, DYNAMIC_TYPE_ECC); - #endif - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_ALLOC_VAR(sp_point_521, point, 2, heap, DYNAMIC_TYPE_ECC); +#else + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); #endif - + SP_ALLOC_VAR(sp_digit, k, 9, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN infinity = point + 1; @@ -111759,11 +110401,9 @@ int sp_ecc_make_key_521(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) err = sp_521_point_to_ecc_point_9(point, pub); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); /* point is not sensitive, so no need to zeroize */ - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -111889,34 +110529,16 @@ static void sp_521_to_bin_9(sp_digit* r, byte* a) int sp_ecc_secret_gen_521(const mp_int* priv, const ecc_point* pub, byte* out, word32* outLen, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[1]; - sp_digit k[9]; -#endif + SP_DECL_VAR(sp_point_521, point, 1); + SP_DECL_VAR(sp_digit, k, 9); int err = MP_OKAY; if (*outLen < 65U) { err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 9, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(k, 9, priv); sp_521_point_from_ecc_point_9(point, pub); @@ -111927,10 +110549,8 @@ int sp_ecc_secret_gen_521(const mp_int* priv, const ecc_point* pub, byte* out, *outLen = 66; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -112251,13 +110871,8 @@ static int sp_521_calc_s_9(sp_digit* s, const sp_digit* r, sp_digit* k, int sp_ecc_sign_521(const byte* hash, word32 hashLen, WC_RNG* rng, const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* e = NULL; - sp_point_521* point = NULL; -#else - sp_digit e[7 * 2 * 9]; - sp_point_521 point[1]; -#endif + SP_DECL_VAR(sp_digit, e, 7 * 2 * 9); + SP_DECL_VAR(sp_point_521, point, 1); sp_digit* x = NULL; sp_digit* k = NULL; sp_digit* r = NULL; @@ -112269,21 +110884,8 @@ int sp_ecc_sign_521(const byte* hash, word32 hashLen, WC_RNG* rng, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - e = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7 * 2 * 9, heap, - DYNAMIC_TYPE_ECC); - if (e == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, e, 7 * 2 * 9, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { x = e + 2 * 9; k = e + 4 * 9; @@ -112353,24 +110955,8 @@ int sp_ecc_sign_521(const byte* hash, word32 hashLen, WC_RNG* rng, err = sp_521_to_mp(s, sm); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (e != NULL) -#endif - { - ForceZero(e, sizeof(sp_digit) * 7 * 2 * 9); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(e, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (point != NULL) -#endif - { - ForceZero(point, sizeof(sp_point_521)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(point, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_digit, e, 7 * 2 * 9, heap, DYNAMIC_TYPE_ECC); return err; } @@ -112837,13 +111423,8 @@ int sp_ecc_verify_521(const byte* hash, word32 hashLen, const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* rm, const mp_int* sm, int* res, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u1 = NULL; - sp_point_521* p1 = NULL; -#else - sp_digit u1[18 * 9]; - sp_point_521 p1[2]; -#endif + SP_DECL_VAR(sp_digit, u1, 18 * 9); + SP_DECL_VAR(sp_point_521, p1, 2); sp_digit* u2 = NULL; sp_digit* s = NULL; sp_digit* tmp = NULL; @@ -112852,21 +111433,8 @@ int sp_ecc_verify_521(const byte* hash, word32 hashLen, const mp_int* pX, sp_int64 c = 0; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p1 = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (p1 == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - u1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 9, heap, - DYNAMIC_TYPE_ECC); - if (u1 == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, u1, 18 * 9, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p1, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { u2 = u1 + 2 * 9; s = u1 + 4 * 9; @@ -112927,11 +111495,8 @@ int sp_ecc_verify_521(const byte* hash, word32 hashLen, const mp_int* pX, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u1, heap, DYNAMIC_TYPE_ECC); - XFREE(p1, heap, DYNAMIC_TYPE_ECC); -#endif - + SP_FREE_VAR(p1, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(u1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -113100,21 +111665,13 @@ int sp_ecc_verify_521_nb(sp_ecc_ctx_t* sp_ctx, const byte* hash, static int sp_521_ecc_is_point_9(const sp_point_521* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[9 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 9 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 9 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 9; @@ -113138,9 +111695,7 @@ static int sp_521_ecc_is_point_9(const sp_point_521* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -113154,21 +111709,11 @@ static int sp_521_ecc_is_point_9(const sp_point_521* point, */ int sp_ecc_is_point_521(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* pub = NULL; -#else - sp_point_521 pub[1]; -#endif + SP_DECL_VAR(sp_point_521, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_521*)XMALLOC(sizeof(sp_point_521), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_521, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(pub->x, 9, pX); sp_521_from_mp(pub->y, 9, pY); @@ -113177,9 +111722,7 @@ int sp_ecc_is_point_521(const mp_int* pX, const mp_int* pY) err = sp_521_ecc_is_point_9(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -113198,13 +111741,8 @@ int sp_ecc_is_point_521(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_521(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_521* pub = NULL; -#else - sp_digit priv[9]; - sp_point_521 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 9); + SP_DECL_VAR(sp_point_521, pub, 2); sp_point_521* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -113219,21 +111757,8 @@ int sp_ecc_check_key_521(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 9, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -113285,10 +111810,8 @@ int sp_ecc_check_key_521(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } @@ -113312,32 +111835,13 @@ int sp_ecc_proj_add_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_521* p = NULL; -#else - sp_digit tmp[2 * 9 * 6]; - sp_point_521 p[2]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 6); + SP_DECL_VAR(sp_point_521, p, 2); sp_point_521* q = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p, 2, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { q = p + 1; @@ -113365,10 +111869,8 @@ int sp_ecc_proj_add_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_521_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -113387,30 +111889,12 @@ int sp_ecc_proj_add_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, int sp_ecc_proj_dbl_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_521* p = NULL; -#else - sp_digit tmp[2 * 9 * 2]; - sp_point_521 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 2); + SP_DECL_VAR(sp_point_521, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 2, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(p->x, 9, pX); sp_521_from_mp(p->y, 9, pY); @@ -113431,10 +111915,8 @@ int sp_ecc_proj_dbl_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_521_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -113449,30 +111931,13 @@ int sp_ecc_proj_dbl_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, */ int sp_ecc_map_521(mp_int* pX, mp_int* pY, mp_int* pZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_521* p = NULL; -#else - sp_digit tmp[2 * 9 * 5]; - sp_point_521 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 5); + SP_DECL_VAR(sp_point_521, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 5, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 5, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(p->x, 9, pX); sp_521_from_mp(p->y, 9, pY); @@ -113493,10 +111958,8 @@ int sp_ecc_map_521(mp_int* pX, mp_int* pY, mp_int* pZ) err = sp_521_to_mp(p->z, pZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -113516,19 +111979,10 @@ static const word64 p521_sqrt_power[9] = { */ static int sp_521_mont_sqrt_9(sp_digit* y) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t = NULL; -#else - sp_digit t[2 * 9]; -#endif + SP_DECL_VAR(sp_digit, t, 2 * 9); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9, NULL, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, t, 2 * 9, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { { @@ -113544,9 +111998,7 @@ static int sp_521_mont_sqrt_9(sp_digit* y) } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -113561,20 +112013,11 @@ static int sp_521_mont_sqrt_9(sp_digit* y) */ int sp_ecc_uncompress_521(mp_int* xm, int odd, mp_int* ym) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* x = NULL; -#else - sp_digit x[4 * 9]; -#endif + SP_DECL_VAR(sp_digit, x, 4 * 9); sp_digit* y = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - x = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 9, NULL, DYNAMIC_TYPE_ECC); - if (x == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, x, 4 * 9, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { y = x + 2 * 9; @@ -113609,9 +112052,7 @@ int sp_ecc_uncompress_521(mp_int* xm, int odd, mp_int* ym) err = sp_521_to_mp(y, ym); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(x, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(x, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -117702,13 +116143,8 @@ static void sp_1024_ecc_recode_7_16(const sp_digit* k, ecc_recode_1024* v) static int sp_1024_ecc_mulmod_win_add_sub_16(sp_point_1024* r, const sp_point_1024* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_1024 t[65+2]; - sp_digit tmp[2 * 16 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, t, 65+2); + SP_DECL_VAR(sp_digit, tmp, 2 * 16 * 37); sp_point_1024* rt = NULL; sp_point_1024* p = NULL; sp_digit* negy; @@ -117720,19 +116156,8 @@ static int sp_1024_ecc_mulmod_win_add_sub_16(sp_point_1024* r, const sp_point_10 (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * - (65+2), heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 16 * 37, - heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, t, 65+2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 16 * 37, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 65; p = t + 65+1; @@ -117820,10 +116245,8 @@ static int sp_1024_ecc_mulmod_win_add_sub_16(sp_point_1024* r, const sp_point_10 } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; } @@ -117954,11 +116377,7 @@ static void sp_1024_proj_to_affine_16(sp_point_1024* a, sp_digit* t) static int sp_1024_gen_stripe_table_16(const sp_point_1024* a, sp_table_entry_1024* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* t = NULL; -#else - sp_point_1024 t[3]; -#endif + SP_DECL_VAR(sp_point_1024, t, 3); sp_point_1024* s1 = NULL; sp_point_1024* s2 = NULL; int i; @@ -117967,13 +116386,7 @@ static int sp_1024_gen_stripe_table_16(const sp_point_1024* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_1024, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -118022,9 +116435,7 @@ static int sp_1024_gen_stripe_table_16(const sp_point_1024* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -118050,13 +116461,8 @@ static int sp_1024_ecc_mulmod_stripe_16(sp_point_1024* r, const sp_point_1024* g const sp_table_entry_1024* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_1024 rt[2]; - sp_digit t[2 * 16 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 16 * 37); sp_point_1024* p = NULL; int i; int j; @@ -118070,19 +116476,8 @@ static int sp_1024_ecc_mulmod_stripe_16(sp_point_1024* r, const sp_point_1024* g (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 16 * 37, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 16 * 37, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -118121,10 +116516,8 @@ static int sp_1024_ecc_mulmod_stripe_16(sp_point_1024* r, const sp_point_1024* g } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -118241,20 +116634,11 @@ static int sp_1024_ecc_mulmod_16(sp_point_1024* r, const sp_point_1024* g, #ifndef FP_ECC return sp_1024_ecc_mulmod_win_add_sub_16(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 16 * 38]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 16 * 38); sp_cache_1024_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 16 * 38, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 16 * 38, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -118287,9 +116671,7 @@ static int sp_1024_ecc_mulmod_16(sp_point_1024* r, const sp_point_1024* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -118307,28 +116689,12 @@ static int sp_1024_ecc_mulmod_16(sp_point_1024* r, const sp_point_1024* g, int sp_ecc_mulmod_1024(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[1]; - sp_digit k[16]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, k, 16); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 16, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(k, 16, km); sp_1024_point_from_ecc_point_16(point, gm); @@ -118339,10 +116705,8 @@ int sp_ecc_mulmod_1024(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_1024_point_to_ecc_point_16(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -121707,28 +120071,12 @@ static int sp_1024_ecc_mulmod_base_16(sp_point_1024* r, const sp_digit* k, */ int sp_ecc_mulmod_base_1024(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[1]; - sp_digit k[16]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, k, 16); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 16, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(k, 16, km); @@ -121738,10 +120086,8 @@ int sp_ecc_mulmod_base_1024(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_1024_point_to_ecc_point_16(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -121760,31 +120106,14 @@ int sp_ecc_mulmod_base_1024(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[2]; - sp_digit k[16 + 16 * 2 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, point, 2); + SP_DECL_VAR(sp_digit, k, 16 + 16 * 2 * 37); sp_point_1024* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (16 + 16 * 2 * 37), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 16 + 16 * 2 * 37, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 16; @@ -121814,10 +120143,8 @@ int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am, err = sp_1024_point_to_ecc_point_16(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -121835,13 +120162,8 @@ int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am, int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* t = NULL; -#else - sp_point_1024 point[1]; - sp_digit t[38 * 2 * 16]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, t, 38 * 2 * 16); int err = MP_OKAY; if ((gm == NULL) || (len == NULL)) { @@ -121856,21 +120178,8 @@ int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 38 * 2 * 16, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 38 * 2 * 16, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_point_from_ecc_point_16(point, gm); err = sp_1024_gen_stripe_table_16(point, @@ -121880,10 +120189,8 @@ int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, *len = sizeof(sp_table_entry_1024) * 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -121936,28 +120243,12 @@ int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, int sp_ecc_mulmod_table_1024(const mp_int* km, const ecc_point* gm, byte* table, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[1]; - sp_digit k[16]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, k, 16); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) { - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16, heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 16, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(k, 16, km); sp_1024_point_from_ecc_point_16(point, gm); @@ -121974,10 +120265,8 @@ int sp_ecc_mulmod_table_1024(const mp_int* km, const ecc_point* gm, byte* table, err = sp_1024_point_to_ecc_point_16(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); return err; } @@ -125248,22 +123537,14 @@ static void sp_1024_from_bin(sp_digit* r, int size, const byte* a, int n) static int sp_1024_ecc_is_point_16(const sp_point_1024* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[16 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 16 * 4); sp_digit* t2 = NULL; sp_int64 n; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 16 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 16; @@ -125290,9 +123571,7 @@ static int sp_1024_ecc_is_point_16(const sp_point_1024* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -125306,21 +123585,11 @@ static int sp_1024_ecc_is_point_16(const sp_point_1024* point, */ int sp_ecc_is_point_1024(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* pub = NULL; -#else - sp_point_1024 pub[1]; -#endif + SP_DECL_VAR(sp_point_1024, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_1024, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(pub->x, 16, pX); sp_1024_from_mp(pub->y, 16, pY); @@ -125329,9 +123598,7 @@ int sp_ecc_is_point_1024(const mp_int* pX, const mp_int* pY) err = sp_1024_ecc_is_point_16(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -125350,13 +123617,8 @@ int sp_ecc_is_point_1024(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_1024(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_1024* pub = NULL; -#else - sp_digit priv[16]; - sp_point_1024 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 16); + SP_DECL_VAR(sp_point_1024, pub, 2); sp_point_1024* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -125371,21 +123633,8 @@ int sp_ecc_check_key_1024(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 16, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_1024, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -125437,10 +123686,8 @@ int sp_ecc_check_key_1024(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } diff --git a/wolfcrypt/src/sp_armthumb.c b/wolfcrypt/src/sp_armthumb.c index 76c85f9b6..98516b901 100644 --- a/wolfcrypt/src/sp_armthumb.c +++ b/wolfcrypt/src/sp_armthumb.c @@ -57,6 +57,57 @@ #define __volatile__ volatile #endif +#ifdef WOLFSSL_SP_SMALL_STACK + #define SP_DECL_VAR(TYPE, NAME, CNT) \ + TYPE* NAME = NULL + #define SP_ALLOC_VAR(TYPE, NAME, CNT, HEAP, DT) \ + if (err == MP_OKAY) { \ + (NAME) = (TYPE*)XMALLOC(sizeof(TYPE) * (CNT), (HEAP), DT); \ + if ((NAME) == NULL) { \ + err = MEMORY_E; \ + } \ + } + + #define SP_VAR_OK(NAME) ((NAME) != NULL) + + #define SP_FREE_VAR(NAME, HEAP, DT) \ + XFREE(NAME, (HEAP), DT) + #define SP_ZEROFREE_VAR(TYPE, NAME, CNT, HEAP, DT) \ + do { \ + if ((NAME) != NULL) { \ + ForceZero(NAME, sizeof(TYPE) * (CNT)); \ + } \ + SP_FREE_VAR(NAME, HEAP, DT); \ + } while (0) + #define SP_ZEROFREE_VAR_ALT(TYPE, NAME, FZ_NAME, CNT, HEAP, DT) \ + do { \ + if ((FZ_NAME) != NULL) { \ + ForceZero(FZ_NAME, sizeof(TYPE) * (CNT)); \ + } \ + SP_FREE_VAR(NAME, HEAP, DT); \ + } while (0) +#else + #define SP_DECL_VAR(TYPE, NAME, CNT) \ + TYPE NAME[CNT] + #define SP_ALLOC_VAR(TYPE, NAME, CNT, HEAP, DT) \ + WC_DO_NOTHING + #define SP_VAR_OK(NAME) (1) + #define SP_FREE_VAR(NAME, HEAP, DT) \ + WC_DO_NOTHING + #define SP_ZEROFREE_VAR(TYPE, NAME, CNT, HEAP, DT) \ + do { \ + if ((NAME) != NULL) { \ + ForceZero(NAME, sizeof(TYPE) * (CNT)); \ + } \ + } while (0) + #define SP_ZEROFREE_VAR_ALT(TYPE, NAME, FZ_NAME, CNT, HEAP, DT) \ + do { \ + if ((FZ_NAME) != NULL) { \ + ForceZero(FZ_NAME, sizeof(TYPE) * (CNT)); \ + } \ + } while (0) +#endif + #ifdef WOLFSSL_SP_ARM_THUMB_ASM #define SP_PRINT_NUM(var, name, total, words, bits) \ do { \ @@ -24012,11 +24063,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[16 * 64]; -#endif + SP_DECL_VAR(sp_digit, td, 16 * 64); sp_digit* t[16]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -24031,15 +24078,7 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 64), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 16 * 64, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) { @@ -24142,9 +24181,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -24163,11 +24200,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[32 * 64]; -#endif + SP_DECL_VAR(sp_digit, td, 32 * 64); sp_digit* t[32]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -24182,15 +24215,7 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 64), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 32 * 64, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) { @@ -24310,9 +24335,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -27600,11 +27623,7 @@ static WC_INLINE int sp_2048_mod_64(sp_digit* r, const sp_digit* a, const sp_dig 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[8 * 128]; -#endif + SP_DECL_VAR(sp_digit, td, 8 * 128); sp_digit* t[8]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -27619,15 +27638,7 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 128), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 8 * 128, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<8; i++) { @@ -27721,9 +27732,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -27742,11 +27751,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[16 * 128]; -#endif + SP_DECL_VAR(sp_digit, td, 16 * 128); sp_digit* t[16]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -27761,15 +27766,7 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 128), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 16 * 128, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) { @@ -27872,9 +27869,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -27898,11 +27893,7 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[64 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 64 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit *ah = NULL; @@ -27920,15 +27911,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 64 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 64 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { ah = a + 64; r = a + 64 * 2; @@ -28035,9 +28018,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; } @@ -28134,11 +28115,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, const mp_int* qim, const mp_int* mm, byte* out, word32* outLen) { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[64 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 64 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -28168,15 +28145,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 64 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 64 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 64; m = a + 128; @@ -28193,25 +28162,11 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 64); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 64, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[32 * 11]; -#endif + SP_DECL_VAR(sp_digit, a, 32 * 11); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -28242,15 +28197,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 11, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 32 * 11, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 64 * 2; q = p + 32; @@ -28290,15 +28237,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 32 * 11); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 32 * 11, NULL, DYNAMIC_TYPE_RSA); #endif /* SP_RSA_PRIVATE_EXP_D || RSA_LOW_MEM */ return err; } @@ -30030,11 +29969,7 @@ static void sp_2048_lshift_64(sp_digit* r, const 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[193]; -#endif + SP_DECL_VAR(sp_digit, td, 193); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -30050,15 +29985,7 @@ static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 193, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 193, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 128; @@ -30135,9 +30062,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -75477,11 +75402,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[16 * 96]; -#endif + SP_DECL_VAR(sp_digit, td, 16 * 96); sp_digit* t[16]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -75496,15 +75417,7 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 96), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 16 * 96, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) { @@ -75607,9 +75520,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -75628,11 +75539,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[32 * 96]; -#endif + SP_DECL_VAR(sp_digit, td, 32 * 96); sp_digit* t[32]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -75647,15 +75554,7 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 96), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 32 * 96, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) { @@ -75775,9 +75674,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -79896,11 +79793,7 @@ static WC_INLINE int sp_3072_mod_96(sp_digit* r, const sp_digit* a, const sp_dig 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[8 * 192]; -#endif + SP_DECL_VAR(sp_digit, td, 8 * 192); sp_digit* t[8]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -79915,15 +79808,7 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 192), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 8 * 192, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<8; i++) { @@ -80017,9 +79902,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -80038,11 +79921,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[16 * 192]; -#endif + SP_DECL_VAR(sp_digit, td, 16 * 192); sp_digit* t[16]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -80057,15 +79936,7 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 192), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 16 * 192, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) { @@ -80168,9 +80039,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -80194,11 +80063,7 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[96 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 96 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit *ah = NULL; @@ -80216,15 +80081,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 96 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 96 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { ah = a + 96; r = a + 96 * 2; @@ -80331,9 +80188,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; } @@ -80430,11 +80285,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, const mp_int* qim, const mp_int* mm, byte* out, word32* outLen) { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[96 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 96 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -80464,15 +80315,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 96 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 96 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 96; m = a + 192; @@ -80489,25 +80332,11 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 96); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 96, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[48 * 11]; -#endif + SP_DECL_VAR(sp_digit, a, 48 * 11); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -80538,15 +80367,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 48 * 11, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 48 * 11, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 96 * 2; q = p + 48; @@ -80586,15 +80407,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 48 * 11); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 48 * 11, NULL, DYNAMIC_TYPE_RSA); #endif /* SP_RSA_PRIVATE_EXP_D || RSA_LOW_MEM */ return err; } @@ -83124,11 +82937,7 @@ static void sp_3072_lshift_96(sp_digit* r, const 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[289]; -#endif + SP_DECL_VAR(sp_digit, td, 289); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -83144,15 +82953,7 @@ static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 289, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 289, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 192; @@ -83229,9 +83030,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -92450,11 +92249,7 @@ static WC_INLINE int sp_4096_mod_128(sp_digit* r, const sp_digit* a, const sp_di 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[8 * 256]; -#endif + SP_DECL_VAR(sp_digit, td, 8 * 256); sp_digit* t[8]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -92469,15 +92264,7 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 256), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 8 * 256, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<8; i++) { @@ -92571,9 +92358,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -92592,11 +92377,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[16 * 256]; -#endif + SP_DECL_VAR(sp_digit, td, 16 * 256); sp_digit* t[16]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -92611,15 +92392,7 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 256), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 16 * 256, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) { @@ -92722,9 +92495,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -92748,11 +92519,7 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[128 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 128 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit *ah = NULL; @@ -92770,15 +92537,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 128 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 128 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { ah = a + 128; r = a + 128 * 2; @@ -92885,9 +92644,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; } @@ -92989,11 +92746,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, const mp_int* qim, const mp_int* mm, byte* out, word32* outLen) { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[128 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 128 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -93023,15 +92776,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 128 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 128 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 128; m = a + 256; @@ -93048,25 +92793,11 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 128); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 128, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[64 * 11]; -#endif + SP_DECL_VAR(sp_digit, a, 64 * 11); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -93097,15 +92828,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 64 * 11, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 64 * 11, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 128 * 2; q = p + 64; @@ -93145,15 +92868,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 64 * 11); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 64 * 11, NULL, DYNAMIC_TYPE_RSA); #endif /* SP_RSA_PRIVATE_EXP_D || RSA_LOW_MEM */ return err; } @@ -96471,11 +96186,7 @@ static void sp_4096_lshift_128(sp_digit* r, const 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[385]; -#endif + SP_DECL_VAR(sp_digit, td, 385); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -96491,15 +96202,7 @@ static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 385, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 385, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 256; @@ -96576,9 +96279,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -100928,20 +100629,11 @@ static void sp_256_get_point_16_8(sp_point_256* r, const sp_point_256* table, static int sp_256_ecc_mulmod_fast_8(sp_point_256* r, const sp_point_256* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_256 t[16 + 1]; - sp_digit tmp[2 * 8 * 6]; -#endif + SP_DECL_VAR(sp_point_256, t, 16 + 1); + SP_DECL_VAR(sp_digit, tmp, 2 * 8 * 6); sp_point_256* rt = NULL; #ifndef WC_NO_CACHE_RESISTANT -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* p = NULL; -#else - sp_point_256 p[1]; -#endif + SP_DECL_VAR(sp_point_256, p, 1); #endif /* !WC_NO_CACHE_RESISTANT */ sp_digit n; int i; @@ -100953,26 +100645,11 @@ static int sp_256_ecc_mulmod_fast_8(sp_point_256* r, const sp_point_256* g, cons (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * (16 + 1), - heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - #ifndef WC_NO_CACHE_RESISTANT - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256), - heap, DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - #endif - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 6, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } + SP_ALLOC_VAR(sp_point_256, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); +#ifndef WC_NO_CACHE_RESISTANT + SP_ALLOC_VAR(sp_point_256, p, 1, heap, DYNAMIC_TYPE_ECC); #endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 8 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 16; @@ -101064,35 +100741,12 @@ static int sp_256_ecc_mulmod_fast_8(sp_point_256* r, const sp_point_256* g, cons } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 8 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 8 * 6, heap, + DYNAMIC_TYPE_ECC); #ifndef WC_NO_CACHE_RESISTANT -#ifdef WOLFSSL_SP_SMALL_STACK - if (p != NULL) -#endif - { - ForceZero(p, sizeof(sp_point_256)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(p, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_256, p, 1, heap, DYNAMIC_TYPE_ECC); #endif /* !WC_NO_CACHE_RESISTANT */ -#ifdef WOLFSSL_SP_SMALL_STACK - if (t != NULL) -#endif - { - ForceZero(t, sizeof(sp_point_256) * 17); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_256, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -101317,11 +100971,7 @@ static void sp_256_proj_point_add_qz1_8(sp_point_256* r, static int sp_256_gen_stripe_table_8(const sp_point_256* a, sp_table_entry_256* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* t = NULL; -#else - sp_point_256 t[3]; -#endif + SP_DECL_VAR(sp_point_256, t, 3); sp_point_256* s1 = NULL; sp_point_256* s2 = NULL; int i; @@ -101330,13 +100980,7 @@ static int sp_256_gen_stripe_table_8(const sp_point_256* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_256, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -101385,9 +101029,7 @@ static int sp_256_gen_stripe_table_8(const sp_point_256* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -101463,13 +101105,8 @@ static int sp_256_ecc_mulmod_stripe_8(sp_point_256* r, const sp_point_256* g, const sp_table_entry_256* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_256 rt[2]; - sp_digit t[2 * 8 * 6]; -#endif + SP_DECL_VAR(sp_point_256, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 8 * 6); sp_point_256* p = NULL; int i; int j; @@ -101483,19 +101120,8 @@ static int sp_256_ecc_mulmod_stripe_8(sp_point_256* r, const sp_point_256* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 8 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -101549,10 +101175,8 @@ static int sp_256_ecc_mulmod_stripe_8(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -101669,20 +101293,11 @@ static int sp_256_ecc_mulmod_8(sp_point_256* r, const sp_point_256* g, #ifndef FP_ECC return sp_256_ecc_mulmod_fast_8(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 8 * 6]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 8 * 6); sp_cache_256_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 6, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 8 * 6, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -101715,9 +101330,7 @@ static int sp_256_ecc_mulmod_8(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -101738,11 +101351,7 @@ static int sp_256_ecc_mulmod_8(sp_point_256* r, const sp_point_256* g, static int sp_256_gen_stripe_table_8(const sp_point_256* a, sp_table_entry_256* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* t = NULL; -#else - sp_point_256 t[3]; -#endif + SP_DECL_VAR(sp_point_256, t, 3); sp_point_256* s1 = NULL; sp_point_256* s2 = NULL; int i; @@ -101751,13 +101360,7 @@ static int sp_256_gen_stripe_table_8(const sp_point_256* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_256, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -101806,9 +101409,7 @@ static int sp_256_gen_stripe_table_8(const sp_point_256* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -101884,13 +101485,8 @@ static int sp_256_ecc_mulmod_stripe_8(sp_point_256* r, const sp_point_256* g, const sp_table_entry_256* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_256 rt[2]; - sp_digit t[2 * 8 * 6]; -#endif + SP_DECL_VAR(sp_point_256, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 8 * 6); sp_point_256* p = NULL; int i; int j; @@ -101904,19 +101500,8 @@ static int sp_256_ecc_mulmod_stripe_8(sp_point_256* r, const sp_point_256* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 8 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -101970,10 +101555,8 @@ static int sp_256_ecc_mulmod_stripe_8(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -102090,20 +101673,11 @@ static int sp_256_ecc_mulmod_8(sp_point_256* r, const sp_point_256* g, #ifndef FP_ECC return sp_256_ecc_mulmod_fast_8(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 8 * 6]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 8 * 6); sp_cache_256_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 6, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 8 * 6, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -102136,9 +101710,7 @@ static int sp_256_ecc_mulmod_8(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -102157,28 +101729,12 @@ static int sp_256_ecc_mulmod_8(sp_point_256* r, const sp_point_256* g, int sp_ecc_mulmod_256(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[1]; - sp_digit k[8]; -#endif + SP_DECL_VAR(sp_point_256, point, 1); + SP_DECL_VAR(sp_digit, k, 8); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 8, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 8, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(k, 8, km); sp_256_point_from_ecc_point_8(point, gm); @@ -102189,10 +101745,8 @@ int sp_ecc_mulmod_256(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_256_point_to_ecc_point_8(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -102212,31 +101766,14 @@ int sp_ecc_mulmod_256(const mp_int* km, const ecc_point* gm, ecc_point* r, int sp_ecc_mulmod_add_256(const mp_int* km, const ecc_point* gm, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[2]; - sp_digit k[8 + 8 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_256, point, 2); + SP_DECL_VAR(sp_digit, k, 8 + 8 * 2 * 6); sp_point_256* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (8 + 8 * 2 * 6), heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 8 + 8 * 2 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 8; @@ -102267,10 +101804,8 @@ int sp_ecc_mulmod_add_256(const mp_int* km, const ecc_point* gm, err = sp_256_point_to_ecc_point_8(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -103704,28 +103239,12 @@ static int sp_256_ecc_mulmod_base_8(sp_point_256* r, const sp_digit* k, */ int sp_ecc_mulmod_base_256(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[1]; - sp_digit k[8]; -#endif + SP_DECL_VAR(sp_point_256, point, 1); + SP_DECL_VAR(sp_digit, k, 8); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 8, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 8, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(k, 8, km); @@ -103735,10 +103254,8 @@ int sp_ecc_mulmod_base_256(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_256_point_to_ecc_point_8(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -103757,31 +103274,14 @@ int sp_ecc_mulmod_base_256(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_256(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[2]; - sp_digit k[8 + 8 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_256, point, 2); + SP_DECL_VAR(sp_digit, k, 8 + 8 * 2 * 6); sp_point_256* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (8 + 8 * 2 * 6), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 8 + 8 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 8; @@ -103811,10 +103311,8 @@ int sp_ecc_mulmod_base_add_256(const mp_int* km, const ecc_point* am, err = sp_256_point_to_ecc_point_8(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -104000,17 +103498,12 @@ static int sp_256_ecc_gen_k_8(WC_RNG* rng, sp_digit* k) */ int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_DECL_VAR(sp_point_256, point, 2); #else - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - sp_point_256 point[2]; - #else - sp_point_256 point[1]; - #endif - sp_digit k[8]; + SP_DECL_VAR(sp_point_256, point, 1); #endif + SP_DECL_VAR(sp_digit, k, 8); #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN sp_point_256* infinity = NULL; #endif @@ -104019,22 +103512,12 @@ int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, DYNAMIC_TYPE_ECC); - #else - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, DYNAMIC_TYPE_ECC); - #endif - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 8, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_ALLOC_VAR(sp_point_256, point, 2, heap, DYNAMIC_TYPE_ECC); +#else + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); #endif - + SP_ALLOC_VAR(sp_digit, k, 8, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN infinity = point + 1; @@ -104064,11 +103547,9 @@ int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) err = sp_256_point_to_ecc_point_8(point, pub); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); /* point is not sensitive, so no need to zeroize */ - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -104186,34 +103667,16 @@ static void sp_256_to_bin_8(sp_digit* r, byte* a) int sp_ecc_secret_gen_256(const mp_int* priv, const ecc_point* pub, byte* out, word32* outLen, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[1]; - sp_digit k[8]; -#endif + SP_DECL_VAR(sp_point_256, point, 1); + SP_DECL_VAR(sp_digit, k, 8); int err = MP_OKAY; if (*outLen < 32U) { err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 8, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 8, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(k, 8, priv); sp_256_point_from_ecc_point_8(point, pub); @@ -104224,10 +103687,8 @@ int sp_ecc_secret_gen_256(const mp_int* priv, const ecc_point* pub, byte* out, *outLen = 32; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -105620,13 +105081,8 @@ static int sp_256_calc_s_8(sp_digit* s, const sp_digit* r, sp_digit* k, int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* e = NULL; - sp_point_256* point = NULL; -#else - sp_digit e[7 * 2 * 8]; - sp_point_256 point[1]; -#endif + SP_DECL_VAR(sp_digit, e, 7 * 2 * 8); + SP_DECL_VAR(sp_point_256, point, 1); sp_digit* x = NULL; sp_digit* k = NULL; sp_digit* r = NULL; @@ -105638,21 +105094,8 @@ int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - e = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7 * 2 * 8, heap, - DYNAMIC_TYPE_ECC); - if (e == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, e, 7 * 2 * 8, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { x = e + 2 * 8; k = e + 4 * 8; @@ -105717,24 +105160,8 @@ int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, err = sp_256_to_mp(s, sm); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (e != NULL) -#endif - { - ForceZero(e, sizeof(sp_digit) * 7 * 2 * 8); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(e, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (point != NULL) -#endif - { - ForceZero(point, sizeof(sp_point_256)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(point, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_digit, e, 7 * 2 * 8, heap, DYNAMIC_TYPE_ECC); return err; } @@ -107441,13 +106868,8 @@ int sp_ecc_verify_256(const byte* hash, word32 hashLen, const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* rm, const mp_int* sm, int* res, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u1 = NULL; - sp_point_256* p1 = NULL; -#else - sp_digit u1[18 * 8]; - sp_point_256 p1[2]; -#endif + SP_DECL_VAR(sp_digit, u1, 18 * 8); + SP_DECL_VAR(sp_point_256, p1, 2); sp_digit* u2 = NULL; sp_digit* s = NULL; sp_digit* tmp = NULL; @@ -107456,21 +106878,8 @@ int sp_ecc_verify_256(const byte* hash, word32 hashLen, const mp_int* pX, sp_int32 c = 0; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p1 = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (p1 == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - u1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 8, heap, - DYNAMIC_TYPE_ECC); - if (u1 == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, u1, 18 * 8, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p1, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { u2 = u1 + 2 * 8; s = u1 + 4 * 8; @@ -107527,11 +106936,8 @@ int sp_ecc_verify_256(const byte* hash, word32 hashLen, const mp_int* pX, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u1, heap, DYNAMIC_TYPE_ECC); - XFREE(p1, heap, DYNAMIC_TYPE_ECC); -#endif - + SP_FREE_VAR(p1, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(u1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -107697,21 +107103,13 @@ int sp_ecc_verify_256_nb(sp_ecc_ctx_t* sp_ctx, const byte* hash, static int sp_256_ecc_is_point_8(const sp_point_256* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[8 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 8 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 8 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 8 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 8; @@ -107735,9 +107133,7 @@ static int sp_256_ecc_is_point_8(const sp_point_256* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -107751,21 +107147,11 @@ static int sp_256_ecc_is_point_8(const sp_point_256* point, */ int sp_ecc_is_point_256(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* pub = NULL; -#else - sp_point_256 pub[1]; -#endif + SP_DECL_VAR(sp_point_256, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_256*)XMALLOC(sizeof(sp_point_256), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_256, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(pub->x, 8, pX); sp_256_from_mp(pub->y, 8, pY); @@ -107774,9 +107160,7 @@ int sp_ecc_is_point_256(const mp_int* pX, const mp_int* pY) err = sp_256_ecc_is_point_8(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -107795,13 +107179,8 @@ int sp_ecc_is_point_256(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_256(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_256* pub = NULL; -#else - sp_digit priv[8]; - sp_point_256 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 8); + SP_DECL_VAR(sp_point_256, pub, 2); sp_point_256* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -107816,21 +107195,8 @@ int sp_ecc_check_key_256(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 8, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 8, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -107882,10 +107248,8 @@ int sp_ecc_check_key_256(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } @@ -107909,32 +107273,13 @@ int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_256* p = NULL; -#else - sp_digit tmp[2 * 8 * 6]; - sp_point_256 p[2]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 8 * 6); + SP_DECL_VAR(sp_point_256, p, 2); sp_point_256* q = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 8 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p, 2, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { q = p + 1; @@ -107962,10 +107307,8 @@ int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_256_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -107984,30 +107327,12 @@ int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_256* p = NULL; -#else - sp_digit tmp[2 * 8 * 2]; - sp_point_256 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 8 * 2); + SP_DECL_VAR(sp_point_256, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 2, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 8 * 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(p->x, 8, pX); sp_256_from_mp(p->y, 8, pY); @@ -108028,10 +107353,8 @@ int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_256_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -108046,30 +107369,13 @@ int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, */ int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_256* p = NULL; -#else - sp_digit tmp[2 * 8 * 4]; - sp_point_256 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 8 * 4); + SP_DECL_VAR(sp_point_256, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 4, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 8 * 4, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(p->x, 8, pX); sp_256_from_mp(p->y, 8, pY); @@ -108090,10 +107396,8 @@ int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ) err = sp_256_to_mp(p->z, pZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -108106,21 +107410,11 @@ int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ) */ static int sp_256_mont_sqrt_8(sp_digit* y) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 8]; -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 8); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 8, NULL, DYNAMIC_TYPE_ECC); - if (t1 == NULL) { - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, t1, 4 * 8, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 8; @@ -108157,9 +107451,7 @@ static int sp_256_mont_sqrt_8(sp_digit* y) } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -108174,20 +107466,11 @@ static int sp_256_mont_sqrt_8(sp_digit* y) */ int sp_ecc_uncompress_256(mp_int* xm, int odd, mp_int* ym) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* x = NULL; -#else - sp_digit x[4 * 8]; -#endif + SP_DECL_VAR(sp_digit, x, 4 * 8); sp_digit* y = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - x = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 8, NULL, DYNAMIC_TYPE_ECC); - if (x == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, x, 4 * 8, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { y = x + 2 * 8; @@ -108222,9 +107505,7 @@ int sp_ecc_uncompress_256(mp_int* xm, int odd, mp_int* ym) err = sp_256_to_mp(y, ym); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(x, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(x, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -109196,23 +108477,13 @@ SP_NOINLINE static sp_digit sp_384_add_12(sp_digit* r, const sp_digit* a, */ static int sp_384_mod_mul_norm_12(sp_digit* r, const sp_digit* a, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - int64_t* t = NULL; -#else - int64_t t[12]; -#endif + SP_DECL_VAR(int64_t, t, 12); int64_t o; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (int64_t*)XMALLOC(sizeof(int64_t) * 12, NULL, DYNAMIC_TYPE_ECC); - if (t == NULL) { - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(int64_t, t, 12, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { /* 1 0 0 0 0 0 0 0 1 1 0 -1 */ t[0] = 0 + (int64_t)a[0] + (int64_t)a[8] + (int64_t)a[9] - (int64_t)a[11]; @@ -109281,9 +108552,7 @@ static int sp_384_mod_mul_norm_12(sp_digit* r, const sp_digit* a, const sp_digit r[11] = (sp_digit)t[11]; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -111774,20 +111043,11 @@ static void sp_384_get_point_16_12(sp_point_384* r, const sp_point_384* table, static int sp_384_ecc_mulmod_fast_12(sp_point_384* r, const sp_point_384* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_384 t[16 + 1]; - sp_digit tmp[2 * 12 * 6]; -#endif + SP_DECL_VAR(sp_point_384, t, 16 + 1); + SP_DECL_VAR(sp_digit, tmp, 2 * 12 * 6); sp_point_384* rt = NULL; #ifndef WC_NO_CACHE_RESISTANT -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* p = NULL; -#else - sp_point_384 p[1]; -#endif + SP_DECL_VAR(sp_point_384, p, 1); #endif /* !WC_NO_CACHE_RESISTANT */ sp_digit n; int i; @@ -111799,26 +111059,11 @@ static int sp_384_ecc_mulmod_fast_12(sp_point_384* r, const sp_point_384* g, con (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * (16 + 1), - heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - #ifndef WC_NO_CACHE_RESISTANT - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384), - heap, DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - #endif - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 6, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } + SP_ALLOC_VAR(sp_point_384, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); +#ifndef WC_NO_CACHE_RESISTANT + SP_ALLOC_VAR(sp_point_384, p, 1, heap, DYNAMIC_TYPE_ECC); #endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 12 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 16; @@ -111910,35 +111155,12 @@ static int sp_384_ecc_mulmod_fast_12(sp_point_384* r, const sp_point_384* g, con } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 12 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 12 * 6, heap, + DYNAMIC_TYPE_ECC); #ifndef WC_NO_CACHE_RESISTANT -#ifdef WOLFSSL_SP_SMALL_STACK - if (p != NULL) -#endif - { - ForceZero(p, sizeof(sp_point_384)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(p, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_384, p, 1, heap, DYNAMIC_TYPE_ECC); #endif /* !WC_NO_CACHE_RESISTANT */ -#ifdef WOLFSSL_SP_SMALL_STACK - if (t != NULL) -#endif - { - ForceZero(t, sizeof(sp_point_384) * 17); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_384, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -112163,11 +111385,7 @@ static void sp_384_proj_point_add_qz1_12(sp_point_384* r, static int sp_384_gen_stripe_table_12(const sp_point_384* a, sp_table_entry_384* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* t = NULL; -#else - sp_point_384 t[3]; -#endif + SP_DECL_VAR(sp_point_384, t, 3); sp_point_384* s1 = NULL; sp_point_384* s2 = NULL; int i; @@ -112176,13 +111394,7 @@ static int sp_384_gen_stripe_table_12(const sp_point_384* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_384, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -112231,9 +111443,7 @@ static int sp_384_gen_stripe_table_12(const sp_point_384* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -112325,13 +111535,8 @@ static int sp_384_ecc_mulmod_stripe_12(sp_point_384* r, const sp_point_384* g, const sp_table_entry_384* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_384 rt[2]; - sp_digit t[2 * 12 * 6]; -#endif + SP_DECL_VAR(sp_point_384, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 12 * 6); sp_point_384* p = NULL; int i; int j; @@ -112345,19 +111550,8 @@ static int sp_384_ecc_mulmod_stripe_12(sp_point_384* r, const sp_point_384* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 12 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -112411,10 +111605,8 @@ static int sp_384_ecc_mulmod_stripe_12(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -112531,20 +111723,11 @@ static int sp_384_ecc_mulmod_12(sp_point_384* r, const sp_point_384* g, #ifndef FP_ECC return sp_384_ecc_mulmod_fast_12(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 12 * 7]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 12 * 7); sp_cache_384_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 7, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 12 * 7, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -112577,9 +111760,7 @@ static int sp_384_ecc_mulmod_12(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -112600,11 +111781,7 @@ static int sp_384_ecc_mulmod_12(sp_point_384* r, const sp_point_384* g, static int sp_384_gen_stripe_table_12(const sp_point_384* a, sp_table_entry_384* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* t = NULL; -#else - sp_point_384 t[3]; -#endif + SP_DECL_VAR(sp_point_384, t, 3); sp_point_384* s1 = NULL; sp_point_384* s2 = NULL; int i; @@ -112613,13 +111790,7 @@ static int sp_384_gen_stripe_table_12(const sp_point_384* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_384, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -112668,9 +111839,7 @@ static int sp_384_gen_stripe_table_12(const sp_point_384* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -112762,13 +111931,8 @@ static int sp_384_ecc_mulmod_stripe_12(sp_point_384* r, const sp_point_384* g, const sp_table_entry_384* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_384 rt[2]; - sp_digit t[2 * 12 * 6]; -#endif + SP_DECL_VAR(sp_point_384, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 12 * 6); sp_point_384* p = NULL; int i; int j; @@ -112782,19 +111946,8 @@ static int sp_384_ecc_mulmod_stripe_12(sp_point_384* r, const sp_point_384* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 12 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -112848,10 +112001,8 @@ static int sp_384_ecc_mulmod_stripe_12(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -112968,20 +112119,11 @@ static int sp_384_ecc_mulmod_12(sp_point_384* r, const sp_point_384* g, #ifndef FP_ECC return sp_384_ecc_mulmod_fast_12(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 12 * 7]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 12 * 7); sp_cache_384_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 7, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 12 * 7, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -113014,9 +112156,7 @@ static int sp_384_ecc_mulmod_12(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -113035,28 +112175,12 @@ static int sp_384_ecc_mulmod_12(sp_point_384* r, const sp_point_384* g, int sp_ecc_mulmod_384(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[1]; - sp_digit k[12]; -#endif + SP_DECL_VAR(sp_point_384, point, 1); + SP_DECL_VAR(sp_digit, k, 12); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 12, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 12, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(k, 12, km); sp_384_point_from_ecc_point_12(point, gm); @@ -113067,10 +112191,8 @@ int sp_ecc_mulmod_384(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_384_point_to_ecc_point_12(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -113090,31 +112212,14 @@ int sp_ecc_mulmod_384(const mp_int* km, const ecc_point* gm, ecc_point* r, int sp_ecc_mulmod_add_384(const mp_int* km, const ecc_point* gm, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[2]; - sp_digit k[12 + 12 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_384, point, 2); + SP_DECL_VAR(sp_digit, k, 12 + 12 * 2 * 6); sp_point_384* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (12 + 12 * 2 * 6), heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 12 + 12 * 2 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 12; @@ -113145,10 +112250,8 @@ int sp_ecc_mulmod_add_384(const mp_int* km, const ecc_point* gm, err = sp_384_point_to_ecc_point_12(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -114582,28 +113685,12 @@ static int sp_384_ecc_mulmod_base_12(sp_point_384* r, const sp_digit* k, */ int sp_ecc_mulmod_base_384(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[1]; - sp_digit k[12]; -#endif + SP_DECL_VAR(sp_point_384, point, 1); + SP_DECL_VAR(sp_digit, k, 12); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 12, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 12, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(k, 12, km); @@ -114613,10 +113700,8 @@ int sp_ecc_mulmod_base_384(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_384_point_to_ecc_point_12(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -114635,31 +113720,14 @@ int sp_ecc_mulmod_base_384(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_384(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[2]; - sp_digit k[12 + 12 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_384, point, 2); + SP_DECL_VAR(sp_digit, k, 12 + 12 * 2 * 6); sp_point_384* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (12 + 12 * 2 * 6), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 12 + 12 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 12; @@ -114689,10 +113757,8 @@ int sp_ecc_mulmod_base_add_384(const mp_int* km, const ecc_point* am, err = sp_384_point_to_ecc_point_12(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -114914,17 +113980,12 @@ static int sp_384_ecc_gen_k_12(WC_RNG* rng, sp_digit* k) */ int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_DECL_VAR(sp_point_384, point, 2); #else - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - sp_point_384 point[2]; - #else - sp_point_384 point[1]; - #endif - sp_digit k[12]; + SP_DECL_VAR(sp_point_384, point, 1); #endif + SP_DECL_VAR(sp_digit, k, 12); #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN sp_point_384* infinity = NULL; #endif @@ -114933,22 +113994,12 @@ int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, DYNAMIC_TYPE_ECC); - #else - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, DYNAMIC_TYPE_ECC); - #endif - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 12, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_ALLOC_VAR(sp_point_384, point, 2, heap, DYNAMIC_TYPE_ECC); +#else + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); #endif - + SP_ALLOC_VAR(sp_digit, k, 12, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN infinity = point + 1; @@ -114978,11 +114029,9 @@ int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) err = sp_384_point_to_ecc_point_12(point, pub); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); /* point is not sensitive, so no need to zeroize */ - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -115100,34 +114149,16 @@ static void sp_384_to_bin_12(sp_digit* r, byte* a) int sp_ecc_secret_gen_384(const mp_int* priv, const ecc_point* pub, byte* out, word32* outLen, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[1]; - sp_digit k[12]; -#endif + SP_DECL_VAR(sp_point_384, point, 1); + SP_DECL_VAR(sp_digit, k, 12); int err = MP_OKAY; if (*outLen < 48U) { err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 12, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 12, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(k, 12, priv); sp_384_point_from_ecc_point_12(point, pub); @@ -115138,10 +114169,8 @@ int sp_ecc_secret_gen_384(const mp_int* priv, const ecc_point* pub, byte* out, *outLen = 48; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -116541,13 +115570,8 @@ static int sp_384_calc_s_12(sp_digit* s, const sp_digit* r, sp_digit* k, int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng, const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* e = NULL; - sp_point_384* point = NULL; -#else - sp_digit e[7 * 2 * 12]; - sp_point_384 point[1]; -#endif + SP_DECL_VAR(sp_digit, e, 7 * 2 * 12); + SP_DECL_VAR(sp_point_384, point, 1); sp_digit* x = NULL; sp_digit* k = NULL; sp_digit* r = NULL; @@ -116559,21 +115583,8 @@ int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - e = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7 * 2 * 12, heap, - DYNAMIC_TYPE_ECC); - if (e == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, e, 7 * 2 * 12, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { x = e + 2 * 12; k = e + 4 * 12; @@ -116638,24 +115649,8 @@ int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng, err = sp_384_to_mp(s, sm); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (e != NULL) -#endif - { - ForceZero(e, sizeof(sp_digit) * 7 * 2 * 12); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(e, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (point != NULL) -#endif - { - ForceZero(point, sizeof(sp_point_384)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(point, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_digit, e, 7 * 2 * 12, heap, DYNAMIC_TYPE_ECC); return err; } @@ -118631,13 +117626,8 @@ int sp_ecc_verify_384(const byte* hash, word32 hashLen, const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* rm, const mp_int* sm, int* res, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u1 = NULL; - sp_point_384* p1 = NULL; -#else - sp_digit u1[18 * 12]; - sp_point_384 p1[2]; -#endif + SP_DECL_VAR(sp_digit, u1, 18 * 12); + SP_DECL_VAR(sp_point_384, p1, 2); sp_digit* u2 = NULL; sp_digit* s = NULL; sp_digit* tmp = NULL; @@ -118646,21 +117636,8 @@ int sp_ecc_verify_384(const byte* hash, word32 hashLen, const mp_int* pX, sp_int32 c = 0; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p1 = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (p1 == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - u1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 12, heap, - DYNAMIC_TYPE_ECC); - if (u1 == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, u1, 18 * 12, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p1, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { u2 = u1 + 2 * 12; s = u1 + 4 * 12; @@ -118717,11 +117694,8 @@ int sp_ecc_verify_384(const byte* hash, word32 hashLen, const mp_int* pX, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u1, heap, DYNAMIC_TYPE_ECC); - XFREE(p1, heap, DYNAMIC_TYPE_ECC); -#endif - + SP_FREE_VAR(p1, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(u1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -118887,21 +117861,13 @@ int sp_ecc_verify_384_nb(sp_ecc_ctx_t* sp_ctx, const byte* hash, static int sp_384_ecc_is_point_12(const sp_point_384* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[12 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 12 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 12 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 12 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 12; @@ -118925,9 +117891,7 @@ static int sp_384_ecc_is_point_12(const sp_point_384* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -118941,21 +117905,11 @@ static int sp_384_ecc_is_point_12(const sp_point_384* point, */ int sp_ecc_is_point_384(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* pub = NULL; -#else - sp_point_384 pub[1]; -#endif + SP_DECL_VAR(sp_point_384, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_384*)XMALLOC(sizeof(sp_point_384), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_384, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(pub->x, 12, pX); sp_384_from_mp(pub->y, 12, pY); @@ -118964,9 +117918,7 @@ int sp_ecc_is_point_384(const mp_int* pX, const mp_int* pY) err = sp_384_ecc_is_point_12(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -118985,13 +117937,8 @@ int sp_ecc_is_point_384(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_384(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_384* pub = NULL; -#else - sp_digit priv[12]; - sp_point_384 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 12); + SP_DECL_VAR(sp_point_384, pub, 2); sp_point_384* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -119006,21 +117953,8 @@ int sp_ecc_check_key_384(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 12, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 12, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -119072,10 +118006,8 @@ int sp_ecc_check_key_384(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } @@ -119099,32 +118031,13 @@ int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_384* p = NULL; -#else - sp_digit tmp[2 * 12 * 6]; - sp_point_384 p[2]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 12 * 6); + SP_DECL_VAR(sp_point_384, p, 2); sp_point_384* q = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 12 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p, 2, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { q = p + 1; @@ -119152,10 +118065,8 @@ int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_384_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -119174,30 +118085,12 @@ int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_384* p = NULL; -#else - sp_digit tmp[2 * 12 * 2]; - sp_point_384 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 12 * 2); + SP_DECL_VAR(sp_point_384, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 2, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 12 * 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(p->x, 12, pX); sp_384_from_mp(p->y, 12, pY); @@ -119218,10 +118111,8 @@ int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_384_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -119236,30 +118127,13 @@ int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, */ int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_384* p = NULL; -#else - sp_digit tmp[2 * 12 * 6]; - sp_point_384 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 12 * 6); + SP_DECL_VAR(sp_point_384, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 12 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(p->x, 12, pX); sp_384_from_mp(p->y, 12, pY); @@ -119280,10 +118154,8 @@ int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ) err = sp_384_to_mp(p->z, pZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -119296,23 +118168,14 @@ int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ) */ static int sp_384_mont_sqrt_12(sp_digit* y) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[5 * 2 * 12]; -#endif + SP_DECL_VAR(sp_digit, t1, 5 * 2 * 12); sp_digit* t2 = NULL; sp_digit* t3 = NULL; sp_digit* t4 = NULL; sp_digit* t5 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 5 * 2 * 12, NULL, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, t1, 5 * 2 * 12, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 12; t3 = t1 + 4 * 12; @@ -119377,9 +118240,7 @@ static int sp_384_mont_sqrt_12(sp_digit* y) } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -119394,20 +118255,11 @@ static int sp_384_mont_sqrt_12(sp_digit* y) */ int sp_ecc_uncompress_384(mp_int* xm, int odd, mp_int* ym) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* x = NULL; -#else - sp_digit x[4 * 12]; -#endif + SP_DECL_VAR(sp_digit, x, 4 * 12); sp_digit* y = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - x = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 12, NULL, DYNAMIC_TYPE_ECC); - if (x == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, x, 4 * 12, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { y = x + 2 * 12; @@ -119442,9 +118294,7 @@ int sp_ecc_uncompress_384(mp_int* xm, int odd, mp_int* ym) err = sp_384_to_mp(y, ym); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(x, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(x, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -125388,20 +124238,11 @@ static void sp_521_get_point_16_17(sp_point_521* r, const sp_point_521* table, static int sp_521_ecc_mulmod_fast_17(sp_point_521* r, const sp_point_521* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_521 t[16 + 1]; - sp_digit tmp[2 * 17 * 6]; -#endif + SP_DECL_VAR(sp_point_521, t, 16 + 1); + SP_DECL_VAR(sp_digit, tmp, 2 * 17 * 6); sp_point_521* rt = NULL; #ifndef WC_NO_CACHE_RESISTANT -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* p = NULL; -#else - sp_point_521 p[1]; -#endif + SP_DECL_VAR(sp_point_521, p, 1); #endif /* !WC_NO_CACHE_RESISTANT */ sp_digit n; int i; @@ -125413,26 +124254,11 @@ static int sp_521_ecc_mulmod_fast_17(sp_point_521* r, const sp_point_521* g, con (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * (16 + 1), - heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - #ifndef WC_NO_CACHE_RESISTANT - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521), - heap, DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - #endif - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 6, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } + SP_ALLOC_VAR(sp_point_521, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); +#ifndef WC_NO_CACHE_RESISTANT + SP_ALLOC_VAR(sp_point_521, p, 1, heap, DYNAMIC_TYPE_ECC); #endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 17 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 16; @@ -125528,35 +124354,12 @@ static int sp_521_ecc_mulmod_fast_17(sp_point_521* r, const sp_point_521* g, con } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 17 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 17 * 6, heap, + DYNAMIC_TYPE_ECC); #ifndef WC_NO_CACHE_RESISTANT -#ifdef WOLFSSL_SP_SMALL_STACK - if (p != NULL) -#endif - { - ForceZero(p, sizeof(sp_point_521)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(p, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_521, p, 1, heap, DYNAMIC_TYPE_ECC); #endif /* !WC_NO_CACHE_RESISTANT */ -#ifdef WOLFSSL_SP_SMALL_STACK - if (t != NULL) -#endif - { - ForceZero(t, sizeof(sp_point_521) * 17); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_521, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -125781,11 +124584,7 @@ static void sp_521_proj_point_add_qz1_17(sp_point_521* r, static int sp_521_gen_stripe_table_17(const sp_point_521* a, sp_table_entry_521* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* t = NULL; -#else - sp_point_521 t[3]; -#endif + SP_DECL_VAR(sp_point_521, t, 3); sp_point_521* s1 = NULL; sp_point_521* s2 = NULL; int i; @@ -125794,13 +124593,7 @@ static int sp_521_gen_stripe_table_17(const sp_point_521* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_521, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -125849,9 +124642,7 @@ static int sp_521_gen_stripe_table_17(const sp_point_521* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -125963,13 +124754,8 @@ static int sp_521_ecc_mulmod_stripe_17(sp_point_521* r, const sp_point_521* g, const sp_table_entry_521* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_521 rt[2]; - sp_digit t[2 * 17 * 6]; -#endif + SP_DECL_VAR(sp_point_521, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 17 * 6); sp_point_521* p = NULL; int i; int j; @@ -125983,19 +124769,8 @@ static int sp_521_ecc_mulmod_stripe_17(sp_point_521* r, const sp_point_521* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 17 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -126049,10 +124824,8 @@ static int sp_521_ecc_mulmod_stripe_17(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -126169,20 +124942,11 @@ static int sp_521_ecc_mulmod_17(sp_point_521* r, const sp_point_521* g, #ifndef FP_ECC return sp_521_ecc_mulmod_fast_17(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 17 * 6]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 17 * 6); sp_cache_521_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 6, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 17 * 6, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -126215,9 +124979,7 @@ static int sp_521_ecc_mulmod_17(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -126238,11 +125000,7 @@ static int sp_521_ecc_mulmod_17(sp_point_521* r, const sp_point_521* g, static int sp_521_gen_stripe_table_17(const sp_point_521* a, sp_table_entry_521* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* t = NULL; -#else - sp_point_521 t[3]; -#endif + SP_DECL_VAR(sp_point_521, t, 3); sp_point_521* s1 = NULL; sp_point_521* s2 = NULL; int i; @@ -126251,13 +125009,7 @@ static int sp_521_gen_stripe_table_17(const sp_point_521* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_521, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -126306,9 +125058,7 @@ static int sp_521_gen_stripe_table_17(const sp_point_521* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -126420,13 +125170,8 @@ static int sp_521_ecc_mulmod_stripe_17(sp_point_521* r, const sp_point_521* g, const sp_table_entry_521* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_521 rt[2]; - sp_digit t[2 * 17 * 6]; -#endif + SP_DECL_VAR(sp_point_521, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 17 * 6); sp_point_521* p = NULL; int i; int j; @@ -126440,19 +125185,8 @@ static int sp_521_ecc_mulmod_stripe_17(sp_point_521* r, const sp_point_521* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 17 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -126506,10 +125240,8 @@ static int sp_521_ecc_mulmod_stripe_17(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -126626,20 +125358,11 @@ static int sp_521_ecc_mulmod_17(sp_point_521* r, const sp_point_521* g, #ifndef FP_ECC return sp_521_ecc_mulmod_fast_17(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 17 * 6]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 17 * 6); sp_cache_521_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 6, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 17 * 6, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -126672,9 +125395,7 @@ static int sp_521_ecc_mulmod_17(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -126693,28 +125414,12 @@ static int sp_521_ecc_mulmod_17(sp_point_521* r, const sp_point_521* g, int sp_ecc_mulmod_521(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[1]; - sp_digit k[17]; -#endif + SP_DECL_VAR(sp_point_521, point, 1); + SP_DECL_VAR(sp_digit, k, 17); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 17, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 17, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(k, 17, km); sp_521_point_from_ecc_point_17(point, gm); @@ -126725,10 +125430,8 @@ int sp_ecc_mulmod_521(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_521_point_to_ecc_point_17(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -126748,31 +125451,14 @@ int sp_ecc_mulmod_521(const mp_int* km, const ecc_point* gm, ecc_point* r, int sp_ecc_mulmod_add_521(const mp_int* km, const ecc_point* gm, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[2]; - sp_digit k[17 + 17 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_521, point, 2); + SP_DECL_VAR(sp_digit, k, 17 + 17 * 2 * 6); sp_point_521* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (17 + 17 * 2 * 6), heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 17 + 17 * 2 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 17; @@ -126803,10 +125489,8 @@ int sp_ecc_mulmod_add_521(const mp_int* km, const ecc_point* gm, err = sp_521_point_to_ecc_point_17(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -128784,28 +127468,12 @@ static int sp_521_ecc_mulmod_base_17(sp_point_521* r, const sp_digit* k, */ int sp_ecc_mulmod_base_521(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[1]; - sp_digit k[17]; -#endif + SP_DECL_VAR(sp_point_521, point, 1); + SP_DECL_VAR(sp_digit, k, 17); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 17, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 17, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(k, 17, km); @@ -128815,10 +127483,8 @@ int sp_ecc_mulmod_base_521(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_521_point_to_ecc_point_17(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -128837,31 +127503,14 @@ int sp_ecc_mulmod_base_521(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_521(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[2]; - sp_digit k[17 + 17 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_521, point, 2); + SP_DECL_VAR(sp_digit, k, 17 + 17 * 2 * 6); sp_point_521* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (17 + 17 * 2 * 6), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 17 + 17 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 17; @@ -128891,10 +127540,8 @@ int sp_ecc_mulmod_base_add_521(const mp_int* km, const ecc_point* am, err = sp_521_point_to_ecc_point_17(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -129162,17 +127809,12 @@ static int sp_521_ecc_gen_k_17(WC_RNG* rng, sp_digit* k) */ int sp_ecc_make_key_521(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_DECL_VAR(sp_point_521, point, 2); #else - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - sp_point_521 point[2]; - #else - sp_point_521 point[1]; - #endif - sp_digit k[17]; + SP_DECL_VAR(sp_point_521, point, 1); #endif + SP_DECL_VAR(sp_digit, k, 17); #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN sp_point_521* infinity = NULL; #endif @@ -129181,22 +127823,12 @@ int sp_ecc_make_key_521(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, DYNAMIC_TYPE_ECC); - #else - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, DYNAMIC_TYPE_ECC); - #endif - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 17, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_ALLOC_VAR(sp_point_521, point, 2, heap, DYNAMIC_TYPE_ECC); +#else + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); #endif - + SP_ALLOC_VAR(sp_digit, k, 17, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN infinity = point + 1; @@ -129226,11 +127858,9 @@ int sp_ecc_make_key_521(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) err = sp_521_point_to_ecc_point_17(point, pub); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); /* point is not sensitive, so no need to zeroize */ - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -129350,34 +127980,16 @@ static void sp_521_to_bin_17(sp_digit* r, byte* a) int sp_ecc_secret_gen_521(const mp_int* priv, const ecc_point* pub, byte* out, word32* outLen, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[1]; - sp_digit k[17]; -#endif + SP_DECL_VAR(sp_point_521, point, 1); + SP_DECL_VAR(sp_digit, k, 17); int err = MP_OKAY; if (*outLen < 65U) { err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 17, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 17, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(k, 17, priv); sp_521_point_from_ecc_point_17(point, pub); @@ -129388,10 +128000,8 @@ int sp_ecc_secret_gen_521(const mp_int* priv, const ecc_point* pub, byte* out, *outLen = 66; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -132495,13 +131105,8 @@ static int sp_521_calc_s_17(sp_digit* s, const sp_digit* r, sp_digit* k, int sp_ecc_sign_521(const byte* hash, word32 hashLen, WC_RNG* rng, const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* e = NULL; - sp_point_521* point = NULL; -#else - sp_digit e[7 * 2 * 17]; - sp_point_521 point[1]; -#endif + SP_DECL_VAR(sp_digit, e, 7 * 2 * 17); + SP_DECL_VAR(sp_point_521, point, 1); sp_digit* x = NULL; sp_digit* k = NULL; sp_digit* r = NULL; @@ -132513,21 +131118,8 @@ int sp_ecc_sign_521(const byte* hash, word32 hashLen, WC_RNG* rng, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - e = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7 * 2 * 17, heap, - DYNAMIC_TYPE_ECC); - if (e == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, e, 7 * 2 * 17, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { x = e + 2 * 17; k = e + 4 * 17; @@ -132597,24 +131189,8 @@ int sp_ecc_sign_521(const byte* hash, word32 hashLen, WC_RNG* rng, err = sp_521_to_mp(s, sm); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (e != NULL) -#endif - { - ForceZero(e, sizeof(sp_digit) * 7 * 2 * 17); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(e, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (point != NULL) -#endif - { - ForceZero(point, sizeof(sp_point_521)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(point, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_digit, e, 7 * 2 * 17, heap, DYNAMIC_TYPE_ECC); return err; } @@ -135561,13 +134137,8 @@ int sp_ecc_verify_521(const byte* hash, word32 hashLen, const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* rm, const mp_int* sm, int* res, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u1 = NULL; - sp_point_521* p1 = NULL; -#else - sp_digit u1[18 * 17]; - sp_point_521 p1[2]; -#endif + SP_DECL_VAR(sp_digit, u1, 18 * 17); + SP_DECL_VAR(sp_point_521, p1, 2); sp_digit* u2 = NULL; sp_digit* s = NULL; sp_digit* tmp = NULL; @@ -135576,21 +134147,8 @@ int sp_ecc_verify_521(const byte* hash, word32 hashLen, const mp_int* pX, sp_int32 c = 0; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p1 = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (p1 == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - u1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 17, heap, - DYNAMIC_TYPE_ECC); - if (u1 == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, u1, 18 * 17, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p1, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { u2 = u1 + 2 * 17; s = u1 + 4 * 17; @@ -135651,11 +134209,8 @@ int sp_ecc_verify_521(const byte* hash, word32 hashLen, const mp_int* pX, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u1, heap, DYNAMIC_TYPE_ECC); - XFREE(p1, heap, DYNAMIC_TYPE_ECC); -#endif - + SP_FREE_VAR(p1, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(u1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -135824,21 +134379,13 @@ int sp_ecc_verify_521_nb(sp_ecc_ctx_t* sp_ctx, const byte* hash, static int sp_521_ecc_is_point_17(const sp_point_521* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[17 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 17 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 17 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 17 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 17; @@ -135862,9 +134409,7 @@ static int sp_521_ecc_is_point_17(const sp_point_521* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -135878,21 +134423,11 @@ static int sp_521_ecc_is_point_17(const sp_point_521* point, */ int sp_ecc_is_point_521(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* pub = NULL; -#else - sp_point_521 pub[1]; -#endif + SP_DECL_VAR(sp_point_521, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_521*)XMALLOC(sizeof(sp_point_521), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_521, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(pub->x, 17, pX); sp_521_from_mp(pub->y, 17, pY); @@ -135901,9 +134436,7 @@ int sp_ecc_is_point_521(const mp_int* pX, const mp_int* pY) err = sp_521_ecc_is_point_17(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -135922,13 +134455,8 @@ int sp_ecc_is_point_521(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_521(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_521* pub = NULL; -#else - sp_digit priv[17]; - sp_point_521 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 17); + SP_DECL_VAR(sp_point_521, pub, 2); sp_point_521* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -135943,21 +134471,8 @@ int sp_ecc_check_key_521(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 17, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 17, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -136009,10 +134524,8 @@ int sp_ecc_check_key_521(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } @@ -136036,32 +134549,13 @@ int sp_ecc_proj_add_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_521* p = NULL; -#else - sp_digit tmp[2 * 17 * 6]; - sp_point_521 p[2]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 17 * 6); + SP_DECL_VAR(sp_point_521, p, 2); sp_point_521* q = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 17 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p, 2, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { q = p + 1; @@ -136089,10 +134583,8 @@ int sp_ecc_proj_add_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_521_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -136111,30 +134603,12 @@ int sp_ecc_proj_add_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, int sp_ecc_proj_dbl_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_521* p = NULL; -#else - sp_digit tmp[2 * 17 * 2]; - sp_point_521 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 17 * 2); + SP_DECL_VAR(sp_point_521, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 2, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 17 * 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(p->x, 17, pX); sp_521_from_mp(p->y, 17, pY); @@ -136155,10 +134629,8 @@ int sp_ecc_proj_dbl_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_521_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -136173,30 +134645,13 @@ int sp_ecc_proj_dbl_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, */ int sp_ecc_map_521(mp_int* pX, mp_int* pY, mp_int* pZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_521* p = NULL; -#else - sp_digit tmp[2 * 17 * 5]; - sp_point_521 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 17 * 5); + SP_DECL_VAR(sp_point_521, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 5, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 17 * 5, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(p->x, 17, pX); sp_521_from_mp(p->y, 17, pY); @@ -136217,10 +134672,8 @@ int sp_ecc_map_521(mp_int* pX, mp_int* pY, mp_int* pZ) err = sp_521_to_mp(p->z, pZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -136240,19 +134693,10 @@ static const word32 p521_sqrt_power[17] = { */ static int sp_521_mont_sqrt_17(sp_digit* y) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t = NULL; -#else - sp_digit t[2 * 17]; -#endif + SP_DECL_VAR(sp_digit, t, 2 * 17); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17, NULL, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, t, 2 * 17, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { { @@ -136268,9 +134712,7 @@ static int sp_521_mont_sqrt_17(sp_digit* y) } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -136285,20 +134727,11 @@ static int sp_521_mont_sqrt_17(sp_digit* y) */ int sp_ecc_uncompress_521(mp_int* xm, int odd, mp_int* ym) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* x = NULL; -#else - sp_digit x[4 * 17]; -#endif + SP_DECL_VAR(sp_digit, x, 4 * 17); sp_digit* y = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - x = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 17, NULL, DYNAMIC_TYPE_ECC); - if (x == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, x, 4 * 17, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { y = x + 2 * 17; @@ -136333,9 +134766,7 @@ int sp_ecc_uncompress_521(mp_int* xm, int odd, mp_int* ym) err = sp_521_to_mp(y, ym); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(x, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(x, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -210149,13 +208580,8 @@ static int sp_1024_proj_point_add_32_nb(sp_ecc_ctx_t* sp_ctx, sp_point_1024* r, static int sp_1024_ecc_mulmod_fast_32(sp_point_1024* r, const sp_point_1024* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_1024 t[16 + 1]; - sp_digit tmp[2 * 32 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, t, 16 + 1); + SP_DECL_VAR(sp_digit, tmp, 2 * 32 * 37); sp_point_1024* rt = NULL; sp_digit n; int i; @@ -210167,18 +208593,8 @@ static int sp_1024_ecc_mulmod_fast_32(sp_point_1024* r, const sp_point_1024* g, (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * (16 + 1), - heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 32 * 37, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_point_1024, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 32 * 37, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 16; @@ -210250,24 +208666,9 @@ static int sp_1024_ecc_mulmod_fast_32(sp_point_1024* r, const sp_point_1024* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 32 * 37); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (t != NULL) -#endif - { - ForceZero(t, sizeof(sp_point_1024) * 17); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 32 * 37, heap, + DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_point_1024, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -210492,11 +208893,7 @@ static void sp_1024_proj_point_add_qz1_32(sp_point_1024* r, static int sp_1024_gen_stripe_table_32(const sp_point_1024* a, sp_table_entry_1024* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* t = NULL; -#else - sp_point_1024 t[3]; -#endif + SP_DECL_VAR(sp_point_1024, t, 3); sp_point_1024* s1 = NULL; sp_point_1024* s2 = NULL; int i; @@ -210505,13 +208902,7 @@ static int sp_1024_gen_stripe_table_32(const sp_point_1024* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_1024, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -210560,9 +208951,7 @@ static int sp_1024_gen_stripe_table_32(const sp_point_1024* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -210588,13 +208977,8 @@ static int sp_1024_ecc_mulmod_stripe_32(sp_point_1024* r, const sp_point_1024* g const sp_table_entry_1024* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_1024 rt[2]; - sp_digit t[2 * 32 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 32 * 37); sp_point_1024* p = NULL; int i; int j; @@ -210608,19 +208992,8 @@ static int sp_1024_ecc_mulmod_stripe_32(sp_point_1024* r, const sp_point_1024* g (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 32 * 37, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 32 * 37, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -210659,10 +209032,8 @@ static int sp_1024_ecc_mulmod_stripe_32(sp_point_1024* r, const sp_point_1024* g } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -210779,20 +209150,11 @@ static int sp_1024_ecc_mulmod_32(sp_point_1024* r, const sp_point_1024* g, #ifndef FP_ECC return sp_1024_ecc_mulmod_fast_32(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 32 * 38]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 32 * 38); sp_cache_1024_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 32 * 38, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 32 * 38, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -210825,9 +209187,7 @@ static int sp_1024_ecc_mulmod_32(sp_point_1024* r, const sp_point_1024* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -210848,11 +209208,7 @@ static int sp_1024_ecc_mulmod_32(sp_point_1024* r, const sp_point_1024* g, static int sp_1024_gen_stripe_table_32(const sp_point_1024* a, sp_table_entry_1024* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* t = NULL; -#else - sp_point_1024 t[3]; -#endif + SP_DECL_VAR(sp_point_1024, t, 3); sp_point_1024* s1 = NULL; sp_point_1024* s2 = NULL; int i; @@ -210861,13 +209217,7 @@ static int sp_1024_gen_stripe_table_32(const sp_point_1024* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_1024, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -210916,9 +209266,7 @@ static int sp_1024_gen_stripe_table_32(const sp_point_1024* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -210944,13 +209292,8 @@ static int sp_1024_ecc_mulmod_stripe_32(sp_point_1024* r, const sp_point_1024* g const sp_table_entry_1024* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_1024 rt[2]; - sp_digit t[2 * 32 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 32 * 37); sp_point_1024* p = NULL; int i; int j; @@ -210964,19 +209307,8 @@ static int sp_1024_ecc_mulmod_stripe_32(sp_point_1024* r, const sp_point_1024* g (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 32 * 37, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 32 * 37, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -211015,10 +209347,8 @@ static int sp_1024_ecc_mulmod_stripe_32(sp_point_1024* r, const sp_point_1024* g } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -211135,20 +209465,11 @@ static int sp_1024_ecc_mulmod_32(sp_point_1024* r, const sp_point_1024* g, #ifndef FP_ECC return sp_1024_ecc_mulmod_fast_32(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 32 * 38]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 32 * 38); sp_cache_1024_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 32 * 38, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 32 * 38, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -211181,9 +209502,7 @@ static int sp_1024_ecc_mulmod_32(sp_point_1024* r, const sp_point_1024* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -211202,28 +209521,12 @@ static int sp_1024_ecc_mulmod_32(sp_point_1024* r, const sp_point_1024* g, int sp_ecc_mulmod_1024(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[1]; - sp_digit k[32]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, k, 32); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 32, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(k, 32, km); sp_1024_point_from_ecc_point_32(point, gm); @@ -211234,10 +209537,8 @@ int sp_ecc_mulmod_1024(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_1024_point_to_ecc_point_32(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -214839,28 +213140,12 @@ static int sp_1024_ecc_mulmod_base_32(sp_point_1024* r, const sp_digit* k, */ int sp_ecc_mulmod_base_1024(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[1]; - sp_digit k[32]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, k, 32); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 32, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(k, 32, km); @@ -214870,10 +213155,8 @@ int sp_ecc_mulmod_base_1024(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_1024_point_to_ecc_point_32(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -214892,31 +213175,14 @@ int sp_ecc_mulmod_base_1024(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[2]; - sp_digit k[32 + 32 * 2 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, point, 2); + SP_DECL_VAR(sp_digit, k, 32 + 32 * 2 * 37); sp_point_1024* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (32 + 32 * 2 * 37), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 32 + 32 * 2 * 37, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 32; @@ -214946,10 +213212,8 @@ int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am, err = sp_1024_point_to_ecc_point_32(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -214967,13 +213231,8 @@ int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am, int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* t = NULL; -#else - sp_point_1024 point[1]; - sp_digit t[38 * 2 * 32]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, t, 38 * 2 * 32); int err = MP_OKAY; if ((gm == NULL) || (len == NULL)) { @@ -214988,21 +213247,8 @@ int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 38 * 2 * 32, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 38 * 2 * 32, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_point_from_ecc_point_32(point, gm); err = sp_1024_gen_stripe_table_32(point, @@ -215012,10 +213258,8 @@ int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, *len = sizeof(sp_table_entry_1024) * 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -215068,28 +213312,12 @@ int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, int sp_ecc_mulmod_table_1024(const mp_int* km, const ecc_point* gm, byte* table, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[1]; - sp_digit k[32]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, k, 32); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) { - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32, heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 32, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(k, 32, km); sp_1024_point_from_ecc_point_32(point, gm); @@ -215106,10 +213334,8 @@ int sp_ecc_mulmod_table_1024(const mp_int* km, const ecc_point* gm, byte* table, err = sp_1024_point_to_ecc_point_32(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); return err; } @@ -218570,22 +216796,14 @@ static void sp_1024_from_bin(sp_digit* r, int size, const byte* a, int n) static int sp_1024_ecc_is_point_32(const sp_point_1024* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[32 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 32 * 4); sp_digit* t2 = NULL; sp_int32 n; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 32 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 32; @@ -218612,9 +216830,7 @@ static int sp_1024_ecc_is_point_32(const sp_point_1024* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -218628,21 +216844,11 @@ static int sp_1024_ecc_is_point_32(const sp_point_1024* point, */ int sp_ecc_is_point_1024(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* pub = NULL; -#else - sp_point_1024 pub[1]; -#endif + SP_DECL_VAR(sp_point_1024, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_1024, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(pub->x, 32, pX); sp_1024_from_mp(pub->y, 32, pY); @@ -218651,9 +216857,7 @@ int sp_ecc_is_point_1024(const mp_int* pX, const mp_int* pY) err = sp_1024_ecc_is_point_32(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -218672,13 +216876,8 @@ int sp_ecc_is_point_1024(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_1024(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_1024* pub = NULL; -#else - sp_digit priv[32]; - sp_point_1024 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 32); + SP_DECL_VAR(sp_point_1024, pub, 2); sp_point_1024* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -218693,21 +216892,8 @@ int sp_ecc_check_key_1024(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 32, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_1024, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -218759,10 +216945,8 @@ int sp_ecc_check_key_1024(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } diff --git a/wolfcrypt/src/sp_c32.c b/wolfcrypt/src/sp_c32.c index 6edd9f41b..0ae27b8c8 100644 --- a/wolfcrypt/src/sp_c32.c +++ b/wolfcrypt/src/sp_c32.c @@ -61,6 +61,57 @@ #define __volatile__ volatile #endif +#ifdef WOLFSSL_SP_SMALL_STACK + #define SP_DECL_VAR(TYPE, NAME, CNT) \ + TYPE* NAME = NULL + #define SP_ALLOC_VAR(TYPE, NAME, CNT, HEAP, DT) \ + if (err == MP_OKAY) { \ + (NAME) = (TYPE*)XMALLOC(sizeof(TYPE) * (CNT), (HEAP), DT); \ + if ((NAME) == NULL) { \ + err = MEMORY_E; \ + } \ + } + + #define SP_VAR_OK(NAME) ((NAME) != NULL) + + #define SP_FREE_VAR(NAME, HEAP, DT) \ + XFREE(NAME, (HEAP), DT) + #define SP_ZEROFREE_VAR(TYPE, NAME, CNT, HEAP, DT) \ + do { \ + if ((NAME) != NULL) { \ + ForceZero(NAME, sizeof(TYPE) * (CNT)); \ + } \ + SP_FREE_VAR(NAME, HEAP, DT); \ + } while (0) + #define SP_ZEROFREE_VAR_ALT(TYPE, NAME, FZ_NAME, CNT, HEAP, DT) \ + do { \ + if ((FZ_NAME) != NULL) { \ + ForceZero(FZ_NAME, sizeof(TYPE) * (CNT)); \ + } \ + SP_FREE_VAR(NAME, HEAP, DT); \ + } while (0) +#else + #define SP_DECL_VAR(TYPE, NAME, CNT) \ + TYPE NAME[CNT] + #define SP_ALLOC_VAR(TYPE, NAME, CNT, HEAP, DT) \ + WC_DO_NOTHING + #define SP_VAR_OK(NAME) (1) + #define SP_FREE_VAR(NAME, HEAP, DT) \ + WC_DO_NOTHING + #define SP_ZEROFREE_VAR(TYPE, NAME, CNT, HEAP, DT) \ + do { \ + if ((NAME) != NULL) { \ + ForceZero(NAME, sizeof(TYPE) * (CNT)); \ + } \ + } while (0) + #define SP_ZEROFREE_VAR_ALT(TYPE, NAME, FZ_NAME, CNT, HEAP, DT) \ + do { \ + if ((FZ_NAME) != NULL) { \ + ForceZero(FZ_NAME, sizeof(TYPE) * (CNT)); \ + } \ + } while (0) +#endif + #ifndef WOLFSSL_SP_ASM #if SP_WORD_SIZE == 32 #define SP_PRINT_NUM(var, name, total, words, bits) \ @@ -2033,30 +2084,16 @@ static int sp_2048_div_36(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_32 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 36 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 36 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 36 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 36 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 72 + 1; sd = t2 + 36 + 1; @@ -2099,9 +2136,7 @@ static int sp_2048_div_36(const sp_digit* a, const sp_digit* d, sp_2048_rshift_36(r, r, 20); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -2133,11 +2168,7 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 72]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 72); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -2151,15 +2182,7 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 36 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 72, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -2217,17 +2240,11 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #elif !defined(WC_NO_CACHE_RESISTANT) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 72]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 72); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -2241,15 +2258,7 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 36 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 72, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -2306,17 +2315,11 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, t[0], sizeof(*r) * 36 * 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(32 * 72) + 72]; -#endif + SP_DECL_VAR(sp_digit, td, (32 * 72) + 72); sp_digit* t[32]; sp_digit* rt = NULL; sp_digit* norm = NULL; @@ -2331,15 +2334,8 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 72) + 72), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (32 * 72) + 72, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) @@ -2451,9 +2447,7 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, rt, sizeof(sp_digit) * 72); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #endif @@ -3091,30 +3085,16 @@ static int sp_2048_div_72(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_32 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 72 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 72 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 72 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 72 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 144 + 1; sd = t2 + 72 + 1; @@ -3157,9 +3137,7 @@ static int sp_2048_div_72(const sp_digit* a, const sp_digit* d, r[71] = 0; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -3194,11 +3172,7 @@ static int sp_2048_mod_exp_72(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 144]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 144); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -3212,15 +3186,7 @@ static int sp_2048_mod_exp_72(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 72 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 144, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -3278,17 +3244,11 @@ static int sp_2048_mod_exp_72(sp_digit* r, const sp_digit* a, const sp_digit* e, } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #elif !defined(WC_NO_CACHE_RESISTANT) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 144]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 144); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -3302,15 +3262,7 @@ static int sp_2048_mod_exp_72(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 72 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 144, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -3367,17 +3319,11 @@ static int sp_2048_mod_exp_72(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, t[0], sizeof(*r) * 72 * 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(16 * 144) + 144]; -#endif + SP_DECL_VAR(sp_digit, td, (16 * 144) + 144); sp_digit* t[16]; sp_digit* rt = NULL; sp_digit* norm = NULL; @@ -3392,15 +3338,8 @@ static int sp_2048_mod_exp_72(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 144) + 144), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (16 * 144) + 144, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) @@ -3495,9 +3434,7 @@ static int sp_2048_mod_exp_72(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, rt, sizeof(sp_digit) * 144); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #endif @@ -3523,11 +3460,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { #ifdef WOLFSSL_SP_SMALL -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[72 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 72 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit* norm = NULL; @@ -3555,15 +3488,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 72 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 72 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { r = a + 72 * 2; m = r + 72 * 2; @@ -3616,17 +3541,11 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[72 * 5]; -#endif + SP_DECL_VAR(sp_digit, d, 72 * 5); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -3651,15 +3570,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 72 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 72 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d; r = a + 72 * 2; @@ -3727,9 +3638,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(d, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ @@ -3761,11 +3670,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) #if defined(WOLFSSL_SP_SMALL) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[72 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 72 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -3795,15 +3700,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 72 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 72 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 72; m = a + 144; @@ -3820,25 +3717,11 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 72); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 72, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[72 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 72 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -3868,15 +3751,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 72 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 72 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 72; m = a + 144; @@ -3893,27 +3768,13 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 72); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 72, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ #else #if defined(WOLFSSL_SP_SMALL) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[36 * 8]; -#endif + SP_DECL_VAR(sp_digit, a, 36 * 8); sp_digit* p = NULL; sp_digit* dp = NULL; sp_digit* dq = NULL; @@ -3947,14 +3808,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 36 * 8, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, a, 36 * 8, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 72; qi = dq = dp = p + 36; @@ -3995,23 +3849,11 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 36 * 8); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR(sp_digit, a, 36 * 8, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[36 * 13]; -#endif + SP_DECL_VAR(sp_digit, a, 36 * 13); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -4046,15 +3888,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 36 * 13, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 36 * 13, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 72 * 2; q = p + 36; @@ -4097,15 +3931,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 36 * 13); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 36 * 13, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ @@ -4198,11 +4024,7 @@ int sp_ModExp_2048(const mp_int* base, const mp_int* exp, const mp_int* mod, { #ifdef WOLFSSL_SP_SMALL int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[72 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 72 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -4221,14 +4043,7 @@ int sp_ModExp_2048(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 72 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 72 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 72 * 2; @@ -4246,24 +4061,11 @@ int sp_ModExp_2048(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_2048_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 72U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 72U, NULL, DYNAMIC_TYPE_DH); + return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[72 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 72 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -4283,13 +4085,7 @@ int sp_ModExp_2048(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 72 * 4, NULL, DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 72 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 72 * 2; @@ -4307,18 +4103,7 @@ int sp_ModExp_2048(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_2048_to_mp(r, res); } - -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 72U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 72U, NULL, DYNAMIC_TYPE_DH); return err; #endif @@ -4501,11 +4286,7 @@ SP_NOINLINE static void sp_2048_lshift_72(sp_digit* r, const sp_digit* a, */ static int sp_2048_mod_exp_2_72(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[217]; -#endif + SP_DECL_VAR(sp_digit, td, 217); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -4520,15 +4301,7 @@ static int sp_2048_mod_exp_2_72(sp_digit* r, const sp_digit* e, int bits, const err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 217, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 217, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 144; @@ -4599,9 +4372,7 @@ static int sp_2048_mod_exp_2_72(sp_digit* r, const sp_digit* e, int bits, const sp_2048_cond_sub_72(r, r, m, (sp_digit)~(n >> 31)); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -4623,11 +4394,7 @@ static int sp_2048_mod_exp_2_72(sp_digit* r, const sp_digit* e, int bits, const int sp_DhExp_2048(const mp_int* base, const byte* exp, word32 expLen, const mp_int* mod, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[72 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 72 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -4647,14 +4414,7 @@ int sp_DhExp_2048(const mp_int* base, const byte* exp, word32 expLen, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 72 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 72 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 72 * 2; @@ -4688,17 +4448,7 @@ int sp_DhExp_2048(const mp_int* base, const byte* exp, word32 expLen, XMEMMOVE(out, out + i, *outLen); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 72U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 72U, NULL, DYNAMIC_TYPE_DH); return err; } @@ -4718,11 +4468,7 @@ int sp_ModExp_1024(const mp_int* base, const mp_int* exp, const mp_int* mod, { #ifdef WOLFSSL_SP_SMALL int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[36 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 36 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -4741,14 +4487,7 @@ int sp_ModExp_1024(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 36 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 36 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 36 * 2; @@ -4767,24 +4506,11 @@ int sp_ModExp_1024(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_2048_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 72U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 72U, NULL, DYNAMIC_TYPE_DH); + return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[36 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 36 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -4804,13 +4530,7 @@ int sp_ModExp_1024(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 36 * 4, NULL, DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 36 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 36 * 2; @@ -4829,18 +4549,7 @@ int sp_ModExp_1024(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_2048_to_mp(r, res); } - -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 72U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 72U, NULL, DYNAMIC_TYPE_DH); return err; #endif @@ -5707,30 +5416,16 @@ static int sp_3072_div_53(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_32 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 53 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 53 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 53 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 53 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 106 + 1; sd = t2 + 53 + 1; @@ -5773,9 +5468,7 @@ static int sp_3072_div_53(const sp_digit* a, const sp_digit* d, sp_3072_rshift_53(r, r, 1); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -5807,11 +5500,7 @@ static int sp_3072_mod_exp_53(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 106]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 106); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -5825,15 +5514,7 @@ static int sp_3072_mod_exp_53(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 53 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 106, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -5891,17 +5572,11 @@ static int sp_3072_mod_exp_53(sp_digit* r, const sp_digit* a, const sp_digit* e, } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #elif !defined(WC_NO_CACHE_RESISTANT) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 106]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 106); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -5915,15 +5590,7 @@ static int sp_3072_mod_exp_53(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 53 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 106, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -5980,17 +5647,11 @@ static int sp_3072_mod_exp_53(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, t[0], sizeof(*r) * 53 * 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(32 * 106) + 106]; -#endif + SP_DECL_VAR(sp_digit, td, (32 * 106) + 106); sp_digit* t[32]; sp_digit* rt = NULL; sp_digit* norm = NULL; @@ -6005,15 +5666,8 @@ static int sp_3072_mod_exp_53(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 106) + 106), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (32 * 106) + 106, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) @@ -6125,9 +5779,7 @@ static int sp_3072_mod_exp_53(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, rt, sizeof(sp_digit) * 106); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #endif @@ -6542,30 +6194,16 @@ static int sp_3072_div_106(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_32 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 106 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 106 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 106 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 106 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 212 + 1; sd = t2 + 106 + 1; @@ -6608,9 +6246,7 @@ static int sp_3072_div_106(const sp_digit* a, const sp_digit* d, sp_3072_rshift_106(r, r, 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -6643,11 +6279,7 @@ static int sp_3072_mod_exp_106(sp_digit* r, const sp_digit* a, const sp_digit* e int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 212]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 212); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -6661,15 +6293,7 @@ static int sp_3072_mod_exp_106(sp_digit* r, const sp_digit* a, const sp_digit* e err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 106 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 212, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -6727,17 +6351,11 @@ static int sp_3072_mod_exp_106(sp_digit* r, const sp_digit* a, const sp_digit* e } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #elif !defined(WC_NO_CACHE_RESISTANT) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 212]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 212); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -6751,15 +6369,7 @@ static int sp_3072_mod_exp_106(sp_digit* r, const sp_digit* a, const sp_digit* e err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 106 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 212, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -6816,17 +6426,11 @@ static int sp_3072_mod_exp_106(sp_digit* r, const sp_digit* a, const sp_digit* e XMEMCPY(r, t[0], sizeof(*r) * 106 * 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(16 * 212) + 212]; -#endif + SP_DECL_VAR(sp_digit, td, (16 * 212) + 212); sp_digit* t[16]; sp_digit* rt = NULL; sp_digit* norm = NULL; @@ -6841,15 +6445,8 @@ static int sp_3072_mod_exp_106(sp_digit* r, const sp_digit* a, const sp_digit* e err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 212) + 212), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (16 * 212) + 212, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) @@ -6944,9 +6541,7 @@ static int sp_3072_mod_exp_106(sp_digit* r, const sp_digit* a, const sp_digit* e XMEMCPY(r, rt, sizeof(sp_digit) * 212); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #endif @@ -6970,11 +6565,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { #ifdef WOLFSSL_SP_SMALL -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[106 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 106 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit* norm = NULL; @@ -7002,15 +6593,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 106 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 106 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { r = a + 106 * 2; m = r + 106 * 2; @@ -7063,17 +6646,11 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[106 * 5]; -#endif + SP_DECL_VAR(sp_digit, d, 106 * 5); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -7098,15 +6675,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 106 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 106 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d; r = a + 106 * 2; @@ -7174,9 +6743,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(d, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ @@ -7208,11 +6775,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) #if defined(WOLFSSL_SP_SMALL) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[106 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 106 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -7242,15 +6805,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 106 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 106 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 106; m = a + 212; @@ -7267,25 +6822,11 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 106); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 106, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[106 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 106 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -7315,15 +6856,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 106 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 106 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 106; m = a + 212; @@ -7340,27 +6873,13 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 106); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 106, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ #else #if defined(WOLFSSL_SP_SMALL) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[53 * 8]; -#endif + SP_DECL_VAR(sp_digit, a, 53 * 8); sp_digit* p = NULL; sp_digit* dp = NULL; sp_digit* dq = NULL; @@ -7394,14 +6913,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 53 * 8, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, a, 53 * 8, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 106; qi = dq = dp = p + 53; @@ -7442,23 +6954,11 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 53 * 8); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR(sp_digit, a, 53 * 8, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[53 * 13]; -#endif + SP_DECL_VAR(sp_digit, a, 53 * 13); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -7493,15 +6993,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 53 * 13, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 53 * 13, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 106 * 2; q = p + 53; @@ -7544,15 +7036,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 53 * 13); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 53 * 13, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ @@ -7645,11 +7129,7 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, { #ifdef WOLFSSL_SP_SMALL int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[106 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 106 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -7668,14 +7148,7 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 106 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 106 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 106 * 2; @@ -7693,24 +7166,11 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_3072_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 106U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 106U, NULL, DYNAMIC_TYPE_DH); + return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[106 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 106 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -7730,13 +7190,7 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 106 * 4, NULL, DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 106 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 106 * 2; @@ -7754,18 +7208,7 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_3072_to_mp(r, res); } - -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 106U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 106U, NULL, DYNAMIC_TYPE_DH); return err; #endif @@ -7798,11 +7241,7 @@ SP_NOINLINE static void sp_3072_lshift_106(sp_digit* r, const sp_digit* a, */ static int sp_3072_mod_exp_2_106(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[319]; -#endif + SP_DECL_VAR(sp_digit, td, 319); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -7817,15 +7256,7 @@ static int sp_3072_mod_exp_2_106(sp_digit* r, const sp_digit* e, int bits, const err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 319, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 319, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 212; @@ -7896,9 +7327,7 @@ static int sp_3072_mod_exp_2_106(sp_digit* r, const sp_digit* e, int bits, const sp_3072_cond_sub_106(r, r, m, (sp_digit)~(n >> 31)); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -7920,11 +7349,7 @@ static int sp_3072_mod_exp_2_106(sp_digit* r, const sp_digit* e, int bits, const int sp_DhExp_3072(const mp_int* base, const byte* exp, word32 expLen, const mp_int* mod, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[106 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 106 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -7944,14 +7369,7 @@ int sp_DhExp_3072(const mp_int* base, const byte* exp, word32 expLen, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 106 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 106 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 106 * 2; @@ -7985,17 +7403,7 @@ int sp_DhExp_3072(const mp_int* base, const byte* exp, word32 expLen, XMEMMOVE(out, out + i, *outLen); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 106U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 106U, NULL, DYNAMIC_TYPE_DH); return err; } @@ -8015,11 +7423,7 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, { #ifdef WOLFSSL_SP_SMALL int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[53 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 53 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -8038,14 +7442,7 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 53 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 53 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 53 * 2; @@ -8064,24 +7461,11 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_3072_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 106U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 106U, NULL, DYNAMIC_TYPE_DH); + return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[53 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 53 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -8101,13 +7485,7 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 53 * 4, NULL, DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 53 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 53 * 2; @@ -8126,18 +7504,7 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_3072_to_mp(r, res); } - -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 106U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 106U, NULL, DYNAMIC_TYPE_DH); return err; #endif @@ -9730,30 +9097,16 @@ static int sp_3072_div_56(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_32 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 56 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 56 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 56 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 56 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 112 + 1; sd = t2 + 56 + 1; @@ -9796,9 +9149,7 @@ static int sp_3072_div_56(const sp_digit* a, const sp_digit* d, r[55] = 0; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -9830,11 +9181,7 @@ static int sp_3072_mod_exp_56(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 112]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 112); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -9848,15 +9195,7 @@ static int sp_3072_mod_exp_56(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 56 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 112, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -9914,17 +9253,11 @@ static int sp_3072_mod_exp_56(sp_digit* r, const sp_digit* a, const sp_digit* e, } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #elif !defined(WC_NO_CACHE_RESISTANT) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 112]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 112); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -9938,15 +9271,7 @@ static int sp_3072_mod_exp_56(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 56 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 112, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -10003,17 +9328,11 @@ static int sp_3072_mod_exp_56(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, t[0], sizeof(*r) * 56 * 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(32 * 112) + 112]; -#endif + SP_DECL_VAR(sp_digit, td, (32 * 112) + 112); sp_digit* t[32]; sp_digit* rt = NULL; sp_digit* norm = NULL; @@ -10028,15 +9347,8 @@ static int sp_3072_mod_exp_56(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 112) + 112), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (32 * 112) + 112, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) @@ -10148,9 +9460,7 @@ static int sp_3072_mod_exp_56(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, rt, sizeof(sp_digit) * 112); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #endif @@ -10644,30 +9954,16 @@ static int sp_3072_div_112(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_32 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 112 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 112 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 112 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 112 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 224 + 1; sd = t2 + 112 + 1; @@ -10711,9 +10007,7 @@ static int sp_3072_div_112(const sp_digit* a, const sp_digit* d, r[111] = 0; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -10748,11 +10042,7 @@ static int sp_3072_mod_exp_112(sp_digit* r, const sp_digit* a, const sp_digit* e int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 224]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 224); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -10766,15 +10056,7 @@ static int sp_3072_mod_exp_112(sp_digit* r, const sp_digit* a, const sp_digit* e err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 112 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 224, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -10832,17 +10114,11 @@ static int sp_3072_mod_exp_112(sp_digit* r, const sp_digit* a, const sp_digit* e } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #elif !defined(WC_NO_CACHE_RESISTANT) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 224]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 224); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -10856,15 +10132,7 @@ static int sp_3072_mod_exp_112(sp_digit* r, const sp_digit* a, const sp_digit* e err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 112 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 224, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -10921,17 +10189,11 @@ static int sp_3072_mod_exp_112(sp_digit* r, const sp_digit* a, const sp_digit* e XMEMCPY(r, t[0], sizeof(*r) * 112 * 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(16 * 224) + 224]; -#endif + SP_DECL_VAR(sp_digit, td, (16 * 224) + 224); sp_digit* t[16]; sp_digit* rt = NULL; sp_digit* norm = NULL; @@ -10946,15 +10208,8 @@ static int sp_3072_mod_exp_112(sp_digit* r, const sp_digit* a, const sp_digit* e err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 224) + 224), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (16 * 224) + 224, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) @@ -11049,9 +10304,7 @@ static int sp_3072_mod_exp_112(sp_digit* r, const sp_digit* a, const sp_digit* e XMEMCPY(r, rt, sizeof(sp_digit) * 224); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #endif @@ -11077,11 +10330,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { #ifdef WOLFSSL_SP_SMALL -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[112 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 112 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit* norm = NULL; @@ -11109,15 +10358,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 112 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 112 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { r = a + 112 * 2; m = r + 112 * 2; @@ -11170,17 +10411,11 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[112 * 5]; -#endif + SP_DECL_VAR(sp_digit, d, 112 * 5); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -11205,15 +10440,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 112 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 112 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d; r = a + 112 * 2; @@ -11281,9 +10508,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(d, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ @@ -11315,11 +10540,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) #if defined(WOLFSSL_SP_SMALL) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[112 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 112 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -11349,15 +10570,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 112 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 112 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 112; m = a + 224; @@ -11374,25 +10587,11 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 112); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 112, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[112 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 112 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -11422,15 +10621,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 112 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 112 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 112; m = a + 224; @@ -11447,27 +10638,13 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 112); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 112, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ #else #if defined(WOLFSSL_SP_SMALL) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[56 * 8]; -#endif + SP_DECL_VAR(sp_digit, a, 56 * 8); sp_digit* p = NULL; sp_digit* dp = NULL; sp_digit* dq = NULL; @@ -11501,14 +10678,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 56 * 8, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, a, 56 * 8, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 112; qi = dq = dp = p + 56; @@ -11549,23 +10719,11 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 56 * 8); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR(sp_digit, a, 56 * 8, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[56 * 13]; -#endif + SP_DECL_VAR(sp_digit, a, 56 * 13); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -11600,15 +10758,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 56 * 13, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 56 * 13, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 112 * 2; q = p + 56; @@ -11651,15 +10801,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 56 * 13); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 56 * 13, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ @@ -11752,11 +10894,7 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, { #ifdef WOLFSSL_SP_SMALL int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[112 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 112 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -11775,14 +10913,7 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 112 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 112 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 112 * 2; @@ -11800,24 +10931,11 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_3072_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 112U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 112U, NULL, DYNAMIC_TYPE_DH); + return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[112 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 112 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -11837,13 +10955,7 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 112 * 4, NULL, DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 112 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 112 * 2; @@ -11861,18 +10973,7 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_3072_to_mp(r, res); } - -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 112U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 112U, NULL, DYNAMIC_TYPE_DH); return err; #endif @@ -12126,11 +11227,7 @@ SP_NOINLINE static void sp_3072_lshift_112(sp_digit* r, const sp_digit* a, */ static int sp_3072_mod_exp_2_112(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[337]; -#endif + SP_DECL_VAR(sp_digit, td, 337); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -12145,15 +11242,7 @@ static int sp_3072_mod_exp_2_112(sp_digit* r, const sp_digit* e, int bits, const err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 337, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 337, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 224; @@ -12224,9 +11313,7 @@ static int sp_3072_mod_exp_2_112(sp_digit* r, const sp_digit* e, int bits, const sp_3072_cond_sub_112(r, r, m, (sp_digit)~(n >> 31)); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -12248,11 +11335,7 @@ static int sp_3072_mod_exp_2_112(sp_digit* r, const sp_digit* e, int bits, const int sp_DhExp_3072(const mp_int* base, const byte* exp, word32 expLen, const mp_int* mod, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[112 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 112 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -12272,14 +11355,7 @@ int sp_DhExp_3072(const mp_int* base, const byte* exp, word32 expLen, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 112 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 112 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 112 * 2; @@ -12313,17 +11389,7 @@ int sp_DhExp_3072(const mp_int* base, const byte* exp, word32 expLen, XMEMMOVE(out, out + i, *outLen); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 112U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 112U, NULL, DYNAMIC_TYPE_DH); return err; } @@ -12343,11 +11409,7 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, { #ifdef WOLFSSL_SP_SMALL int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[56 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 56 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -12366,14 +11428,7 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 56 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 56 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 56 * 2; @@ -12392,24 +11447,11 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_3072_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 112U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 112U, NULL, DYNAMIC_TYPE_DH); + return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[56 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 56 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -12429,13 +11471,7 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 56 * 4, NULL, DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 56 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 56 * 2; @@ -12454,18 +11490,7 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_3072_to_mp(r, res); } - -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 112U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 112U, NULL, DYNAMIC_TYPE_DH); return err; #endif @@ -13342,30 +12367,16 @@ static int sp_4096_div_71(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_32 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 71 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 71 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 71 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 71 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 142 + 1; sd = t2 + 71 + 1; @@ -13408,9 +12419,7 @@ static int sp_4096_div_71(const sp_digit* a, const sp_digit* d, sp_4096_rshift_71(r, r, 11); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -13442,11 +12451,7 @@ static int sp_4096_mod_exp_71(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 142]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 142); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -13460,15 +12465,7 @@ static int sp_4096_mod_exp_71(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 71 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 142, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -13526,17 +12523,11 @@ static int sp_4096_mod_exp_71(sp_digit* r, const sp_digit* a, const sp_digit* e, } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #elif !defined(WC_NO_CACHE_RESISTANT) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 142]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 142); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -13550,15 +12541,7 @@ static int sp_4096_mod_exp_71(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 71 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 142, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -13615,17 +12598,11 @@ static int sp_4096_mod_exp_71(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, t[0], sizeof(*r) * 71 * 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(32 * 142) + 142]; -#endif + SP_DECL_VAR(sp_digit, td, (32 * 142) + 142); sp_digit* t[32]; sp_digit* rt = NULL; sp_digit* norm = NULL; @@ -13640,15 +12617,8 @@ static int sp_4096_mod_exp_71(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 142) + 142), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (32 * 142) + 142, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) @@ -13760,9 +12730,7 @@ static int sp_4096_mod_exp_71(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, rt, sizeof(sp_digit) * 142); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #endif @@ -14178,30 +13146,16 @@ static int sp_4096_div_142(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_32 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 142 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 142 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 142 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 142 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 284 + 1; sd = t2 + 142 + 1; @@ -14244,9 +13198,7 @@ static int sp_4096_div_142(const sp_digit* a, const sp_digit* d, sp_4096_rshift_142(r, r, 22); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -14279,11 +13231,7 @@ static int sp_4096_mod_exp_142(sp_digit* r, const sp_digit* a, const sp_digit* e int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 284]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 284); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -14297,15 +13245,7 @@ static int sp_4096_mod_exp_142(sp_digit* r, const sp_digit* a, const sp_digit* e err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 142 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 284, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -14363,17 +13303,11 @@ static int sp_4096_mod_exp_142(sp_digit* r, const sp_digit* a, const sp_digit* e } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #elif !defined(WC_NO_CACHE_RESISTANT) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 284]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 284); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -14387,15 +13321,7 @@ static int sp_4096_mod_exp_142(sp_digit* r, const sp_digit* a, const sp_digit* e err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 142 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 284, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -14452,17 +13378,11 @@ static int sp_4096_mod_exp_142(sp_digit* r, const sp_digit* a, const sp_digit* e XMEMCPY(r, t[0], sizeof(*r) * 142 * 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(16 * 284) + 284]; -#endif + SP_DECL_VAR(sp_digit, td, (16 * 284) + 284); sp_digit* t[16]; sp_digit* rt = NULL; sp_digit* norm = NULL; @@ -14477,15 +13397,8 @@ static int sp_4096_mod_exp_142(sp_digit* r, const sp_digit* a, const sp_digit* e err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 284) + 284), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (16 * 284) + 284, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) @@ -14580,9 +13493,7 @@ static int sp_4096_mod_exp_142(sp_digit* r, const sp_digit* a, const sp_digit* e XMEMCPY(r, rt, sizeof(sp_digit) * 284); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #endif @@ -14606,11 +13517,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { #ifdef WOLFSSL_SP_SMALL -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[142 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 142 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit* norm = NULL; @@ -14638,15 +13545,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 142 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 142 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { r = a + 142 * 2; m = r + 142 * 2; @@ -14699,17 +13598,11 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[142 * 5]; -#endif + SP_DECL_VAR(sp_digit, d, 142 * 5); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -14734,15 +13627,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 142 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 142 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d; r = a + 142 * 2; @@ -14810,9 +13695,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(d, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ @@ -14844,11 +13727,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) #if defined(WOLFSSL_SP_SMALL) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[142 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 142 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -14878,15 +13757,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 142 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 142 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 142; m = a + 284; @@ -14903,25 +13774,11 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 142); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 142, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[142 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 142 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -14951,15 +13808,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 142 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 142 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 142; m = a + 284; @@ -14976,27 +13825,13 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 142); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 142, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ #else #if defined(WOLFSSL_SP_SMALL) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[71 * 8]; -#endif + SP_DECL_VAR(sp_digit, a, 71 * 8); sp_digit* p = NULL; sp_digit* dp = NULL; sp_digit* dq = NULL; @@ -15030,14 +13865,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 71 * 8, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, a, 71 * 8, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 142; qi = dq = dp = p + 71; @@ -15078,23 +13906,11 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 71 * 8); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR(sp_digit, a, 71 * 8, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[71 * 13]; -#endif + SP_DECL_VAR(sp_digit, a, 71 * 13); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -15129,15 +13945,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 71 * 13, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 71 * 13, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 142 * 2; q = p + 71; @@ -15180,15 +13988,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 71 * 13); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 71 * 13, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ @@ -15281,11 +14081,7 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, { #ifdef WOLFSSL_SP_SMALL int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[142 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 142 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -15304,14 +14100,7 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 142 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 142 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 142 * 2; @@ -15329,24 +14118,11 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_4096_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 142U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 142U, NULL, DYNAMIC_TYPE_DH); + return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[142 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 142 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -15366,13 +14142,7 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 142 * 4, NULL, DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 142 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 142 * 2; @@ -15390,18 +14160,7 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_4096_to_mp(r, res); } - -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 142U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 142U, NULL, DYNAMIC_TYPE_DH); return err; #endif @@ -15434,11 +14193,7 @@ SP_NOINLINE static void sp_4096_lshift_142(sp_digit* r, const sp_digit* a, */ static int sp_4096_mod_exp_2_142(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[427]; -#endif + SP_DECL_VAR(sp_digit, td, 427); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -15453,15 +14208,7 @@ static int sp_4096_mod_exp_2_142(sp_digit* r, const sp_digit* e, int bits, const err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 427, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 427, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 284; @@ -15532,9 +14279,7 @@ static int sp_4096_mod_exp_2_142(sp_digit* r, const sp_digit* e, int bits, const sp_4096_cond_sub_142(r, r, m, (sp_digit)~(n >> 31)); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -15556,11 +14301,7 @@ static int sp_4096_mod_exp_2_142(sp_digit* r, const sp_digit* e, int bits, const int sp_DhExp_4096(const mp_int* base, const byte* exp, word32 expLen, const mp_int* mod, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[142 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 142 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -15580,14 +14321,7 @@ int sp_DhExp_4096(const mp_int* base, const byte* exp, word32 expLen, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 142 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 142 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 142 * 2; @@ -15621,17 +14355,7 @@ int sp_DhExp_4096(const mp_int* base, const byte* exp, word32 expLen, XMEMMOVE(out, out + i, *outLen); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 142U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 142U, NULL, DYNAMIC_TYPE_DH); return err; } @@ -17245,30 +15969,16 @@ static int sp_4096_div_81(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_32 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 81 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 81 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 81 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 81 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 162 + 1; sd = t2 + 81 + 1; @@ -17312,9 +16022,7 @@ static int sp_4096_div_81(const sp_digit* a, const sp_digit* d, r[80] = 0; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -17346,11 +16054,7 @@ static int sp_4096_mod_exp_81(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 162]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 162); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -17364,15 +16068,7 @@ static int sp_4096_mod_exp_81(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 81 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 162, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -17430,17 +16126,11 @@ static int sp_4096_mod_exp_81(sp_digit* r, const sp_digit* a, const sp_digit* e, } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #elif !defined(WC_NO_CACHE_RESISTANT) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 162]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 162); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -17454,15 +16144,7 @@ static int sp_4096_mod_exp_81(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 81 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 162, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -17519,17 +16201,11 @@ static int sp_4096_mod_exp_81(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, t[0], sizeof(*r) * 81 * 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(32 * 162) + 162]; -#endif + SP_DECL_VAR(sp_digit, td, (32 * 162) + 162); sp_digit* t[32]; sp_digit* rt = NULL; sp_digit* norm = NULL; @@ -17544,15 +16220,8 @@ static int sp_4096_mod_exp_81(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 162) + 162), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (32 * 162) + 162, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) @@ -17664,9 +16333,7 @@ static int sp_4096_mod_exp_81(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, rt, sizeof(sp_digit) * 162); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #endif @@ -18145,30 +16812,16 @@ static int sp_4096_div_162(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_32 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 162 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 162 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 162 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 162 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 324 + 1; sd = t2 + 162 + 1; @@ -18214,9 +16867,7 @@ static int sp_4096_div_162(const sp_digit* a, const sp_digit* d, r[161] = 0; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -18251,11 +16902,7 @@ static int sp_4096_mod_exp_162(sp_digit* r, const sp_digit* a, const sp_digit* e int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 324]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 324); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -18269,15 +16916,7 @@ static int sp_4096_mod_exp_162(sp_digit* r, const sp_digit* a, const sp_digit* e err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 162 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 324, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -18335,17 +16974,11 @@ static int sp_4096_mod_exp_162(sp_digit* r, const sp_digit* a, const sp_digit* e } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #elif !defined(WC_NO_CACHE_RESISTANT) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 324]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 324); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -18359,15 +16992,7 @@ static int sp_4096_mod_exp_162(sp_digit* r, const sp_digit* a, const sp_digit* e err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 162 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 324, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -18424,17 +17049,11 @@ static int sp_4096_mod_exp_162(sp_digit* r, const sp_digit* a, const sp_digit* e XMEMCPY(r, t[0], sizeof(*r) * 162 * 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(16 * 324) + 324]; -#endif + SP_DECL_VAR(sp_digit, td, (16 * 324) + 324); sp_digit* t[16]; sp_digit* rt = NULL; sp_digit* norm = NULL; @@ -18449,15 +17068,8 @@ static int sp_4096_mod_exp_162(sp_digit* r, const sp_digit* a, const sp_digit* e err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 324) + 324), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (16 * 324) + 324, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) @@ -18552,9 +17164,7 @@ static int sp_4096_mod_exp_162(sp_digit* r, const sp_digit* a, const sp_digit* e XMEMCPY(r, rt, sizeof(sp_digit) * 324); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #endif @@ -18580,11 +17190,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { #ifdef WOLFSSL_SP_SMALL -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[162 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 162 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit* norm = NULL; @@ -18612,15 +17218,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 162 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 162 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { r = a + 162 * 2; m = r + 162 * 2; @@ -18673,17 +17271,11 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[162 * 5]; -#endif + SP_DECL_VAR(sp_digit, d, 162 * 5); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -18708,15 +17300,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 162 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 162 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d; r = a + 162 * 2; @@ -18784,9 +17368,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(d, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ @@ -18818,11 +17400,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) #if defined(WOLFSSL_SP_SMALL) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[162 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 162 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -18852,15 +17430,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 162 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 162 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 162; m = a + 324; @@ -18877,25 +17447,11 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 162); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 162, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[162 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 162 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -18925,15 +17481,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 162 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 162 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 162; m = a + 324; @@ -18950,27 +17498,13 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 162); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 162, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ #else #if defined(WOLFSSL_SP_SMALL) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[81 * 8]; -#endif + SP_DECL_VAR(sp_digit, a, 81 * 8); sp_digit* p = NULL; sp_digit* dp = NULL; sp_digit* dq = NULL; @@ -19004,14 +17538,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 81 * 8, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, a, 81 * 8, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 162; qi = dq = dp = p + 81; @@ -19052,23 +17579,11 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 81 * 8); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR(sp_digit, a, 81 * 8, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[81 * 13]; -#endif + SP_DECL_VAR(sp_digit, a, 81 * 13); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -19103,15 +17618,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 81 * 13, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 81 * 13, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 162 * 2; q = p + 81; @@ -19154,15 +17661,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 81 * 13); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 81 * 13, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ @@ -19255,11 +17754,7 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, { #ifdef WOLFSSL_SP_SMALL int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[162 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 162 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -19278,14 +17773,7 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 162 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 162 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 162 * 2; @@ -19303,24 +17791,11 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_4096_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 162U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 162U, NULL, DYNAMIC_TYPE_DH); + return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[162 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 162 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -19340,13 +17815,7 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 162 * 4, NULL, DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 162 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 162 * 2; @@ -19364,18 +17833,7 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_4096_to_mp(r, res); } - -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 162U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 162U, NULL, DYNAMIC_TYPE_DH); return err; #endif @@ -19729,11 +18187,7 @@ SP_NOINLINE static void sp_4096_lshift_162(sp_digit* r, const sp_digit* a, */ static int sp_4096_mod_exp_2_162(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[487]; -#endif + SP_DECL_VAR(sp_digit, td, 487); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -19748,15 +18202,7 @@ static int sp_4096_mod_exp_2_162(sp_digit* r, const sp_digit* e, int bits, const err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 487, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 487, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 324; @@ -19827,9 +18273,7 @@ static int sp_4096_mod_exp_2_162(sp_digit* r, const sp_digit* e, int bits, const sp_4096_cond_sub_162(r, r, m, (sp_digit)~(n >> 31)); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -19851,11 +18295,7 @@ static int sp_4096_mod_exp_2_162(sp_digit* r, const sp_digit* e, int bits, const int sp_DhExp_4096(const mp_int* base, const byte* exp, word32 expLen, const mp_int* mod, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[162 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 162 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -19875,14 +18315,7 @@ int sp_DhExp_4096(const mp_int* base, const byte* exp, word32 expLen, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 162 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 162 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 162 * 2; @@ -19916,17 +18349,7 @@ int sp_DhExp_4096(const mp_int* base, const byte* exp, word32 expLen, XMEMMOVE(out, out + i, *outLen); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 162U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 162U, NULL, DYNAMIC_TYPE_DH); return err; } @@ -21742,23 +20165,14 @@ static int sp_256_proj_point_add_9_nb(sp_ecc_ctx_t* sp_ctx, sp_point_256* r, */ static int sp_256_mod_mul_norm_9(sp_digit* r, const sp_digit* a, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - int64_t* t = NULL; -#else - int64_t t[2 * 8]; -#endif + SP_DECL_VAR(int64_t, t, 2 * 8); int64_t* a32 = NULL; int64_t o; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (int64_t*)XMALLOC(sizeof(int64_t) * 2 * 8, NULL, DYNAMIC_TYPE_ECC); - if (t == NULL) - return MEMORY_E; -#endif - + SP_ALLOC_VAR(int64_t, t, 2 * 8, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { a32 = t + 8; @@ -21849,9 +20263,7 @@ static int sp_256_mod_mul_norm_9(sp_digit* r, const sp_digit* a, const sp_digit* r[8] = (sp_digit)(t[7] >> 8U); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -21875,13 +20287,8 @@ static int sp_256_mod_mul_norm_9(sp_digit* r, const sp_digit* a, const sp_digit* static int sp_256_ecc_mulmod_9(sp_point_256* r, const sp_point_256* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_256 t[3]; - sp_digit tmp[2 * 9 * 6]; -#endif + SP_DECL_VAR(sp_point_256, t, 3); + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 6); sp_digit n; int i; int c; @@ -21892,18 +20299,8 @@ static int sp_256_ecc_mulmod_9(sp_point_256* r, const sp_point_256* g, (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_point_256, t, 3, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { XMEMSET(t, 0, sizeof(sp_point_256) * 3); @@ -21953,24 +20350,8 @@ static int sp_256_ecc_mulmod_9(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 9 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (t != NULL) -#endif - { - ForceZero(t, sizeof(sp_point_256) * 3); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_point_256, t, 3, heap, DYNAMIC_TYPE_ECC); return err; } @@ -22544,13 +20925,8 @@ static void sp_256_get_point_33_9(sp_point_256* r, const sp_point_256* table, static int sp_256_ecc_mulmod_win_add_sub_9(sp_point_256* r, const sp_point_256* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_256 t[33+2]; - sp_digit tmp[2 * 9 * 6]; -#endif + SP_DECL_VAR(sp_point_256, t, 33+2); + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 6); sp_point_256* rt = NULL; sp_point_256* p = NULL; sp_digit* negy; @@ -22562,19 +20938,8 @@ static int sp_256_ecc_mulmod_win_add_sub_9(sp_point_256* r, const sp_point_256* (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * - (33+2), heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, - heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, t, 33+2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 33; p = t + 33+1; @@ -22658,10 +21023,8 @@ static int sp_256_ecc_mulmod_win_add_sub_9(sp_point_256* r, const sp_point_256* } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; } @@ -22786,11 +21149,7 @@ static void sp_256_proj_to_affine_9(sp_point_256* a, sp_digit* t) static int sp_256_gen_stripe_table_9(const sp_point_256* a, sp_table_entry_256* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* t = NULL; -#else - sp_point_256 t[3]; -#endif + SP_DECL_VAR(sp_point_256, t, 3); sp_point_256* s1 = NULL; sp_point_256* s2 = NULL; int i; @@ -22799,13 +21158,7 @@ static int sp_256_gen_stripe_table_9(const sp_point_256* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_256, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -22854,9 +21207,7 @@ static int sp_256_gen_stripe_table_9(const sp_point_256* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -22936,13 +21287,8 @@ static int sp_256_ecc_mulmod_stripe_9(sp_point_256* r, const sp_point_256* g, const sp_table_entry_256* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_256 rt[2]; - sp_digit t[2 * 9 * 6]; -#endif + SP_DECL_VAR(sp_point_256, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 9 * 6); sp_point_256* p = NULL; int i; int j; @@ -22956,19 +21302,8 @@ static int sp_256_ecc_mulmod_stripe_9(sp_point_256* r, const sp_point_256* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -23022,10 +21357,8 @@ static int sp_256_ecc_mulmod_stripe_9(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -23142,20 +21475,11 @@ static int sp_256_ecc_mulmod_9(sp_point_256* r, const sp_point_256* g, #ifndef FP_ECC return sp_256_ecc_mulmod_win_add_sub_9(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 9 * 6]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 6); sp_cache_256_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -23188,9 +21512,7 @@ static int sp_256_ecc_mulmod_9(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -23209,28 +21531,12 @@ static int sp_256_ecc_mulmod_9(sp_point_256* r, const sp_point_256* g, int sp_ecc_mulmod_256(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[1]; - sp_digit k[9]; -#endif + SP_DECL_VAR(sp_point_256, point, 1); + SP_DECL_VAR(sp_digit, k, 9); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 9, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(k, 9, km); sp_256_point_from_ecc_point_9(point, gm); @@ -23241,10 +21547,8 @@ int sp_ecc_mulmod_256(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_256_point_to_ecc_point_9(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -23264,31 +21568,14 @@ int sp_ecc_mulmod_256(const mp_int* km, const ecc_point* gm, ecc_point* r, int sp_ecc_mulmod_add_256(const mp_int* km, const ecc_point* gm, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[2]; - sp_digit k[9 + 9 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_256, point, 2); + SP_DECL_VAR(sp_digit, k, 9 + 9 * 2 * 6); sp_point_256* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (9 + 9 * 2 * 6), heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 9 + 9 * 2 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 9; @@ -23319,10 +21606,8 @@ int sp_ecc_mulmod_add_256(const mp_int* km, const ecc_point* gm, err = sp_256_point_to_ecc_point_9(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -24675,28 +22960,12 @@ static int sp_256_ecc_mulmod_base_9(sp_point_256* r, const sp_digit* k, */ int sp_ecc_mulmod_base_256(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[1]; - sp_digit k[9]; -#endif + SP_DECL_VAR(sp_point_256, point, 1); + SP_DECL_VAR(sp_digit, k, 9); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 9, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(k, 9, km); @@ -24706,10 +22975,8 @@ int sp_ecc_mulmod_base_256(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_256_point_to_ecc_point_9(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -24728,31 +22995,14 @@ int sp_ecc_mulmod_base_256(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_256(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[2]; - sp_digit k[9 + 9 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_256, point, 2); + SP_DECL_VAR(sp_digit, k, 9 + 9 * 2 * 6); sp_point_256* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (9 + 9 * 2 * 6), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 9 + 9 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 9; @@ -24782,10 +23032,8 @@ int sp_ecc_mulmod_base_add_256(const mp_int* km, const ecc_point* am, err = sp_256_point_to_ecc_point_9(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -24885,17 +23133,12 @@ static int sp_256_ecc_gen_k_9(WC_RNG* rng, sp_digit* k) */ int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_DECL_VAR(sp_point_256, point, 2); #else - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - sp_point_256 point[2]; - #else - sp_point_256 point[1]; - #endif - sp_digit k[9]; + SP_DECL_VAR(sp_point_256, point, 1); #endif + SP_DECL_VAR(sp_digit, k, 9); #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN sp_point_256* infinity = NULL; #endif @@ -24904,22 +23147,12 @@ int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, DYNAMIC_TYPE_ECC); - #else - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, DYNAMIC_TYPE_ECC); - #endif - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_ALLOC_VAR(sp_point_256, point, 2, heap, DYNAMIC_TYPE_ECC); +#else + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); #endif - + SP_ALLOC_VAR(sp_digit, k, 9, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN infinity = point + 1; @@ -24949,11 +23182,9 @@ int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) err = sp_256_point_to_ecc_point_9(point, pub); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); /* point is not sensitive, so no need to zeroize */ - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -25096,34 +23327,16 @@ static void sp_256_to_bin_9(sp_digit* r, byte* a) int sp_ecc_secret_gen_256(const mp_int* priv, const ecc_point* pub, byte* out, word32* outLen, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[1]; - sp_digit k[9]; -#endif + SP_DECL_VAR(sp_point_256, point, 1); + SP_DECL_VAR(sp_digit, k, 9); int err = MP_OKAY; if (*outLen < 32U) { err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 9, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(k, 9, priv); sp_256_point_from_ecc_point_9(point, pub); @@ -25134,10 +23347,8 @@ int sp_ecc_secret_gen_256(const mp_int* priv, const ecc_point* pub, byte* out, *outLen = 32; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -25334,29 +23545,17 @@ SP_NOINLINE static void sp_256_lshift_18(sp_digit* r, const sp_digit* a, static int sp_256_div_9(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { + SP_DECL_VAR(sp_digit, t1, 4 * 9 + 3); int i; sp_digit r1; sp_digit mask; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 9 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 9 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 9 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 18 + 1; sd = t2 + 9 + 1; @@ -25380,9 +23579,7 @@ static int sp_256_div_9(const sp_digit* a, const sp_digit* d, sp_256_rshift_9(r, t1, 5); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -25679,13 +23876,8 @@ static int sp_256_calc_s_9(sp_digit* s, const sp_digit* r, sp_digit* k, int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* e = NULL; - sp_point_256* point = NULL; -#else - sp_digit e[7 * 2 * 9]; - sp_point_256 point[1]; -#endif + SP_DECL_VAR(sp_digit, e, 7 * 2 * 9); + SP_DECL_VAR(sp_point_256, point, 1); sp_digit* x = NULL; sp_digit* k = NULL; sp_digit* r = NULL; @@ -25697,21 +23889,8 @@ int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - e = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7 * 2 * 9, heap, - DYNAMIC_TYPE_ECC); - if (e == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, e, 7 * 2 * 9, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { x = e + 2 * 9; k = e + 4 * 9; @@ -25776,24 +23955,8 @@ int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, err = sp_256_to_mp(s, sm); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (e != NULL) -#endif - { - ForceZero(e, sizeof(sp_digit) * 7 * 2 * 9); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(e, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (point != NULL) -#endif - { - ForceZero(point, sizeof(sp_point_256)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(point, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_digit, e, 7 * 2 * 9, heap, DYNAMIC_TYPE_ECC); return err; } @@ -26006,24 +24169,14 @@ static int sp_256_num_bits_9(const sp_digit* a) static int sp_256_mod_inv_9(sp_digit* r, const sp_digit* a, const sp_digit* m) { int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u = NULL; -#else - sp_digit u[9 * 4]; -#endif + SP_DECL_VAR(sp_digit, u, 9 * 4); sp_digit* v = NULL; sp_digit* b = NULL; sp_digit* d = NULL; int ut; int vt; -#ifdef WOLFSSL_SP_SMALL_STACK - u = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9 * 4, NULL, - DYNAMIC_TYPE_ECC); - if (u == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, u, 9 * 4, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { v = u + 9; b = u + 2 * 9; @@ -26105,9 +24258,7 @@ static int sp_256_mod_inv_9(sp_digit* r, const sp_digit* a, const sp_digit* m) else XMEMCPY(r, d, sizeof(sp_digit) * 9); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(u, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -26227,13 +24378,8 @@ int sp_ecc_verify_256(const byte* hash, word32 hashLen, const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* rm, const mp_int* sm, int* res, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u1 = NULL; - sp_point_256* p1 = NULL; -#else - sp_digit u1[18 * 9]; - sp_point_256 p1[2]; -#endif + SP_DECL_VAR(sp_digit, u1, 18 * 9); + SP_DECL_VAR(sp_point_256, p1, 2); sp_digit* u2 = NULL; sp_digit* s = NULL; sp_digit* tmp = NULL; @@ -26242,21 +24388,8 @@ int sp_ecc_verify_256(const byte* hash, word32 hashLen, const mp_int* pX, sp_int32 c = 0; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p1 = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (p1 == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - u1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 9, heap, - DYNAMIC_TYPE_ECC); - if (u1 == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, u1, 18 * 9, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p1, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { u2 = u1 + 2 * 9; s = u1 + 4 * 9; @@ -26313,11 +24446,8 @@ int sp_ecc_verify_256(const byte* hash, word32 hashLen, const mp_int* pX, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u1, heap, DYNAMIC_TYPE_ECC); - XFREE(p1, heap, DYNAMIC_TYPE_ECC); -#endif - + SP_FREE_VAR(p1, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(u1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -26483,21 +24613,13 @@ int sp_ecc_verify_256_nb(sp_ecc_ctx_t* sp_ctx, const byte* hash, static int sp_256_ecc_is_point_9(const sp_point_256* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[9 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 9 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 9 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 9; @@ -26521,9 +24643,7 @@ static int sp_256_ecc_is_point_9(const sp_point_256* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -26537,21 +24657,11 @@ static int sp_256_ecc_is_point_9(const sp_point_256* point, */ int sp_ecc_is_point_256(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* pub = NULL; -#else - sp_point_256 pub[1]; -#endif + SP_DECL_VAR(sp_point_256, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_256*)XMALLOC(sizeof(sp_point_256), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_256, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(pub->x, 9, pX); sp_256_from_mp(pub->y, 9, pY); @@ -26560,9 +24670,7 @@ int sp_ecc_is_point_256(const mp_int* pX, const mp_int* pY) err = sp_256_ecc_is_point_9(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -26581,13 +24689,8 @@ int sp_ecc_is_point_256(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_256(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_256* pub = NULL; -#else - sp_digit priv[9]; - sp_point_256 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 9); + SP_DECL_VAR(sp_point_256, pub, 2); sp_point_256* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -26602,21 +24705,8 @@ int sp_ecc_check_key_256(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 9, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -26668,10 +24758,8 @@ int sp_ecc_check_key_256(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } @@ -26695,32 +24783,13 @@ int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_256* p = NULL; -#else - sp_digit tmp[2 * 9 * 6]; - sp_point_256 p[2]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 6); + SP_DECL_VAR(sp_point_256, p, 2); sp_point_256* q = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p, 2, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { q = p + 1; @@ -26748,10 +24817,8 @@ int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_256_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -26770,30 +24837,12 @@ int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_256* p = NULL; -#else - sp_digit tmp[2 * 9 * 2]; - sp_point_256 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 2); + SP_DECL_VAR(sp_point_256, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 2, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(p->x, 9, pX); sp_256_from_mp(p->y, 9, pY); @@ -26814,10 +24863,8 @@ int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_256_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -26832,30 +24879,13 @@ int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, */ int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_256* p = NULL; -#else - sp_digit tmp[2 * 9 * 4]; - sp_point_256 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 4); + SP_DECL_VAR(sp_point_256, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 4, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 4, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(p->x, 9, pX); sp_256_from_mp(p->y, 9, pY); @@ -26876,10 +24906,8 @@ int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ) err = sp_256_to_mp(p->z, pZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -26892,21 +24920,11 @@ int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ) */ static int sp_256_mont_sqrt_9(sp_digit* y) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 9]; -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 9); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 9, NULL, DYNAMIC_TYPE_ECC); - if (t1 == NULL) { - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, t1, 4 * 9, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 9; @@ -26943,9 +24961,7 @@ static int sp_256_mont_sqrt_9(sp_digit* y) } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -26960,20 +24976,11 @@ static int sp_256_mont_sqrt_9(sp_digit* y) */ int sp_ecc_uncompress_256(mp_int* xm, int odd, mp_int* ym) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* x = NULL; -#else - sp_digit x[4 * 9]; -#endif + SP_DECL_VAR(sp_digit, x, 4 * 9); sp_digit* y = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - x = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 9, NULL, DYNAMIC_TYPE_ECC); - if (x == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, x, 4 * 9, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { y = x + 2 * 9; @@ -27008,9 +25015,7 @@ int sp_ecc_uncompress_256(mp_int* xm, int odd, mp_int* ym) err = sp_256_to_mp(y, ym); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(x, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(x, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -29160,23 +27165,14 @@ static int sp_384_proj_point_add_15_nb(sp_ecc_ctx_t* sp_ctx, sp_point_384* r, */ static int sp_384_mod_mul_norm_15(sp_digit* r, const sp_digit* a, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - int64_t* t = NULL; -#else - int64_t t[2 * 12]; -#endif + SP_DECL_VAR(int64_t, t, 2 * 12); int64_t* a32 = NULL; int64_t o; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (int64_t*)XMALLOC(sizeof(int64_t) * 2 * 12, NULL, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(int64_t, t, 2 * 12, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { a32 = t + 12; @@ -29311,9 +27307,7 @@ static int sp_384_mod_mul_norm_15(sp_digit* r, const sp_digit* a, const sp_digit r[14] = (sp_digit)(t[11] >> 12U); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -29337,13 +27331,8 @@ static int sp_384_mod_mul_norm_15(sp_digit* r, const sp_digit* a, const sp_digit static int sp_384_ecc_mulmod_15(sp_point_384* r, const sp_point_384* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_384 t[3]; - sp_digit tmp[2 * 15 * 6]; -#endif + SP_DECL_VAR(sp_point_384, t, 3); + SP_DECL_VAR(sp_digit, tmp, 2 * 15 * 6); sp_digit n; int i; int c; @@ -29354,18 +27343,8 @@ static int sp_384_ecc_mulmod_15(sp_point_384* r, const sp_point_384* g, (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 15 * 6, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_point_384, t, 3, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 15 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { XMEMSET(t, 0, sizeof(sp_point_384) * 3); @@ -29415,24 +27394,8 @@ static int sp_384_ecc_mulmod_15(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 15 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (t != NULL) -#endif - { - ForceZero(t, sizeof(sp_point_384) * 3); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 15 * 6, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_point_384, t, 3, heap, DYNAMIC_TYPE_ECC); return err; } @@ -30054,13 +28017,8 @@ static void sp_384_get_point_33_15(sp_point_384* r, const sp_point_384* table, static int sp_384_ecc_mulmod_win_add_sub_15(sp_point_384* r, const sp_point_384* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_384 t[33+2]; - sp_digit tmp[2 * 15 * 6]; -#endif + SP_DECL_VAR(sp_point_384, t, 33+2); + SP_DECL_VAR(sp_digit, tmp, 2 * 15 * 6); sp_point_384* rt = NULL; sp_point_384* p = NULL; sp_digit* negy; @@ -30072,19 +28030,8 @@ static int sp_384_ecc_mulmod_win_add_sub_15(sp_point_384* r, const sp_point_384* (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * - (33+2), heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 15 * 6, - heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, t, 33+2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 15 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 33; p = t + 33+1; @@ -30168,10 +28115,8 @@ static int sp_384_ecc_mulmod_win_add_sub_15(sp_point_384* r, const sp_point_384* } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; } @@ -30296,11 +28241,7 @@ static void sp_384_proj_to_affine_15(sp_point_384* a, sp_digit* t) static int sp_384_gen_stripe_table_15(const sp_point_384* a, sp_table_entry_384* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* t = NULL; -#else - sp_point_384 t[3]; -#endif + SP_DECL_VAR(sp_point_384, t, 3); sp_point_384* s1 = NULL; sp_point_384* s2 = NULL; int i; @@ -30309,13 +28250,7 @@ static int sp_384_gen_stripe_table_15(const sp_point_384* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_384, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -30364,9 +28299,7 @@ static int sp_384_gen_stripe_table_15(const sp_point_384* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -30470,13 +28403,8 @@ static int sp_384_ecc_mulmod_stripe_15(sp_point_384* r, const sp_point_384* g, const sp_table_entry_384* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_384 rt[2]; - sp_digit t[2 * 15 * 6]; -#endif + SP_DECL_VAR(sp_point_384, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 15 * 6); sp_point_384* p = NULL; int i; int j; @@ -30490,19 +28418,8 @@ static int sp_384_ecc_mulmod_stripe_15(sp_point_384* r, const sp_point_384* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 15 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 15 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -30556,10 +28473,8 @@ static int sp_384_ecc_mulmod_stripe_15(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -30676,20 +28591,11 @@ static int sp_384_ecc_mulmod_15(sp_point_384* r, const sp_point_384* g, #ifndef FP_ECC return sp_384_ecc_mulmod_win_add_sub_15(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 15 * 7]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 15 * 7); sp_cache_384_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 15 * 7, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 15 * 7, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -30722,9 +28628,7 @@ static int sp_384_ecc_mulmod_15(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -30743,28 +28647,12 @@ static int sp_384_ecc_mulmod_15(sp_point_384* r, const sp_point_384* g, int sp_ecc_mulmod_384(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[1]; - sp_digit k[15]; -#endif + SP_DECL_VAR(sp_point_384, point, 1); + SP_DECL_VAR(sp_digit, k, 15); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 15, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 15, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(k, 15, km); sp_384_point_from_ecc_point_15(point, gm); @@ -30775,10 +28663,8 @@ int sp_ecc_mulmod_384(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_384_point_to_ecc_point_15(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -30798,31 +28684,14 @@ int sp_ecc_mulmod_384(const mp_int* km, const ecc_point* gm, ecc_point* r, int sp_ecc_mulmod_add_384(const mp_int* km, const ecc_point* gm, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[2]; - sp_digit k[15 + 15 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_384, point, 2); + SP_DECL_VAR(sp_digit, k, 15 + 15 * 2 * 6); sp_point_384* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (15 + 15 * 2 * 6), heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 15 + 15 * 2 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 15; @@ -30853,10 +28722,8 @@ int sp_ecc_mulmod_add_384(const mp_int* km, const ecc_point* gm, err = sp_384_point_to_ecc_point_15(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -32721,28 +30588,12 @@ static int sp_384_ecc_mulmod_base_15(sp_point_384* r, const sp_digit* k, */ int sp_ecc_mulmod_base_384(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[1]; - sp_digit k[15]; -#endif + SP_DECL_VAR(sp_point_384, point, 1); + SP_DECL_VAR(sp_digit, k, 15); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 15, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 15, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(k, 15, km); @@ -32752,10 +30603,8 @@ int sp_ecc_mulmod_base_384(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_384_point_to_ecc_point_15(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -32774,31 +30623,14 @@ int sp_ecc_mulmod_base_384(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_384(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[2]; - sp_digit k[15 + 15 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_384, point, 2); + SP_DECL_VAR(sp_digit, k, 15 + 15 * 2 * 6); sp_point_384* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (15 + 15 * 2 * 6), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 15 + 15 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 15; @@ -32828,10 +30660,8 @@ int sp_ecc_mulmod_base_add_384(const mp_int* km, const ecc_point* am, err = sp_384_point_to_ecc_point_15(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -32931,17 +30761,12 @@ static int sp_384_ecc_gen_k_15(WC_RNG* rng, sp_digit* k) */ int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_DECL_VAR(sp_point_384, point, 2); #else - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - sp_point_384 point[2]; - #else - sp_point_384 point[1]; - #endif - sp_digit k[15]; + SP_DECL_VAR(sp_point_384, point, 1); #endif + SP_DECL_VAR(sp_digit, k, 15); #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN sp_point_384* infinity = NULL; #endif @@ -32950,22 +30775,12 @@ int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, DYNAMIC_TYPE_ECC); - #else - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, DYNAMIC_TYPE_ECC); - #endif - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 15, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_ALLOC_VAR(sp_point_384, point, 2, heap, DYNAMIC_TYPE_ECC); +#else + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); #endif - + SP_ALLOC_VAR(sp_digit, k, 15, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN infinity = point + 1; @@ -32995,11 +30810,9 @@ int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) err = sp_384_point_to_ecc_point_15(point, pub); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); /* point is not sensitive, so no need to zeroize */ - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -33142,34 +30955,16 @@ static void sp_384_to_bin_15(sp_digit* r, byte* a) int sp_ecc_secret_gen_384(const mp_int* priv, const ecc_point* pub, byte* out, word32* outLen, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[1]; - sp_digit k[15]; -#endif + SP_DECL_VAR(sp_point_384, point, 1); + SP_DECL_VAR(sp_digit, k, 15); int err = MP_OKAY; if (*outLen < 48U) { err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 15, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 15, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(k, 15, priv); sp_384_point_from_ecc_point_15(point, pub); @@ -33180,10 +30975,8 @@ int sp_ecc_secret_gen_384(const mp_int* priv, const ecc_point* pub, byte* out, *outLen = 48; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -33422,29 +31215,17 @@ SP_NOINLINE static void sp_384_lshift_30(sp_digit* r, const sp_digit* a, static int sp_384_div_15(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { + SP_DECL_VAR(sp_digit, t1, 4 * 15 + 3); int i; sp_digit r1; sp_digit mask; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 15 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 15 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 15 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 30 + 1; sd = t2 + 15 + 1; @@ -33468,9 +31249,7 @@ static int sp_384_div_15(const sp_digit* a, const sp_digit* d, sp_384_rshift_15(r, t1, 6); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -33734,13 +31513,8 @@ static int sp_384_calc_s_15(sp_digit* s, const sp_digit* r, sp_digit* k, int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng, const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* e = NULL; - sp_point_384* point = NULL; -#else - sp_digit e[7 * 2 * 15]; - sp_point_384 point[1]; -#endif + SP_DECL_VAR(sp_digit, e, 7 * 2 * 15); + SP_DECL_VAR(sp_point_384, point, 1); sp_digit* x = NULL; sp_digit* k = NULL; sp_digit* r = NULL; @@ -33752,21 +31526,8 @@ int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - e = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7 * 2 * 15, heap, - DYNAMIC_TYPE_ECC); - if (e == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, e, 7 * 2 * 15, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { x = e + 2 * 15; k = e + 4 * 15; @@ -33831,24 +31592,8 @@ int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng, err = sp_384_to_mp(s, sm); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (e != NULL) -#endif - { - ForceZero(e, sizeof(sp_digit) * 7 * 2 * 15); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(e, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (point != NULL) -#endif - { - ForceZero(point, sizeof(sp_point_384)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(point, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_digit, e, 7 * 2 * 15, heap, DYNAMIC_TYPE_ECC); return err; } @@ -34061,24 +31806,14 @@ static int sp_384_num_bits_15(const sp_digit* a) static int sp_384_mod_inv_15(sp_digit* r, const sp_digit* a, const sp_digit* m) { int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u = NULL; -#else - sp_digit u[15 * 4]; -#endif + SP_DECL_VAR(sp_digit, u, 15 * 4); sp_digit* v = NULL; sp_digit* b = NULL; sp_digit* d = NULL; int ut; int vt; -#ifdef WOLFSSL_SP_SMALL_STACK - u = (sp_digit*)XMALLOC(sizeof(sp_digit) * 15 * 4, NULL, - DYNAMIC_TYPE_ECC); - if (u == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, u, 15 * 4, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { v = u + 15; b = u + 2 * 15; @@ -34160,9 +31895,7 @@ static int sp_384_mod_inv_15(sp_digit* r, const sp_digit* a, const sp_digit* m) else XMEMCPY(r, d, sizeof(sp_digit) * 15); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(u, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -34288,13 +32021,8 @@ int sp_ecc_verify_384(const byte* hash, word32 hashLen, const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* rm, const mp_int* sm, int* res, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u1 = NULL; - sp_point_384* p1 = NULL; -#else - sp_digit u1[18 * 15]; - sp_point_384 p1[2]; -#endif + SP_DECL_VAR(sp_digit, u1, 18 * 15); + SP_DECL_VAR(sp_point_384, p1, 2); sp_digit* u2 = NULL; sp_digit* s = NULL; sp_digit* tmp = NULL; @@ -34303,21 +32031,8 @@ int sp_ecc_verify_384(const byte* hash, word32 hashLen, const mp_int* pX, sp_int32 c = 0; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p1 = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (p1 == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - u1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 15, heap, - DYNAMIC_TYPE_ECC); - if (u1 == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, u1, 18 * 15, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p1, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { u2 = u1 + 2 * 15; s = u1 + 4 * 15; @@ -34374,11 +32089,8 @@ int sp_ecc_verify_384(const byte* hash, word32 hashLen, const mp_int* pX, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u1, heap, DYNAMIC_TYPE_ECC); - XFREE(p1, heap, DYNAMIC_TYPE_ECC); -#endif - + SP_FREE_VAR(p1, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(u1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -34544,21 +32256,13 @@ int sp_ecc_verify_384_nb(sp_ecc_ctx_t* sp_ctx, const byte* hash, static int sp_384_ecc_is_point_15(const sp_point_384* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[15 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 15 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 15 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 15 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 15; @@ -34582,9 +32286,7 @@ static int sp_384_ecc_is_point_15(const sp_point_384* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -34598,21 +32300,11 @@ static int sp_384_ecc_is_point_15(const sp_point_384* point, */ int sp_ecc_is_point_384(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* pub = NULL; -#else - sp_point_384 pub[1]; -#endif + SP_DECL_VAR(sp_point_384, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_384*)XMALLOC(sizeof(sp_point_384), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_384, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(pub->x, 15, pX); sp_384_from_mp(pub->y, 15, pY); @@ -34621,9 +32313,7 @@ int sp_ecc_is_point_384(const mp_int* pX, const mp_int* pY) err = sp_384_ecc_is_point_15(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -34642,13 +32332,8 @@ int sp_ecc_is_point_384(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_384(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_384* pub = NULL; -#else - sp_digit priv[15]; - sp_point_384 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 15); + SP_DECL_VAR(sp_point_384, pub, 2); sp_point_384* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -34663,21 +32348,8 @@ int sp_ecc_check_key_384(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 15, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 15, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -34729,10 +32401,8 @@ int sp_ecc_check_key_384(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } @@ -34756,32 +32426,13 @@ int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_384* p = NULL; -#else - sp_digit tmp[2 * 15 * 6]; - sp_point_384 p[2]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 15 * 6); + SP_DECL_VAR(sp_point_384, p, 2); sp_point_384* q = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 15 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 15 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p, 2, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { q = p + 1; @@ -34809,10 +32460,8 @@ int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_384_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -34831,30 +32480,12 @@ int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_384* p = NULL; -#else - sp_digit tmp[2 * 15 * 2]; - sp_point_384 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 15 * 2); + SP_DECL_VAR(sp_point_384, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 15 * 2, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 15 * 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(p->x, 15, pX); sp_384_from_mp(p->y, 15, pY); @@ -34875,10 +32506,8 @@ int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_384_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -34893,30 +32522,13 @@ int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, */ int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_384* p = NULL; -#else - sp_digit tmp[2 * 15 * 6]; - sp_point_384 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 15 * 6); + SP_DECL_VAR(sp_point_384, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 15 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 15 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(p->x, 15, pX); sp_384_from_mp(p->y, 15, pY); @@ -34937,10 +32549,8 @@ int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ) err = sp_384_to_mp(p->z, pZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -34953,23 +32563,14 @@ int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ) */ static int sp_384_mont_sqrt_15(sp_digit* y) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[5 * 2 * 15]; -#endif + SP_DECL_VAR(sp_digit, t1, 5 * 2 * 15); sp_digit* t2 = NULL; sp_digit* t3 = NULL; sp_digit* t4 = NULL; sp_digit* t5 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 5 * 2 * 15, NULL, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, t1, 5 * 2 * 15, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 15; t3 = t1 + 4 * 15; @@ -35034,9 +32635,7 @@ static int sp_384_mont_sqrt_15(sp_digit* y) } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -35051,20 +32650,11 @@ static int sp_384_mont_sqrt_15(sp_digit* y) */ int sp_ecc_uncompress_384(mp_int* xm, int odd, mp_int* ym) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* x = NULL; -#else - sp_digit x[4 * 15]; -#endif + SP_DECL_VAR(sp_digit, x, 4 * 15); sp_digit* y = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - x = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 15, NULL, DYNAMIC_TYPE_ECC); - if (x == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, x, 4 * 15, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { y = x + 2 * 15; @@ -35099,9 +32689,7 @@ int sp_ecc_uncompress_384(mp_int* xm, int odd, mp_int* ym) err = sp_384_to_mp(y, ym); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(x, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(x, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -36849,13 +34437,8 @@ static int sp_521_mod_mul_norm_21(sp_digit* r, const sp_digit* a, const sp_digit static int sp_521_ecc_mulmod_21(sp_point_521* r, const sp_point_521* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_521 t[3]; - sp_digit tmp[2 * 21 * 6]; -#endif + SP_DECL_VAR(sp_point_521, t, 3); + SP_DECL_VAR(sp_digit, tmp, 2 * 21 * 6); sp_digit n; int i; int c; @@ -36866,18 +34449,8 @@ static int sp_521_ecc_mulmod_21(sp_point_521* r, const sp_point_521* g, (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 21 * 6, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_point_521, t, 3, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 21 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { XMEMSET(t, 0, sizeof(sp_point_521) * 3); @@ -36927,24 +34500,8 @@ static int sp_521_ecc_mulmod_21(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 21 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (t != NULL) -#endif - { - ForceZero(t, sizeof(sp_point_521) * 3); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 21 * 6, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_point_521, t, 3, heap, DYNAMIC_TYPE_ECC); return err; } @@ -37614,13 +35171,8 @@ static void sp_521_get_point_33_21(sp_point_521* r, const sp_point_521* table, static int sp_521_ecc_mulmod_win_add_sub_21(sp_point_521* r, const sp_point_521* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_521 t[33+2]; - sp_digit tmp[2 * 21 * 6]; -#endif + SP_DECL_VAR(sp_point_521, t, 33+2); + SP_DECL_VAR(sp_digit, tmp, 2 * 21 * 6); sp_point_521* rt = NULL; sp_point_521* p = NULL; sp_digit* negy; @@ -37632,19 +35184,8 @@ static int sp_521_ecc_mulmod_win_add_sub_21(sp_point_521* r, const sp_point_521* (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * - (33+2), heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 21 * 6, - heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, t, 33+2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 21 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 33; p = t + 33+1; @@ -37728,10 +35269,8 @@ static int sp_521_ecc_mulmod_win_add_sub_21(sp_point_521* r, const sp_point_521* } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; } @@ -37856,11 +35395,7 @@ static void sp_521_proj_to_affine_21(sp_point_521* a, sp_digit* t) static int sp_521_gen_stripe_table_21(const sp_point_521* a, sp_table_entry_521* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* t = NULL; -#else - sp_point_521 t[3]; -#endif + SP_DECL_VAR(sp_point_521, t, 3); sp_point_521* s1 = NULL; sp_point_521* s2 = NULL; int i; @@ -37869,13 +35404,7 @@ static int sp_521_gen_stripe_table_21(const sp_point_521* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_521, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -37924,9 +35453,7 @@ static int sp_521_gen_stripe_table_21(const sp_point_521* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -38054,13 +35581,8 @@ static int sp_521_ecc_mulmod_stripe_21(sp_point_521* r, const sp_point_521* g, const sp_table_entry_521* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_521 rt[2]; - sp_digit t[2 * 21 * 6]; -#endif + SP_DECL_VAR(sp_point_521, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 21 * 6); sp_point_521* p = NULL; int i; int j; @@ -38074,19 +35596,8 @@ static int sp_521_ecc_mulmod_stripe_21(sp_point_521* r, const sp_point_521* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 21 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 21 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -38140,10 +35651,8 @@ static int sp_521_ecc_mulmod_stripe_21(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -38260,20 +35769,11 @@ static int sp_521_ecc_mulmod_21(sp_point_521* r, const sp_point_521* g, #ifndef FP_ECC return sp_521_ecc_mulmod_win_add_sub_21(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 21 * 6]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 21 * 6); sp_cache_521_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 21 * 6, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 21 * 6, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -38306,9 +35806,7 @@ static int sp_521_ecc_mulmod_21(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -38327,28 +35825,12 @@ static int sp_521_ecc_mulmod_21(sp_point_521* r, const sp_point_521* g, int sp_ecc_mulmod_521(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[1]; - sp_digit k[21]; -#endif + SP_DECL_VAR(sp_point_521, point, 1); + SP_DECL_VAR(sp_digit, k, 21); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 21, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 21, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(k, 21, km); sp_521_point_from_ecc_point_21(point, gm); @@ -38359,10 +35841,8 @@ int sp_ecc_mulmod_521(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_521_point_to_ecc_point_21(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -38382,31 +35862,14 @@ int sp_ecc_mulmod_521(const mp_int* km, const ecc_point* gm, ecc_point* r, int sp_ecc_mulmod_add_521(const mp_int* km, const ecc_point* gm, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[2]; - sp_digit k[21 + 21 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_521, point, 2); + SP_DECL_VAR(sp_digit, k, 21 + 21 * 2 * 6); sp_point_521* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (21 + 21 * 2 * 6), heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 21 + 21 * 2 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 21; @@ -38437,10 +35900,8 @@ int sp_ecc_mulmod_add_521(const mp_int* km, const ecc_point* gm, err = sp_521_point_to_ecc_point_21(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -40815,28 +38276,12 @@ static int sp_521_ecc_mulmod_base_21(sp_point_521* r, const sp_digit* k, */ int sp_ecc_mulmod_base_521(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[1]; - sp_digit k[21]; -#endif + SP_DECL_VAR(sp_point_521, point, 1); + SP_DECL_VAR(sp_digit, k, 21); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 21, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 21, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(k, 21, km); @@ -40846,10 +38291,8 @@ int sp_ecc_mulmod_base_521(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_521_point_to_ecc_point_21(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -40868,31 +38311,14 @@ int sp_ecc_mulmod_base_521(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_521(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[2]; - sp_digit k[21 + 21 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_521, point, 2); + SP_DECL_VAR(sp_digit, k, 21 + 21 * 2 * 6); sp_point_521* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (21 + 21 * 2 * 6), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 21 + 21 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 21; @@ -40922,10 +38348,8 @@ int sp_ecc_mulmod_base_add_521(const mp_int* km, const ecc_point* am, err = sp_521_point_to_ecc_point_21(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -41026,17 +38450,12 @@ static int sp_521_ecc_gen_k_21(WC_RNG* rng, sp_digit* k) */ int sp_ecc_make_key_521(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_DECL_VAR(sp_point_521, point, 2); #else - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - sp_point_521 point[2]; - #else - sp_point_521 point[1]; - #endif - sp_digit k[21]; + SP_DECL_VAR(sp_point_521, point, 1); #endif + SP_DECL_VAR(sp_digit, k, 21); #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN sp_point_521* infinity = NULL; #endif @@ -41045,22 +38464,12 @@ int sp_ecc_make_key_521(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, DYNAMIC_TYPE_ECC); - #else - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, DYNAMIC_TYPE_ECC); - #endif - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 21, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_ALLOC_VAR(sp_point_521, point, 2, heap, DYNAMIC_TYPE_ECC); +#else + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); #endif - + SP_ALLOC_VAR(sp_digit, k, 21, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN infinity = point + 1; @@ -41090,11 +38499,9 @@ int sp_ecc_make_key_521(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) err = sp_521_point_to_ecc_point_21(point, pub); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); /* point is not sensitive, so no need to zeroize */ - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -41237,34 +38644,16 @@ static void sp_521_to_bin_21(sp_digit* r, byte* a) int sp_ecc_secret_gen_521(const mp_int* priv, const ecc_point* pub, byte* out, word32* outLen, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[1]; - sp_digit k[21]; -#endif + SP_DECL_VAR(sp_point_521, point, 1); + SP_DECL_VAR(sp_digit, k, 21); int err = MP_OKAY; if (*outLen < 65U) { err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 21, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 21, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(k, 21, priv); sp_521_point_from_ecc_point_21(point, pub); @@ -41275,10 +38664,8 @@ int sp_ecc_secret_gen_521(const mp_int* priv, const ecc_point* pub, byte* out, *outLen = 66; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -41537,29 +38924,17 @@ SP_NOINLINE static void sp_521_lshift_42(sp_digit* r, const sp_digit* a, static int sp_521_div_21(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { + SP_DECL_VAR(sp_digit, t1, 4 * 21 + 3); int i; sp_digit r1; sp_digit mask; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 21 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 21 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 21 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 42 + 1; sd = t2 + 21 + 1; @@ -41583,9 +38958,7 @@ static int sp_521_div_21(const sp_digit* a, const sp_digit* d, sp_521_rshift_21(r, t1, 4); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -41864,13 +39237,8 @@ static int sp_521_calc_s_21(sp_digit* s, const sp_digit* r, sp_digit* k, int sp_ecc_sign_521(const byte* hash, word32 hashLen, WC_RNG* rng, const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* e = NULL; - sp_point_521* point = NULL; -#else - sp_digit e[7 * 2 * 21]; - sp_point_521 point[1]; -#endif + SP_DECL_VAR(sp_digit, e, 7 * 2 * 21); + SP_DECL_VAR(sp_point_521, point, 1); sp_digit* x = NULL; sp_digit* k = NULL; sp_digit* r = NULL; @@ -41882,21 +39250,8 @@ int sp_ecc_sign_521(const byte* hash, word32 hashLen, WC_RNG* rng, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - e = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7 * 2 * 21, heap, - DYNAMIC_TYPE_ECC); - if (e == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, e, 7 * 2 * 21, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { x = e + 2 * 21; k = e + 4 * 21; @@ -41967,24 +39322,8 @@ int sp_ecc_sign_521(const byte* hash, word32 hashLen, WC_RNG* rng, err = sp_521_to_mp(s, sm); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (e != NULL) -#endif - { - ForceZero(e, sizeof(sp_digit) * 7 * 2 * 21); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(e, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (point != NULL) -#endif - { - ForceZero(point, sizeof(sp_point_521)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(point, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_digit, e, 7 * 2 * 21, heap, DYNAMIC_TYPE_ECC); return err; } @@ -42201,24 +39540,14 @@ static int sp_521_num_bits_21(const sp_digit* a) static int sp_521_mod_inv_21(sp_digit* r, const sp_digit* a, const sp_digit* m) { int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u = NULL; -#else - sp_digit u[21 * 4]; -#endif + SP_DECL_VAR(sp_digit, u, 21 * 4); sp_digit* v = NULL; sp_digit* b = NULL; sp_digit* d = NULL; int ut; int vt; -#ifdef WOLFSSL_SP_SMALL_STACK - u = (sp_digit*)XMALLOC(sizeof(sp_digit) * 21 * 4, NULL, - DYNAMIC_TYPE_ECC); - if (u == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, u, 21 * 4, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { v = u + 21; b = u + 2 * 21; @@ -42300,9 +39629,7 @@ static int sp_521_mod_inv_21(sp_digit* r, const sp_digit* a, const sp_digit* m) else XMEMCPY(r, d, sizeof(sp_digit) * 21); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(u, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -42434,13 +39761,8 @@ int sp_ecc_verify_521(const byte* hash, word32 hashLen, const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* rm, const mp_int* sm, int* res, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u1 = NULL; - sp_point_521* p1 = NULL; -#else - sp_digit u1[18 * 21]; - sp_point_521 p1[2]; -#endif + SP_DECL_VAR(sp_digit, u1, 18 * 21); + SP_DECL_VAR(sp_point_521, p1, 2); sp_digit* u2 = NULL; sp_digit* s = NULL; sp_digit* tmp = NULL; @@ -42449,21 +39771,8 @@ int sp_ecc_verify_521(const byte* hash, word32 hashLen, const mp_int* pX, sp_int32 c = 0; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p1 = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (p1 == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - u1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 21, heap, - DYNAMIC_TYPE_ECC); - if (u1 == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, u1, 18 * 21, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p1, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { u2 = u1 + 2 * 21; s = u1 + 4 * 21; @@ -42525,11 +39834,8 @@ int sp_ecc_verify_521(const byte* hash, word32 hashLen, const mp_int* pX, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u1, heap, DYNAMIC_TYPE_ECC); - XFREE(p1, heap, DYNAMIC_TYPE_ECC); -#endif - + SP_FREE_VAR(p1, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(u1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -42699,21 +40005,13 @@ int sp_ecc_verify_521_nb(sp_ecc_ctx_t* sp_ctx, const byte* hash, static int sp_521_ecc_is_point_21(const sp_point_521* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[21 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 21 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 21 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 21 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 21; @@ -42737,9 +40035,7 @@ static int sp_521_ecc_is_point_21(const sp_point_521* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -42753,21 +40049,11 @@ static int sp_521_ecc_is_point_21(const sp_point_521* point, */ int sp_ecc_is_point_521(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* pub = NULL; -#else - sp_point_521 pub[1]; -#endif + SP_DECL_VAR(sp_point_521, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_521*)XMALLOC(sizeof(sp_point_521), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_521, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(pub->x, 21, pX); sp_521_from_mp(pub->y, 21, pY); @@ -42776,9 +40062,7 @@ int sp_ecc_is_point_521(const mp_int* pX, const mp_int* pY) err = sp_521_ecc_is_point_21(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -42797,13 +40081,8 @@ int sp_ecc_is_point_521(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_521(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_521* pub = NULL; -#else - sp_digit priv[21]; - sp_point_521 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 21); + SP_DECL_VAR(sp_point_521, pub, 2); sp_point_521* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -42818,21 +40097,8 @@ int sp_ecc_check_key_521(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 21, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 21, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -42884,10 +40150,8 @@ int sp_ecc_check_key_521(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } @@ -42911,32 +40175,13 @@ int sp_ecc_proj_add_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_521* p = NULL; -#else - sp_digit tmp[2 * 21 * 6]; - sp_point_521 p[2]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 21 * 6); + SP_DECL_VAR(sp_point_521, p, 2); sp_point_521* q = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 21 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 21 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p, 2, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { q = p + 1; @@ -42964,10 +40209,8 @@ int sp_ecc_proj_add_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_521_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -42986,30 +40229,12 @@ int sp_ecc_proj_add_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, int sp_ecc_proj_dbl_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_521* p = NULL; -#else - sp_digit tmp[2 * 21 * 2]; - sp_point_521 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 21 * 2); + SP_DECL_VAR(sp_point_521, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 21 * 2, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 21 * 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(p->x, 21, pX); sp_521_from_mp(p->y, 21, pY); @@ -43030,10 +40255,8 @@ int sp_ecc_proj_dbl_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_521_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -43048,30 +40271,13 @@ int sp_ecc_proj_dbl_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, */ int sp_ecc_map_521(mp_int* pX, mp_int* pY, mp_int* pZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_521* p = NULL; -#else - sp_digit tmp[2 * 21 * 5]; - sp_point_521 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 21 * 5); + SP_DECL_VAR(sp_point_521, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 21 * 5, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 21 * 5, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(p->x, 21, pX); sp_521_from_mp(p->y, 21, pY); @@ -43092,10 +40298,8 @@ int sp_ecc_map_521(mp_int* pX, mp_int* pY, mp_int* pZ) err = sp_521_to_mp(p->z, pZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -43115,19 +40319,10 @@ static const word32 p521_sqrt_power[17] = { */ static int sp_521_mont_sqrt_21(sp_digit* y) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t = NULL; -#else - sp_digit t[2 * 21]; -#endif + SP_DECL_VAR(sp_digit, t, 2 * 21); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 21, NULL, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, t, 2 * 21, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { { @@ -43143,9 +40338,7 @@ static int sp_521_mont_sqrt_21(sp_digit* y) } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -43160,20 +40353,11 @@ static int sp_521_mont_sqrt_21(sp_digit* y) */ int sp_ecc_uncompress_521(mp_int* xm, int odd, mp_int* ym) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* x = NULL; -#else - sp_digit x[4 * 21]; -#endif + SP_DECL_VAR(sp_digit, x, 4 * 21); sp_digit* y = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - x = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 21, NULL, DYNAMIC_TYPE_ECC); - if (x == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, x, 4 * 21, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { y = x + 2 * 21; @@ -43208,9 +40392,7 @@ int sp_ecc_uncompress_521(mp_int* xm, int odd, mp_int* ym) err = sp_521_to_mp(y, ym); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(x, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(x, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -44184,30 +41366,16 @@ static int sp_1024_div_42(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_32 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 42 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 42 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 42 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 42 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 84 + 1; sd = t2 + 42 + 1; @@ -44250,9 +41418,7 @@ static int sp_1024_div_42(const sp_digit* a, const sp_digit* d, r[41] = 0; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -45618,13 +42784,8 @@ static int sp_1024_proj_point_add_42_nb(sp_ecc_ctx_t* sp_ctx, sp_point_1024* r, static int sp_1024_ecc_mulmod_42(sp_point_1024* r, const sp_point_1024* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_1024 t[3]; - sp_digit tmp[2 * 42 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, t, 3); + SP_DECL_VAR(sp_digit, tmp, 2 * 42 * 37); sp_digit n; int i; int c; @@ -45635,18 +42796,8 @@ static int sp_1024_ecc_mulmod_42(sp_point_1024* r, const sp_point_1024* g, (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 42 * 37, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_point_1024, t, 3, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 42 * 37, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { XMEMSET(t, 0, sizeof(sp_point_1024) * 3); @@ -45696,24 +42847,8 @@ static int sp_1024_ecc_mulmod_42(sp_point_1024* r, const sp_point_1024* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 42 * 37); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (t != NULL) -#endif - { - ForceZero(t, sizeof(sp_point_1024) * 3); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 42 * 37, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_point_1024, t, 3, heap, DYNAMIC_TYPE_ECC); return err; } @@ -46289,13 +43424,8 @@ static void sp_1024_ecc_recode_7_42(const sp_digit* k, ecc_recode_1024* v) static int sp_1024_ecc_mulmod_win_add_sub_42(sp_point_1024* r, const sp_point_1024* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_1024 t[65+2]; - sp_digit tmp[2 * 42 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, t, 65+2); + SP_DECL_VAR(sp_digit, tmp, 2 * 42 * 37); sp_point_1024* rt = NULL; sp_point_1024* p = NULL; sp_digit* negy; @@ -46307,19 +43437,8 @@ static int sp_1024_ecc_mulmod_win_add_sub_42(sp_point_1024* r, const sp_point_10 (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * - (65+2), heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 42 * 37, - heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, t, 65+2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 42 * 37, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 65; p = t + 65+1; @@ -46407,10 +43526,8 @@ static int sp_1024_ecc_mulmod_win_add_sub_42(sp_point_1024* r, const sp_point_10 } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; } @@ -46535,11 +43652,7 @@ static void sp_1024_proj_to_affine_42(sp_point_1024* a, sp_digit* t) static int sp_1024_gen_stripe_table_42(const sp_point_1024* a, sp_table_entry_1024* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* t = NULL; -#else - sp_point_1024 t[3]; -#endif + SP_DECL_VAR(sp_point_1024, t, 3); sp_point_1024* s1 = NULL; sp_point_1024* s2 = NULL; int i; @@ -46548,13 +43661,7 @@ static int sp_1024_gen_stripe_table_42(const sp_point_1024* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_1024, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -46603,9 +43710,7 @@ static int sp_1024_gen_stripe_table_42(const sp_point_1024* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -46631,13 +43736,8 @@ static int sp_1024_ecc_mulmod_stripe_42(sp_point_1024* r, const sp_point_1024* g const sp_table_entry_1024* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_1024 rt[2]; - sp_digit t[2 * 42 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 42 * 37); sp_point_1024* p = NULL; int i; int j; @@ -46651,19 +43751,8 @@ static int sp_1024_ecc_mulmod_stripe_42(sp_point_1024* r, const sp_point_1024* g (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 42 * 37, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 42 * 37, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -46702,10 +43791,8 @@ static int sp_1024_ecc_mulmod_stripe_42(sp_point_1024* r, const sp_point_1024* g } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -46822,20 +43909,11 @@ static int sp_1024_ecc_mulmod_42(sp_point_1024* r, const sp_point_1024* g, #ifndef FP_ECC return sp_1024_ecc_mulmod_win_add_sub_42(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 42 * 38]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 42 * 38); sp_cache_1024_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 42 * 38, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 42 * 38, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -46868,9 +43946,7 @@ static int sp_1024_ecc_mulmod_42(sp_point_1024* r, const sp_point_1024* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -46889,28 +43965,12 @@ static int sp_1024_ecc_mulmod_42(sp_point_1024* r, const sp_point_1024* g, int sp_ecc_mulmod_1024(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[1]; - sp_digit k[42]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, k, 42); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 42, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 42, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(k, 42, km); sp_1024_point_from_ecc_point_42(point, gm); @@ -46921,10 +43981,8 @@ int sp_ecc_mulmod_1024(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_1024_point_to_ecc_point_42(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -50833,28 +47891,12 @@ static int sp_1024_ecc_mulmod_base_42(sp_point_1024* r, const sp_digit* k, */ int sp_ecc_mulmod_base_1024(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[1]; - sp_digit k[42]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, k, 42); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 42, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 42, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(k, 42, km); @@ -50864,10 +47906,8 @@ int sp_ecc_mulmod_base_1024(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_1024_point_to_ecc_point_42(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -50886,31 +47926,14 @@ int sp_ecc_mulmod_base_1024(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[2]; - sp_digit k[42 + 42 * 2 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, point, 2); + SP_DECL_VAR(sp_digit, k, 42 + 42 * 2 * 37); sp_point_1024* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (42 + 42 * 2 * 37), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 42 + 42 * 2 * 37, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 42; @@ -50940,10 +47963,8 @@ int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am, err = sp_1024_point_to_ecc_point_42(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -50961,13 +47982,8 @@ int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am, int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* t = NULL; -#else - sp_point_1024 point[1]; - sp_digit t[38 * 2 * 42]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, t, 38 * 2 * 42); int err = MP_OKAY; if ((gm == NULL) || (len == NULL)) { @@ -50982,21 +47998,8 @@ int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 38 * 2 * 42, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 38 * 2 * 42, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_point_from_ecc_point_42(point, gm); err = sp_1024_gen_stripe_table_42(point, @@ -51006,10 +48009,8 @@ int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, *len = sizeof(sp_table_entry_1024) * 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -51062,28 +48063,12 @@ int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, int sp_ecc_mulmod_table_1024(const mp_int* km, const ecc_point* gm, byte* table, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[1]; - sp_digit k[42]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, k, 42); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) { - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 42, heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 42, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(k, 42, km); sp_1024_point_from_ecc_point_42(point, gm); @@ -51100,10 +48085,8 @@ int sp_ecc_mulmod_table_1024(const mp_int* km, const ecc_point* gm, byte* table, err = sp_1024_point_to_ecc_point_42(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); return err; } @@ -54552,22 +51535,14 @@ static void sp_1024_from_bin(sp_digit* r, int size, const byte* a, int n) static int sp_1024_ecc_is_point_42(const sp_point_1024* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[42 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 42 * 4); sp_digit* t2 = NULL; sp_int32 n; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 42 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 42 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 42; @@ -54594,9 +51569,7 @@ static int sp_1024_ecc_is_point_42(const sp_point_1024* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -54610,21 +51583,11 @@ static int sp_1024_ecc_is_point_42(const sp_point_1024* point, */ int sp_ecc_is_point_1024(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* pub = NULL; -#else - sp_point_1024 pub[1]; -#endif + SP_DECL_VAR(sp_point_1024, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_1024, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(pub->x, 42, pX); sp_1024_from_mp(pub->y, 42, pY); @@ -54633,9 +51596,7 @@ int sp_ecc_is_point_1024(const mp_int* pX, const mp_int* pY) err = sp_1024_ecc_is_point_42(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -54654,13 +51615,8 @@ int sp_ecc_is_point_1024(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_1024(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_1024* pub = NULL; -#else - sp_digit priv[42]; - sp_point_1024 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 42); + SP_DECL_VAR(sp_point_1024, pub, 2); sp_point_1024* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -54675,21 +51631,8 @@ int sp_ecc_check_key_1024(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 42, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 42, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_1024, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -54741,10 +51684,8 @@ int sp_ecc_check_key_1024(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } diff --git a/wolfcrypt/src/sp_c64.c b/wolfcrypt/src/sp_c64.c index 56d80838c..21473732a 100644 --- a/wolfcrypt/src/sp_c64.c +++ b/wolfcrypt/src/sp_c64.c @@ -61,6 +61,57 @@ #define __volatile__ volatile #endif +#ifdef WOLFSSL_SP_SMALL_STACK + #define SP_DECL_VAR(TYPE, NAME, CNT) \ + TYPE* NAME = NULL + #define SP_ALLOC_VAR(TYPE, NAME, CNT, HEAP, DT) \ + if (err == MP_OKAY) { \ + (NAME) = (TYPE*)XMALLOC(sizeof(TYPE) * (CNT), (HEAP), DT); \ + if ((NAME) == NULL) { \ + err = MEMORY_E; \ + } \ + } + + #define SP_VAR_OK(NAME) ((NAME) != NULL) + + #define SP_FREE_VAR(NAME, HEAP, DT) \ + XFREE(NAME, (HEAP), DT) + #define SP_ZEROFREE_VAR(TYPE, NAME, CNT, HEAP, DT) \ + do { \ + if ((NAME) != NULL) { \ + ForceZero(NAME, sizeof(TYPE) * (CNT)); \ + } \ + SP_FREE_VAR(NAME, HEAP, DT); \ + } while (0) + #define SP_ZEROFREE_VAR_ALT(TYPE, NAME, FZ_NAME, CNT, HEAP, DT) \ + do { \ + if ((FZ_NAME) != NULL) { \ + ForceZero(FZ_NAME, sizeof(TYPE) * (CNT)); \ + } \ + SP_FREE_VAR(NAME, HEAP, DT); \ + } while (0) +#else + #define SP_DECL_VAR(TYPE, NAME, CNT) \ + TYPE NAME[CNT] + #define SP_ALLOC_VAR(TYPE, NAME, CNT, HEAP, DT) \ + WC_DO_NOTHING + #define SP_VAR_OK(NAME) (1) + #define SP_FREE_VAR(NAME, HEAP, DT) \ + WC_DO_NOTHING + #define SP_ZEROFREE_VAR(TYPE, NAME, CNT, HEAP, DT) \ + do { \ + if ((NAME) != NULL) { \ + ForceZero(NAME, sizeof(TYPE) * (CNT)); \ + } \ + } while (0) + #define SP_ZEROFREE_VAR_ALT(TYPE, NAME, FZ_NAME, CNT, HEAP, DT) \ + do { \ + if ((FZ_NAME) != NULL) { \ + ForceZero(FZ_NAME, sizeof(TYPE) * (CNT)); \ + } \ + } while (0) +#endif + #ifndef WOLFSSL_SP_ASM #if SP_WORD_SIZE == 64 #define SP_PRINT_NUM(var, name, total, words, bits) \ @@ -945,30 +996,16 @@ static int sp_2048_div_17(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_64 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 17 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 17 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 17 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 17 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 34 + 1; sd = t2 + 17 + 1; @@ -1011,9 +1048,7 @@ static int sp_2048_div_17(const sp_digit* a, const sp_digit* d, sp_2048_rshift_17(r, r, 13); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -1045,11 +1080,7 @@ static int sp_2048_mod_exp_17(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 34]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 34); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -1063,15 +1094,7 @@ static int sp_2048_mod_exp_17(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 17 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 34, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -1129,17 +1152,11 @@ static int sp_2048_mod_exp_17(sp_digit* r, const sp_digit* a, const sp_digit* e, } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #elif !defined(WC_NO_CACHE_RESISTANT) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 34]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 34); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -1153,15 +1170,7 @@ static int sp_2048_mod_exp_17(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 17 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 34, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -1218,17 +1227,11 @@ static int sp_2048_mod_exp_17(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, t[0], sizeof(*r) * 17 * 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(32 * 34) + 34]; -#endif + SP_DECL_VAR(sp_digit, td, (32 * 34) + 34); sp_digit* t[32]; sp_digit* rt = NULL; sp_digit* norm = NULL; @@ -1243,15 +1246,8 @@ static int sp_2048_mod_exp_17(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 34) + 34), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (32 * 34) + 34, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) @@ -1363,9 +1359,7 @@ static int sp_2048_mod_exp_17(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, rt, sizeof(sp_digit) * 34); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #endif @@ -1766,30 +1760,16 @@ static int sp_2048_div_34(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_64 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 34 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 34 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 34 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 34 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 68 + 1; sd = t2 + 34 + 1; @@ -1832,9 +1812,7 @@ static int sp_2048_div_34(const sp_digit* a, const sp_digit* d, sp_2048_rshift_34(r, r, 26); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -1867,11 +1845,7 @@ static int sp_2048_mod_exp_34(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 68]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 68); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -1885,15 +1859,7 @@ static int sp_2048_mod_exp_34(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 34 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 68, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -1951,17 +1917,11 @@ static int sp_2048_mod_exp_34(sp_digit* r, const sp_digit* a, const sp_digit* e, } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #elif !defined(WC_NO_CACHE_RESISTANT) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 68]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 68); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -1975,15 +1935,7 @@ static int sp_2048_mod_exp_34(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 34 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 68, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -2040,17 +1992,11 @@ static int sp_2048_mod_exp_34(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, t[0], sizeof(*r) * 34 * 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(16 * 68) + 68]; -#endif + SP_DECL_VAR(sp_digit, td, (16 * 68) + 68); sp_digit* t[16]; sp_digit* rt = NULL; sp_digit* norm = NULL; @@ -2065,15 +2011,8 @@ static int sp_2048_mod_exp_34(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 68) + 68), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (16 * 68) + 68, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) @@ -2168,9 +2107,7 @@ static int sp_2048_mod_exp_34(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, rt, sizeof(sp_digit) * 68); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #endif @@ -2194,11 +2131,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { #ifdef WOLFSSL_SP_SMALL -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[34 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 34 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit* norm = NULL; @@ -2226,15 +2159,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 34 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 34 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { r = a + 34 * 2; m = r + 34 * 2; @@ -2287,17 +2212,11 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[34 * 5]; -#endif + SP_DECL_VAR(sp_digit, d, 34 * 5); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -2322,15 +2241,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 34 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 34 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d; r = a + 34 * 2; @@ -2398,9 +2309,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(d, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ @@ -2432,11 +2341,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) #if defined(WOLFSSL_SP_SMALL) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[34 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 34 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -2466,15 +2371,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 34 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 34 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 34; m = a + 68; @@ -2491,25 +2388,11 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 34); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 34, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[34 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 34 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -2539,15 +2422,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 34 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 34 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 34; m = a + 68; @@ -2564,27 +2439,13 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 34); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 34, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ #else #if defined(WOLFSSL_SP_SMALL) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[17 * 8]; -#endif + SP_DECL_VAR(sp_digit, a, 17 * 8); sp_digit* p = NULL; sp_digit* dp = NULL; sp_digit* dq = NULL; @@ -2618,14 +2479,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 17 * 8, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, a, 17 * 8, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 34; qi = dq = dp = p + 17; @@ -2666,23 +2520,11 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 17 * 8); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR(sp_digit, a, 17 * 8, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[17 * 13]; -#endif + SP_DECL_VAR(sp_digit, a, 17 * 13); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -2717,15 +2559,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 17 * 13, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 17 * 13, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 34 * 2; q = p + 17; @@ -2768,15 +2602,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 17 * 13); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 17 * 13, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ @@ -2869,11 +2695,7 @@ int sp_ModExp_2048(const mp_int* base, const mp_int* exp, const mp_int* mod, { #ifdef WOLFSSL_SP_SMALL int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[34 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 34 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -2892,14 +2714,7 @@ int sp_ModExp_2048(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 34 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 34 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 34 * 2; @@ -2917,24 +2732,11 @@ int sp_ModExp_2048(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_2048_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 34U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 34U, NULL, DYNAMIC_TYPE_DH); + return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[34 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 34 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -2954,13 +2756,7 @@ int sp_ModExp_2048(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 34 * 4, NULL, DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 34 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 34 * 2; @@ -2978,18 +2774,7 @@ int sp_ModExp_2048(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_2048_to_mp(r, res); } - -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 34U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 34U, NULL, DYNAMIC_TYPE_DH); return err; #endif @@ -3022,11 +2807,7 @@ SP_NOINLINE static void sp_2048_lshift_34(sp_digit* r, const sp_digit* a, */ static int sp_2048_mod_exp_2_34(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[103]; -#endif + SP_DECL_VAR(sp_digit, td, 103); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -3041,15 +2822,7 @@ static int sp_2048_mod_exp_2_34(sp_digit* r, const sp_digit* e, int bits, const err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 103, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 103, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 68; @@ -3121,9 +2894,7 @@ static int sp_2048_mod_exp_2_34(sp_digit* r, const sp_digit* e, int bits, const sp_2048_cond_sub_34(r, r, m, (sp_digit)~(n >> 63)); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -3145,11 +2916,7 @@ static int sp_2048_mod_exp_2_34(sp_digit* r, const sp_digit* e, int bits, const int sp_DhExp_2048(const mp_int* base, const byte* exp, word32 expLen, const mp_int* mod, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[34 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 34 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -3169,14 +2936,7 @@ int sp_DhExp_2048(const mp_int* base, const byte* exp, word32 expLen, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 34 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 34 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 34 * 2; @@ -3210,17 +2970,7 @@ int sp_DhExp_2048(const mp_int* base, const byte* exp, word32 expLen, XMEMMOVE(out, out + i, *outLen); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 34U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 34U, NULL, DYNAMIC_TYPE_DH); return err; } @@ -3240,11 +2990,7 @@ int sp_ModExp_1024(const mp_int* base, const mp_int* exp, const mp_int* mod, { #ifdef WOLFSSL_SP_SMALL int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[17 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 17 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -3263,14 +3009,7 @@ int sp_ModExp_1024(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 17 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 17 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 17 * 2; @@ -3289,24 +3028,11 @@ int sp_ModExp_1024(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_2048_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 34U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 34U, NULL, DYNAMIC_TYPE_DH); + return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[17 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 17 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -3326,13 +3052,7 @@ int sp_ModExp_1024(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 17 * 4, NULL, DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 17 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 17 * 2; @@ -3351,18 +3071,7 @@ int sp_ModExp_1024(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_2048_to_mp(r, res); } - -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 34U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 34U, NULL, DYNAMIC_TYPE_DH); return err; #endif @@ -4469,30 +4178,16 @@ static int sp_2048_div_18(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_64 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 18 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 18 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 18 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 18 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 36 + 1; sd = t2 + 18 + 1; @@ -4535,9 +4230,7 @@ static int sp_2048_div_18(const sp_digit* a, const sp_digit* d, sp_2048_rshift_18(r, r, 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -4569,11 +4262,7 @@ static int sp_2048_mod_exp_18(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 36]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 36); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -4587,15 +4276,7 @@ static int sp_2048_mod_exp_18(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 18 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 36, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -4653,17 +4334,11 @@ static int sp_2048_mod_exp_18(sp_digit* r, const sp_digit* a, const sp_digit* e, } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #elif !defined(WC_NO_CACHE_RESISTANT) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 36]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 36); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -4677,15 +4352,7 @@ static int sp_2048_mod_exp_18(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 18 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 36, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -4742,17 +4409,11 @@ static int sp_2048_mod_exp_18(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, t[0], sizeof(*r) * 18 * 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(32 * 36) + 36]; -#endif + SP_DECL_VAR(sp_digit, td, (32 * 36) + 36); sp_digit* t[32]; sp_digit* rt = NULL; sp_digit* norm = NULL; @@ -4767,15 +4428,8 @@ static int sp_2048_mod_exp_18(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 36) + 36), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (32 * 36) + 36, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) @@ -4887,9 +4541,7 @@ static int sp_2048_mod_exp_18(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, rt, sizeof(sp_digit) * 36); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #endif @@ -5351,30 +5003,16 @@ static int sp_2048_div_36(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_64 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 36 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 36 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 36 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 36 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 72 + 1; sd = t2 + 36 + 1; @@ -5417,9 +5055,7 @@ static int sp_2048_div_36(const sp_digit* a, const sp_digit* d, sp_2048_rshift_36(r, r, 4); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -5454,11 +5090,7 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 72]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 72); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -5472,15 +5104,7 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 36 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 72, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -5538,17 +5162,11 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #elif !defined(WC_NO_CACHE_RESISTANT) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 72]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 72); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -5562,15 +5180,7 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 36 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 72, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -5627,17 +5237,11 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, t[0], sizeof(*r) * 36 * 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(16 * 72) + 72]; -#endif + SP_DECL_VAR(sp_digit, td, (16 * 72) + 72); sp_digit* t[16]; sp_digit* rt = NULL; sp_digit* norm = NULL; @@ -5652,15 +5256,8 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 72) + 72), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (16 * 72) + 72, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) @@ -5755,9 +5352,7 @@ static int sp_2048_mod_exp_36(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, rt, sizeof(sp_digit) * 72); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #endif @@ -5783,11 +5378,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { #ifdef WOLFSSL_SP_SMALL -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[36 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 36 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit* norm = NULL; @@ -5815,15 +5406,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 36 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 36 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { r = a + 36 * 2; m = r + 36 * 2; @@ -5876,17 +5459,11 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[36 * 5]; -#endif + SP_DECL_VAR(sp_digit, d, 36 * 5); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -5911,15 +5488,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 36 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 36 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d; r = a + 36 * 2; @@ -5987,9 +5556,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(d, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ @@ -6021,11 +5588,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) #if defined(WOLFSSL_SP_SMALL) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[36 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 36 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -6055,15 +5618,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 36 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 36 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 36; m = a + 72; @@ -6080,25 +5635,11 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 36); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 36, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[36 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 36 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -6128,15 +5669,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 36 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 36 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 36; m = a + 72; @@ -6153,27 +5686,13 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 36); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 36, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ #else #if defined(WOLFSSL_SP_SMALL) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[18 * 8]; -#endif + SP_DECL_VAR(sp_digit, a, 18 * 8); sp_digit* p = NULL; sp_digit* dp = NULL; sp_digit* dq = NULL; @@ -6207,14 +5726,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 8, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, a, 18 * 8, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 36; qi = dq = dp = p + 18; @@ -6255,23 +5767,11 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 18 * 8); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR(sp_digit, a, 18 * 8, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[18 * 13]; -#endif + SP_DECL_VAR(sp_digit, a, 18 * 13); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -6306,15 +5806,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 13, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 18 * 13, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 36 * 2; q = p + 18; @@ -6357,15 +5849,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 18 * 13); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 18 * 13, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ @@ -6458,11 +5942,7 @@ int sp_ModExp_2048(const mp_int* base, const mp_int* exp, const mp_int* mod, { #ifdef WOLFSSL_SP_SMALL int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[36 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 36 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -6481,14 +5961,7 @@ int sp_ModExp_2048(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 36 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 36 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 36 * 2; @@ -6506,24 +5979,11 @@ int sp_ModExp_2048(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_2048_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 36U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 36U, NULL, DYNAMIC_TYPE_DH); + return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[36 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 36 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -6543,13 +6003,7 @@ int sp_ModExp_2048(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 36 * 4, NULL, DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 36 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 36 * 2; @@ -6567,18 +6021,7 @@ int sp_ModExp_2048(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_2048_to_mp(r, res); } - -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 36U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 36U, NULL, DYNAMIC_TYPE_DH); return err; #endif @@ -6680,11 +6123,7 @@ SP_NOINLINE static void sp_2048_lshift_36(sp_digit* r, const sp_digit* a, */ static int sp_2048_mod_exp_2_36(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[109]; -#endif + SP_DECL_VAR(sp_digit, td, 109); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -6699,15 +6138,7 @@ static int sp_2048_mod_exp_2_36(sp_digit* r, const sp_digit* e, int bits, const err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 109, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 109, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 72; @@ -6779,9 +6210,7 @@ static int sp_2048_mod_exp_2_36(sp_digit* r, const sp_digit* e, int bits, const sp_2048_cond_sub_36(r, r, m, (sp_digit)~(n >> 63)); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -6803,11 +6232,7 @@ static int sp_2048_mod_exp_2_36(sp_digit* r, const sp_digit* e, int bits, const int sp_DhExp_2048(const mp_int* base, const byte* exp, word32 expLen, const mp_int* mod, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[36 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 36 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -6827,14 +6252,7 @@ int sp_DhExp_2048(const mp_int* base, const byte* exp, word32 expLen, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 36 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 36 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 36 * 2; @@ -6868,17 +6286,7 @@ int sp_DhExp_2048(const mp_int* base, const byte* exp, word32 expLen, XMEMMOVE(out, out + i, *outLen); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 36U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 36U, NULL, DYNAMIC_TYPE_DH); return err; } @@ -6898,11 +6306,7 @@ int sp_ModExp_1024(const mp_int* base, const mp_int* exp, const mp_int* mod, { #ifdef WOLFSSL_SP_SMALL int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[18 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 18 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -6921,14 +6325,7 @@ int sp_ModExp_1024(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 18 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 18 * 2; @@ -6947,24 +6344,11 @@ int sp_ModExp_1024(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_2048_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 36U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 36U, NULL, DYNAMIC_TYPE_DH); + return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[18 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 18 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -6984,13 +6368,7 @@ int sp_ModExp_1024(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 4, NULL, DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 18 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 18 * 2; @@ -7009,18 +6387,7 @@ int sp_ModExp_1024(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_2048_to_mp(r, res); } - -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 36U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 36U, NULL, DYNAMIC_TYPE_DH); return err; #endif @@ -7800,30 +7167,16 @@ static int sp_3072_div_26(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_64 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 26 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 26 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 26 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 26 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 52 + 1; sd = t2 + 26 + 1; @@ -7866,9 +7219,7 @@ static int sp_3072_div_26(const sp_digit* a, const sp_digit* d, sp_3072_rshift_26(r, r, 24); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -7900,11 +7251,7 @@ static int sp_3072_mod_exp_26(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 52]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 52); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -7918,15 +7265,7 @@ static int sp_3072_mod_exp_26(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 26 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 52, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -7984,17 +7323,11 @@ static int sp_3072_mod_exp_26(sp_digit* r, const sp_digit* a, const sp_digit* e, } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #elif !defined(WC_NO_CACHE_RESISTANT) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 52]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 52); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -8008,15 +7341,7 @@ static int sp_3072_mod_exp_26(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 26 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 52, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -8073,17 +7398,11 @@ static int sp_3072_mod_exp_26(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, t[0], sizeof(*r) * 26 * 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(32 * 52) + 52]; -#endif + SP_DECL_VAR(sp_digit, td, (32 * 52) + 52); sp_digit* t[32]; sp_digit* rt = NULL; sp_digit* norm = NULL; @@ -8098,15 +7417,8 @@ static int sp_3072_mod_exp_26(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 52) + 52), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (32 * 52) + 52, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) @@ -8218,9 +7530,7 @@ static int sp_3072_mod_exp_26(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, rt, sizeof(sp_digit) * 52); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #endif @@ -8627,30 +7937,16 @@ static int sp_3072_div_52(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_64 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 52 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 52 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 52 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 52 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 104 + 1; sd = t2 + 52 + 1; @@ -8693,9 +7989,7 @@ static int sp_3072_div_52(const sp_digit* a, const sp_digit* d, sp_3072_rshift_52(r, r, 48); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -8728,11 +8022,7 @@ static int sp_3072_mod_exp_52(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 104]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 104); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -8746,15 +8036,7 @@ static int sp_3072_mod_exp_52(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 52 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 104, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -8812,17 +8094,11 @@ static int sp_3072_mod_exp_52(sp_digit* r, const sp_digit* a, const sp_digit* e, } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #elif !defined(WC_NO_CACHE_RESISTANT) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 104]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 104); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -8836,15 +8112,7 @@ static int sp_3072_mod_exp_52(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 52 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 104, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -8901,17 +8169,11 @@ static int sp_3072_mod_exp_52(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, t[0], sizeof(*r) * 52 * 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(16 * 104) + 104]; -#endif + SP_DECL_VAR(sp_digit, td, (16 * 104) + 104); sp_digit* t[16]; sp_digit* rt = NULL; sp_digit* norm = NULL; @@ -8926,15 +8188,8 @@ static int sp_3072_mod_exp_52(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 104) + 104), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (16 * 104) + 104, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) @@ -9029,9 +8284,7 @@ static int sp_3072_mod_exp_52(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, rt, sizeof(sp_digit) * 104); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #endif @@ -9055,11 +8308,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { #ifdef WOLFSSL_SP_SMALL -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[52 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 52 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit* norm = NULL; @@ -9087,15 +8336,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 52 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 52 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { r = a + 52 * 2; m = r + 52 * 2; @@ -9148,17 +8389,11 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[52 * 5]; -#endif + SP_DECL_VAR(sp_digit, d, 52 * 5); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -9183,15 +8418,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 52 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 52 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d; r = a + 52 * 2; @@ -9259,9 +8486,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(d, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ @@ -9293,11 +8518,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) #if defined(WOLFSSL_SP_SMALL) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[52 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 52 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -9327,15 +8548,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 52 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 52 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 52; m = a + 104; @@ -9352,25 +8565,11 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 52); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 52, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[52 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 52 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -9400,15 +8599,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 52 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 52 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 52; m = a + 104; @@ -9425,27 +8616,13 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 52); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 52, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ #else #if defined(WOLFSSL_SP_SMALL) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[26 * 8]; -#endif + SP_DECL_VAR(sp_digit, a, 26 * 8); sp_digit* p = NULL; sp_digit* dp = NULL; sp_digit* dq = NULL; @@ -9479,14 +8656,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 26 * 8, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, a, 26 * 8, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 52; qi = dq = dp = p + 26; @@ -9527,23 +8697,11 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 26 * 8); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR(sp_digit, a, 26 * 8, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[26 * 13]; -#endif + SP_DECL_VAR(sp_digit, a, 26 * 13); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -9578,15 +8736,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 26 * 13, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 26 * 13, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 52 * 2; q = p + 26; @@ -9629,15 +8779,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 26 * 13); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 26 * 13, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ @@ -9730,11 +8872,7 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, { #ifdef WOLFSSL_SP_SMALL int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[52 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 52 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -9753,14 +8891,7 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 52 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 52 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 52 * 2; @@ -9778,24 +8909,11 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_3072_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 52U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 52U, NULL, DYNAMIC_TYPE_DH); + return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[52 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 52 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -9815,13 +8933,7 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 52 * 4, NULL, DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 52 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 52 * 2; @@ -9839,18 +8951,7 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_3072_to_mp(r, res); } - -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 52U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 52U, NULL, DYNAMIC_TYPE_DH); return err; #endif @@ -9883,11 +8984,7 @@ SP_NOINLINE static void sp_3072_lshift_52(sp_digit* r, const sp_digit* a, */ static int sp_3072_mod_exp_2_52(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[157]; -#endif + SP_DECL_VAR(sp_digit, td, 157); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -9902,15 +8999,7 @@ static int sp_3072_mod_exp_2_52(sp_digit* r, const sp_digit* e, int bits, const err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 157, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 157, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 104; @@ -9982,9 +9071,7 @@ static int sp_3072_mod_exp_2_52(sp_digit* r, const sp_digit* e, int bits, const sp_3072_cond_sub_52(r, r, m, (sp_digit)~(n >> 63)); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -10006,11 +9093,7 @@ static int sp_3072_mod_exp_2_52(sp_digit* r, const sp_digit* e, int bits, const int sp_DhExp_3072(const mp_int* base, const byte* exp, word32 expLen, const mp_int* mod, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[52 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 52 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -10030,14 +9113,7 @@ int sp_DhExp_3072(const mp_int* base, const byte* exp, word32 expLen, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 52 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 52 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 52 * 2; @@ -10071,17 +9147,7 @@ int sp_DhExp_3072(const mp_int* base, const byte* exp, word32 expLen, XMEMMOVE(out, out + i, *outLen); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 52U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 52U, NULL, DYNAMIC_TYPE_DH); return err; } @@ -10101,11 +9167,7 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, { #ifdef WOLFSSL_SP_SMALL int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[26 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 26 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -10124,14 +9186,7 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 26 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 26 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 26 * 2; @@ -10150,24 +9205,11 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_3072_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 52U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 52U, NULL, DYNAMIC_TYPE_DH); + return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[26 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 26 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -10187,13 +9229,7 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 26 * 4, NULL, DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 26 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 26 * 2; @@ -10212,18 +9248,7 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_3072_to_mp(r, res); } - -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 52U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 52U, NULL, DYNAMIC_TYPE_DH); return err; #endif @@ -11468,30 +10493,16 @@ static int sp_3072_div_27(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_64 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 27 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 27 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 27 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 27 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 54 + 1; sd = t2 + 27 + 1; @@ -11534,9 +10545,7 @@ static int sp_3072_div_27(const sp_digit* a, const sp_digit* d, sp_3072_rshift_27(r, r, 3); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -11568,11 +10577,7 @@ static int sp_3072_mod_exp_27(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 54]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 54); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -11586,15 +10591,7 @@ static int sp_3072_mod_exp_27(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 27 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 54, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -11652,17 +10649,11 @@ static int sp_3072_mod_exp_27(sp_digit* r, const sp_digit* a, const sp_digit* e, } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #elif !defined(WC_NO_CACHE_RESISTANT) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 54]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 54); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -11676,15 +10667,7 @@ static int sp_3072_mod_exp_27(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 27 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 54, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -11741,17 +10724,11 @@ static int sp_3072_mod_exp_27(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, t[0], sizeof(*r) * 27 * 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(32 * 54) + 54]; -#endif + SP_DECL_VAR(sp_digit, td, (32 * 54) + 54); sp_digit* t[32]; sp_digit* rt = NULL; sp_digit* norm = NULL; @@ -11766,15 +10743,8 @@ static int sp_3072_mod_exp_27(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 54) + 54), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (32 * 54) + 54, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) @@ -11886,9 +10856,7 @@ static int sp_3072_mod_exp_27(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, rt, sizeof(sp_digit) * 54); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #endif @@ -12361,30 +11329,16 @@ static int sp_3072_div_54(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_64 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 54 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 54 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 54 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 54 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 108 + 1; sd = t2 + 54 + 1; @@ -12427,9 +11381,7 @@ static int sp_3072_div_54(const sp_digit* a, const sp_digit* d, sp_3072_rshift_54(r, r, 6); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -12464,11 +11416,7 @@ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 108]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 108); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -12482,15 +11430,7 @@ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 54 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 108, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -12548,17 +11488,11 @@ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e, } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #elif !defined(WC_NO_CACHE_RESISTANT) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 108]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 108); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -12572,15 +11506,7 @@ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 54 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 108, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -12637,17 +11563,11 @@ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, t[0], sizeof(*r) * 54 * 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(16 * 108) + 108]; -#endif + SP_DECL_VAR(sp_digit, td, (16 * 108) + 108); sp_digit* t[16]; sp_digit* rt = NULL; sp_digit* norm = NULL; @@ -12662,15 +11582,8 @@ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 108) + 108), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (16 * 108) + 108, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) @@ -12765,9 +11678,7 @@ static int sp_3072_mod_exp_54(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, rt, sizeof(sp_digit) * 108); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #endif @@ -12793,11 +11704,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { #ifdef WOLFSSL_SP_SMALL -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[54 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 54 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit* norm = NULL; @@ -12825,15 +11732,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 54 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 54 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { r = a + 54 * 2; m = r + 54 * 2; @@ -12886,17 +11785,11 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[54 * 5]; -#endif + SP_DECL_VAR(sp_digit, d, 54 * 5); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -12921,15 +11814,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 54 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 54 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d; r = a + 54 * 2; @@ -12997,9 +11882,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(d, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ @@ -13031,11 +11914,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) #if defined(WOLFSSL_SP_SMALL) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[54 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 54 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -13065,15 +11944,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 54 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 54 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 54; m = a + 108; @@ -13090,25 +11961,11 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 54); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 54, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[54 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 54 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -13138,15 +11995,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 54 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 54 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 54; m = a + 108; @@ -13163,27 +12012,13 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 54); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 54, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ #else #if defined(WOLFSSL_SP_SMALL) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[27 * 8]; -#endif + SP_DECL_VAR(sp_digit, a, 27 * 8); sp_digit* p = NULL; sp_digit* dp = NULL; sp_digit* dq = NULL; @@ -13217,14 +12052,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 27 * 8, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, a, 27 * 8, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 54; qi = dq = dp = p + 27; @@ -13265,23 +12093,11 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 27 * 8); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR(sp_digit, a, 27 * 8, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[27 * 13]; -#endif + SP_DECL_VAR(sp_digit, a, 27 * 13); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -13316,15 +12132,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 27 * 13, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 27 * 13, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 54 * 2; q = p + 27; @@ -13367,15 +12175,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 27 * 13); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 27 * 13, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ @@ -13468,11 +12268,7 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, { #ifdef WOLFSSL_SP_SMALL int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[54 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 54 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -13491,14 +12287,7 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 54 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 54 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 54 * 2; @@ -13516,24 +12305,11 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_3072_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 54U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 54U, NULL, DYNAMIC_TYPE_DH); + return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[54 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 54 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -13553,13 +12329,7 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 54 * 4, NULL, DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 54 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 54 * 2; @@ -13577,18 +12347,7 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_3072_to_mp(r, res); } - -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 54U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 54U, NULL, DYNAMIC_TYPE_DH); return err; #endif @@ -13726,11 +12485,7 @@ SP_NOINLINE static void sp_3072_lshift_54(sp_digit* r, const sp_digit* a, */ static int sp_3072_mod_exp_2_54(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[163]; -#endif + SP_DECL_VAR(sp_digit, td, 163); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -13745,15 +12500,7 @@ static int sp_3072_mod_exp_2_54(sp_digit* r, const sp_digit* e, int bits, const err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 163, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 163, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 108; @@ -13825,9 +12572,7 @@ static int sp_3072_mod_exp_2_54(sp_digit* r, const sp_digit* e, int bits, const sp_3072_cond_sub_54(r, r, m, (sp_digit)~(n >> 63)); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -13849,11 +12594,7 @@ static int sp_3072_mod_exp_2_54(sp_digit* r, const sp_digit* e, int bits, const int sp_DhExp_3072(const mp_int* base, const byte* exp, word32 expLen, const mp_int* mod, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[54 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 54 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -13873,14 +12614,7 @@ int sp_DhExp_3072(const mp_int* base, const byte* exp, word32 expLen, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 54 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 54 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 54 * 2; @@ -13914,17 +12648,7 @@ int sp_DhExp_3072(const mp_int* base, const byte* exp, word32 expLen, XMEMMOVE(out, out + i, *outLen); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 54U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 54U, NULL, DYNAMIC_TYPE_DH); return err; } @@ -13944,11 +12668,7 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, { #ifdef WOLFSSL_SP_SMALL int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[27 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 27 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -13967,14 +12687,7 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 27 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 27 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 27 * 2; @@ -13993,24 +12706,11 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_3072_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 54U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 54U, NULL, DYNAMIC_TYPE_DH); + return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[27 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 27 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -14030,13 +12730,7 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 27 * 4, NULL, DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 27 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 27 * 2; @@ -14055,18 +12749,7 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_3072_to_mp(r, res); } - -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 54U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 54U, NULL, DYNAMIC_TYPE_DH); return err; #endif @@ -14852,30 +13535,16 @@ static int sp_4096_div_35(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_64 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 35 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 35 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 35 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 35 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 70 + 1; sd = t2 + 35 + 1; @@ -14918,9 +13587,7 @@ static int sp_4096_div_35(const sp_digit* a, const sp_digit* d, sp_4096_rshift_35(r, r, 17); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -14952,11 +13619,7 @@ static int sp_4096_mod_exp_35(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 70]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 70); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -14970,15 +13633,7 @@ static int sp_4096_mod_exp_35(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 35 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 70, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -15036,17 +13691,11 @@ static int sp_4096_mod_exp_35(sp_digit* r, const sp_digit* a, const sp_digit* e, } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #elif !defined(WC_NO_CACHE_RESISTANT) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 70]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 70); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -15060,15 +13709,7 @@ static int sp_4096_mod_exp_35(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 35 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 70, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -15125,17 +13766,11 @@ static int sp_4096_mod_exp_35(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, t[0], sizeof(*r) * 35 * 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(32 * 70) + 70]; -#endif + SP_DECL_VAR(sp_digit, td, (32 * 70) + 70); sp_digit* t[32]; sp_digit* rt = NULL; sp_digit* norm = NULL; @@ -15150,15 +13785,8 @@ static int sp_4096_mod_exp_35(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 70) + 70), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (32 * 70) + 70, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) @@ -15270,9 +13898,7 @@ static int sp_4096_mod_exp_35(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, rt, sizeof(sp_digit) * 70); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #endif @@ -15674,30 +14300,16 @@ static int sp_4096_div_70(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_64 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 70 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 70 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 70 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 70 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 140 + 1; sd = t2 + 70 + 1; @@ -15740,9 +14352,7 @@ static int sp_4096_div_70(const sp_digit* a, const sp_digit* d, sp_4096_rshift_70(r, r, 34); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -15775,11 +14385,7 @@ static int sp_4096_mod_exp_70(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 140]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 140); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -15793,15 +14399,7 @@ static int sp_4096_mod_exp_70(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 70 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 140, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -15859,17 +14457,11 @@ static int sp_4096_mod_exp_70(sp_digit* r, const sp_digit* a, const sp_digit* e, } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #elif !defined(WC_NO_CACHE_RESISTANT) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 140]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 140); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -15883,15 +14475,7 @@ static int sp_4096_mod_exp_70(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 70 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 140, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -15948,17 +14532,11 @@ static int sp_4096_mod_exp_70(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, t[0], sizeof(*r) * 70 * 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(16 * 140) + 140]; -#endif + SP_DECL_VAR(sp_digit, td, (16 * 140) + 140); sp_digit* t[16]; sp_digit* rt = NULL; sp_digit* norm = NULL; @@ -15973,15 +14551,8 @@ static int sp_4096_mod_exp_70(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 140) + 140), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (16 * 140) + 140, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) @@ -16076,9 +14647,7 @@ static int sp_4096_mod_exp_70(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, rt, sizeof(sp_digit) * 140); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #endif @@ -16102,11 +14671,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { #ifdef WOLFSSL_SP_SMALL -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[70 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 70 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit* norm = NULL; @@ -16134,15 +14699,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 70 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 70 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { r = a + 70 * 2; m = r + 70 * 2; @@ -16195,17 +14752,11 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[70 * 5]; -#endif + SP_DECL_VAR(sp_digit, d, 70 * 5); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -16230,15 +14781,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 70 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 70 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d; r = a + 70 * 2; @@ -16306,9 +14849,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(d, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ @@ -16340,11 +14881,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) #if defined(WOLFSSL_SP_SMALL) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[70 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 70 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -16374,15 +14911,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 70 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 70 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 70; m = a + 140; @@ -16399,25 +14928,11 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 70); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 70, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[70 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 70 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -16447,15 +14962,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 70 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 70 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 70; m = a + 140; @@ -16472,27 +14979,13 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 70); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 70, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ #else #if defined(WOLFSSL_SP_SMALL) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[35 * 8]; -#endif + SP_DECL_VAR(sp_digit, a, 35 * 8); sp_digit* p = NULL; sp_digit* dp = NULL; sp_digit* dq = NULL; @@ -16526,14 +15019,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 35 * 8, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, a, 35 * 8, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 70; qi = dq = dp = p + 35; @@ -16574,23 +15060,11 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 35 * 8); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR(sp_digit, a, 35 * 8, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[35 * 13]; -#endif + SP_DECL_VAR(sp_digit, a, 35 * 13); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -16625,15 +15099,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 35 * 13, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 35 * 13, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 70 * 2; q = p + 35; @@ -16676,15 +15142,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 35 * 13); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 35 * 13, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ @@ -16777,11 +15235,7 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, { #ifdef WOLFSSL_SP_SMALL int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[70 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 70 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -16800,14 +15254,7 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 70 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 70 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 70 * 2; @@ -16825,24 +15272,11 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_4096_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 70U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 70U, NULL, DYNAMIC_TYPE_DH); + return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[70 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 70 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -16862,13 +15296,7 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 70 * 4, NULL, DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 70 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 70 * 2; @@ -16886,18 +15314,7 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_4096_to_mp(r, res); } - -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 70U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 70U, NULL, DYNAMIC_TYPE_DH); return err; #endif @@ -16930,11 +15347,7 @@ SP_NOINLINE static void sp_4096_lshift_70(sp_digit* r, const sp_digit* a, */ static int sp_4096_mod_exp_2_70(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[211]; -#endif + SP_DECL_VAR(sp_digit, td, 211); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -16949,15 +15362,7 @@ static int sp_4096_mod_exp_2_70(sp_digit* r, const sp_digit* e, int bits, const err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 211, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 211, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 140; @@ -17029,9 +15434,7 @@ static int sp_4096_mod_exp_2_70(sp_digit* r, const sp_digit* e, int bits, const sp_4096_cond_sub_70(r, r, m, (sp_digit)~(n >> 63)); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -17053,11 +15456,7 @@ static int sp_4096_mod_exp_2_70(sp_digit* r, const sp_digit* e, int bits, const int sp_DhExp_4096(const mp_int* base, const byte* exp, word32 expLen, const mp_int* mod, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[70 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 70 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -17077,14 +15476,7 @@ int sp_DhExp_4096(const mp_int* base, const byte* exp, word32 expLen, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 70 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 70 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 70 * 2; @@ -17118,17 +15510,7 @@ int sp_DhExp_4096(const mp_int* base, const byte* exp, word32 expLen, XMEMMOVE(out, out + i, *outLen); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 70U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 70U, NULL, DYNAMIC_TYPE_DH); return err; } @@ -18570,30 +16952,16 @@ static int sp_4096_div_39(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_64 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 39 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 39 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 39 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 39 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 78 + 1; sd = t2 + 39 + 1; @@ -18636,9 +17004,7 @@ static int sp_4096_div_39(const sp_digit* a, const sp_digit* d, sp_4096_rshift_39(r, r, 19); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -18670,11 +17036,7 @@ static int sp_4096_mod_exp_39(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 78]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 78); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -18688,15 +17050,7 @@ static int sp_4096_mod_exp_39(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 39 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 78, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -18754,17 +17108,11 @@ static int sp_4096_mod_exp_39(sp_digit* r, const sp_digit* a, const sp_digit* e, } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #elif !defined(WC_NO_CACHE_RESISTANT) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 78]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 78); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -18778,15 +17126,7 @@ static int sp_4096_mod_exp_39(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 39 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 78, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -18843,17 +17183,11 @@ static int sp_4096_mod_exp_39(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, t[0], sizeof(*r) * 39 * 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(32 * 78) + 78]; -#endif + SP_DECL_VAR(sp_digit, td, (32 * 78) + 78); sp_digit* t[32]; sp_digit* rt = NULL; sp_digit* norm = NULL; @@ -18868,15 +17202,8 @@ static int sp_4096_mod_exp_39(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((32 * 78) + 78), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (32 * 78) + 78, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) @@ -18988,9 +17315,7 @@ static int sp_4096_mod_exp_39(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, rt, sizeof(sp_digit) * 78); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #endif @@ -19464,30 +17789,16 @@ static int sp_4096_div_78(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_64 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 78 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 78 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 78 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 78 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 156 + 1; sd = t2 + 78 + 1; @@ -19530,9 +17841,7 @@ static int sp_4096_div_78(const sp_digit* a, const sp_digit* d, sp_4096_rshift_78(r, r, 38); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -19567,11 +17876,7 @@ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e, int bits, const sp_digit* m, int reduceA) { #if defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 156]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 156); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -19585,15 +17890,7 @@ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 78 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 156, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -19651,17 +17948,11 @@ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e, } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #elif !defined(WC_NO_CACHE_RESISTANT) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[3 * 156]; -#endif + SP_DECL_VAR(sp_digit, td, 3 * 156); sp_digit* t[3] = {0, 0, 0}; sp_digit* norm = NULL; sp_digit mp = 1; @@ -19675,15 +17966,7 @@ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 3 * 78 * 2, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 3 * 156, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<3; i++) { @@ -19740,17 +18023,11 @@ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, t[0], sizeof(*r) * 78 * 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(16 * 156) + 156]; -#endif + SP_DECL_VAR(sp_digit, td, (16 * 156) + 156); sp_digit* t[16]; sp_digit* rt = NULL; sp_digit* norm = NULL; @@ -19765,15 +18042,8 @@ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * ((16 * 156) + 156), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (16 * 156) + 156, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) @@ -19868,9 +18138,7 @@ static int sp_4096_mod_exp_78(sp_digit* r, const sp_digit* a, const sp_digit* e, XMEMCPY(r, rt, sizeof(sp_digit) * 156); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; #endif @@ -19896,11 +18164,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { #ifdef WOLFSSL_SP_SMALL -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[78 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 78 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit* norm = NULL; @@ -19928,15 +18192,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 78 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 78 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { r = a + 78 * 2; m = r + 78 * 2; @@ -19989,17 +18245,11 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[78 * 5]; -#endif + SP_DECL_VAR(sp_digit, d, 78 * 5); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -20024,15 +18274,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 78 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 78 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d; r = a + 78 * 2; @@ -20100,9 +18342,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(d, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ @@ -20134,11 +18374,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) #if defined(WOLFSSL_SP_SMALL) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[78 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 78 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -20168,15 +18404,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 78 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 78 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 78; m = a + 156; @@ -20193,25 +18421,11 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 78); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 78, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[78 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 78 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -20241,15 +18455,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 78 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 78 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 78; m = a + 156; @@ -20266,27 +18472,13 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 78); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 78, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ #else #if defined(WOLFSSL_SP_SMALL) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[39 * 8]; -#endif + SP_DECL_VAR(sp_digit, a, 39 * 8); sp_digit* p = NULL; sp_digit* dp = NULL; sp_digit* dq = NULL; @@ -20320,14 +18512,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 39 * 8, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, a, 39 * 8, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 78; qi = dq = dp = p + 39; @@ -20368,23 +18553,11 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 39 * 8); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR(sp_digit, a, 39 * 8, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[39 * 13]; -#endif + SP_DECL_VAR(sp_digit, a, 39 * 13); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -20419,15 +18592,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 39 * 13, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 39 * 13, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 78 * 2; q = p + 39; @@ -20470,15 +18635,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 39 * 13); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 39 * 13, NULL, DYNAMIC_TYPE_RSA); return err; #endif /* WOLFSSL_SP_SMALL */ @@ -20571,11 +18728,7 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, { #ifdef WOLFSSL_SP_SMALL int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[78 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 78 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -20594,14 +18747,7 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 78 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 78 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 78 * 2; @@ -20619,24 +18765,11 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_4096_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 78U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 78U, NULL, DYNAMIC_TYPE_DH); + return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[78 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 78 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -20656,13 +18789,7 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 78 * 4, NULL, DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 78 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 78 * 2; @@ -20680,18 +18807,7 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_4096_to_mp(r, res); } - -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 78U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 78U, NULL, DYNAMIC_TYPE_DH); return err; #endif @@ -20877,11 +18993,7 @@ SP_NOINLINE static void sp_4096_lshift_78(sp_digit* r, const sp_digit* a, */ static int sp_4096_mod_exp_2_78(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[235]; -#endif + SP_DECL_VAR(sp_digit, td, 235); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -20896,15 +19008,7 @@ static int sp_4096_mod_exp_2_78(sp_digit* r, const sp_digit* e, int bits, const err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 235, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 235, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 156; @@ -20976,9 +19080,7 @@ static int sp_4096_mod_exp_2_78(sp_digit* r, const sp_digit* e, int bits, const sp_4096_cond_sub_78(r, r, m, (sp_digit)~(n >> 63)); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -21000,11 +19102,7 @@ static int sp_4096_mod_exp_2_78(sp_digit* r, const sp_digit* e, int bits, const int sp_DhExp_4096(const mp_int* base, const byte* exp, word32 expLen, const mp_int* mod, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* b = NULL; -#else - sp_digit b[78 * 4]; -#endif + SP_DECL_VAR(sp_digit, b, 78 * 4); sp_digit* e = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -21024,14 +19122,7 @@ int sp_DhExp_4096(const mp_int* base, const byte* exp, word32 expLen, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - b = (sp_digit*)XMALLOC(sizeof(sp_digit) * 78 * 4, NULL, - DYNAMIC_TYPE_DH); - if (b == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, b, 78 * 4, NULL, DYNAMIC_TYPE_DH); if (err == MP_OKAY) { e = b + 78 * 2; @@ -21065,17 +19156,7 @@ int sp_DhExp_4096(const mp_int* base, const byte* exp, word32 expLen, XMEMMOVE(out, out + i, *outLen); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (b != NULL) -#endif - { - /* only "e" is sensitive and needs zeroized */ - if (e != NULL) - ForceZero(e, sizeof(sp_digit) * 78U); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_DH); - #endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, b, e, 78U, NULL, DYNAMIC_TYPE_DH); return err; } @@ -22701,23 +20782,14 @@ static int sp_256_proj_point_add_5_nb(sp_ecc_ctx_t* sp_ctx, sp_point_256* r, */ static int sp_256_mod_mul_norm_5(sp_digit* r, const sp_digit* a, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - int64_t* t = NULL; -#else - int64_t t[2 * 8]; -#endif + SP_DECL_VAR(int64_t, t, 2 * 8); int64_t* a32 = NULL; int64_t o; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (int64_t*)XMALLOC(sizeof(int64_t) * 2 * 8, NULL, DYNAMIC_TYPE_ECC); - if (t == NULL) - return MEMORY_E; -#endif - + SP_ALLOC_VAR(int64_t, t, 2 * 8, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { a32 = t + 8; @@ -22793,9 +20865,7 @@ static int sp_256_mod_mul_norm_5(sp_digit* r, const sp_digit* a, const sp_digit* r[4] |= t[7] << 16U; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -22819,13 +20889,8 @@ static int sp_256_mod_mul_norm_5(sp_digit* r, const sp_digit* a, const sp_digit* static int sp_256_ecc_mulmod_5(sp_point_256* r, const sp_point_256* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_256 t[3]; - sp_digit tmp[2 * 5 * 6]; -#endif + SP_DECL_VAR(sp_point_256, t, 3); + SP_DECL_VAR(sp_digit, tmp, 2 * 5 * 6); sp_digit n; int i; int c; @@ -22836,18 +20901,8 @@ static int sp_256_ecc_mulmod_5(sp_point_256* r, const sp_point_256* g, (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 5 * 6, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_point_256, t, 3, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 5 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { XMEMSET(t, 0, sizeof(sp_point_256) * 3); @@ -22897,24 +20952,8 @@ static int sp_256_ecc_mulmod_5(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 5 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (t != NULL) -#endif - { - ForceZero(t, sizeof(sp_point_256) * 3); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 5 * 6, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_point_256, t, 3, heap, DYNAMIC_TYPE_ECC); return err; } @@ -23456,13 +21495,8 @@ static void sp_256_get_point_33_5(sp_point_256* r, const sp_point_256* table, static int sp_256_ecc_mulmod_win_add_sub_5(sp_point_256* r, const sp_point_256* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_256 t[33+2]; - sp_digit tmp[2 * 5 * 6]; -#endif + SP_DECL_VAR(sp_point_256, t, 33+2); + SP_DECL_VAR(sp_digit, tmp, 2 * 5 * 6); sp_point_256* rt = NULL; sp_point_256* p = NULL; sp_digit* negy; @@ -23474,19 +21508,8 @@ static int sp_256_ecc_mulmod_win_add_sub_5(sp_point_256* r, const sp_point_256* (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * - (33+2), heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 5 * 6, - heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, t, 33+2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 5 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 33; p = t + 33+1; @@ -23570,10 +21593,8 @@ static int sp_256_ecc_mulmod_win_add_sub_5(sp_point_256* r, const sp_point_256* } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; } @@ -23698,11 +21719,7 @@ static void sp_256_proj_to_affine_5(sp_point_256* a, sp_digit* t) static int sp_256_gen_stripe_table_5(const sp_point_256* a, sp_table_entry_256* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* t = NULL; -#else - sp_point_256 t[3]; -#endif + SP_DECL_VAR(sp_point_256, t, 3); sp_point_256* s1 = NULL; sp_point_256* s2 = NULL; int i; @@ -23711,13 +21728,7 @@ static int sp_256_gen_stripe_table_5(const sp_point_256* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_256, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -23766,9 +21777,7 @@ static int sp_256_gen_stripe_table_5(const sp_point_256* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -23832,13 +21841,8 @@ static int sp_256_ecc_mulmod_stripe_5(sp_point_256* r, const sp_point_256* g, const sp_table_entry_256* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_256 rt[2]; - sp_digit t[2 * 5 * 6]; -#endif + SP_DECL_VAR(sp_point_256, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 5 * 6); sp_point_256* p = NULL; int i; int j; @@ -23852,19 +21856,8 @@ static int sp_256_ecc_mulmod_stripe_5(sp_point_256* r, const sp_point_256* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 5 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 5 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -23918,10 +21911,8 @@ static int sp_256_ecc_mulmod_stripe_5(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -24038,20 +22029,11 @@ static int sp_256_ecc_mulmod_5(sp_point_256* r, const sp_point_256* g, #ifndef FP_ECC return sp_256_ecc_mulmod_win_add_sub_5(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 5 * 6]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 5 * 6); sp_cache_256_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 5 * 6, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 5 * 6, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -24084,9 +22066,7 @@ static int sp_256_ecc_mulmod_5(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -24105,28 +22085,12 @@ static int sp_256_ecc_mulmod_5(sp_point_256* r, const sp_point_256* g, int sp_ecc_mulmod_256(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[1]; - sp_digit k[5]; -#endif + SP_DECL_VAR(sp_point_256, point, 1); + SP_DECL_VAR(sp_digit, k, 5); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 5, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 5, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(k, 5, km); sp_256_point_from_ecc_point_5(point, gm); @@ -24137,10 +22101,8 @@ int sp_ecc_mulmod_256(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_256_point_to_ecc_point_5(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -24160,31 +22122,14 @@ int sp_ecc_mulmod_256(const mp_int* km, const ecc_point* gm, ecc_point* r, int sp_ecc_mulmod_add_256(const mp_int* km, const ecc_point* gm, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[2]; - sp_digit k[5 + 5 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_256, point, 2); + SP_DECL_VAR(sp_digit, k, 5 + 5 * 2 * 6); sp_point_256* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (5 + 5 * 2 * 6), heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 5 + 5 * 2 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 5; @@ -24215,10 +22160,8 @@ int sp_ecc_mulmod_add_256(const mp_int* km, const ecc_point* gm, err = sp_256_point_to_ecc_point_5(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -25571,28 +23514,12 @@ static int sp_256_ecc_mulmod_base_5(sp_point_256* r, const sp_digit* k, */ int sp_ecc_mulmod_base_256(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[1]; - sp_digit k[5]; -#endif + SP_DECL_VAR(sp_point_256, point, 1); + SP_DECL_VAR(sp_digit, k, 5); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 5, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 5, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(k, 5, km); @@ -25602,10 +23529,8 @@ int sp_ecc_mulmod_base_256(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_256_point_to_ecc_point_5(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -25624,31 +23549,14 @@ int sp_ecc_mulmod_base_256(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_256(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[2]; - sp_digit k[5 + 5 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_256, point, 2); + SP_DECL_VAR(sp_digit, k, 5 + 5 * 2 * 6); sp_point_256* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (5 + 5 * 2 * 6), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 5 + 5 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 5; @@ -25678,10 +23586,8 @@ int sp_ecc_mulmod_base_add_256(const mp_int* km, const ecc_point* am, err = sp_256_point_to_ecc_point_5(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -25781,17 +23687,12 @@ static int sp_256_ecc_gen_k_5(WC_RNG* rng, sp_digit* k) */ int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_DECL_VAR(sp_point_256, point, 2); #else - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - sp_point_256 point[2]; - #else - sp_point_256 point[1]; - #endif - sp_digit k[5]; + SP_DECL_VAR(sp_point_256, point, 1); #endif + SP_DECL_VAR(sp_digit, k, 5); #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN sp_point_256* infinity = NULL; #endif @@ -25800,22 +23701,12 @@ int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, DYNAMIC_TYPE_ECC); - #else - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, DYNAMIC_TYPE_ECC); - #endif - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 5, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_ALLOC_VAR(sp_point_256, point, 2, heap, DYNAMIC_TYPE_ECC); +#else + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); #endif - + SP_ALLOC_VAR(sp_digit, k, 5, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN infinity = point + 1; @@ -25845,11 +23736,9 @@ int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) err = sp_256_point_to_ecc_point_5(point, pub); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); /* point is not sensitive, so no need to zeroize */ - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -25992,34 +23881,16 @@ static void sp_256_to_bin_5(sp_digit* r, byte* a) int sp_ecc_secret_gen_256(const mp_int* priv, const ecc_point* pub, byte* out, word32* outLen, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[1]; - sp_digit k[5]; -#endif + SP_DECL_VAR(sp_point_256, point, 1); + SP_DECL_VAR(sp_digit, k, 5); int err = MP_OKAY; if (*outLen < 32U) { err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 5, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 5, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(k, 5, priv); sp_256_point_from_ecc_point_5(point, pub); @@ -26030,10 +23901,8 @@ int sp_ecc_secret_gen_256(const mp_int* priv, const ecc_point* pub, byte* out, *outLen = 32; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -26210,29 +24079,17 @@ SP_NOINLINE static void sp_256_lshift_10(sp_digit* r, const sp_digit* a, static int sp_256_div_5(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { + SP_DECL_VAR(sp_digit, t1, 4 * 5 + 3); int i; sp_digit r1; sp_digit mask; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 5 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 5 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 5 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 10 + 1; sd = t2 + 5 + 1; @@ -26262,9 +24119,7 @@ static int sp_256_div_5(const sp_digit* a, const sp_digit* d, sp_256_rshift_5(r, t1, 4); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -26561,13 +24416,8 @@ static int sp_256_calc_s_5(sp_digit* s, const sp_digit* r, sp_digit* k, int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* e = NULL; - sp_point_256* point = NULL; -#else - sp_digit e[7 * 2 * 5]; - sp_point_256 point[1]; -#endif + SP_DECL_VAR(sp_digit, e, 7 * 2 * 5); + SP_DECL_VAR(sp_point_256, point, 1); sp_digit* x = NULL; sp_digit* k = NULL; sp_digit* r = NULL; @@ -26579,21 +24429,8 @@ int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - e = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7 * 2 * 5, heap, - DYNAMIC_TYPE_ECC); - if (e == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, e, 7 * 2 * 5, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { x = e + 2 * 5; k = e + 4 * 5; @@ -26658,24 +24495,8 @@ int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, err = sp_256_to_mp(s, sm); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (e != NULL) -#endif - { - ForceZero(e, sizeof(sp_digit) * 7 * 2 * 5); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(e, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (point != NULL) -#endif - { - ForceZero(point, sizeof(sp_point_256)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(point, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_digit, e, 7 * 2 * 5, heap, DYNAMIC_TYPE_ECC); return err; } @@ -26893,24 +24714,14 @@ static int sp_256_num_bits_5(const sp_digit* a) static int sp_256_mod_inv_5(sp_digit* r, const sp_digit* a, const sp_digit* m) { int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u = NULL; -#else - sp_digit u[5 * 4]; -#endif + SP_DECL_VAR(sp_digit, u, 5 * 4); sp_digit* v = NULL; sp_digit* b = NULL; sp_digit* d = NULL; int ut; int vt; -#ifdef WOLFSSL_SP_SMALL_STACK - u = (sp_digit*)XMALLOC(sizeof(sp_digit) * 5 * 4, NULL, - DYNAMIC_TYPE_ECC); - if (u == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, u, 5 * 4, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { v = u + 5; b = u + 2 * 5; @@ -26992,9 +24803,7 @@ static int sp_256_mod_inv_5(sp_digit* r, const sp_digit* a, const sp_digit* m) else XMEMCPY(r, d, sizeof(sp_digit) * 5); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(u, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -27110,13 +24919,8 @@ int sp_ecc_verify_256(const byte* hash, word32 hashLen, const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* rm, const mp_int* sm, int* res, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u1 = NULL; - sp_point_256* p1 = NULL; -#else - sp_digit u1[18 * 5]; - sp_point_256 p1[2]; -#endif + SP_DECL_VAR(sp_digit, u1, 18 * 5); + SP_DECL_VAR(sp_point_256, p1, 2); sp_digit* u2 = NULL; sp_digit* s = NULL; sp_digit* tmp = NULL; @@ -27125,21 +24929,8 @@ int sp_ecc_verify_256(const byte* hash, word32 hashLen, const mp_int* pX, sp_int64 c = 0; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p1 = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (p1 == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - u1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 5, heap, - DYNAMIC_TYPE_ECC); - if (u1 == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, u1, 18 * 5, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p1, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { u2 = u1 + 2 * 5; s = u1 + 4 * 5; @@ -27196,11 +24987,8 @@ int sp_ecc_verify_256(const byte* hash, word32 hashLen, const mp_int* pX, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u1, heap, DYNAMIC_TYPE_ECC); - XFREE(p1, heap, DYNAMIC_TYPE_ECC); -#endif - + SP_FREE_VAR(p1, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(u1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -27366,21 +25154,13 @@ int sp_ecc_verify_256_nb(sp_ecc_ctx_t* sp_ctx, const byte* hash, static int sp_256_ecc_is_point_5(const sp_point_256* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[5 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 5 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 5 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 5 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 5; @@ -27404,9 +25184,7 @@ static int sp_256_ecc_is_point_5(const sp_point_256* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -27420,21 +25198,11 @@ static int sp_256_ecc_is_point_5(const sp_point_256* point, */ int sp_ecc_is_point_256(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* pub = NULL; -#else - sp_point_256 pub[1]; -#endif + SP_DECL_VAR(sp_point_256, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_256*)XMALLOC(sizeof(sp_point_256), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_256, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(pub->x, 5, pX); sp_256_from_mp(pub->y, 5, pY); @@ -27443,9 +25211,7 @@ int sp_ecc_is_point_256(const mp_int* pX, const mp_int* pY) err = sp_256_ecc_is_point_5(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -27464,13 +25230,8 @@ int sp_ecc_is_point_256(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_256(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_256* pub = NULL; -#else - sp_digit priv[5]; - sp_point_256 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 5); + SP_DECL_VAR(sp_point_256, pub, 2); sp_point_256* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -27485,21 +25246,8 @@ int sp_ecc_check_key_256(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 5, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 5, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -27551,10 +25299,8 @@ int sp_ecc_check_key_256(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } @@ -27578,32 +25324,13 @@ int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_256* p = NULL; -#else - sp_digit tmp[2 * 5 * 6]; - sp_point_256 p[2]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 5 * 6); + SP_DECL_VAR(sp_point_256, p, 2); sp_point_256* q = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 5 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 5 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p, 2, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { q = p + 1; @@ -27631,10 +25358,8 @@ int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_256_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -27653,30 +25378,12 @@ int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_256* p = NULL; -#else - sp_digit tmp[2 * 5 * 2]; - sp_point_256 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 5 * 2); + SP_DECL_VAR(sp_point_256, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 5 * 2, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 5 * 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(p->x, 5, pX); sp_256_from_mp(p->y, 5, pY); @@ -27697,10 +25404,8 @@ int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_256_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -27715,30 +25420,13 @@ int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, */ int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_256* p = NULL; -#else - sp_digit tmp[2 * 5 * 4]; - sp_point_256 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 5 * 4); + SP_DECL_VAR(sp_point_256, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 5 * 4, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 5 * 4, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(p->x, 5, pX); sp_256_from_mp(p->y, 5, pY); @@ -27759,10 +25447,8 @@ int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ) err = sp_256_to_mp(p->z, pZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -27775,21 +25461,11 @@ int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ) */ static int sp_256_mont_sqrt_5(sp_digit* y) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 5]; -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 5); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 5, NULL, DYNAMIC_TYPE_ECC); - if (t1 == NULL) { - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, t1, 4 * 5, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 5; @@ -27826,9 +25502,7 @@ static int sp_256_mont_sqrt_5(sp_digit* y) } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -27843,20 +25517,11 @@ static int sp_256_mont_sqrt_5(sp_digit* y) */ int sp_ecc_uncompress_256(mp_int* xm, int odd, mp_int* ym) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* x = NULL; -#else - sp_digit x[4 * 5]; -#endif + SP_DECL_VAR(sp_digit, x, 4 * 5); sp_digit* y = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - x = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 5, NULL, DYNAMIC_TYPE_ECC); - if (x == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, x, 4 * 5, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { y = x + 2 * 5; @@ -27891,9 +25556,7 @@ int sp_ecc_uncompress_256(mp_int* xm, int odd, mp_int* ym) err = sp_256_to_mp(y, ym); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(x, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(x, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -29606,23 +27269,14 @@ static int sp_384_proj_point_add_7_nb(sp_ecc_ctx_t* sp_ctx, sp_point_384* r, */ static int sp_384_mod_mul_norm_7(sp_digit* r, const sp_digit* a, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - int64_t* t = NULL; -#else - int64_t t[2 * 12]; -#endif + SP_DECL_VAR(int64_t, t, 2 * 12); int64_t* a32 = NULL; int64_t o; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (int64_t*)XMALLOC(sizeof(int64_t) * 2 * 12, NULL, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(int64_t, t, 2 * 12, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { a32 = t + 12; @@ -29730,9 +27384,7 @@ static int sp_384_mod_mul_norm_7(sp_digit* r, const sp_digit* a, const sp_digit* r[6] |= t[11] << 22U; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -29756,13 +27408,8 @@ static int sp_384_mod_mul_norm_7(sp_digit* r, const sp_digit* a, const sp_digit* static int sp_384_ecc_mulmod_7(sp_point_384* r, const sp_point_384* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_384 t[3]; - sp_digit tmp[2 * 7 * 6]; -#endif + SP_DECL_VAR(sp_point_384, t, 3); + SP_DECL_VAR(sp_digit, tmp, 2 * 7 * 6); sp_digit n; int i; int c; @@ -29773,18 +27420,8 @@ static int sp_384_ecc_mulmod_7(sp_point_384* r, const sp_point_384* g, (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 7 * 6, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_point_384, t, 3, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 7 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { XMEMSET(t, 0, sizeof(sp_point_384) * 3); @@ -29834,24 +27471,8 @@ static int sp_384_ecc_mulmod_7(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 7 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (t != NULL) -#endif - { - ForceZero(t, sizeof(sp_point_384) * 3); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 7 * 6, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_point_384, t, 3, heap, DYNAMIC_TYPE_ECC); return err; } @@ -30409,13 +28030,8 @@ static void sp_384_get_point_33_7(sp_point_384* r, const sp_point_384* table, static int sp_384_ecc_mulmod_win_add_sub_7(sp_point_384* r, const sp_point_384* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_384 t[33+2]; - sp_digit tmp[2 * 7 * 6]; -#endif + SP_DECL_VAR(sp_point_384, t, 33+2); + SP_DECL_VAR(sp_digit, tmp, 2 * 7 * 6); sp_point_384* rt = NULL; sp_point_384* p = NULL; sp_digit* negy; @@ -30427,19 +28043,8 @@ static int sp_384_ecc_mulmod_win_add_sub_7(sp_point_384* r, const sp_point_384* (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * - (33+2), heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 7 * 6, - heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, t, 33+2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 7 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 33; p = t + 33+1; @@ -30523,10 +28128,8 @@ static int sp_384_ecc_mulmod_win_add_sub_7(sp_point_384* r, const sp_point_384* } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; } @@ -30651,11 +28254,7 @@ static void sp_384_proj_to_affine_7(sp_point_384* a, sp_digit* t) static int sp_384_gen_stripe_table_7(const sp_point_384* a, sp_table_entry_384* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* t = NULL; -#else - sp_point_384 t[3]; -#endif + SP_DECL_VAR(sp_point_384, t, 3); sp_point_384* s1 = NULL; sp_point_384* s2 = NULL; int i; @@ -30664,13 +28263,7 @@ static int sp_384_gen_stripe_table_7(const sp_point_384* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_384, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -30719,9 +28312,7 @@ static int sp_384_gen_stripe_table_7(const sp_point_384* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -30793,13 +28384,8 @@ static int sp_384_ecc_mulmod_stripe_7(sp_point_384* r, const sp_point_384* g, const sp_table_entry_384* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_384 rt[2]; - sp_digit t[2 * 7 * 6]; -#endif + SP_DECL_VAR(sp_point_384, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 7 * 6); sp_point_384* p = NULL; int i; int j; @@ -30813,19 +28399,8 @@ static int sp_384_ecc_mulmod_stripe_7(sp_point_384* r, const sp_point_384* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 7 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 7 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -30879,10 +28454,8 @@ static int sp_384_ecc_mulmod_stripe_7(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -30999,20 +28572,11 @@ static int sp_384_ecc_mulmod_7(sp_point_384* r, const sp_point_384* g, #ifndef FP_ECC return sp_384_ecc_mulmod_win_add_sub_7(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 7 * 7]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 7 * 7); sp_cache_384_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 7 * 7, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 7 * 7, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -31045,9 +28609,7 @@ static int sp_384_ecc_mulmod_7(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -31066,28 +28628,12 @@ static int sp_384_ecc_mulmod_7(sp_point_384* r, const sp_point_384* g, int sp_ecc_mulmod_384(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[1]; - sp_digit k[7]; -#endif + SP_DECL_VAR(sp_point_384, point, 1); + SP_DECL_VAR(sp_digit, k, 7); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 7, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(k, 7, km); sp_384_point_from_ecc_point_7(point, gm); @@ -31098,10 +28644,8 @@ int sp_ecc_mulmod_384(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_384_point_to_ecc_point_7(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -31121,31 +28665,14 @@ int sp_ecc_mulmod_384(const mp_int* km, const ecc_point* gm, ecc_point* r, int sp_ecc_mulmod_add_384(const mp_int* km, const ecc_point* gm, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[2]; - sp_digit k[7 + 7 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_384, point, 2); + SP_DECL_VAR(sp_digit, k, 7 + 7 * 2 * 6); sp_point_384* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (7 + 7 * 2 * 6), heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 7 + 7 * 2 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 7; @@ -31176,10 +28703,8 @@ int sp_ecc_mulmod_add_384(const mp_int* km, const ecc_point* gm, err = sp_384_point_to_ecc_point_7(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -33042,28 +30567,12 @@ static int sp_384_ecc_mulmod_base_7(sp_point_384* r, const sp_digit* k, */ int sp_ecc_mulmod_base_384(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[1]; - sp_digit k[7]; -#endif + SP_DECL_VAR(sp_point_384, point, 1); + SP_DECL_VAR(sp_digit, k, 7); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 7, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(k, 7, km); @@ -33073,10 +30582,8 @@ int sp_ecc_mulmod_base_384(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_384_point_to_ecc_point_7(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -33095,31 +30602,14 @@ int sp_ecc_mulmod_base_384(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_384(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[2]; - sp_digit k[7 + 7 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_384, point, 2); + SP_DECL_VAR(sp_digit, k, 7 + 7 * 2 * 6); sp_point_384* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (7 + 7 * 2 * 6), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 7 + 7 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 7; @@ -33149,10 +30639,8 @@ int sp_ecc_mulmod_base_add_384(const mp_int* km, const ecc_point* am, err = sp_384_point_to_ecc_point_7(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -33252,17 +30740,12 @@ static int sp_384_ecc_gen_k_7(WC_RNG* rng, sp_digit* k) */ int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_DECL_VAR(sp_point_384, point, 2); #else - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - sp_point_384 point[2]; - #else - sp_point_384 point[1]; - #endif - sp_digit k[7]; + SP_DECL_VAR(sp_point_384, point, 1); #endif + SP_DECL_VAR(sp_digit, k, 7); #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN sp_point_384* infinity = NULL; #endif @@ -33271,22 +30754,12 @@ int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, DYNAMIC_TYPE_ECC); - #else - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, DYNAMIC_TYPE_ECC); - #endif - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_ALLOC_VAR(sp_point_384, point, 2, heap, DYNAMIC_TYPE_ECC); +#else + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); #endif - + SP_ALLOC_VAR(sp_digit, k, 7, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN infinity = point + 1; @@ -33316,11 +30789,9 @@ int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) err = sp_384_point_to_ecc_point_7(point, pub); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); /* point is not sensitive, so no need to zeroize */ - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -33463,34 +30934,16 @@ static void sp_384_to_bin_7(sp_digit* r, byte* a) int sp_ecc_secret_gen_384(const mp_int* priv, const ecc_point* pub, byte* out, word32* outLen, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[1]; - sp_digit k[7]; -#endif + SP_DECL_VAR(sp_point_384, point, 1); + SP_DECL_VAR(sp_digit, k, 7); int err = MP_OKAY; if (*outLen < 48U) { err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 7, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(k, 7, priv); sp_384_point_from_ecc_point_7(point, pub); @@ -33501,10 +30954,8 @@ int sp_ecc_secret_gen_384(const mp_int* priv, const ecc_point* pub, byte* out, *outLen = 48; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -33695,29 +31146,17 @@ SP_NOINLINE static void sp_384_lshift_14(sp_digit* r, const sp_digit* a, static int sp_384_div_7(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { + SP_DECL_VAR(sp_digit, t1, 4 * 7 + 3); int i; sp_digit r1; sp_digit mask; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 7 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 7 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 7 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 14 + 1; sd = t2 + 7 + 1; @@ -33741,9 +31180,7 @@ static int sp_384_div_7(const sp_digit* a, const sp_digit* d, sp_384_rshift_7(r, t1, 1); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -34007,13 +31444,8 @@ static int sp_384_calc_s_7(sp_digit* s, const sp_digit* r, sp_digit* k, int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng, const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* e = NULL; - sp_point_384* point = NULL; -#else - sp_digit e[7 * 2 * 7]; - sp_point_384 point[1]; -#endif + SP_DECL_VAR(sp_digit, e, 7 * 2 * 7); + SP_DECL_VAR(sp_point_384, point, 1); sp_digit* x = NULL; sp_digit* k = NULL; sp_digit* r = NULL; @@ -34025,21 +31457,8 @@ int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - e = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7 * 2 * 7, heap, - DYNAMIC_TYPE_ECC); - if (e == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, e, 7 * 2 * 7, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { x = e + 2 * 7; k = e + 4 * 7; @@ -34104,24 +31523,8 @@ int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng, err = sp_384_to_mp(s, sm); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (e != NULL) -#endif - { - ForceZero(e, sizeof(sp_digit) * 7 * 2 * 7); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(e, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (point != NULL) -#endif - { - ForceZero(point, sizeof(sp_point_384)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(point, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_digit, e, 7 * 2 * 7, heap, DYNAMIC_TYPE_ECC); return err; } @@ -34339,24 +31742,14 @@ static int sp_384_num_bits_7(const sp_digit* a) static int sp_384_mod_inv_7(sp_digit* r, const sp_digit* a, const sp_digit* m) { int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u = NULL; -#else - sp_digit u[7 * 4]; -#endif + SP_DECL_VAR(sp_digit, u, 7 * 4); sp_digit* v = NULL; sp_digit* b = NULL; sp_digit* d = NULL; int ut; int vt; -#ifdef WOLFSSL_SP_SMALL_STACK - u = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7 * 4, NULL, - DYNAMIC_TYPE_ECC); - if (u == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, u, 7 * 4, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { v = u + 7; b = u + 2 * 7; @@ -34438,9 +31831,7 @@ static int sp_384_mod_inv_7(sp_digit* r, const sp_digit* a, const sp_digit* m) else XMEMCPY(r, d, sizeof(sp_digit) * 7); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(u, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -34558,13 +31949,8 @@ int sp_ecc_verify_384(const byte* hash, word32 hashLen, const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* rm, const mp_int* sm, int* res, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u1 = NULL; - sp_point_384* p1 = NULL; -#else - sp_digit u1[18 * 7]; - sp_point_384 p1[2]; -#endif + SP_DECL_VAR(sp_digit, u1, 18 * 7); + SP_DECL_VAR(sp_point_384, p1, 2); sp_digit* u2 = NULL; sp_digit* s = NULL; sp_digit* tmp = NULL; @@ -34573,21 +31959,8 @@ int sp_ecc_verify_384(const byte* hash, word32 hashLen, const mp_int* pX, sp_int64 c = 0; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p1 = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (p1 == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - u1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 7, heap, - DYNAMIC_TYPE_ECC); - if (u1 == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, u1, 18 * 7, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p1, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { u2 = u1 + 2 * 7; s = u1 + 4 * 7; @@ -34644,11 +32017,8 @@ int sp_ecc_verify_384(const byte* hash, word32 hashLen, const mp_int* pX, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u1, heap, DYNAMIC_TYPE_ECC); - XFREE(p1, heap, DYNAMIC_TYPE_ECC); -#endif - + SP_FREE_VAR(p1, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(u1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -34814,21 +32184,13 @@ int sp_ecc_verify_384_nb(sp_ecc_ctx_t* sp_ctx, const byte* hash, static int sp_384_ecc_is_point_7(const sp_point_384* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[7 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 7 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 7 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 7; @@ -34852,9 +32214,7 @@ static int sp_384_ecc_is_point_7(const sp_point_384* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -34868,21 +32228,11 @@ static int sp_384_ecc_is_point_7(const sp_point_384* point, */ int sp_ecc_is_point_384(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* pub = NULL; -#else - sp_point_384 pub[1]; -#endif + SP_DECL_VAR(sp_point_384, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_384*)XMALLOC(sizeof(sp_point_384), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_384, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(pub->x, 7, pX); sp_384_from_mp(pub->y, 7, pY); @@ -34891,9 +32241,7 @@ int sp_ecc_is_point_384(const mp_int* pX, const mp_int* pY) err = sp_384_ecc_is_point_7(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -34912,13 +32260,8 @@ int sp_ecc_is_point_384(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_384(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_384* pub = NULL; -#else - sp_digit priv[7]; - sp_point_384 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 7); + SP_DECL_VAR(sp_point_384, pub, 2); sp_point_384* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -34933,21 +32276,8 @@ int sp_ecc_check_key_384(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 7, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -34999,10 +32329,8 @@ int sp_ecc_check_key_384(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } @@ -35026,32 +32354,13 @@ int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_384* p = NULL; -#else - sp_digit tmp[2 * 7 * 6]; - sp_point_384 p[2]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 7 * 6); + SP_DECL_VAR(sp_point_384, p, 2); sp_point_384* q = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 7 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 7 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p, 2, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { q = p + 1; @@ -35079,10 +32388,8 @@ int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_384_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -35101,30 +32408,12 @@ int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_384* p = NULL; -#else - sp_digit tmp[2 * 7 * 2]; - sp_point_384 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 7 * 2); + SP_DECL_VAR(sp_point_384, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 7 * 2, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 7 * 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(p->x, 7, pX); sp_384_from_mp(p->y, 7, pY); @@ -35145,10 +32434,8 @@ int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_384_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -35163,30 +32450,13 @@ int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, */ int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_384* p = NULL; -#else - sp_digit tmp[2 * 7 * 6]; - sp_point_384 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 7 * 6); + SP_DECL_VAR(sp_point_384, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 7 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 7 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(p->x, 7, pX); sp_384_from_mp(p->y, 7, pY); @@ -35207,10 +32477,8 @@ int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ) err = sp_384_to_mp(p->z, pZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -35223,23 +32491,14 @@ int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ) */ static int sp_384_mont_sqrt_7(sp_digit* y) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[5 * 2 * 7]; -#endif + SP_DECL_VAR(sp_digit, t1, 5 * 2 * 7); sp_digit* t2 = NULL; sp_digit* t3 = NULL; sp_digit* t4 = NULL; sp_digit* t5 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 5 * 2 * 7, NULL, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, t1, 5 * 2 * 7, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 7; t3 = t1 + 4 * 7; @@ -35304,9 +32563,7 @@ static int sp_384_mont_sqrt_7(sp_digit* y) } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -35321,20 +32578,11 @@ static int sp_384_mont_sqrt_7(sp_digit* y) */ int sp_ecc_uncompress_384(mp_int* xm, int odd, mp_int* ym) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* x = NULL; -#else - sp_digit x[4 * 7]; -#endif + SP_DECL_VAR(sp_digit, x, 4 * 7); sp_digit* y = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - x = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 7, NULL, DYNAMIC_TYPE_ECC); - if (x == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, x, 4 * 7, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { y = x + 2 * 7; @@ -35369,9 +32617,7 @@ int sp_ecc_uncompress_384(mp_int* xm, int odd, mp_int* ym) err = sp_384_to_mp(y, ym); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(x, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(x, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -37154,13 +34400,8 @@ static int sp_521_mod_mul_norm_9(sp_digit* r, const sp_digit* a, const sp_digit* static int sp_521_ecc_mulmod_9(sp_point_521* r, const sp_point_521* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_521 t[3]; - sp_digit tmp[2 * 9 * 6]; -#endif + SP_DECL_VAR(sp_point_521, t, 3); + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 6); sp_digit n; int i; int c; @@ -37171,18 +34412,8 @@ static int sp_521_ecc_mulmod_9(sp_point_521* r, const sp_point_521* g, (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_point_521, t, 3, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { XMEMSET(t, 0, sizeof(sp_point_521) * 3); @@ -37232,24 +34463,8 @@ static int sp_521_ecc_mulmod_9(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 9 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (t != NULL) -#endif - { - ForceZero(t, sizeof(sp_point_521) * 3); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_point_521, t, 3, heap, DYNAMIC_TYPE_ECC); return err; } @@ -37823,13 +35038,8 @@ static void sp_521_get_point_33_9(sp_point_521* r, const sp_point_521* table, static int sp_521_ecc_mulmod_win_add_sub_9(sp_point_521* r, const sp_point_521* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_521 t[33+2]; - sp_digit tmp[2 * 9 * 6]; -#endif + SP_DECL_VAR(sp_point_521, t, 33+2); + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 6); sp_point_521* rt = NULL; sp_point_521* p = NULL; sp_digit* negy; @@ -37841,19 +35051,8 @@ static int sp_521_ecc_mulmod_win_add_sub_9(sp_point_521* r, const sp_point_521* (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * - (33+2), heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, - heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, t, 33+2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 33; p = t + 33+1; @@ -37937,10 +35136,8 @@ static int sp_521_ecc_mulmod_win_add_sub_9(sp_point_521* r, const sp_point_521* } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; } @@ -38065,11 +35262,7 @@ static void sp_521_proj_to_affine_9(sp_point_521* a, sp_digit* t) static int sp_521_gen_stripe_table_9(const sp_point_521* a, sp_table_entry_521* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* t = NULL; -#else - sp_point_521 t[3]; -#endif + SP_DECL_VAR(sp_point_521, t, 3); sp_point_521* s1 = NULL; sp_point_521* s2 = NULL; int i; @@ -38078,13 +35271,7 @@ static int sp_521_gen_stripe_table_9(const sp_point_521* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_521, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -38133,9 +35320,7 @@ static int sp_521_gen_stripe_table_9(const sp_point_521* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -38215,13 +35400,8 @@ static int sp_521_ecc_mulmod_stripe_9(sp_point_521* r, const sp_point_521* g, const sp_table_entry_521* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_521 rt[2]; - sp_digit t[2 * 9 * 6]; -#endif + SP_DECL_VAR(sp_point_521, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 9 * 6); sp_point_521* p = NULL; int i; int j; @@ -38235,19 +35415,8 @@ static int sp_521_ecc_mulmod_stripe_9(sp_point_521* r, const sp_point_521* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -38301,10 +35470,8 @@ static int sp_521_ecc_mulmod_stripe_9(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -38421,20 +35588,11 @@ static int sp_521_ecc_mulmod_9(sp_point_521* r, const sp_point_521* g, #ifndef FP_ECC return sp_521_ecc_mulmod_win_add_sub_9(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 9 * 6]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 6); sp_cache_521_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -38467,9 +35625,7 @@ static int sp_521_ecc_mulmod_9(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -38488,28 +35644,12 @@ static int sp_521_ecc_mulmod_9(sp_point_521* r, const sp_point_521* g, int sp_ecc_mulmod_521(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[1]; - sp_digit k[9]; -#endif + SP_DECL_VAR(sp_point_521, point, 1); + SP_DECL_VAR(sp_digit, k, 9); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 9, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(k, 9, km); sp_521_point_from_ecc_point_9(point, gm); @@ -38520,10 +35660,8 @@ int sp_ecc_mulmod_521(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_521_point_to_ecc_point_9(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -38543,31 +35681,14 @@ int sp_ecc_mulmod_521(const mp_int* km, const ecc_point* gm, ecc_point* r, int sp_ecc_mulmod_add_521(const mp_int* km, const ecc_point* gm, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[2]; - sp_digit k[9 + 9 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_521, point, 2); + SP_DECL_VAR(sp_digit, k, 9 + 9 * 2 * 6); sp_point_521* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (9 + 9 * 2 * 6), heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 9 + 9 * 2 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 9; @@ -38598,10 +35719,8 @@ int sp_ecc_mulmod_add_521(const mp_int* km, const ecc_point* gm, err = sp_521_point_to_ecc_point_9(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -40464,28 +37583,12 @@ static int sp_521_ecc_mulmod_base_9(sp_point_521* r, const sp_digit* k, */ int sp_ecc_mulmod_base_521(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[1]; - sp_digit k[9]; -#endif + SP_DECL_VAR(sp_point_521, point, 1); + SP_DECL_VAR(sp_digit, k, 9); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 9, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(k, 9, km); @@ -40495,10 +37598,8 @@ int sp_ecc_mulmod_base_521(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_521_point_to_ecc_point_9(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -40517,31 +37618,14 @@ int sp_ecc_mulmod_base_521(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_521(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[2]; - sp_digit k[9 + 9 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_521, point, 2); + SP_DECL_VAR(sp_digit, k, 9 + 9 * 2 * 6); sp_point_521* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (9 + 9 * 2 * 6), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 9 + 9 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 9; @@ -40571,10 +37655,8 @@ int sp_ecc_mulmod_base_add_521(const mp_int* km, const ecc_point* am, err = sp_521_point_to_ecc_point_9(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -40675,17 +37757,12 @@ static int sp_521_ecc_gen_k_9(WC_RNG* rng, sp_digit* k) */ int sp_ecc_make_key_521(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_DECL_VAR(sp_point_521, point, 2); #else - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - sp_point_521 point[2]; - #else - sp_point_521 point[1]; - #endif - sp_digit k[9]; + SP_DECL_VAR(sp_point_521, point, 1); #endif + SP_DECL_VAR(sp_digit, k, 9); #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN sp_point_521* infinity = NULL; #endif @@ -40694,22 +37771,12 @@ int sp_ecc_make_key_521(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, DYNAMIC_TYPE_ECC); - #else - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, DYNAMIC_TYPE_ECC); - #endif - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_ALLOC_VAR(sp_point_521, point, 2, heap, DYNAMIC_TYPE_ECC); +#else + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); #endif - + SP_ALLOC_VAR(sp_digit, k, 9, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN infinity = point + 1; @@ -40739,11 +37806,9 @@ int sp_ecc_make_key_521(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) err = sp_521_point_to_ecc_point_9(point, pub); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); /* point is not sensitive, so no need to zeroize */ - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -40886,34 +37951,16 @@ static void sp_521_to_bin_9(sp_digit* r, byte* a) int sp_ecc_secret_gen_521(const mp_int* priv, const ecc_point* pub, byte* out, word32* outLen, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[1]; - sp_digit k[9]; -#endif + SP_DECL_VAR(sp_point_521, point, 1); + SP_DECL_VAR(sp_digit, k, 9); int err = MP_OKAY; if (*outLen < 65U) { err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 9, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(k, 9, priv); sp_521_point_from_ecc_point_9(point, pub); @@ -40924,10 +37971,8 @@ int sp_ecc_secret_gen_521(const mp_int* priv, const ecc_point* pub, byte* out, *outLen = 66; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -41124,29 +38169,17 @@ SP_NOINLINE static void sp_521_lshift_18(sp_digit* r, const sp_digit* a, static int sp_521_div_9(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { + SP_DECL_VAR(sp_digit, t1, 4 * 9 + 3); int i; sp_digit r1; sp_digit mask; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 9 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 9 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 9 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 18 + 1; sd = t2 + 9 + 1; @@ -41170,9 +38203,7 @@ static int sp_521_div_9(const sp_digit* a, const sp_digit* d, sp_521_rshift_9(r, t1, 1); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -41451,13 +38482,8 @@ static int sp_521_calc_s_9(sp_digit* s, const sp_digit* r, sp_digit* k, int sp_ecc_sign_521(const byte* hash, word32 hashLen, WC_RNG* rng, const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* e = NULL; - sp_point_521* point = NULL; -#else - sp_digit e[7 * 2 * 9]; - sp_point_521 point[1]; -#endif + SP_DECL_VAR(sp_digit, e, 7 * 2 * 9); + SP_DECL_VAR(sp_point_521, point, 1); sp_digit* x = NULL; sp_digit* k = NULL; sp_digit* r = NULL; @@ -41469,21 +38495,8 @@ int sp_ecc_sign_521(const byte* hash, word32 hashLen, WC_RNG* rng, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - e = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7 * 2 * 9, heap, - DYNAMIC_TYPE_ECC); - if (e == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, e, 7 * 2 * 9, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { x = e + 2 * 9; k = e + 4 * 9; @@ -41554,24 +38567,8 @@ int sp_ecc_sign_521(const byte* hash, word32 hashLen, WC_RNG* rng, err = sp_521_to_mp(s, sm); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (e != NULL) -#endif - { - ForceZero(e, sizeof(sp_digit) * 7 * 2 * 9); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(e, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (point != NULL) -#endif - { - ForceZero(point, sizeof(sp_point_521)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(point, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_digit, e, 7 * 2 * 9, heap, DYNAMIC_TYPE_ECC); return err; } @@ -41793,24 +38790,14 @@ static int sp_521_num_bits_9(const sp_digit* a) static int sp_521_mod_inv_9(sp_digit* r, const sp_digit* a, const sp_digit* m) { int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u = NULL; -#else - sp_digit u[9 * 4]; -#endif + SP_DECL_VAR(sp_digit, u, 9 * 4); sp_digit* v = NULL; sp_digit* b = NULL; sp_digit* d = NULL; int ut; int vt; -#ifdef WOLFSSL_SP_SMALL_STACK - u = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9 * 4, NULL, - DYNAMIC_TYPE_ECC); - if (u == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, u, 9 * 4, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { v = u + 9; b = u + 2 * 9; @@ -41892,9 +38879,7 @@ static int sp_521_mod_inv_9(sp_digit* r, const sp_digit* a, const sp_digit* m) else XMEMCPY(r, d, sizeof(sp_digit) * 9); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(u, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -42014,13 +38999,8 @@ int sp_ecc_verify_521(const byte* hash, word32 hashLen, const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* rm, const mp_int* sm, int* res, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u1 = NULL; - sp_point_521* p1 = NULL; -#else - sp_digit u1[18 * 9]; - sp_point_521 p1[2]; -#endif + SP_DECL_VAR(sp_digit, u1, 18 * 9); + SP_DECL_VAR(sp_point_521, p1, 2); sp_digit* u2 = NULL; sp_digit* s = NULL; sp_digit* tmp = NULL; @@ -42029,21 +39009,8 @@ int sp_ecc_verify_521(const byte* hash, word32 hashLen, const mp_int* pX, sp_int64 c = 0; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p1 = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (p1 == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - u1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 9, heap, - DYNAMIC_TYPE_ECC); - if (u1 == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, u1, 18 * 9, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p1, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { u2 = u1 + 2 * 9; s = u1 + 4 * 9; @@ -42105,11 +39072,8 @@ int sp_ecc_verify_521(const byte* hash, word32 hashLen, const mp_int* pX, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u1, heap, DYNAMIC_TYPE_ECC); - XFREE(p1, heap, DYNAMIC_TYPE_ECC); -#endif - + SP_FREE_VAR(p1, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(u1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -42279,21 +39243,13 @@ int sp_ecc_verify_521_nb(sp_ecc_ctx_t* sp_ctx, const byte* hash, static int sp_521_ecc_is_point_9(const sp_point_521* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[9 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 9 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 9 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 9; @@ -42317,9 +39273,7 @@ static int sp_521_ecc_is_point_9(const sp_point_521* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -42333,21 +39287,11 @@ static int sp_521_ecc_is_point_9(const sp_point_521* point, */ int sp_ecc_is_point_521(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* pub = NULL; -#else - sp_point_521 pub[1]; -#endif + SP_DECL_VAR(sp_point_521, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_521*)XMALLOC(sizeof(sp_point_521), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_521, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(pub->x, 9, pX); sp_521_from_mp(pub->y, 9, pY); @@ -42356,9 +39300,7 @@ int sp_ecc_is_point_521(const mp_int* pX, const mp_int* pY) err = sp_521_ecc_is_point_9(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -42377,13 +39319,8 @@ int sp_ecc_is_point_521(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_521(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_521* pub = NULL; -#else - sp_digit priv[9]; - sp_point_521 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 9); + SP_DECL_VAR(sp_point_521, pub, 2); sp_point_521* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -42398,21 +39335,8 @@ int sp_ecc_check_key_521(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 9, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -42464,10 +39388,8 @@ int sp_ecc_check_key_521(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } @@ -42491,32 +39413,13 @@ int sp_ecc_proj_add_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_521* p = NULL; -#else - sp_digit tmp[2 * 9 * 6]; - sp_point_521 p[2]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 6); + SP_DECL_VAR(sp_point_521, p, 2); sp_point_521* q = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p, 2, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { q = p + 1; @@ -42544,10 +39447,8 @@ int sp_ecc_proj_add_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_521_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -42566,30 +39467,12 @@ int sp_ecc_proj_add_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, int sp_ecc_proj_dbl_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_521* p = NULL; -#else - sp_digit tmp[2 * 9 * 2]; - sp_point_521 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 2); + SP_DECL_VAR(sp_point_521, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 2, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(p->x, 9, pX); sp_521_from_mp(p->y, 9, pY); @@ -42610,10 +39493,8 @@ int sp_ecc_proj_dbl_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_521_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -42628,30 +39509,13 @@ int sp_ecc_proj_dbl_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, */ int sp_ecc_map_521(mp_int* pX, mp_int* pY, mp_int* pZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_521* p = NULL; -#else - sp_digit tmp[2 * 9 * 5]; - sp_point_521 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 5); + SP_DECL_VAR(sp_point_521, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 5, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 5, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(p->x, 9, pX); sp_521_from_mp(p->y, 9, pY); @@ -42672,10 +39536,8 @@ int sp_ecc_map_521(mp_int* pX, mp_int* pY, mp_int* pZ) err = sp_521_to_mp(p->z, pZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -42695,19 +39557,10 @@ static const word64 p521_sqrt_power[9] = { */ static int sp_521_mont_sqrt_9(sp_digit* y) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t = NULL; -#else - sp_digit t[2 * 9]; -#endif + SP_DECL_VAR(sp_digit, t, 2 * 9); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9, NULL, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, t, 2 * 9, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { { @@ -42723,9 +39576,7 @@ static int sp_521_mont_sqrt_9(sp_digit* y) } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -42740,20 +39591,11 @@ static int sp_521_mont_sqrt_9(sp_digit* y) */ int sp_ecc_uncompress_521(mp_int* xm, int odd, mp_int* ym) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* x = NULL; -#else - sp_digit x[4 * 9]; -#endif + SP_DECL_VAR(sp_digit, x, 4 * 9); sp_digit* y = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - x = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 9, NULL, DYNAMIC_TYPE_ECC); - if (x == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, x, 4 * 9, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { y = x + 2 * 9; @@ -42788,9 +39630,7 @@ int sp_ecc_uncompress_521(mp_int* xm, int odd, mp_int* ym) err = sp_521_to_mp(y, ym); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(x, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(x, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -43638,30 +40478,16 @@ static int sp_1024_div_18(const sp_digit* a, const sp_digit* d, const sp_digit* m, sp_digit* r) { int i; -#ifndef WOLFSSL_SP_DIV_64 -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 18 + 3); sp_digit dv; sp_digit r1; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 18 + 3]; -#endif sp_digit* t2 = NULL; sp_digit* sd = NULL; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * (4 * 18 + 3), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (t1 == NULL) - err = MEMORY_E; -#endif - - (void)m; - + SP_ALLOC_VAR(sp_digit, t1, 4 * 18 + 3, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { t2 = t1 + 36 + 1; sd = t2 + 18 + 1; @@ -43704,9 +40530,7 @@ static int sp_1024_div_18(const sp_digit* a, const sp_digit* d, sp_1024_rshift_18(r, r, 2); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -45009,13 +41833,8 @@ static int sp_1024_proj_point_add_18_nb(sp_ecc_ctx_t* sp_ctx, sp_point_1024* r, static int sp_1024_ecc_mulmod_18(sp_point_1024* r, const sp_point_1024* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_1024 t[3]; - sp_digit tmp[2 * 18 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, t, 3); + SP_DECL_VAR(sp_digit, tmp, 2 * 18 * 37); sp_digit n; int i; int c; @@ -45026,18 +41845,8 @@ static int sp_1024_ecc_mulmod_18(sp_point_1024* r, const sp_point_1024* g, (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 18 * 37, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_point_1024, t, 3, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 18 * 37, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { XMEMSET(t, 0, sizeof(sp_point_1024) * 3); @@ -45087,24 +41896,8 @@ static int sp_1024_ecc_mulmod_18(sp_point_1024* r, const sp_point_1024* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 18 * 37); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (t != NULL) -#endif - { - ForceZero(t, sizeof(sp_point_1024) * 3); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 18 * 37, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_point_1024, t, 3, heap, DYNAMIC_TYPE_ECC); return err; } @@ -45632,13 +42425,8 @@ static void sp_1024_ecc_recode_7_18(const sp_digit* k, ecc_recode_1024* v) static int sp_1024_ecc_mulmod_win_add_sub_18(sp_point_1024* r, const sp_point_1024* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_1024 t[65+2]; - sp_digit tmp[2 * 18 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, t, 65+2); + SP_DECL_VAR(sp_digit, tmp, 2 * 18 * 37); sp_point_1024* rt = NULL; sp_point_1024* p = NULL; sp_digit* negy; @@ -45650,19 +42438,8 @@ static int sp_1024_ecc_mulmod_win_add_sub_18(sp_point_1024* r, const sp_point_10 (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * - (65+2), heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 18 * 37, - heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, t, 65+2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 18 * 37, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 65; p = t + 65+1; @@ -45750,10 +42527,8 @@ static int sp_1024_ecc_mulmod_win_add_sub_18(sp_point_1024* r, const sp_point_10 } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; } @@ -45878,11 +42653,7 @@ static void sp_1024_proj_to_affine_18(sp_point_1024* a, sp_digit* t) static int sp_1024_gen_stripe_table_18(const sp_point_1024* a, sp_table_entry_1024* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* t = NULL; -#else - sp_point_1024 t[3]; -#endif + SP_DECL_VAR(sp_point_1024, t, 3); sp_point_1024* s1 = NULL; sp_point_1024* s2 = NULL; int i; @@ -45891,13 +42662,7 @@ static int sp_1024_gen_stripe_table_18(const sp_point_1024* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_1024, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -45946,9 +42711,7 @@ static int sp_1024_gen_stripe_table_18(const sp_point_1024* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -45974,13 +42737,8 @@ static int sp_1024_ecc_mulmod_stripe_18(sp_point_1024* r, const sp_point_1024* g const sp_table_entry_1024* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_1024 rt[2]; - sp_digit t[2 * 18 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 18 * 37); sp_point_1024* p = NULL; int i; int j; @@ -45994,19 +42752,8 @@ static int sp_1024_ecc_mulmod_stripe_18(sp_point_1024* r, const sp_point_1024* g (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 18 * 37, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 18 * 37, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -46045,10 +42792,8 @@ static int sp_1024_ecc_mulmod_stripe_18(sp_point_1024* r, const sp_point_1024* g } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -46165,20 +42910,11 @@ static int sp_1024_ecc_mulmod_18(sp_point_1024* r, const sp_point_1024* g, #ifndef FP_ECC return sp_1024_ecc_mulmod_win_add_sub_18(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 18 * 38]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 18 * 38); sp_cache_1024_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 18 * 38, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 18 * 38, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -46211,9 +42947,7 @@ static int sp_1024_ecc_mulmod_18(sp_point_1024* r, const sp_point_1024* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -46232,28 +42966,12 @@ static int sp_1024_ecc_mulmod_18(sp_point_1024* r, const sp_point_1024* g, int sp_ecc_mulmod_1024(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[1]; - sp_digit k[18]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, k, 18); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 18, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(k, 18, km); sp_1024_point_from_ecc_point_18(point, gm); @@ -46264,10 +42982,8 @@ int sp_ecc_mulmod_1024(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_1024_point_to_ecc_point_18(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -49662,28 +46378,12 @@ static int sp_1024_ecc_mulmod_base_18(sp_point_1024* r, const sp_digit* k, */ int sp_ecc_mulmod_base_1024(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[1]; - sp_digit k[18]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, k, 18); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 18, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(k, 18, km); @@ -49693,10 +46393,8 @@ int sp_ecc_mulmod_base_1024(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_1024_point_to_ecc_point_18(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -49715,31 +46413,14 @@ int sp_ecc_mulmod_base_1024(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[2]; - sp_digit k[18 + 18 * 2 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, point, 2); + SP_DECL_VAR(sp_digit, k, 18 + 18 * 2 * 37); sp_point_1024* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (18 + 18 * 2 * 37), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 18 + 18 * 2 * 37, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 18; @@ -49769,10 +46450,8 @@ int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am, err = sp_1024_point_to_ecc_point_18(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -49790,13 +46469,8 @@ int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am, int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* t = NULL; -#else - sp_point_1024 point[1]; - sp_digit t[38 * 2 * 18]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, t, 38 * 2 * 18); int err = MP_OKAY; if ((gm == NULL) || (len == NULL)) { @@ -49811,21 +46485,8 @@ int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 38 * 2 * 18, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 38 * 2 * 18, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_point_from_ecc_point_18(point, gm); err = sp_1024_gen_stripe_table_18(point, @@ -49835,10 +46496,8 @@ int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, *len = sizeof(sp_table_entry_1024) * 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -49891,28 +46550,12 @@ int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, int sp_ecc_mulmod_table_1024(const mp_int* km, const ecc_point* gm, byte* table, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[1]; - sp_digit k[18]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, k, 18); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) { - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18, heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 18, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(k, 18, km); sp_1024_point_from_ecc_point_18(point, gm); @@ -49929,10 +46572,8 @@ int sp_ecc_mulmod_table_1024(const mp_int* km, const ecc_point* gm, byte* table, err = sp_1024_point_to_ecc_point_18(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); return err; } @@ -53125,22 +49766,14 @@ static void sp_1024_from_bin(sp_digit* r, int size, const byte* a, int n) static int sp_1024_ecc_is_point_18(const sp_point_1024* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[18 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 18 * 4); sp_digit* t2 = NULL; sp_int64 n; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 18 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 18; @@ -53167,9 +49800,7 @@ static int sp_1024_ecc_is_point_18(const sp_point_1024* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -53183,21 +49814,11 @@ static int sp_1024_ecc_is_point_18(const sp_point_1024* point, */ int sp_ecc_is_point_1024(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* pub = NULL; -#else - sp_point_1024 pub[1]; -#endif + SP_DECL_VAR(sp_point_1024, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_1024, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(pub->x, 18, pX); sp_1024_from_mp(pub->y, 18, pY); @@ -53206,9 +49827,7 @@ int sp_ecc_is_point_1024(const mp_int* pX, const mp_int* pY) err = sp_1024_ecc_is_point_18(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -53227,13 +49846,8 @@ int sp_ecc_is_point_1024(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_1024(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_1024* pub = NULL; -#else - sp_digit priv[18]; - sp_point_1024 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 18); + SP_DECL_VAR(sp_point_1024, pub, 2); sp_point_1024* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -53248,21 +49862,8 @@ int sp_ecc_check_key_1024(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 18, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_1024, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -53314,10 +49915,8 @@ int sp_ecc_check_key_1024(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } diff --git a/wolfcrypt/src/sp_cortexm.c b/wolfcrypt/src/sp_cortexm.c index bb4937e60..7996078eb 100644 --- a/wolfcrypt/src/sp_cortexm.c +++ b/wolfcrypt/src/sp_cortexm.c @@ -57,6 +57,57 @@ #define __volatile__ volatile #endif +#ifdef WOLFSSL_SP_SMALL_STACK + #define SP_DECL_VAR(TYPE, NAME, CNT) \ + TYPE* NAME = NULL + #define SP_ALLOC_VAR(TYPE, NAME, CNT, HEAP, DT) \ + if (err == MP_OKAY) { \ + (NAME) = (TYPE*)XMALLOC(sizeof(TYPE) * (CNT), (HEAP), DT); \ + if ((NAME) == NULL) { \ + err = MEMORY_E; \ + } \ + } + + #define SP_VAR_OK(NAME) ((NAME) != NULL) + + #define SP_FREE_VAR(NAME, HEAP, DT) \ + XFREE(NAME, (HEAP), DT) + #define SP_ZEROFREE_VAR(TYPE, NAME, CNT, HEAP, DT) \ + do { \ + if ((NAME) != NULL) { \ + ForceZero(NAME, sizeof(TYPE) * (CNT)); \ + } \ + SP_FREE_VAR(NAME, HEAP, DT); \ + } while (0) + #define SP_ZEROFREE_VAR_ALT(TYPE, NAME, FZ_NAME, CNT, HEAP, DT) \ + do { \ + if ((FZ_NAME) != NULL) { \ + ForceZero(FZ_NAME, sizeof(TYPE) * (CNT)); \ + } \ + SP_FREE_VAR(NAME, HEAP, DT); \ + } while (0) +#else + #define SP_DECL_VAR(TYPE, NAME, CNT) \ + TYPE NAME[CNT] + #define SP_ALLOC_VAR(TYPE, NAME, CNT, HEAP, DT) \ + WC_DO_NOTHING + #define SP_VAR_OK(NAME) (1) + #define SP_FREE_VAR(NAME, HEAP, DT) \ + WC_DO_NOTHING + #define SP_ZEROFREE_VAR(TYPE, NAME, CNT, HEAP, DT) \ + do { \ + if ((NAME) != NULL) { \ + ForceZero(NAME, sizeof(TYPE) * (CNT)); \ + } \ + } while (0) + #define SP_ZEROFREE_VAR_ALT(TYPE, NAME, FZ_NAME, CNT, HEAP, DT) \ + do { \ + if ((FZ_NAME) != NULL) { \ + ForceZero(FZ_NAME, sizeof(TYPE) * (CNT)); \ + } \ + } while (0) +#endif + #ifdef WOLFSSL_SP_ARM_CORTEX_M_ASM #define SP_PRINT_NUM(var, name, total, words, bits) \ do { \ @@ -5307,11 +5358,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[16 * 64]; -#endif + SP_DECL_VAR(sp_digit, td, 16 * 64); sp_digit* t[16]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -5326,15 +5373,7 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 64), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 16 * 64, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) { @@ -5437,9 +5476,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -5458,11 +5495,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[32 * 64]; -#endif + SP_DECL_VAR(sp_digit, td, 32 * 64); sp_digit* t[32]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -5477,15 +5510,7 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 64), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 32 * 64, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) { @@ -5605,9 +5630,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -8460,11 +8483,7 @@ static WC_INLINE int sp_2048_mod_64(sp_digit* r, const sp_digit* a, const sp_dig 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[8 * 128]; -#endif + SP_DECL_VAR(sp_digit, td, 8 * 128); sp_digit* t[8]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -8479,15 +8498,7 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 128), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 8 * 128, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<8; i++) { @@ -8581,9 +8592,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -8602,11 +8611,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[16 * 128]; -#endif + SP_DECL_VAR(sp_digit, td, 16 * 128); sp_digit* t[16]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -8621,15 +8626,7 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 128), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 16 * 128, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) { @@ -8732,9 +8729,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -8758,11 +8753,7 @@ static int sp_2048_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[64 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 64 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit *ah = NULL; @@ -8780,15 +8771,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 64 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 64 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { ah = a + 64; r = a + 64 * 2; @@ -8895,9 +8878,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; } @@ -9130,11 +9111,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, const mp_int* qim, const mp_int* mm, byte* out, word32* outLen) { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[64 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 64 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -9164,15 +9141,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 64 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 64 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 64; m = a + 128; @@ -9189,25 +9158,11 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 64); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 64, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[32 * 11]; -#endif + SP_DECL_VAR(sp_digit, a, 32 * 11); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -9238,15 +9193,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 11, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 32 * 11, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 64 * 2; q = p + 32; @@ -9286,15 +9233,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 32 * 11); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 32 * 11, NULL, DYNAMIC_TYPE_RSA); #endif /* SP_RSA_PRIVATE_EXP_D || RSA_LOW_MEM */ return err; } @@ -9841,11 +9780,7 @@ WC_OMIT_FRAME_POINTER static void sp_2048_lshift_64(sp_digit* r, static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[193]; -#endif + SP_DECL_VAR(sp_digit, td, 193); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -9861,15 +9796,7 @@ static int sp_2048_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 193, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 193, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 128; @@ -9946,9 +9873,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -17101,11 +17026,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[16 * 96]; -#endif + SP_DECL_VAR(sp_digit, td, 16 * 96); sp_digit* t[16]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -17120,15 +17041,7 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 96), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 16 * 96, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) { @@ -17231,9 +17144,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -17252,11 +17163,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[32 * 96]; -#endif + SP_DECL_VAR(sp_digit, td, 32 * 96); sp_digit* t[32]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -17271,15 +17178,7 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (32 * 96), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 32 * 96, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) { @@ -17399,9 +17298,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -21190,11 +21087,7 @@ static WC_INLINE int sp_3072_mod_96(sp_digit* r, const sp_digit* a, const sp_dig 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[8 * 192]; -#endif + SP_DECL_VAR(sp_digit, td, 8 * 192); sp_digit* t[8]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -21209,15 +21102,7 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 192), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 8 * 192, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<8; i++) { @@ -21311,9 +21196,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -21332,11 +21215,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[16 * 192]; -#endif + SP_DECL_VAR(sp_digit, td, 16 * 192); sp_digit* t[16]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -21351,15 +21230,7 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 192), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 16 * 192, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) { @@ -21462,9 +21333,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -21488,11 +21357,7 @@ static int sp_3072_mod_exp_96(sp_digit* r, const sp_digit* a, const sp_digit* e, int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[96 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 96 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit *ah = NULL; @@ -21510,15 +21375,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 96 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 96 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { ah = a + 96; r = a + 96 * 2; @@ -21625,9 +21482,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; } @@ -21916,11 +21771,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, const mp_int* qim, const mp_int* mm, byte* out, word32* outLen) { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[96 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 96 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -21950,15 +21801,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 96 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 96 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 96; m = a + 192; @@ -21975,25 +21818,11 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 96); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 96, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[48 * 11]; -#endif + SP_DECL_VAR(sp_digit, a, 48 * 11); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -22024,15 +21853,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 48 * 11, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 48 * 11, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 96 * 2; q = p + 48; @@ -22072,15 +21893,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 48 * 11); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 48 * 11, NULL, DYNAMIC_TYPE_RSA); #endif /* SP_RSA_PRIVATE_EXP_D || RSA_LOW_MEM */ return err; } @@ -22819,11 +22632,7 @@ WC_OMIT_FRAME_POINTER static void sp_3072_lshift_96(sp_digit* r, static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[289]; -#endif + SP_DECL_VAR(sp_digit, td, 289); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -22839,15 +22648,7 @@ static int sp_3072_mod_exp_2_96(sp_digit* r, const sp_digit* e, int bits, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 289, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 289, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 192; @@ -22924,9 +22725,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -29635,11 +29434,7 @@ static WC_INLINE int sp_4096_mod_128(sp_digit* r, const sp_digit* a, const sp_di 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[8 * 256]; -#endif + SP_DECL_VAR(sp_digit, td, 8 * 256); sp_digit* t[8]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -29654,15 +29449,7 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (8 * 256), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 8 * 256, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<8; i++) { @@ -29756,9 +29543,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -29777,11 +29562,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[16 * 256]; -#endif + SP_DECL_VAR(sp_digit, td, 16 * 256); sp_digit* t[16]; sp_digit* norm = NULL; sp_digit mp = 1; @@ -29796,15 +29577,7 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (16 * 256), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 16 * 256, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) { @@ -29907,9 +29680,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -29933,11 +29704,7 @@ static int sp_4096_mod_exp_128(sp_digit* r, const sp_digit* a, const sp_digit* e int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[128 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 128 * 5); sp_digit* m = NULL; sp_digit* r = NULL; sp_digit *ah = NULL; @@ -29955,15 +29722,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 128 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 128 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { ah = a + 128; r = a + 128 * 2; @@ -30070,9 +29829,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; } @@ -30417,11 +30174,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, const mp_int* qim, const mp_int* mm, byte* out, word32* outLen) { #if defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM) -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[128 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 128 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -30451,15 +30204,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 128 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 128 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 128; m = a + 256; @@ -30476,25 +30221,11 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only "a" and "r" are sensitive and need zeroized (same pointer) */ - if (a != NULL) - ForceZero(a, sizeof(sp_digit) * 128); -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); -#endif - } + SP_ZEROFREE_VAR_ALT(sp_digit, d, a, 128, NULL, DYNAMIC_TYPE_RSA); return err; #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[64 * 11]; -#endif + SP_DECL_VAR(sp_digit, a, 64 * 11); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -30525,15 +30256,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 64 * 11, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 64 * 11, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 128 * 2; q = p + 64; @@ -30573,15 +30296,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 64 * 11); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 64 * 11, NULL, DYNAMIC_TYPE_RSA); #endif /* SP_RSA_PRIVATE_EXP_D || RSA_LOW_MEM */ return err; } @@ -31512,11 +31227,7 @@ WC_OMIT_FRAME_POINTER static void sp_4096_lshift_128(sp_digit* r, static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[385]; -#endif + SP_DECL_VAR(sp_digit, td, 385); sp_digit* norm = NULL; sp_digit* tmp = NULL; sp_digit mp = 1; @@ -31532,15 +31243,7 @@ static int sp_4096_mod_exp_2_128(sp_digit* r, const sp_digit* e, int bits, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * 385, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 385, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; tmp = td + 256; @@ -31617,9 +31320,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -36866,20 +36567,11 @@ static void sp_256_get_point_16_8(sp_point_256* r, const sp_point_256* table, static int sp_256_ecc_mulmod_fast_8(sp_point_256* r, const sp_point_256* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_256 t[16 + 1]; - sp_digit tmp[2 * 8 * 6]; -#endif + SP_DECL_VAR(sp_point_256, t, 16 + 1); + SP_DECL_VAR(sp_digit, tmp, 2 * 8 * 6); sp_point_256* rt = NULL; #ifndef WC_NO_CACHE_RESISTANT -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* p = NULL; -#else - sp_point_256 p[1]; -#endif + SP_DECL_VAR(sp_point_256, p, 1); #endif /* !WC_NO_CACHE_RESISTANT */ sp_digit n; int i; @@ -36891,26 +36583,11 @@ static int sp_256_ecc_mulmod_fast_8(sp_point_256* r, const sp_point_256* g, cons (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * (16 + 1), - heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - #ifndef WC_NO_CACHE_RESISTANT - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256), - heap, DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - #endif - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 6, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } + SP_ALLOC_VAR(sp_point_256, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); +#ifndef WC_NO_CACHE_RESISTANT + SP_ALLOC_VAR(sp_point_256, p, 1, heap, DYNAMIC_TYPE_ECC); #endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 8 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 16; @@ -37002,35 +36679,12 @@ static int sp_256_ecc_mulmod_fast_8(sp_point_256* r, const sp_point_256* g, cons } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 8 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 8 * 6, heap, + DYNAMIC_TYPE_ECC); #ifndef WC_NO_CACHE_RESISTANT -#ifdef WOLFSSL_SP_SMALL_STACK - if (p != NULL) -#endif - { - ForceZero(p, sizeof(sp_point_256)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(p, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_256, p, 1, heap, DYNAMIC_TYPE_ECC); #endif /* !WC_NO_CACHE_RESISTANT */ -#ifdef WOLFSSL_SP_SMALL_STACK - if (t != NULL) -#endif - { - ForceZero(t, sizeof(sp_point_256) * 17); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_256, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -37255,11 +36909,7 @@ static void sp_256_proj_point_add_qz1_8(sp_point_256* r, static int sp_256_gen_stripe_table_8(const sp_point_256* a, sp_table_entry_256* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* t = NULL; -#else - sp_point_256 t[3]; -#endif + SP_DECL_VAR(sp_point_256, t, 3); sp_point_256* s1 = NULL; sp_point_256* s2 = NULL; int i; @@ -37268,13 +36918,7 @@ static int sp_256_gen_stripe_table_8(const sp_point_256* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_256, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -37323,9 +36967,7 @@ static int sp_256_gen_stripe_table_8(const sp_point_256* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -37401,13 +37043,8 @@ static int sp_256_ecc_mulmod_stripe_8(sp_point_256* r, const sp_point_256* g, const sp_table_entry_256* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_256 rt[2]; - sp_digit t[2 * 8 * 6]; -#endif + SP_DECL_VAR(sp_point_256, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 8 * 6); sp_point_256* p = NULL; int i; int j; @@ -37421,19 +37058,8 @@ static int sp_256_ecc_mulmod_stripe_8(sp_point_256* r, const sp_point_256* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 8 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -37487,10 +37113,8 @@ static int sp_256_ecc_mulmod_stripe_8(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -37607,20 +37231,11 @@ static int sp_256_ecc_mulmod_8(sp_point_256* r, const sp_point_256* g, #ifndef FP_ECC return sp_256_ecc_mulmod_fast_8(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 8 * 6]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 8 * 6); sp_cache_256_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 6, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 8 * 6, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -37653,9 +37268,7 @@ static int sp_256_ecc_mulmod_8(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -37676,11 +37289,7 @@ static int sp_256_ecc_mulmod_8(sp_point_256* r, const sp_point_256* g, static int sp_256_gen_stripe_table_8(const sp_point_256* a, sp_table_entry_256* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* t = NULL; -#else - sp_point_256 t[3]; -#endif + SP_DECL_VAR(sp_point_256, t, 3); sp_point_256* s1 = NULL; sp_point_256* s2 = NULL; int i; @@ -37689,13 +37298,7 @@ static int sp_256_gen_stripe_table_8(const sp_point_256* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_256, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -37744,9 +37347,7 @@ static int sp_256_gen_stripe_table_8(const sp_point_256* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -37822,13 +37423,8 @@ static int sp_256_ecc_mulmod_stripe_8(sp_point_256* r, const sp_point_256* g, const sp_table_entry_256* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_256 rt[2]; - sp_digit t[2 * 8 * 6]; -#endif + SP_DECL_VAR(sp_point_256, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 8 * 6); sp_point_256* p = NULL; int i; int j; @@ -37842,19 +37438,8 @@ static int sp_256_ecc_mulmod_stripe_8(sp_point_256* r, const sp_point_256* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 8 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -37908,10 +37493,8 @@ static int sp_256_ecc_mulmod_stripe_8(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -38028,20 +37611,11 @@ static int sp_256_ecc_mulmod_8(sp_point_256* r, const sp_point_256* g, #ifndef FP_ECC return sp_256_ecc_mulmod_fast_8(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 8 * 6]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 8 * 6); sp_cache_256_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 6, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 8 * 6, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -38074,9 +37648,7 @@ static int sp_256_ecc_mulmod_8(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -38095,28 +37667,12 @@ static int sp_256_ecc_mulmod_8(sp_point_256* r, const sp_point_256* g, int sp_ecc_mulmod_256(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[1]; - sp_digit k[8]; -#endif + SP_DECL_VAR(sp_point_256, point, 1); + SP_DECL_VAR(sp_digit, k, 8); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 8, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 8, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(k, 8, km); sp_256_point_from_ecc_point_8(point, gm); @@ -38127,10 +37683,8 @@ int sp_ecc_mulmod_256(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_256_point_to_ecc_point_8(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -38150,31 +37704,14 @@ int sp_ecc_mulmod_256(const mp_int* km, const ecc_point* gm, ecc_point* r, int sp_ecc_mulmod_add_256(const mp_int* km, const ecc_point* gm, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[2]; - sp_digit k[8 + 8 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_256, point, 2); + SP_DECL_VAR(sp_digit, k, 8 + 8 * 2 * 6); sp_point_256* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (8 + 8 * 2 * 6), heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 8 + 8 * 2 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 8; @@ -38205,10 +37742,8 @@ int sp_ecc_mulmod_add_256(const mp_int* km, const ecc_point* gm, err = sp_256_point_to_ecc_point_8(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -39642,28 +39177,12 @@ static int sp_256_ecc_mulmod_base_8(sp_point_256* r, const sp_digit* k, */ int sp_ecc_mulmod_base_256(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[1]; - sp_digit k[8]; -#endif + SP_DECL_VAR(sp_point_256, point, 1); + SP_DECL_VAR(sp_digit, k, 8); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 8, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 8, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(k, 8, km); @@ -39673,10 +39192,8 @@ int sp_ecc_mulmod_base_256(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_256_point_to_ecc_point_8(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -39695,31 +39212,14 @@ int sp_ecc_mulmod_base_256(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_256(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[2]; - sp_digit k[8 + 8 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_256, point, 2); + SP_DECL_VAR(sp_digit, k, 8 + 8 * 2 * 6); sp_point_256* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (8 + 8 * 2 * 6), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 8 + 8 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 8; @@ -39749,10 +39249,8 @@ int sp_ecc_mulmod_base_add_256(const mp_int* km, const ecc_point* am, err = sp_256_point_to_ecc_point_8(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -39886,17 +39384,12 @@ static int sp_256_ecc_gen_k_8(WC_RNG* rng, sp_digit* k) */ int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_DECL_VAR(sp_point_256, point, 2); #else - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - sp_point_256 point[2]; - #else - sp_point_256 point[1]; - #endif - sp_digit k[8]; + SP_DECL_VAR(sp_point_256, point, 1); #endif + SP_DECL_VAR(sp_digit, k, 8); #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN sp_point_256* infinity = NULL; #endif @@ -39905,22 +39398,12 @@ int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, DYNAMIC_TYPE_ECC); - #else - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, DYNAMIC_TYPE_ECC); - #endif - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 8, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_ALLOC_VAR(sp_point_256, point, 2, heap, DYNAMIC_TYPE_ECC); +#else + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); #endif - + SP_ALLOC_VAR(sp_digit, k, 8, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN infinity = point + 1; @@ -39950,11 +39433,9 @@ int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) err = sp_256_point_to_ecc_point_8(point, pub); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); /* point is not sensitive, so no need to zeroize */ - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -40072,34 +39553,16 @@ static void sp_256_to_bin_8(sp_digit* r, byte* a) int sp_ecc_secret_gen_256(const mp_int* priv, const ecc_point* pub, byte* out, word32* outLen, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[1]; - sp_digit k[8]; -#endif + SP_DECL_VAR(sp_point_256, point, 1); + SP_DECL_VAR(sp_digit, k, 8); int err = MP_OKAY; if (*outLen < 32U) { err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 8, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 8, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(k, 8, priv); sp_256_point_from_ecc_point_8(point, pub); @@ -40110,10 +39573,8 @@ int sp_ecc_secret_gen_256(const mp_int* priv, const ecc_point* pub, byte* out, *outLen = 32; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -40914,13 +40375,8 @@ static int sp_256_calc_s_8(sp_digit* s, const sp_digit* r, sp_digit* k, int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* e = NULL; - sp_point_256* point = NULL; -#else - sp_digit e[7 * 2 * 8]; - sp_point_256 point[1]; -#endif + SP_DECL_VAR(sp_digit, e, 7 * 2 * 8); + SP_DECL_VAR(sp_point_256, point, 1); sp_digit* x = NULL; sp_digit* k = NULL; sp_digit* r = NULL; @@ -40932,21 +40388,8 @@ int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - e = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7 * 2 * 8, heap, - DYNAMIC_TYPE_ECC); - if (e == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, e, 7 * 2 * 8, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { x = e + 2 * 8; k = e + 4 * 8; @@ -41011,24 +40454,8 @@ int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, err = sp_256_to_mp(s, sm); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (e != NULL) -#endif - { - ForceZero(e, sizeof(sp_digit) * 7 * 2 * 8); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(e, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (point != NULL) -#endif - { - ForceZero(point, sizeof(sp_point_256)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(point, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_digit, e, 7 * 2 * 8, heap, DYNAMIC_TYPE_ECC); return err; } @@ -41841,13 +41268,8 @@ int sp_ecc_verify_256(const byte* hash, word32 hashLen, const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* rm, const mp_int* sm, int* res, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u1 = NULL; - sp_point_256* p1 = NULL; -#else - sp_digit u1[18 * 8]; - sp_point_256 p1[2]; -#endif + SP_DECL_VAR(sp_digit, u1, 18 * 8); + SP_DECL_VAR(sp_point_256, p1, 2); sp_digit* u2 = NULL; sp_digit* s = NULL; sp_digit* tmp = NULL; @@ -41856,21 +41278,8 @@ int sp_ecc_verify_256(const byte* hash, word32 hashLen, const mp_int* pX, sp_int32 c = 0; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p1 = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (p1 == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - u1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 8, heap, - DYNAMIC_TYPE_ECC); - if (u1 == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, u1, 18 * 8, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p1, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { u2 = u1 + 2 * 8; s = u1 + 4 * 8; @@ -41927,11 +41336,8 @@ int sp_ecc_verify_256(const byte* hash, word32 hashLen, const mp_int* pX, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u1, heap, DYNAMIC_TYPE_ECC); - XFREE(p1, heap, DYNAMIC_TYPE_ECC); -#endif - + SP_FREE_VAR(p1, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(u1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -42097,21 +41503,13 @@ int sp_ecc_verify_256_nb(sp_ecc_ctx_t* sp_ctx, const byte* hash, static int sp_256_ecc_is_point_8(const sp_point_256* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[8 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 8 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 8 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 8 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 8; @@ -42135,9 +41533,7 @@ static int sp_256_ecc_is_point_8(const sp_point_256* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -42151,21 +41547,11 @@ static int sp_256_ecc_is_point_8(const sp_point_256* point, */ int sp_ecc_is_point_256(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* pub = NULL; -#else - sp_point_256 pub[1]; -#endif + SP_DECL_VAR(sp_point_256, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_256*)XMALLOC(sizeof(sp_point_256), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_256, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(pub->x, 8, pX); sp_256_from_mp(pub->y, 8, pY); @@ -42174,9 +41560,7 @@ int sp_ecc_is_point_256(const mp_int* pX, const mp_int* pY) err = sp_256_ecc_is_point_8(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -42195,13 +41579,8 @@ int sp_ecc_is_point_256(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_256(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_256* pub = NULL; -#else - sp_digit priv[8]; - sp_point_256 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 8); + SP_DECL_VAR(sp_point_256, pub, 2); sp_point_256* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -42216,21 +41595,8 @@ int sp_ecc_check_key_256(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 8, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 8, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -42282,10 +41648,8 @@ int sp_ecc_check_key_256(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } @@ -42309,32 +41673,13 @@ int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_256* p = NULL; -#else - sp_digit tmp[2 * 8 * 6]; - sp_point_256 p[2]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 8 * 6); + SP_DECL_VAR(sp_point_256, p, 2); sp_point_256* q = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 8 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p, 2, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { q = p + 1; @@ -42362,10 +41707,8 @@ int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_256_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -42384,30 +41727,12 @@ int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_256* p = NULL; -#else - sp_digit tmp[2 * 8 * 2]; - sp_point_256 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 8 * 2); + SP_DECL_VAR(sp_point_256, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 2, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 8 * 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(p->x, 8, pX); sp_256_from_mp(p->y, 8, pY); @@ -42428,10 +41753,8 @@ int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_256_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -42446,30 +41769,13 @@ int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, */ int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_256* p = NULL; -#else - sp_digit tmp[2 * 8 * 4]; - sp_point_256 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 8 * 4); + SP_DECL_VAR(sp_point_256, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 8 * 4, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 8 * 4, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(p->x, 8, pX); sp_256_from_mp(p->y, 8, pY); @@ -42490,10 +41796,8 @@ int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ) err = sp_256_to_mp(p->z, pZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -42506,21 +41810,11 @@ int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ) */ static int sp_256_mont_sqrt_8(sp_digit* y) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 8]; -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 8); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 8, NULL, DYNAMIC_TYPE_ECC); - if (t1 == NULL) { - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, t1, 4 * 8, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 8; @@ -42557,9 +41851,7 @@ static int sp_256_mont_sqrt_8(sp_digit* y) } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -42574,20 +41866,11 @@ static int sp_256_mont_sqrt_8(sp_digit* y) */ int sp_ecc_uncompress_256(mp_int* xm, int odd, mp_int* ym) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* x = NULL; -#else - sp_digit x[4 * 8]; -#endif + SP_DECL_VAR(sp_digit, x, 4 * 8); sp_digit* y = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - x = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 8, NULL, DYNAMIC_TYPE_ECC); - if (x == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, x, 4 * 8, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { y = x + 2 * 8; @@ -42622,9 +41905,7 @@ int sp_ecc_uncompress_256(mp_int* xm, int odd, mp_int* ym) err = sp_256_to_mp(y, ym); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(x, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(x, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -44823,23 +44104,13 @@ WC_OMIT_FRAME_POINTER static sp_digit sp_384_add_12(sp_digit* r, */ static int sp_384_mod_mul_norm_12(sp_digit* r, const sp_digit* a, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - int64_t* t = NULL; -#else - int64_t t[12]; -#endif + SP_DECL_VAR(int64_t, t, 12); int64_t o; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (int64_t*)XMALLOC(sizeof(int64_t) * 12, NULL, DYNAMIC_TYPE_ECC); - if (t == NULL) { - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(int64_t, t, 12, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { /* 1 0 0 0 0 0 0 0 1 1 0 -1 */ t[0] = 0 + (int64_t)a[0] + (int64_t)a[8] + (int64_t)a[9] - (int64_t)a[11]; @@ -44908,9 +44179,7 @@ static int sp_384_mod_mul_norm_12(sp_digit* r, const sp_digit* a, const sp_digit r[11] = (sp_digit)t[11]; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -46975,20 +46244,11 @@ static void sp_384_get_point_16_12(sp_point_384* r, const sp_point_384* table, static int sp_384_ecc_mulmod_fast_12(sp_point_384* r, const sp_point_384* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_384 t[16 + 1]; - sp_digit tmp[2 * 12 * 6]; -#endif + SP_DECL_VAR(sp_point_384, t, 16 + 1); + SP_DECL_VAR(sp_digit, tmp, 2 * 12 * 6); sp_point_384* rt = NULL; #ifndef WC_NO_CACHE_RESISTANT -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* p = NULL; -#else - sp_point_384 p[1]; -#endif + SP_DECL_VAR(sp_point_384, p, 1); #endif /* !WC_NO_CACHE_RESISTANT */ sp_digit n; int i; @@ -47000,26 +46260,11 @@ static int sp_384_ecc_mulmod_fast_12(sp_point_384* r, const sp_point_384* g, con (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * (16 + 1), - heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - #ifndef WC_NO_CACHE_RESISTANT - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384), - heap, DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - #endif - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 6, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } + SP_ALLOC_VAR(sp_point_384, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); +#ifndef WC_NO_CACHE_RESISTANT + SP_ALLOC_VAR(sp_point_384, p, 1, heap, DYNAMIC_TYPE_ECC); #endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 12 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 16; @@ -47111,35 +46356,12 @@ static int sp_384_ecc_mulmod_fast_12(sp_point_384* r, const sp_point_384* g, con } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 12 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 12 * 6, heap, + DYNAMIC_TYPE_ECC); #ifndef WC_NO_CACHE_RESISTANT -#ifdef WOLFSSL_SP_SMALL_STACK - if (p != NULL) -#endif - { - ForceZero(p, sizeof(sp_point_384)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(p, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_384, p, 1, heap, DYNAMIC_TYPE_ECC); #endif /* !WC_NO_CACHE_RESISTANT */ -#ifdef WOLFSSL_SP_SMALL_STACK - if (t != NULL) -#endif - { - ForceZero(t, sizeof(sp_point_384) * 17); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_384, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -47364,11 +46586,7 @@ static void sp_384_proj_point_add_qz1_12(sp_point_384* r, static int sp_384_gen_stripe_table_12(const sp_point_384* a, sp_table_entry_384* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* t = NULL; -#else - sp_point_384 t[3]; -#endif + SP_DECL_VAR(sp_point_384, t, 3); sp_point_384* s1 = NULL; sp_point_384* s2 = NULL; int i; @@ -47377,13 +46595,7 @@ static int sp_384_gen_stripe_table_12(const sp_point_384* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_384, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -47432,9 +46644,7 @@ static int sp_384_gen_stripe_table_12(const sp_point_384* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -47526,13 +46736,8 @@ static int sp_384_ecc_mulmod_stripe_12(sp_point_384* r, const sp_point_384* g, const sp_table_entry_384* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_384 rt[2]; - sp_digit t[2 * 12 * 6]; -#endif + SP_DECL_VAR(sp_point_384, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 12 * 6); sp_point_384* p = NULL; int i; int j; @@ -47546,19 +46751,8 @@ static int sp_384_ecc_mulmod_stripe_12(sp_point_384* r, const sp_point_384* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 12 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -47612,10 +46806,8 @@ static int sp_384_ecc_mulmod_stripe_12(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -47732,20 +46924,11 @@ static int sp_384_ecc_mulmod_12(sp_point_384* r, const sp_point_384* g, #ifndef FP_ECC return sp_384_ecc_mulmod_fast_12(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 12 * 7]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 12 * 7); sp_cache_384_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 7, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 12 * 7, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -47778,9 +46961,7 @@ static int sp_384_ecc_mulmod_12(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -47801,11 +46982,7 @@ static int sp_384_ecc_mulmod_12(sp_point_384* r, const sp_point_384* g, static int sp_384_gen_stripe_table_12(const sp_point_384* a, sp_table_entry_384* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* t = NULL; -#else - sp_point_384 t[3]; -#endif + SP_DECL_VAR(sp_point_384, t, 3); sp_point_384* s1 = NULL; sp_point_384* s2 = NULL; int i; @@ -47814,13 +46991,7 @@ static int sp_384_gen_stripe_table_12(const sp_point_384* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_384, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -47869,9 +47040,7 @@ static int sp_384_gen_stripe_table_12(const sp_point_384* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -47963,13 +47132,8 @@ static int sp_384_ecc_mulmod_stripe_12(sp_point_384* r, const sp_point_384* g, const sp_table_entry_384* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_384 rt[2]; - sp_digit t[2 * 12 * 6]; -#endif + SP_DECL_VAR(sp_point_384, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 12 * 6); sp_point_384* p = NULL; int i; int j; @@ -47983,19 +47147,8 @@ static int sp_384_ecc_mulmod_stripe_12(sp_point_384* r, const sp_point_384* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 12 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -48049,10 +47202,8 @@ static int sp_384_ecc_mulmod_stripe_12(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -48169,20 +47320,11 @@ static int sp_384_ecc_mulmod_12(sp_point_384* r, const sp_point_384* g, #ifndef FP_ECC return sp_384_ecc_mulmod_fast_12(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 12 * 7]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 12 * 7); sp_cache_384_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 7, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 12 * 7, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -48215,9 +47357,7 @@ static int sp_384_ecc_mulmod_12(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -48236,28 +47376,12 @@ static int sp_384_ecc_mulmod_12(sp_point_384* r, const sp_point_384* g, int sp_ecc_mulmod_384(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[1]; - sp_digit k[12]; -#endif + SP_DECL_VAR(sp_point_384, point, 1); + SP_DECL_VAR(sp_digit, k, 12); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 12, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 12, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(k, 12, km); sp_384_point_from_ecc_point_12(point, gm); @@ -48268,10 +47392,8 @@ int sp_ecc_mulmod_384(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_384_point_to_ecc_point_12(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -48291,31 +47413,14 @@ int sp_ecc_mulmod_384(const mp_int* km, const ecc_point* gm, ecc_point* r, int sp_ecc_mulmod_add_384(const mp_int* km, const ecc_point* gm, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[2]; - sp_digit k[12 + 12 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_384, point, 2); + SP_DECL_VAR(sp_digit, k, 12 + 12 * 2 * 6); sp_point_384* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (12 + 12 * 2 * 6), heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 12 + 12 * 2 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 12; @@ -48346,10 +47451,8 @@ int sp_ecc_mulmod_add_384(const mp_int* km, const ecc_point* gm, err = sp_384_point_to_ecc_point_12(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -49783,28 +48886,12 @@ static int sp_384_ecc_mulmod_base_12(sp_point_384* r, const sp_digit* k, */ int sp_ecc_mulmod_base_384(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[1]; - sp_digit k[12]; -#endif + SP_DECL_VAR(sp_point_384, point, 1); + SP_DECL_VAR(sp_digit, k, 12); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 12, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 12, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(k, 12, km); @@ -49814,10 +48901,8 @@ int sp_ecc_mulmod_base_384(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_384_point_to_ecc_point_12(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -49836,31 +48921,14 @@ int sp_ecc_mulmod_base_384(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_384(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[2]; - sp_digit k[12 + 12 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_384, point, 2); + SP_DECL_VAR(sp_digit, k, 12 + 12 * 2 * 6); sp_point_384* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (12 + 12 * 2 * 6), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 12 + 12 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 12; @@ -49890,10 +48958,8 @@ int sp_ecc_mulmod_base_add_384(const mp_int* km, const ecc_point* am, err = sp_384_point_to_ecc_point_12(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -50033,17 +49099,12 @@ static int sp_384_ecc_gen_k_12(WC_RNG* rng, sp_digit* k) */ int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_DECL_VAR(sp_point_384, point, 2); #else - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - sp_point_384 point[2]; - #else - sp_point_384 point[1]; - #endif - sp_digit k[12]; + SP_DECL_VAR(sp_point_384, point, 1); #endif + SP_DECL_VAR(sp_digit, k, 12); #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN sp_point_384* infinity = NULL; #endif @@ -50052,22 +49113,12 @@ int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, DYNAMIC_TYPE_ECC); - #else - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, DYNAMIC_TYPE_ECC); - #endif - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 12, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_ALLOC_VAR(sp_point_384, point, 2, heap, DYNAMIC_TYPE_ECC); +#else + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); #endif - + SP_ALLOC_VAR(sp_digit, k, 12, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN infinity = point + 1; @@ -50097,11 +49148,9 @@ int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) err = sp_384_point_to_ecc_point_12(point, pub); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); /* point is not sensitive, so no need to zeroize */ - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -50219,34 +49268,16 @@ static void sp_384_to_bin_12(sp_digit* r, byte* a) int sp_ecc_secret_gen_384(const mp_int* priv, const ecc_point* pub, byte* out, word32* outLen, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[1]; - sp_digit k[12]; -#endif + SP_DECL_VAR(sp_point_384, point, 1); + SP_DECL_VAR(sp_digit, k, 12); int err = MP_OKAY; if (*outLen < 48U) { err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 12, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 12, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(k, 12, priv); sp_384_point_from_ecc_point_12(point, pub); @@ -50257,10 +49288,8 @@ int sp_ecc_secret_gen_384(const mp_int* priv, const ecc_point* pub, byte* out, *outLen = 48; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -51059,13 +50088,8 @@ static int sp_384_calc_s_12(sp_digit* s, const sp_digit* r, sp_digit* k, int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng, const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* e = NULL; - sp_point_384* point = NULL; -#else - sp_digit e[7 * 2 * 12]; - sp_point_384 point[1]; -#endif + SP_DECL_VAR(sp_digit, e, 7 * 2 * 12); + SP_DECL_VAR(sp_point_384, point, 1); sp_digit* x = NULL; sp_digit* k = NULL; sp_digit* r = NULL; @@ -51077,21 +50101,8 @@ int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - e = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7 * 2 * 12, heap, - DYNAMIC_TYPE_ECC); - if (e == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, e, 7 * 2 * 12, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { x = e + 2 * 12; k = e + 4 * 12; @@ -51156,24 +50167,8 @@ int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng, err = sp_384_to_mp(s, sm); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (e != NULL) -#endif - { - ForceZero(e, sizeof(sp_digit) * 7 * 2 * 12); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(e, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (point != NULL) -#endif - { - ForceZero(point, sizeof(sp_point_384)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(point, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_digit, e, 7 * 2 * 12, heap, DYNAMIC_TYPE_ECC); return err; } @@ -51980,13 +50975,8 @@ int sp_ecc_verify_384(const byte* hash, word32 hashLen, const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* rm, const mp_int* sm, int* res, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u1 = NULL; - sp_point_384* p1 = NULL; -#else - sp_digit u1[18 * 12]; - sp_point_384 p1[2]; -#endif + SP_DECL_VAR(sp_digit, u1, 18 * 12); + SP_DECL_VAR(sp_point_384, p1, 2); sp_digit* u2 = NULL; sp_digit* s = NULL; sp_digit* tmp = NULL; @@ -51995,21 +50985,8 @@ int sp_ecc_verify_384(const byte* hash, word32 hashLen, const mp_int* pX, sp_int32 c = 0; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p1 = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (p1 == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - u1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 12, heap, - DYNAMIC_TYPE_ECC); - if (u1 == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, u1, 18 * 12, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p1, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { u2 = u1 + 2 * 12; s = u1 + 4 * 12; @@ -52066,11 +51043,8 @@ int sp_ecc_verify_384(const byte* hash, word32 hashLen, const mp_int* pX, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u1, heap, DYNAMIC_TYPE_ECC); - XFREE(p1, heap, DYNAMIC_TYPE_ECC); -#endif - + SP_FREE_VAR(p1, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(u1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -52236,21 +51210,13 @@ int sp_ecc_verify_384_nb(sp_ecc_ctx_t* sp_ctx, const byte* hash, static int sp_384_ecc_is_point_12(const sp_point_384* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[12 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 12 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 12 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 12 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 12; @@ -52274,9 +51240,7 @@ static int sp_384_ecc_is_point_12(const sp_point_384* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -52290,21 +51254,11 @@ static int sp_384_ecc_is_point_12(const sp_point_384* point, */ int sp_ecc_is_point_384(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* pub = NULL; -#else - sp_point_384 pub[1]; -#endif + SP_DECL_VAR(sp_point_384, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_384*)XMALLOC(sizeof(sp_point_384), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_384, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(pub->x, 12, pX); sp_384_from_mp(pub->y, 12, pY); @@ -52313,9 +51267,7 @@ int sp_ecc_is_point_384(const mp_int* pX, const mp_int* pY) err = sp_384_ecc_is_point_12(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -52334,13 +51286,8 @@ int sp_ecc_is_point_384(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_384(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_384* pub = NULL; -#else - sp_digit priv[12]; - sp_point_384 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 12); + SP_DECL_VAR(sp_point_384, pub, 2); sp_point_384* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -52355,21 +51302,8 @@ int sp_ecc_check_key_384(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 12, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 12, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -52421,10 +51355,8 @@ int sp_ecc_check_key_384(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } @@ -52448,32 +51380,13 @@ int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_384* p = NULL; -#else - sp_digit tmp[2 * 12 * 6]; - sp_point_384 p[2]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 12 * 6); + SP_DECL_VAR(sp_point_384, p, 2); sp_point_384* q = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 12 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p, 2, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { q = p + 1; @@ -52501,10 +51414,8 @@ int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_384_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -52523,30 +51434,12 @@ int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_384* p = NULL; -#else - sp_digit tmp[2 * 12 * 2]; - sp_point_384 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 12 * 2); + SP_DECL_VAR(sp_point_384, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 2, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 12 * 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(p->x, 12, pX); sp_384_from_mp(p->y, 12, pY); @@ -52567,10 +51460,8 @@ int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_384_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -52585,30 +51476,13 @@ int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, */ int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_384* p = NULL; -#else - sp_digit tmp[2 * 12 * 6]; - sp_point_384 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 12 * 6); + SP_DECL_VAR(sp_point_384, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 12 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 12 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(p->x, 12, pX); sp_384_from_mp(p->y, 12, pY); @@ -52629,10 +51503,8 @@ int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ) err = sp_384_to_mp(p->z, pZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -52645,23 +51517,14 @@ int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ) */ static int sp_384_mont_sqrt_12(sp_digit* y) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[5 * 2 * 12]; -#endif + SP_DECL_VAR(sp_digit, t1, 5 * 2 * 12); sp_digit* t2 = NULL; sp_digit* t3 = NULL; sp_digit* t4 = NULL; sp_digit* t5 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 5 * 2 * 12, NULL, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, t1, 5 * 2 * 12, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 12; t3 = t1 + 4 * 12; @@ -52726,9 +51589,7 @@ static int sp_384_mont_sqrt_12(sp_digit* y) } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -52743,20 +51604,11 @@ static int sp_384_mont_sqrt_12(sp_digit* y) */ int sp_ecc_uncompress_384(mp_int* xm, int odd, mp_int* ym) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* x = NULL; -#else - sp_digit x[4 * 12]; -#endif + SP_DECL_VAR(sp_digit, x, 4 * 12); sp_digit* y = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - x = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 12, NULL, DYNAMIC_TYPE_ECC); - if (x == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, x, 4 * 12, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { y = x + 2 * 12; @@ -52791,9 +51643,7 @@ int sp_ecc_uncompress_384(mp_int* xm, int odd, mp_int* ym) err = sp_384_to_mp(y, ym); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(x, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(x, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -59214,20 +58064,11 @@ static void sp_521_get_point_16_17(sp_point_521* r, const sp_point_521* table, static int sp_521_ecc_mulmod_fast_17(sp_point_521* r, const sp_point_521* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_521 t[16 + 1]; - sp_digit tmp[2 * 17 * 6]; -#endif + SP_DECL_VAR(sp_point_521, t, 16 + 1); + SP_DECL_VAR(sp_digit, tmp, 2 * 17 * 6); sp_point_521* rt = NULL; #ifndef WC_NO_CACHE_RESISTANT -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* p = NULL; -#else - sp_point_521 p[1]; -#endif + SP_DECL_VAR(sp_point_521, p, 1); #endif /* !WC_NO_CACHE_RESISTANT */ sp_digit n; int i; @@ -59239,26 +58080,11 @@ static int sp_521_ecc_mulmod_fast_17(sp_point_521* r, const sp_point_521* g, con (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * (16 + 1), - heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - #ifndef WC_NO_CACHE_RESISTANT - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521), - heap, DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - #endif - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 6, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } + SP_ALLOC_VAR(sp_point_521, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); +#ifndef WC_NO_CACHE_RESISTANT + SP_ALLOC_VAR(sp_point_521, p, 1, heap, DYNAMIC_TYPE_ECC); #endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 17 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 16; @@ -59354,35 +58180,12 @@ static int sp_521_ecc_mulmod_fast_17(sp_point_521* r, const sp_point_521* g, con } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 17 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 17 * 6, heap, + DYNAMIC_TYPE_ECC); #ifndef WC_NO_CACHE_RESISTANT -#ifdef WOLFSSL_SP_SMALL_STACK - if (p != NULL) -#endif - { - ForceZero(p, sizeof(sp_point_521)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(p, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_521, p, 1, heap, DYNAMIC_TYPE_ECC); #endif /* !WC_NO_CACHE_RESISTANT */ -#ifdef WOLFSSL_SP_SMALL_STACK - if (t != NULL) -#endif - { - ForceZero(t, sizeof(sp_point_521) * 17); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_521, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -59607,11 +58410,7 @@ static void sp_521_proj_point_add_qz1_17(sp_point_521* r, static int sp_521_gen_stripe_table_17(const sp_point_521* a, sp_table_entry_521* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* t = NULL; -#else - sp_point_521 t[3]; -#endif + SP_DECL_VAR(sp_point_521, t, 3); sp_point_521* s1 = NULL; sp_point_521* s2 = NULL; int i; @@ -59620,13 +58419,7 @@ static int sp_521_gen_stripe_table_17(const sp_point_521* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_521, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -59675,9 +58468,7 @@ static int sp_521_gen_stripe_table_17(const sp_point_521* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -59789,13 +58580,8 @@ static int sp_521_ecc_mulmod_stripe_17(sp_point_521* r, const sp_point_521* g, const sp_table_entry_521* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_521 rt[2]; - sp_digit t[2 * 17 * 6]; -#endif + SP_DECL_VAR(sp_point_521, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 17 * 6); sp_point_521* p = NULL; int i; int j; @@ -59809,19 +58595,8 @@ static int sp_521_ecc_mulmod_stripe_17(sp_point_521* r, const sp_point_521* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 17 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -59875,10 +58650,8 @@ static int sp_521_ecc_mulmod_stripe_17(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -59995,20 +58768,11 @@ static int sp_521_ecc_mulmod_17(sp_point_521* r, const sp_point_521* g, #ifndef FP_ECC return sp_521_ecc_mulmod_fast_17(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 17 * 6]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 17 * 6); sp_cache_521_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 6, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 17 * 6, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -60041,9 +58805,7 @@ static int sp_521_ecc_mulmod_17(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -60064,11 +58826,7 @@ static int sp_521_ecc_mulmod_17(sp_point_521* r, const sp_point_521* g, static int sp_521_gen_stripe_table_17(const sp_point_521* a, sp_table_entry_521* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* t = NULL; -#else - sp_point_521 t[3]; -#endif + SP_DECL_VAR(sp_point_521, t, 3); sp_point_521* s1 = NULL; sp_point_521* s2 = NULL; int i; @@ -60077,13 +58835,7 @@ static int sp_521_gen_stripe_table_17(const sp_point_521* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_521, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -60132,9 +58884,7 @@ static int sp_521_gen_stripe_table_17(const sp_point_521* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -60246,13 +58996,8 @@ static int sp_521_ecc_mulmod_stripe_17(sp_point_521* r, const sp_point_521* g, const sp_table_entry_521* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_521 rt[2]; - sp_digit t[2 * 17 * 6]; -#endif + SP_DECL_VAR(sp_point_521, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 17 * 6); sp_point_521* p = NULL; int i; int j; @@ -60266,19 +59011,8 @@ static int sp_521_ecc_mulmod_stripe_17(sp_point_521* r, const sp_point_521* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 17 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -60332,10 +59066,8 @@ static int sp_521_ecc_mulmod_stripe_17(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -60452,20 +59184,11 @@ static int sp_521_ecc_mulmod_17(sp_point_521* r, const sp_point_521* g, #ifndef FP_ECC return sp_521_ecc_mulmod_fast_17(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 17 * 6]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 17 * 6); sp_cache_521_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 6, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 17 * 6, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -60498,9 +59221,7 @@ static int sp_521_ecc_mulmod_17(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -60519,28 +59240,12 @@ static int sp_521_ecc_mulmod_17(sp_point_521* r, const sp_point_521* g, int sp_ecc_mulmod_521(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[1]; - sp_digit k[17]; -#endif + SP_DECL_VAR(sp_point_521, point, 1); + SP_DECL_VAR(sp_digit, k, 17); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 17, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 17, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(k, 17, km); sp_521_point_from_ecc_point_17(point, gm); @@ -60551,10 +59256,8 @@ int sp_ecc_mulmod_521(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_521_point_to_ecc_point_17(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -60574,31 +59277,14 @@ int sp_ecc_mulmod_521(const mp_int* km, const ecc_point* gm, ecc_point* r, int sp_ecc_mulmod_add_521(const mp_int* km, const ecc_point* gm, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[2]; - sp_digit k[17 + 17 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_521, point, 2); + SP_DECL_VAR(sp_digit, k, 17 + 17 * 2 * 6); sp_point_521* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (17 + 17 * 2 * 6), heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 17 + 17 * 2 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 17; @@ -60629,10 +59315,8 @@ int sp_ecc_mulmod_add_521(const mp_int* km, const ecc_point* gm, err = sp_521_point_to_ecc_point_17(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -62610,28 +61294,12 @@ static int sp_521_ecc_mulmod_base_17(sp_point_521* r, const sp_digit* k, */ int sp_ecc_mulmod_base_521(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[1]; - sp_digit k[17]; -#endif + SP_DECL_VAR(sp_point_521, point, 1); + SP_DECL_VAR(sp_digit, k, 17); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 17, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 17, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(k, 17, km); @@ -62641,10 +61309,8 @@ int sp_ecc_mulmod_base_521(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_521_point_to_ecc_point_17(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -62663,31 +61329,14 @@ int sp_ecc_mulmod_base_521(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_521(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[2]; - sp_digit k[17 + 17 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_521, point, 2); + SP_DECL_VAR(sp_digit, k, 17 + 17 * 2 * 6); sp_point_521* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (17 + 17 * 2 * 6), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 17 + 17 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 17; @@ -62717,10 +61366,8 @@ int sp_ecc_mulmod_base_add_521(const mp_int* km, const ecc_point* am, err = sp_521_point_to_ecc_point_17(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -62870,17 +61517,12 @@ static int sp_521_ecc_gen_k_17(WC_RNG* rng, sp_digit* k) */ int sp_ecc_make_key_521(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_DECL_VAR(sp_point_521, point, 2); #else - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - sp_point_521 point[2]; - #else - sp_point_521 point[1]; - #endif - sp_digit k[17]; + SP_DECL_VAR(sp_point_521, point, 1); #endif + SP_DECL_VAR(sp_digit, k, 17); #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN sp_point_521* infinity = NULL; #endif @@ -62889,22 +61531,12 @@ int sp_ecc_make_key_521(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, DYNAMIC_TYPE_ECC); - #else - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, DYNAMIC_TYPE_ECC); - #endif - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 17, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_ALLOC_VAR(sp_point_521, point, 2, heap, DYNAMIC_TYPE_ECC); +#else + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); #endif - + SP_ALLOC_VAR(sp_digit, k, 17, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN infinity = point + 1; @@ -62934,11 +61566,9 @@ int sp_ecc_make_key_521(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) err = sp_521_point_to_ecc_point_17(point, pub); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); /* point is not sensitive, so no need to zeroize */ - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -63058,34 +61688,16 @@ static void sp_521_to_bin_17(sp_digit* r, byte* a) int sp_ecc_secret_gen_521(const mp_int* priv, const ecc_point* pub, byte* out, word32* outLen, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[1]; - sp_digit k[17]; -#endif + SP_DECL_VAR(sp_point_521, point, 1); + SP_DECL_VAR(sp_digit, k, 17); int err = MP_OKAY; if (*outLen < 65U) { err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 17, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 17, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(k, 17, priv); sp_521_point_from_ecc_point_17(point, pub); @@ -63096,10 +61708,8 @@ int sp_ecc_secret_gen_521(const mp_int* priv, const ecc_point* pub, byte* out, *outLen = 66; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -64414,13 +63024,8 @@ static int sp_521_calc_s_17(sp_digit* s, const sp_digit* r, sp_digit* k, int sp_ecc_sign_521(const byte* hash, word32 hashLen, WC_RNG* rng, const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* e = NULL; - sp_point_521* point = NULL; -#else - sp_digit e[7 * 2 * 17]; - sp_point_521 point[1]; -#endif + SP_DECL_VAR(sp_digit, e, 7 * 2 * 17); + SP_DECL_VAR(sp_point_521, point, 1); sp_digit* x = NULL; sp_digit* k = NULL; sp_digit* r = NULL; @@ -64432,21 +63037,8 @@ int sp_ecc_sign_521(const byte* hash, word32 hashLen, WC_RNG* rng, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - e = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7 * 2 * 17, heap, - DYNAMIC_TYPE_ECC); - if (e == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, e, 7 * 2 * 17, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { x = e + 2 * 17; k = e + 4 * 17; @@ -64516,24 +63108,8 @@ int sp_ecc_sign_521(const byte* hash, word32 hashLen, WC_RNG* rng, err = sp_521_to_mp(s, sm); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (e != NULL) -#endif - { - ForceZero(e, sizeof(sp_digit) * 7 * 2 * 17); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(e, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (point != NULL) -#endif - { - ForceZero(point, sizeof(sp_point_521)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(point, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_digit, e, 7 * 2 * 17, heap, DYNAMIC_TYPE_ECC); return err; } @@ -65632,13 +64208,8 @@ int sp_ecc_verify_521(const byte* hash, word32 hashLen, const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* rm, const mp_int* sm, int* res, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u1 = NULL; - sp_point_521* p1 = NULL; -#else - sp_digit u1[18 * 17]; - sp_point_521 p1[2]; -#endif + SP_DECL_VAR(sp_digit, u1, 18 * 17); + SP_DECL_VAR(sp_point_521, p1, 2); sp_digit* u2 = NULL; sp_digit* s = NULL; sp_digit* tmp = NULL; @@ -65647,21 +64218,8 @@ int sp_ecc_verify_521(const byte* hash, word32 hashLen, const mp_int* pX, sp_int32 c = 0; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p1 = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (p1 == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - u1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 17, heap, - DYNAMIC_TYPE_ECC); - if (u1 == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, u1, 18 * 17, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p1, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { u2 = u1 + 2 * 17; s = u1 + 4 * 17; @@ -65722,11 +64280,8 @@ int sp_ecc_verify_521(const byte* hash, word32 hashLen, const mp_int* pX, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u1, heap, DYNAMIC_TYPE_ECC); - XFREE(p1, heap, DYNAMIC_TYPE_ECC); -#endif - + SP_FREE_VAR(p1, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(u1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -65895,21 +64450,13 @@ int sp_ecc_verify_521_nb(sp_ecc_ctx_t* sp_ctx, const byte* hash, static int sp_521_ecc_is_point_17(const sp_point_521* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[17 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 17 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 17 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 17 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 17; @@ -65933,9 +64480,7 @@ static int sp_521_ecc_is_point_17(const sp_point_521* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -65949,21 +64494,11 @@ static int sp_521_ecc_is_point_17(const sp_point_521* point, */ int sp_ecc_is_point_521(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* pub = NULL; -#else - sp_point_521 pub[1]; -#endif + SP_DECL_VAR(sp_point_521, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_521*)XMALLOC(sizeof(sp_point_521), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_521, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(pub->x, 17, pX); sp_521_from_mp(pub->y, 17, pY); @@ -65972,9 +64507,7 @@ int sp_ecc_is_point_521(const mp_int* pX, const mp_int* pY) err = sp_521_ecc_is_point_17(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -65993,13 +64526,8 @@ int sp_ecc_is_point_521(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_521(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_521* pub = NULL; -#else - sp_digit priv[17]; - sp_point_521 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 17); + SP_DECL_VAR(sp_point_521, pub, 2); sp_point_521* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -66014,21 +64542,8 @@ int sp_ecc_check_key_521(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 17, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 17, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -66080,10 +64595,8 @@ int sp_ecc_check_key_521(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } @@ -66107,32 +64620,13 @@ int sp_ecc_proj_add_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_521* p = NULL; -#else - sp_digit tmp[2 * 17 * 6]; - sp_point_521 p[2]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 17 * 6); + SP_DECL_VAR(sp_point_521, p, 2); sp_point_521* q = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 17 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p, 2, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { q = p + 1; @@ -66160,10 +64654,8 @@ int sp_ecc_proj_add_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_521_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -66182,30 +64674,12 @@ int sp_ecc_proj_add_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, int sp_ecc_proj_dbl_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_521* p = NULL; -#else - sp_digit tmp[2 * 17 * 2]; - sp_point_521 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 17 * 2); + SP_DECL_VAR(sp_point_521, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 2, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 17 * 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(p->x, 17, pX); sp_521_from_mp(p->y, 17, pY); @@ -66226,10 +64700,8 @@ int sp_ecc_proj_dbl_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_521_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -66244,30 +64716,13 @@ int sp_ecc_proj_dbl_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, */ int sp_ecc_map_521(mp_int* pX, mp_int* pY, mp_int* pZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_521* p = NULL; -#else - sp_digit tmp[2 * 17 * 5]; - sp_point_521 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 17 * 5); + SP_DECL_VAR(sp_point_521, p, 1); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17 * 5, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 17 * 5, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(p->x, 17, pX); sp_521_from_mp(p->y, 17, pY); @@ -66288,10 +64743,8 @@ int sp_ecc_map_521(mp_int* pX, mp_int* pY, mp_int* pZ) err = sp_521_to_mp(p->z, pZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -66311,19 +64764,10 @@ static const word32 p521_sqrt_power[17] = { */ static int sp_521_mont_sqrt_17(sp_digit* y) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t = NULL; -#else - sp_digit t[2 * 17]; -#endif + SP_DECL_VAR(sp_digit, t, 2 * 17); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 17, NULL, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, t, 2 * 17, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { { @@ -66339,9 +64783,7 @@ static int sp_521_mont_sqrt_17(sp_digit* y) } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -66356,20 +64798,11 @@ static int sp_521_mont_sqrt_17(sp_digit* y) */ int sp_ecc_uncompress_521(mp_int* xm, int odd, mp_int* ym) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* x = NULL; -#else - sp_digit x[4 * 17]; -#endif + SP_DECL_VAR(sp_digit, x, 4 * 17); sp_digit* y = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - x = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 17, NULL, DYNAMIC_TYPE_ECC); - if (x == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, x, 4 * 17, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { y = x + 2 * 17; @@ -66404,9 +64837,7 @@ int sp_ecc_uncompress_521(mp_int* xm, int odd, mp_int* ym) err = sp_521_to_mp(y, ym); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(x, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(x, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -74051,13 +72482,8 @@ static int sp_1024_proj_point_add_32_nb(sp_ecc_ctx_t* sp_ctx, sp_point_1024* r, static int sp_1024_ecc_mulmod_fast_32(sp_point_1024* r, const sp_point_1024* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_1024 t[16 + 1]; - sp_digit tmp[2 * 32 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, t, 16 + 1); + SP_DECL_VAR(sp_digit, tmp, 2 * 32 * 37); sp_point_1024* rt = NULL; sp_digit n; int i; @@ -74069,18 +72495,8 @@ static int sp_1024_ecc_mulmod_fast_32(sp_point_1024* r, const sp_point_1024* g, (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * (16 + 1), - heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 32 * 37, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_point_1024, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 32 * 37, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 16; @@ -74152,24 +72568,9 @@ static int sp_1024_ecc_mulmod_fast_32(sp_point_1024* r, const sp_point_1024* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 32 * 37); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (t != NULL) -#endif - { - ForceZero(t, sizeof(sp_point_1024) * 17); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 32 * 37, heap, + DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_point_1024, t, 16 + 1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -74394,11 +72795,7 @@ static void sp_1024_proj_point_add_qz1_32(sp_point_1024* r, static int sp_1024_gen_stripe_table_32(const sp_point_1024* a, sp_table_entry_1024* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* t = NULL; -#else - sp_point_1024 t[3]; -#endif + SP_DECL_VAR(sp_point_1024, t, 3); sp_point_1024* s1 = NULL; sp_point_1024* s2 = NULL; int i; @@ -74407,13 +72804,7 @@ static int sp_1024_gen_stripe_table_32(const sp_point_1024* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_1024, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -74462,9 +72853,7 @@ static int sp_1024_gen_stripe_table_32(const sp_point_1024* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -74490,13 +72879,8 @@ static int sp_1024_ecc_mulmod_stripe_32(sp_point_1024* r, const sp_point_1024* g const sp_table_entry_1024* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_1024 rt[2]; - sp_digit t[2 * 32 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 32 * 37); sp_point_1024* p = NULL; int i; int j; @@ -74510,19 +72894,8 @@ static int sp_1024_ecc_mulmod_stripe_32(sp_point_1024* r, const sp_point_1024* g (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 32 * 37, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 32 * 37, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -74561,10 +72934,8 @@ static int sp_1024_ecc_mulmod_stripe_32(sp_point_1024* r, const sp_point_1024* g } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -74681,20 +73052,11 @@ static int sp_1024_ecc_mulmod_32(sp_point_1024* r, const sp_point_1024* g, #ifndef FP_ECC return sp_1024_ecc_mulmod_fast_32(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 32 * 38]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 32 * 38); sp_cache_1024_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 32 * 38, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 32 * 38, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -74727,9 +73089,7 @@ static int sp_1024_ecc_mulmod_32(sp_point_1024* r, const sp_point_1024* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -74750,11 +73110,7 @@ static int sp_1024_ecc_mulmod_32(sp_point_1024* r, const sp_point_1024* g, static int sp_1024_gen_stripe_table_32(const sp_point_1024* a, sp_table_entry_1024* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* t = NULL; -#else - sp_point_1024 t[3]; -#endif + SP_DECL_VAR(sp_point_1024, t, 3); sp_point_1024* s1 = NULL; sp_point_1024* s2 = NULL; int i; @@ -74763,13 +73119,7 @@ static int sp_1024_gen_stripe_table_32(const sp_point_1024* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_1024, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -74818,9 +73168,7 @@ static int sp_1024_gen_stripe_table_32(const sp_point_1024* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -74846,13 +73194,8 @@ static int sp_1024_ecc_mulmod_stripe_32(sp_point_1024* r, const sp_point_1024* g const sp_table_entry_1024* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_1024 rt[2]; - sp_digit t[2 * 32 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 32 * 37); sp_point_1024* p = NULL; int i; int j; @@ -74866,19 +73209,8 @@ static int sp_1024_ecc_mulmod_stripe_32(sp_point_1024* r, const sp_point_1024* g (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 32 * 37, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 32 * 37, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -74917,10 +73249,8 @@ static int sp_1024_ecc_mulmod_stripe_32(sp_point_1024* r, const sp_point_1024* g } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -75037,20 +73367,11 @@ static int sp_1024_ecc_mulmod_32(sp_point_1024* r, const sp_point_1024* g, #ifndef FP_ECC return sp_1024_ecc_mulmod_fast_32(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 32 * 38]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 32 * 38); sp_cache_1024_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 32 * 38, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 32 * 38, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -75083,9 +73404,7 @@ static int sp_1024_ecc_mulmod_32(sp_point_1024* r, const sp_point_1024* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -75104,28 +73423,12 @@ static int sp_1024_ecc_mulmod_32(sp_point_1024* r, const sp_point_1024* g, int sp_ecc_mulmod_1024(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[1]; - sp_digit k[32]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, k, 32); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 32, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(k, 32, km); sp_1024_point_from_ecc_point_32(point, gm); @@ -75136,10 +73439,8 @@ int sp_ecc_mulmod_1024(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_1024_point_to_ecc_point_32(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -78741,28 +77042,12 @@ static int sp_1024_ecc_mulmod_base_32(sp_point_1024* r, const sp_digit* k, */ int sp_ecc_mulmod_base_1024(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[1]; - sp_digit k[32]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, k, 32); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 32, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(k, 32, km); @@ -78772,10 +77057,8 @@ int sp_ecc_mulmod_base_1024(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_1024_point_to_ecc_point_32(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -78794,31 +77077,14 @@ int sp_ecc_mulmod_base_1024(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[2]; - sp_digit k[32 + 32 * 2 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, point, 2); + SP_DECL_VAR(sp_digit, k, 32 + 32 * 2 * 37); sp_point_1024* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (32 + 32 * 2 * 37), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 32 + 32 * 2 * 37, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 32; @@ -78848,10 +77114,8 @@ int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am, err = sp_1024_point_to_ecc_point_32(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -78869,13 +77133,8 @@ int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am, int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* t = NULL; -#else - sp_point_1024 point[1]; - sp_digit t[38 * 2 * 32]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, t, 38 * 2 * 32); int err = MP_OKAY; if ((gm == NULL) || (len == NULL)) { @@ -78890,21 +77149,8 @@ int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 38 * 2 * 32, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 38 * 2 * 32, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_point_from_ecc_point_32(point, gm); err = sp_1024_gen_stripe_table_32(point, @@ -78914,10 +77160,8 @@ int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, *len = sizeof(sp_table_entry_1024) * 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -78970,28 +77214,12 @@ int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, int sp_ecc_mulmod_table_1024(const mp_int* km, const ecc_point* gm, byte* table, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[1]; - sp_digit k[32]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, k, 32); int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) { - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32, heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 32, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(k, 32, km); sp_1024_point_from_ecc_point_32(point, gm); @@ -79008,10 +77236,8 @@ int sp_ecc_mulmod_table_1024(const mp_int* km, const ecc_point* gm, byte* table, err = sp_1024_point_to_ecc_point_32(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); return err; } @@ -82472,22 +80698,14 @@ static void sp_1024_from_bin(sp_digit* r, int size, const byte* a, int n) static int sp_1024_ecc_is_point_32(const sp_point_1024* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[32 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 32 * 4); sp_digit* t2 = NULL; sp_int32 n; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 32 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 32; @@ -82514,9 +80732,7 @@ static int sp_1024_ecc_is_point_32(const sp_point_1024* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -82530,21 +80746,11 @@ static int sp_1024_ecc_is_point_32(const sp_point_1024* point, */ int sp_ecc_is_point_1024(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* pub = NULL; -#else - sp_point_1024 pub[1]; -#endif + SP_DECL_VAR(sp_point_1024, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_1024, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(pub->x, 32, pX); sp_1024_from_mp(pub->y, 32, pY); @@ -82553,9 +80759,7 @@ int sp_ecc_is_point_1024(const mp_int* pX, const mp_int* pY) err = sp_1024_ecc_is_point_32(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -82574,13 +80778,8 @@ int sp_ecc_is_point_1024(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_1024(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_1024* pub = NULL; -#else - sp_digit priv[32]; - sp_point_1024 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 32); + SP_DECL_VAR(sp_point_1024, pub, 2); sp_point_1024* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -82595,21 +80794,8 @@ int sp_ecc_check_key_1024(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 32, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_1024, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -82661,10 +80847,8 @@ int sp_ecc_check_key_1024(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } diff --git a/wolfcrypt/src/sp_x86_64.c b/wolfcrypt/src/sp_x86_64.c index f3035cbee..b79217597 100644 --- a/wolfcrypt/src/sp_x86_64.c +++ b/wolfcrypt/src/sp_x86_64.c @@ -57,6 +57,57 @@ #define __volatile__ volatile #endif +#ifdef WOLFSSL_SP_SMALL_STACK + #define SP_DECL_VAR(TYPE, NAME, CNT) \ + TYPE* NAME = NULL + #define SP_ALLOC_VAR(TYPE, NAME, CNT, HEAP, DT) \ + if (err == MP_OKAY) { \ + (NAME) = (TYPE*)XMALLOC(sizeof(TYPE) * (CNT), (HEAP), DT); \ + if ((NAME) == NULL) { \ + err = MEMORY_E; \ + } \ + } + + #define SP_VAR_OK(NAME) ((NAME) != NULL) + + #define SP_FREE_VAR(NAME, HEAP, DT) \ + XFREE(NAME, (HEAP), DT) + #define SP_ZEROFREE_VAR(TYPE, NAME, CNT, HEAP, DT) \ + do { \ + if ((NAME) != NULL) { \ + ForceZero(NAME, sizeof(TYPE) * (CNT)); \ + } \ + SP_FREE_VAR(NAME, HEAP, DT); \ + } while (0) + #define SP_ZEROFREE_VAR_ALT(TYPE, NAME, FZ_NAME, CNT, HEAP, DT) \ + do { \ + if ((FZ_NAME) != NULL) { \ + ForceZero(FZ_NAME, sizeof(TYPE) * (CNT)); \ + } \ + SP_FREE_VAR(NAME, HEAP, DT); \ + } while (0) +#else + #define SP_DECL_VAR(TYPE, NAME, CNT) \ + TYPE NAME[CNT] + #define SP_ALLOC_VAR(TYPE, NAME, CNT, HEAP, DT) \ + WC_DO_NOTHING + #define SP_VAR_OK(NAME) (1) + #define SP_FREE_VAR(NAME, HEAP, DT) \ + WC_DO_NOTHING + #define SP_ZEROFREE_VAR(TYPE, NAME, CNT, HEAP, DT) \ + do { \ + if ((NAME) != NULL) { \ + ForceZero(NAME, sizeof(TYPE) * (CNT)); \ + } \ + } while (0) + #define SP_ZEROFREE_VAR_ALT(TYPE, NAME, FZ_NAME, CNT, HEAP, DT) \ + do { \ + if ((FZ_NAME) != NULL) { \ + ForceZero(FZ_NAME, sizeof(TYPE) * (CNT)); \ + } \ + } while (0) +#endif + #ifdef WOLFSSL_SP_X86_64_ASM #define SP_PRINT_NUM(var, name, total, words, bits) \ do { \ @@ -487,7 +538,7 @@ static WC_INLINE sp_digit div_2048_word_16(sp_digit d1, sp_digit d0, static WC_INLINE sp_digit div_2048_word_16(sp_digit d1, sp_digit d0, sp_digit div) { - register sp_digit r __asm__("rax"); + register sp_digit r asm("rax"); ASSERT_SAVED_VECTOR_REGISTERS(); __asm__ __volatile__ ( "divq %3" @@ -636,11 +687,7 @@ extern void sp_2048_get_from_table_16(sp_digit* r, sp_digit** table, int idx); 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(33 * 32) + 32]; -#endif + SP_DECL_VAR(sp_digit, td, (33 * 32) + 32); sp_digit* t[32]; sp_digit* rt = NULL; sp_digit* norm; @@ -658,16 +705,8 @@ static int sp_2048_mod_exp_16(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 * 32) + 32, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (33 * 32) + 32, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) @@ -805,9 +844,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -876,11 +913,7 @@ extern void sp_2048_get_from_table_avx2_16(sp_digit* r, sp_digit** table, int id 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(33 * 32) + 32]; -#endif + SP_DECL_VAR(sp_digit, td, (33 * 32) + 32); sp_digit* t[32]; sp_digit* rt = NULL; sp_digit* norm; @@ -898,16 +931,8 @@ static int sp_2048_mod_exp_avx2_16(sp_digit* r, const sp_digit* a, const sp_digi err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 * 32) + 32, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (33 * 32) + 32, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) @@ -1045,9 +1070,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -1169,7 +1192,7 @@ static WC_INLINE sp_digit div_2048_word_32(sp_digit d1, sp_digit d0, static WC_INLINE sp_digit div_2048_word_32(sp_digit d1, sp_digit d0, sp_digit div) { - register sp_digit r __asm__("rax"); + register sp_digit r asm("rax"); ASSERT_SAVED_VECTOR_REGISTERS(); __asm__ __volatile__ ( "divq %3" @@ -1413,11 +1436,7 @@ extern void sp_2048_get_from_table_32(sp_digit* r, sp_digit** table, int idx); 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(65 * 64) + 64]; -#endif + SP_DECL_VAR(sp_digit, td, (65 * 64) + 64); sp_digit* t[64]; sp_digit* rt = NULL; sp_digit* norm; @@ -1435,16 +1454,8 @@ static int sp_2048_mod_exp_32(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (65 * 64) + 64, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (65 * 64) + 64, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<64; i++) @@ -1615,9 +1626,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -1688,11 +1697,7 @@ extern void sp_2048_get_from_table_avx2_32(sp_digit* r, sp_digit** table, int id 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(65 * 64) + 64]; -#endif + SP_DECL_VAR(sp_digit, td, (65 * 64) + 64); sp_digit* t[64]; sp_digit* rt = NULL; sp_digit* norm; @@ -1710,16 +1715,8 @@ static int sp_2048_mod_exp_avx2_32(sp_digit* r, const sp_digit* a, const sp_digi err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (65 * 64) + 64, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (65 * 64) + 64, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<64; i++) @@ -1890,9 +1887,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -1915,11 +1910,7 @@ static int sp_2048_mod_exp_avx2_32(sp_digit* r, const sp_digit* a, const sp_digi int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[32 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 32 * 5); sp_digit* ah = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -1942,15 +1933,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 32 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { r = a + 32 * 2; m = r + 32 * 2; @@ -2098,9 +2081,7 @@ int sp_RsaPublic_2048(const byte* in, word32 inLen, const mp_int* em, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; } @@ -2128,11 +2109,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, const mp_int* pm, const mp_int* qm,const mp_int* dpm, const mp_int* dqm, const mp_int* qim, const mp_int* mm, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[32 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 32 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -2164,16 +2141,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 32 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 32; m = a + 64; @@ -2191,16 +2159,8 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only zeroing private "d" */ - ForceZero(d, sizeof(sp_digit) * 32); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); - #endif - } + /* only zeroing private "d" */ + SP_ZEROFREE_VAR(sp_digit, d, 32, NULL, DYNAMIC_TYPE_RSA); return err; } @@ -2241,11 +2201,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, const mp_int* pm, const mp_int* qm, const mp_int* dpm, const mp_int* dqm, const mp_int* qim, const mp_int* mm, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[16 * 11]; -#endif + SP_DECL_VAR(sp_digit, a, 16 * 11); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -2281,15 +2237,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 11, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 16 * 11, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 32 * 2; q = p + 16; @@ -2371,15 +2319,7 @@ int sp_RsaPrivate_2048(const byte* in, word32 inLen, const mp_int* dm, *outLen = 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 16 * 11); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 16 * 11, NULL, DYNAMIC_TYPE_RSA); return err; } @@ -2469,17 +2409,10 @@ int sp_ModExp_2048(const mp_int* base, const mp_int* exp, const mp_int* mod, mp_int* res) { int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit *b = NULL; - sp_digit *e = NULL; - sp_digit *m = NULL; + SP_DECL_VAR(sp_digit, b, 64); + SP_DECL_VAR(sp_digit, e, 32); + SP_DECL_VAR(sp_digit, m, 32); sp_digit* r; -#else - sp_digit b[64]; - sp_digit e[32]; - sp_digit m[32]; - sp_digit* r = b; -#endif #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif @@ -2500,20 +2433,12 @@ int sp_ModExp_2048(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK + SP_ALLOC_VAR(sp_digit, b, 64, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ALLOC_VAR(sp_digit, e, 32, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ALLOC_VAR(sp_digit, m, 32, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { - if (((b = (sp_digit *)XMALLOC(64 * sizeof(*b), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) || - ((e = (sp_digit *)XMALLOC(32 * sizeof(*e), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) || - ((m = (sp_digit *)XMALLOC(32 * sizeof(*m), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) - { - err = MEMORY_E; - } else { - r = b; - } - } -#endif + r = b; - if (err == MP_OKAY) { sp_2048_from_mp(b, 32, base); sp_2048_from_mp(e, 32, exp); sp_2048_from_mp(m, 32, mod); @@ -2532,16 +2457,9 @@ int sp_ModExp_2048(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_2048_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (e != NULL) { - XMEMSET(e, 0, 32); - XFREE(e, NULL, DYNAMIC_TYPE_TMP_BUFFER); - } - XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#else - XMEMSET(e, 0, sizeof(e)); -#endif + SP_FREE_VAR(m, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ZEROFREE_VAR(sp_digit, e, 32, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_FREE_VAR(b, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -2569,11 +2487,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[33 + 64]; -#endif + SP_DECL_VAR(sp_digit, td, 33 + 64); sp_digit* norm; sp_digit* tmp; sp_digit mp = 1; @@ -2591,23 +2505,10 @@ static int sp_2048_mod_exp_2_avx2_32(sp_digit* r, const sp_digit* e, int bits, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 + 64), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 33 + 64, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; -#ifdef WOLFSSL_SP_SMALL_STACK tmp = td + 64; -#else - tmp = &td[64]; -#endif sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_32(norm, m); @@ -2686,9 +2587,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -2707,11 +2606,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[33 + 64]; -#endif + SP_DECL_VAR(sp_digit, td, 33 + 64); sp_digit* norm; sp_digit* tmp; sp_digit mp = 1; @@ -2729,23 +2624,10 @@ static int sp_2048_mod_exp_2_32(sp_digit* r, const sp_digit* e, int bits, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 + 64), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 33 + 64, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; -#ifdef WOLFSSL_SP_SMALL_STACK tmp = td + 64; -#else - tmp = &td[64]; -#endif sp_2048_mont_setup(m, &mp); sp_2048_mont_norm_32(norm, m); @@ -2824,9 +2706,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -2849,17 +2729,10 @@ int sp_DhExp_2048(const mp_int* base, const byte* exp, word32 expLen, const mp_int* mod, byte* out, word32* outLen) { int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit *b = NULL; - sp_digit *e = NULL; - sp_digit *m = NULL; + SP_DECL_VAR(sp_digit, b, 64); + SP_DECL_VAR(sp_digit, e, 32); + SP_DECL_VAR(sp_digit, m, 32); sp_digit* r; -#else - sp_digit b[64]; - sp_digit e[32]; - sp_digit m[32]; - sp_digit* r = b; -#endif word32 i; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); @@ -2880,20 +2753,12 @@ int sp_DhExp_2048(const mp_int* base, const byte* exp, word32 expLen, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK + SP_ALLOC_VAR(sp_digit, b, 64, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ALLOC_VAR(sp_digit, e, 32, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ALLOC_VAR(sp_digit, m, 32, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { - if (((b = (sp_digit *)XMALLOC(64 * sizeof(*b), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) || - ((e = (sp_digit *)XMALLOC(32 * sizeof(*e), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) || - ((m = (sp_digit *)XMALLOC(32 * sizeof(*m), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) - { - err = MEMORY_E; - } else { - r = b; - } - } -#endif + r = b; - if (err == MP_OKAY) { sp_2048_from_mp(b, 32, base); sp_2048_from_bin(e, 32, exp, (int)expLen); sp_2048_from_mp(m, 32, mod); @@ -2933,16 +2798,9 @@ int sp_DhExp_2048(const mp_int* base, const byte* exp, word32 expLen, XMEMMOVE(out, out + i, *outLen); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (e != NULL) { - XMEMSET(e, 0, 32); - XFREE(e, NULL, DYNAMIC_TYPE_TMP_BUFFER); - } - XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#else - XMEMSET(e, 0, sizeof(e)); -#endif + SP_FREE_VAR(m, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ZEROFREE_VAR(sp_digit, e, 32, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_FREE_VAR(b, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -2960,17 +2818,10 @@ int sp_ModExp_1024(const mp_int* base, const mp_int* exp, const mp_int* mod, mp_int* res) { int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit *b = NULL; - sp_digit *e = NULL; - sp_digit *m = NULL; + SP_DECL_VAR(sp_digit, b, 32); + SP_DECL_VAR(sp_digit, e, 16); + SP_DECL_VAR(sp_digit, m, 16); sp_digit* r; -#else - sp_digit b[32]; - sp_digit e[16]; - sp_digit m[16]; - sp_digit* r = b; -#endif #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif @@ -2991,20 +2842,12 @@ int sp_ModExp_1024(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK + SP_ALLOC_VAR(sp_digit, b, 32, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ALLOC_VAR(sp_digit, e, 16, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ALLOC_VAR(sp_digit, m, 16, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { - if (((b = (sp_digit *)XMALLOC(32 * sizeof(*b), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) || - ((e = (sp_digit *)XMALLOC(16 * sizeof(*e), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) || - ((m = (sp_digit *)XMALLOC(16 * sizeof(*m), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) - { - err = MEMORY_E; - } else { - r = b; - } - } -#endif + r = b; - if (err == MP_OKAY) { sp_2048_from_mp(b, 16, base); sp_2048_from_mp(e, 16, exp); sp_2048_from_mp(m, 16, mod); @@ -3024,16 +2867,9 @@ int sp_ModExp_1024(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_2048_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (e != NULL) { - XMEMSET(e, 0, 16); - XFREE(e, NULL, DYNAMIC_TYPE_TMP_BUFFER); - } - XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#else - XMEMSET(e, 0, sizeof(e)); -#endif + SP_FREE_VAR(m, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ZEROFREE_VAR(sp_digit, e, 16, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_FREE_VAR(b, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -3494,7 +3330,7 @@ static WC_INLINE sp_digit div_3072_word_24(sp_digit d1, sp_digit d0, static WC_INLINE sp_digit div_3072_word_24(sp_digit d1, sp_digit d0, sp_digit div) { - register sp_digit r __asm__("rax"); + register sp_digit r asm("rax"); ASSERT_SAVED_VECTOR_REGISTERS(); __asm__ __volatile__ ( "divq %3" @@ -3643,11 +3479,7 @@ extern void sp_3072_get_from_table_24(sp_digit* r, sp_digit** table, int idx); 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(33 * 48) + 48]; -#endif + SP_DECL_VAR(sp_digit, td, (33 * 48) + 48); sp_digit* t[32]; sp_digit* rt = NULL; sp_digit* norm; @@ -3665,16 +3497,8 @@ static int sp_3072_mod_exp_24(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 * 48) + 48, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (33 * 48) + 48, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) @@ -3812,9 +3636,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -3883,11 +3705,7 @@ extern void sp_3072_get_from_table_avx2_24(sp_digit* r, sp_digit** table, int id 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(33 * 48) + 48]; -#endif + SP_DECL_VAR(sp_digit, td, (33 * 48) + 48); sp_digit* t[32]; sp_digit* rt = NULL; sp_digit* norm; @@ -3905,16 +3723,8 @@ static int sp_3072_mod_exp_avx2_24(sp_digit* r, const sp_digit* a, const sp_digi err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (33 * 48) + 48, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (33 * 48) + 48, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<32; i++) @@ -4052,9 +3862,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -4176,7 +3984,7 @@ static WC_INLINE sp_digit div_3072_word_48(sp_digit d1, sp_digit d0, static WC_INLINE sp_digit div_3072_word_48(sp_digit d1, sp_digit d0, sp_digit div) { - register sp_digit r __asm__("rax"); + register sp_digit r asm("rax"); ASSERT_SAVED_VECTOR_REGISTERS(); __asm__ __volatile__ ( "divq %3" @@ -4420,11 +4228,7 @@ extern void sp_3072_get_from_table_48(sp_digit* r, sp_digit** table, int idx); 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(17 * 96) + 96]; -#endif + SP_DECL_VAR(sp_digit, td, (17 * 96) + 96); sp_digit* t[16]; sp_digit* rt = NULL; sp_digit* norm; @@ -4442,16 +4246,8 @@ static int sp_3072_mod_exp_48(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (17 * 96) + 96, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (17 * 96) + 96, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) @@ -4570,9 +4366,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -4643,11 +4437,7 @@ extern void sp_3072_get_from_table_avx2_48(sp_digit* r, sp_digit** table, int id 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(17 * 96) + 96]; -#endif + SP_DECL_VAR(sp_digit, td, (17 * 96) + 96); sp_digit* t[16]; sp_digit* rt = NULL; sp_digit* norm; @@ -4665,16 +4455,8 @@ static int sp_3072_mod_exp_avx2_48(sp_digit* r, const sp_digit* a, const sp_digi err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (17 * 96) + 96, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (17 * 96) + 96, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) @@ -4793,9 +4575,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -4818,11 +4598,7 @@ static int sp_3072_mod_exp_avx2_48(sp_digit* r, const sp_digit* a, const sp_digi int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[48 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 48 * 5); sp_digit* ah = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -4845,15 +4621,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 48 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 48 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { r = a + 48 * 2; m = r + 48 * 2; @@ -5001,9 +4769,7 @@ int sp_RsaPublic_3072(const byte* in, word32 inLen, const mp_int* em, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; } @@ -5031,11 +4797,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, const mp_int* pm, const mp_int* qm,const mp_int* dpm, const mp_int* dqm, const mp_int* qim, const mp_int* mm, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[48 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 48 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -5067,16 +4829,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 48 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 48 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 48; m = a + 96; @@ -5094,16 +4847,8 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only zeroing private "d" */ - ForceZero(d, sizeof(sp_digit) * 48); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); - #endif - } + /* only zeroing private "d" */ + SP_ZEROFREE_VAR(sp_digit, d, 48, NULL, DYNAMIC_TYPE_RSA); return err; } @@ -5144,11 +4889,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, const mp_int* pm, const mp_int* qm, const mp_int* dpm, const mp_int* dqm, const mp_int* qim, const mp_int* mm, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[24 * 11]; -#endif + SP_DECL_VAR(sp_digit, a, 24 * 11); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -5184,15 +4925,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 24 * 11, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 24 * 11, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 48 * 2; q = p + 24; @@ -5274,15 +5007,7 @@ int sp_RsaPrivate_3072(const byte* in, word32 inLen, const mp_int* dm, *outLen = 384; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 24 * 11); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 24 * 11, NULL, DYNAMIC_TYPE_RSA); return err; } @@ -5372,17 +5097,10 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, mp_int* res) { int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit *b = NULL; - sp_digit *e = NULL; - sp_digit *m = NULL; + SP_DECL_VAR(sp_digit, b, 96); + SP_DECL_VAR(sp_digit, e, 48); + SP_DECL_VAR(sp_digit, m, 48); sp_digit* r; -#else - sp_digit b[96]; - sp_digit e[48]; - sp_digit m[48]; - sp_digit* r = b; -#endif #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif @@ -5403,20 +5121,12 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK + SP_ALLOC_VAR(sp_digit, b, 96, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ALLOC_VAR(sp_digit, e, 48, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ALLOC_VAR(sp_digit, m, 48, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { - if (((b = (sp_digit *)XMALLOC(96 * sizeof(*b), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) || - ((e = (sp_digit *)XMALLOC(48 * sizeof(*e), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) || - ((m = (sp_digit *)XMALLOC(48 * sizeof(*m), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) - { - err = MEMORY_E; - } else { - r = b; - } - } -#endif + r = b; - if (err == MP_OKAY) { sp_3072_from_mp(b, 48, base); sp_3072_from_mp(e, 48, exp); sp_3072_from_mp(m, 48, mod); @@ -5435,16 +5145,9 @@ int sp_ModExp_3072(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_3072_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (e != NULL) { - XMEMSET(e, 0, 48); - XFREE(e, NULL, DYNAMIC_TYPE_TMP_BUFFER); - } - XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#else - XMEMSET(e, 0, sizeof(e)); -#endif + SP_FREE_VAR(m, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ZEROFREE_VAR(sp_digit, e, 48, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_FREE_VAR(b, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -5472,11 +5175,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[49 + 96]; -#endif + SP_DECL_VAR(sp_digit, td, 49 + 96); sp_digit* norm; sp_digit* tmp; sp_digit mp = 1; @@ -5494,23 +5193,10 @@ static int sp_3072_mod_exp_2_avx2_48(sp_digit* r, const sp_digit* e, int bits, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (49 + 96), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 49 + 96, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; -#ifdef WOLFSSL_SP_SMALL_STACK tmp = td + 96; -#else - tmp = &td[96]; -#endif sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_48(norm, m); @@ -5589,9 +5275,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -5610,11 +5294,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[49 + 96]; -#endif + SP_DECL_VAR(sp_digit, td, 49 + 96); sp_digit* norm; sp_digit* tmp; sp_digit mp = 1; @@ -5632,23 +5312,10 @@ static int sp_3072_mod_exp_2_48(sp_digit* r, const sp_digit* e, int bits, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (49 + 96), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 49 + 96, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; -#ifdef WOLFSSL_SP_SMALL_STACK tmp = td + 96; -#else - tmp = &td[96]; -#endif sp_3072_mont_setup(m, &mp); sp_3072_mont_norm_48(norm, m); @@ -5727,9 +5394,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -5752,17 +5417,10 @@ int sp_DhExp_3072(const mp_int* base, const byte* exp, word32 expLen, const mp_int* mod, byte* out, word32* outLen) { int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit *b = NULL; - sp_digit *e = NULL; - sp_digit *m = NULL; + SP_DECL_VAR(sp_digit, b, 96); + SP_DECL_VAR(sp_digit, e, 48); + SP_DECL_VAR(sp_digit, m, 48); sp_digit* r; -#else - sp_digit b[96]; - sp_digit e[48]; - sp_digit m[48]; - sp_digit* r = b; -#endif word32 i; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); @@ -5783,20 +5441,12 @@ int sp_DhExp_3072(const mp_int* base, const byte* exp, word32 expLen, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK + SP_ALLOC_VAR(sp_digit, b, 96, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ALLOC_VAR(sp_digit, e, 48, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ALLOC_VAR(sp_digit, m, 48, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { - if (((b = (sp_digit *)XMALLOC(96 * sizeof(*b), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) || - ((e = (sp_digit *)XMALLOC(48 * sizeof(*e), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) || - ((m = (sp_digit *)XMALLOC(48 * sizeof(*m), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) - { - err = MEMORY_E; - } else { - r = b; - } - } -#endif + r = b; - if (err == MP_OKAY) { sp_3072_from_mp(b, 48, base); sp_3072_from_bin(e, 48, exp, (int)expLen); sp_3072_from_mp(m, 48, mod); @@ -5836,16 +5486,9 @@ int sp_DhExp_3072(const mp_int* base, const byte* exp, word32 expLen, XMEMMOVE(out, out + i, *outLen); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (e != NULL) { - XMEMSET(e, 0, 48); - XFREE(e, NULL, DYNAMIC_TYPE_TMP_BUFFER); - } - XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#else - XMEMSET(e, 0, sizeof(e)); -#endif + SP_FREE_VAR(m, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ZEROFREE_VAR(sp_digit, e, 48, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_FREE_VAR(b, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -5863,17 +5506,10 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, mp_int* res) { int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit *b = NULL; - sp_digit *e = NULL; - sp_digit *m = NULL; + SP_DECL_VAR(sp_digit, b, 48); + SP_DECL_VAR(sp_digit, e, 24); + SP_DECL_VAR(sp_digit, m, 24); sp_digit* r; -#else - sp_digit b[48]; - sp_digit e[24]; - sp_digit m[24]; - sp_digit* r = b; -#endif #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif @@ -5894,20 +5530,12 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK + SP_ALLOC_VAR(sp_digit, b, 48, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ALLOC_VAR(sp_digit, e, 24, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ALLOC_VAR(sp_digit, m, 24, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { - if (((b = (sp_digit *)XMALLOC(48 * sizeof(*b), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) || - ((e = (sp_digit *)XMALLOC(24 * sizeof(*e), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) || - ((m = (sp_digit *)XMALLOC(24 * sizeof(*m), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) - { - err = MEMORY_E; - } else { - r = b; - } - } -#endif + r = b; - if (err == MP_OKAY) { sp_3072_from_mp(b, 24, base); sp_3072_from_mp(e, 24, exp); sp_3072_from_mp(m, 24, mod); @@ -5927,16 +5555,9 @@ int sp_ModExp_1536(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_3072_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (e != NULL) { - XMEMSET(e, 0, 24); - XFREE(e, NULL, DYNAMIC_TYPE_TMP_BUFFER); - } - XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#else - XMEMSET(e, 0, sizeof(e)); -#endif + SP_FREE_VAR(m, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ZEROFREE_VAR(sp_digit, e, 24, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_FREE_VAR(b, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -6307,7 +5928,7 @@ static WC_INLINE sp_digit div_4096_word_64(sp_digit d1, sp_digit d0, static WC_INLINE sp_digit div_4096_word_64(sp_digit d1, sp_digit d0, sp_digit div) { - register sp_digit r __asm__("rax"); + register sp_digit r asm("rax"); ASSERT_SAVED_VECTOR_REGISTERS(); __asm__ __volatile__ ( "divq %3" @@ -6551,11 +6172,7 @@ extern void sp_4096_get_from_table_64(sp_digit* r, sp_digit** table, int idx); 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(17 * 128) + 128]; -#endif + SP_DECL_VAR(sp_digit, td, (17 * 128) + 128); sp_digit* t[16]; sp_digit* rt = NULL; sp_digit* norm; @@ -6573,16 +6190,8 @@ static int sp_4096_mod_exp_64(sp_digit* r, const sp_digit* a, const sp_digit* e, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (17 * 128) + 128, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (17 * 128) + 128, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) @@ -6701,9 +6310,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -6774,11 +6381,7 @@ extern void sp_4096_get_from_table_avx2_64(sp_digit* r, sp_digit** table, int id 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[(17 * 128) + 128]; -#endif + SP_DECL_VAR(sp_digit, td, (17 * 128) + 128); sp_digit* t[16]; sp_digit* rt = NULL; sp_digit* norm; @@ -6796,16 +6399,8 @@ static int sp_4096_mod_exp_avx2_64(sp_digit* r, const sp_digit* a, const sp_digi err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (17 * 128) + 128, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, td, (17 * 128) + 128, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; for (i=0; i<16; i++) @@ -6924,9 +6519,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -6949,11 +6542,7 @@ static int sp_4096_mod_exp_avx2_64(sp_digit* r, const sp_digit* a, const sp_digi int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, const mp_int* mm, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[64 * 5]; -#endif + SP_DECL_VAR(sp_digit, a, 64 * 5); sp_digit* ah = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -6976,15 +6565,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 64 * 5, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 64 * 5, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { r = a + 64 * 2; m = r + 64 * 2; @@ -7132,9 +6713,7 @@ int sp_RsaPublic_4096(const byte* in, word32 inLen, const mp_int* em, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); -#endif + SP_FREE_VAR(a, NULL, DYNAMIC_TYPE_RSA); return err; } @@ -7162,11 +6741,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, const mp_int* pm, const mp_int* qm,const mp_int* dpm, const mp_int* dqm, const mp_int* qim, const mp_int* mm, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* d = NULL; -#else - sp_digit d[64 * 4]; -#endif + SP_DECL_VAR(sp_digit, d, 64 * 4); sp_digit* a = NULL; sp_digit* m = NULL; sp_digit* r = NULL; @@ -7198,16 +6773,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - d = (sp_digit*)XMALLOC(sizeof(sp_digit) * 64 * 4, NULL, - DYNAMIC_TYPE_RSA); - if (d == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, d, 64 * 4, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { a = d + 64; m = a + 128; @@ -7225,16 +6791,8 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (d != NULL) -#endif - { - /* only zeroing private "d" */ - ForceZero(d, sizeof(sp_digit) * 64); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(d, NULL, DYNAMIC_TYPE_RSA); - #endif - } + /* only zeroing private "d" */ + SP_ZEROFREE_VAR(sp_digit, d, 64, NULL, DYNAMIC_TYPE_RSA); return err; } @@ -7275,11 +6833,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, const mp_int* pm, const mp_int* qm, const mp_int* dpm, const mp_int* dqm, const mp_int* qim, const mp_int* mm, byte* out, word32* outLen) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* a = NULL; -#else - sp_digit a[32 * 11]; -#endif + SP_DECL_VAR(sp_digit, a, 32 * 11); sp_digit* p = NULL; sp_digit* q = NULL; sp_digit* dp = NULL; @@ -7315,15 +6869,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - a = (sp_digit*)XMALLOC(sizeof(sp_digit) * 32 * 11, NULL, - DYNAMIC_TYPE_RSA); - if (a == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, a, 32 * 11, NULL, DYNAMIC_TYPE_RSA); if (err == MP_OKAY) { p = a + 64 * 2; q = p + 32; @@ -7405,15 +6951,7 @@ int sp_RsaPrivate_4096(const byte* in, word32 inLen, const mp_int* dm, *outLen = 512; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (a != NULL) -#endif - { - ForceZero(a, sizeof(sp_digit) * 32 * 11); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(a, NULL, DYNAMIC_TYPE_RSA); - #endif - } + SP_ZEROFREE_VAR(sp_digit, a, 32 * 11, NULL, DYNAMIC_TYPE_RSA); return err; } @@ -7503,17 +7041,10 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, mp_int* res) { int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit *b = NULL; - sp_digit *e = NULL; - sp_digit *m = NULL; + SP_DECL_VAR(sp_digit, b, 128); + SP_DECL_VAR(sp_digit, e, 64); + SP_DECL_VAR(sp_digit, m, 64); sp_digit* r; -#else - sp_digit b[128]; - sp_digit e[64]; - sp_digit m[64]; - sp_digit* r = b; -#endif #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif @@ -7534,20 +7065,12 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK + SP_ALLOC_VAR(sp_digit, b, 128, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ALLOC_VAR(sp_digit, e, 64, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ALLOC_VAR(sp_digit, m, 64, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { - if (((b = (sp_digit *)XMALLOC(128 * sizeof(*b), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) || - ((e = (sp_digit *)XMALLOC(64 * sizeof(*e), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) || - ((m = (sp_digit *)XMALLOC(64 * sizeof(*m), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) - { - err = MEMORY_E; - } else { - r = b; - } - } -#endif + r = b; - if (err == MP_OKAY) { sp_4096_from_mp(b, 64, base); sp_4096_from_mp(e, 64, exp); sp_4096_from_mp(m, 64, mod); @@ -7566,16 +7089,9 @@ int sp_ModExp_4096(const mp_int* base, const mp_int* exp, const mp_int* mod, err = sp_4096_to_mp(r, res); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (e != NULL) { - XMEMSET(e, 0, 64); - XFREE(e, NULL, DYNAMIC_TYPE_TMP_BUFFER); - } - XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#else - XMEMSET(e, 0, sizeof(e)); -#endif + SP_FREE_VAR(m, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ZEROFREE_VAR(sp_digit, e, 64, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_FREE_VAR(b, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -7603,11 +7119,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[65 + 128]; -#endif + SP_DECL_VAR(sp_digit, td, 65 + 128); sp_digit* norm; sp_digit* tmp; sp_digit mp = 1; @@ -7625,23 +7137,10 @@ static int sp_4096_mod_exp_2_avx2_64(sp_digit* r, const sp_digit* e, int bits, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (65 + 128), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 65 + 128, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; -#ifdef WOLFSSL_SP_SMALL_STACK tmp = td + 128; -#else - tmp = &td[128]; -#endif sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_64(norm, m); @@ -7720,9 +7219,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -7741,11 +7238,7 @@ 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) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* td = NULL; -#else - sp_digit td[65 + 128]; -#endif + SP_DECL_VAR(sp_digit, td, 65 + 128); sp_digit* norm; sp_digit* tmp; sp_digit mp = 1; @@ -7763,23 +7256,10 @@ static int sp_4096_mod_exp_2_64(sp_digit* r, const sp_digit* e, int bits, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - td = (sp_digit*)XMALLOC(sizeof(sp_digit) * (65 + 128), NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (td == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, td, 65 + 128, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { norm = td; -#ifdef WOLFSSL_SP_SMALL_STACK tmp = td + 128; -#else - tmp = &td[128]; -#endif sp_4096_mont_setup(m, &mp); sp_4096_mont_norm_64(norm, m); @@ -7858,9 +7338,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_SP_SMALL_STACK - XFREE(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#endif + SP_FREE_VAR(td, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -7883,17 +7361,10 @@ int sp_DhExp_4096(const mp_int* base, const byte* exp, word32 expLen, const mp_int* mod, byte* out, word32* outLen) { int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit *b = NULL; - sp_digit *e = NULL; - sp_digit *m = NULL; + SP_DECL_VAR(sp_digit, b, 128); + SP_DECL_VAR(sp_digit, e, 64); + SP_DECL_VAR(sp_digit, m, 64); sp_digit* r; -#else - sp_digit b[128]; - sp_digit e[64]; - sp_digit m[64]; - sp_digit* r = b; -#endif word32 i; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); @@ -7914,20 +7385,12 @@ int sp_DhExp_4096(const mp_int* base, const byte* exp, word32 expLen, err = MP_VAL; } -#ifdef WOLFSSL_SP_SMALL_STACK + SP_ALLOC_VAR(sp_digit, b, 128, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ALLOC_VAR(sp_digit, e, 64, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ALLOC_VAR(sp_digit, m, 64, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (err == MP_OKAY) { - if (((b = (sp_digit *)XMALLOC(128 * sizeof(*b), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) || - ((e = (sp_digit *)XMALLOC(64 * sizeof(*e), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) || - ((m = (sp_digit *)XMALLOC(64 * sizeof(*m), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) - { - err = MEMORY_E; - } else { - r = b; - } - } -#endif + r = b; - if (err == MP_OKAY) { sp_4096_from_mp(b, 64, base); sp_4096_from_bin(e, 64, exp, (int)expLen); sp_4096_from_mp(m, 64, mod); @@ -7967,16 +7430,9 @@ int sp_DhExp_4096(const mp_int* base, const byte* exp, word32 expLen, XMEMMOVE(out, out + i, *outLen); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (e != NULL) { - XMEMSET(e, 0, 64); - XFREE(e, NULL, DYNAMIC_TYPE_TMP_BUFFER); - } - XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER); -#else - XMEMSET(e, 0, sizeof(e)); -#endif + SP_FREE_VAR(m, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_ZEROFREE_VAR(sp_digit, e, 64, NULL, DYNAMIC_TYPE_TMP_BUFFER); + SP_FREE_VAR(b, NULL, DYNAMIC_TYPE_TMP_BUFFER); return err; } @@ -9475,13 +8931,8 @@ extern void sp_256_get_point_33_avx2_4(sp_point_256* r, const sp_point_256* tabl static int sp_256_ecc_mulmod_win_add_sub_4(sp_point_256* r, const sp_point_256* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_256 t[33+2]; - sp_digit tmp[2 * 4 * 6]; -#endif + SP_DECL_VAR(sp_point_256, t, 33+2); + SP_DECL_VAR(sp_digit, tmp, 2 * 4 * 6); sp_point_256* rt = NULL; sp_point_256* p = NULL; sp_digit* negy; @@ -9493,19 +8944,8 @@ static int sp_256_ecc_mulmod_win_add_sub_4(sp_point_256* r, const sp_point_256* (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * - (33+2), heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 4 * 6, - heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, t, 33+2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 4 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 33; p = t + 33+1; @@ -9589,10 +9029,8 @@ static int sp_256_ecc_mulmod_win_add_sub_4(sp_point_256* r, const sp_point_256* } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; } @@ -10508,13 +9946,8 @@ static void sp_256_proj_point_add_sub_avx2_4(sp_point_256* ra, static int sp_256_ecc_mulmod_win_add_sub_avx2_4(sp_point_256* r, const sp_point_256* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_256 t[33+2]; - sp_digit tmp[2 * 4 * 6]; -#endif + SP_DECL_VAR(sp_point_256, t, 33+2); + SP_DECL_VAR(sp_digit, tmp, 2 * 4 * 6); sp_point_256* rt = NULL; sp_point_256* p = NULL; sp_digit* negy; @@ -10526,19 +9959,8 @@ static int sp_256_ecc_mulmod_win_add_sub_avx2_4(sp_point_256* r, const sp_point_ (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * - (33+2), heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 4 * 6, - heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, t, 33+2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 4 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 33; p = t + 33+1; @@ -10622,10 +10044,8 @@ static int sp_256_ecc_mulmod_win_add_sub_avx2_4(sp_point_256* r, const sp_point_ } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; } @@ -10754,11 +10174,7 @@ static void sp_256_proj_to_affine_4(sp_point_256* a, sp_digit* t) static int sp_256_gen_stripe_table_4(const sp_point_256* a, sp_table_entry_256* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* t = NULL; -#else - sp_point_256 t[3]; -#endif + SP_DECL_VAR(sp_point_256, t, 3); sp_point_256* s1 = NULL; sp_point_256* s2 = NULL; int i; @@ -10767,13 +10183,7 @@ static int sp_256_gen_stripe_table_4(const sp_point_256* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_256, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -10822,9 +10232,7 @@ static int sp_256_gen_stripe_table_4(const sp_point_256* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -10865,13 +10273,8 @@ static int sp_256_ecc_mulmod_stripe_4(sp_point_256* r, const sp_point_256* g, const sp_table_entry_256* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_256 rt[2]; - sp_digit t[2 * 4 * 5]; -#endif + SP_DECL_VAR(sp_point_256, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 4 * 5); sp_point_256* p = NULL; int i; int j; @@ -10885,19 +10288,8 @@ static int sp_256_ecc_mulmod_stripe_4(sp_point_256* r, const sp_point_256* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 4 * 5, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 4 * 5, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -10951,10 +10343,8 @@ static int sp_256_ecc_mulmod_stripe_4(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -11072,20 +10462,11 @@ static int sp_256_ecc_mulmod_4(sp_point_256* r, const sp_point_256* g, #ifndef FP_ECC return sp_256_ecc_mulmod_win_add_sub_4(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 4 * 5]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 4 * 5); sp_cache_256_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 4 * 5, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 4 * 5, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -11118,9 +10499,7 @@ static int sp_256_ecc_mulmod_4(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -11243,11 +10622,7 @@ static void sp_256_proj_to_affine_avx2_4(sp_point_256* a, sp_digit* t) static int sp_256_gen_stripe_table_avx2_4(const sp_point_256* a, sp_table_entry_256* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* t = NULL; -#else - sp_point_256 t[3]; -#endif + SP_DECL_VAR(sp_point_256, t, 3); sp_point_256* s1 = NULL; sp_point_256* s2 = NULL; int i; @@ -11256,13 +10631,7 @@ static int sp_256_gen_stripe_table_avx2_4(const sp_point_256* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_256, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -11311,9 +10680,7 @@ static int sp_256_gen_stripe_table_avx2_4(const sp_point_256* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -11340,13 +10707,8 @@ static int sp_256_ecc_mulmod_stripe_avx2_4(sp_point_256* r, const sp_point_256* const sp_table_entry_256* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_256 rt[2]; - sp_digit t[2 * 4 * 5]; -#endif + SP_DECL_VAR(sp_point_256, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 4 * 5); sp_point_256* p = NULL; int i; int j; @@ -11360,19 +10722,8 @@ static int sp_256_ecc_mulmod_stripe_avx2_4(sp_point_256* r, const sp_point_256* (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 4 * 5, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 4 * 5, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -11426,10 +10777,8 @@ static int sp_256_ecc_mulmod_stripe_avx2_4(sp_point_256* r, const sp_point_256* } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -11452,20 +10801,11 @@ static int sp_256_ecc_mulmod_avx2_4(sp_point_256* r, const sp_point_256* g, #ifndef FP_ECC return sp_256_ecc_mulmod_win_add_sub_avx2_4(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 4 * 5]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 4 * 5); sp_cache_256_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 4 * 5, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 4 * 5, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -11498,9 +10838,7 @@ static int sp_256_ecc_mulmod_avx2_4(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -11519,31 +10857,15 @@ static int sp_256_ecc_mulmod_avx2_4(sp_point_256* r, const sp_point_256* g, int sp_ecc_mulmod_256(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[1]; - sp_digit k[4]; -#endif + SP_DECL_VAR(sp_point_256, point, 1); + SP_DECL_VAR(sp_digit, k, 4); int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(k, 4, km); sp_256_point_from_ecc_point_4(point, gm); @@ -11561,10 +10883,8 @@ int sp_ecc_mulmod_256(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_256_point_to_ecc_point_4(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -11584,13 +10904,8 @@ int sp_ecc_mulmod_256(const mp_int* km, const ecc_point* gm, ecc_point* r, int sp_ecc_mulmod_add_256(const mp_int* km, const ecc_point* gm, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[2]; - sp_digit k[4 + 4 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_256, point, 2); + SP_DECL_VAR(sp_digit, k, 4 + 4 * 2 * 6); sp_point_256* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; @@ -11598,20 +10913,8 @@ int sp_ecc_mulmod_add_256(const mp_int* km, const ecc_point* gm, word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (4 + 4 * 2 * 6), heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 4 + 4 * 2 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 4; @@ -11663,10 +10966,8 @@ int sp_ecc_mulmod_add_256(const mp_int* km, const ecc_point* gm, err = sp_256_point_to_ecc_point_4(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -24103,13 +23404,8 @@ static int sp_256_ecc_mulmod_add_only_4(sp_point_256* r, const sp_point_256* g, const sp_table_entry_256* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* rt = NULL; - sp_digit* tmp = NULL; -#else - sp_point_256 rt[2]; - sp_digit tmp[2 * 4 * 5]; -#endif + SP_DECL_VAR(sp_point_256, rt, 2); + SP_DECL_VAR(sp_digit, tmp, 2 * 4 * 5); sp_point_256* p = NULL; sp_digit* negy = NULL; int i; @@ -24121,19 +23417,8 @@ static int sp_256_ecc_mulmod_add_only_4(sp_point_256* r, const sp_point_256* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 4 * 5, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 4 * 5, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { negy = tmp; p = rt + 1; @@ -24182,18 +23467,8 @@ static int sp_256_ecc_mulmod_add_only_4(sp_point_256* r, const sp_point_256* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 4 * 5); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 4 * 5, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); return err; } @@ -24236,13 +23511,8 @@ static int sp_256_ecc_mulmod_add_only_avx2_4(sp_point_256* r, const sp_point_256 const sp_table_entry_256* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* rt = NULL; - sp_digit* tmp = NULL; -#else - sp_point_256 rt[2]; - sp_digit tmp[2 * 4 * 5]; -#endif + SP_DECL_VAR(sp_point_256, rt, 2); + SP_DECL_VAR(sp_digit, tmp, 2 * 4 * 5); sp_point_256* p = NULL; sp_digit* negy = NULL; int i; @@ -24254,19 +23524,8 @@ static int sp_256_ecc_mulmod_add_only_avx2_4(sp_point_256* r, const sp_point_256 (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 4 * 5, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 4 * 5, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { negy = tmp; p = rt + 1; @@ -24315,18 +23574,8 @@ static int sp_256_ecc_mulmod_add_only_avx2_4(sp_point_256* r, const sp_point_256 } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 4 * 5); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 4 * 5, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); return err; } @@ -24361,31 +23610,15 @@ static int sp_256_ecc_mulmod_base_avx2_4(sp_point_256* r, const sp_digit* k, */ int sp_ecc_mulmod_base_256(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[1]; - sp_digit k[4]; -#endif + SP_DECL_VAR(sp_point_256, point, 1); + SP_DECL_VAR(sp_digit, k, 4); int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(k, 4, km); @@ -24402,10 +23635,8 @@ int sp_ecc_mulmod_base_256(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_256_point_to_ecc_point_4(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -24424,13 +23655,8 @@ int sp_ecc_mulmod_base_256(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_256(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[2]; - sp_digit k[4 + 4 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_256, point, 2); + SP_DECL_VAR(sp_digit, k, 4 + 4 * 2 * 6); sp_point_256* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; @@ -24438,20 +23664,8 @@ int sp_ecc_mulmod_base_add_256(const mp_int* km, const ecc_point* am, word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (4 + 4 * 2 * 6), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 4 + 4 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 4; @@ -24502,10 +23716,8 @@ int sp_ecc_mulmod_base_add_256(const mp_int* km, const ecc_point* am, err = sp_256_point_to_ecc_point_4(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -24602,17 +23814,12 @@ static int sp_256_ecc_gen_k_4(WC_RNG* rng, sp_digit* k) */ int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_DECL_VAR(sp_point_256, point, 2); #else - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - sp_point_256 point[2]; - #else - sp_point_256 point[1]; - #endif - sp_digit k[4]; + SP_DECL_VAR(sp_point_256, point, 1); #endif + SP_DECL_VAR(sp_digit, k, 4); #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN sp_point_256* infinity = NULL; #endif @@ -24624,22 +23831,12 @@ int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, DYNAMIC_TYPE_ECC); - #else - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, DYNAMIC_TYPE_ECC); - #endif - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_ALLOC_VAR(sp_point_256, point, 2, heap, DYNAMIC_TYPE_ECC); +#else + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); #endif - + SP_ALLOC_VAR(sp_digit, k, 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN infinity = point + 1; @@ -24684,11 +23881,9 @@ int sp_ecc_make_key_256(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) err = sp_256_point_to_ecc_point_4(point, pub); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); /* point is not sensitive, so no need to zeroize */ - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -24822,13 +24017,8 @@ static void sp_256_to_bin_4(sp_digit* r, byte* a) int sp_ecc_secret_gen_256(const mp_int* priv, const ecc_point* pub, byte* out, word32* outLen, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* point = NULL; - sp_digit* k = NULL; -#else - sp_point_256 point[1]; - sp_digit k[4]; -#endif + SP_DECL_VAR(sp_point_256, point, 1); + SP_DECL_VAR(sp_digit, k, 4); int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); @@ -24838,21 +24028,8 @@ int sp_ecc_secret_gen_256(const mp_int* priv, const ecc_point* pub, byte* out, err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(k, 4, priv); sp_256_point_from_ecc_point_4(point, pub); @@ -24870,10 +24047,8 @@ int sp_ecc_secret_gen_256(const mp_int* priv, const ecc_point* pub, byte* out, *outLen = 32; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -24991,7 +24166,7 @@ static WC_INLINE sp_digit div_256_word_4(sp_digit d1, sp_digit d0, static WC_INLINE sp_digit div_256_word_4(sp_digit d1, sp_digit d0, sp_digit div) { - register sp_digit r __asm__("rax"); + register sp_digit r asm("rax"); ASSERT_SAVED_VECTOR_REGISTERS(); __asm__ __volatile__ ( "divq %3" @@ -25682,13 +24857,8 @@ static int sp_256_calc_s_4(sp_digit* s, const sp_digit* r, sp_digit* k, int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* e = NULL; - sp_point_256* point = NULL; -#else - sp_digit e[8 * 2 * 4]; - sp_point_256 point[1]; -#endif + SP_DECL_VAR(sp_digit, e, 8 * 2 * 4); + SP_DECL_VAR(sp_point_256, point, 1); sp_digit* x = NULL; sp_digit* k = NULL; sp_digit* r = NULL; @@ -25703,21 +24873,8 @@ int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_256*)XMALLOC(sizeof(sp_point_256), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - e = (sp_digit*)XMALLOC(sizeof(sp_digit) * 8 * 2 * 4, heap, - DYNAMIC_TYPE_ECC); - if (e == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, e, 8 * 2 * 4, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { x = e + 2 * 4; k = e + 4 * 4; @@ -25789,24 +24946,8 @@ int sp_ecc_sign_256(const byte* hash, word32 hashLen, WC_RNG* rng, err = sp_256_to_mp(s, sm); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (e != NULL) -#endif - { - ForceZero(e, sizeof(sp_digit) * 8 * 2 * 4); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(e, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (point != NULL) -#endif - { - ForceZero(point, sizeof(sp_point_256)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(point, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_256, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_digit, e, 8 * 2 * 4, heap, DYNAMIC_TYPE_ECC); return err; } @@ -26168,13 +25309,8 @@ int sp_ecc_verify_256(const byte* hash, word32 hashLen, const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* rm, const mp_int* sm, int* res, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u1 = NULL; - sp_point_256* p1 = NULL; -#else - sp_digit u1[18 * 4]; - sp_point_256 p1[2]; -#endif + SP_DECL_VAR(sp_digit, u1, 18 * 4); + SP_DECL_VAR(sp_point_256, p1, 2); sp_digit* u2 = NULL; sp_digit* s = NULL; sp_digit* tmp = NULL; @@ -26186,21 +25322,8 @@ int sp_ecc_verify_256(const byte* hash, word32 hashLen, const mp_int* pX, word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p1 = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (p1 == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - u1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 4, heap, - DYNAMIC_TYPE_ECC); - if (u1 == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, u1, 18 * 4, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p1, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { u2 = u1 + 2 * 4; s = u1 + 4 * 4; @@ -26279,11 +25402,8 @@ int sp_ecc_verify_256(const byte* hash, word32 hashLen, const mp_int* pX, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u1, heap, DYNAMIC_TYPE_ECC); - XFREE(p1, heap, DYNAMIC_TYPE_ECC); -#endif - + SP_FREE_VAR(p1, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(u1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -26449,21 +25569,13 @@ int sp_ecc_verify_256_nb(sp_ecc_ctx_t* sp_ctx, const byte* hash, static int sp_256_ecc_is_point_4(const sp_point_256* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 4 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 4; @@ -26487,9 +25599,7 @@ static int sp_256_ecc_is_point_4(const sp_point_256* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -26503,21 +25613,11 @@ static int sp_256_ecc_is_point_4(const sp_point_256* point, */ int sp_ecc_is_point_256(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_256* pub = NULL; -#else - sp_point_256 pub[1]; -#endif + SP_DECL_VAR(sp_point_256, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_256*)XMALLOC(sizeof(sp_point_256), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_256, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(pub->x, 4, pX); sp_256_from_mp(pub->y, 4, pY); @@ -26526,9 +25626,7 @@ int sp_ecc_is_point_256(const mp_int* pX, const mp_int* pY) err = sp_256_ecc_is_point_4(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -26547,13 +25645,8 @@ int sp_ecc_is_point_256(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_256(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_256* pub = NULL; -#else - sp_digit priv[4]; - sp_point_256 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 4); + SP_DECL_VAR(sp_point_256, pub, 2); sp_point_256* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -26571,21 +25664,8 @@ int sp_ecc_check_key_256(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 4, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -26651,10 +25731,8 @@ int sp_ecc_check_key_256(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } @@ -26678,35 +25756,16 @@ int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_256* p = NULL; -#else - sp_digit tmp[2 * 4 * 6]; - sp_point_256 p[2]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 4 * 6); + SP_DECL_VAR(sp_point_256, p, 2); sp_point_256* q = NULL; int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256) * 2, NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 4 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 4 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p, 2, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { q = p + 1; @@ -26741,10 +25800,8 @@ int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_256_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -26763,33 +25820,15 @@ int sp_ecc_proj_add_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_256* p = NULL; -#else - sp_digit tmp[2 * 4 * 2]; - sp_point_256 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 4 * 2); + SP_DECL_VAR(sp_point_256, p, 1); int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 4 * 2, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 4 * 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(p->x, 4, pX); sp_256_from_mp(p->y, 4, pY); @@ -26817,10 +25856,8 @@ int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_256_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -26835,33 +25872,16 @@ int sp_ecc_proj_dbl_point_256(mp_int* pX, mp_int* pY, mp_int* pZ, */ int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_256* p = NULL; -#else - sp_digit tmp[2 * 4 * 4]; - sp_point_256 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 4 * 4); + SP_DECL_VAR(sp_point_256, p, 1); int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_256*)XMALLOC(sizeof(sp_point_256), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 4 * 4, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 4 * 4, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_256, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_256_from_mp(p->x, 4, pX); sp_256_from_mp(p->y, 4, pY); @@ -26889,10 +25909,8 @@ int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ) err = sp_256_to_mp(p->z, pZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -26905,24 +25923,14 @@ int sp_ecc_map_256(mp_int* pX, mp_int* pY, mp_int* pZ) */ static int sp_256_mont_sqrt_4(sp_digit* y) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[4 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 4 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 4, NULL, DYNAMIC_TYPE_ECC); - if (t1 == NULL) { - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, t1, 4 * 4, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 4; @@ -26994,9 +26002,7 @@ static int sp_256_mont_sqrt_4(sp_digit* y) } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -27011,23 +26017,14 @@ static int sp_256_mont_sqrt_4(sp_digit* y) */ int sp_ecc_uncompress_256(mp_int* xm, int odd, mp_int* ym) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* x = NULL; -#else - sp_digit x[4 * 4]; -#endif + SP_DECL_VAR(sp_digit, x, 4 * 4); sp_digit* y = NULL; int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - x = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 4, NULL, DYNAMIC_TYPE_ECC); - if (x == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, x, 4 * 4, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { y = x + 2 * 4; @@ -27070,9 +26067,7 @@ int sp_ecc_uncompress_256(mp_int* xm, int odd, mp_int* ym) err = sp_256_to_mp(y, ym); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(x, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(x, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -27221,23 +26216,14 @@ extern sp_digit sp_384_sub_6(sp_digit* r, const sp_digit* a, const sp_digit* b); */ static int sp_384_mod_mul_norm_6(sp_digit* r, const sp_digit* a, const sp_digit* m) { -#ifdef WOLFSSL_SP_SMALL_STACK - int64_t* t = NULL; -#else - int64_t t[2 * 12]; -#endif + SP_DECL_VAR(int64_t, t, 2 * 12); int64_t* a32 = NULL; int64_t o; int err = MP_OKAY; (void)m; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (int64_t*)XMALLOC(sizeof(int64_t) * 2 * 12, NULL, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(int64_t, t, 2 * 12, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { a32 = t + 12; @@ -27315,9 +26301,7 @@ static int sp_384_mod_mul_norm_6(sp_digit* r, const sp_digit* a, const sp_digit* r[5] = (sp_digit)((t[11] << 32) | t[10]); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -28641,13 +27625,8 @@ extern void sp_384_get_point_33_avx2_6(sp_point_384* r, const sp_point_384* tabl static int sp_384_ecc_mulmod_win_add_sub_6(sp_point_384* r, const sp_point_384* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_384 t[33+2]; - sp_digit tmp[2 * 6 * 6]; -#endif + SP_DECL_VAR(sp_point_384, t, 33+2); + SP_DECL_VAR(sp_digit, tmp, 2 * 6 * 6); sp_point_384* rt = NULL; sp_point_384* p = NULL; sp_digit* negy; @@ -28659,19 +27638,8 @@ static int sp_384_ecc_mulmod_win_add_sub_6(sp_point_384* r, const sp_point_384* (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * - (33+2), heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 6 * 6, - heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, t, 33+2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 6 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 33; p = t + 33+1; @@ -28755,10 +27723,8 @@ static int sp_384_ecc_mulmod_win_add_sub_6(sp_point_384* r, const sp_point_384* } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; } @@ -29727,13 +28693,8 @@ static void sp_384_proj_point_add_sub_avx2_6(sp_point_384* ra, static int sp_384_ecc_mulmod_win_add_sub_avx2_6(sp_point_384* r, const sp_point_384* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_384 t[33+2]; - sp_digit tmp[2 * 6 * 6]; -#endif + SP_DECL_VAR(sp_point_384, t, 33+2); + SP_DECL_VAR(sp_digit, tmp, 2 * 6 * 6); sp_point_384* rt = NULL; sp_point_384* p = NULL; sp_digit* negy; @@ -29745,19 +28706,8 @@ static int sp_384_ecc_mulmod_win_add_sub_avx2_6(sp_point_384* r, const sp_point_ (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * - (33+2), heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 6 * 6, - heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, t, 33+2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 6 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 33; p = t + 33+1; @@ -29841,10 +28791,8 @@ static int sp_384_ecc_mulmod_win_add_sub_avx2_6(sp_point_384* r, const sp_point_ } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; } @@ -29976,11 +28924,7 @@ static void sp_384_proj_to_affine_6(sp_point_384* a, sp_digit* t) static int sp_384_gen_stripe_table_6(const sp_point_384* a, sp_table_entry_384* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* t = NULL; -#else - sp_point_384 t[3]; -#endif + SP_DECL_VAR(sp_point_384, t, 3); sp_point_384* s1 = NULL; sp_point_384* s2 = NULL; int i; @@ -29989,13 +28933,7 @@ static int sp_384_gen_stripe_table_6(const sp_point_384* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_384, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -30044,9 +28982,7 @@ static int sp_384_gen_stripe_table_6(const sp_point_384* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -30087,13 +29023,8 @@ static int sp_384_ecc_mulmod_stripe_6(sp_point_384* r, const sp_point_384* g, const sp_table_entry_384* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_384 rt[2]; - sp_digit t[2 * 6 * 6]; -#endif + SP_DECL_VAR(sp_point_384, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 6 * 6); sp_point_384* p = NULL; int i; int j; @@ -30107,19 +29038,8 @@ static int sp_384_ecc_mulmod_stripe_6(sp_point_384* r, const sp_point_384* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 6 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 6 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -30173,10 +29093,8 @@ static int sp_384_ecc_mulmod_stripe_6(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -30294,20 +29212,11 @@ static int sp_384_ecc_mulmod_6(sp_point_384* r, const sp_point_384* g, #ifndef FP_ECC return sp_384_ecc_mulmod_win_add_sub_6(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 6 * 7]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 6 * 7); sp_cache_384_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 6 * 7, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 6 * 7, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -30340,9 +29249,7 @@ static int sp_384_ecc_mulmod_6(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -30468,11 +29375,7 @@ static void sp_384_proj_to_affine_avx2_6(sp_point_384* a, sp_digit* t) static int sp_384_gen_stripe_table_avx2_6(const sp_point_384* a, sp_table_entry_384* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* t = NULL; -#else - sp_point_384 t[3]; -#endif + SP_DECL_VAR(sp_point_384, t, 3); sp_point_384* s1 = NULL; sp_point_384* s2 = NULL; int i; @@ -30481,13 +29384,7 @@ static int sp_384_gen_stripe_table_avx2_6(const sp_point_384* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_384, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -30536,9 +29433,7 @@ static int sp_384_gen_stripe_table_avx2_6(const sp_point_384* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -30565,13 +29460,8 @@ static int sp_384_ecc_mulmod_stripe_avx2_6(sp_point_384* r, const sp_point_384* const sp_table_entry_384* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_384 rt[2]; - sp_digit t[2 * 6 * 6]; -#endif + SP_DECL_VAR(sp_point_384, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 6 * 6); sp_point_384* p = NULL; int i; int j; @@ -30585,19 +29475,8 @@ static int sp_384_ecc_mulmod_stripe_avx2_6(sp_point_384* r, const sp_point_384* (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 6 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 6 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -30651,10 +29530,8 @@ static int sp_384_ecc_mulmod_stripe_avx2_6(sp_point_384* r, const sp_point_384* } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -30677,20 +29554,11 @@ static int sp_384_ecc_mulmod_avx2_6(sp_point_384* r, const sp_point_384* g, #ifndef FP_ECC return sp_384_ecc_mulmod_win_add_sub_avx2_6(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 6 * 7]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 6 * 7); sp_cache_384_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 6 * 7, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 6 * 7, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -30723,9 +29591,7 @@ static int sp_384_ecc_mulmod_avx2_6(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -30744,31 +29610,15 @@ static int sp_384_ecc_mulmod_avx2_6(sp_point_384* r, const sp_point_384* g, int sp_ecc_mulmod_384(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[1]; - sp_digit k[6]; -#endif + SP_DECL_VAR(sp_point_384, point, 1); + SP_DECL_VAR(sp_digit, k, 6); int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 6, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(k, 6, km); sp_384_point_from_ecc_point_6(point, gm); @@ -30786,10 +29636,8 @@ int sp_ecc_mulmod_384(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_384_point_to_ecc_point_6(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -30809,13 +29657,8 @@ int sp_ecc_mulmod_384(const mp_int* km, const ecc_point* gm, ecc_point* r, int sp_ecc_mulmod_add_384(const mp_int* km, const ecc_point* gm, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[2]; - sp_digit k[6 + 6 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_384, point, 2); + SP_DECL_VAR(sp_digit, k, 6 + 6 * 2 * 6); sp_point_384* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; @@ -30823,20 +29666,8 @@ int sp_ecc_mulmod_add_384(const mp_int* km, const ecc_point* gm, word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (6 + 6 * 2 * 6), heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 6 + 6 * 2 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 6; @@ -30888,10 +29719,8 @@ int sp_ecc_mulmod_add_384(const mp_int* km, const ecc_point* gm, err = sp_384_point_to_ecc_point_6(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -49142,13 +47971,8 @@ static int sp_384_ecc_mulmod_add_only_6(sp_point_384* r, const sp_point_384* g, const sp_table_entry_384* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* rt = NULL; - sp_digit* tmp = NULL; -#else - sp_point_384 rt[2]; - sp_digit tmp[2 * 6 * 6]; -#endif + SP_DECL_VAR(sp_point_384, rt, 2); + SP_DECL_VAR(sp_digit, tmp, 2 * 6 * 6); sp_point_384* p = NULL; sp_digit* negy = NULL; int i; @@ -49160,19 +47984,8 @@ static int sp_384_ecc_mulmod_add_only_6(sp_point_384* r, const sp_point_384* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 6 * 6, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 6 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { negy = tmp; p = rt + 1; @@ -49221,18 +48034,8 @@ static int sp_384_ecc_mulmod_add_only_6(sp_point_384* r, const sp_point_384* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 6 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 6 * 6, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); return err; } @@ -49275,13 +48078,8 @@ static int sp_384_ecc_mulmod_add_only_avx2_6(sp_point_384* r, const sp_point_384 const sp_table_entry_384* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* rt = NULL; - sp_digit* tmp = NULL; -#else - sp_point_384 rt[2]; - sp_digit tmp[2 * 6 * 6]; -#endif + SP_DECL_VAR(sp_point_384, rt, 2); + SP_DECL_VAR(sp_digit, tmp, 2 * 6 * 6); sp_point_384* p = NULL; sp_digit* negy = NULL; int i; @@ -49293,19 +48091,8 @@ static int sp_384_ecc_mulmod_add_only_avx2_6(sp_point_384* r, const sp_point_384 (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 6 * 6, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 6 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { negy = tmp; p = rt + 1; @@ -49354,18 +48141,8 @@ static int sp_384_ecc_mulmod_add_only_avx2_6(sp_point_384* r, const sp_point_384 } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 6 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 6 * 6, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); return err; } @@ -49400,31 +48177,15 @@ static int sp_384_ecc_mulmod_base_avx2_6(sp_point_384* r, const sp_digit* k, */ int sp_ecc_mulmod_base_384(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[1]; - sp_digit k[6]; -#endif + SP_DECL_VAR(sp_point_384, point, 1); + SP_DECL_VAR(sp_digit, k, 6); int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 6, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(k, 6, km); @@ -49441,10 +48202,8 @@ int sp_ecc_mulmod_base_384(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_384_point_to_ecc_point_6(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -49463,13 +48222,8 @@ int sp_ecc_mulmod_base_384(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_384(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[2]; - sp_digit k[6 + 6 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_384, point, 2); + SP_DECL_VAR(sp_digit, k, 6 + 6 * 2 * 6); sp_point_384* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; @@ -49477,20 +48231,8 @@ int sp_ecc_mulmod_base_add_384(const mp_int* km, const ecc_point* am, word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (6 + 6 * 2 * 6), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 6 + 6 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 6; @@ -49541,10 +48283,8 @@ int sp_ecc_mulmod_base_add_384(const mp_int* km, const ecc_point* am, err = sp_384_point_to_ecc_point_6(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -49641,17 +48381,12 @@ static int sp_384_ecc_gen_k_6(WC_RNG* rng, sp_digit* k) */ int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_DECL_VAR(sp_point_384, point, 2); #else - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - sp_point_384 point[2]; - #else - sp_point_384 point[1]; - #endif - sp_digit k[6]; + SP_DECL_VAR(sp_point_384, point, 1); #endif + SP_DECL_VAR(sp_digit, k, 6); #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN sp_point_384* infinity = NULL; #endif @@ -49663,22 +48398,12 @@ int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, DYNAMIC_TYPE_ECC); - #else - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, DYNAMIC_TYPE_ECC); - #endif - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 6, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_ALLOC_VAR(sp_point_384, point, 2, heap, DYNAMIC_TYPE_ECC); +#else + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); #endif - + SP_ALLOC_VAR(sp_digit, k, 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN infinity = point + 1; @@ -49723,11 +48448,9 @@ int sp_ecc_make_key_384(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) err = sp_384_point_to_ecc_point_6(point, pub); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); /* point is not sensitive, so no need to zeroize */ - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -49861,13 +48584,8 @@ static void sp_384_to_bin_6(sp_digit* r, byte* a) int sp_ecc_secret_gen_384(const mp_int* priv, const ecc_point* pub, byte* out, word32* outLen, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* point = NULL; - sp_digit* k = NULL; -#else - sp_point_384 point[1]; - sp_digit k[6]; -#endif + SP_DECL_VAR(sp_point_384, point, 1); + SP_DECL_VAR(sp_digit, k, 6); int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); @@ -49877,21 +48595,8 @@ int sp_ecc_secret_gen_384(const mp_int* priv, const ecc_point* pub, byte* out, err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 6, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(k, 6, priv); sp_384_point_from_ecc_point_6(point, pub); @@ -49909,10 +48614,8 @@ int sp_ecc_secret_gen_384(const mp_int* priv, const ecc_point* pub, byte* out, *outLen = 48; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -50030,7 +48733,7 @@ static WC_INLINE sp_digit div_384_word_6(sp_digit d1, sp_digit d0, static WC_INLINE sp_digit div_384_word_6(sp_digit d1, sp_digit d0, sp_digit div) { - register sp_digit r __asm__("rax"); + register sp_digit r asm("rax"); ASSERT_SAVED_VECTOR_REGISTERS(); __asm__ __volatile__ ( "divq %3" @@ -50579,13 +49282,8 @@ static int sp_384_calc_s_6(sp_digit* s, const sp_digit* r, sp_digit* k, int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng, const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* e = NULL; - sp_point_384* point = NULL; -#else - sp_digit e[7 * 2 * 6]; - sp_point_384 point[1]; -#endif + SP_DECL_VAR(sp_digit, e, 7 * 2 * 6); + SP_DECL_VAR(sp_point_384, point, 1); sp_digit* x = NULL; sp_digit* k = NULL; sp_digit* r = NULL; @@ -50600,21 +49298,8 @@ int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_384*)XMALLOC(sizeof(sp_point_384), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - e = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7 * 2 * 6, heap, - DYNAMIC_TYPE_ECC); - if (e == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, e, 7 * 2 * 6, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { x = e + 2 * 6; k = e + 4 * 6; @@ -50686,24 +49371,8 @@ int sp_ecc_sign_384(const byte* hash, word32 hashLen, WC_RNG* rng, err = sp_384_to_mp(s, sm); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (e != NULL) -#endif - { - ForceZero(e, sizeof(sp_digit) * 7 * 2 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(e, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (point != NULL) -#endif - { - ForceZero(point, sizeof(sp_point_384)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(point, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_384, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_digit, e, 7 * 2 * 6, heap, DYNAMIC_TYPE_ECC); return err; } @@ -51148,13 +49817,8 @@ int sp_ecc_verify_384(const byte* hash, word32 hashLen, const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* rm, const mp_int* sm, int* res, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u1 = NULL; - sp_point_384* p1 = NULL; -#else - sp_digit u1[18 * 6]; - sp_point_384 p1[2]; -#endif + SP_DECL_VAR(sp_digit, u1, 18 * 6); + SP_DECL_VAR(sp_point_384, p1, 2); sp_digit* u2 = NULL; sp_digit* s = NULL; sp_digit* tmp = NULL; @@ -51166,21 +49830,8 @@ int sp_ecc_verify_384(const byte* hash, word32 hashLen, const mp_int* pX, word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p1 = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (p1 == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - u1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 6, heap, - DYNAMIC_TYPE_ECC); - if (u1 == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, u1, 18 * 6, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p1, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { u2 = u1 + 2 * 6; s = u1 + 4 * 6; @@ -51259,11 +49910,8 @@ int sp_ecc_verify_384(const byte* hash, word32 hashLen, const mp_int* pX, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u1, heap, DYNAMIC_TYPE_ECC); - XFREE(p1, heap, DYNAMIC_TYPE_ECC); -#endif - + SP_FREE_VAR(p1, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(u1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -51429,21 +50077,13 @@ int sp_ecc_verify_384_nb(sp_ecc_ctx_t* sp_ctx, const byte* hash, static int sp_384_ecc_is_point_6(const sp_point_384* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[6 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 6 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 6 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 6 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 6; @@ -51467,9 +50107,7 @@ static int sp_384_ecc_is_point_6(const sp_point_384* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -51483,21 +50121,11 @@ static int sp_384_ecc_is_point_6(const sp_point_384* point, */ int sp_ecc_is_point_384(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_384* pub = NULL; -#else - sp_point_384 pub[1]; -#endif + SP_DECL_VAR(sp_point_384, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_384*)XMALLOC(sizeof(sp_point_384), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_384, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(pub->x, 6, pX); sp_384_from_mp(pub->y, 6, pY); @@ -51506,9 +50134,7 @@ int sp_ecc_is_point_384(const mp_int* pX, const mp_int* pY) err = sp_384_ecc_is_point_6(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -51527,13 +50153,8 @@ int sp_ecc_is_point_384(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_384(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_384* pub = NULL; -#else - sp_digit priv[6]; - sp_point_384 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 6); + SP_DECL_VAR(sp_point_384, pub, 2); sp_point_384* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -51551,21 +50172,8 @@ int sp_ecc_check_key_384(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 6, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 6, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -51631,10 +50239,8 @@ int sp_ecc_check_key_384(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } @@ -51658,35 +50264,16 @@ int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_384* p = NULL; -#else - sp_digit tmp[2 * 6 * 6]; - sp_point_384 p[2]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 6 * 6); + SP_DECL_VAR(sp_point_384, p, 2); sp_point_384* q = NULL; int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384) * 2, NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 6 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 6 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p, 2, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { q = p + 1; @@ -51721,10 +50308,8 @@ int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_384_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -51743,33 +50328,15 @@ int sp_ecc_proj_add_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_384* p = NULL; -#else - sp_digit tmp[2 * 6 * 2]; - sp_point_384 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 6 * 2); + SP_DECL_VAR(sp_point_384, p, 1); int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 6 * 2, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 6 * 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(p->x, 6, pX); sp_384_from_mp(p->y, 6, pY); @@ -51797,10 +50364,8 @@ int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_384_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -51815,33 +50380,16 @@ int sp_ecc_proj_dbl_point_384(mp_int* pX, mp_int* pY, mp_int* pZ, */ int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_384* p = NULL; -#else - sp_digit tmp[2 * 6 * 6]; - sp_point_384 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 6 * 6); + SP_DECL_VAR(sp_point_384, p, 1); int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_384*)XMALLOC(sizeof(sp_point_384), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 6 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 6 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_384, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_384_from_mp(p->x, 6, pX); sp_384_from_mp(p->y, 6, pY); @@ -51869,10 +50417,8 @@ int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ) err = sp_384_to_mp(p->z, pZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -51885,11 +50431,7 @@ int sp_ecc_map_384(mp_int* pX, mp_int* pY, mp_int* pZ) */ static int sp_384_mont_sqrt_6(sp_digit* y) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[5 * 2 * 6]; -#endif + SP_DECL_VAR(sp_digit, t1, 5 * 2 * 6); sp_digit* t2 = NULL; sp_digit* t3 = NULL; sp_digit* t4 = NULL; @@ -51899,12 +50441,7 @@ static int sp_384_mont_sqrt_6(sp_digit* y) word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 5 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, t1, 5 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 6; t3 = t1 + 4 * 6; @@ -52029,9 +50566,7 @@ static int sp_384_mont_sqrt_6(sp_digit* y) } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -52046,23 +50581,14 @@ static int sp_384_mont_sqrt_6(sp_digit* y) */ int sp_ecc_uncompress_384(mp_int* xm, int odd, mp_int* ym) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* x = NULL; -#else - sp_digit x[4 * 6]; -#endif + SP_DECL_VAR(sp_digit, x, 4 * 6); sp_digit* y = NULL; int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - x = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 6, NULL, DYNAMIC_TYPE_ECC); - if (x == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, x, 4 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { y = x + 2 * 6; @@ -52105,9 +50631,7 @@ int sp_ecc_uncompress_384(mp_int* xm, int odd, mp_int* ym) err = sp_384_to_mp(y, ym); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(x, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(x, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -53576,13 +52100,8 @@ extern void sp_521_get_point_33_avx2_9(sp_point_521* r, const sp_point_521* tabl static int sp_521_ecc_mulmod_win_add_sub_9(sp_point_521* r, const sp_point_521* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_521 t[33+2]; - sp_digit tmp[2 * 9 * 6]; -#endif + SP_DECL_VAR(sp_point_521, t, 33+2); + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 6); sp_point_521* rt = NULL; sp_point_521* p = NULL; sp_digit* negy; @@ -53594,19 +52113,8 @@ static int sp_521_ecc_mulmod_win_add_sub_9(sp_point_521* r, const sp_point_521* (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * - (33+2), heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, - heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, t, 33+2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 33; p = t + 33+1; @@ -53690,10 +52198,8 @@ static int sp_521_ecc_mulmod_win_add_sub_9(sp_point_521* r, const sp_point_521* } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; } @@ -54639,13 +53145,8 @@ static void sp_521_proj_point_add_sub_avx2_9(sp_point_521* ra, static int sp_521_ecc_mulmod_win_add_sub_avx2_9(sp_point_521* r, const sp_point_521* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_521 t[33+2]; - sp_digit tmp[2 * 9 * 6]; -#endif + SP_DECL_VAR(sp_point_521, t, 33+2); + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 6); sp_point_521* rt = NULL; sp_point_521* p = NULL; sp_digit* negy; @@ -54657,19 +53158,8 @@ static int sp_521_ecc_mulmod_win_add_sub_avx2_9(sp_point_521* r, const sp_point_ (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * - (33+2), heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, - heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, t, 33+2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 33; p = t + 33+1; @@ -54753,10 +53243,8 @@ static int sp_521_ecc_mulmod_win_add_sub_avx2_9(sp_point_521* r, const sp_point_ } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; } @@ -54888,11 +53376,7 @@ static void sp_521_proj_to_affine_9(sp_point_521* a, sp_digit* t) static int sp_521_gen_stripe_table_9(const sp_point_521* a, sp_table_entry_521* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* t = NULL; -#else - sp_point_521 t[3]; -#endif + SP_DECL_VAR(sp_point_521, t, 3); sp_point_521* s1 = NULL; sp_point_521* s2 = NULL; int i; @@ -54901,13 +53385,7 @@ static int sp_521_gen_stripe_table_9(const sp_point_521* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_521, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -54956,9 +53434,7 @@ static int sp_521_gen_stripe_table_9(const sp_point_521* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -54999,13 +53475,8 @@ static int sp_521_ecc_mulmod_stripe_9(sp_point_521* r, const sp_point_521* g, const sp_table_entry_521* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_521 rt[2]; - sp_digit t[2 * 9 * 6]; -#endif + SP_DECL_VAR(sp_point_521, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 9 * 6); sp_point_521* p = NULL; int i; int j; @@ -55019,19 +53490,8 @@ static int sp_521_ecc_mulmod_stripe_9(sp_point_521* r, const sp_point_521* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -55085,10 +53545,8 @@ static int sp_521_ecc_mulmod_stripe_9(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -55206,20 +53664,11 @@ static int sp_521_ecc_mulmod_9(sp_point_521* r, const sp_point_521* g, #ifndef FP_ECC return sp_521_ecc_mulmod_win_add_sub_9(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 9 * 6]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 6); sp_cache_521_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -55252,9 +53701,7 @@ static int sp_521_ecc_mulmod_9(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -55380,11 +53827,7 @@ static void sp_521_proj_to_affine_avx2_9(sp_point_521* a, sp_digit* t) static int sp_521_gen_stripe_table_avx2_9(const sp_point_521* a, sp_table_entry_521* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* t = NULL; -#else - sp_point_521 t[3]; -#endif + SP_DECL_VAR(sp_point_521, t, 3); sp_point_521* s1 = NULL; sp_point_521* s2 = NULL; int i; @@ -55393,13 +53836,7 @@ static int sp_521_gen_stripe_table_avx2_9(const sp_point_521* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_521, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -55448,9 +53885,7 @@ static int sp_521_gen_stripe_table_avx2_9(const sp_point_521* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -55477,13 +53912,8 @@ static int sp_521_ecc_mulmod_stripe_avx2_9(sp_point_521* r, const sp_point_521* const sp_table_entry_521* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_521 rt[2]; - sp_digit t[2 * 9 * 6]; -#endif + SP_DECL_VAR(sp_point_521, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 9 * 6); sp_point_521* p = NULL; int i; int j; @@ -55497,19 +53927,8 @@ static int sp_521_ecc_mulmod_stripe_avx2_9(sp_point_521* r, const sp_point_521* (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -55563,10 +53982,8 @@ static int sp_521_ecc_mulmod_stripe_avx2_9(sp_point_521* r, const sp_point_521* } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -55589,20 +54006,11 @@ static int sp_521_ecc_mulmod_avx2_9(sp_point_521* r, const sp_point_521* g, #ifndef FP_ECC return sp_521_ecc_mulmod_win_add_sub_avx2_9(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 9 * 6]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 6); sp_cache_521_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -55635,9 +54043,7 @@ static int sp_521_ecc_mulmod_avx2_9(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -55656,31 +54062,15 @@ static int sp_521_ecc_mulmod_avx2_9(sp_point_521* r, const sp_point_521* g, int sp_ecc_mulmod_521(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[1]; - sp_digit k[9]; -#endif + SP_DECL_VAR(sp_point_521, point, 1); + SP_DECL_VAR(sp_digit, k, 9); int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 9, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(k, 9, km); sp_521_point_from_ecc_point_9(point, gm); @@ -55698,10 +54088,8 @@ int sp_ecc_mulmod_521(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_521_point_to_ecc_point_9(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -55721,13 +54109,8 @@ int sp_ecc_mulmod_521(const mp_int* km, const ecc_point* gm, ecc_point* r, int sp_ecc_mulmod_add_521(const mp_int* km, const ecc_point* gm, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[2]; - sp_digit k[9 + 9 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_521, point, 2); + SP_DECL_VAR(sp_digit, k, 9 + 9 * 2 * 6); sp_point_521* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; @@ -55735,20 +54118,8 @@ int sp_ecc_mulmod_add_521(const mp_int* km, const ecc_point* gm, word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (9 + 9 * 2 * 6), heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 9 + 9 * 2 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 9; @@ -55800,10 +54171,8 @@ int sp_ecc_mulmod_add_521(const mp_int* km, const ecc_point* gm, err = sp_521_point_to_ecc_point_9(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -90240,13 +88609,8 @@ static int sp_521_ecc_mulmod_add_only_9(sp_point_521* r, const sp_point_521* g, const sp_table_entry_521* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* rt = NULL; - sp_digit* tmp = NULL; -#else - sp_point_521 rt[2]; - sp_digit tmp[2 * 9 * 6]; -#endif + SP_DECL_VAR(sp_point_521, rt, 2); + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 6); sp_point_521* p = NULL; sp_digit* negy = NULL; int i; @@ -90258,19 +88622,8 @@ static int sp_521_ecc_mulmod_add_only_9(sp_point_521* r, const sp_point_521* g, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { negy = tmp; p = rt + 1; @@ -90319,18 +88672,8 @@ static int sp_521_ecc_mulmod_add_only_9(sp_point_521* r, const sp_point_521* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 9 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); return err; } @@ -90373,13 +88716,8 @@ static int sp_521_ecc_mulmod_add_only_avx2_9(sp_point_521* r, const sp_point_521 const sp_table_entry_521* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* rt = NULL; - sp_digit* tmp = NULL; -#else - sp_point_521 rt[2]; - sp_digit tmp[2 * 9 * 6]; -#endif + SP_DECL_VAR(sp_point_521, rt, 2); + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 6); sp_point_521* p = NULL; sp_digit* negy = NULL; int i; @@ -90391,19 +88729,8 @@ static int sp_521_ecc_mulmod_add_only_avx2_9(sp_point_521* r, const sp_point_521 (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, heap, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { negy = tmp; p = rt + 1; @@ -90452,18 +88779,8 @@ static int sp_521_ecc_mulmod_add_only_avx2_9(sp_point_521* r, const sp_point_521 } } -#ifdef WOLFSSL_SP_SMALL_STACK - if (tmp != NULL) -#endif - { - ForceZero(tmp, sizeof(sp_digit) * 2 * 9 * 6); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_ZEROFREE_VAR(sp_digit, tmp, 2 * 9 * 6, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); return err; } @@ -90498,31 +88815,15 @@ static int sp_521_ecc_mulmod_base_avx2_9(sp_point_521* r, const sp_digit* k, */ int sp_ecc_mulmod_base_521(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[1]; - sp_digit k[9]; -#endif + SP_DECL_VAR(sp_point_521, point, 1); + SP_DECL_VAR(sp_digit, k, 9); int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 9, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(k, 9, km); @@ -90539,10 +88840,8 @@ int sp_ecc_mulmod_base_521(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_521_point_to_ecc_point_9(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -90561,13 +88860,8 @@ int sp_ecc_mulmod_base_521(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_521(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[2]; - sp_digit k[9 + 9 * 2 * 6]; -#endif + SP_DECL_VAR(sp_point_521, point, 2); + SP_DECL_VAR(sp_digit, k, 9 + 9 * 2 * 6); sp_point_521* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; @@ -90575,20 +88869,8 @@ int sp_ecc_mulmod_base_add_521(const mp_int* km, const ecc_point* am, word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (9 + 9 * 2 * 6), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 9 + 9 * 2 * 6, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 9; @@ -90639,10 +88921,8 @@ int sp_ecc_mulmod_base_add_521(const mp_int* km, const ecc_point* am, err = sp_521_point_to_ecc_point_9(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -90740,17 +89020,12 @@ static int sp_521_ecc_gen_k_9(WC_RNG* rng, sp_digit* k) */ int sp_ecc_make_key_521(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_DECL_VAR(sp_point_521, point, 2); #else - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - sp_point_521 point[2]; - #else - sp_point_521 point[1]; - #endif - sp_digit k[9]; + SP_DECL_VAR(sp_point_521, point, 1); #endif + SP_DECL_VAR(sp_digit, k, 9); #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN sp_point_521* infinity = NULL; #endif @@ -90762,22 +89037,12 @@ int sp_ecc_make_key_521(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, DYNAMIC_TYPE_ECC); - #else - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, DYNAMIC_TYPE_ECC); - #endif - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } +#ifdef WOLFSSL_VALIDATE_ECC_KEYGEN + SP_ALLOC_VAR(sp_point_521, point, 2, heap, DYNAMIC_TYPE_ECC); +#else + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); #endif - + SP_ALLOC_VAR(sp_digit, k, 9, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN infinity = point + 1; @@ -90822,11 +89087,9 @@ int sp_ecc_make_key_521(WC_RNG* rng, mp_int* priv, ecc_point* pub, void* heap) err = sp_521_point_to_ecc_point_9(point, pub); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); /* point is not sensitive, so no need to zeroize */ - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -90960,13 +89223,8 @@ static void sp_521_to_bin_9(sp_digit* r, byte* a) int sp_ecc_secret_gen_521(const mp_int* priv, const ecc_point* pub, byte* out, word32* outLen, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* point = NULL; - sp_digit* k = NULL; -#else - sp_point_521 point[1]; - sp_digit k[9]; -#endif + SP_DECL_VAR(sp_point_521, point, 1); + SP_DECL_VAR(sp_digit, k, 9); int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); @@ -90976,21 +89234,8 @@ int sp_ecc_secret_gen_521(const mp_int* priv, const ecc_point* pub, byte* out, err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 9, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(k, 9, priv); sp_521_point_from_ecc_point_9(point, pub); @@ -91008,10 +89253,8 @@ int sp_ecc_secret_gen_521(const mp_int* priv, const ecc_point* pub, byte* out, *outLen = 66; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -91150,7 +89393,7 @@ static WC_INLINE sp_digit div_521_word_9(sp_digit d1, sp_digit d0, static WC_INLINE sp_digit div_521_word_9(sp_digit d1, sp_digit d0, sp_digit div) { - register sp_digit r __asm__("rax"); + register sp_digit r asm("rax"); ASSERT_SAVED_VECTOR_REGISTERS(); __asm__ __volatile__ ( "divq %3" @@ -91733,13 +89976,8 @@ static int sp_521_calc_s_9(sp_digit* s, const sp_digit* r, sp_digit* k, int sp_ecc_sign_521(const byte* hash, word32 hashLen, WC_RNG* rng, const mp_int* priv, mp_int* rm, mp_int* sm, mp_int* km, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* e = NULL; - sp_point_521* point = NULL; -#else - sp_digit e[7 * 2 * 9]; - sp_point_521 point[1]; -#endif + SP_DECL_VAR(sp_digit, e, 7 * 2 * 9); + SP_DECL_VAR(sp_point_521, point, 1); sp_digit* x = NULL; sp_digit* k = NULL; sp_digit* r = NULL; @@ -91754,21 +89992,8 @@ int sp_ecc_sign_521(const byte* hash, word32 hashLen, WC_RNG* rng, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_521*)XMALLOC(sizeof(sp_point_521), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - e = (sp_digit*)XMALLOC(sizeof(sp_digit) * 7 * 2 * 9, heap, - DYNAMIC_TYPE_ECC); - if (e == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, e, 7 * 2 * 9, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { x = e + 2 * 9; k = e + 4 * 9; @@ -91845,24 +90070,8 @@ int sp_ecc_sign_521(const byte* hash, word32 hashLen, WC_RNG* rng, err = sp_521_to_mp(s, sm); } -#ifdef WOLFSSL_SP_SMALL_STACK - if (e != NULL) -#endif - { - ForceZero(e, sizeof(sp_digit) * 7 * 2 * 9); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(e, heap, DYNAMIC_TYPE_ECC); - #endif - } -#ifdef WOLFSSL_SP_SMALL_STACK - if (point != NULL) -#endif - { - ForceZero(point, sizeof(sp_point_521)); - #ifdef WOLFSSL_SP_SMALL_STACK - XFREE(point, heap, DYNAMIC_TYPE_ECC); - #endif - } + SP_ZEROFREE_VAR(sp_point_521, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ZEROFREE_VAR(sp_digit, e, 7 * 2 * 9, heap, DYNAMIC_TYPE_ECC); return err; } @@ -92313,13 +90522,8 @@ int sp_ecc_verify_521(const byte* hash, word32 hashLen, const mp_int* pX, const mp_int* pY, const mp_int* pZ, const mp_int* rm, const mp_int* sm, int* res, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* u1 = NULL; - sp_point_521* p1 = NULL; -#else - sp_digit u1[18 * 9]; - sp_point_521 p1[2]; -#endif + SP_DECL_VAR(sp_digit, u1, 18 * 9); + SP_DECL_VAR(sp_point_521, p1, 2); sp_digit* u2 = NULL; sp_digit* s = NULL; sp_digit* tmp = NULL; @@ -92331,21 +90535,8 @@ int sp_ecc_verify_521(const byte* hash, word32 hashLen, const mp_int* pX, word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p1 = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (p1 == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - u1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 18 * 9, heap, - DYNAMIC_TYPE_ECC); - if (u1 == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, u1, 18 * 9, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p1, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { u2 = u1 + 2 * 9; s = u1 + 4 * 9; @@ -92428,11 +90619,8 @@ int sp_ecc_verify_521(const byte* hash, word32 hashLen, const mp_int* pX, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(u1, heap, DYNAMIC_TYPE_ECC); - XFREE(p1, heap, DYNAMIC_TYPE_ECC); -#endif - + SP_FREE_VAR(p1, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(u1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -92601,21 +90789,13 @@ int sp_ecc_verify_521_nb(sp_ecc_ctx_t* sp_ctx, const byte* hash, static int sp_521_ecc_is_point_9(const sp_point_521* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[9 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 9 * 4); sp_digit* t2 = NULL; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 9 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 9; @@ -92639,9 +90819,7 @@ static int sp_521_ecc_is_point_9(const sp_point_521* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -92655,21 +90833,11 @@ static int sp_521_ecc_is_point_9(const sp_point_521* point, */ int sp_ecc_is_point_521(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_521* pub = NULL; -#else - sp_point_521 pub[1]; -#endif + SP_DECL_VAR(sp_point_521, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_521*)XMALLOC(sizeof(sp_point_521), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_521, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(pub->x, 9, pX); sp_521_from_mp(pub->y, 9, pY); @@ -92678,9 +90846,7 @@ int sp_ecc_is_point_521(const mp_int* pX, const mp_int* pY) err = sp_521_ecc_is_point_9(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -92699,13 +90865,8 @@ int sp_ecc_is_point_521(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_521(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_521* pub = NULL; -#else - sp_digit priv[9]; - sp_point_521 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 9); + SP_DECL_VAR(sp_point_521, pub, 2); sp_point_521* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -92723,21 +90884,8 @@ int sp_ecc_check_key_521(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 9, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 9, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -92803,10 +90951,8 @@ int sp_ecc_check_key_521(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } @@ -92830,35 +90976,16 @@ int sp_ecc_proj_add_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* qX, mp_int* qY, mp_int* qZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_521* p = NULL; -#else - sp_digit tmp[2 * 9 * 6]; - sp_point_521 p[2]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 6); + SP_DECL_VAR(sp_point_521, p, 2); sp_point_521* q = NULL; int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521) * 2, NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 6, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 6, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p, 2, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { q = p + 1; @@ -92893,10 +91020,8 @@ int sp_ecc_proj_add_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_521_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -92915,33 +91040,15 @@ int sp_ecc_proj_add_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, int sp_ecc_proj_dbl_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, mp_int* rX, mp_int* rY, mp_int* rZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_521* p = NULL; -#else - sp_digit tmp[2 * 9 * 2]; - sp_point_521 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 2); + SP_DECL_VAR(sp_point_521, p, 1); int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 2, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(p->x, 9, pX); sp_521_from_mp(p->y, 9, pY); @@ -92969,10 +91076,8 @@ int sp_ecc_proj_dbl_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, err = sp_521_to_mp(p->z, rZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -92987,33 +91092,16 @@ int sp_ecc_proj_dbl_point_521(mp_int* pX, mp_int* pY, mp_int* pZ, */ int sp_ecc_map_521(mp_int* pX, mp_int* pY, mp_int* pZ) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp = NULL; - sp_point_521* p = NULL; -#else - sp_digit tmp[2 * 9 * 5]; - sp_point_521 p[1]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 9 * 5); + SP_DECL_VAR(sp_point_521, p, 1); int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - p = (sp_point_521*)XMALLOC(sizeof(sp_point_521), NULL, - DYNAMIC_TYPE_ECC); - if (p == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9 * 5, NULL, - DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 9 * 5, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_521, p, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_521_from_mp(p->x, 9, pX); sp_521_from_mp(p->y, 9, pY); @@ -93041,10 +91129,8 @@ int sp_ecc_map_521(mp_int* pX, mp_int* pY, mp_int* pZ) err = sp_521_to_mp(p->z, pZ); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, NULL, DYNAMIC_TYPE_ECC); - XFREE(p, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(p, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -93064,22 +91150,13 @@ static const word64 p521_sqrt_power[9] = { */ static int sp_521_mont_sqrt_9(sp_digit* y) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t = NULL; -#else - sp_digit t[2 * 9]; -#endif + SP_DECL_VAR(sp_digit, t, 2 * 9); int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 9, NULL, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, t, 2 * 9, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { #ifdef HAVE_INTEL_AVX2 @@ -93110,9 +91187,7 @@ static int sp_521_mont_sqrt_9(sp_digit* y) } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -93127,23 +91202,14 @@ static int sp_521_mont_sqrt_9(sp_digit* y) */ int sp_ecc_uncompress_521(mp_int* xm, int odd, mp_int* ym) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* x = NULL; -#else - sp_digit x[4 * 9]; -#endif + SP_DECL_VAR(sp_digit, x, 4 * 9); sp_digit* y = NULL; int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - x = (sp_digit*)XMALLOC(sizeof(sp_digit) * 4 * 9, NULL, DYNAMIC_TYPE_ECC); - if (x == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_digit, x, 4 * 9, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { y = x + 2 * 9; @@ -93186,9 +91252,7 @@ int sp_ecc_uncompress_521(mp_int* xm, int odd, mp_int* ym) err = sp_521_to_mp(y, ym); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(x, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(x, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -93393,7 +91457,7 @@ static WC_INLINE sp_digit div_1024_word_16(sp_digit d1, sp_digit d0, static WC_INLINE sp_digit div_1024_word_16(sp_digit d1, sp_digit d0, sp_digit div) { - register sp_digit r __asm__("rax"); + register sp_digit r asm("rax"); ASSERT_SAVED_VECTOR_REGISTERS(); __asm__ __volatile__ ( "divq %3" @@ -94858,13 +92922,8 @@ static void sp_1024_ecc_recode_7_16(const sp_digit* k, ecc_recode_1024* v) static int sp_1024_ecc_mulmod_win_add_sub_16(sp_point_1024* r, const sp_point_1024* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_1024 t[65+2]; - sp_digit tmp[2 * 16 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, t, 65+2); + SP_DECL_VAR(sp_digit, tmp, 2 * 16 * 37); sp_point_1024* rt = NULL; sp_point_1024* p = NULL; sp_digit* negy; @@ -94876,19 +92935,8 @@ static int sp_1024_ecc_mulmod_win_add_sub_16(sp_point_1024* r, const sp_point_10 (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * - (65+2), heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 16 * 37, - heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, t, 65+2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 16 * 37, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 65; p = t + 65+1; @@ -94976,10 +93024,8 @@ static int sp_1024_ecc_mulmod_win_add_sub_16(sp_point_1024* r, const sp_point_10 } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; } @@ -95897,13 +93943,8 @@ static void sp_1024_proj_point_add_sub_avx2_16(sp_point_1024* ra, static int sp_1024_ecc_mulmod_win_add_sub_avx2_16(sp_point_1024* r, const sp_point_1024* g, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* t = NULL; - sp_digit* tmp = NULL; -#else - sp_point_1024 t[65+2]; - sp_digit tmp[2 * 16 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, t, 65+2); + SP_DECL_VAR(sp_digit, tmp, 2 * 16 * 37); sp_point_1024* rt = NULL; sp_point_1024* p = NULL; sp_digit* negy; @@ -95915,19 +93956,8 @@ static int sp_1024_ecc_mulmod_win_add_sub_avx2_16(sp_point_1024* r, const sp_poi (void)ct; (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * - (65+2), heap, DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 16 * 37, - heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, t, 65+2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, tmp, 2 * 16 * 37, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { rt = t + 65; p = t + 65+1; @@ -96015,10 +94045,8 @@ static int sp_1024_ecc_mulmod_win_add_sub_avx2_16(sp_point_1024* r, const sp_poi } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; } @@ -96150,11 +94178,7 @@ static void sp_1024_proj_to_affine_16(sp_point_1024* a, sp_digit* t) static int sp_1024_gen_stripe_table_16(const sp_point_1024* a, sp_table_entry_1024* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* t = NULL; -#else - sp_point_1024 t[3]; -#endif + SP_DECL_VAR(sp_point_1024, t, 3); sp_point_1024* s1 = NULL; sp_point_1024* s2 = NULL; int i; @@ -96163,13 +94187,7 @@ static int sp_1024_gen_stripe_table_16(const sp_point_1024* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_1024, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -96218,9 +94236,7 @@ static int sp_1024_gen_stripe_table_16(const sp_point_1024* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -96246,13 +94262,8 @@ static int sp_1024_ecc_mulmod_stripe_16(sp_point_1024* r, const sp_point_1024* g const sp_table_entry_1024* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_1024 rt[2]; - sp_digit t[2 * 16 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 16 * 37); sp_point_1024* p = NULL; int i; int j; @@ -96266,19 +94277,8 @@ static int sp_1024_ecc_mulmod_stripe_16(sp_point_1024* r, const sp_point_1024* g (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 16 * 37, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 16 * 37, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -96317,10 +94317,8 @@ static int sp_1024_ecc_mulmod_stripe_16(sp_point_1024* r, const sp_point_1024* g } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -96437,20 +94435,11 @@ static int sp_1024_ecc_mulmod_16(sp_point_1024* r, const sp_point_1024* g, #ifndef FP_ECC return sp_1024_ecc_mulmod_win_add_sub_16(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 16 * 38]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 16 * 38); sp_cache_1024_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 16 * 38, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 16 * 38, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -96483,9 +94472,7 @@ static int sp_1024_ecc_mulmod_16(sp_point_1024* r, const sp_point_1024* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -96611,11 +94598,7 @@ static void sp_1024_proj_to_affine_avx2_16(sp_point_1024* a, sp_digit* t) static int sp_1024_gen_stripe_table_avx2_16(const sp_point_1024* a, sp_table_entry_1024* table, sp_digit* tmp, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* t = NULL; -#else - sp_point_1024 t[3]; -#endif + SP_DECL_VAR(sp_point_1024, t, 3); sp_point_1024* s1 = NULL; sp_point_1024* s2 = NULL; int i; @@ -96624,13 +94607,7 @@ static int sp_1024_gen_stripe_table_avx2_16(const sp_point_1024* a, (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - t = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 3, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_1024, t, 3, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { s1 = t + 1; s2 = t + 2; @@ -96679,9 +94656,7 @@ static int sp_1024_gen_stripe_table_avx2_16(const sp_point_1024* a, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -96707,13 +94682,8 @@ static int sp_1024_ecc_mulmod_stripe_avx2_16(sp_point_1024* r, const sp_point_10 const sp_table_entry_1024* table, const sp_digit* k, int map, int ct, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* rt = NULL; - sp_digit* t = NULL; -#else - sp_point_1024 rt[2]; - sp_digit t[2 * 16 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, rt, 2); + SP_DECL_VAR(sp_digit, t, 2 * 16 * 37); sp_point_1024* p = NULL; int i; int j; @@ -96727,19 +94697,8 @@ static int sp_1024_ecc_mulmod_stripe_avx2_16(sp_point_1024* r, const sp_point_10 (void)heap; -#ifdef WOLFSSL_SP_SMALL_STACK - rt = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (rt == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 16 * 37, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, rt, 2, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 2 * 16 * 37, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = rt + 1; @@ -96778,10 +94737,8 @@ static int sp_1024_ecc_mulmod_stripe_avx2_16(sp_point_1024* r, const sp_point_10 } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(rt, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(rt, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); return err; } @@ -96803,20 +94760,11 @@ static int sp_1024_ecc_mulmod_avx2_16(sp_point_1024* r, const sp_point_1024* g, #ifndef FP_ECC return sp_1024_ecc_mulmod_win_add_sub_avx2_16(r, g, k, map, ct, heap); #else -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* tmp; -#else - sp_digit tmp[2 * 16 * 38]; -#endif + SP_DECL_VAR(sp_digit, tmp, 2 * 16 * 38); sp_cache_1024_t* cache; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - tmp = (sp_digit*)XMALLOC(sizeof(sp_digit) * 2 * 16 * 38, heap, DYNAMIC_TYPE_ECC); - if (tmp == NULL) { - err = MEMORY_E; - } -#endif + SP_ALLOC_VAR(sp_digit, tmp, 2 * 16 * 38, heap, DYNAMIC_TYPE_ECC); #ifndef HAVE_THREAD_LS if (err == MP_OKAY) { #ifndef WOLFSSL_MUTEX_INITIALIZER @@ -96849,9 +94797,7 @@ static int sp_1024_ecc_mulmod_avx2_16(sp_point_1024* r, const sp_point_1024* g, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(tmp, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(tmp, heap, DYNAMIC_TYPE_ECC); return err; #endif } @@ -96870,31 +94816,15 @@ static int sp_1024_ecc_mulmod_avx2_16(sp_point_1024* r, const sp_point_1024* g, int sp_ecc_mulmod_1024(const mp_int* km, const ecc_point* gm, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[1]; - sp_digit k[16]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, k, 16); int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 16, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(k, 16, km); sp_1024_point_from_ecc_point_16(point, gm); @@ -96912,10 +94842,8 @@ int sp_ecc_mulmod_1024(const mp_int* km, const ecc_point* gm, ecc_point* r, err = sp_1024_point_to_ecc_point_16(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -100304,31 +98232,15 @@ static int sp_1024_ecc_mulmod_base_avx2_16(sp_point_1024* r, const sp_digit* k, */ int sp_ecc_mulmod_base_1024(const mp_int* km, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[1]; - sp_digit k[16]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, k, 16); int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16, heap, - DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 16, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(k, 16, km); @@ -100345,10 +98257,8 @@ int sp_ecc_mulmod_base_1024(const mp_int* km, ecc_point* r, int map, void* heap) err = sp_1024_point_to_ecc_point_16(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -100367,13 +98277,8 @@ int sp_ecc_mulmod_base_1024(const mp_int* km, ecc_point* r, int map, void* heap) int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am, int inMont, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[2]; - sp_digit k[16 + 16 * 2 * 37]; -#endif + SP_DECL_VAR(sp_point_1024, point, 2); + SP_DECL_VAR(sp_digit, k, 16 + 16 * 2 * 37); sp_point_1024* addP = NULL; sp_digit* tmp = NULL; int err = MP_OKAY; @@ -100381,20 +98286,8 @@ int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am, word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC( - sizeof(sp_digit) * (16 + 16 * 2 * 37), - heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 2, NULL, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 16 + 16 * 2 * 37, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { addP = point + 1; tmp = k + 16; @@ -100445,10 +98338,8 @@ int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am, err = sp_1024_point_to_ecc_point_16(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(k, NULL, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -100466,13 +98357,8 @@ int sp_ecc_mulmod_base_add_1024(const mp_int* km, const ecc_point* am, int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* t = NULL; -#else - sp_point_1024 point[1]; - sp_digit t[38 * 2 * 16]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, t, 38 * 2 * 16); int err = MP_OKAY; #ifdef HAVE_INTEL_AVX2 word32 cpuid_flags = cpuid_get_flags(); @@ -100490,21 +98376,8 @@ int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, err = BUFFER_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY) { - t = (sp_digit*)XMALLOC(sizeof(sp_digit) * 38 * 2 * 16, heap, - DYNAMIC_TYPE_ECC); - if (t == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, t, 38 * 2 * 16, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_point_from_ecc_point_16(point, gm); #ifdef HAVE_INTEL_AVX2 @@ -100522,10 +98395,8 @@ int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, *len = sizeof(sp_table_entry_1024) * 256; } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); return err; } @@ -100578,31 +98449,15 @@ int sp_ecc_gen_table_1024(const ecc_point* gm, byte* table, word32* len, int sp_ecc_mulmod_table_1024(const mp_int* km, const ecc_point* gm, byte* table, ecc_point* r, int map, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* point = NULL; - sp_digit* k = NULL; -#else - sp_point_1024 point[1]; - sp_digit k[16]; -#endif + SP_DECL_VAR(sp_point_1024, point, 1); + SP_DECL_VAR(sp_digit, k, 16); int err = MP_OKAY; #if defined(HAVE_INTEL_AVX2) && !defined(WOLFSSL_SP_SMALL) word32 cpuid_flags = cpuid_get_flags(); #endif -#ifdef WOLFSSL_SP_SMALL_STACK - point = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), heap, - DYNAMIC_TYPE_ECC); - if (point == NULL) { - err = MEMORY_E; - } - if (err == MP_OKAY) { - k = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16, heap, DYNAMIC_TYPE_ECC); - if (k == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_point_1024, point, 1, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_digit, k, 16, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(k, 16, km); sp_1024_point_from_ecc_point_16(point, gm); @@ -100627,10 +98482,8 @@ int sp_ecc_mulmod_table_1024(const mp_int* km, const ecc_point* gm, byte* table, err = sp_1024_point_to_ecc_point_16(point, r); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(k, heap, DYNAMIC_TYPE_ECC); - XFREE(point, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(point, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(k, heap, DYNAMIC_TYPE_ECC); return err; } @@ -105508,22 +103361,14 @@ static void sp_1024_from_bin(sp_digit* r, int size, const byte* a, int n) static int sp_1024_ecc_is_point_16(const sp_point_1024* point, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* t1 = NULL; -#else - sp_digit t1[16 * 4]; -#endif + SP_DECL_VAR(sp_digit, t1, 16 * 4); sp_digit* t2 = NULL; sp_int64 n; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - t1 = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16 * 4, heap, DYNAMIC_TYPE_ECC); - if (t1 == NULL) - err = MEMORY_E; -#endif (void)heap; + SP_ALLOC_VAR(sp_digit, t1, 16 * 4, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { t2 = t1 + 2 * 16; @@ -105550,9 +103395,7 @@ static int sp_1024_ecc_is_point_16(const sp_point_1024* point, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(t1, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(t1, heap, DYNAMIC_TYPE_ECC); return err; } @@ -105566,21 +103409,11 @@ static int sp_1024_ecc_is_point_16(const sp_point_1024* point, */ int sp_ecc_is_point_1024(const mp_int* pX, const mp_int* pY) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_point_1024* pub = NULL; -#else - sp_point_1024 pub[1]; -#endif + SP_DECL_VAR(sp_point_1024, pub, 1); const byte one[1] = { 1 }; int err = MP_OKAY; -#ifdef WOLFSSL_SP_SMALL_STACK - pub = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024), NULL, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; -#endif - + SP_ALLOC_VAR(sp_point_1024, pub, 1, NULL, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { sp_1024_from_mp(pub->x, 16, pX); sp_1024_from_mp(pub->y, 16, pY); @@ -105589,9 +103422,7 @@ int sp_ecc_is_point_1024(const mp_int* pX, const mp_int* pY) err = sp_1024_ecc_is_point_16(pub, NULL); } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, NULL, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, NULL, DYNAMIC_TYPE_ECC); return err; } @@ -105610,13 +103441,8 @@ int sp_ecc_is_point_1024(const mp_int* pX, const mp_int* pY) int sp_ecc_check_key_1024(const mp_int* pX, const mp_int* pY, const mp_int* privm, void* heap) { -#ifdef WOLFSSL_SP_SMALL_STACK - sp_digit* priv = NULL; - sp_point_1024* pub = NULL; -#else - sp_digit priv[16]; - sp_point_1024 pub[2]; -#endif + SP_DECL_VAR(sp_digit, priv, 16); + SP_DECL_VAR(sp_point_1024, pub, 2); sp_point_1024* p = NULL; const byte one[1] = { 1 }; int err = MP_OKAY; @@ -105634,21 +103460,8 @@ int sp_ecc_check_key_1024(const mp_int* pX, const mp_int* pY, err = ECC_OUT_OF_RANGE_E; } -#ifdef WOLFSSL_SP_SMALL_STACK - if (err == MP_OKAY) { - pub = (sp_point_1024*)XMALLOC(sizeof(sp_point_1024) * 2, heap, - DYNAMIC_TYPE_ECC); - if (pub == NULL) - err = MEMORY_E; - } - if (err == MP_OKAY && privm) { - priv = (sp_digit*)XMALLOC(sizeof(sp_digit) * 16, heap, - DYNAMIC_TYPE_ECC); - if (priv == NULL) - err = MEMORY_E; - } -#endif - + SP_ALLOC_VAR(sp_digit, priv, 16, heap, DYNAMIC_TYPE_ECC); + SP_ALLOC_VAR(sp_point_1024, pub, 2, heap, DYNAMIC_TYPE_ECC); if (err == MP_OKAY) { p = pub + 1; @@ -105714,10 +103527,8 @@ int sp_ecc_check_key_1024(const mp_int* pX, const mp_int* pY, } } -#ifdef WOLFSSL_SP_SMALL_STACK - XFREE(pub, heap, DYNAMIC_TYPE_ECC); - XFREE(priv, heap, DYNAMIC_TYPE_ECC); -#endif + SP_FREE_VAR(pub, heap, DYNAMIC_TYPE_ECC); + SP_FREE_VAR(priv, heap, DYNAMIC_TYPE_ECC); return err; } diff --git a/wolfcrypt/src/sp_x86_64_asm.S b/wolfcrypt/src/sp_x86_64_asm.S index a81e20aaa..9281f1017 100644 --- a/wolfcrypt/src/sp_x86_64_asm.S +++ b/wolfcrypt/src/sp_x86_64_asm.S @@ -58952,10 +58952,10 @@ L_256_mod_inv_4_store_end: .section __DATA,__data #endif /* __APPLE__ */ L_sp256_mod_inv_avx2_4_order: -.long 0x632551,0x1e84f3b,0x3bce6fa,0x3ffffff -.long 0x3ff0000,0x0,0x0,0x0 -.long 0x272b0bf,0x2b69c5e,0x3ffffff,0x3ff -.long 0x3fffff,0x0,0x0,0x0 +.long 0x00632551,0x01e84f3b,0x03bce6fa,0x03ffffff +.long 0x03ff0000,0x00000000,0x00000000,0x00000000 +.long 0x0272b0bf,0x02b69c5e,0x03ffffff,0x000003ff +.long 0x003fffff,0x00000000,0x00000000,0x00000000 #ifndef __APPLE__ .data #else @@ -58975,48 +58975,48 @@ L_sp256_mod_inv_avx2_4_one: .section __DATA,__data #endif /* __APPLE__ */ L_sp256_mod_inv_avx2_4_all_one: -.long 0x1,0x1,0x1,0x1 -.long 0x1,0x1,0x1,0x1 +.long 0x00000001,0x00000001,0x00000001,0x00000001 +.long 0x00000001,0x00000001,0x00000001,0x00000001 #ifndef __APPLE__ .data #else .section __DATA,__data #endif /* __APPLE__ */ L_sp256_mod_inv_avx2_4_mask01111: -.long 0x0,0x1,0x1,0x1 -.long 0x1,0x0,0x0,0x0 +.long 0x00000000,0x00000001,0x00000001,0x00000001 +.long 0x00000001,0x00000000,0x00000000,0x00000000 #ifndef __APPLE__ .data #else .section __DATA,__data #endif /* __APPLE__ */ L_sp256_mod_inv_avx2_4_down_one_dword: -.long 0x1,0x2,0x3,0x4 -.long 0x5,0x6,0x7,0x7 +.long 0x00000001,0x00000002,0x00000003,0x00000004 +.long 0x00000005,0x00000006,0x00000007,0x00000007 #ifndef __APPLE__ .data #else .section __DATA,__data #endif /* __APPLE__ */ L_sp256_mod_inv_avx2_4_neg: -.long 0x0,0x0,0x0,0x0 -.long 0x80000000,0x0,0x0,0x0 +.long 0x00000000,0x00000000,0x00000000,0x00000000 +.long 0x80000000,0x00000000,0x00000000,0x00000000 #ifndef __APPLE__ .data #else .section __DATA,__data #endif /* __APPLE__ */ L_sp256_mod_inv_avx2_4_up_one_dword: -.long 0x7,0x0,0x1,0x2 -.long 0x3,0x7,0x7,0x7 +.long 0x00000007,0x00000000,0x00000001,0x00000002 +.long 0x00000003,0x00000007,0x00000007,0x00000007 #ifndef __APPLE__ .data #else .section __DATA,__data #endif /* __APPLE__ */ L_sp256_mod_inv_avx2_4_mask26: -.long 0x3ffffff,0x3ffffff,0x3ffffff,0x3ffffff -.long 0x3ffffff,0x0,0x0,0x0 +.long 0x03ffffff,0x03ffffff,0x03ffffff,0x03ffffff +.long 0x03ffffff,0x00000000,0x00000000,0x00000000 /* Non-constant time modular inversion. * * @param [out] r Resulting number. diff --git a/wolfcrypt/src/sp_x86_64_asm.asm b/wolfcrypt/src/sp_x86_64_asm.asm index de95a1ab8..fcce0f5ea 100644 --- a/wolfcrypt/src/sp_x86_64_asm.asm +++ b/wolfcrypt/src/sp_x86_64_asm.asm @@ -1,6 +1,6 @@ ; /* sp_x86_64_asm.asm */ ; /* -; * Copyright (C) 2006-2025 wolfSSL Inc. +; * Copyright (C) 2006-2025 wolfSSL Inc. ; * ; * This file is part of wolfSSL. ; *