diff --git a/configure.ac b/configure.ac index 2e134be97..63c890002 100644 --- a/configure.ac +++ b/configure.ac @@ -1341,9 +1341,11 @@ AC_ARG_ENABLE([smallstack], [ ENABLED_SMALL_STACK=yes ] ) -if test "$ENABLED_SMALL_STACK" = "no" +if test "x$ENABLED_SMALL_STACK" = "xyes" then - AM_CFLAGS="$AM_CFLAGS -DNO_SMALL_STACK" + AM_CFLAGS="$AM_CFLAGS -DCYASSL_SMALL_STACK" +else + AM_CFLAGS="$AM_CFLAGS -DNO_CYASSL_SMALL_STACK" fi diff --git a/ctaocrypt/src/blake2b.c b/ctaocrypt/src/blake2b.c index 77b736d22..a9d1753ac 100644 --- a/ctaocrypt/src/blake2b.c +++ b/ctaocrypt/src/blake2b.c @@ -163,7 +163,7 @@ int blake2b_init_key( blake2b_state *S, const byte outlen, const void *key, if( blake2b_init_param( S, P ) < 0 ) return -1; { -#ifndef NO_SMALL_STACK +#ifdef CYASSL_SMALL_STACK byte* block; block = (byte*)XMALLOC(BLAKE2B_BLOCKBYTES, NULL, DYNAMIC_TYPE_TMP_BUFFER); @@ -179,7 +179,7 @@ int blake2b_init_key( blake2b_state *S, const byte outlen, const void *key, secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from */ /* memory */ -#ifndef NO_SMALL_STACK +#ifdef CYASSL_SMALL_STACK XFREE(block, NULL, DYNAMIC_TYPE_TMP_BUFFER); #endif } @@ -191,7 +191,7 @@ static int blake2b_compress( blake2b_state *S, { int i; -#ifndef NO_SMALL_STACK +#ifdef CYASSL_SMALL_STACK word64* m; word64* v; @@ -266,7 +266,7 @@ static int blake2b_compress( blake2b_state *S, #undef G #undef ROUND -#ifndef NO_SMALL_STACK +#ifdef CYASSL_SMALL_STACK XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER); XFREE(v, NULL, DYNAMIC_TYPE_TMP_BUFFER); #endif diff --git a/ctaocrypt/src/camellia.c b/ctaocrypt/src/camellia.c index e177896fd..eaed4c90b 100644 --- a/ctaocrypt/src/camellia.c +++ b/ctaocrypt/src/camellia.c @@ -491,6 +491,8 @@ static int camellia_setup128(const unsigned char *key, u32 *subkey) u32 kll, klr, krl, krr; u32 il, ir, t0, t1, w0, w1; u32 kw4l, kw4r, dw, tl, tr; + +#ifdef CYASSL_SMALL_STACK u32* subL; u32* subR; @@ -503,6 +505,10 @@ static int camellia_setup128(const unsigned char *key, u32 *subkey) XFREE(subL, NULL, DYNAMIC_TYPE_TMP_BUFFER); return MEMORY_E; } +#else + u32 subL[26]; + u32 subR[26]; +#endif /** * k == kll || klr || krl || krr (|| is concatination) @@ -704,8 +710,10 @@ static int camellia_setup128(const unsigned char *key, u32 *subkey) dw = CamelliaSubkeyL(23) ^ CamelliaSubkeyR(23), dw = CAMELLIA_RL8(dw); CamelliaSubkeyR(23) = CamelliaSubkeyL(23) ^ dw, CamelliaSubkeyL(23) = dw; +#ifdef CYASSL_SMALL_STACK XFREE(subL, NULL, DYNAMIC_TYPE_TMP_BUFFER); XFREE(subR, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif return 0; } @@ -716,6 +724,8 @@ static int camellia_setup256(const unsigned char *key, u32 *subkey) u32 krll,krlr,krrl,krrr; /* right half of key */ u32 il, ir, t0, t1, w0, w1; /* temporary variables */ u32 kw4l, kw4r, dw, tl, tr; + +#ifdef CYASSL_SMALL_STACK u32* subL; u32* subR; @@ -728,6 +738,10 @@ static int camellia_setup256(const unsigned char *key, u32 *subkey) XFREE(subL, NULL, DYNAMIC_TYPE_TMP_BUFFER); return MEMORY_E; } +#else + u32 subL[34]; + u32 subR[34]; +#endif /** * key = (kll || klr || krl || krr || krll || krlr || krrl || krrr) @@ -1003,8 +1017,10 @@ static int camellia_setup256(const unsigned char *key, u32 *subkey) dw = CamelliaSubkeyL(31) ^ CamelliaSubkeyR(31), dw = CAMELLIA_RL8(dw); CamelliaSubkeyR(31) = CamelliaSubkeyL(31) ^ dw,CamelliaSubkeyL(31) = dw; +#ifdef CYASSL_SMALL_STACK XFREE(subL, NULL, DYNAMIC_TYPE_TMP_BUFFER); XFREE(subR, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif return 0; } diff --git a/ctaocrypt/src/des3.c b/ctaocrypt/src/des3.c index 98fa2d8e7..dc1aac910 100644 --- a/ctaocrypt/src/des3.c +++ b/ctaocrypt/src/des3.c @@ -988,12 +988,16 @@ static INLINE void FPERM(word32* left, word32* right) static int DesSetKey(const byte* key, int dir, word32* out) { +#ifdef CYASSL_SMALL_STACK byte* buffer = (byte*)XMALLOC(56+56+8, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (!buffer) { + if (buffer == NULL) return MEMORY_E; - } - else { +#else + byte buffer[56+56+8]; +#endif + + { byte* const pc1m = buffer; /* place to modify pc1 into */ byte* const pcr = pc1m + 56; /* place to rotate pc1 into */ byte* const ks = pcr + 56; @@ -1048,7 +1052,9 @@ static int DesSetKey(const byte* key, int dir, word32* out) } } +#ifdef CYASSL_SMALL_STACK XFREE(buffer, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif } return 0; diff --git a/ctaocrypt/src/sha256.c b/ctaocrypt/src/sha256.c index 30f639efb..2a0d1f979 100644 --- a/ctaocrypt/src/sha256.c +++ b/ctaocrypt/src/sha256.c @@ -133,12 +133,17 @@ static const word32 K[64] = { static int Transform(Sha256* sha256) { word32 S[8], t0, t1; - word32* W; int i; +#ifdef CYASSL_SMALL_STACK + word32* W; + W = (word32*) XMALLOC(sizeof(word32) * 64, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (W == NULL) return MEMORY_E; +#else + word32 W[64]; +#endif /* Copy context->state[] to working vars */ for (i = 0; i < 8; i++) @@ -166,7 +171,9 @@ static int Transform(Sha256* sha256) sha256->digest[i] += S[i]; } +#ifdef CYASSL_SMALL_STACK XFREE(W, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif return 0; } diff --git a/ctaocrypt/src/sha512.c b/ctaocrypt/src/sha512.c index 81595b8d0..ceb5a7e72 100644 --- a/ctaocrypt/src/sha512.c +++ b/ctaocrypt/src/sha512.c @@ -149,11 +149,16 @@ static int Transform(Sha512* sha512) word32 j; word64 T[8]; + +#ifdef CYASSL_SMALL_STACK word64* W; W = (word64*) XMALLOC(sizeof(word64) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (W == NULL) return MEMORY_E; +#else + word64 W[16]; +#endif /* Copy digest to working vars */ XMEMCPY(T, sha512->digest, sizeof(T)); @@ -192,7 +197,9 @@ static int Transform(Sha512* sha512) XMEMSET(W, 0, sizeof(word64) * 16); XMEMSET(T, 0, sizeof(T)); +#ifdef CYASSL_SMALL_STACK XFREE(W, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif return 0; } @@ -317,11 +324,16 @@ static int Transform384(Sha384* sha384) word32 j; word64 T[8]; + +#ifdef CYASSL_SMALL_STACK word64* W; W = (word64*) XMALLOC(sizeof(word64) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (W == NULL) return MEMORY_E; +#else + word64 W[16]; +#endif /* Copy digest to working vars */ XMEMCPY(T, sha384->digest, sizeof(T)); @@ -360,7 +372,9 @@ static int Transform384(Sha384* sha384) XMEMSET(W, 0, sizeof(word64) * 16); XMEMSET(T, 0, sizeof(T)); +#ifdef CYASSL_SMALL_STACK XFREE(W, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif return 0; }