Skip to content

Commit be96866

Browse files
committed
cryptocb: add AES CryptoCB key import support and tests
Add CryptoCB-based AES key import support to enable Secure Element offload without exposing raw AES key material to wolfCrypt. This change introduces a new optional CryptoCB hook (WOLF_CRYPTO_CB_AES_SETKEY) that allows AES keys to be imported into external devices (e.g. Secure Elements or HSMs) and referenced via an opaque handle stored in aes->devCtx. When this mode is active, wolfCrypt stores only key metadata and routes AES-GCM operations through CryptoCB, bypassing software key storage and GCM table generation. Key points: - Add wc_CryptoCb_AesSetKey() callback for AES key import - Update AES SetKey paths to support key import mode with graceful fallback to software when CryptoCB is unavailable - Skip GCM H/M table generation when AES-GCM is handled by the device - Preserve existing software AES behavior when devId is INVALID_DEVID Testing: - Add unit test for CryptoCB AES SetKey behavior - Add end-to-end AES-GCM offload unit test that verifies: * SetKey, Encrypt, Decrypt, and Free are routed via CryptoCB * Correct ciphertext/auth tag generation * Correct plaintext recovery after decrypt * Proper lifecycle handling of device context handles - Tests use a mock Secure Element that internally performs software AES to validate routing without requiring hardware Signed-off-by: Sameeh Jubran <sameeh@wolfssl.com>
1 parent 0f3c769 commit be96866

File tree

8 files changed

+976
-25
lines changed

8 files changed

+976
-25
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ suites are available. You can remove this error by defining
5757
`WOLFSSL_ALLOW_NO_SUITES` in the event that you desire that, i.e., you're
5858
not using TLS cipher suites.
5959

60+
### AES CryptoCB Key Import Support
61+
62+
wolfSSL supports offloading AES key handling to external devices
63+
(e.g., Secure Elements or HSMs) using the Crypto Callback (CryptoCB)
64+
interface - the same mechanism used for RSA, ECC, and other algorithms.
65+
66+
When `WOLF_CRYPTO_CB_AES_SETKEY` is enabled, AES keys can be imported
67+
into a device via the CryptoCB callback. The device stores the key
68+
internally and provides an opaque handle via `aes->devCtx`. Subsequent
69+
AES-GCM operations are routed through CryptoCB, and wolfCrypt does not
70+
retain the raw key material.
71+
72+
This feature is commonly used for TLS 1.3 traffic key protection on
73+
embedded platforms with hardware security modules.
74+
6075
### Note 2
6176
wolfSSL takes a different approach to certificate verification than OpenSSL
6277
does. The default policy for the client is to verify the server, this means

doc/dox_comments/header_files/cryptocb.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,52 @@ void wc_CryptoCb_SetDeviceFindCb(CryptoDevCallbackFind cb);
180180
\sa wc_CryptoCb_RegisterDevice
181181
*/
182182
void wc_CryptoCb_InfoString(wc_CryptoInfo* info);
183+
184+
/*!
185+
\ingroup CryptoCb
186+
187+
\brief Import an AES key into a CryptoCB device for hardware offload.
188+
189+
This function allows AES keys to be handled by an external device
190+
(e.g. Secure Element or HSM) without exposing raw key material to
191+
wolfCrypt. When supported, the device callback stores the key internally
192+
and sets an opaque handle in aes->devCtx.
193+
194+
When CryptoCB AES SetKey support is enabled
195+
(WOLF_CRYPTO_CB_AES_SETKEY), wolfCrypt will route AES-GCM operations
196+
through the CryptoCB interface and avoid storing key bytes or
197+
generating GCM tables in software.
198+
199+
\param aes AES context
200+
\param key Pointer to raw AES key material
201+
\param keySz Size of key in bytes
202+
203+
\return 0 on success
204+
\return CRYPTOCB_UNAVAILABLE if device does not support this operation
205+
\return BAD_FUNC_ARG on invalid parameters
206+
207+
_Example_
208+
\code
209+
#include <wolfssl/wolfcrypt/cryptocb.h>
210+
#include <wolfssl/wolfcrypt/aes.h>
211+
212+
Aes aes;
213+
byte key[32] = { /* 256-bit key */ };
214+
int devId = 1;
215+
216+
// Register your CryptoCB callback first
217+
wc_CryptoCb_RegisterDevice(devId, myCryptoCallback, NULL);
218+
219+
wc_AesInit(&aes, NULL, devId);
220+
// wc_AesGcmSetKey internally calls wc_CryptoCb_AesSetKey
221+
if (wc_AesGcmSetKey(&aes, key, sizeof(key)) == 0) {
222+
// Key successfully imported to device via callback
223+
// aes.devCtx now contains device handle
224+
// Subsequent AES-GCM operations will use the device
225+
}
226+
\endcode
227+
228+
\sa wc_CryptoCb_RegisterDevice
229+
\sa wc_AesInit
230+
*/
231+
int wc_CryptoCb_AesSetKey(Aes* aes, const byte* key, word32 keySz);

doc/dox_comments/header_files/doxygen_pages.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,24 @@
7474
- \ref SAKKE_RSK
7575
- \ref SAKKE_Operations
7676
*/
77+
/*!
78+
\page AES_CryptoCB_KeyImport AES CryptoCB Key Import
79+
80+
When enabled via WOLF_CRYPTO_CB_AES_SETKEY, wolfSSL allows AES keys
81+
to be imported into external cryptographic devices using the Crypto
82+
Callback interface. In key import mode, AES keys are not retained in
83+
wolfCrypt memory. Instead, an opaque device handle is used for all
84+
subsequent AES-GCM operations.
85+
86+
This mode is compatible with Secure Elements and hardware-backed
87+
key storage and is intended for protecting TLS traffic keys.
88+
89+
Software fallback to the standard AES implementation occurs
90+
automatically during key setup if the device does not handle the
91+
SetKey operation. However, once a key is imported (devCtx is set),
92+
AES-GCM operations are expected to be handled by the device.
93+
94+
\sa wc_CryptoCb_AesSetKey
95+
\sa \ref Crypto Callbacks
96+
*/
7797

0 commit comments

Comments
 (0)