Skip to content

Commit 912dac8

Browse files
committed
fixes
Signed-off-by: Sameeh Jubran <sameeh@wolfssl.com>
1 parent cdc15fd commit 912dac8

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

doc/dox_comments/header_files/cryptocb.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,20 +195,22 @@ void wc_CryptoCb_InfoString(wc_CryptoInfo* info);
195195
through the CryptoCB interface.
196196
197197
**TLS Builds (Default):**
198-
- Key bytes ARE stored in wolfCrypt memory for fallback
198+
- Key bytes ARE stored in wolfCrypt memory (devKey) for fallback
199199
- GCM tables ARE generated for software fallback
200200
- Provides hardware acceleration with automatic fallback
201+
- Even when WC_CRYPTOCB_AES_GCM is set, tables are generated for safety
201202
202203
**Crypto-Only Builds (--disable-tls):**
203-
- Key bytes NOT stored (true key isolation)
204+
- Key bytes NOT stored in wolfCrypt memory (true key isolation)
204205
- GCM tables skipped when WC_CRYPTOCB_AES_GCM is set
205-
- True hardware offload
206+
- True hardware offload - callback must handle all GCM operations
206207
207208
The callback declares its capabilities by setting flags in the
208209
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.
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.
212214
213215
\param aes AES context
214216
\param key Pointer to raw AES key material

wolfcrypt/src/aes.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4379,6 +4379,10 @@ static WARN_UNUSED_RESULT int wc_AesDecrypt(
43794379

43804380
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
43814381
if (ret != 0) {
4382+
/* Clear devCtx on error to prevent resource leak */
4383+
if (aes->devCtx != NULL) {
4384+
aes->devCtx = NULL;
4385+
}
43824386
return ret;
43834387
}
43844388

@@ -4388,7 +4392,12 @@ static WARN_UNUSED_RESULT int wc_AesDecrypt(
43884392

43894393
#ifdef WOLFSSL_AES_GCM_TLS_SAFE
43904394
WOLFSSL_MSG("CryptoCB AES-GCM declared but TLS-safe fallback retained");
4391-
/* TLS-safe: continue to software path for fallback */
4395+
/* TLS-safe: copy key to devKey for fallback, then continue to software path */
4396+
if (keylen > sizeof(aes->devKey)) {
4397+
aes->devCtx = NULL; /* Clear devCtx on error */
4398+
return BAD_FUNC_ARG;
4399+
}
4400+
XMEMCPY(aes->devKey, userKey, keylen);
43924401
goto cryptocb_aes_setkey_fallback;
43934402
#else
43944403
/* Set IV if provided */
@@ -4842,6 +4851,10 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)
48424851

48434852
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
48444853
if (ret != 0) {
4854+
/* Clear devCtx on error to prevent resource leak */
4855+
if (aes->devCtx != NULL) {
4856+
aes->devCtx = NULL;
4857+
}
48454858
return ret;
48464859
}
48474860

@@ -4851,8 +4864,13 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)
48514864

48524865
#ifdef WOLFSSL_AES_GCM_TLS_SAFE
48534866
WOLFSSL_MSG("CryptoCB AES-GCM declared but TLS-safe fallback retained");
4854-
/* TLS-safe: continue to software path for fallback */
4855-
goto cryptocb_aes_setkey_fallback2;
4867+
/* TLS-safe: copy key to devKey for fallback, then continue to software path */
4868+
if (keylen > sizeof(aes->devKey)) {
4869+
aes->devCtx = NULL; /* Clear devCtx on error */
4870+
return BAD_FUNC_ARG;
4871+
}
4872+
XMEMCPY(aes->devKey, userKey, keylen);
4873+
goto cryptocb_aes_setkey_fallback;
48564874
#else
48574875
WOLFSSL_MSG("CryptoCB AES-GCM full offload active (crypto-only build)");
48584876
/* Set IV if provided */
@@ -4868,7 +4886,7 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)
48684886
/* Key-import-only or partial support: fallback to software */
48694887
}
48704888
/* CRYPTOCB_UNAVAILABLE or TLS-safe fallback: continue to software */
4871-
cryptocb_aes_setkey_fallback2: (void)0;
4889+
cryptocb_aes_setkey_fallback: (void)0;
48724890
#else
48734891
/* Copy key to devKey for standard CryptoCB path */
48744892
XMEMCPY(aes->devKey, userKey, keylen);

0 commit comments

Comments
 (0)