Skip to content

Conversation

@sameehj
Copy link
Contributor

@sameehj sameehj commented Jan 14, 2026

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

@sameehj sameehj requested a review from douzzer January 14, 2026 14:35
@devin-ai-integration
Copy link
Contributor

🛟 Devin Lifeguard found 2 likely issues in this PR

  • check-all-return-codes snippet snippet: Capture the return value of wc_CryptoCb_UnRegisterDevice() in each test (e.g., ret = wc_CryptoCb_UnRegisterDevice(TEST_CRYPTOCB_AES_DEVID); ExpectIntEQ(ret, 0);) to ensure any error is detected.
  • limit-stack-usage snippet snippet: Use the WOLFSSL_SMALL_STACK pattern in the two newly added test functions: dynamically allocate the large local objects (Aes aes, the plaintext/ciphertext/authTag buffers, etc.) with XMALLOC/XFREE (guarded by #ifdef WOLFSSL_SMALL_STACK) instead of placing them on the stack.

@sameehj
please take a look at the above issues which Devin flagged. Devin will not fix these issues automatically.

@sameehj
Copy link
Contributor Author

sameehj commented Jan 14, 2026

retest this please

@sameehj sameehj force-pushed the aes-offload branch 2 times, most recently from b3bcd1c to fd6d469 Compare January 15, 2026 16:17
Copy link
Member

@julek-wolfssl julek-wolfssl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see "proxy-key" appearing multiple times in the PR. What does it mean?
The ORIGINAL: comments should be cleaned up.

Comment on lines 536 to 543
WOLFSSL_API void wc_CryptoCb_UnRegisterDevice(int devId);
WOLFSSL_API int wc_CryptoCb_UnRegisterDevice(int devId);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly to satisfy Devin's comment on the PR, but maybe we shouldn't break the API to do so.
Thank you

Comment on lines 689 to 709
/**
* \brief Import an AES key into a CryptoCB device (proxy-key mode).
*
* This function allows AES keys to be handled by an external device
* (e.g. Secure Element or HSM) without exposing raw key material to
* wolfCrypt. When supported, the device callback stores the key internally
* and sets an opaque handle in aes->devCtx.
*
* When CryptoCB AES SetKey support is enabled
* (WOLF_CRYPTO_CB_AES_SETKEY), wolfCrypt will route AES-GCM operations
* through the CryptoCB interface and avoid storing key bytes or
* generating GCM tables in software.
*
* \param aes AES context
* \param key Pointer to raw AES key material
* \param keySz Size of key in bytes
*
* \return 0 on success
* \return CRYPTOCB_UNAVAILABLE if device does not support this operation
* \return BAD_FUNC_ARG on invalid parameters
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong place for docs.

Comment on lines +5237 to +5238
static int g_ccAesSetKeyCalled = 0;
static int g_ccAesFreeCalled = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this g_ prefix?

Comment on lines 4832 to 4837
#else
/* ORIGINAL: Copy key to devKey for existing CryptoCB users */
if (keylen > sizeof(aes->devKey)) {
return BAD_FUNC_ARG;
}
XMEMCPY(aes->devKey, userKey, keylen);
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why move this up here instead of allowing aes->devId != INVALID_DEVID section to handle it?

Comment on lines 7585 to 7588
/* ORIGINAL: Copy to devKey */
if (len > sizeof(aes->devKey)) {
return BAD_FUNC_ARG;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

len is already checked on function entry. Why this check here?

Comment on lines -13389 to -13393
ret = wc_CryptoCb_Free(aes->devId, WC_ALGO_TYPE_CIPHER,
WC_CIPHER_AES, (void*)aes);
/* If they want the standard free, they can call it themselves */
/* via their callback setting devId to INVALID_DEVID */
/* otherwise assume the callback handled it */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this comment being removed?

@julek-wolfssl
Copy link
Member

Is it possible to setup a gh action that tests this with wolfHSM? At the very least this new config should be added to os-check.yml.

@sameehj sameehj force-pushed the aes-offload branch 3 times, most recently from 519cd36 to be96866 Compare January 16, 2026 16:26
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>
@sameehj sameehj changed the title cryptocb: add AES proxy-key support and end-to-end CryptoCB AES-GCM t… cryptocb: add AES CryptoCB key import support and tests Jan 16, 2026
@sameehj
Copy link
Contributor Author

sameehj commented Jan 16, 2026

I see "proxy-key" appearing multiple times in the PR. What does it mean? The ORIGINAL: comments should be cleaned up.

Dropped proxy-key terminology
and dropped 'ORIGINAL" comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants