-
Notifications
You must be signed in to change notification settings - Fork 925
Fix Crash when using Sha224 Callback with MAX32666 #9712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
d6cbbb9
23be39c
de36166
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -29,6 +29,7 @@ | |||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #include <stdint.h> | ||||||||||||||||||||||||||||||||||||||||||||
| #include <stdarg.h> | ||||||||||||||||||||||||||||||||||||||||||||
| #include <stdio.h> | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #include <wolfssl/wolfcrypt/wolfmath.h> | ||||||||||||||||||||||||||||||||||||||||||||
| #include <wolfssl/wolfcrypt/error-crypt.h> | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -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); | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+335
to
+340
|
||||||||||||||||||||||||||||||||||||||||||||
| /* 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); | |
| /* Hardware copy of MAX3266X-specific SHA context. | |
| * Return CRYPTOCB_UNAVAILABLE so core performs full | |
| * software deep-copy (W, msg, async ctx, etc.). */ | |
| (void)srcDevId; | |
| (void)dstDevId; | |
| (void)copySize; | |
| (void)savedDevId; | |
| (void)copySize; | |
| (void)savedDevId; | |
| (void)srcDevId; | |
| (void)dstDevId; | |
| (void)copySize; | |
| ret = wc_MXC_TPU_SHA_Copy(srcMxcCtx, dstMxcCtx); | |
| /* Signal to use standard copy path for software state. */ | |
| ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
Copilot
AI
Jan 27, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The WC_ALGO_TYPE_FREE handler frees only the MAX3266X mxcCtx and then ForceZero()s the whole hash object and returns success. With WOLF_CRYPTO_CB_FREE enabled, wc_Sha224Free/wc_Sha256Free will return immediately after a successful callback (see wolfcrypt/src/sha256.c:2330-2333 and 2407-2409), skipping their normal cleanup (e.g., freeing sha224/sha256->W under WOLFSSL_SMALL_STACK_CACHE, async ctx, HASH_KEEP msg, etc.). This causes leaks and can leave hardware/software state inconsistent. A safer pattern is to free only the MAX3266X-specific allocations (mxcCtx), set obj->devId to INVALID_DEVID if needed, and return CRYPTOCB_UNAVAILABLE so the standard Free path runs (mxcCtx free is idempotent if msg is NULL). Alternatively, the callback must explicitly duplicate the full standard free logic for each hash type.
| /* Hardware free */ | |
| wc_MXC_TPU_SHA_Free(dstMxcCtx); | |
| /* Software free */ | |
| *dstDevId = INVALID_DEVID; | |
| ForceZero(info->free.obj, copySize); | |
| ret = 0; | |
| /* Hardware free: release MAX3266X-specific SHA context. */ | |
| wc_MXC_TPU_SHA_Free(dstMxcCtx); | |
| /* Mark device as no longer using hardware so SW free can run. */ | |
| if (dstDevId != NULL) { | |
| *dstDevId = INVALID_DEVID; | |
| } | |
| /* Return unavailable so the standard software Free path runs and | |
| * performs any remaining cleanup (buffers, async state, etc.). */ | |
| ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment says "free then zero" but it looks like it conditionally frees here if not null and pointers are not the same, or it zeros if NULL or the pointers are the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, sorry, the comment is referring to the process that happens when we use the wc_MXC_TPU_SHA_Free call. Inside this call it will zero out the message buffer and free the message buffer point and then set the other values as 0 for size and used.
JacobBarthelmeh marked this conversation as resolved.
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 */ | ||||||||||||||||||
|
Comment on lines
+36
to
+37
|
||||||||||||||||||
| #define WOLF_CRYPTO_CB_COPY /* Enable copy callback for deep copy */ | |
| #define WOLF_CRYPTO_CB_FREE /* Enable free callback for proper cleanup */ | |
| /* | |
| * Do NOT define WOLF_CRYPTO_CB_COPY / WOLF_CRYPTO_CB_FREE here. | |
| * Those macros change generic hash Copy/Free behavior and require | |
| * callback implementations that fully mirror the normal deep-copy/free | |
| * semantics. They must only be enabled in code that provides that logic. | |
| */ |
Uh oh!
There was an error while loading. Please reload this page.