Skip to content

Commit aaa54dc

Browse files
committed
fix GCM test
Signed-off-by: Sameeh Jubran <sameeh@wolfssl.com>
1 parent 74a94c0 commit aaa54dc

File tree

5 files changed

+61
-37
lines changed

5 files changed

+61
-37
lines changed

tests/api/test_aes.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5283,6 +5283,7 @@ static int test_CryptoCb_Aes_Cb(int devId, wc_CryptoInfo* info, void* ctx)
52835283

52845284
/* Store handle in aes->devCtx - this is what wolfSSL will use */
52855285
aes->devCtx = cryptoCbAesMockHandle;
5286+
aes->devFlags |= WC_AES_FLAG_GCM_OFFLOAD; /* We handle GCM ops */
52865287

52875288
cryptoCbAesSetKeyCalled++;
52885289

@@ -5627,6 +5628,7 @@ static int test_CryptoCb_AesGcm_Offload_Cb(int devId, wc_CryptoInfo* info, void*
56275628

56285629
/* Store handle in aes->devCtx - this is what wolfSSL will use */
56295630
aes->devCtx = cryptoCbAesGcmMockHandle;
5631+
aes->devFlags |= WC_AES_FLAG_GCM_OFFLOAD; /* We handle GCM ops */
56305632

56315633
cryptoCbAesGcmSetKeyCalled++;
56325634

wolfcrypt/src/aes.c

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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

wolfcrypt/src/cryptocb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ static CryptoCb* wc_CryptoCb_GetDevice(int devId)
301301

302302
/* Filters through find callback set when trying to get the device,
303303
* returns the device found on success and null if not found. */
304-
static CryptoCb* wc_CryptoCb_FindDevice(int devId, int algoType)
304+
WOLFSSL_LOCAL CryptoCb* wc_CryptoCb_FindDevice(int devId, int algoType)
305305
{
306306
int localDevId = devId;
307307

wolfssl/wolfcrypt/aes.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ enum {
237237
WOLF_ENUM_DUMMY_LAST_ELEMENT(AES)
238238
};
239239

240+
/* CryptoCB device capability flags */
241+
#define WC_AES_FLAG_NONE 0x00
242+
#define WC_AES_FLAG_GCM_OFFLOAD 0x01 /* Device handles AES-GCM encrypt/decrypt */
243+
240244
#ifdef WC_AES_BITSLICED
241245
#ifdef WC_AES_BS_WORD_SIZE
242246
#define BS_WORD_SIZE WC_AES_BS_WORD_SIZE
@@ -335,6 +339,9 @@ struct Aes {
335339
#if defined(WOLF_CRYPTO_CB) || defined(WOLFSSL_STM32U5_DHUK)
336340
int devId;
337341
void* devCtx;
342+
#ifdef WOLF_CRYPTO_CB
343+
byte devFlags; /* CryptoCB capability flags */
344+
#endif
338345
#endif
339346
#ifdef WOLF_PRIVATE_KEY_ID
340347
byte id[AES_MAX_ID_LEN];

wolfssl/wolfcrypt/cryptocb.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,8 @@ typedef int (*CryptoDevCallbackFunc)(int devId, struct wc_CryptoInfo* info, void
539539
WOLFSSL_LOCAL void wc_CryptoCb_Init(void);
540540
WOLFSSL_LOCAL void wc_CryptoCb_Cleanup(void);
541541
WOLFSSL_LOCAL int wc_CryptoCb_GetDevIdAtIndex(int startIdx);
542+
/* Internal function to check if CryptoCB device exists - returns NULL if not found */
543+
WOLFSSL_LOCAL void* wc_CryptoCb_FindDevice(int devId, int algoType);
542544
WOLFSSL_API int wc_CryptoCb_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx);
543545
WOLFSSL_API void wc_CryptoCb_UnRegisterDevice(int devId);
544546
WOLFSSL_API int wc_CryptoCb_DefaultDevID(void);

0 commit comments

Comments
 (0)