@@ -4361,24 +4361,26 @@ static WARN_UNUSED_RESULT int wc_AesDecrypt(
43614361 #ifdef WOLF_CRYPTO_CB
43624362 if (aes->devId != INVALID_DEVID) {
43634363 #ifdef WOLF_CRYPTO_CB_AES_SETKEY
4364- /* CryptoCB key import path */
4365- ret = wc_CryptoCb_AesSetKey(aes, userKey, keylen);
4366-
4367- if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
4368- /* Callback handled it (success or error) */
4369- if (ret == 0) {
4370- /* Store metadata only - NO raw key */
4371- aes->keylen = (int)keylen;
4372- /* Set IV if provided */
4373- if (iv != NULL) {
4374- XMEMCPY(aes->reg, iv, WC_AES_BLOCK_SIZE);
4375- } else {
4376- XMEMSET(aes->reg, 0, WC_AES_BLOCK_SIZE);
4364+ /* CryptoCB key import path - only if device exists and supports AES */
4365+ if (wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER) != NULL) {
4366+ ret = wc_CryptoCb_AesSetKey(aes, userKey, keylen);
4367+
4368+ if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
4369+ /* Callback handled it (success or error) */
4370+ if (ret == 0) {
4371+ /* Store metadata only - NO raw key */
4372+ aes->keylen = (int)keylen;
4373+ /* Set IV if provided */
4374+ if (iv != NULL) {
4375+ XMEMCPY(aes->reg, iv, WC_AES_BLOCK_SIZE);
4376+ } else {
4377+ XMEMSET(aes->reg, 0, WC_AES_BLOCK_SIZE);
4378+ }
43774379 }
4380+ return ret;
43784381 }
4379- return ret;
4382+ /* CRYPTOCB_UNAVAILABLE: fall through to software */
43804383 }
4381- /* CRYPTOCB_UNAVAILABLE: fall through to software */
43824384 #else
43834385 /* Standard CryptoCB path - copy key to devKey */
43844386 if (keylen > sizeof(aes->devKey)) {
@@ -4812,25 +4814,28 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)
48124814 if (aes->devId != INVALID_DEVID) {
48134815 #ifdef WOLF_CRYPTO_CB_AES_SETKEY
48144816 /* CryptoCB key import mode: attempt to import key to secure element.
4817+ * Only if device exists and supports AES.
48154818 * If the callback handles it, we're done.
48164819 * If CRYPTOCB_UNAVAILABLE, fall through to software path. */
4817- ret = wc_CryptoCb_AesSetKey(aes, userKey, keylen);
4818-
4819- if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
4820- /* Callback handled it (success or error) */
4821- if (ret == 0) {
4822- /* Store metadata only - NO raw key */
4823- aes->keylen = (int)keylen;
4824- /* Set IV if provided */
4825- if (iv != NULL) {
4826- XMEMCPY(aes->reg, iv, WC_AES_BLOCK_SIZE);
4827- } else {
4828- XMEMSET(aes->reg, 0, WC_AES_BLOCK_SIZE);
4820+ if (wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER) != NULL) {
4821+ ret = wc_CryptoCb_AesSetKey(aes, userKey, keylen);
4822+
4823+ if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
4824+ /* Callback handled it (success or error) */
4825+ if (ret == 0) {
4826+ /* Store metadata only - NO raw key */
4827+ aes->keylen = (int)keylen;
4828+ /* Set IV if provided */
4829+ if (iv != NULL) {
4830+ XMEMCPY(aes->reg, iv, WC_AES_BLOCK_SIZE);
4831+ } else {
4832+ XMEMSET(aes->reg, 0, WC_AES_BLOCK_SIZE);
4833+ }
48294834 }
4835+ return ret;
48304836 }
4831- return ret;
4837+ /* CRYPTOCB_UNAVAILABLE: fall through to software */
48324838 }
4833- /* CRYPTOCB_UNAVAILABLE: fall through to software */
48344839 #else
48354840 /* Copy key to devKey for standard CryptoCB path */
48364841 XMEMCPY(aes->devKey, userKey, keylen);
@@ -7511,7 +7516,8 @@ int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len)
75117516#ifdef WOLF_CRYPTO_CB_AES_SETKEY
75127517 /* In CryptoCB key import mode, skip H table generation.
75137518 * The secure element handles GCM internally. */
7514- if (aes->devId != INVALID_DEVID && aes->devCtx != NULL) {
7519+ if (aes->devId != INVALID_DEVID && aes->devCtx != NULL &&
7520+ (aes->devFlags & WC_AES_FLAG_GCM_OFFLOAD)) {
75157521 /* H table not needed - SE does GCM */
75167522 }
75177523 else
@@ -7529,7 +7535,8 @@ int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len)
75297535#ifdef WOLF_CRYPTO_CB_AES_SETKEY
75307536 /* In CryptoCB key import mode, skip M0 table generation.
75317537 * The secure element handles GCM internally. */
7532- if (aes->devId != INVALID_DEVID && aes->devCtx != NULL) {
7538+ if (aes->devId != INVALID_DEVID && aes->devCtx != NULL &&
7539+ (aes->devFlags & WC_AES_FLAG_GCM_OFFLOAD)) {
75337540 /* M0 table not needed - SE does GCM */
75347541 }
75357542 else
@@ -13381,6 +13388,9 @@ int wc_AesInit(Aes* aes, void* heap, int devId)
1338113388
1338213389#if defined(WOLF_CRYPTO_CB) || defined(WOLFSSL_STM32U5_DHUK)
1338313390 aes->devId = devId;
13391+ #ifdef WOLF_CRYPTO_CB
13392+ aes->devFlags = 0; /* Defensive init: ensure flags start clean */
13393+ #endif
1338413394#else
1338513395 (void)devId;
1338613396#endif
@@ -13473,14 +13483,17 @@ void wc_AesFree(Aes* aes)
1347313483 {
1347413484 int ret = wc_CryptoCb_Free(aes->devId, WC_ALGO_TYPE_CIPHER,
1347513485 WC_CIPHER_AES, aes);
13486+ if (ret == WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
13487+ /* Callback didn't handle it - fall through to software cleanup */
13488+ }
13489+ else {
13490+ /* Callback handled cleanup */
1347613491 #ifdef WOLF_CRYPTO_CB_AES_SETKEY
13477- aes->devCtx = NULL; /* Clear device context handle */
13492+ aes->devCtx = NULL;
13493+ aes->devFlags = 0;
1347813494 #endif
13479- /* If callback wants standard free, it can set devId to INVALID_DEVID.
13480- * Otherwise assume the callback handled cleanup. */
13481- if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
1348213495 return;
13483- /* fall-through when unavailable */
13496+ }
1348413497 }
1348513498#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_FREE */
1348613499
0 commit comments