Skip to content

Commit 4ef0492

Browse files
committed
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.
1 parent 350706d commit 4ef0492

3 files changed

Lines changed: 186 additions & 15 deletions

File tree

wolfcrypt/src/port/maxim/max3266x.c

Lines changed: 178 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <stdint.h>
3131
#include <stdarg.h>
32+
#include <stdio.h>
3233

3334
#include <wolfssl/wolfcrypt/wolfmath.h>
3435
#include <wolfssl/wolfcrypt/error-crypt.h>
@@ -245,7 +246,16 @@ int wc_MxcShaCryptoCb(wc_CryptoInfo* info)
245246
int wc_MxcCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
246247
{
247248
int ret;
249+
#ifdef MAX3266X_SHA_CB
250+
int savedDevId;
251+
wc_MXC_Sha *srcMxcCtx;
252+
wc_MXC_Sha *dstMxcCtx;
253+
int *srcDevId;
254+
int *dstDevId;
255+
word32 copySize;
256+
#endif
248257
(void)ctx;
258+
(void)devIdArg;
249259

250260
if (info == NULL) {
251261
return BAD_FUNC_ARG;
@@ -265,6 +275,132 @@ int wc_MxcCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
265275
MAX3266X_MSG("Using MXC SHA HW Callback:");
266276
ret = wc_MxcShaCryptoCb(info); /* Determine SHA HW or SW */
267277
break;
278+
case WC_ALGO_TYPE_COPY:
279+
MAX3266X_MSG("Using MXC Copy Callback:");
280+
if (info->copy.algo == WC_ALGO_TYPE_HASH) {
281+
srcMxcCtx = NULL;
282+
dstMxcCtx = NULL;
283+
srcDevId = NULL;
284+
dstDevId = NULL;
285+
copySize = 0;
286+
/* Get pointers and size based on hash type */
287+
switch (info->copy.type) {
288+
#ifndef NO_SHA
289+
case WC_HASH_TYPE_SHA:
290+
srcMxcCtx = &((wc_Sha*)info->copy.src)->mxcCtx;
291+
dstMxcCtx = &((wc_Sha*)info->copy.dst)->mxcCtx;
292+
srcDevId = &((wc_Sha*)info->copy.src)->devId;
293+
dstDevId = &((wc_Sha*)info->copy.dst)->devId;
294+
copySize = sizeof(wc_Sha);
295+
break;
296+
#endif
297+
#ifdef WOLFSSL_SHA224
298+
case WC_HASH_TYPE_SHA224:
299+
srcMxcCtx = &((wc_Sha224*)info->copy.src)->mxcCtx;
300+
dstMxcCtx = &((wc_Sha224*)info->copy.dst)->mxcCtx;
301+
srcDevId = &((wc_Sha224*)info->copy.src)->devId;
302+
dstDevId = &((wc_Sha224*)info->copy.dst)->devId;
303+
copySize = sizeof(wc_Sha224);
304+
break;
305+
#endif
306+
#ifndef NO_SHA256
307+
case WC_HASH_TYPE_SHA256:
308+
srcMxcCtx = &((wc_Sha256*)info->copy.src)->mxcCtx;
309+
dstMxcCtx = &((wc_Sha256*)info->copy.dst)->mxcCtx;
310+
srcDevId = &((wc_Sha256*)info->copy.src)->devId;
311+
dstDevId = &((wc_Sha256*)info->copy.dst)->devId;
312+
copySize = sizeof(wc_Sha256);
313+
break;
314+
#endif
315+
#ifdef WOLFSSL_SHA384
316+
case WC_HASH_TYPE_SHA384:
317+
srcMxcCtx = &((wc_Sha384*)info->copy.src)->mxcCtx;
318+
dstMxcCtx = &((wc_Sha384*)info->copy.dst)->mxcCtx;
319+
srcDevId = &((wc_Sha384*)info->copy.src)->devId;
320+
dstDevId = &((wc_Sha384*)info->copy.dst)->devId;
321+
copySize = sizeof(wc_Sha384);
322+
break;
323+
#endif
324+
#ifdef WOLFSSL_SHA512
325+
case WC_HASH_TYPE_SHA512:
326+
srcMxcCtx = &((wc_Sha512*)info->copy.src)->mxcCtx;
327+
dstMxcCtx = &((wc_Sha512*)info->copy.dst)->mxcCtx;
328+
srcDevId = &((wc_Sha512*)info->copy.src)->devId;
329+
dstDevId = &((wc_Sha512*)info->copy.dst)->devId;
330+
copySize = sizeof(wc_Sha512);
331+
break;
332+
#endif
333+
default:
334+
return WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
335+
}
336+
/* Software copy */
337+
savedDevId = *srcDevId;
338+
XMEMCPY(info->copy.dst, info->copy.src, copySize);
339+
*dstDevId = savedDevId;
340+
/* Hardware copy - handles shallow copy from XMEMCPY */
341+
ret = wc_MXC_TPU_SHA_Copy(srcMxcCtx, dstMxcCtx);
342+
}
343+
else {
344+
ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
345+
}
346+
break;
347+
case WC_ALGO_TYPE_FREE:
348+
MAX3266X_MSG("Using MXC Free Callback:");
349+
if (info->free.algo == WC_ALGO_TYPE_HASH) {
350+
dstMxcCtx = NULL;
351+
dstDevId = NULL;
352+
copySize = 0;
353+
/* Get pointers and size based on hash type */
354+
switch (info->free.type) {
355+
#ifndef NO_SHA
356+
case WC_HASH_TYPE_SHA:
357+
dstMxcCtx = &((wc_Sha*)info->free.obj)->mxcCtx;
358+
dstDevId = &((wc_Sha*)info->free.obj)->devId;
359+
copySize = sizeof(wc_Sha);
360+
break;
361+
#endif
362+
#ifdef WOLFSSL_SHA224
363+
case WC_HASH_TYPE_SHA224:
364+
dstMxcCtx = &((wc_Sha224*)info->free.obj)->mxcCtx;
365+
dstDevId = &((wc_Sha224*)info->free.obj)->devId;
366+
copySize = sizeof(wc_Sha224);
367+
break;
368+
#endif
369+
#ifndef NO_SHA256
370+
case WC_HASH_TYPE_SHA256:
371+
dstMxcCtx = &((wc_Sha256*)info->free.obj)->mxcCtx;
372+
dstDevId = &((wc_Sha256*)info->free.obj)->devId;
373+
copySize = sizeof(wc_Sha256);
374+
break;
375+
#endif
376+
#ifdef WOLFSSL_SHA384
377+
case WC_HASH_TYPE_SHA384:
378+
dstMxcCtx = &((wc_Sha384*)info->free.obj)->mxcCtx;
379+
dstDevId = &((wc_Sha384*)info->free.obj)->devId;
380+
copySize = sizeof(wc_Sha384);
381+
break;
382+
#endif
383+
#ifdef WOLFSSL_SHA512
384+
case WC_HASH_TYPE_SHA512:
385+
dstMxcCtx = &((wc_Sha512*)info->free.obj)->mxcCtx;
386+
dstDevId = &((wc_Sha512*)info->free.obj)->devId;
387+
copySize = sizeof(wc_Sha512);
388+
break;
389+
#endif
390+
default:
391+
return WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
392+
}
393+
/* Hardware free */
394+
wc_MXC_TPU_SHA_Free(dstMxcCtx);
395+
/* Software free */
396+
*dstDevId = INVALID_DEVID;
397+
ForceZero(info->free.obj, copySize);
398+
ret = 0;
399+
}
400+
else {
401+
ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
402+
}
403+
break;
268404
#endif /* MAX3266X_SHA_CB */
269405
default:
270406
MAX3266X_MSG("Callback not support with MXC, using SW");
@@ -708,31 +844,58 @@ int wc_MXC_TPU_SHA_Copy(wc_MXC_Sha* src, wc_MXC_Sha* dst)
708844
if (src == NULL || dst == NULL) {
709845
return BAD_FUNC_ARG;
710846
}
711-
dst->used = src->used;
712-
dst->size = src->size;
713-
if (dst->msg == src->msg && src->msg != 0) {
714-
/* Allocate new memory for dst->msg if it points to the same location */
715-
/* as src->msg */
716-
dst->msg = (unsigned char*)XMALLOC(src->size, NULL,
717-
DYNAMIC_TYPE_TMP_BUFFER);
718-
if (dst->msg == NULL) {
719-
return MEMORY_E; /* Handle memory allocation failure */
847+
848+
/* Handle case where src has no data */
849+
if (src->msg == NULL || src->size == 0) {
850+
/* Free dst if it has different data, then zero it */
851+
if (dst->msg != NULL && dst->msg != src->msg) {
852+
wc_MXC_TPU_SHA_Free(dst);
853+
}
854+
else {
855+
dst->msg = NULL;
856+
dst->used = 0;
857+
dst->size = 0;
720858
}
859+
return 0;
721860
}
722-
XMEMCPY(dst->msg, src->msg, src->size);
861+
862+
/* Only free dst if it points to different memory than src */
863+
if (dst->msg != NULL && dst->msg != src->msg) {
864+
wc_MXC_TPU_SHA_Free(dst);
865+
}
866+
else {
867+
/* Reset dst without freeing (would free src's buffer) */
868+
dst->msg = NULL;
869+
dst->used = 0;
870+
dst->size = 0;
871+
}
872+
873+
/* Allocate new buffer for dst */
874+
dst->msg = (unsigned char*)XMALLOC(src->size, NULL,
875+
DYNAMIC_TYPE_TMP_BUFFER);
876+
if (dst->msg == NULL) {
877+
return MEMORY_E;
878+
}
879+
880+
XMEMCPY(dst->msg, src->msg, src->used);
881+
dst->used = src->used;
882+
dst->size = src->size;
723883
return 0;
724884
}
725885

726886
/* Free the given struct's msg buffer and then reinitialize the struct to 0 */
727887
/* returns void to match other wc_Sha*Free api */
728888
void wc_MXC_TPU_SHA_Free(wc_MXC_Sha* hash)
729889
{
730-
if (hash == NULL) {
731-
return; /* Hash Struct is Null already, dont edit potentially */
732-
/* undefined memory */
890+
/* Securely zero the buffer before freeing */
891+
if (hash->msg != NULL) {
892+
ForceZero(hash->msg, hash->size);
893+
XFREE(hash->msg, NULL, DYNAMIC_TYPE_TMP_BUFFER);
733894
}
734-
XFREE(hash->msg, NULL, DYNAMIC_TYPE_TMP_BUFFER);
735-
wc_MXC_TPU_SHA_Init(hash); /* sets hash->msg to null + zero's attributes */
895+
/* Reset struct members to initial state */
896+
hash->msg = NULL;
897+
hash->used = 0;
898+
hash->size = 0;
736899
return;
737900
}
738901

wolfcrypt/src/sha256.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,6 +2138,12 @@ static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
21382138
sha224->devId = devId;
21392139
sha224->devCtx = NULL;
21402140
#endif
2141+
#ifdef MAX3266X_SHA_CB
2142+
ret = wc_MXC_TPU_SHA_Init(&(sha224->mxcCtx));
2143+
if (ret != 0) {
2144+
return ret;
2145+
}
2146+
#endif
21412147
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
21422148
#if defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224)
21432149
/* We know this is a fresh, uninitialized item, so set to INIT */

wolfssl/wolfcrypt/port/maxim/max3266x.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
/* Some extra conditions when using callbacks */
3434
#if defined(WOLF_CRYPTO_CB)
3535
#define MAX3266X_CB
36+
#define WOLF_CRYPTO_CB_COPY /* Enable copy callback for deep copy */
37+
#define WOLF_CRYPTO_CB_FREE /* Enable free callback for proper cleanup */
3638
#ifdef MAX3266X_MATH
3739
#error Cannot have MAX3266X_MATH and MAX3266X_CB
3840
#endif

0 commit comments

Comments
 (0)