From eac96fba400b8f4ba533421ddd759374c68ed878 Mon Sep 17 00:00:00 2001 From: Zackery Backman Date: Thu, 22 Jan 2026 15:56:45 -0700 Subject: [PATCH 1/5] Improve logic behind copy and free for sha, add copy and free callback functions, fix sha224 crashing when using callbacks for MAX32666 due to unitialized struct. --- wolfcrypt/src/port/maxim/max3266x.c | 193 ++++++++++++++++++++++-- wolfcrypt/src/sha256.c | 6 + wolfssl/wolfcrypt/port/maxim/max3266x.h | 2 + 3 files changed, 186 insertions(+), 15 deletions(-) diff --git a/wolfcrypt/src/port/maxim/max3266x.c b/wolfcrypt/src/port/maxim/max3266x.c index 752613cf4ec..270a23caaab 100644 --- a/wolfcrypt/src/port/maxim/max3266x.c +++ b/wolfcrypt/src/port/maxim/max3266x.c @@ -29,6 +29,7 @@ #include #include +#include #include #include @@ -245,7 +246,16 @@ int wc_MxcShaCryptoCb(wc_CryptoInfo* info) int wc_MxcCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx) { int ret; +#ifdef MAX3266X_SHA_CB + int savedDevId; + wc_MXC_Sha *srcMxcCtx; + wc_MXC_Sha *dstMxcCtx; + int *srcDevId; + int *dstDevId; + word32 copySize; +#endif (void)ctx; + (void)devIdArg; if (info == NULL) { return BAD_FUNC_ARG; @@ -265,6 +275,132 @@ int wc_MxcCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx) MAX3266X_MSG("Using MXC SHA HW Callback:"); ret = wc_MxcShaCryptoCb(info); /* Determine SHA HW or SW */ break; + case WC_ALGO_TYPE_COPY: + MAX3266X_MSG("Using MXC Copy Callback:"); + if (info->copy.algo == WC_ALGO_TYPE_HASH) { + srcMxcCtx = NULL; + dstMxcCtx = NULL; + srcDevId = NULL; + dstDevId = NULL; + copySize = 0; + /* Get pointers and size based on hash type */ + switch (info->copy.type) { + #ifndef NO_SHA + case WC_HASH_TYPE_SHA: + srcMxcCtx = &((wc_Sha*)info->copy.src)->mxcCtx; + dstMxcCtx = &((wc_Sha*)info->copy.dst)->mxcCtx; + srcDevId = &((wc_Sha*)info->copy.src)->devId; + dstDevId = &((wc_Sha*)info->copy.dst)->devId; + copySize = sizeof(wc_Sha); + break; + #endif + #ifdef WOLFSSL_SHA224 + case WC_HASH_TYPE_SHA224: + srcMxcCtx = &((wc_Sha224*)info->copy.src)->mxcCtx; + dstMxcCtx = &((wc_Sha224*)info->copy.dst)->mxcCtx; + srcDevId = &((wc_Sha224*)info->copy.src)->devId; + dstDevId = &((wc_Sha224*)info->copy.dst)->devId; + copySize = sizeof(wc_Sha224); + break; + #endif + #ifndef NO_SHA256 + case WC_HASH_TYPE_SHA256: + srcMxcCtx = &((wc_Sha256*)info->copy.src)->mxcCtx; + dstMxcCtx = &((wc_Sha256*)info->copy.dst)->mxcCtx; + srcDevId = &((wc_Sha256*)info->copy.src)->devId; + dstDevId = &((wc_Sha256*)info->copy.dst)->devId; + copySize = sizeof(wc_Sha256); + break; + #endif + #ifdef WOLFSSL_SHA384 + case WC_HASH_TYPE_SHA384: + srcMxcCtx = &((wc_Sha384*)info->copy.src)->mxcCtx; + dstMxcCtx = &((wc_Sha384*)info->copy.dst)->mxcCtx; + srcDevId = &((wc_Sha384*)info->copy.src)->devId; + dstDevId = &((wc_Sha384*)info->copy.dst)->devId; + copySize = sizeof(wc_Sha384); + break; + #endif + #ifdef WOLFSSL_SHA512 + case WC_HASH_TYPE_SHA512: + srcMxcCtx = &((wc_Sha512*)info->copy.src)->mxcCtx; + dstMxcCtx = &((wc_Sha512*)info->copy.dst)->mxcCtx; + srcDevId = &((wc_Sha512*)info->copy.src)->devId; + dstDevId = &((wc_Sha512*)info->copy.dst)->devId; + copySize = sizeof(wc_Sha512); + break; + #endif + default: + return WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); + } + /* Software copy */ + savedDevId = *srcDevId; + XMEMCPY(info->copy.dst, info->copy.src, copySize); + *dstDevId = savedDevId; + /* Hardware copy - handles shallow copy from XMEMCPY */ + ret = wc_MXC_TPU_SHA_Copy(srcMxcCtx, dstMxcCtx); + } + else { + ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); + } + break; + case WC_ALGO_TYPE_FREE: + MAX3266X_MSG("Using MXC Free Callback:"); + if (info->free.algo == WC_ALGO_TYPE_HASH) { + dstMxcCtx = NULL; + dstDevId = NULL; + copySize = 0; + /* Get pointers and size based on hash type */ + switch (info->free.type) { + #ifndef NO_SHA + case WC_HASH_TYPE_SHA: + dstMxcCtx = &((wc_Sha*)info->free.obj)->mxcCtx; + dstDevId = &((wc_Sha*)info->free.obj)->devId; + copySize = sizeof(wc_Sha); + break; + #endif + #ifdef WOLFSSL_SHA224 + case WC_HASH_TYPE_SHA224: + dstMxcCtx = &((wc_Sha224*)info->free.obj)->mxcCtx; + dstDevId = &((wc_Sha224*)info->free.obj)->devId; + copySize = sizeof(wc_Sha224); + break; + #endif + #ifndef NO_SHA256 + case WC_HASH_TYPE_SHA256: + dstMxcCtx = &((wc_Sha256*)info->free.obj)->mxcCtx; + dstDevId = &((wc_Sha256*)info->free.obj)->devId; + copySize = sizeof(wc_Sha256); + break; + #endif + #ifdef WOLFSSL_SHA384 + case WC_HASH_TYPE_SHA384: + dstMxcCtx = &((wc_Sha384*)info->free.obj)->mxcCtx; + dstDevId = &((wc_Sha384*)info->free.obj)->devId; + copySize = sizeof(wc_Sha384); + break; + #endif + #ifdef WOLFSSL_SHA512 + case WC_HASH_TYPE_SHA512: + dstMxcCtx = &((wc_Sha512*)info->free.obj)->mxcCtx; + dstDevId = &((wc_Sha512*)info->free.obj)->devId; + copySize = sizeof(wc_Sha512); + break; + #endif + default: + return WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); + } + /* Hardware free */ + wc_MXC_TPU_SHA_Free(dstMxcCtx); + /* Software free */ + *dstDevId = INVALID_DEVID; + ForceZero(info->free.obj, copySize); + ret = 0; + } + else { + ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); + } + break; #endif /* MAX3266X_SHA_CB */ default: MAX3266X_MSG("Callback not support with MXC, using SW"); @@ -708,18 +844,42 @@ int wc_MXC_TPU_SHA_Copy(wc_MXC_Sha* src, wc_MXC_Sha* dst) if (src == NULL || dst == NULL) { return BAD_FUNC_ARG; } - dst->used = src->used; - dst->size = src->size; - if (dst->msg == src->msg && src->msg != 0) { - /* Allocate new memory for dst->msg if it points to the same location */ - /* as src->msg */ - dst->msg = (unsigned char*)XMALLOC(src->size, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (dst->msg == NULL) { - return MEMORY_E; /* Handle memory allocation failure */ + + /* Handle case where src has no data */ + if (src->msg == NULL || src->size == 0) { + /* Free dst if it has different data, then zero it */ + if (dst->msg != NULL && dst->msg != src->msg) { + wc_MXC_TPU_SHA_Free(dst); + } + else { + dst->msg = NULL; + dst->used = 0; + dst->size = 0; } + return 0; } - XMEMCPY(dst->msg, src->msg, src->size); + + /* Only free dst if it points to different memory than src */ + if (dst->msg != NULL && dst->msg != src->msg) { + wc_MXC_TPU_SHA_Free(dst); + } + else { + /* Reset dst without freeing (would free src's buffer) */ + dst->msg = NULL; + dst->used = 0; + dst->size = 0; + } + + /* Allocate new buffer for dst */ + dst->msg = (unsigned char*)XMALLOC(src->size, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (dst->msg == NULL) { + return MEMORY_E; + } + + XMEMCPY(dst->msg, src->msg, src->used); + dst->used = src->used; + dst->size = src->size; return 0; } @@ -727,12 +887,15 @@ int wc_MXC_TPU_SHA_Copy(wc_MXC_Sha* src, wc_MXC_Sha* dst) /* returns void to match other wc_Sha*Free api */ void wc_MXC_TPU_SHA_Free(wc_MXC_Sha* hash) { - if (hash == NULL) { - return; /* Hash Struct is Null already, dont edit potentially */ - /* undefined memory */ + /* Securely zero the buffer before freeing */ + if (hash->msg != NULL) { + ForceZero(hash->msg, hash->size); + XFREE(hash->msg, NULL, DYNAMIC_TYPE_TMP_BUFFER); } - XFREE(hash->msg, NULL, DYNAMIC_TYPE_TMP_BUFFER); - wc_MXC_TPU_SHA_Init(hash); /* sets hash->msg to null + zero's attributes */ + /* Reset struct members to initial state */ + hash->msg = NULL; + hash->used = 0; + hash->size = 0; return; } diff --git a/wolfcrypt/src/sha256.c b/wolfcrypt/src/sha256.c index 690af7564c1..40b21083d43 100644 --- a/wolfcrypt/src/sha256.c +++ b/wolfcrypt/src/sha256.c @@ -2138,6 +2138,12 @@ static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data, sha224->devId = devId; sha224->devCtx = NULL; #endif + #ifdef MAX3266X_SHA_CB + ret = wc_MXC_TPU_SHA_Init(&(sha224->mxcCtx)); + if (ret != 0) { + return ret; + } + #endif #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) #if defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224) /* We know this is a fresh, uninitialized item, so set to INIT */ diff --git a/wolfssl/wolfcrypt/port/maxim/max3266x.h b/wolfssl/wolfcrypt/port/maxim/max3266x.h index 969c328c3fc..86f361fc6ff 100644 --- a/wolfssl/wolfcrypt/port/maxim/max3266x.h +++ b/wolfssl/wolfcrypt/port/maxim/max3266x.h @@ -33,6 +33,8 @@ /* Some extra conditions when using callbacks */ #if defined(WOLF_CRYPTO_CB) #define MAX3266X_CB + #define WOLF_CRYPTO_CB_COPY /* Enable copy callback for deep copy */ + #define WOLF_CRYPTO_CB_FREE /* Enable free callback for proper cleanup */ #ifdef MAX3266X_MATH #error Cannot have MAX3266X_MATH and MAX3266X_CB #endif From ac1e43f331a6d7e1d17ae889ebb81265663061aa Mon Sep 17 00:00:00 2001 From: night1rider Date: Mon, 26 Jan 2026 17:50:12 -0700 Subject: [PATCH 2/5] Add setting callback and MXC init when using arm asm with callbacks --- wolfcrypt/src/sha256.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/wolfcrypt/src/sha256.c b/wolfcrypt/src/sha256.c index 40b21083d43..22fb946329b 100644 --- a/wolfcrypt/src/sha256.c +++ b/wolfcrypt/src/sha256.c @@ -1106,7 +1106,19 @@ int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId) return ret; sha256->heap = heap; +#ifdef WOLF_CRYPTO_CB + sha256->devId = devId; + sha256->devCtx = NULL; +#else (void)devId; +#endif + +#ifdef MAX3266X_SHA_CB + ret = wc_MXC_TPU_SHA_Init(&(sha256->mxcCtx)); + if (ret != 0) { + return ret; + } +#endif #ifdef WOLFSSL_SMALL_STACK_CACHE sha256->W = NULL; From ee3c785852e44cd0a79a23d9920c89efdd2e7ef4 Mon Sep 17 00:00:00 2001 From: Zackery Backman Date: Tue, 27 Jan 2026 15:20:21 -0700 Subject: [PATCH 3/5] Remove stdio inclusion and then revert removal of null check for MXC free --- wolfcrypt/src/port/maxim/max3266x.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/wolfcrypt/src/port/maxim/max3266x.c b/wolfcrypt/src/port/maxim/max3266x.c index 270a23caaab..988c34bd67a 100644 --- a/wolfcrypt/src/port/maxim/max3266x.c +++ b/wolfcrypt/src/port/maxim/max3266x.c @@ -29,7 +29,6 @@ #include #include -#include #include #include @@ -887,6 +886,10 @@ int wc_MXC_TPU_SHA_Copy(wc_MXC_Sha* src, wc_MXC_Sha* dst) /* returns void to match other wc_Sha*Free api */ void wc_MXC_TPU_SHA_Free(wc_MXC_Sha* hash) { + if (hash == NULL) { + return; /* Hash Struct is Null already, dont edit potentially */ + /* undefined memory by accident */ + } /* Securely zero the buffer before freeing */ if (hash->msg != NULL) { ForceZero(hash->msg, hash->size); From 6892bcb70981246f6d8ed7385d6ae3f50ed623f0 Mon Sep 17 00:00:00 2001 From: night1rider Date: Fri, 20 Feb 2026 16:31:03 -0700 Subject: [PATCH 4/5] Refactor to use HASH_KEEP option instead of dedicated context for SHA, also add HASH_KEEP to sha1 context with correct init/free calls --- wolfcrypt/src/port/maxim/max3266x.c | 718 +++++++----------- wolfcrypt/src/sha.c | 39 +- wolfcrypt/src/sha256.c | 27 - wolfcrypt/src/sha512.c | 31 +- .../wolfcrypt/port/maxim/max3266x-cryptocb.h | 6 - wolfssl/wolfcrypt/port/maxim/max3266x.h | 39 +- wolfssl/wolfcrypt/sha.h | 3 - wolfssl/wolfcrypt/sha256.h | 3 - wolfssl/wolfcrypt/sha512.h | 3 - 9 files changed, 334 insertions(+), 535 deletions(-) diff --git a/wolfcrypt/src/port/maxim/max3266x.c b/wolfcrypt/src/port/maxim/max3266x.c index 988c34bd67a..f4aaee2aada 100644 --- a/wolfcrypt/src/port/maxim/max3266x.c +++ b/wolfcrypt/src/port/maxim/max3266x.c @@ -34,7 +34,7 @@ #include #include #include - +#include #ifdef NO_INLINE #include #else @@ -136,123 +136,78 @@ int wc_MxcAesCryptoCb(wc_CryptoInfo* info) #ifdef MAX3266X_SHA_CB +/* Shared callback handler: Update grows buffer, Final computes hash. */ +static int wc_MxcShaCbDispatch(byte** msg, word32* used, word32* len, + void* heap, const byte* in, word32 inSz, + byte* digest, MXC_TPU_HASH_TYPE algo) +{ + if (in != NULL && digest == NULL) { + MAX3266X_MSG("Update CB"); + return _wc_Hash_Grow(msg, used, len, in, (int)inSz, heap); + } + if (in == NULL && digest != NULL) { + MAX3266X_MSG("Final CB"); + return wc_MXC_TPU_SHA_Final(msg, used, len, heap, digest, algo); + } + if (inSz == 0) { + return 0; /* Don't need to Update when Size is Zero */ + } + return BAD_FUNC_ARG; +} + int wc_MxcShaCryptoCb(wc_CryptoInfo* info) { switch (info->hash.type) { #ifndef NO_SHA case WC_HASH_TYPE_SHA: - MAX3266X_MSG("SHA-1 CB:"); - /* Update Case */ - if (info->hash.in != NULL && info->hash.digest == NULL) { - MAX3266X_MSG("Update CB"); - return wc_MXC_TPU_SHA_Update(&(info->hash.sha1->mxcCtx), - info->hash.in, info->hash.inSz); - } - /* Sha 1 Final Case */ - if (info->hash.in == NULL && info->hash.digest != NULL) { - MAX3266X_MSG("Final CB"); - return wc_MXC_TPU_SHA_Final(&(info->hash.sha1->mxcCtx), - info->hash.digest, - MXC_TPU_HASH_SHA1); - } - break; /* Break Out and Return Error */ + return wc_MxcShaCbDispatch(&info->hash.sha1->msg, + &info->hash.sha1->used, &info->hash.sha1->len, + info->hash.sha1->heap, info->hash.in, + info->hash.inSz, info->hash.digest, + MXC_TPU_HASH_SHA1); #endif #ifdef WOLFSSL_SHA224 case WC_HASH_TYPE_SHA224: - MAX3266X_MSG("SHA-224 CB:"); - /* Update Case */ - if (info->hash.in != NULL && info->hash.digest == NULL) { - MAX3266X_MSG("Update CB"); - return wc_MXC_TPU_SHA_Update(&(info->hash.sha224->mxcCtx), - info->hash.in, info->hash.inSz); - } - /* Sha 256 Final Case */ - if (info->hash.in == NULL && info->hash.digest != NULL) { - MAX3266X_MSG("Final CB"); - return wc_MXC_TPU_SHA_Final(&(info->hash.sha224->mxcCtx), - info->hash.digest, - MXC_TPU_HASH_SHA224); - } - break; /* Break Out and Return Error */ + return wc_MxcShaCbDispatch(&info->hash.sha224->msg, + &info->hash.sha224->used, &info->hash.sha224->len, + info->hash.sha224->heap, info->hash.in, + info->hash.inSz, info->hash.digest, + MXC_TPU_HASH_SHA224); #endif #ifndef NO_SHA256 case WC_HASH_TYPE_SHA256: - MAX3266X_MSG("SHA-256 CB:"); - /* Update Case */ - if (info->hash.in != NULL && info->hash.digest == NULL) { - MAX3266X_MSG("Update CB"); - return wc_MXC_TPU_SHA_Update(&(info->hash.sha256->mxcCtx), - info->hash.in, info->hash.inSz); - } - /* Sha 256 Final Case */ - if (info->hash.in == NULL && info->hash.digest != NULL) { - MAX3266X_MSG("Final CB"); - return wc_MXC_TPU_SHA_Final(&(info->hash.sha256->mxcCtx), - info->hash.digest, - MXC_TPU_HASH_SHA256); - } - break; /* Break Out and Return Error */ + return wc_MxcShaCbDispatch(&info->hash.sha256->msg, + &info->hash.sha256->used, &info->hash.sha256->len, + info->hash.sha256->heap, info->hash.in, + info->hash.inSz, info->hash.digest, + MXC_TPU_HASH_SHA256); #endif #ifdef WOLFSSL_SHA384 case WC_HASH_TYPE_SHA384: - MAX3266X_MSG("SHA-384 CB:"); - /* Update Case */ - if (info->hash.in != NULL && info->hash.digest == NULL) { - MAX3266X_MSG("Update CB"); - return wc_MXC_TPU_SHA_Update(&(info->hash.sha384->mxcCtx), - info->hash.in, info->hash.inSz); - } - /* Sha 384 Final Case */ - if (info->hash.in == NULL && info->hash.digest != NULL) { - MAX3266X_MSG("Final CB"); - return wc_MXC_TPU_SHA_Final(&(info->hash.sha384->mxcCtx), - info->hash.digest, - MXC_TPU_HASH_SHA384); - } - break; /* Break Out and Return Error */ + return wc_MxcShaCbDispatch(&info->hash.sha384->msg, + &info->hash.sha384->used, &info->hash.sha384->len, + info->hash.sha384->heap, info->hash.in, + info->hash.inSz, info->hash.digest, + MXC_TPU_HASH_SHA384); #endif #ifdef WOLFSSL_SHA512 case WC_HASH_TYPE_SHA512: - MAX3266X_MSG("SHA-512 CB:"); - /* Update Case */ - if (info->hash.in != NULL && info->hash.digest == NULL) { - MAX3266X_MSG("Update CB"); - return wc_MXC_TPU_SHA_Update(&(info->hash.sha512->mxcCtx), - info->hash.in, info->hash.inSz); - } - /* Sha 512 Final Case */ - if (info->hash.in == NULL && info->hash.digest != NULL) { - MAX3266X_MSG("Final CB"); - return wc_MXC_TPU_SHA_Final(&(info->hash.sha512->mxcCtx), - info->hash.digest, - MXC_TPU_HASH_SHA512); - } - break; /* Break Out and Return Error */ + return wc_MxcShaCbDispatch(&info->hash.sha512->msg, + &info->hash.sha512->used, &info->hash.sha512->len, + info->hash.sha512->heap, info->hash.in, + info->hash.inSz, info->hash.digest, + MXC_TPU_HASH_SHA512); #endif default: - /* Hash type not supported */ return WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); } - if (info->hash.inSz == 0) { - return 0; /* Dont need to Update when Size is Zero */ - } - return BAD_FUNC_ARG; } #endif /* MAX3266X_SHA_CB */ -/* Determines AES Type for Callback */ /* General Callback Function to determine ALGO Type */ int wc_MxcCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx) { int ret; -#ifdef MAX3266X_SHA_CB - int savedDevId; - wc_MXC_Sha *srcMxcCtx; - wc_MXC_Sha *dstMxcCtx; - int *srcDevId; - int *dstDevId; - word32 copySize; -#endif (void)ctx; (void)devIdArg; @@ -274,132 +229,6 @@ int wc_MxcCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx) MAX3266X_MSG("Using MXC SHA HW Callback:"); ret = wc_MxcShaCryptoCb(info); /* Determine SHA HW or SW */ break; - case WC_ALGO_TYPE_COPY: - MAX3266X_MSG("Using MXC Copy Callback:"); - if (info->copy.algo == WC_ALGO_TYPE_HASH) { - srcMxcCtx = NULL; - dstMxcCtx = NULL; - srcDevId = NULL; - dstDevId = NULL; - copySize = 0; - /* Get pointers and size based on hash type */ - switch (info->copy.type) { - #ifndef NO_SHA - case WC_HASH_TYPE_SHA: - srcMxcCtx = &((wc_Sha*)info->copy.src)->mxcCtx; - dstMxcCtx = &((wc_Sha*)info->copy.dst)->mxcCtx; - srcDevId = &((wc_Sha*)info->copy.src)->devId; - dstDevId = &((wc_Sha*)info->copy.dst)->devId; - copySize = sizeof(wc_Sha); - break; - #endif - #ifdef WOLFSSL_SHA224 - case WC_HASH_TYPE_SHA224: - srcMxcCtx = &((wc_Sha224*)info->copy.src)->mxcCtx; - dstMxcCtx = &((wc_Sha224*)info->copy.dst)->mxcCtx; - srcDevId = &((wc_Sha224*)info->copy.src)->devId; - dstDevId = &((wc_Sha224*)info->copy.dst)->devId; - copySize = sizeof(wc_Sha224); - break; - #endif - #ifndef NO_SHA256 - case WC_HASH_TYPE_SHA256: - srcMxcCtx = &((wc_Sha256*)info->copy.src)->mxcCtx; - dstMxcCtx = &((wc_Sha256*)info->copy.dst)->mxcCtx; - srcDevId = &((wc_Sha256*)info->copy.src)->devId; - dstDevId = &((wc_Sha256*)info->copy.dst)->devId; - copySize = sizeof(wc_Sha256); - break; - #endif - #ifdef WOLFSSL_SHA384 - case WC_HASH_TYPE_SHA384: - srcMxcCtx = &((wc_Sha384*)info->copy.src)->mxcCtx; - dstMxcCtx = &((wc_Sha384*)info->copy.dst)->mxcCtx; - srcDevId = &((wc_Sha384*)info->copy.src)->devId; - dstDevId = &((wc_Sha384*)info->copy.dst)->devId; - copySize = sizeof(wc_Sha384); - break; - #endif - #ifdef WOLFSSL_SHA512 - case WC_HASH_TYPE_SHA512: - srcMxcCtx = &((wc_Sha512*)info->copy.src)->mxcCtx; - dstMxcCtx = &((wc_Sha512*)info->copy.dst)->mxcCtx; - srcDevId = &((wc_Sha512*)info->copy.src)->devId; - dstDevId = &((wc_Sha512*)info->copy.dst)->devId; - copySize = sizeof(wc_Sha512); - break; - #endif - default: - return WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); - } - /* Software copy */ - savedDevId = *srcDevId; - XMEMCPY(info->copy.dst, info->copy.src, copySize); - *dstDevId = savedDevId; - /* Hardware copy - handles shallow copy from XMEMCPY */ - ret = wc_MXC_TPU_SHA_Copy(srcMxcCtx, dstMxcCtx); - } - else { - ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); - } - break; - case WC_ALGO_TYPE_FREE: - MAX3266X_MSG("Using MXC Free Callback:"); - if (info->free.algo == WC_ALGO_TYPE_HASH) { - dstMxcCtx = NULL; - dstDevId = NULL; - copySize = 0; - /* Get pointers and size based on hash type */ - switch (info->free.type) { - #ifndef NO_SHA - case WC_HASH_TYPE_SHA: - dstMxcCtx = &((wc_Sha*)info->free.obj)->mxcCtx; - dstDevId = &((wc_Sha*)info->free.obj)->devId; - copySize = sizeof(wc_Sha); - break; - #endif - #ifdef WOLFSSL_SHA224 - case WC_HASH_TYPE_SHA224: - dstMxcCtx = &((wc_Sha224*)info->free.obj)->mxcCtx; - dstDevId = &((wc_Sha224*)info->free.obj)->devId; - copySize = sizeof(wc_Sha224); - break; - #endif - #ifndef NO_SHA256 - case WC_HASH_TYPE_SHA256: - dstMxcCtx = &((wc_Sha256*)info->free.obj)->mxcCtx; - dstDevId = &((wc_Sha256*)info->free.obj)->devId; - copySize = sizeof(wc_Sha256); - break; - #endif - #ifdef WOLFSSL_SHA384 - case WC_HASH_TYPE_SHA384: - dstMxcCtx = &((wc_Sha384*)info->free.obj)->mxcCtx; - dstDevId = &((wc_Sha384*)info->free.obj)->devId; - copySize = sizeof(wc_Sha384); - break; - #endif - #ifdef WOLFSSL_SHA512 - case WC_HASH_TYPE_SHA512: - dstMxcCtx = &((wc_Sha512*)info->free.obj)->mxcCtx; - dstDevId = &((wc_Sha512*)info->free.obj)->devId; - copySize = sizeof(wc_Sha512); - break; - #endif - default: - return WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); - } - /* Hardware free */ - wc_MXC_TPU_SHA_Free(dstMxcCtx); - /* Software free */ - *dstDevId = INVALID_DEVID; - ForceZero(info->free.obj, copySize); - ret = 0; - } - else { - ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); - } - break; #endif /* MAX3266X_SHA_CB */ default: MAX3266X_MSG("Callback not support with MXC, using SW"); @@ -723,71 +552,60 @@ int wc_MxcCb_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz) #if defined(MAX3266X_SHA) || defined(MAX3266X_SHA_CB) -int wc_MXC_TPU_SHA_Init(wc_MXC_Sha *hash) -{ - if (hash == NULL) { - return BAD_FUNC_ARG; /* Appropriate error handling for null argument */ - } - hash->msg = NULL; - hash->used = 0; - hash->size = 0; - return 0; -} - -/* Used to update the msg. Currently the SDK only supports 1 shots, so the */ -/* hash->msg buffer needs to be updated and resized. hash->msg will keep the */ -/* unhashed msg and produce a digest when wc_MXC_TPU_SHA_Final or */ -/* wc_MXC_TPU_SHA_GetHash is called */ -int wc_MXC_TPU_SHA_Update(wc_MXC_Sha *hash, const unsigned char* data, - unsigned int size) +/* Check for empty message and provide pre-computed digest if so */ +/* Returns 1 if empty (digest filled), 0 if needs hardware processing */ +int wc_MXC_TPU_SHA_GetDigest(const unsigned char* msg, unsigned int msgSz, + unsigned char* digest, + MXC_TPU_HASH_TYPE algo) { - void *p; - /* Only update if size is not 0 */ - if (size == 0) { - return 0; - } - /* Check for NULL pointers After Size Check */ - if (hash == NULL || data == NULL) { + if (digest == NULL) { return BAD_FUNC_ARG; } - if (hash->size < hash->used+size) { - if (hash->msg == NULL) { - p = XMALLOC(hash->used+size, NULL, DYNAMIC_TYPE_TMP_BUFFER); - } - else { - #ifdef WOLFSSL_NO_REALLOC - p = XMALLOC(hash->used + size, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (p != NULL) { - XMEMCPY(p, hash->msg, hash->used); - XFREE(hash->msg, NULL, DYNAMIC_TYPE_TMP_BUFFER); - } - #else - p = XREALLOC(hash->msg, hash->used+size, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - #endif - } - if (p == NULL) { - return MEMORY_E; + if (msg == NULL && msgSz == 0) { + switch (algo) { + #ifndef NO_SHA + case MXC_TPU_HASH_SHA1: + XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA1, WC_SHA_DIGEST_SIZE); + break; + #endif /* NO_SHA */ + #ifdef WOLFSSL_SHA224 + case MXC_TPU_HASH_SHA224: + XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA224, WC_SHA224_DIGEST_SIZE); + break; + #endif /* WOLFSSL_SHA224 */ + #ifndef NO_SHA256 + case MXC_TPU_HASH_SHA256: + XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA256, WC_SHA256_DIGEST_SIZE); + break; + #endif /* NO_SHA256 */ + #ifdef WOLFSSL_SHA384 + case MXC_TPU_HASH_SHA384: + XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA384, WC_SHA384_DIGEST_SIZE); + break; + #endif /* WOLFSSL_SHA384 */ + #ifdef WOLFSSL_SHA512 + case MXC_TPU_HASH_SHA512: + XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA512, WC_SHA512_DIGEST_SIZE); + break; + #endif /* WOLFSSL_SHA512 */ + default: + return BAD_FUNC_ARG; } - hash->msg = p; - hash->size = hash->used+size; + return 1; /* True: empty digest provided */ } - XMEMCPY(hash->msg+hash->used, data, size); - hash->used += size; - if (hash->msg == NULL) { - return BAD_FUNC_ARG; - } - return 0; + return 0; /* False: needs hardware processing */ } -int wc_MXC_TPU_SHA_GetHash(wc_MXC_Sha *hash, unsigned char* digest, +/* Compute hash from accumulated message using TPU hardware */ +int wc_MXC_TPU_SHA_GetHash(const unsigned char* msg, unsigned int msgSz, + unsigned char* digest, MXC_TPU_HASH_TYPE algo) { int status; - if (hash == NULL || digest == NULL) { + if (digest == NULL || (msg == NULL && msgSz != 0)) { return BAD_FUNC_ARG; } - status = wc_MXC_TPU_SHA_GetDigest(hash, digest, algo); + status = wc_MXC_TPU_SHA_GetDigest(msg, msgSz, digest, algo); /* True Case that msg is an empty string */ if (status == 1) { /* Hardware cannot handle the case of an empty string */ @@ -802,7 +620,7 @@ int wc_MXC_TPU_SHA_GetHash(wc_MXC_Sha *hash, unsigned char* digest, } MXC_TPU_Init(MXC_SYS_PERIPH_CLOCK_TPU); MXC_TPU_Hash_Config(algo); - status = MXC_TPU_Hash_SHA((const char *)hash->msg, algo, hash->size, + status = MXC_TPU_Hash_SHA((const char *)msg, algo, msgSz, (char *)digest); MAX3266X_MSG("SHA HW Acceleration Used"); wolfSSL_HwHashMutexUnLock(); /* Release Mutex */ @@ -815,134 +633,37 @@ int wc_MXC_TPU_SHA_GetHash(wc_MXC_Sha *hash, unsigned char* digest, return status; } -/* Calls GetHash to determine the digest and then reinitialize the hash */ -/* struct */ -int wc_MXC_TPU_SHA_Final(wc_MXC_Sha *hash, unsigned char* digest, - MXC_TPU_HASH_TYPE algo) +/* Free HASH_KEEP message buffer and reset fields */ +void wc_MXC_TPU_SHA_Free(byte** msg, word32* used, word32* len, void* heap) { - int status; - if (hash == NULL || digest == NULL) { - return BAD_FUNC_ARG; + if (msg == NULL) { + return; } - status = wc_MXC_TPU_SHA_GetHash(hash, digest, algo); - /* Free hash->msg no matter result */ - XFREE(hash->msg, NULL, DYNAMIC_TYPE_TMP_BUFFER); - if (status != 0) { - return status; + if (*msg != NULL) { + XFREE(*msg, heap, DYNAMIC_TYPE_TMP_BUFFER); + *msg = NULL; } - status = wc_MXC_TPU_SHA_Init(hash); - if (status != 0) { - return status; + if (used != NULL) { + *used = 0; } - return status; -} - -/* Copies Struct values from SRC struct to DST struct */ -int wc_MXC_TPU_SHA_Copy(wc_MXC_Sha* src, wc_MXC_Sha* dst) -{ - if (src == NULL || dst == NULL) { - return BAD_FUNC_ARG; - } - - /* Handle case where src has no data */ - if (src->msg == NULL || src->size == 0) { - /* Free dst if it has different data, then zero it */ - if (dst->msg != NULL && dst->msg != src->msg) { - wc_MXC_TPU_SHA_Free(dst); - } - else { - dst->msg = NULL; - dst->used = 0; - dst->size = 0; - } - return 0; - } - - /* Only free dst if it points to different memory than src */ - if (dst->msg != NULL && dst->msg != src->msg) { - wc_MXC_TPU_SHA_Free(dst); - } - else { - /* Reset dst without freeing (would free src's buffer) */ - dst->msg = NULL; - dst->used = 0; - dst->size = 0; - } - - /* Allocate new buffer for dst */ - dst->msg = (unsigned char*)XMALLOC(src->size, NULL, - DYNAMIC_TYPE_TMP_BUFFER); - if (dst->msg == NULL) { - return MEMORY_E; - } - - XMEMCPY(dst->msg, src->msg, src->used); - dst->used = src->used; - dst->size = src->size; - return 0; -} - -/* Free the given struct's msg buffer and then reinitialize the struct to 0 */ -/* returns void to match other wc_Sha*Free api */ -void wc_MXC_TPU_SHA_Free(wc_MXC_Sha* hash) -{ - if (hash == NULL) { - return; /* Hash Struct is Null already, dont edit potentially */ - /* undefined memory by accident */ - } - /* Securely zero the buffer before freeing */ - if (hash->msg != NULL) { - ForceZero(hash->msg, hash->size); - XFREE(hash->msg, NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (len != NULL) { + *len = 0; } - /* Reset struct members to initial state */ - hash->msg = NULL; - hash->used = 0; - hash->size = 0; - return; } -/* Acts as a True/False if true it will provide the stored digest */ -/* for the edge case of an empty string */ -int wc_MXC_TPU_SHA_GetDigest(wc_MXC_Sha *hash, unsigned char* digest, - MXC_TPU_HASH_TYPE algo) +/* Compute hash, free message buffer, and reset HASH_KEEP fields */ +int wc_MXC_TPU_SHA_Final(unsigned char** msg, unsigned int* used, + unsigned int* len, void* heap, + unsigned char* digest, + MXC_TPU_HASH_TYPE algo) { - if (hash == NULL || digest == NULL) { + int status; + if (msg == NULL || used == NULL || len == NULL || digest == NULL) { return BAD_FUNC_ARG; } - if (hash->msg == 0 && hash->size == 0) { - switch (algo) { - #ifndef NO_SHA - case MXC_TPU_HASH_SHA1: - XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA1, WC_SHA_DIGEST_SIZE); - break; - #endif /* NO_SHA */ - #ifdef WOLFSSL_SHA224 - case MXC_TPU_HASH_SHA224: - XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA224, WC_SHA224_DIGEST_SIZE); - break; - #endif /* WOLFSSL_SHA224 */ - #ifndef NO_SHA256 - case MXC_TPU_HASH_SHA256: - XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA256, WC_SHA256_DIGEST_SIZE); - break; - #endif /* NO_SHA256 */ - #ifdef WOLFSSL_SHA384 - case MXC_TPU_HASH_SHA384: - XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA384, WC_SHA384_DIGEST_SIZE); - break; - #endif /* WOLFSSL_SHA384 */ - #ifdef WOLFSSL_SHA512 - case MXC_TPU_HASH_SHA512: - XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA512, WC_SHA512_DIGEST_SIZE); - break; - #endif /* WOLFSSL_SHA512 */ - default: - return BAD_FUNC_ARG; - } - return 1; /* True */ - } - return 0; /* False */ + status = wc_MXC_TPU_SHA_GetHash(*msg, *used, digest, algo); + wc_MXC_TPU_SHA_Free(msg, used, len, heap); + return status; } #ifndef MAX3266X_SHA_CB @@ -953,38 +674,63 @@ WOLFSSL_API int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId) if (sha == NULL) { return BAD_FUNC_ARG; } - (void)heap; (void)devId; - return wc_MXC_TPU_SHA_Init(&(sha->mxcCtx)); + XMEMSET(sha, 0, sizeof(*sha)); + sha->heap = heap; + return 0; } WOLFSSL_API int wc_ShaUpdate(wc_Sha* sha, const unsigned char* data, unsigned int len) { - return wc_MXC_TPU_SHA_Update(&(sha->mxcCtx), data, len); + if (sha == NULL || (data == NULL && len > 0)) { + return BAD_FUNC_ARG; + } + return _wc_Hash_Grow(&sha->msg, &sha->used, &sha->len, + data, (int)len, sha->heap); } WOLFSSL_API int wc_ShaFinal(wc_Sha* sha, unsigned char* hash) { - return wc_MXC_TPU_SHA_Final(&(sha->mxcCtx), hash, - MXC_TPU_HASH_SHA1); + if (sha == NULL || hash == NULL) { + return BAD_FUNC_ARG; + } + return wc_MXC_TPU_SHA_Final(&sha->msg, &sha->used, &sha->len, + sha->heap, hash, MXC_TPU_HASH_SHA1); } WOLFSSL_API int wc_ShaGetHash(wc_Sha* sha, unsigned char* hash) { - return wc_MXC_TPU_SHA_GetHash(&(sha->mxcCtx), hash, - MXC_TPU_HASH_SHA1); + if (sha == NULL || hash == NULL) { + return BAD_FUNC_ARG; + } + return wc_MXC_TPU_SHA_GetHash((const unsigned char*)sha->msg, + sha->used, hash, MXC_TPU_HASH_SHA1); } WOLFSSL_API int wc_ShaCopy(wc_Sha* src, wc_Sha* dst) { - return wc_MXC_TPU_SHA_Copy(&(src->mxcCtx), &(dst->mxcCtx)); + if (src == NULL || dst == NULL) { + return BAD_FUNC_ARG; + } + XMEMCPY(dst, src, sizeof(*src)); + if (src->msg != NULL) { + dst->msg = (byte*)XMALLOC(src->len, dst->heap, + DYNAMIC_TYPE_TMP_BUFFER); + if (dst->msg == NULL) { + return MEMORY_E; + } + XMEMCPY(dst->msg, src->msg, src->used); + } + return 0; } WOLFSSL_API void wc_ShaFree(wc_Sha* sha) { - wc_MXC_TPU_SHA_Free(&(sha->mxcCtx)); - return; + if (sha == NULL) { + return; + } + wc_MXC_TPU_SHA_Free(&sha->msg, &sha->used, &sha->len, sha->heap); } #endif /* NO_SHA */ @@ -996,9 +742,10 @@ WOLFSSL_API int wc_InitSha224_ex(wc_Sha224* sha224, void* heap, int devId) if (sha224 == NULL) { return BAD_FUNC_ARG; } - (void)heap; (void)devId; - return wc_MXC_TPU_SHA_Init(&(sha224->mxcCtx)); + XMEMSET(sha224, 0, sizeof(*sha224)); + sha224->heap = heap; + return 0; } WOLFSSL_API int wc_InitSha224(wc_Sha224* sha224) @@ -1009,30 +756,57 @@ WOLFSSL_API int wc_InitSha224(wc_Sha224* sha224) WOLFSSL_API int wc_Sha224Update(wc_Sha224* sha224, const unsigned char* data, unsigned int len) { - return wc_MXC_TPU_SHA_Update(&(sha224->mxcCtx), data, len); + if (sha224 == NULL || (data == NULL && len > 0)) { + return BAD_FUNC_ARG; + } + return _wc_Hash_Grow(&sha224->msg, &sha224->used, &sha224->len, + data, (int)len, sha224->heap); } WOLFSSL_API int wc_Sha224Final(wc_Sha224* sha224, unsigned char* hash) { - return wc_MXC_TPU_SHA_Final(&(sha224->mxcCtx), hash, + if (sha224 == NULL || hash == NULL) { + return BAD_FUNC_ARG; + } + return wc_MXC_TPU_SHA_Final(&sha224->msg, &sha224->used, &sha224->len, + sha224->heap, hash, MXC_TPU_HASH_SHA224); } WOLFSSL_API int wc_Sha224GetHash(wc_Sha224* sha224, unsigned char* hash) { - return wc_MXC_TPU_SHA_GetHash(&(sha224->mxcCtx), hash, + if (sha224 == NULL || hash == NULL) { + return BAD_FUNC_ARG; + } + return wc_MXC_TPU_SHA_GetHash((const unsigned char*)sha224->msg, + sha224->used, hash, MXC_TPU_HASH_SHA224); } WOLFSSL_API int wc_Sha224Copy(wc_Sha224* src, wc_Sha224* dst) { - return wc_MXC_TPU_SHA_Copy(&(src->mxcCtx), &(dst->mxcCtx)); + if (src == NULL || dst == NULL) { + return BAD_FUNC_ARG; + } + XMEMCPY(dst, src, sizeof(*src)); + if (src->msg != NULL) { + dst->msg = (byte*)XMALLOC(src->len, dst->heap, + DYNAMIC_TYPE_TMP_BUFFER); + if (dst->msg == NULL) { + return MEMORY_E; + } + XMEMCPY(dst->msg, src->msg, src->used); + } + return 0; } WOLFSSL_API void wc_Sha224Free(wc_Sha224* sha224) { - wc_MXC_TPU_SHA_Free(&(sha224->mxcCtx)); - return; + if (sha224 == NULL) { + return; + } + wc_MXC_TPU_SHA_Free(&sha224->msg, &sha224->used, &sha224->len, + sha224->heap); } #endif /* WOLFSSL_SHA224 */ @@ -1044,9 +818,10 @@ WOLFSSL_API int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId) if (sha256 == NULL) { return BAD_FUNC_ARG; } - (void)heap; (void)devId; - return wc_MXC_TPU_SHA_Init(&(sha256->mxcCtx)); + XMEMSET(sha256, 0, sizeof(*sha256)); + sha256->heap = heap; + return 0; } WOLFSSL_API int wc_InitSha256(wc_Sha256* sha256) @@ -1057,30 +832,57 @@ WOLFSSL_API int wc_InitSha256(wc_Sha256* sha256) WOLFSSL_API int wc_Sha256Update(wc_Sha256* sha256, const unsigned char* data, unsigned int len) { - return wc_MXC_TPU_SHA_Update(&(sha256->mxcCtx), data, len); + if (sha256 == NULL || (data == NULL && len > 0)) { + return BAD_FUNC_ARG; + } + return _wc_Hash_Grow(&sha256->msg, &sha256->used, &sha256->len, + data, (int)len, sha256->heap); } WOLFSSL_API int wc_Sha256Final(wc_Sha256* sha256, unsigned char* hash) { - return wc_MXC_TPU_SHA_Final(&(sha256->mxcCtx), hash, + if (sha256 == NULL || hash == NULL) { + return BAD_FUNC_ARG; + } + return wc_MXC_TPU_SHA_Final(&sha256->msg, &sha256->used, &sha256->len, + sha256->heap, hash, MXC_TPU_HASH_SHA256); } WOLFSSL_API int wc_Sha256GetHash(wc_Sha256* sha256, unsigned char* hash) { - return wc_MXC_TPU_SHA_GetHash(&(sha256->mxcCtx), hash, + if (sha256 == NULL || hash == NULL) { + return BAD_FUNC_ARG; + } + return wc_MXC_TPU_SHA_GetHash((const unsigned char*)sha256->msg, + sha256->used, hash, MXC_TPU_HASH_SHA256); } WOLFSSL_API int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst) { - return wc_MXC_TPU_SHA_Copy(&(src->mxcCtx), &(dst->mxcCtx)); + if (src == NULL || dst == NULL) { + return BAD_FUNC_ARG; + } + XMEMCPY(dst, src, sizeof(*src)); + if (src->msg != NULL) { + dst->msg = (byte*)XMALLOC(src->len, dst->heap, + DYNAMIC_TYPE_TMP_BUFFER); + if (dst->msg == NULL) { + return MEMORY_E; + } + XMEMCPY(dst->msg, src->msg, src->used); + } + return 0; } WOLFSSL_API void wc_Sha256Free(wc_Sha256* sha256) { - wc_MXC_TPU_SHA_Free(&(sha256->mxcCtx)); - return; + if (sha256 == NULL) { + return; + } + wc_MXC_TPU_SHA_Free(&sha256->msg, &sha256->used, &sha256->len, + sha256->heap); } #endif /* NO_SHA256 */ @@ -1092,9 +894,10 @@ WOLFSSL_API int wc_InitSha384_ex(wc_Sha384* sha384, void* heap, int devId) if (sha384 == NULL) { return BAD_FUNC_ARG; } - (void)heap; (void)devId; - return wc_MXC_TPU_SHA_Init(&(sha384->mxcCtx)); + XMEMSET(sha384, 0, sizeof(*sha384)); + sha384->heap = heap; + return 0; } WOLFSSL_API int wc_InitSha384(wc_Sha384* sha384) @@ -1105,30 +908,57 @@ WOLFSSL_API int wc_InitSha384(wc_Sha384* sha384) WOLFSSL_API int wc_Sha384Update(wc_Sha384* sha384, const unsigned char* data, unsigned int len) { - return wc_MXC_TPU_SHA_Update(&(sha384->mxcCtx), data, len); + if (sha384 == NULL || (data == NULL && len > 0)) { + return BAD_FUNC_ARG; + } + return _wc_Hash_Grow(&sha384->msg, &sha384->used, &sha384->len, + data, (int)len, sha384->heap); } WOLFSSL_API int wc_Sha384Final(wc_Sha384* sha384, unsigned char* hash) { - return wc_MXC_TPU_SHA_Final(&(sha384->mxcCtx), hash, + if (sha384 == NULL || hash == NULL) { + return BAD_FUNC_ARG; + } + return wc_MXC_TPU_SHA_Final(&sha384->msg, &sha384->used, &sha384->len, + sha384->heap, hash, MXC_TPU_HASH_SHA384); } WOLFSSL_API int wc_Sha384GetHash(wc_Sha384* sha384, unsigned char* hash) { - return wc_MXC_TPU_SHA_GetHash(&(sha384->mxcCtx), hash, + if (sha384 == NULL || hash == NULL) { + return BAD_FUNC_ARG; + } + return wc_MXC_TPU_SHA_GetHash((const unsigned char*)sha384->msg, + sha384->used, hash, MXC_TPU_HASH_SHA384); } WOLFSSL_API int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst) { - return wc_MXC_TPU_SHA_Copy(&(src->mxcCtx), &(dst->mxcCtx)); + if (src == NULL || dst == NULL) { + return BAD_FUNC_ARG; + } + XMEMCPY(dst, src, sizeof(*src)); + if (src->msg != NULL) { + dst->msg = (byte*)XMALLOC(src->len, dst->heap, + DYNAMIC_TYPE_TMP_BUFFER); + if (dst->msg == NULL) { + return MEMORY_E; + } + XMEMCPY(dst->msg, src->msg, src->used); + } + return 0; } WOLFSSL_API void wc_Sha384Free(wc_Sha384* sha384) { - wc_MXC_TPU_SHA_Free(&(sha384->mxcCtx)); - return; + if (sha384 == NULL) { + return; + } + wc_MXC_TPU_SHA_Free(&sha384->msg, &sha384->used, &sha384->len, + sha384->heap); } #endif /* WOLFSSL_SHA384 */ @@ -1140,9 +970,10 @@ WOLFSSL_API int wc_InitSha512_ex(wc_Sha512* sha512, void* heap, int devId) if (sha512 == NULL) { return BAD_FUNC_ARG; } - (void)heap; (void)devId; - return wc_MXC_TPU_SHA_Init(&(sha512->mxcCtx)); + XMEMSET(sha512, 0, sizeof(*sha512)); + sha512->heap = heap; + return 0; } WOLFSSL_API int wc_InitSha512(wc_Sha512* sha512) @@ -1153,30 +984,57 @@ WOLFSSL_API int wc_InitSha512(wc_Sha512* sha512) WOLFSSL_API int wc_Sha512Update(wc_Sha512* sha512, const unsigned char* data, unsigned int len) { - return wc_MXC_TPU_SHA_Update(&(sha512->mxcCtx), data, len); + if (sha512 == NULL || (data == NULL && len > 0)) { + return BAD_FUNC_ARG; + } + return _wc_Hash_Grow(&sha512->msg, &sha512->used, &sha512->len, + data, (int)len, sha512->heap); } WOLFSSL_API int wc_Sha512Final(wc_Sha512* sha512, unsigned char* hash) { - return wc_MXC_TPU_SHA_Final(&(sha512->mxcCtx), hash, + if (sha512 == NULL || hash == NULL) { + return BAD_FUNC_ARG; + } + return wc_MXC_TPU_SHA_Final(&sha512->msg, &sha512->used, &sha512->len, + sha512->heap, hash, MXC_TPU_HASH_SHA512); } WOLFSSL_API int wc_Sha512GetHash(wc_Sha512* sha512, unsigned char* hash) { - return wc_MXC_TPU_SHA_GetHash(&(sha512->mxcCtx), hash, + if (sha512 == NULL || hash == NULL) { + return BAD_FUNC_ARG; + } + return wc_MXC_TPU_SHA_GetHash((const unsigned char*)sha512->msg, + sha512->used, hash, MXC_TPU_HASH_SHA512); } WOLFSSL_API int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst) { - return wc_MXC_TPU_SHA_Copy(&(src->mxcCtx), &(dst->mxcCtx)); + if (src == NULL || dst == NULL) { + return BAD_FUNC_ARG; + } + XMEMCPY(dst, src, sizeof(*src)); + if (src->msg != NULL) { + dst->msg = (byte*)XMALLOC(src->len, dst->heap, + DYNAMIC_TYPE_TMP_BUFFER); + if (dst->msg == NULL) { + return MEMORY_E; + } + XMEMCPY(dst->msg, src->msg, src->used); + } + return 0; } WOLFSSL_API void wc_Sha512Free(wc_Sha512* sha512) { - wc_MXC_TPU_SHA_Free(&(sha512->mxcCtx)); - return; + if (sha512 == NULL) { + return; + } + wc_MXC_TPU_SHA_Free(&sha512->msg, &sha512->used, &sha512->len, + sha512->heap); } #endif /* WOLFSSL_SHA512 */ diff --git a/wolfcrypt/src/sha.c b/wolfcrypt/src/sha.c index 700380951a3..18139daf76e 100644 --- a/wolfcrypt/src/sha.c +++ b/wolfcrypt/src/sha.c @@ -561,12 +561,10 @@ int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId) sha->devId = devId; sha->devCtx = NULL; #endif - -#ifdef MAX3266X_SHA_CB - ret = wc_MXC_TPU_SHA_Init(&(sha->mxcCtx)); - if (ret != 0) { - return ret; - } +#ifdef WOLFSSL_HASH_KEEP + sha->msg = NULL; + sha->len = 0; + sha->used = 0; #endif #ifdef WOLFSSL_USE_ESP32_CRYPT_HASH_HW @@ -1087,9 +1085,6 @@ void wc_ShaFree(wc_Sha* sha) #ifdef WOLFSSL_PIC32MZ_HASH wc_ShaPic32Free(sha); #endif -#ifdef MAX3266X_SHA_CB - wc_MXC_TPU_SHA_Free(&(sha->mxcCtx)); -#endif #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH) se050_hash_free(&sha->se050Ctx); #endif @@ -1105,6 +1100,14 @@ void wc_ShaFree(wc_Sha* sha) DCPShaFree(sha); #endif +#ifdef WOLFSSL_HASH_KEEP + if (sha->msg != NULL) { + ForceZero(sha->msg, sha->len); + XFREE(sha->msg, sha->heap, DYNAMIC_TYPE_TMP_BUFFER); + sha->msg = NULL; + } +#endif + #if defined(PSOC6_HASH_SHA1) wc_Psoc6_Sha_Free(); #endif @@ -1195,12 +1198,6 @@ int wc_ShaCopy(wc_Sha* src, wc_Sha* dst) esp_sha_ctx_copy(src, dst); #endif -#ifdef MAX3266X_SHA_CB - ret = wc_MXC_TPU_SHA_Copy(&(src->mxcCtx), &(dst->mxcCtx)); - if (ret != 0) { - return ret; - } -#endif #if defined(PSOC6_HASH_SHA1) wc_Psoc6_Sha1_Sha2_Init(dst, WC_PSOC6_SHA1, 0); @@ -1209,6 +1206,18 @@ int wc_ShaCopy(wc_Sha* src, wc_Sha* dst) #ifdef WOLFSSL_HASH_FLAGS dst->flags |= WC_HASH_FLAG_ISCOPY; #endif + +#if defined(WOLFSSL_HASH_KEEP) + if (src->msg != NULL) { + dst->msg = (byte*)XMALLOC(src->len, dst->heap, + DYNAMIC_TYPE_TMP_BUFFER); + if (dst->msg == NULL) { + return MEMORY_E; + } + XMEMCPY(dst->msg, src->msg, src->used); + } +#endif + return ret; } #endif /* WOLFSSL_RENESAS_RX64_HASH */ diff --git a/wolfcrypt/src/sha256.c b/wolfcrypt/src/sha256.c index 22fb946329b..355305010ff 100644 --- a/wolfcrypt/src/sha256.c +++ b/wolfcrypt/src/sha256.c @@ -1113,12 +1113,6 @@ int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId) (void)devId; #endif -#ifdef MAX3266X_SHA_CB - ret = wc_MXC_TPU_SHA_Init(&(sha256->mxcCtx)); - if (ret != 0) { - return ret; - } -#endif #ifdef WOLFSSL_SMALL_STACK_CACHE sha256->W = NULL; @@ -1171,12 +1165,6 @@ static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data, sha256->devId = devId; sha256->devCtx = NULL; #endif - #ifdef MAX3266X_SHA_CB - ret = wc_MXC_TPU_SHA_Init(&(sha256->mxcCtx)); - if (ret != 0) { - return ret; - } - #endif #ifdef WOLFSSL_SMALL_STACK_CACHE sha256->W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE, sha256->heap, DYNAMIC_TYPE_DIGEST); @@ -2150,12 +2138,6 @@ static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data, sha224->devId = devId; sha224->devCtx = NULL; #endif - #ifdef MAX3266X_SHA_CB - ret = wc_MXC_TPU_SHA_Init(&(sha224->mxcCtx)); - if (ret != 0) { - return ret; - } - #endif #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) #if defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224) /* We know this is a fresh, uninitialized item, so set to INIT */ @@ -2428,9 +2410,6 @@ void wc_Sha256Free(wc_Sha256* sha256) } #endif -#ifdef MAX3266X_SHA_CB - wc_MXC_TPU_SHA_Free(&(sha256->mxcCtx)); -#endif #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256) wolfAsync_DevCtxFree(&sha256->asyncDev, WOLFSSL_ASYNC_MARKER_SHA256); @@ -2752,12 +2731,6 @@ int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst) wc_MAXQ10XX_Sha256Copy(src); #endif -#ifdef MAX3266X_SHA_CB - ret = wc_MXC_TPU_SHA_Copy(&(src->mxcCtx), &(dst->mxcCtx)); - if (ret != 0) { - return ret; - } -#endif #ifdef WOLFSSL_SMALL_STACK_CACHE dst->W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE, diff --git a/wolfcrypt/src/sha512.c b/wolfcrypt/src/sha512.c index 6564b438d16..8a4fc8d5a18 100644 --- a/wolfcrypt/src/sha512.c +++ b/wolfcrypt/src/sha512.c @@ -887,6 +887,8 @@ static int InitSha512_Family(wc_Sha512* sha512, void* heap, int devId, #ifdef WOLFSSL_HASH_KEEP sha512->msg = NULL; + sha512->len = 0; + sha512->used = 0; #endif /* call the initialization function pointed to by initfp */ @@ -927,11 +929,6 @@ int wc_InitSha512_ex(wc_Sha512* sha512, void* heap, int devId) sha512->ctx.mode = ESP32_SHA_INIT; #endif -#ifdef MAX3266X_SHA_CB - if (wc_MXC_TPU_SHA_Init(&(sha512->mxcCtx)) != 0){ - return BAD_FUNC_ARG; - } -#endif return InitSha512_Family(sha512, heap, devId, InitSha512); } @@ -1676,9 +1673,6 @@ void wc_Sha512Free(wc_Sha512* sha512) } #endif -#ifdef MAX3266X_SHA_CB - wc_MXC_TPU_SHA_Free(&(sha512->mxcCtx)); -#endif #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512) wolfAsync_DevCtxFree(&sha512->asyncDev, WOLFSSL_ASYNC_MARKER_SHA512); @@ -2062,12 +2056,6 @@ int wc_InitSha384_ex(wc_Sha384* sha384, void* heap, int devId) sha384->ctx.mode = ESP32_SHA_INIT; #endif -#ifdef MAX3266X_SHA_CB - ret = wc_MXC_TPU_SHA_Init(&(sha384->mxcCtx)); - if (ret != 0) { - return ret; - } -#endif ret = InitSha384(sha384); if (ret != 0) { @@ -2172,9 +2160,6 @@ void wc_Sha384Free(wc_Sha384* sha384) } #endif -#ifdef MAX3266X_SHA_CB - wc_MXC_TPU_SHA_Free(&(sha384->mxcCtx)); -#endif ForceZero(sha384, sizeof(*sha384)); } @@ -2310,12 +2295,6 @@ int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst) } #endif -#ifdef MAX3266X_SHA_CB - ret = wc_MXC_TPU_SHA_Copy(&(src->mxcCtx), &(dst->mxcCtx)); - if (ret != 0) { - return ret; - } -#endif #if defined(PSOC6_HASH_SHA2) wc_Psoc6_Sha1_Sha2_Init(dst, WC_PSOC6_SHA512, 0); @@ -2750,12 +2729,6 @@ int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst) } #endif -#ifdef MAX3266X_SHA_CB - ret = wc_MXC_TPU_SHA_Copy(&(src->mxcCtx), &(dst->mxcCtx)); - if (ret != 0) { - return ret; - } -#endif #if defined(PSOC6_HASH_SHA2) wc_Psoc6_Sha1_Sha2_Init(dst, WC_PSOC6_SHA384, 0); diff --git a/wolfssl/wolfcrypt/port/maxim/max3266x-cryptocb.h b/wolfssl/wolfcrypt/port/maxim/max3266x-cryptocb.h index e30d94a75d2..8fe1a6949cf 100644 --- a/wolfssl/wolfcrypt/port/maxim/max3266x-cryptocb.h +++ b/wolfssl/wolfcrypt/port/maxim/max3266x-cryptocb.h @@ -59,12 +59,6 @@ #endif /* HAVE_AES_DECRYPT */ - WOLFSSL_LOCAL int wc_MXC_Sha256Update(wc_MXC_Sha* sha256, - const unsigned char* data, - unsigned int len); - WOLFSSL_LOCAL int wc_MXC_Sha256Final(wc_MXC_Sha* sha256, - unsigned char* hash); - #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/wolfssl/wolfcrypt/port/maxim/max3266x.h b/wolfssl/wolfcrypt/port/maxim/max3266x.h index 86f361fc6ff..ea7245ad442 100644 --- a/wolfssl/wolfcrypt/port/maxim/max3266x.h +++ b/wolfssl/wolfcrypt/port/maxim/max3266x.h @@ -33,8 +33,6 @@ /* Some extra conditions when using callbacks */ #if defined(WOLF_CRYPTO_CB) #define MAX3266X_CB - #define WOLF_CRYPTO_CB_COPY /* Enable copy callback for deep copy */ - #define WOLF_CRYPTO_CB_FREE /* Enable free callback for proper cleanup */ #ifdef MAX3266X_MATH #error Cannot have MAX3266X_MATH and MAX3266X_CB #endif @@ -238,14 +236,10 @@ #if defined(MAX3266X_SHA) || defined(MAX3266X_SHA_CB) - /* Need to update this struct accordingly if other SHA Structs change */ - /* This is a generic struct to use so only this is needed */ - - typedef struct { - unsigned char *msg; - unsigned int used; - unsigned int size; - } wc_MXC_Sha; + /* Use HASH_KEEP to accumulate message data for one-shot TPU hardware */ + #ifndef WOLFSSL_HASH_KEEP + #define WOLFSSL_HASH_KEEP + #endif #if !defined(NO_SHA) /* Define the SHA digest for an empty string */ @@ -313,19 +307,26 @@ #endif /* WOLFSSL_SHA512 */ - WOLFSSL_LOCAL int wc_MXC_TPU_SHA_Init(wc_MXC_Sha *hash); - WOLFSSL_LOCAL int wc_MXC_TPU_SHA_Update(wc_MXC_Sha *hash, - const unsigned char* data, - unsigned int size); - WOLFSSL_LOCAL int wc_MXC_TPU_SHA_Final(wc_MXC_Sha *hash, + /* Check for empty message and provide pre-computed digest if so */ + WOLFSSL_LOCAL int wc_MXC_TPU_SHA_GetDigest(const unsigned char* msg, + unsigned int msgSz, unsigned char* digest, MXC_TPU_HASH_TYPE algo); - WOLFSSL_LOCAL int wc_MXC_TPU_SHA_GetHash(wc_MXC_Sha *hash, + /* Compute hash from accumulated message using TPU hardware */ + WOLFSSL_LOCAL int wc_MXC_TPU_SHA_GetHash(const unsigned char* msg, + unsigned int msgSz, unsigned char* digest, MXC_TPU_HASH_TYPE algo); - WOLFSSL_LOCAL int wc_MXC_TPU_SHA_Copy(wc_MXC_Sha* src, wc_MXC_Sha* dst); - WOLFSSL_LOCAL void wc_MXC_TPU_SHA_Free(wc_MXC_Sha* hash); - WOLFSSL_LOCAL int wc_MXC_TPU_SHA_GetDigest(wc_MXC_Sha *hash, + /* Free HASH_KEEP message buffer and reset fields */ + WOLFSSL_LOCAL void wc_MXC_TPU_SHA_Free(unsigned char** msg, + unsigned int* used, + unsigned int* len, + void* heap); + /* Compute hash, free message buffer, and reset fields */ + WOLFSSL_LOCAL int wc_MXC_TPU_SHA_Final(unsigned char** msg, + unsigned int* used, + unsigned int* len, + void* heap, unsigned char* digest, MXC_TPU_HASH_TYPE algo); diff --git a/wolfssl/wolfcrypt/sha.h b/wolfssl/wolfcrypt/sha.h index e80a0d4a39e..7dd45001f92 100644 --- a/wolfssl/wolfcrypt/sha.h +++ b/wolfssl/wolfcrypt/sha.h @@ -175,9 +175,6 @@ struct wc_Sha { int devId; void* devCtx; /* generic crypto callback context */ #endif -#if defined(MAX3266X_SHA_CB) || defined(MAX3266X_SHA) - wc_MXC_Sha mxcCtx; -#endif #ifdef WOLFSSL_IMXRT1170_CAAM caam_hash_ctx_t ctx; caam_handle_t hndl; diff --git a/wolfssl/wolfcrypt/sha256.h b/wolfssl/wolfcrypt/sha256.h index b3b647901ac..2312fe701ef 100644 --- a/wolfssl/wolfcrypt/sha256.h +++ b/wolfssl/wolfcrypt/sha256.h @@ -215,9 +215,6 @@ struct wc_Sha256 { #ifdef WOLFSSL_DEVCRYPTO_HASH WC_CRYPTODEV ctx; #endif -#if defined(MAX3266X_SHA_CB) || defined(MAX3266X_SHA) - wc_MXC_Sha mxcCtx; -#endif #if defined(WOLFSSL_DEVCRYPTO_HASH) || defined(WOLFSSL_HASH_KEEP) byte* msg; word32 used; diff --git a/wolfssl/wolfcrypt/sha512.h b/wolfssl/wolfcrypt/sha512.h index 41945927a6a..b9d5adcc637 100644 --- a/wolfssl/wolfcrypt/sha512.h +++ b/wolfssl/wolfcrypt/sha512.h @@ -195,9 +195,6 @@ struct wc_Sha512 { int devId; void* devCtx; /* generic crypto callback context */ #endif -#if defined(MAX3266X_SHA_CB) || defined(MAX3266X_SHA) - wc_MXC_Sha mxcCtx; -#endif #ifdef WOLFSSL_HASH_FLAGS word32 flags; /* enum wc_HashFlags in hash.h */ #endif From f386691605e853fef2e9bd450269423735bdca7e Mon Sep 17 00:00:00 2001 From: night1rider Date: Tue, 24 Feb 2026 12:07:34 -0700 Subject: [PATCH 5/5] Add common SHA copy/free helpers with leak-safe msg buffer handling and copy/free crypto callbacks to replicate the non-callback code behavior when using MAX3266X_SHA_CB. --- wolfcrypt/src/port/maxim/max3266x.c | 263 +++++++++++++++++++----- wolfssl/wolfcrypt/port/maxim/max3266x.h | 26 ++- 2 files changed, 233 insertions(+), 56 deletions(-) diff --git a/wolfcrypt/src/port/maxim/max3266x.c b/wolfcrypt/src/port/maxim/max3266x.c index f4aaee2aada..c7c4cbc41ed 100644 --- a/wolfcrypt/src/port/maxim/max3266x.c +++ b/wolfcrypt/src/port/maxim/max3266x.c @@ -204,6 +204,133 @@ int wc_MxcShaCryptoCb(wc_CryptoInfo* info) } #endif /* MAX3266X_SHA_CB */ +#ifdef WOLF_CRYPTO_CB_COPY +static int wc_MxcCopyCb(wc_CryptoInfo* info) +{ + if (info == NULL || info->copy.src == NULL || info->copy.dst == NULL) { + return BAD_FUNC_ARG; + } + + switch (info->copy.type) { +#ifdef MAX3266X_SHA_CB + #ifndef NO_SHA + case WC_HASH_TYPE_SHA: + return wc_MXC_TPU_SHA_Copy(info->copy.src, info->copy.dst, + sizeof(wc_Sha), + &((wc_Sha*)info->copy.dst)->msg, + &((wc_Sha*)info->copy.dst)->used, + &((wc_Sha*)info->copy.dst)->len, + ((wc_Sha*)info->copy.dst)->heap, + ((wc_Sha*)info->copy.src)->heap); + #endif + #ifdef WOLFSSL_SHA224 + case WC_HASH_TYPE_SHA224: + return wc_MXC_TPU_SHA_Copy(info->copy.src, info->copy.dst, + sizeof(wc_Sha224), + &((wc_Sha224*)info->copy.dst)->msg, + &((wc_Sha224*)info->copy.dst)->used, + &((wc_Sha224*)info->copy.dst)->len, + ((wc_Sha224*)info->copy.dst)->heap, + ((wc_Sha224*)info->copy.src)->heap); + #endif + #ifndef NO_SHA256 + case WC_HASH_TYPE_SHA256: + return wc_MXC_TPU_SHA_Copy(info->copy.src, info->copy.dst, + sizeof(wc_Sha256), + &((wc_Sha256*)info->copy.dst)->msg, + &((wc_Sha256*)info->copy.dst)->used, + &((wc_Sha256*)info->copy.dst)->len, + ((wc_Sha256*)info->copy.dst)->heap, + ((wc_Sha256*)info->copy.src)->heap); + #endif + #ifdef WOLFSSL_SHA384 + case WC_HASH_TYPE_SHA384: + return wc_MXC_TPU_SHA_Copy(info->copy.src, info->copy.dst, + sizeof(wc_Sha384), + &((wc_Sha384*)info->copy.dst)->msg, + &((wc_Sha384*)info->copy.dst)->used, + &((wc_Sha384*)info->copy.dst)->len, + ((wc_Sha384*)info->copy.dst)->heap, + ((wc_Sha384*)info->copy.src)->heap); + #endif + #ifdef WOLFSSL_SHA512 + case WC_HASH_TYPE_SHA512: + return wc_MXC_TPU_SHA_Copy(info->copy.src, info->copy.dst, + sizeof(wc_Sha512), + &((wc_Sha512*)info->copy.dst)->msg, + &((wc_Sha512*)info->copy.dst)->used, + &((wc_Sha512*)info->copy.dst)->len, + ((wc_Sha512*)info->copy.dst)->heap, + ((wc_Sha512*)info->copy.src)->heap); + #endif +#endif /* MAX3266X_SHA_CB */ + default: + return WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); + } +} +#endif /* WOLF_CRYPTO_CB_COPY */ + +#ifdef WOLF_CRYPTO_CB_FREE +static int wc_MxcFreeCb(wc_CryptoInfo* info) +{ + if (info == NULL || info->free.obj == NULL) { + return BAD_FUNC_ARG; + } + + switch (info->free.type) { +#ifdef MAX3266X_SHA_CB + #ifndef NO_SHA + case WC_HASH_TYPE_SHA: + wc_MXC_TPU_SHA_FreeCtx(info->free.obj, sizeof(wc_Sha), + &((wc_Sha*)info->free.obj)->msg, + &((wc_Sha*)info->free.obj)->used, + &((wc_Sha*)info->free.obj)->len, + ((wc_Sha*)info->free.obj)->heap); + return 0; + #endif + #ifdef WOLFSSL_SHA224 + case WC_HASH_TYPE_SHA224: + wc_MXC_TPU_SHA_FreeCtx(info->free.obj, sizeof(wc_Sha224), + &((wc_Sha224*)info->free.obj)->msg, + &((wc_Sha224*)info->free.obj)->used, + &((wc_Sha224*)info->free.obj)->len, + ((wc_Sha224*)info->free.obj)->heap); + return 0; + #endif + #ifndef NO_SHA256 + case WC_HASH_TYPE_SHA256: + wc_MXC_TPU_SHA_FreeCtx(info->free.obj, sizeof(wc_Sha256), + &((wc_Sha256*)info->free.obj)->msg, + &((wc_Sha256*)info->free.obj)->used, + &((wc_Sha256*)info->free.obj)->len, + ((wc_Sha256*)info->free.obj)->heap); + return 0; + #endif + #ifdef WOLFSSL_SHA384 + case WC_HASH_TYPE_SHA384: + wc_MXC_TPU_SHA_FreeCtx(info->free.obj, sizeof(wc_Sha384), + &((wc_Sha384*)info->free.obj)->msg, + &((wc_Sha384*)info->free.obj)->used, + &((wc_Sha384*)info->free.obj)->len, + ((wc_Sha384*)info->free.obj)->heap); + return 0; + #endif + #ifdef WOLFSSL_SHA512 + case WC_HASH_TYPE_SHA512: + wc_MXC_TPU_SHA_FreeCtx(info->free.obj, sizeof(wc_Sha512), + &((wc_Sha512*)info->free.obj)->msg, + &((wc_Sha512*)info->free.obj)->used, + &((wc_Sha512*)info->free.obj)->len, + ((wc_Sha512*)info->free.obj)->heap); + return 0; + #endif +#endif /* MAX3266X_SHA_CB */ + default: + return WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); + } +} +#endif /* WOLF_CRYPTO_CB_FREE */ + /* General Callback Function to determine ALGO Type */ int wc_MxcCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx) { @@ -230,6 +357,18 @@ int wc_MxcCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx) ret = wc_MxcShaCryptoCb(info); /* Determine SHA HW or SW */ break; #endif /* MAX3266X_SHA_CB */ +#ifdef WOLF_CRYPTO_CB_COPY + case WC_ALGO_TYPE_COPY: + MAX3266X_MSG("Using MXC Copy Callback:"); + ret = wc_MxcCopyCb(info); + break; +#endif /* WOLF_CRYPTO_CB_COPY */ +#ifdef WOLF_CRYPTO_CB_FREE + case WC_ALGO_TYPE_FREE: + MAX3266X_MSG("Using MXC Free Callback:"); + ret = wc_MxcFreeCb(info); + break; +#endif /* WOLF_CRYPTO_CB_FREE */ default: MAX3266X_MSG("Callback not support with MXC, using SW"); /* return this to bypass HW and use SW */ @@ -651,6 +790,50 @@ void wc_MXC_TPU_SHA_Free(byte** msg, word32* used, word32* len, void* heap) } } +/* Free HASH_KEEP message buffer and zero the full SHA context struct */ +void wc_MXC_TPU_SHA_FreeCtx(void* ctx, word32 ctxSz, byte** msg, word32* used, + word32* len, void* heap) +{ + if (ctx == NULL) { + return; + } + wc_MXC_TPU_SHA_Free(msg, used, len, heap); + XMEMSET(ctx, 0, ctxSz); +} + +/* Copy SHA context struct and deep copy the HASH_KEEP message buffer. + * Frees any existing dst msg buffer to prevent memory leaks when copying + * into an already-used context. */ +int wc_MXC_TPU_SHA_Copy(void* src, void* dst, word32 ctxSz, + byte** dstMsg, word32* dstUsed, word32* dstLen, + void* dstHeap, void* srcHeap) +{ + byte* srcBuf; + + if (src == NULL || dst == NULL || dstMsg == NULL || + dstUsed == NULL || dstLen == NULL || ctxSz == 0) { + return BAD_FUNC_ARG; + } + + /* Free existing dst msg buffer using dst's original heap */ + wc_MXC_TPU_SHA_Free(dstMsg, dstUsed, dstLen, dstHeap); + + /* Shallow copy the full context struct */ + XMEMCPY(dst, src, ctxSz); + + /* Deep copy src msg buffer if present, allocate using src's heap */ + if (*dstMsg != NULL) { + srcBuf = *dstMsg; + *dstMsg = (byte*)XMALLOC(*dstLen, srcHeap, DYNAMIC_TYPE_TMP_BUFFER); + if (*dstMsg == NULL) { + return MEMORY_E; + } + XMEMCPY(*dstMsg, srcBuf, *dstUsed); + } + + return 0; +} + /* Compute hash, free message buffer, and reset HASH_KEEP fields */ int wc_MXC_TPU_SHA_Final(unsigned char** msg, unsigned int* used, unsigned int* len, void* heap, @@ -713,16 +896,9 @@ WOLFSSL_API int wc_ShaCopy(wc_Sha* src, wc_Sha* dst) if (src == NULL || dst == NULL) { return BAD_FUNC_ARG; } - XMEMCPY(dst, src, sizeof(*src)); - if (src->msg != NULL) { - dst->msg = (byte*)XMALLOC(src->len, dst->heap, - DYNAMIC_TYPE_TMP_BUFFER); - if (dst->msg == NULL) { - return MEMORY_E; - } - XMEMCPY(dst->msg, src->msg, src->used); - } - return 0; + return wc_MXC_TPU_SHA_Copy(src, dst, sizeof(wc_Sha), + &dst->msg, &dst->used, &dst->len, + dst->heap, src->heap); } WOLFSSL_API void wc_ShaFree(wc_Sha* sha) @@ -730,7 +906,8 @@ WOLFSSL_API void wc_ShaFree(wc_Sha* sha) if (sha == NULL) { return; } - wc_MXC_TPU_SHA_Free(&sha->msg, &sha->used, &sha->len, sha->heap); + wc_MXC_TPU_SHA_FreeCtx(sha, sizeof(wc_Sha), &sha->msg, &sha->used, + &sha->len, sha->heap); } #endif /* NO_SHA */ @@ -788,16 +965,9 @@ WOLFSSL_API int wc_Sha224Copy(wc_Sha224* src, wc_Sha224* dst) if (src == NULL || dst == NULL) { return BAD_FUNC_ARG; } - XMEMCPY(dst, src, sizeof(*src)); - if (src->msg != NULL) { - dst->msg = (byte*)XMALLOC(src->len, dst->heap, - DYNAMIC_TYPE_TMP_BUFFER); - if (dst->msg == NULL) { - return MEMORY_E; - } - XMEMCPY(dst->msg, src->msg, src->used); - } - return 0; + return wc_MXC_TPU_SHA_Copy(src, dst, sizeof(wc_Sha224), + &dst->msg, &dst->used, &dst->len, + dst->heap, src->heap); } WOLFSSL_API void wc_Sha224Free(wc_Sha224* sha224) @@ -805,7 +975,8 @@ WOLFSSL_API void wc_Sha224Free(wc_Sha224* sha224) if (sha224 == NULL) { return; } - wc_MXC_TPU_SHA_Free(&sha224->msg, &sha224->used, &sha224->len, + wc_MXC_TPU_SHA_FreeCtx(sha224, sizeof(wc_Sha224), &sha224->msg, + &sha224->used, &sha224->len, sha224->heap); } @@ -864,16 +1035,9 @@ WOLFSSL_API int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst) if (src == NULL || dst == NULL) { return BAD_FUNC_ARG; } - XMEMCPY(dst, src, sizeof(*src)); - if (src->msg != NULL) { - dst->msg = (byte*)XMALLOC(src->len, dst->heap, - DYNAMIC_TYPE_TMP_BUFFER); - if (dst->msg == NULL) { - return MEMORY_E; - } - XMEMCPY(dst->msg, src->msg, src->used); - } - return 0; + return wc_MXC_TPU_SHA_Copy(src, dst, sizeof(wc_Sha256), + &dst->msg, &dst->used, &dst->len, + dst->heap, src->heap); } WOLFSSL_API void wc_Sha256Free(wc_Sha256* sha256) @@ -881,7 +1045,8 @@ WOLFSSL_API void wc_Sha256Free(wc_Sha256* sha256) if (sha256 == NULL) { return; } - wc_MXC_TPU_SHA_Free(&sha256->msg, &sha256->used, &sha256->len, + wc_MXC_TPU_SHA_FreeCtx(sha256, sizeof(wc_Sha256), &sha256->msg, + &sha256->used, &sha256->len, sha256->heap); } @@ -940,16 +1105,9 @@ WOLFSSL_API int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst) if (src == NULL || dst == NULL) { return BAD_FUNC_ARG; } - XMEMCPY(dst, src, sizeof(*src)); - if (src->msg != NULL) { - dst->msg = (byte*)XMALLOC(src->len, dst->heap, - DYNAMIC_TYPE_TMP_BUFFER); - if (dst->msg == NULL) { - return MEMORY_E; - } - XMEMCPY(dst->msg, src->msg, src->used); - } - return 0; + return wc_MXC_TPU_SHA_Copy(src, dst, sizeof(wc_Sha384), + &dst->msg, &dst->used, &dst->len, + dst->heap, src->heap); } WOLFSSL_API void wc_Sha384Free(wc_Sha384* sha384) @@ -957,7 +1115,8 @@ WOLFSSL_API void wc_Sha384Free(wc_Sha384* sha384) if (sha384 == NULL) { return; } - wc_MXC_TPU_SHA_Free(&sha384->msg, &sha384->used, &sha384->len, + wc_MXC_TPU_SHA_FreeCtx(sha384, sizeof(wc_Sha384), &sha384->msg, + &sha384->used, &sha384->len, sha384->heap); } @@ -1016,16 +1175,9 @@ WOLFSSL_API int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst) if (src == NULL || dst == NULL) { return BAD_FUNC_ARG; } - XMEMCPY(dst, src, sizeof(*src)); - if (src->msg != NULL) { - dst->msg = (byte*)XMALLOC(src->len, dst->heap, - DYNAMIC_TYPE_TMP_BUFFER); - if (dst->msg == NULL) { - return MEMORY_E; - } - XMEMCPY(dst->msg, src->msg, src->used); - } - return 0; + return wc_MXC_TPU_SHA_Copy(src, dst, sizeof(wc_Sha512), + &dst->msg, &dst->used, &dst->len, + dst->heap, src->heap); } WOLFSSL_API void wc_Sha512Free(wc_Sha512* sha512) @@ -1033,7 +1185,8 @@ WOLFSSL_API void wc_Sha512Free(wc_Sha512* sha512) if (sha512 == NULL) { return; } - wc_MXC_TPU_SHA_Free(&sha512->msg, &sha512->used, &sha512->len, + wc_MXC_TPU_SHA_FreeCtx(sha512, sizeof(wc_Sha512), &sha512->msg, + &sha512->used, &sha512->len, sha512->heap); } diff --git a/wolfssl/wolfcrypt/port/maxim/max3266x.h b/wolfssl/wolfcrypt/port/maxim/max3266x.h index ea7245ad442..f59a0469690 100644 --- a/wolfssl/wolfcrypt/port/maxim/max3266x.h +++ b/wolfssl/wolfcrypt/port/maxim/max3266x.h @@ -57,6 +57,16 @@ #endif #endif +/* Enable copy/free callbacks when using callback mode */ +#if defined(MAX3266X_SHA_CB) + #ifndef WOLF_CRYPTO_CB_COPY + #define WOLF_CRYPTO_CB_COPY + #endif + #ifndef WOLF_CRYPTO_CB_FREE + #define WOLF_CRYPTO_CB_FREE + #endif +#endif + /* Crypto HW can be used in parallel on this device */ /* Sets up new Mutexing if desired */ #ifdef WOLFSSL_ALGO_HW_MUTEX @@ -322,6 +332,20 @@ unsigned int* used, unsigned int* len, void* heap); + /* Free HASH_KEEP message buffer and zero the full SHA context */ + WOLFSSL_LOCAL void wc_MXC_TPU_SHA_FreeCtx(void* ctx, + unsigned int ctxSz, + unsigned char** msg, + unsigned int* used, + unsigned int* len, + void* heap); + /* Copy SHA context and deep copy HASH_KEEP message buffer */ + WOLFSSL_LOCAL int wc_MXC_TPU_SHA_Copy(void* src, void* dst, + unsigned int ctxSz, + unsigned char** dstMsg, + unsigned int* dstUsed, + unsigned int* dstLen, + void* dstHeap, void* srcHeap); /* Compute hash, free message buffer, and reset fields */ WOLFSSL_LOCAL int wc_MXC_TPU_SHA_Final(unsigned char** msg, unsigned int* used, @@ -331,7 +355,7 @@ MXC_TPU_HASH_TYPE algo); -#endif /* defined(MAX3266X_SHA) && !defined(WOLF_CRYPTO_CB) */ +#endif /* defined(MAX3266X_SHA) || defined(MAX3266X_SHA_CB) */ #if defined(MAX3266X_MATH) #define WOLFSSL_USE_HW_MP