Skip to content

Commit fc7d4ff

Browse files
committed
PR#9545 20251211-DRBG-SHA2-smallstackcache-prealloc addressing peer review: clear dest if necessary in InitHandshakeHashesAndCopy(), style tweaks in random.c, explanatory comments in sha512.c.
1 parent 33fc601 commit fc7d4ff

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

src/internal.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7345,14 +7345,19 @@ int InitHandshakeHashesAndCopy(WOLFSSL* ssl, HS_Hashes* source,
73457345
{
73467346
int ret;
73477347

7348-
if (source == NULL)
7348+
if ((ssl == NULL) || (source == NULL) || (destination == NULL))
73497349
return BAD_FUNC_ARG;
73507350

7351-
/* Note we can't call InitHandshakeHashes() here, because the copy methods
7352-
* overwrite the entire dest low level hash struct. With some hashes and
7353-
* settings (e.g. SHA-2 hashes with WOLFSSL_SMALL_STACK_CACHE), internal
7354-
* scratch buffers are preallocated at init and will leak if overwritten.
7351+
/* If *destination is already allocated, its constituent hashes need to be
7352+
* freed, else they would leak. To keep things simple, we reuse
7353+
* FreeHandshakeHashes(), which deallocates *destination.
73557354
*/
7355+
if (*destination != NULL) {
7356+
HS_Hashes* tmp = ssl->hsHashes;
7357+
ssl->hsHashes = *destination;
7358+
FreeHandshakeHashes(ssl);
7359+
ssl->hsHashes = tmp;
7360+
}
73567361

73577362
/* allocate handshake hashes */
73587363
*destination = (HS_Hashes*)XMALLOC(sizeof(HS_Hashes), ssl->heap,
@@ -7361,6 +7366,12 @@ int InitHandshakeHashesAndCopy(WOLFSSL* ssl, HS_Hashes* source,
73617366
WOLFSSL_MSG("HS_Hashes Memory error");
73627367
return MEMORY_E;
73637368
}
7369+
7370+
/* Note we can't call InitHandshakeHashes() here, because the copy methods
7371+
* overwrite the entire dest low level hash struct. With some hashes and
7372+
* settings (e.g. SHA-2 hashes with WOLFSSL_SMALL_STACK_CACHE), internal
7373+
* scratch buffers are preallocated at init and will leak if overwritten.
7374+
*/
73647375
XMEMSET(*destination, 0, sizeof(HS_Hashes));
73657376

73667377
/* now copy the source contents to the destination */

wolfcrypt/src/random.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -735,9 +735,7 @@ static int Hash_DRBG_Instantiate(DRBG_internal* drbg, const byte* seed, word32 s
735735
const byte* nonce, word32 nonceSz,
736736
void* heap, int devId)
737737
{
738-
#ifdef WOLFSSL_SMALL_STACK_CACHE
739738
int ret = DRBG_FAILURE;
740-
#endif
741739

742740
XMEMSET(drbg, 0, sizeof(DRBG_internal));
743741
drbg->heap = heap;
@@ -757,10 +755,9 @@ static int Hash_DRBG_Instantiate(DRBG_internal* drbg, const byte* seed, word32 s
757755
return ret;
758756
#endif
759757

760-
if (seed == NULL)
761-
return 0;
762-
else
763-
return Hash_DRBG_Init(drbg, seed, seedSz, nonce, nonceSz);
758+
if (seed != NULL)
759+
ret = Hash_DRBG_Init(drbg, seed, seedSz, nonce, nonceSz);
760+
return ret;
764761
}
765762

766763
/* Returns: DRBG_SUCCESS or DRBG_FAILURE */
@@ -815,11 +812,7 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz,
815812
int ret = 0;
816813
#ifdef HAVE_HASHDRBG
817814
word32 seedSz = SEED_SZ + SEED_BLOCK_SZ;
818-
#ifdef WOLFSSL_SMALL_STACK
819-
byte* seed = NULL;
820-
#else
821-
byte seed[MAX_SEED_SZ];
822-
#endif
815+
WC_DECLARE_VAR(seed, byte, MAX_SEED_SZ, rng->heap);
823816
int drbg_instantiated = 0;
824817
#ifdef WOLFSSL_SMALL_STACK_CACHE
825818
int drbg_scratch_instantiated = 0;
@@ -981,8 +974,7 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz,
981974

982975
#ifdef WOLFSSL_SMALL_STACK
983976
if (ret == 0) {
984-
seed = (byte*)XMALLOC(MAX_SEED_SZ, rng->heap,
985-
DYNAMIC_TYPE_SEED);
977+
WC_ALLOC_VAR_EX(seed, byte, MAX_SEED_SZ, rng->heap, DYNAMIC_TYPE_SEED, WC_DO_NOTHING);
986978
if (seed == NULL) {
987979
ret = MEMORY_E;
988980
rng->status = DRBG_FAILED;
@@ -1418,7 +1410,8 @@ static int wc_RNG_HealthTest_ex_internal(DRBG_internal* drbg,
14181410
}
14191411

14201412
#ifdef WOLFSSL_SMALL_STACK_CACHE
1421-
(void)heap; (void)devId;
1413+
(void)heap;
1414+
(void)devId;
14221415

14231416
if (Hash_DRBG_Init(drbg, seedA, seedASz, nonce, nonceSz) != 0) {
14241417
goto exit_rng_ht;

wolfcrypt/src/sha512.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +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+
/* This allocation combines the customary W buffer used by
874+
* _Transform_Sha512() with additional buffer space used by
875+
* wc_Sha512Transform().
876+
*/
873877
sha512->W = (word64 *)XMALLOC((sizeof(word64) * 16) + WC_SHA512_BLOCK_SIZE,
874878
sha512->heap, DYNAMIC_TYPE_DIGEST);
875879
if (sha512->W == NULL)
@@ -1702,6 +1706,9 @@ int wc_Sha512Transform(wc_Sha512* sha, const unsigned char* data)
17021706
#if defined(WOLFSSL_SMALL_STACK_CACHE)
17031707
if (sha->W == NULL)
17041708
return BAD_FUNC_ARG;
1709+
/* Skip over the initial `W' buffer at the start (used by
1710+
* _Transform_Sha512()).
1711+
*/
17051712
buffer = sha->W + 16;
17061713
#elif defined(WOLFSSL_SMALL_STACK)
17071714
buffer = (word64*)XMALLOC(WC_SHA512_BLOCK_SIZE, sha->heap,
@@ -1873,6 +1880,10 @@ static int InitSha384(wc_Sha384* sha384)
18731880

18741881
#ifdef WOLFSSL_SMALL_STACK_CACHE
18751882
if (sha384->W == NULL) {
1883+
/* This allocation combines the customary W buffer used by
1884+
* _Transform_Sha512() with additional buffer space used by
1885+
* wc_Sha512Transform().
1886+
*/
18761887
sha384->W = (word64 *)XMALLOC((sizeof(word64) * 16) + WC_SHA512_BLOCK_SIZE,
18771888
sha384->heap, DYNAMIC_TYPE_DIGEST);
18781889
if (sha384->W == NULL)
@@ -2232,6 +2243,10 @@ int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst)
22322243

22332244
XMEMCPY(dst, src, sizeof(wc_Sha512));
22342245
#ifdef WOLFSSL_SMALL_STACK_CACHE
2246+
/* This allocation combines the customary W buffer used by
2247+
* _Transform_Sha512() with additional buffer space used by
2248+
* wc_Sha512Transform().
2249+
*/
22352250
dst->W = (word64 *)XMALLOC((sizeof(word64) * 16) + WC_SHA512_BLOCK_SIZE,
22362251
dst->heap, DYNAMIC_TYPE_DIGEST);
22372252
if (dst->W == NULL) {
@@ -2667,6 +2682,10 @@ int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst)
26672682
XMEMCPY(dst, src, sizeof(wc_Sha384));
26682683

26692684
#ifdef WOLFSSL_SMALL_STACK_CACHE
2685+
/* This allocation combines the customary W buffer used by
2686+
* _Transform_Sha512() with additional buffer space used by
2687+
* wc_Sha512Transform().
2688+
*/
26702689
dst->W = (word64 *)XMALLOC((sizeof(word64) * 16) + WC_SHA384_BLOCK_SIZE,
26712690
dst->heap, DYNAMIC_TYPE_DIGEST);
26722691
if (dst->W == NULL) {

0 commit comments

Comments
 (0)