Skip to content

Commit 20f640a

Browse files
authored
Merge pull request #10035 from night1rider/allow-0-len-input-hash-update
Allow zero-length input in _wc_Hash_Grow and fix SHA Copy MAX32666
2 parents 6f23de4 + 92e3647 commit 20f640a

3 files changed

Lines changed: 15 additions & 6 deletions

File tree

.github/workflows/os-check.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ jobs:
9898
'--enable-curve25519=nonblock --enable-ecc=nonblock --enable-sp=yes,nonblock CPPFLAGS="-DWOLFSSL_PUBLIC_MP -DWOLFSSL_DEBUG_NONBLOCK"',
9999
'--enable-certreq --enable-certext --enable-certgen --disable-secure-renegotiation-info CPPFLAGS="-DNO_TLS"',
100100
'--enable-ocsp --enable-ocsp-responder --enable-ocspstapling CPPFLAGS="-DWOLFSSL_NONBLOCK_OCSP" --enable-maxfragment',
101+
'--enable-all CPPFLAGS=-DWOLFSSL_HASH_KEEP',
101102
]
102103
name: make check
103104
if: github.repository_owner == 'wolfssl'

wolfcrypt/src/hash.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1956,9 +1956,14 @@ int _wc_Hash_Grow(byte** msg, word32* used, word32* len, const byte* in,
19561956
{
19571957
word32 usedSz = 0;
19581958

1959-
if (inSz <= 0 || !WC_SAFE_SUM_WORD32(*used, (word32)inSz, usedSz))
1959+
if (inSz < 0 || !WC_SAFE_SUM_WORD32(*used, (word32)inSz, usedSz))
19601960
return BAD_FUNC_ARG;
19611961

1962+
/* Allow zero-length input as a no-op. Some callers may pass zero-length
1963+
* data during hash operations and this should not be treated as an error. */
1964+
if (inSz == 0)
1965+
return 0;
1966+
19621967
if (*len < usedSz) {
19631968
if (*msg == NULL) {
19641969
*msg = (byte*)XMALLOC(usedSz, heap, DYNAMIC_TYPE_TMP_BUFFER);

wolfcrypt/src/port/maxim/max3266x.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -808,23 +808,26 @@ int wc_MXC_TPU_SHA_Copy(void* src, void* dst, word32 ctxSz,
808808
byte** dstMsg, word32* dstUsed, word32* dstLen,
809809
void* dstHeap, void* srcHeap)
810810
{
811-
byte* srcBuf;
811+
byte* srcBuf = NULL;
812812

813813
if (src == NULL || dst == NULL || dstMsg == NULL ||
814814
dstUsed == NULL || dstLen == NULL || ctxSz == 0) {
815815
return BAD_FUNC_ARG;
816816
}
817817

818-
srcBuf = *dstMsg;
819-
820818
/* Free existing dst msg buffer using dst's original heap */
821819
wc_MXC_TPU_SHA_Free(dstMsg, dstUsed, dstLen, dstHeap);
822820

823821
/* Shallow copy the full context struct */
824822
XMEMCPY(dst, src, ctxSz);
825823

826-
/* Deep copy src msg buffer if present, allocate using src's heap */
827-
if (srcBuf != NULL) {
824+
/* Deep copy src msg buffer if present. Since dstMsg points into the dst
825+
* struct, the XMEMCPY above overwrites it with the src's msg pointer.
826+
* Save that pointer, allocate a new buffer for dst, and copy the data.
827+
* Do NOT move srcBuf assignment before XMEMCPY - it must capture the
828+
* src msg pointer that lands in *dstMsg after the shallow copy. */
829+
if (*dstMsg != NULL) {
830+
srcBuf = *dstMsg;
828831
*dstMsg = (byte*)XMALLOC(*dstLen, srcHeap, DYNAMIC_TYPE_TMP_BUFFER);
829832
if (*dstMsg == NULL) {
830833
return MEMORY_E;

0 commit comments

Comments
 (0)