Skip to content

Commit b634662

Browse files
committed
Drop key runtime capability
Signed-off-by: Sameeh Jubran <[email protected]>
1 parent 34a363a commit b634662

File tree

7 files changed

+54
-371
lines changed

7 files changed

+54
-371
lines changed

doc/dox_comments/header_files/cryptocb.h

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -198,27 +198,18 @@ void wc_CryptoCb_InfoString(wc_CryptoInfo* info);
198198
- Key bytes ARE stored in wolfCrypt memory (devKey) for fallback
199199
- GCM tables ARE generated for software fallback
200200
- Provides hardware acceleration with automatic fallback
201-
- Even when WC_CRYPTOCB_AES_GCM is set, tables are generated for safety
202201
203202
**Crypto-Only Builds (--disable-tls):**
204203
- Key bytes NOT stored in wolfCrypt memory (true key isolation)
205-
- GCM tables skipped when WC_CRYPTOCB_AES_GCM is set
206-
- True hardware offload - callback must handle all GCM operations
204+
- GCM tables skipped (true hardware offload)
205+
- Callback must handle all GCM operations (SetKey, Encrypt, Decrypt, Free)
207206
208-
The callback declares its capabilities by setting flags in the
209-
capabilities parameter. If WC_CRYPTOCB_AES_GCM is set, the callback
210-
supports AES-GCM acceleration. In TLS builds, tables are ALWAYS generated
211-
for fallback regardless of this flag. In crypto-only builds, tables are
212-
skipped only when WC_CRYPTOCB_AES_GCM is set. If not set, wolfCrypt
213-
generates tables for software fallback.
207+
If the callback returns success (0), full AES-GCM offload is assumed.
208+
The callback must handle SetKey, Encrypt, Decrypt, and Free operations.
214209
215210
\param aes AES context
216211
\param key Pointer to raw AES key material
217212
\param keySz Size of key in bytes
218-
\param capabilities Output parameter receiving capability flags set by
219-
callback. May be NULL if capabilities are not needed.
220-
Callback sets WC_CRYPTOCB_AES_GCM to indicate full
221-
GCM offload support.
222213
223214
\return 0 on success
224215
\return CRYPTOCB_UNAVAILABLE if device does not support this operation
@@ -232,28 +223,20 @@ void wc_CryptoCb_InfoString(wc_CryptoInfo* info);
232223
Aes aes;
233224
byte key[32] = { /* 256-bit key */ };
234225
int devId = 1;
235-
int capabilities = 0;
236226

237227
/* Register your CryptoCB callback first */
238228
wc_CryptoCb_RegisterDevice(devId, myCryptoCallback, NULL);
239229

240230
wc_AesInit(&aes, NULL, devId);
241231
/* wc_AesGcmSetKey internally calls wc_CryptoCb_AesSetKey */
242-
if (wc_CryptoCb_AesSetKey(&aes, key, sizeof(key), &capabilities) == 0) {
232+
if (wc_CryptoCb_AesSetKey(&aes, key, sizeof(key)) == 0) {
243233
/* Key successfully imported to device via callback */
244234
/* aes.devCtx now contains device handle */
245-
/* Check if GCM acceleration is supported */
246-
if (capabilities & WC_CRYPTOCB_AES_GCM) {
247-
/* GCM acceleration active */
248-
/* Note: In TLS builds, tables still generated for fallback */
249-
/* In crypto-only builds, tables skipped (true offload) */
250-
}
235+
/* Full GCM offload is assumed - callback must handle all operations */
251236
}
252237
\endcode
253238

254239
\sa wc_CryptoCb_RegisterDevice
255240
\sa wc_AesInit
256-
\sa WC_CRYPTOCB_AES_GCM
257241
*/
258-
int wc_CryptoCb_AesSetKey(Aes* aes, const byte* key, word32 keySz,
259-
int* capabilities);
242+
int wc_CryptoCb_AesSetKey(Aes* aes, const byte* key, word32 keySz);

tests/api/test_aes.c

Lines changed: 0 additions & 210 deletions
Original file line numberDiff line numberDiff line change
@@ -5284,10 +5284,6 @@ static int test_CryptoCb_Aes_Cb(int devId, wc_CryptoInfo* info, void* ctx)
52845284
/* Store handle in aes->devCtx - this is what wolfSSL will use */
52855285
aes->devCtx = cryptoCbAesMockHandle;
52865286

5287-
/* Declare AES-GCM capability.
5288-
* In TLS builds: wolfSSL will still generate tables for fallback.
5289-
* In crypto-only builds: wolfSSL skips tables (true offload). */
5290-
info->cipher.aessetkey.capabilities = WC_CRYPTOCB_AES_GCM;
52915287

52925288
cryptoCbAesSetKeyCalled++;
52935289

@@ -5491,9 +5487,6 @@ int test_wc_CryptoCb_AesSetKey(void)
54915487
/* Verify callback was invoked */
54925488
ExpectIntEQ(cryptoCbAesSetKeyCalled, 1);
54935489

5494-
/* Verify GCM offload flag was set (callback declared capability) */
5495-
ExpectIntEQ(aes->gcmCryptoCbOffload, 1);
5496-
54975490
/* Verify handle stored in devCtx */
54985491
ExpectPtrEq(aes->devCtx, cryptoCbAesMockHandle);
54995492

@@ -5654,10 +5647,6 @@ static int test_CryptoCb_AesGcm_Offload_Cb(int devId, wc_CryptoInfo* info, void*
56545647
/* Store handle in aes->devCtx - this is what wolfSSL will use */
56555648
aes->devCtx = cryptoCbAesGcmMockHandle;
56565649

5657-
/* Declare full AES-GCM offload capability.
5658-
* This tells wolfSSL to skip GCM table generation and expect
5659-
* all GCM operations to go through CryptoCB. */
5660-
info->cipher.aessetkey.capabilities = WC_CRYPTOCB_AES_GCM;
56615650

56625651
cryptoCbAesGcmSetKeyCalled++;
56635652

@@ -5894,9 +5883,6 @@ int test_wc_CryptoCb_AesGcm_EncryptDecrypt(void)
58945883
/* Verify SetKey callback was invoked */
58955884
ExpectIntEQ(cryptoCbAesGcmSetKeyCalled, 1);
58965885

5897-
/* Verify GCM offload flag was set (callback declared capability) */
5898-
ExpectIntEQ(aes->gcmCryptoCbOffload, 1);
5899-
59005886
/* Verify handle stored in devCtx */
59015887
ExpectPtrEq(aes->devCtx, cryptoCbAesGcmMockHandle);
59025888

@@ -5984,201 +5970,5 @@ int test_wc_CryptoCb_AesGcm_EncryptDecrypt(void)
59845970
return EXPECT_RESULT();
59855971
}
59865972

5987-
/*
5988-
* Test: Key import only callback - verifies software fallback works
5989-
* when callback does NOT declare WC_CRYPTOCB_AES_GCM capability
5990-
*/
5991-
#define TEST_CRYPTOCB_AES_KEYIMPORT_ONLY_DEVID 9
5992-
5993-
/* Test state tracking for key-import-only test */
5994-
static int cryptoCbKeyImportOnlySetKeyCalled = 0;
5995-
static void* cryptoCbKeyImportOnlyMockHandle = (void*)0xDEADBEEF;
5996-
5997-
/* Mock SE key storage for key-import-only test */
5998-
static struct {
5999-
byte key[AES_256_KEY_SIZE];
6000-
word32 keySz;
6001-
int valid;
6002-
} mockSeKeyImportOnly;
6003-
6004-
/* Callback that handles key import but NOT GCM operations */
6005-
static int test_CryptoCb_KeyImportOnly_Cb(int devId, wc_CryptoInfo* info, void* ctx)
6006-
{
6007-
(void)ctx;
6008-
6009-
if (devId != TEST_CRYPTOCB_AES_KEYIMPORT_ONLY_DEVID)
6010-
return CRYPTOCB_UNAVAILABLE;
6011-
6012-
/* AES SetKey operation - simulate SE key import */
6013-
if (info->algo_type == WC_ALGO_TYPE_CIPHER &&
6014-
info->cipher.type == WC_CIPHER_AES &&
6015-
info->cipher.aessetkey.aes != NULL) {
6016-
6017-
Aes* aes = info->cipher.aessetkey.aes;
6018-
const byte* key = info->cipher.aessetkey.key;
6019-
word32 keySz = info->cipher.aessetkey.keySz;
6020-
6021-
/* Validate key */
6022-
if (key == NULL || keySz == 0 || keySz > AES_256_KEY_SIZE) {
6023-
return BAD_FUNC_ARG;
6024-
}
6025-
6026-
/* "Import" key to simulated SE storage */
6027-
XMEMCPY(mockSeKeyImportOnly.key, key, keySz);
6028-
mockSeKeyImportOnly.keySz = keySz;
6029-
mockSeKeyImportOnly.valid = 1;
6030-
6031-
/* Store handle in aes->devCtx */
6032-
aes->devCtx = cryptoCbKeyImportOnlyMockHandle;
6033-
6034-
/* Do not set WC_CRYPTOCB_AES_GCM capability.
6035-
* This simulates a device that can import keys but cannot
6036-
* perform GCM operations. wolfSSL should generate GCM tables
6037-
* and use software fallback for GCM operations. */
6038-
/* info->cipher.aessetkey.capabilities stays 0 (default) */
6039-
6040-
cryptoCbKeyImportOnlySetKeyCalled++;
6041-
6042-
return 0;
6043-
}
6044-
6045-
/* GCM operations not supported - return UNAVAILABLE to trigger software fallback */
6046-
if (info->algo_type == WC_ALGO_TYPE_CIPHER &&
6047-
info->cipher.type == WC_CIPHER_AES_GCM) {
6048-
return CRYPTOCB_UNAVAILABLE;
6049-
}
6050-
6051-
return CRYPTOCB_UNAVAILABLE;
6052-
}
6053-
6054-
int test_wc_CryptoCb_AesKeyImportOnly(void)
6055-
{
6056-
EXPECT_DECLS;
6057-
#ifdef WOLFSSL_SMALL_STACK
6058-
Aes* aes = NULL;
6059-
byte* key = NULL;
6060-
byte* iv = NULL;
6061-
byte* plaintext = NULL;
6062-
byte* ciphertext = NULL;
6063-
byte* decrypted = NULL;
6064-
byte* authTag = NULL;
6065-
byte* aad = NULL;
6066-
#else
6067-
Aes aes[1];
6068-
byte key[AES_128_KEY_SIZE];
6069-
byte iv[GCM_NONCE_MID_SZ];
6070-
byte plaintext[32];
6071-
byte ciphertext[32];
6072-
byte decrypted[32];
6073-
byte authTag[AES_BLOCK_SIZE];
6074-
byte aad[16];
6075-
#endif
6076-
int ret;
6077-
6078-
#ifdef WOLFSSL_SMALL_STACK
6079-
aes = (Aes*)XMALLOC(sizeof(Aes), NULL, DYNAMIC_TYPE_TMP_BUFFER);
6080-
key = (byte*)XMALLOC(AES_128_KEY_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
6081-
iv = (byte*)XMALLOC(GCM_NONCE_MID_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER);
6082-
plaintext = (byte*)XMALLOC(32, NULL, DYNAMIC_TYPE_TMP_BUFFER);
6083-
ciphertext = (byte*)XMALLOC(32, NULL, DYNAMIC_TYPE_TMP_BUFFER);
6084-
decrypted = (byte*)XMALLOC(32, NULL, DYNAMIC_TYPE_TMP_BUFFER);
6085-
authTag = (byte*)XMALLOC(AES_BLOCK_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
6086-
aad = (byte*)XMALLOC(16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
6087-
6088-
if (aes == NULL || key == NULL || iv == NULL || plaintext == NULL ||
6089-
ciphertext == NULL || decrypted == NULL || authTag == NULL || aad == NULL) {
6090-
ret = MEMORY_E;
6091-
goto out;
6092-
}
6093-
#endif
6094-
6095-
/* Initialize test data */
6096-
XMEMSET(key, 0x01, AES_128_KEY_SIZE);
6097-
XMEMSET(iv, 0x02, GCM_NONCE_MID_SZ);
6098-
XMEMSET(plaintext, 0x03, 32);
6099-
XMEMSET(aad, 0x04, 16);
6100-
XMEMSET(aes, 0, sizeof(Aes));
6101-
XMEMSET(&mockSeKeyImportOnly, 0, sizeof(mockSeKeyImportOnly));
6102-
cryptoCbKeyImportOnlySetKeyCalled = 0;
6103-
6104-
/* Register callback for key-import-only device */
6105-
ret = wc_CryptoCb_RegisterDevice(TEST_CRYPTOCB_AES_KEYIMPORT_ONLY_DEVID,
6106-
test_CryptoCb_KeyImportOnly_Cb, NULL);
6107-
ExpectIntEQ(ret, 0);
6108-
6109-
/* Initialize Aes with device ID */
6110-
ret = wc_AesInit(aes, NULL, TEST_CRYPTOCB_AES_KEYIMPORT_ONLY_DEVID);
6111-
ExpectIntEQ(ret, 0);
6112-
ExpectIntEQ(aes->devId, TEST_CRYPTOCB_AES_KEYIMPORT_ONLY_DEVID);
6113-
6114-
/* Set key - should trigger CryptoCB and "import" to mock SE */
6115-
ret = wc_AesGcmSetKey(aes, key, sizeof(key));
6116-
ExpectIntEQ(ret, 0);
6117-
6118-
/* Verify SetKey callback was invoked (callback is called but then we fall through) */
6119-
ExpectIntEQ(cryptoCbKeyImportOnlySetKeyCalled, 1);
6120-
6121-
/* Verify GCM offload flag is 0 (capability not declared) */
6122-
ExpectIntEQ(aes->gcmCryptoCbOffload, 0);
6123-
6124-
/* Verify keylen is set (software path sets this after falling through) */
6125-
ExpectIntEQ(aes->keylen, (int)sizeof(key));
6126-
6127-
/* The callback sets devCtx, but since capability is not set, we fall through
6128-
* to software path. The software path will set up the key in aes->key for GCM
6129-
* table generation. devCtx may or may not be cleared by software path. */
6130-
6131-
/* Verify GCM tables were generated (H table should be non-zero) */
6132-
/* If gcmCryptoCbOffload is 0, tables should be generated for software fallback */
6133-
{
6134-
int hTableNonZero = 0;
6135-
int i;
6136-
for (i = 0; i < WC_AES_BLOCK_SIZE; i++) {
6137-
if (aes->gcm.H[i] != 0) {
6138-
hTableNonZero = 1;
6139-
break;
6140-
}
6141-
}
6142-
ExpectIntEQ(hTableNonZero, 1); /* H table should be generated */
6143-
}
6144-
6145-
/* Encrypt via wolfCrypt API - should fall back to software */
6146-
/* Callback will return CRYPTOCB_UNAVAILABLE, triggering software path */
6147-
ret = wc_AesGcmEncrypt(aes, ciphertext, plaintext, 32,
6148-
iv, sizeof(iv), authTag, sizeof(authTag),
6149-
aad, 16);
6150-
ExpectIntEQ(ret, 0);
6151-
6152-
/* Decrypt via wolfCrypt API - should fall back to software */
6153-
ret = wc_AesGcmDecrypt(aes, decrypted, ciphertext, 32,
6154-
iv, sizeof(iv), authTag, sizeof(authTag),
6155-
aad, 16);
6156-
ExpectIntEQ(ret, 0);
6157-
6158-
/* Verify decrypted plaintext matches original */
6159-
ExpectIntEQ(XMEMCMP(plaintext, decrypted, 32), 0);
6160-
6161-
/* Cleanup */
6162-
wc_AesFree(aes);
6163-
wc_CryptoCb_UnRegisterDevice(TEST_CRYPTOCB_AES_KEYIMPORT_ONLY_DEVID);
6164-
6165-
/* mockSeKeyImportOnly.valid may still be 1 since callback was called,
6166-
* but the key is not actually used (software path is taken) */
6167-
6168-
#ifdef WOLFSSL_SMALL_STACK
6169-
out:
6170-
XFREE(aes, NULL, DYNAMIC_TYPE_TMP_BUFFER);
6171-
XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
6172-
XFREE(iv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
6173-
XFREE(aad, NULL, DYNAMIC_TYPE_TMP_BUFFER);
6174-
XFREE(plaintext, NULL, DYNAMIC_TYPE_TMP_BUFFER);
6175-
XFREE(ciphertext, NULL, DYNAMIC_TYPE_TMP_BUFFER);
6176-
XFREE(decrypted, NULL, DYNAMIC_TYPE_TMP_BUFFER);
6177-
XFREE(authTag, NULL, DYNAMIC_TYPE_TMP_BUFFER);
6178-
#endif
6179-
6180-
return EXPECT_RESULT();
6181-
}
6182-
61835973
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_AES_SETKEY && !NO_AES && HAVE_AESGCM */
61845974

tests/api/test_aes.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,12 @@ int test_wc_GmacUpdate(void);
5757
!defined(NO_AES) && defined(HAVE_AESGCM)
5858
int test_wc_CryptoCb_AesSetKey(void);
5959
int test_wc_CryptoCb_AesGcm_EncryptDecrypt(void);
60-
int test_wc_CryptoCb_AesKeyImportOnly(void);
6160
#endif
6261

6362
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_AES_SETKEY) && \
6463
!defined(NO_AES) && defined(HAVE_AESGCM)
6564
#define TEST_CRYPTOCB_AES_SETKEY_DECL , TEST_DECL_GROUP("aes", test_wc_CryptoCb_AesSetKey), \
66-
TEST_DECL_GROUP("aes", test_wc_CryptoCb_AesGcm_EncryptDecrypt), \
67-
TEST_DECL_GROUP("aes", test_wc_CryptoCb_AesKeyImportOnly)
65+
TEST_DECL_GROUP("aes", test_wc_CryptoCb_AesGcm_EncryptDecrypt)
6866
#else
6967
#define TEST_CRYPTOCB_AES_SETKEY_DECL
7068
#endif

0 commit comments

Comments
 (0)