@@ -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
0 commit comments