@@ -4379,6 +4379,10 @@ static WARN_UNUSED_RESULT int wc_AesDecrypt(
43794379
43804380 if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
43814381 if (ret != 0) {
4382+ /* Clear devCtx on error to prevent resource leak */
4383+ if (aes->devCtx != NULL) {
4384+ aes->devCtx = NULL;
4385+ }
43824386 return ret;
43834387 }
43844388
@@ -4388,7 +4392,12 @@ static WARN_UNUSED_RESULT int wc_AesDecrypt(
43884392
43894393 #ifdef WOLFSSL_AES_GCM_TLS_SAFE
43904394 WOLFSSL_MSG("CryptoCB AES-GCM declared but TLS-safe fallback retained");
4391- /* TLS-safe: continue to software path for fallback */
4395+ /* TLS-safe: copy key to devKey for fallback, then continue to software path */
4396+ if (keylen > sizeof(aes->devKey)) {
4397+ aes->devCtx = NULL; /* Clear devCtx on error */
4398+ return BAD_FUNC_ARG;
4399+ }
4400+ XMEMCPY(aes->devKey, userKey, keylen);
43924401 goto cryptocb_aes_setkey_fallback;
43934402 #else
43944403 /* Set IV if provided */
@@ -4842,6 +4851,10 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)
48424851
48434852 if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
48444853 if (ret != 0) {
4854+ /* Clear devCtx on error to prevent resource leak */
4855+ if (aes->devCtx != NULL) {
4856+ aes->devCtx = NULL;
4857+ }
48454858 return ret;
48464859 }
48474860
@@ -4851,8 +4864,13 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)
48514864
48524865 #ifdef WOLFSSL_AES_GCM_TLS_SAFE
48534866 WOLFSSL_MSG("CryptoCB AES-GCM declared but TLS-safe fallback retained");
4854- /* TLS-safe: continue to software path for fallback */
4855- goto cryptocb_aes_setkey_fallback2;
4867+ /* TLS-safe: copy key to devKey for fallback, then continue to software path */
4868+ if (keylen > sizeof(aes->devKey)) {
4869+ aes->devCtx = NULL; /* Clear devCtx on error */
4870+ return BAD_FUNC_ARG;
4871+ }
4872+ XMEMCPY(aes->devKey, userKey, keylen);
4873+ goto cryptocb_aes_setkey_fallback;
48564874 #else
48574875 WOLFSSL_MSG("CryptoCB AES-GCM full offload active (crypto-only build)");
48584876 /* Set IV if provided */
@@ -4868,7 +4886,7 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)
48684886 /* Key-import-only or partial support: fallback to software */
48694887 }
48704888 /* CRYPTOCB_UNAVAILABLE or TLS-safe fallback: continue to software */
4871- cryptocb_aes_setkey_fallback2 : (void)0;
4889+ cryptocb_aes_setkey_fallback : (void)0;
48724890 #else
48734891 /* Copy key to devKey for standard CryptoCB path */
48744892 XMEMCPY(aes->devKey, userKey, keylen);
0 commit comments