Skip to content

Commit 6e377c0

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. When WOLF_CRYPTO_CB_AES_SETKEY is defined, wolfCrypt invokes a CryptoCB callback during AES key setup. Behavior is determined by the callback return value: - If callback returns 0: Key is imported to the device (aes->devCtx). Key is NOT copied to wolfCrypt RAM; GCM H/M tables are NOT generated. Full hardware offload is assumed. - If callback returns CRYPTOCB_UNAVAILABLE: Device does not support SetKey. Normal software path is used; key is copied to devKey for optional encrypt/decrypt acceleration. - Any other error: Propagated to the caller. Key points: - Add wc_CryptoCb_AesSetKey() callback for AES key import - Update AES SetKey paths to call CryptoCB and branch on return value - Skip GCM H/M table generation when callback succeeded (devCtx set) - Preserve existing behavior when devId is INVALID_DEVID or WOLF_CRYPTO_CB_AES_SETKEY is not defined Testing: - Add unit test for CryptoCB AES SetKey (verifies key isolation when callback succeeds) - Add end-to-end AES-GCM offload test (SetKey, Encrypt, Decrypt, Free via CryptoCB) - Tests use a mock SE with software AES to validate routing Enable with: CPPFLAGS="-DWOLF_CRYPTO_CB_AES_SETKEY -DWOLF_CRYPTO_CB_FREE" Signed-off-by: Sameeh Jubran <sameeh@wolfssl.com>
1 parent 9ca379f commit 6e377c0

File tree

10 files changed

+988
-22
lines changed

10 files changed

+988
-22
lines changed

.github/workflows/os-check.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ jobs:
7070
'--enable-all --enable-certgencache',
7171
'--enable-sessionexport --enable-dtls --enable-dtls13',
7272
'--enable-sessionexport',
73+
'--enable-cryptocb --enable-aesgcm CPPFLAGS="-DWOLF_CRYPTO_CB_AES_SETKEY -DWOLF_CRYPTO_CB_FREE"',
74+
'--disable-tls --enable-cryptocb --enable-aesgcm CPPFLAGS="-DWOLF_CRYPTO_CB_AES_SETKEY -DWOLF_CRYPTO_CB_FREE"',
75+
'--enable-cryptocb --enable-aesgcm CPPFLAGS="-DWOLF_CRYPTO_CB_AES_SETKEY"',
7376
'--disable-examples CPPFLAGS=-DWOLFSSL_NO_MALLOC',
7477
'CPPFLAGS=-DNO_WOLFSSL_CLIENT',
7578
'CPPFLAGS=-DNO_WOLFSSL_SERVER',

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,29 @@ 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 hardware-accelerated AES operations via CryptoCB.
63+
64+
When `WOLF_CRYPTO_CB_AES_SETKEY` is defined, wolfSSL invokes a CryptoCB
65+
callback during AES key setup. The callback behavior determines the mode:
66+
67+
**If callback returns 0 (success):**
68+
- Key is imported to Secure Element/HSM
69+
- Key is NOT copied to wolfSSL RAM (true key isolation)
70+
- GCM tables are NOT generated (full hardware offload)
71+
- All subsequent AES operations route through CryptoCB
72+
73+
**If callback returns CRYPTOCB_UNAVAILABLE:**
74+
- SE doesn't support key import
75+
- Normal software AES path is used
76+
- Key is copied to devKey for CryptoCB encrypt/decrypt acceleration
77+
78+
This feature enables TLS 1.3 traffic key protection on embedded platforms
79+
where symmetric keys must never exist in main RAM.
80+
81+
Enable with: `CPPFLAGS="-DWOLF_CRYPTO_CB_AES_SETKEY -DWOLF_CRYPTO_CB_FREE"`
82+
6083
### Note 2
6184
wolfSSL takes a different approach to certificate verification than OpenSSL
6285
does. The default policy for the client is to verify the server, this means

doc/dox_comments/header_files/cryptocb.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,63 @@ 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). When supported, the device callback stores
191+
the key internally and sets an opaque handle in aes->devCtx.
192+
193+
When CryptoCB AES SetKey support is enabled
194+
(WOLF_CRYPTO_CB_AES_SETKEY), wolfCrypt routes AES-GCM operations
195+
through the CryptoCB interface.
196+
197+
**TLS Builds (Default):**
198+
- Key bytes ARE stored in wolfCrypt memory (devKey) for fallback
199+
- GCM tables ARE generated for software fallback
200+
- Provides hardware acceleration with automatic fallback
201+
202+
**Crypto-Only Builds (--disable-tls):**
203+
- Key bytes NOT stored in wolfCrypt memory (true key isolation)
204+
- GCM tables skipped (true hardware offload)
205+
- Callback must handle all GCM operations (SetKey, Encrypt, Decrypt, Free)
206+
207+
If the callback returns success (0), full AES-GCM offload is assumed.
208+
The callback must handle SetKey, Encrypt, Decrypt, and Free operations.
209+
210+
\param aes AES context
211+
\param key Pointer to raw AES key material
212+
\param keySz Size of key in bytes
213+
214+
\return 0 on success
215+
\return CRYPTOCB_UNAVAILABLE if device does not support this operation
216+
\return BAD_FUNC_ARG on invalid parameters
217+
218+
_Example_
219+
\code
220+
#include <wolfssl/wolfcrypt/cryptocb.h>
221+
#include <wolfssl/wolfcrypt/aes.h>
222+
223+
Aes aes;
224+
byte key[32] = { /* 256-bit key */ };
225+
int devId = 1;
226+
227+
/* Register your CryptoCB callback first */
228+
wc_CryptoCb_RegisterDevice(devId, myCryptoCallback, NULL);
229+
230+
wc_AesInit(&aes, NULL, devId);
231+
/* wc_AesGcmSetKey internally calls wc_CryptoCb_AesSetKey */
232+
if (wc_CryptoCb_AesSetKey(&aes, key, sizeof(key)) == 0) {
233+
/* Key successfully imported to device via callback */
234+
/* aes.devCtx now contains device handle */
235+
/* Full GCM offload is assumed - callback must handle all operations */
236+
}
237+
\endcode
238+
239+
\sa wc_CryptoCb_RegisterDevice
240+
\sa wc_AesInit
241+
*/
242+
int wc_CryptoCb_AesSetKey(Aes* aes, const byte* key, word32 keySz);

doc/dox_comments/header_files/doxygen_pages.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,27 @@
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 invokes a CryptoCB
81+
callback during AES key setup. The callback behavior determines the mode:
82+
83+
**If callback returns 0 (success):**
84+
- Key is imported to Secure Element/HSM
85+
- Key is NOT copied to wolfSSL RAM (true key isolation)
86+
- GCM tables are NOT generated (full hardware offload)
87+
- All subsequent AES operations route through CryptoCB
88+
89+
**If callback returns CRYPTOCB_UNAVAILABLE:**
90+
- SE doesn't support key import
91+
- Normal software AES path is used
92+
- Key is copied to devKey for CryptoCB encrypt/decrypt acceleration
93+
94+
This mode is compatible with Secure Elements and hardware-backed
95+
key storage and is intended for protecting TLS traffic keys.
96+
97+
\sa wc_CryptoCb_AesSetKey
98+
\sa \ref Crypto Callbacks
99+
*/
77100

0 commit comments

Comments
 (0)