Skip to content

Commit 2a18b7e

Browse files
dgarskeclaude
andcommitted
Fix non-blocking X25519/ECC with WOLFSSL_ASYNC_CRYPT_SW
The non-blocking setup for X25519 and ECC in TLS was unconditionally setting up nbCtx, which caused functions to return FP_WOULDBLOCK. However, with INVALID_DEVID (the default), TLS has no async loop to handle FP_WOULDBLOCK, only WC_PENDING_E via the async framework. The fix follows the pattern used in asn.c: only set up nbCtx when the async device is active (devId != INVALID_DEVID). With INVALID_DEVID, the code now uses the blocking fallback (WC_ECC_NONBLOCK_ONLY) instead. This prevents unit test timeouts when built with --enable-curve25519=nonblock or --enable-ecc=nonblock. Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
1 parent 19bb719 commit 2a18b7e

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/internal.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8338,7 +8338,10 @@ int AllocKey(WOLFSSL* ssl, int type, void** pKey)
83388338
ret = wc_ecc_init_ex(eccKey, ssl->heap, ssl->devId);
83398339
#if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_ASYNC_CRYPT_SW) && \
83408340
defined(WC_ASYNC_ENABLE_ECC)
8341-
if (ret == 0) {
8341+
/* Only set non-blocking context when async device is active. With
8342+
* INVALID_DEVID there is no async loop to retry on FP_WOULDBLOCK, so
8343+
* let the WC_ECC_NONBLOCK_ONLY blocking fallback handle it instead. */
8344+
if (ret == 0 && ssl->devId != INVALID_DEVID) {
83428345
eccNbCtx = (ecc_nb_ctx_t*)XMALLOC(sizeof(ecc_nb_ctx_t),
83438346
eccKey->heap, DYNAMIC_TYPE_TMP_BUFFER);
83448347
if (eccNbCtx == NULL) {
@@ -8365,8 +8368,12 @@ int AllocKey(WOLFSSL* ssl, int type, void** pKey)
83658368
case DYNAMIC_TYPE_CURVE25519:
83668369
x25519Key = (curve25519_key*)*pKey;
83678370
ret = wc_curve25519_init_ex(x25519Key, ssl->heap, ssl->devId);
8368-
#if defined(WC_X25519_NONBLOCK) && defined(WOLFSSL_ASYNC_CRYPT_SW)
8369-
if (ret == 0) {
8371+
#if defined(WC_X25519_NONBLOCK) && defined(WOLFSSL_ASYNC_CRYPT_SW) && \
8372+
defined(WC_ASYNC_ENABLE_X25519)
8373+
/* Only set non-blocking context when async device is active. With
8374+
* INVALID_DEVID there is no async loop to retry on FP_WOULDBLOCK, so
8375+
* skip non-blocking setup and use blocking mode instead. */
8376+
if (ret == 0 && ssl->devId != INVALID_DEVID) {
83708377
x25519NbCtx = (x25519_nb_ctx_t*)XMALLOC(sizeof(x25519_nb_ctx_t),
83718378
ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
83728379
if (x25519NbCtx == NULL) {
@@ -8379,7 +8386,8 @@ int AllocKey(WOLFSSL* ssl, int type, void** pKey)
83798386
}
83808387
}
83818388
}
8382-
#endif /* WC_X25519_NONBLOCK && WOLFSSL_ASYNC_CRYPT_SW */
8389+
#endif /* WC_X25519_NONBLOCK && WOLFSSL_ASYNC_CRYPT_SW &&
8390+
WC_ASYNC_ENABLE_X25519 */
83838391
break;
83848392
#endif /* HAVE_CURVE25519 */
83858393
#ifdef HAVE_ED448

src/tls.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8008,8 +8008,12 @@ static int TLSX_KeyShare_GenX25519Key(WOLFSSL *ssl, KeyShareEntry* kse)
80088008
key = (curve25519_key*)kse->key;
80098009
kse->keyLen = CURVE25519_KEYSIZE;
80108010
}
8011-
#if defined(WC_X25519_NONBLOCK) && defined(WOLFSSL_ASYNC_CRYPT_SW)
8012-
if (ret == 0) {
8011+
#if defined(WC_X25519_NONBLOCK) && defined(WOLFSSL_ASYNC_CRYPT_SW) && \
8012+
defined(WC_ASYNC_ENABLE_X25519)
8013+
/* Only set non-blocking context when async device is active. With
8014+
* INVALID_DEVID there is no async loop to retry on FP_WOULDBLOCK, so
8015+
* skip non-blocking setup and use blocking mode instead. */
8016+
if (ret == 0 && ssl->devId != INVALID_DEVID) {
80138017
x25519_nb_ctx_t* nbCtx = (x25519_nb_ctx_t*)XMALLOC(
80148018
sizeof(x25519_nb_ctx_t), ssl->heap,
80158019
DYNAMIC_TYPE_TMP_BUFFER);
@@ -8018,9 +8022,13 @@ static int TLSX_KeyShare_GenX25519Key(WOLFSSL *ssl, KeyShareEntry* kse)
80188022
}
80198023
else {
80208024
ret = wc_curve25519_set_nonblock(key, nbCtx);
8025+
if (ret != 0) {
8026+
XFREE(nbCtx, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
8027+
}
80218028
}
80228029
}
8023-
#endif /* WC_X25519_NONBLOCK && WOLFSSL_ASYNC_CRYPT_SW */
8030+
#endif /* WC_X25519_NONBLOCK && WOLFSSL_ASYNC_CRYPT_SW &&
8031+
WC_ASYNC_ENABLE_X25519 */
80248032
if (ret == 0) {
80258033
#ifdef WOLFSSL_STATIC_EPHEMERAL
80268034
ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_CURVE25519, kse->key);

0 commit comments

Comments
 (0)