Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/configs/pq-all.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
"comment": "Non-blocking smallest memory signer with the early y and w0 checks and fault hardening: the y check result is carried across yields",
"configure": ["--enable-cryptonly", "--enable-mldsa=yes,small",
"CPPFLAGS=-DWOLFSSL_MLDSA_SIGN_SMALLEST_MEM -DWOLFSSL_MLDSA_VERIFY_SMALLEST_MEM -DWOLFSSL_MLDSA_MAKE_KEY_SMALL_MEM -DWOLFSSL_MLDSA_SIGN_CHECK_Y -DWOLFSSL_MLDSA_SIGN_CHECK_W0 -DWC_MLDSA_FAULT_HARDEN -DWC_MLDSA_NONBLOCK"]},
{"name": "pq-nonblock-tls", "minutes": 5.0,
"comment": "TLS 1.3 driving the non-blocking ML-DSA and ML-KEM code through the software async device: --enable-dilithium=yes,nonblock and --enable-mlkem=yes,nonblock auto-enable asynccrypt-sw, and make check runs test_tls13_pq_nonblock_async (ML-DSA-44 certificate; ML-KEM-768 standalone, SecP256r1MLKEM768 and X25519MLKEM768 key shares, each also resumed with a session ticket) plus the cipher suite PQC tests with the example client and server polling WC_PENDING_E.",
"configure": ["--enable-dilithium=yes,small,nonblock",
"--enable-mlkem=yes,nonblock", "--enable-tls-mlkem-standalone",
"--enable-curve25519", "--enable-session-ticket",
"--enable-experimental",
"CPPFLAGS=-pedantic -Wdeclaration-after-statement -DWOLFSSL_MLDSA_NO_LARGE_CODE"]},
{"name": "pq-nonblock-smallmem-dynkeys", "minutes": 2.5,
"comment": "Non-blocking ML-KEM with the cached matrix A and dynamic key buffers: a resumed keygen must keep the vectors its first slice allocated, and encapsulation must read the cached matrix across yields",
"configure": ["--enable-cryptonly", "--enable-mlkem", "--enable-smallstack",
Expand Down
57 changes: 57 additions & 0 deletions .github/workflows/mlkem-encap-server.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: ML-KEM encapsulate-only server

# START OF COMMON SECTION
on:
push:
branches: [ 'release/**' ]
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
branches: [ '*' ]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

permissions:
contents: read
# END OF COMMON SECTION

jobs:
# A server built with --enable-mlkem=enc has no key generation or
# decapsulation, so no single build can be both ends of the handshake:
# run its example server, blocking and non-blocking, against a full build's
# example client.
interop:
name: ML-KEM encapsulate-only server interop
if: github.repository_owner == 'wolfssl'
runs-on: ubuntu-24.04
timeout-minutes: 10
steps:
- uses: actions/checkout@v5

- name: Build
run: |
./autogen.sh
mkdir full enc enc-nonblock
(cd full && ../configure --enable-mlkem --enable-tls-mlkem-standalone \
--enable-curve25519 --enable-experimental && make -j"$(nproc)")
(cd enc && ../configure --enable-mlkem=enc,768 \
--enable-tls-mlkem-standalone --enable-curve25519 \
--enable-experimental && make -j"$(nproc)")
(cd enc-nonblock && ../configure --enable-mlkem=enc,768,nonblock \
--enable-tls-mlkem-standalone --enable-curve25519 \
--enable-experimental && make -j"$(nproc)")

- name: Full client against encapsulate-only server
run: |
for server in enc enc-nonblock; do
for group in ML_KEM_768 X25519MLKEM768 SecP256r1MLKEM768; do
ready="$PWD/ready.$server.$group"
rm -f "$ready"
./$server/examples/server/server -v 4 -p 11111 -R "$ready" &
pid=$!
for i in $(seq 100); do [ -e "$ready" ] && break; sleep 0.1; done
./full/examples/client/client -v 4 -p 11111 --pqc "$group"
wait "$pid"
done
done
2 changes: 2 additions & 0 deletions .wolfssl_known_macro_extras
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,8 @@ WC_ASYNC_NO_DH
WC_ASYNC_NO_ECC
WC_ASYNC_NO_HMAC
WC_ASYNC_NO_MD5
WC_ASYNC_NO_MLDSA
WC_ASYNC_NO_MLKEM
WC_ASYNC_NO_PKI
WC_ASYNC_NO_RNG
WC_ASYNC_NO_RSA
Expand Down
50 changes: 49 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2553,6 +2553,7 @@ AC_ARG_ENABLE([dilithium],

ENABLED_MLDSA_OPTS=$ENABLED_MLDSA
ENABLED_MLDSA_MAKE_KEY=no
ENABLED_MLDSA_NONBLOCK=no
ENABLED_MLDSA_SIGN=no
ENABLED_MLDSA_VERIFY=no
ENABLED_MLDSA_NO_CTX=no
Expand Down Expand Up @@ -2609,8 +2610,17 @@ do
ENABLED_MLDSA_NO_CTX=yes
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_MLDSA_NO_CTX"
;;
nonblock)
# Resumable operations on the small-memory paths. asynccrypt and
# asynccrypt-sw are auto-enabled earlier in this file so the TLS layer
# drives them a chunk per wolfSSL_AsyncPoll().
ENABLED_MLDSA_NONBLOCK=yes
AM_CFLAGS="$AM_CFLAGS -DWC_MLDSA_NONBLOCK -DWOLFSSL_MLDSA_SIGN_SMALL_MEM"
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_MLDSA_VERIFY_SMALL_MEM"
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_MLDSA_MAKE_KEY_SMALL_MEM"
;;
*)
AC_MSG_ERROR([Invalid choice for ML-DSA [all,make,sign,verify,verify-only,small,44,65,87,no-ctx]: $ENABLED_MLDSA.])
AC_MSG_ERROR([Invalid choice for ML-DSA [all,make,sign,verify,verify-only,small,nonblock,44,65,87,no-ctx]: $ENABLED_MLDSA.])
break;;
esac
done
Expand Down Expand Up @@ -6620,6 +6630,15 @@ then
test -z "$enable_asynccrypt_sw" && enable_asynccrypt_sw=yes
fi

# Handle ML-DSA/ML-KEM nonblock the same way: --enable-mldsa=yes,nonblock and
# --enable-mlkem=yes,nonblock run the operations a chunk per poll.
case ",$ENABLED_MLDSA,$ENABLED_MLKEM," in
*,nonblock,*)
test -z "$enable_asynccrypt" && enable_asynccrypt=yes
test -z "$enable_asynccrypt_sw" && enable_asynccrypt_sw=yes
;;
esac

if test "$ENABLED_CURVE25519" = "no" && test "$ENABLED_QUIC" = "yes" && test "$ENABLED_FIPS" = "no"
then
ENABLED_CURVE25519=yes
Expand Down Expand Up @@ -8801,6 +8820,7 @@ fi

ENABLED_ML_KEM=unset
ENABLED_MLKEM_MAKE_KEY=no
ENABLED_MLKEM_NONBLOCK=no
ENABLED_MLKEM_ENCAPSULATE=no
ENABLED_MLKEM_DECAPSULATE=no
for v in `echo $ENABLED_MLKEM | tr "," " "`
Expand Down Expand Up @@ -8859,6 +8879,14 @@ do
noasm)
AM_CFLAGS="$AM_CFLAGS -DWC_MLKEM_NO_ASM"
;;
nonblock)
# Resumable operations on the small-memory paths. asynccrypt and
# asynccrypt-sw are auto-enabled earlier in this file so the TLS layer
# drives them a chunk per wolfSSL_AsyncPoll().
ENABLED_MLKEM_NONBLOCK=yes
AM_CFLAGS="$AM_CFLAGS -DWC_MLKEM_NONBLOCK -DWOLFSSL_MLKEM_MAKEKEY_SMALL_MEM"
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_MLKEM_ENCAPSULATE_SMALL_MEM"
;;
*)
AC_MSG_ERROR([Invalid choice for MLKEM []: $ENABLED_MLKEM.])
break;;
Expand All @@ -8883,6 +8911,18 @@ then
ENABLED_MLKEM_DECAPSULATE=yes
fi

# FIPS v7 runs a pairwise consistency test on key generation, which the
# nonblock key generation does not support.
if test "$ENABLED_FIPS" != "no" && test "${HAVE_FIPS_VERSION_MAJOR:-0}" -ge 7
then
AS_IF([test "$ENABLED_MLDSA_NONBLOCK" = "yes" &&
test "$ENABLED_MLDSA_MAKE_KEY" = "yes"],
[AC_MSG_ERROR([ML-DSA nonblock with key generation is not available in FIPS v7 builds.])])
AS_IF([test "$ENABLED_MLKEM_NONBLOCK" = "yes" &&
test "$ENABLED_MLKEM_MAKE_KEY" = "yes"],
[AC_MSG_ERROR([ML-KEM nonblock with key generation is not available in FIPS v7 builds.])])
fi

# Selftest uses its own random.c which doesn't support SHA-512 DRBG
# or runtime DRBG disable/enable APIs
AS_IF([test "x$ENABLED_SELFTEST" = "xyes"],
Expand Down Expand Up @@ -12771,6 +12811,14 @@ then
fi
fi

# WC_ASYNC_DEV holds one backend, so the software simulator (also enabled by
# the nonblock options) cannot be combined with Intel QAT or Cavium.
if test "x$ENABLED_ASYNCCRYPT_SW" = "xyes" &&
{ test "x$ENABLED_INTEL_QA" = "xyes" || test "x$ENABLED_CAVIUM" = "xyes"; }
then
AC_MSG_ERROR([asynccrypt-sw (or a nonblock option) cannot be combined with Intel QAT or Cavium])
fi

# Crypto callbacks with async crypt cannot complete TLS 1.2 record ciphers
# unless WOLF_CRYPTO_CB_ASYNC_POLL is defined (TLS 1.3 resumes them by
# re-invoking the callback). Report it once here and silence the
Expand Down
160 changes: 158 additions & 2 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -7712,6 +7712,147 @@ int Ed448Verify(WOLFSSL* ssl, const byte* in, word32 inSz, const byte* msg,
}
#endif /* HAVE_ED448 */

#if defined(WOLFSSL_HAVE_MLDSA) && defined(WC_MLDSA_NONBLOCK) && \
defined(WOLFSSL_ASYNC_CRYPT_SW) && defined(WC_ASYNC_ENABLE_MLDSA)
#define WOLFSSL_MLDSA_TLS_NONBLOCK
/* Attach a non-blocking context for one operation when an async device is
* active. Not at key allocation: a key import runs the same resumable code
* and cannot pend. */
static int MlDsaNbAttach(WOLFSSL* ssl, wc_MlDsaKey* key)
{
int ret = 0;

if (ssl->devId != INVALID_DEVID && key->nb == NULL &&
key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_MLDSA) {
wc_MlDsaNonBlock* nb = (wc_MlDsaNonBlock*)XMALLOC(
sizeof(wc_MlDsaNonBlock), ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
if (nb == NULL) {
ret = MEMORY_E;
}
else {
ret = wc_MlDsaKey_set_nonblock(key, nb);
if (ret != 0) {
XFREE(nb, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
}
}
}
return ret;
}

static void MlDsaNbDetach(WOLFSSL* ssl, wc_MlDsaKey* key)
{
wc_MlDsaNonBlock* nb = key->nb;

if (nb != NULL) {
(void)wc_MlDsaKey_set_nonblock(key, NULL);
XFREE(nb, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
}
}
/* Still attached on entry: the operation pended and TLS re-entered after the
* poll finished it, so its output is already in place. */
#define MlDsaNbDone(key) ((key)->nb != NULL)
#else
#define MlDsaNbAttach(ssl, key) ((void)(ssl), (void)(key), 0)
#define MlDsaNbDetach(ssl, key) do { (void)(ssl); (void)(key); } while (0)
#define MlDsaNbDone(key) 0
#endif

#if defined(WOLFSSL_HAVE_MLDSA) && !defined(WOLFSSL_MLDSA_NO_SIGN)
int MlDsaSign(WOLFSSL* ssl, const byte* in, word32 inSz, byte* out,
word32* outSz, wc_MlDsaKey* key)
{
int ret;

WOLFSSL_ENTER("MlDsaSign");

if (MlDsaNbDone(key)) {
MlDsaNbDetach(ssl, key);
WOLFSSL_LEAVE("MlDsaSign", 0);
return 0;
}

ret = MlDsaNbAttach(ssl, key);
if (ret != 0)
return ret;
#ifdef WOLFSSL_MLDSA_TLS_NONBLOCK
/* initialize event */
ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
if (ret != 0) {
MlDsaNbDetach(ssl, key);
return ret;
}
#endif

ret = wc_MlDsaKey_SignCtx(key, NULL, 0, out, outSz, in, inSz, ssl->rng);

/* Handle async pending response */
#ifdef WOLFSSL_MLDSA_TLS_NONBLOCK
if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) {
ret = wolfSSL_AsyncPush(ssl, &key->asyncDev);
}
else
#endif /* WOLFSSL_MLDSA_TLS_NONBLOCK */
{
MlDsaNbDetach(ssl, key);
}

WOLFSSL_LEAVE("MlDsaSign", ret);

return ret;
}
#endif /* WOLFSSL_HAVE_MLDSA && !WOLFSSL_MLDSA_NO_SIGN */

#if defined(WOLFSSL_HAVE_MLDSA) && !defined(WOLFSSL_MLDSA_NO_VERIFY)
/* Returns 0 when the signature verifies, SIG_VERIFY_E when it does not. */
int MlDsaVerify(WOLFSSL* ssl, const byte* in, word32 inSz, const byte* msg,
word32 msgSz, wc_MlDsaKey* key)
{
int ret;

WOLFSSL_ENTER("MlDsaVerify");

if (MlDsaNbDone(key)) {
/* The poll stored the result in ssl->eccVerifyRes. */
ret = 0;
}
else {
ret = MlDsaNbAttach(ssl, key);
if (ret != 0)
return ret;
#ifdef WOLFSSL_MLDSA_TLS_NONBLOCK
/* initialize event */
ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
if (ret != 0) {
MlDsaNbDetach(ssl, key);
return ret;
}
#endif

ret = wc_MlDsaKey_VerifyCtx(key, in, inSz, NULL, 0, msg, msgSz,
&ssl->eccVerifyRes);

/* Handle async pending response */
#ifdef WOLFSSL_MLDSA_TLS_NONBLOCK
if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) {
ret = wolfSSL_AsyncPush(ssl, &key->asyncDev);
WOLFSSL_LEAVE("MlDsaVerify", ret);
return ret;
}
#endif
}

MlDsaNbDetach(ssl, key);
if ((ret == 0) && (ssl->eccVerifyRes != 1)) {
ret = SIG_VERIFY_E;
WOLFSSL_ERROR_VERBOSE(ret);
}

WOLFSSL_LEAVE("MlDsaVerify", ret);

return ret;
}
#endif /* WOLFSSL_HAVE_MLDSA && !WOLFSSL_MLDSA_NO_VERIFY */

#ifndef WOLFSSL_NO_TLS12

#ifdef HAVE_CURVE448
Expand Down Expand Up @@ -9800,7 +9941,18 @@ void FreeKey(WOLFSSL* ssl, int type, void** pKey)
#endif /* HAVE_FALCON */
#if defined(WOLFSSL_HAVE_MLDSA)
case DYNAMIC_TYPE_MLDSA:
#if defined(WC_MLDSA_NONBLOCK) && \
defined(WOLFSSL_ASYNC_CRYPT_SW) && \
defined(WC_ASYNC_ENABLE_MLDSA)
{
/* Free releases its scratch; the context is ours. */
wc_MlDsaNonBlock* nb = ((wc_MlDsaKey*)*pKey)->nb;
wc_MlDsaKey_Free((wc_MlDsaKey*)*pKey);
XFREE(nb, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
}
#else
wc_MlDsaKey_Free((wc_MlDsaKey*)*pKey);
#endif
break;
#endif /* WOLFSSL_HAVE_MLDSA */
#if defined(WOLFSSL_HAVE_SLHDSA)
Expand Down Expand Up @@ -10247,7 +10399,7 @@ static int ReuseKey(WOLFSSL* ssl, int type, void* pKey)
#if defined(WOLFSSL_HAVE_MLDSA)
case DYNAMIC_TYPE_MLDSA:
wc_MlDsaKey_Free((wc_MlDsaKey*)pKey);
ret = wc_MlDsaKey_Init((wc_MlDsaKey*)pKey, NULL, INVALID_DEVID);
ret = wc_MlDsaKey_Init((wc_MlDsaKey*)pKey, ssl->heap, ssl->devId);
break;
#endif /* WOLFSSL_HAVE_MLDSA */
#if defined(WOLFSSL_HAVE_SLHDSA)
Expand Down Expand Up @@ -10343,7 +10495,7 @@ void FreeKeyExchange(WOLFSSL* ssl)

#if defined(WOLFSSL_ASYNC_CRYPT) && \
(!defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \
defined(HAVE_ED448))
defined(HAVE_ED448) || defined(WOLFSSL_HAVE_MLDSA))
static void DetachPendingAsyncDev(WOLFSSL* ssl, WC_ASYNC_DEV* asyncDev)
{
if (ssl == NULL || asyncDev == NULL)
Expand Down Expand Up @@ -10406,6 +10558,10 @@ void FreePeerCertKeys(WOLFSSL* ssl)
FreeKey(ssl, DYNAMIC_TYPE_FALCON, (void**)&ssl->peerFalconKey);
#endif /* HAVE_FALCON */
#if defined(WOLFSSL_HAVE_MLDSA)
#ifdef WOLFSSL_ASYNC_CRYPT
if (ssl->peerMlDsaKey != NULL)
DetachPendingAsyncDev(ssl, &ssl->peerMlDsaKey->asyncDev);
#endif
ssl->peerMlDsaKeyPresent = 0;
FreeKey(ssl, DYNAMIC_TYPE_MLDSA, (void**)&ssl->peerMlDsaKey);
#endif /* WOLFSSL_HAVE_MLDSA */
Expand Down
Loading
Loading