Skip to content

Commit b211cc4

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. Behavior depends on build configuration: - TLS builds (default): Key material and GCM tables are retained for software fallback. Provides hardware acceleration with safety. - Crypto-only builds (--disable-tls): Key material not stored, GCM tables skipped. Provides true hardware offload with key isolation. 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 in crypto-only builds when device declares WC_CRYPTOCB_AES_GCM capability - Preserve existing software AES behavior when devId is INVALID_DEVID - Preserve existing CryptoCB behavior when WOLF_CRYPTO_CB_AES_SETKEY is not defined 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 eeaa3a7 commit b211cc4

File tree

10 files changed

+1164
-25
lines changed

10 files changed

+1164
-25
lines changed

.github/workflows/os-check.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ 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"',
76+
'--enable-tls13 --enable-cryptocb --enable-aesgcm CPPFLAGS="-DWOLF_CRYPTO_CB_AES_SETKEY -DWOLF_CRYPTO_CB_AES_NO_FALLBACK -DWOLF_CRYPTO_CB_FREE"',
7377
]
7478
name: make check
7579
if: github.repository_owner == 'wolfssl'

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,35 @@ 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+
**TLS Builds with Fallback (Default):**
65+
- Provides hardware **acceleration** with automatic software fallback
66+
- Key material IS copied to wolfSSL memory for fallback capability
67+
- Safe for production TLS servers
68+
- HSMs/SEs accelerate crypto but don't provide key isolation
69+
- Enable with: `CPPFLAGS="-DWOLF_CRYPTO_CB_AES_SETKEY"`
70+
71+
**TLS Builds with Key Isolation (No Fallback):**
72+
- Provides true hardware **offload** with TLS enabled
73+
- Key material NOT copied to wolfSSL memory (true key isolation)
74+
- Requires robust error handling in callbacks
75+
- Suitable for TLS 1.3 with Secure Element key protection
76+
- Enable with: `CPPFLAGS="-DWOLF_CRYPTO_CB_AES_SETKEY -DWOLF_CRYPTO_CB_AES_NO_FALLBACK -DWOLF_CRYPTO_CB_FREE"`
77+
78+
**Crypto-Only Builds (--disable-tls):**
79+
- Provides true hardware **offload**
80+
- Key material NOT copied to wolfSSL memory
81+
- Requires robust error handling in callbacks
82+
- Suitable for embedded crypto-only applications
83+
- Enable with: `--disable-tls` and `CPPFLAGS="-DWOLF_CRYPTO_CB_AES_SETKEY"`
84+
85+
This feature is commonly used for TLS 1.3 traffic key protection on
86+
embedded platforms with hardware security modules where symmetric keys
87+
must never exist in main RAM.
88+
6089
### Note 2
6190
wolfSSL takes a different approach to certificate verification than OpenSSL
6291
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,35 @@
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.
83+
84+
**TLS Builds with Fallback (Default):**
85+
- Keys ARE retained in memory for fallback.
86+
- Provides acceleration, not key isolation.
87+
88+
**TLS Builds with Key Isolation (WOLF_CRYPTO_CB_AES_NO_FALLBACK):**
89+
- Keys NOT retained in memory.
90+
- Provides true offload with key isolation.
91+
- Suitable for TLS 1.3 traffic key protection.
92+
93+
**Crypto-Only Builds (--disable-tls):**
94+
- Keys NOT retained in memory.
95+
- Provides true offload with key isolation.
96+
97+
This mode is compatible with Secure Elements and hardware-backed
98+
key storage and is intended for protecting TLS traffic keys.
99+
100+
Software fallback to the standard AES implementation occurs
101+
automatically during key setup if the device does not handle the
102+
SetKey operation. However, once a key is imported (devCtx is set),
103+
AES-GCM operations are expected to be handled by the device.
104+
105+
\sa wc_CryptoCb_AesSetKey
106+
\sa \ref Crypto Callbacks
107+
*/
77108

0 commit comments

Comments
 (0)