Skip to content

Commit 0dffc8a

Browse files
committed
Fixes for async and crypto callbacks
1 parent 41137ee commit 0dffc8a

6 files changed

Lines changed: 37 additions & 11 deletions

File tree

examples/async/async_client.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,10 @@ int client_async_test(int argc, char** argv)
286286
}
287287
#endif
288288
#ifdef WOLF_CRYPTO_CB
289+
/* Crypto callbacks require a valid devId. When no hardware async driver
290+
* sets one (e.g. Cavium/Intel QA/SW), assign one explicitly. */
291+
if (devId == INVALID_DEVID)
292+
devId = 1;
289293
XMEMSET(&cryptoCbCtx, 0, sizeof(cryptoCbCtx));
290294
if (wc_CryptoCb_RegisterDevice(devId, AsyncTlsCryptoCb, &cryptoCbCtx) != 0) {
291295
fprintf(stderr, "ERROR: wc_CryptoCb_RegisterDevice failed\n");
@@ -497,6 +501,8 @@ int client_async_test(int argc, char** argv)
497501
}
498502
continue;
499503
}
504+
fprintf(stderr, "ERROR: wolfSSL_write failed: %d (%s)\n",
505+
err, wolfSSL_ERR_reason_error_string(err));
500506
goto out;
501507
}
502508

@@ -529,6 +535,8 @@ int client_async_test(int argc, char** argv)
529535
}
530536
continue;
531537
}
538+
fprintf(stderr, "ERROR: wolfSSL_read failed: %d (%s)\n",
539+
err, wolfSSL_ERR_reason_error_string(err));
532540
goto out;
533541
}
534542

examples/async/async_server.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ int server_async_test(int argc, char** argv)
291291
}
292292
#endif
293293
#ifdef WOLF_CRYPTO_CB
294+
/* Crypto callbacks require a valid devId. When no hardware async driver
295+
* sets one (e.g. Cavium/Intel QA/SW), assign one explicitly. */
296+
if (devId == INVALID_DEVID)
297+
devId = 1;
294298
XMEMSET(&cryptoCbCtx, 0, sizeof(cryptoCbCtx));
295299
if (wc_CryptoCb_RegisterDevice(devId, AsyncTlsCryptoCb, &cryptoCbCtx) != 0) {
296300
fprintf(stderr, "ERROR: wc_CryptoCb_RegisterDevice failed\n");
@@ -526,6 +530,8 @@ int server_async_test(int argc, char** argv)
526530
}
527531
continue;
528532
}
533+
fprintf(stderr, "ERROR: wolfSSL_read failed: %d (%s)\n",
534+
err, wolfSSL_ERR_reason_error_string(err));
529535
goto exit;
530536
}
531537

examples/async/async_tls.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,17 @@ int AsyncTlsCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
178178

179179
if (info->algo_type == WC_ALGO_TYPE_PK) {
180180
#ifdef WOLFSSL_ASYNC_CRYPT
181-
/* Test pending response */
181+
/* Simulate async pending for signing only.
182+
* This matches a typical hardware crypto scenario (e.g., TPM) where
183+
* only signing is offloaded to hardware. Keygen, verify, and ECDH
184+
* are performed synchronously in software.
185+
* Note: WOLFSSL_ASYNC_CRYPT + WOLF_CRYPTO_CB pending simulation
186+
* requires operations whose TLS state machines properly handle retry
187+
* via wolfSSL_AsyncPop. ECC keygen in TLSX_KeyShare_GenEccKey does
188+
* not support this because the keygen call is inside the key
189+
* allocation guard (kse->key == NULL) which is skipped on retry. */
182190
if (info->pk.type == WC_PK_TYPE_RSA ||
183-
info->pk.type == WC_PK_TYPE_EC_KEYGEN ||
184-
info->pk.type == WC_PK_TYPE_ECDSA_SIGN ||
185-
info->pk.type == WC_PK_TYPE_ECDSA_VERIFY ||
186-
info->pk.type == WC_PK_TYPE_ECDH)
191+
info->pk.type == WC_PK_TYPE_ECDSA_SIGN)
187192
{
188193
if (myCtx->pendingCount++ < TEST_PEND_COUNT) return WC_PENDING_E;
189194
myCtx->pendingCount = 0;

src/internal.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41789,7 +41789,10 @@ int wolfSSL_AsyncPop(WOLFSSL* ssl, byte* state)
4178941789
* the completion is not detected in the poll like Intel QAT or
4179041790
* Nitrox */
4179141791
ret = wolfEventQueue_Remove(&ssl->ctx->event_queue, event);
41792-
41792+
/* Clear async device so stale pending state from
41793+
* wolfSSL_AsyncInit does not confuse subsequent operations */
41794+
XMEMSET(&asyncDev->event, 0, sizeof(WOLF_EVENT));
41795+
ssl->asyncDev = NULL;
4179341796
}
4179441797
#endif
4179541798
}

wolfcrypt/src/async.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,11 @@ int wolfAsync_EventQueuePoll(WOLF_EVENT_QUEUE* queue, void* context_filter,
705705
event->ret = wolfAsync_DoSw(asyncDev);
706706
}
707707
#elif defined(WOLF_CRYPTO_CB) || defined(HAVE_PK_CALLBACKS)
708-
/* Use crypto or PK callbacks */
708+
/* Crypto/PK callbacks manage their own retry state.
709+
* Leave event->ret as WC_PENDING_E so that
710+
* wolfSSL_AsyncPop can detect the pending state and
711+
* remove the event, allowing the operation to be
712+
* retried with a fresh callback invocation. */
709713

710714
#else
711715
#warning No async crypt device defined!

wolfcrypt/src/cryptocb.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ void wc_CryptoCb_InfoString(wc_CryptoInfo* info)
219219
printf("Crypto CB: %s %s (%d) (%p ctx)\n",
220220
GetAlgoTypeStr(info->algo_type),
221221
GetCipherTypeStr(info->cipher.type),
222-
info->cipher.type, info->cipher.ctx);
222+
info->cipher.type, (void*)info->cipher.ctx);
223223
}
224224
#endif /* !NO_AES || !NO_DES3 */
225225
#if !defined(NO_SHA) || !defined(NO_SHA256) || \
@@ -228,7 +228,7 @@ void wc_CryptoCb_InfoString(wc_CryptoInfo* info)
228228
printf("Crypto CB: %s %s (%d) (%p ctx) %s\n",
229229
GetAlgoTypeStr(info->algo_type),
230230
GetHashTypeStr(info->hash.type),
231-
info->hash.type, info->hash.ctx,
231+
info->hash.type, (void*)info->hash.ctx,
232232
(info->hash.in != NULL) ? "Update" : "Final");
233233
}
234234
#endif
@@ -237,7 +237,7 @@ void wc_CryptoCb_InfoString(wc_CryptoInfo* info)
237237
printf("Crypto CB: %s %s (%d) (%p ctx) %s\n",
238238
GetAlgoTypeStr(info->algo_type),
239239
GetHashTypeStr(info->hmac.macType),
240-
info->hmac.macType, info->hmac.hmac,
240+
info->hmac.macType, (void*)info->hmac.hmac,
241241
(info->hmac.in != NULL) ? "Update" : "Final");
242242
}
243243
#endif
@@ -246,7 +246,7 @@ void wc_CryptoCb_InfoString(wc_CryptoInfo* info)
246246
printf("Crypto CB: %s %s (%d) (%p ctx) %s %s %s\n",
247247
GetAlgoTypeStr(info->algo_type),
248248
GetCmacTypeStr(info->cmac.type),
249-
info->cmac.type, info->cmac.cmac,
249+
info->cmac.type, (void*)info->cmac.cmac,
250250
(info->cmac.key != NULL) ? "Init " : "",
251251
(info->cmac.in != NULL) ? "Update " : "",
252252
(info->cmac.out != NULL) ? "Final" : "");

0 commit comments

Comments
 (0)