backport 2b28931855 "wolfcrypt/src/sha256.c and wolfcrypt/src/sha512.c: in WOLFSSL_SMALL_STACK_CACHE builds, allocate shafoo->W at init or context copy time, rather than in the transform function. for the SHA512 family, allocate additional space in W for "buffer" in wc_Sha512Transform()."
This commit is contained in:
@@ -394,7 +394,10 @@ static int InitSha256(wc_Sha256* sha256)
|
||||
sha256->devCtx = NULL;
|
||||
#endif
|
||||
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
||||
sha256->W = NULL;
|
||||
sha256->W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE,
|
||||
sha256->heap, DYNAMIC_TYPE_DIGEST);
|
||||
if (sha256->W == NULL)
|
||||
return MEMORY_E;
|
||||
#endif
|
||||
|
||||
ret = InitSha256(sha256);
|
||||
@@ -772,7 +775,10 @@ static int InitSha256(wc_Sha256* sha256)
|
||||
sha256->devCtx = NULL;
|
||||
#endif
|
||||
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
||||
sha256->W = NULL;
|
||||
sha256->W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE,
|
||||
sha256->heap, DYNAMIC_TYPE_DIGEST);
|
||||
if (sha256->W == NULL)
|
||||
return MEMORY_E;
|
||||
#endif
|
||||
|
||||
ret = InitSha256(sha256);
|
||||
@@ -854,13 +860,8 @@ static int InitSha256(wc_Sha256* sha256)
|
||||
|
||||
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
||||
word32* W = sha256->W;
|
||||
if (W == NULL) {
|
||||
W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE, NULL,
|
||||
DYNAMIC_TYPE_DIGEST);
|
||||
if (W == NULL)
|
||||
return MEMORY_E;
|
||||
sha256->W = W;
|
||||
}
|
||||
if (W == NULL)
|
||||
return BAD_FUNC_ARG;
|
||||
#elif defined(WOLFSSL_SMALL_STACK)
|
||||
word32* W;
|
||||
W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE, NULL,
|
||||
@@ -1465,6 +1466,15 @@ static int InitSha256(wc_Sha256* sha256)
|
||||
return BAD_FUNC_ARG;
|
||||
}
|
||||
|
||||
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
||||
if (sha224->W == NULL) {
|
||||
sha224->W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE,
|
||||
sha224->heap, DYNAMIC_TYPE_DIGEST);
|
||||
if (sha224->W == NULL)
|
||||
return MEMORY_E;
|
||||
}
|
||||
#endif
|
||||
|
||||
sha224->digest[0] = 0xc1059ed8;
|
||||
sha224->digest[1] = 0x367cd507;
|
||||
sha224->digest[2] = 0x3070dd17;
|
||||
@@ -1690,7 +1700,12 @@ void wc_Sha256Free(wc_Sha256* sha256)
|
||||
|
||||
XMEMCPY(dst, src, sizeof(wc_Sha224));
|
||||
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
||||
dst->W = NULL;
|
||||
dst->W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE,
|
||||
dst->heap, DYNAMIC_TYPE_DIGEST);
|
||||
if (dst->W == NULL) {
|
||||
XMEMSET(dst, 0, sizeof(wc_Sha224));
|
||||
return MEMORY_E;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef WOLFSSL_SILABS_SE_ACCEL
|
||||
@@ -1787,7 +1802,12 @@ int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
|
||||
|
||||
XMEMCPY(dst, src, sizeof(wc_Sha256));
|
||||
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
||||
dst->W = NULL;
|
||||
dst->W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE,
|
||||
dst->heap, DYNAMIC_TYPE_DIGEST);
|
||||
if (dst->W == NULL) {
|
||||
XMEMSET(dst, 0, sizeof(wc_Sha256));
|
||||
return MEMORY_E;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef WOLFSSL_SILABS_SE_ACCEL
|
||||
|
||||
@@ -582,7 +582,10 @@ static int InitSha512_Family(wc_Sha512* sha512, void* heap, int devId,
|
||||
|
||||
sha512->heap = heap;
|
||||
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
||||
sha512->W = NULL;
|
||||
sha512->W = (word64 *)XMALLOC((sizeof(word64) * 16) + WC_SHA512_BLOCK_SIZE,
|
||||
sha512->heap, DYNAMIC_TYPE_DIGEST);
|
||||
if (sha512->W == NULL)
|
||||
return MEMORY_E;
|
||||
#endif
|
||||
#ifdef WOLF_CRYPTO_CB
|
||||
sha512->devId = devId;
|
||||
@@ -714,14 +717,10 @@ static int _Transform_Sha512(wc_Sha512* sha512)
|
||||
word32 j;
|
||||
word64 T[8];
|
||||
|
||||
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
||||
#if defined(WOLFSSL_SMALL_STACK_CACHE)
|
||||
word64* W = sha512->W;
|
||||
if (W == NULL) {
|
||||
W = (word64*)XMALLOC(sizeof(word64) * 16, sha512->heap, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
if (W == NULL)
|
||||
return MEMORY_E;
|
||||
sha512->W = W;
|
||||
}
|
||||
if (W == NULL)
|
||||
return BAD_FUNC_ARG;
|
||||
#elif defined(WOLFSSL_SMALL_STACK)
|
||||
word64* W;
|
||||
W = (word64*) XMALLOC(sizeof(word64) * 16, sha512->heap, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
@@ -1146,6 +1145,7 @@ void wc_Sha512Free(wc_Sha512* sha512)
|
||||
|
||||
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
||||
if (sha512->W != NULL) {
|
||||
ForceZero(sha512->W, (sizeof(word64) * 16) + WC_SHA512_BLOCK_SIZE);
|
||||
XFREE(sha512->W, sha512->heap, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
sha512->W = NULL;
|
||||
}
|
||||
@@ -1168,10 +1168,16 @@ int wc_Sha512Transform(wc_Sha512* sha, const unsigned char* data)
|
||||
{
|
||||
int ret;
|
||||
/* back up buffer */
|
||||
#ifdef WOLFSSL_SMALL_STACK
|
||||
word64 *buffer;
|
||||
#else
|
||||
word64 buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64)];
|
||||
WC_DECLARE_VAR(buffer, word64, WC_SHA512_BLOCK_SIZE / sizeof(word64), 0);
|
||||
#if defined(WOLFSSL_SMALL_STACK_CACHE)
|
||||
if (sha->W == NULL)
|
||||
return BAD_FUNC_ARG;
|
||||
buffer = sha->W + 16;
|
||||
#elif defined(WOLFSSL_SMALL_STACK)
|
||||
word64 *buffer = (word64*)XMALLOC(WC_SHA512_BLOCK_SIZE, sha->heap,
|
||||
DYNAMIC_TYPE_TMP_BUFFER);
|
||||
if (buffer == NULL)
|
||||
return MEMORY_E;
|
||||
#endif
|
||||
|
||||
/* sanity check */
|
||||
@@ -1179,13 +1185,6 @@ int wc_Sha512Transform(wc_Sha512* sha, const unsigned char* data)
|
||||
return BAD_FUNC_ARG;
|
||||
}
|
||||
|
||||
#ifdef WOLFSSL_SMALL_STACK
|
||||
buffer = (word64 *)XMALLOC(sizeof(word64) * 16, sha->heap,
|
||||
DYNAMIC_TYPE_TMP_BUFFER);
|
||||
if (buffer == NULL)
|
||||
return MEMORY_E;
|
||||
#endif
|
||||
|
||||
#if defined(USE_INTEL_SPEEDUP) && \
|
||||
(defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
|
||||
Sha512_SetTransform();
|
||||
@@ -1271,6 +1270,15 @@ static int InitSha384(wc_Sha384* sha384)
|
||||
return BAD_FUNC_ARG;
|
||||
}
|
||||
|
||||
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
||||
if (sha384->W == NULL) {
|
||||
sha384->W = (word64 *)XMALLOC((sizeof(word64) * 16) + WC_SHA512_BLOCK_SIZE,
|
||||
sha384->heap, DYNAMIC_TYPE_DIGEST);
|
||||
if (sha384->W == NULL)
|
||||
return MEMORY_E;
|
||||
}
|
||||
#endif
|
||||
|
||||
sha384->digest[0] = W64LIT(0xcbbb9d5dc1059ed8);
|
||||
sha384->digest[1] = W64LIT(0x629a292a367cd507);
|
||||
sha384->digest[2] = W64LIT(0x9159015a3070dd17);
|
||||
@@ -1437,6 +1445,7 @@ void wc_Sha384Free(wc_Sha384* sha384)
|
||||
|
||||
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
||||
if (sha384->W != NULL) {
|
||||
ForceZero(sha384->W, (sizeof(word64) * 16) + WC_SHA512_BLOCK_SIZE);
|
||||
XFREE(sha384->W, sha384->heap, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
sha384->W = NULL;
|
||||
}
|
||||
@@ -1507,7 +1516,12 @@ int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst)
|
||||
|
||||
XMEMCPY(dst, src, sizeof(wc_Sha512));
|
||||
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
||||
dst->W = NULL;
|
||||
dst->W = (word64 *)XMALLOC((sizeof(word64) * 16) + WC_SHA512_BLOCK_SIZE,
|
||||
dst->heap, DYNAMIC_TYPE_DIGEST);
|
||||
if (dst->W == NULL) {
|
||||
XMEMSET(dst, 0, sizeof(wc_Sha512));
|
||||
return MEMORY_E;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef WOLFSSL_SILABS_SHA512
|
||||
@@ -1725,7 +1739,12 @@ int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst)
|
||||
|
||||
XMEMCPY(dst, src, sizeof(wc_Sha384));
|
||||
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
||||
dst->W = NULL;
|
||||
dst->W = (word64 *)XMALLOC((sizeof(word64) * 16) + WC_SHA384_BLOCK_SIZE,
|
||||
dst->heap, DYNAMIC_TYPE_DIGEST);
|
||||
if (dst->W == NULL) {
|
||||
XMEMSET(dst, 0, sizeof(wc_Sha384));
|
||||
return MEMORY_E;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef WOLFSSL_SILABS_SHA384
|
||||
|
||||
Reference in New Issue
Block a user