Skip to content

Commit 5e03e8f

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 0f3c769 commit 5e03e8f

File tree

10 files changed

+1382
-25
lines changed

10 files changed

+1382
-25
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
]
7477
name: make check
7578
if: github.repository_owner == 'wolfssl'

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,27 @@ 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 (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+
70+
**Crypto-Only Builds (--disable-tls):**
71+
- Provides true hardware **offload**
72+
- Key material NOT copied to wolfSSL memory
73+
- Requires robust error handling in callbacks
74+
- Suitable for embedded crypto-only applications
75+
76+
Enable with: `CPPFLAGS="-DWOLF_CRYPTO_CB_AES_SETKEY"`
77+
78+
This feature is commonly used for TLS 1.3 traffic key protection on
79+
embedded platforms with hardware security modules.
80+
6081
### Note 2
6182
wolfSSL takes a different approach to certificate verification than OpenSSL
6283
does. The default policy for the client is to verify the server, this means

doc/dox_comments/header_files/cryptocb.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,80 @@ 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+
- Even when WC_CRYPTOCB_AES_GCM is set, tables are generated for safety
202+
203+
**Crypto-Only Builds (--disable-tls):**
204+
- 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
207+
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.
214+
215+
\param aes AES context
216+
\param key Pointer to raw AES key material
217+
\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.
222+
223+
\return 0 on success
224+
\return CRYPTOCB_UNAVAILABLE if device does not support this operation
225+
\return BAD_FUNC_ARG on invalid parameters
226+
227+
_Example_
228+
\code
229+
#include <wolfssl/wolfcrypt/cryptocb.h>
230+
#include <wolfssl/wolfcrypt/aes.h>
231+
232+
Aes aes;
233+
byte key[32] = { /* 256-bit key */ };
234+
int devId = 1;
235+
int capabilities = 0;
236+
237+
/* Register your CryptoCB callback first */
238+
wc_CryptoCb_RegisterDevice(devId, myCryptoCallback, NULL);
239+
240+
wc_AesInit(&aes, NULL, devId);
241+
/* wc_AesGcmSetKey internally calls wc_CryptoCb_AesSetKey */
242+
if (wc_CryptoCb_AesSetKey(&aes, key, sizeof(key), &capabilities) == 0) {
243+
/* Key successfully imported to device via callback */
244+
/* 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+
}
251+
}
252+
\endcode
253+
254+
\sa wc_CryptoCb_RegisterDevice
255+
\sa wc_AesInit
256+
\sa WC_CRYPTOCB_AES_GCM
257+
*/
258+
int wc_CryptoCb_AesSetKey(Aes* aes, const byte* key, word32 keySz,
259+
int* capabilities);

doc/dox_comments/header_files/doxygen_pages.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,30 @@
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 (Default):**
85+
- Keys ARE retained in memory for fallback.
86+
- Provides acceleration, not key isolation.
87+
88+
**Crypto-Only Builds (--disable-tls):**
89+
- Keys NOT retained in memory.
90+
- Provides true offload with key isolation.
91+
92+
This mode is compatible with Secure Elements and hardware-backed
93+
key storage and is intended for protecting TLS traffic keys.
94+
95+
Software fallback to the standard AES implementation occurs
96+
automatically during key setup if the device does not handle the
97+
SetKey operation. However, once a key is imported (devCtx is set),
98+
AES-GCM operations are expected to be handled by the device.
99+
100+
\sa wc_CryptoCb_AesSetKey
101+
\sa \ref Crypto Callbacks
102+
*/
77103

0 commit comments

Comments
 (0)