Skip to content

Commit 2b28931

Browse files
committed
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().
1 parent 525266c commit 2b28931

File tree

2 files changed

+68
-25
lines changed

2 files changed

+68
-25
lines changed

wolfcrypt/src/sha256.c

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,10 @@ static int InitSha256(wc_Sha256* sha256)
678678
sha256->devCtx = NULL;
679679
#endif
680680
#ifdef WOLFSSL_SMALL_STACK_CACHE
681-
sha256->W = NULL;
681+
sha256->W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE,
682+
sha256->heap, DYNAMIC_TYPE_DIGEST);
683+
if (sha256->W == NULL)
684+
return MEMORY_E;
682685
#endif
683686

684687
ret = InitSha256(sha256);
@@ -1163,7 +1166,10 @@ static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
11631166
}
11641167
#endif
11651168
#ifdef WOLFSSL_SMALL_STACK_CACHE
1166-
sha256->W = NULL;
1169+
sha256->W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE,
1170+
sha256->heap, DYNAMIC_TYPE_DIGEST);
1171+
if (sha256->W == NULL)
1172+
return MEMORY_E;
11671173
#endif
11681174

11691175
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
@@ -1244,13 +1250,8 @@ static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
12441250

12451251
#if defined(WOLFSSL_SMALL_STACK_CACHE) && !defined(WOLFSSL_NO_MALLOC)
12461252
word32* W = sha256->W;
1247-
if (W == NULL) {
1248-
W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE,
1249-
sha256->heap, DYNAMIC_TYPE_DIGEST);
1250-
if (W == NULL)
1251-
return MEMORY_E;
1252-
sha256->W = W;
1253-
}
1253+
if (W == NULL)
1254+
return BAD_FUNC_ARG;
12541255
#elif defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
12551256
word32* W;
12561257
W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE,
@@ -2065,6 +2066,15 @@ static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
20652066
{
20662067
int ret = 0;
20672068

2069+
#ifdef WOLFSSL_SMALL_STACK_CACHE
2070+
if (sha224->W == NULL) {
2071+
sha224->W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE,
2072+
sha224->heap, DYNAMIC_TYPE_DIGEST);
2073+
if (sha224->W == NULL)
2074+
return MEMORY_E;
2075+
}
2076+
#endif
2077+
20682078
sha224->digest[0] = 0xc1059ed8;
20692079
sha224->digest[1] = 0x367cd507;
20702080
sha224->digest[2] = 0x3070dd17;
@@ -2575,7 +2585,12 @@ int wc_Sha224_Grow(wc_Sha224* sha224, const byte* in, int inSz)
25752585
XMEMCPY(dst, src, sizeof(wc_Sha224));
25762586

25772587
#ifdef WOLFSSL_SMALL_STACK_CACHE
2578-
dst->W = NULL;
2588+
dst->W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE,
2589+
dst->heap, DYNAMIC_TYPE_DIGEST);
2590+
if (dst->W == NULL) {
2591+
XMEMSET(dst, 0, sizeof(wc_Sha224));
2592+
return MEMORY_E;
2593+
}
25792594
#endif
25802595

25812596
#if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3)
@@ -2727,7 +2742,12 @@ int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
27272742
#endif
27282743

27292744
#ifdef WOLFSSL_SMALL_STACK_CACHE
2730-
dst->W = NULL;
2745+
dst->W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE,
2746+
dst->heap, DYNAMIC_TYPE_DIGEST);
2747+
if (dst->W == NULL) {
2748+
XMEMSET(dst, 0, sizeof(wc_Sha256));
2749+
return MEMORY_E;
2750+
}
27312751
#endif
27322752

27332753
#if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3)

wolfcrypt/src/sha512.c

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,10 @@ static int InitSha512_Family(wc_Sha512* sha512, void* heap, int devId,
870870

871871
sha512->heap = heap;
872872
#ifdef WOLFSSL_SMALL_STACK_CACHE
873-
sha512->W = NULL;
873+
sha512->W = (word64 *)XMALLOC((sizeof(word64) * 16) + WC_SHA512_BLOCK_SIZE,
874+
sha512->heap, DYNAMIC_TYPE_DIGEST);
875+
if (sha512->W == NULL)
876+
return MEMORY_E;
874877
#endif
875878
#ifdef WOLF_CRYPTO_CB
876879
sha512->devId = devId;
@@ -1031,14 +1034,10 @@ static int _Transform_Sha512(wc_Sha512* sha512)
10311034
word32 j;
10321035
word64 T[8];
10331036

1034-
#ifdef WOLFSSL_SMALL_STACK_CACHE
1037+
#if defined(WOLFSSL_SMALL_STACK_CACHE)
10351038
word64* W = sha512->W;
1036-
if (W == NULL) {
1037-
W = (word64*)XMALLOC(sizeof(word64) * 16, sha512->heap, DYNAMIC_TYPE_TMP_BUFFER);
1038-
if (W == NULL)
1039-
return MEMORY_E;
1040-
sha512->W = W;
1041-
}
1039+
if (W == NULL)
1040+
return BAD_FUNC_ARG;
10421041
#elif defined(WOLFSSL_SMALL_STACK)
10431042
word64* W;
10441043
W = (word64*) XMALLOC(sizeof(word64) * 16, sha512->heap, DYNAMIC_TYPE_TMP_BUFFER);
@@ -1646,7 +1645,7 @@ void wc_Sha512Free(wc_Sha512* sha512)
16461645

16471646
#ifdef WOLFSSL_SMALL_STACK_CACHE
16481647
if (sha512->W != NULL) {
1649-
ForceZero(sha512->W, sizeof(word64) * 16);
1648+
ForceZero(sha512->W, (sizeof(word64) * 16) + WC_SHA512_BLOCK_SIZE);
16501649
XFREE(sha512->W, sha512->heap, DYNAMIC_TYPE_TMP_BUFFER);
16511650
sha512->W = NULL;
16521651
}
@@ -1699,7 +1698,12 @@ int wc_Sha512Transform(wc_Sha512* sha, const unsigned char* data)
16991698
return BAD_FUNC_ARG;
17001699
}
17011700

1702-
#ifdef WOLFSSL_SMALL_STACK
1701+
1702+
#if defined(WOLFSSL_SMALL_STACK_CACHE)
1703+
if (sha->W == NULL)
1704+
return BAD_FUNC_ARG;
1705+
buffer = sha->W + 16;
1706+
#elif defined(WOLFSSL_SMALL_STACK)
17031707
buffer = (word64*)XMALLOC(WC_SHA512_BLOCK_SIZE, sha->heap,
17041708
DYNAMIC_TYPE_TMP_BUFFER);
17051709
if (buffer == NULL)
@@ -1733,7 +1737,7 @@ int wc_Sha512Transform(wc_Sha512* sha, const unsigned char* data)
17331737

17341738
XMEMCPY(sha->buffer, buffer, WC_SHA512_BLOCK_SIZE);
17351739
#endif
1736-
#ifdef WOLFSSL_SMALL_STACK
1740+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SMALL_STACK_CACHE)
17371741
ForceZero(buffer, WC_SHA512_BLOCK_SIZE);
17381742
XFREE(buffer, sha->heap, DYNAMIC_TYPE_TMP_BUFFER);
17391743
#endif
@@ -1867,6 +1871,15 @@ static int InitSha384(wc_Sha384* sha384)
18671871
return BAD_FUNC_ARG;
18681872
}
18691873

1874+
#ifdef WOLFSSL_SMALL_STACK_CACHE
1875+
if (sha384->W == NULL) {
1876+
sha384->W = (word64 *)XMALLOC((sizeof(word64) * 16) + WC_SHA512_BLOCK_SIZE,
1877+
sha384->heap, DYNAMIC_TYPE_DIGEST);
1878+
if (sha384->W == NULL)
1879+
return MEMORY_E;
1880+
}
1881+
#endif
1882+
18701883
sha384->digest[0] = W64LIT(0xcbbb9d5dc1059ed8);
18711884
sha384->digest[1] = W64LIT(0x629a292a367cd507);
18721885
sha384->digest[2] = W64LIT(0x9159015a3070dd17);
@@ -2106,7 +2119,7 @@ void wc_Sha384Free(wc_Sha384* sha384)
21062119

21072120
#ifdef WOLFSSL_SMALL_STACK_CACHE
21082121
if (sha384->W != NULL) {
2109-
ForceZero(sha384->W, sizeof(word64) * 16);
2122+
ForceZero(sha384->W, (sizeof(word64) * 16) + WC_SHA512_BLOCK_SIZE);
21102123
XFREE(sha384->W, sha384->heap, DYNAMIC_TYPE_TMP_BUFFER);
21112124
sha384->W = NULL;
21122125
}
@@ -2219,7 +2232,12 @@ int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst)
22192232

22202233
XMEMCPY(dst, src, sizeof(wc_Sha512));
22212234
#ifdef WOLFSSL_SMALL_STACK_CACHE
2222-
dst->W = NULL;
2235+
dst->W = (word64 *)XMALLOC((sizeof(word64) * 16) + WC_SHA512_BLOCK_SIZE,
2236+
dst->heap, DYNAMIC_TYPE_DIGEST);
2237+
if (dst->W == NULL) {
2238+
XMEMSET(dst, 0, sizeof(wc_Sha512));
2239+
return MEMORY_E;
2240+
}
22232241
#endif
22242242

22252243
#if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3) && \
@@ -2649,7 +2667,12 @@ int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst)
26492667
XMEMCPY(dst, src, sizeof(wc_Sha384));
26502668

26512669
#ifdef WOLFSSL_SMALL_STACK_CACHE
2652-
dst->W = NULL;
2670+
dst->W = (word64 *)XMALLOC((sizeof(word64) * 16) + WC_SHA384_BLOCK_SIZE,
2671+
dst->heap, DYNAMIC_TYPE_DIGEST);
2672+
if (dst->W == NULL) {
2673+
XMEMSET(dst, 0, sizeof(wc_Sha384));
2674+
return MEMORY_E;
2675+
}
26532676
#endif
26542677

26552678
#if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3) && \

0 commit comments

Comments
 (0)