-
Notifications
You must be signed in to change notification settings - Fork 921
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 all 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 | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -245,7 +245,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 +274,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; | ||||||||||||||||||||||||||||||||
|
Comment on lines
+392
to
+397
|
||||||||||||||||||||||||||||||||
| /* 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.
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.
Minor: comment grammar/spelling — "dont" should be "don't", and the sentence can be simplified for clarity (e.g., avoid "by accident").
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.
Why remove the null check on hash before accessing hash->msg?
| 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. | |
| */ |
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_COPY handler does a raw XMEMCPY of the entire hash object and then only deep-copies the mxcCtx buffer. When WOLF_CRYPTO_CB_COPY is enabled, the generic wc_Sha224Copy/wc_Sha256Copy functions will return early after this callback and will skip their usual deep-copy steps (e.g., allocating a new dst->W under WOLFSSL_SMALL_STACK_CACHE and deep-copying msg under WOLFSSL_HASH_KEEP in wolfcrypt/src/sha256.c:2605-2612, 2636-2644, 2762-2769, 2800-2807). This can leave dst sharing pointers with src, leading to double-free/use-after-free when both contexts are freed. Suggest either (a) have the callback return CRYPTOCB_UNAVAILABLE after handling only MAX3266X-specific state so the normal copy path runs, plus add missing MAX3266X_SHA_CB mxcCtx copy into wc_Sha224Copy, or (b) update the callback to fully mirror the standard copy semantics for each supported hash type (W, msg, async ctx, etc.).