Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion wolfcrypt/src/md5.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,8 @@ int wc_Md5Copy(wc_Md5* src, wc_Md5* dst)
ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
#endif
#ifdef WOLFSSL_PIC32MZ_HASH
ret = wc_Pic32HashCopy(&src->cache, &dst->cache);
ret = wc_Pic32HashCopy(&src->cache, &dst->cache, (byte*)src->buffer,
(byte*)dst->buffer, dst->heap);
#endif
#ifdef WOLFSSL_HASH_FLAGS
dst->flags |= WC_HASH_FLAG_ISCOPY;
Expand Down
40 changes: 31 additions & 9 deletions wolfcrypt/src/port/pic32/pic32mz-crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,35 @@ int wc_Pic32Hash(const byte* in, int inLen, word32* out, int outLen, int algo)
NULL, 0, NULL, 0);
}

int wc_Pic32HashCopy(hashUpdCache* src, hashUpdCache* dst)
/* Give a hash context that was shallow copied from src its own copy of the
* cached message. The caller has already copied the whole hash struct, so
* dst->buf still points at src's storage: either src's embedded block buffer
* (srcStdBuf), whose contents the struct copy already placed in dstStdBuf,
* or a heap buffer that has to be duplicated here. Sharing the buffer would
* let an update or final on one context corrupt the other's message. */
int wc_Pic32HashCopy(hashUpdCache* src, hashUpdCache* dst,
const byte* srcStdBuf, byte* dstStdBuf, void* heap)
{
/* mark destination as copy, so cache->buf is not free'd */
if (dst) {
dst->isCopy = 1;
if (src == NULL || dst == NULL)
return BAD_FUNC_ARG;

if (src->buf == NULL) {
dst->buf = NULL;
}
else if (src->buf == srcStdBuf) {
dst->buf = dstStdBuf;
}
else {
dst->buf = (byte*)XMALLOC(src->bufLen, heap, DYNAMIC_TYPE_HASH_TMP);
if (dst->buf == NULL) {
dst->updLen = dst->bufLen = 0;
return MEMORY_E;
}
XMEMCPY(dst->buf, src->buf, src->updLen);
}
dst->updLen = src->updLen;
dst->bufLen = src->bufLen;

return 0;
}

Expand Down Expand Up @@ -548,15 +571,14 @@ static int wc_Pic32HashUpdate(hashUpdCache* cache, byte* stdBuf, int stdBufLen,
/* alloc buffer */
newBuf = (byte*)XMALLOC(newLenPad, heap, DYNAMIC_TYPE_HASH_TMP);
if (newBuf == NULL) {
if (cache->buf != stdBuf && !cache->isCopy) {
if (cache->buf != stdBuf) {
XFREE(cache->buf, heap, DYNAMIC_TYPE_HASH_TMP);
cache->buf = NULL;
cache->updLen = cache->bufLen = 0;
}
return MEMORY_E;
}
isNewBuf = 1;
cache->isCopy = 0; /* no longer using copy buffer */
}
else {
/* use existing buffer */
Expand Down Expand Up @@ -594,7 +616,7 @@ static int wc_Pic32HashFinal(hashUpdCache* cache, byte* stdBuf,
if (cache->bufLen == cache->finalLen) {
start_engine();
if (wait_engine(digest, (word32)digestSz) != 0) {
if (cache->buf && cache->buf != stdBuf && !cache->isCopy) {
if (cache->buf && cache->buf != stdBuf) {
XFREE(cache->buf, heap, DYNAMIC_TYPE_HASH_TMP);
cache->buf = NULL;
}
Expand Down Expand Up @@ -645,7 +667,7 @@ static int wc_Pic32HashFinal(hashUpdCache* cache, byte* stdBuf,
}
}

if (cache->buf && cache->buf != stdBuf && !cache->isCopy) {
if (cache->buf && cache->buf != stdBuf) {
XFREE(cache->buf, heap, DYNAMIC_TYPE_HASH_TMP);
cache->buf = NULL;
}
Expand All @@ -659,7 +681,7 @@ static int wc_Pic32HashFinal(hashUpdCache* cache, byte* stdBuf,

static void wc_Pic32HashFree(hashUpdCache* cache, void* stdBuf, void* heap)
{
if (cache && cache->buf && cache->buf != stdBuf && !cache->isCopy) {
if (cache && cache->buf && cache->buf != stdBuf) {
XFREE(cache->buf, heap, DYNAMIC_TYPE_HASH_TMP);
cache->buf = NULL;
}
Expand Down
3 changes: 2 additions & 1 deletion wolfcrypt/src/sha.c
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,8 @@ int wc_ShaCopy(wc_Sha* src, wc_Sha* dst)
#endif

#ifdef WOLFSSL_PIC32MZ_HASH
ret = wc_Pic32HashCopy(&src->cache, &dst->cache);
ret = wc_Pic32HashCopy(&src->cache, &dst->cache, (byte*)src->buffer,
(byte*)dst->buffer, dst->heap);
#endif

#if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
Expand Down
3 changes: 2 additions & 1 deletion wolfcrypt/src/sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -3438,7 +3438,8 @@ int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
#endif

#ifdef WOLFSSL_PIC32MZ_HASH
ret = wc_Pic32HashCopy(&src->cache, &dst->cache);
ret = wc_Pic32HashCopy(&src->cache, &dst->cache, (byte*)src->buffer,
(byte*)dst->buffer, dst->heap);
#endif

#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
Expand Down
Loading
Loading