Skip to content

Commit cdc15fd

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 cdc15fd

File tree

10 files changed

+1358
-25
lines changed

10 files changed

+1358
-25
lines changed

.github/workflows/os-check.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ 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"',
7374
]
7475
name: make check
7576
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: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,78 @@ 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 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 (true key isolation)
204+
- GCM tables skipped when WC_CRYPTOCB_AES_GCM is set
205+
- True hardware offload
206+
207+
The callback declares its capabilities by setting flags in the
208+
capabilities parameter. If WC_CRYPTOCB_AES_GCM is set, the callback
209+
supports AES-GCM acceleration. In TLS builds, tables are still generated
210+
for fallback. In crypto-only builds, tables are skipped for true offload.
211+
If not set, wolfCrypt generates tables for software fallback.
212+
213+
\param aes AES context
214+
\param key Pointer to raw AES key material
215+
\param keySz Size of key in bytes
216+
\param capabilities Output parameter receiving capability flags set by
217+
callback. May be NULL if capabilities are not needed.
218+
Callback sets WC_CRYPTOCB_AES_GCM to indicate full
219+
GCM offload support.
220+
221+
\return 0 on success
222+
\return CRYPTOCB_UNAVAILABLE if device does not support this operation
223+
\return BAD_FUNC_ARG on invalid parameters
224+
225+
_Example_
226+
\code
227+
#include <wolfssl/wolfcrypt/cryptocb.h>
228+
#include <wolfssl/wolfcrypt/aes.h>
229+
230+
Aes aes;
231+
byte key[32] = { /* 256-bit key */ };
232+
int devId = 1;
233+
int capabilities = 0;
234+
235+
/* Register your CryptoCB callback first */
236+
wc_CryptoCb_RegisterDevice(devId, myCryptoCallback, NULL);
237+
238+
wc_AesInit(&aes, NULL, devId);
239+
/* wc_AesGcmSetKey internally calls wc_CryptoCb_AesSetKey */
240+
if (wc_CryptoCb_AesSetKey(&aes, key, sizeof(key), &capabilities) == 0) {
241+
/* Key successfully imported to device via callback */
242+
/* aes.devCtx now contains device handle */
243+
/* Check if GCM acceleration is supported */
244+
if (capabilities & WC_CRYPTOCB_AES_GCM) {
245+
/* GCM acceleration active */
246+
/* Note: In TLS builds, tables still generated for fallback */
247+
/* In crypto-only builds, tables skipped (true offload) */
248+
}
249+
}
250+
\endcode
251+
252+
\sa wc_CryptoCb_RegisterDevice
253+
\sa wc_AesInit
254+
\sa WC_CRYPTOCB_AES_GCM
255+
*/
256+
int wc_CryptoCb_AesSetKey(Aes* aes, const byte* key, word32 keySz,
257+
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)