diff --git a/.github/workflows/sanitizers.yml b/.github/workflows/sanitizers.yml index 335d9950..3a1d8448 100644 --- a/.github/workflows/sanitizers.yml +++ b/.github/workflows/sanitizers.yml @@ -187,9 +187,24 @@ jobs: export WOLFPROV_CONFIG_CFLAGS="${SAN_FLAGS}" export WOLFPROV_SKIP_TEST=1 + # Run the PQC tests (incl. the ML-KEM/ML-DSA shared-key concurrency + # tests) under TSan on legs that meet the PQC floor: wolfSSL master + # or v5.9.2-stable+, the same check build-wolfprovider.sh applies. + # Older back-compat legs (v5.8.4-stable) build without PQC. + WOLFSSL_REF="${{ matrix.wolfssl_ref }}" + PQC_ARGS="--enable-pqc" + case "$WOLFSSL_REF" in + v*-stable) + if [ "$(printf '%s\n%s\n' v5.9.2-stable "$WOLFSSL_REF" \ + | sort -V | head -n1)" != "v5.9.2-stable" ]; then + PQC_ARGS="" + fi + ;; + esac + OPENSSL_TAG=${{ needs.discover_versions.outputs.openssl_latest_ref }} \ WOLFSSL_TAG=${{ matrix.wolfssl_ref }} \ - ./scripts/build-wolfprovider.sh + ./scripts/build-wolfprovider.sh $PQC_ARGS - name: Push build dependencies uses: ./.github/actions/oras-build-deps-push diff --git a/include/wolfprovider/alg_funcs.h b/include/wolfprovider/alg_funcs.h index b7a73a2b..2e164084 100644 --- a/include/wolfprovider/alg_funcs.h +++ b/include/wolfprovider/alg_funcs.h @@ -279,6 +279,7 @@ typedef struct wp_MlKemData wp_MlKemData; int wp_mlkem_up_ref(wp_MlKem* mlkem); void wp_mlkem_free(wp_MlKem* mlkem); void* wp_mlkem_get_key(wp_MlKem* mlkem); +wolfSSL_Mutex* wp_mlkem_get_mutex(wp_MlKem* mlkem); const wp_MlKemData* wp_mlkem_get_data(const wp_MlKem* mlkem); word32 wp_mlkem_data_ct_size(const wp_MlKemData* data); /* ML-KEM shared secret size is a FIPS 203 constant (32 bytes) independent @@ -322,6 +323,7 @@ typedef struct wp_MlxData { int wp_mlx_up_ref(wp_Mlx* mlx); void wp_mlx_free(wp_Mlx* mlx); void* wp_mlx_get_mlkem_key(wp_Mlx* mlx); +wolfSSL_Mutex* wp_mlx_get_mutex(wp_Mlx* mlx); void* wp_mlx_get_classical_key(wp_Mlx* mlx); const wp_MlxData* wp_mlx_get_data(const wp_Mlx* mlx); int wp_mlx_has_pub(const wp_Mlx* mlx); @@ -333,6 +335,7 @@ typedef struct wp_MlDsa wp_MlDsa; int wp_mldsa_up_ref(wp_MlDsa* mldsa); void wp_mldsa_free(wp_MlDsa* mldsa); void* wp_mldsa_get_key(wp_MlDsa* mldsa); +wolfSSL_Mutex* wp_mldsa_get_mutex(wp_MlDsa* mldsa); int wp_mldsa_get_sig_size(const wp_MlDsa* mldsa); int wp_mldsa_has_private(const wp_MlDsa* mldsa); int wp_mldsa_get_level(wp_MlDsa* mldsa); diff --git a/src/wp_mldsa_kmgmt.c b/src/wp_mldsa_kmgmt.c index b5a3ba5b..0c6d09b3 100644 --- a/src/wp_mldsa_kmgmt.c +++ b/src/wp_mldsa_kmgmt.c @@ -65,7 +65,8 @@ struct wp_MlDsa { const wp_MlDsaData* data; #ifndef WP_SINGLE_THREADED - /** Mutex for reference count updating. */ + /** Mutex for reference count updating and for signing and verifying: + * wc_MlDsaKey holds SHAKE state that those operations update. */ wolfSSL_Mutex mutex; #endif /** Count of references to this object. */ @@ -176,6 +177,26 @@ void* wp_mldsa_get_key(wp_MlDsa* mldsa) return &mldsa->key; } +/** + * Get the mutex from the wp_MlDsa object. + * + * Hold it around every wolfCrypt sign and verify call on the key. wc_MlDsaKey + * carries SHAKE state that those operations update, and OpenSSL lets one key + * be used by several threads at once. + * + * @param [in] mldsa ML-DSA key object. + * @return Pointer to wolfSSL mutex object, NULL in single-threaded builds. + */ +wolfSSL_Mutex* wp_mldsa_get_mutex(wp_MlDsa* mldsa) +{ +#ifndef WP_SINGLE_THREADED + return &mldsa->mutex; +#else + (void)mldsa; + return NULL; +#endif +} + /** * Get the ML-DSA parameter level (WC_ML_DSA_44/65/87) for the key. * diff --git a/src/wp_mldsa_sig.c b/src/wp_mldsa_sig.c index 928960ce..5c1597fb 100644 --- a/src/wp_mldsa_sig.c +++ b/src/wp_mldsa_sig.c @@ -405,6 +405,7 @@ static int wp_mldsa_sign(wp_MlDsaSigCtx* ctx, unsigned char* sig, { int ok = 1; int rc; + int locked = 0; word32 sigSz; /* FIPS 204 permits an empty message; give wolfSSL a valid pointer so a * NULL+0 message does not become a backend-dependent NULL deref. */ @@ -460,6 +461,16 @@ static int wp_mldsa_sign(wp_MlDsaSigCtx* ctx, unsigned char* sig, ok = 0; } } + /* The key may be shared by other contexts and threads, and wolfCrypt + * updates SHAKE state inside it: hold the key mutex. */ + if (ok) { + if (wp_lock(wp_mldsa_get_mutex(ctx->mldsa)) != 1) { + ok = 0; + } + else { + locked = 1; + } + } if (ok && ctx->mu) { /* External-mu mode: only a seeded signer exists; the message is * the 64-byte mu. */ @@ -485,6 +496,9 @@ static int wp_mldsa_sign(wp_MlDsaSigCtx* ctx, unsigned char* sig, ok = 0; } } + if (locked) { + wp_unlock(wp_mldsa_get_mutex(ctx->mldsa)); + } if (ok) { *sigLen = outLen; } @@ -535,6 +549,12 @@ static int wp_mldsa_verify(wp_MlDsaSigCtx* ctx, const unsigned char* sig, return 0; } + /* The key may be shared by other contexts and threads, and wolfCrypt + * updates SHAKE state inside it: hold the key mutex. */ + if (wp_lock(wp_mldsa_get_mutex(ctx->mldsa)) != 1) { + WOLFPROV_LEAVE(WP_LOG_COMP_PQC, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), 0); + return 0; + } /* Match the sign path: external-mu mode or pure ML-DSA with context. */ if (ctx->mu) { rc = wc_MlDsaKey_VerifyMu( @@ -546,6 +566,7 @@ static int wp_mldsa_verify(wp_MlDsaSigCtx* ctx, const unsigned char* sig, (wc_MlDsaKey*)wp_mldsa_get_key(ctx->mldsa), sig, (word32)sigLen, ctx->context, (byte)ctx->contextLen, m, (word32)msgLen, &res); } + wp_unlock(wp_mldsa_get_mutex(ctx->mldsa)); if ((rc != 0) || (res != 1)) { ok = 0; } @@ -721,9 +742,15 @@ static int wp_mldsa_stream_sign_final(wp_MlDsaSigCtx* ctx, unsigned char* sig, if (wp_mldsa_fill_rnd(ctx, rnd) != 0) { ok = 0; } + /* The key may be shared by other contexts and threads, and wolfCrypt + * updates SHAKE state inside it: hold the key mutex. */ + if (ok && (wp_lock(wp_mldsa_get_mutex(ctx->mldsa)) != 1)) { + ok = 0; + } if (ok) { rc = wc_MlDsaKey_SignMuWithSeed(key, sig, &outLen, mu, MLDSA_MU_SZ, rnd); + wp_unlock(wp_mldsa_get_mutex(ctx->mldsa)); if (rc != 0) { ok = 0; } @@ -753,10 +780,16 @@ static int wp_mldsa_stream_verify_final(wp_MlDsaSigCtx* ctx, if (ok && (wc_Shake256_Final(&ctx->muShake, mu, MLDSA_MU_SZ) != 0)) { ok = 0; } + /* The key may be shared by other contexts and threads, and wolfCrypt + * updates SHAKE state inside it: hold the key mutex. */ + if (ok && (wp_lock(wp_mldsa_get_mutex(ctx->mldsa)) != 1)) { + ok = 0; + } if (ok) { int rc = wc_MlDsaKey_VerifyMu( (wc_MlDsaKey*)wp_mldsa_get_key(ctx->mldsa), sig, (word32)sigLen, mu, MLDSA_MU_SZ, &res); + wp_unlock(wp_mldsa_get_mutex(ctx->mldsa)); if ((rc != 0) || (res != 1)) { ok = 0; } @@ -859,23 +892,26 @@ static int wp_mldsa_sign_prehash(wp_MlDsaSigCtx* ctx, unsigned char* sig, int useSeed = (ctx->testEntropyLen == WP_MLDSA_RND_SZ) || ctx->deterministic; - if (useSeed) { - if (wp_mldsa_fill_rnd(ctx, rnd) != 0) { - ok = 0; - } - if (ok) { + if (useSeed && (wp_mldsa_fill_rnd(ctx, rnd) != 0)) { + ok = 0; + } + /* The key may be shared by other contexts and threads, and wolfCrypt + * updates SHAKE state inside it: hold the key mutex. */ + if (ok && (wp_lock(wp_mldsa_get_mutex(ctx->mldsa)) != 1)) { + ok = 0; + } + if (ok) { + if (useSeed) { rc = wc_MlDsaKey_SignCtxHashWithSeed(key, ctx->context, (byte)ctx->contextLen, sig, &outLen, digest, (word32)digestLen, (int)ctx->hashType, rnd); - if (rc != 0) { - ok = 0; - } } - } - else { - rc = wc_MlDsaKey_SignCtxHash(key, ctx->context, - (byte)ctx->contextLen, sig, &outLen, digest, - (word32)digestLen, (int)ctx->hashType, &ctx->rng); + else { + rc = wc_MlDsaKey_SignCtxHash(key, ctx->context, + (byte)ctx->contextLen, sig, &outLen, digest, + (word32)digestLen, (int)ctx->hashType, &ctx->rng); + } + wp_unlock(wp_mldsa_get_mutex(ctx->mldsa)); if (rc != 0) { ok = 0; } @@ -907,11 +943,17 @@ static int wp_mldsa_verify_prehash(wp_MlDsaSigCtx* ctx, if (ok && (wc_HashFinal(&ctx->hashObj, ctx->hashType, digest) != 0)) { ok = 0; } + /* The key may be shared by other contexts and threads, and wolfCrypt + * updates SHAKE state inside it: hold the key mutex. */ + if (ok && (wp_lock(wp_mldsa_get_mutex(ctx->mldsa)) != 1)) { + ok = 0; + } if (ok) { int rc = wc_MlDsaKey_VerifyCtxHash( (wc_MlDsaKey*)wp_mldsa_get_key(ctx->mldsa), sig, (word32)sigLen, ctx->context, (byte)ctx->contextLen, digest, (word32)digestLen, (int)ctx->hashType, &res); + wp_unlock(wp_mldsa_get_mutex(ctx->mldsa)); if ((rc != 0) || (res != 1)) { ok = 0; } diff --git a/src/wp_mlkem_kem.c b/src/wp_mlkem_kem.c index 10d6cd4b..ef183626 100644 --- a/src/wp_mlkem_kem.c +++ b/src/wp_mlkem_kem.c @@ -239,6 +239,11 @@ static int wp_mlkem_kem_encapsulate(wp_MlKemCtx* ctx, unsigned char* out, WOLFPROV_LEAVE(WP_LOG_COMP_PQC, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), 0); return 0; } + /* The key may be shared by other contexts and threads, and wolfCrypt + * updates hash state inside it: hold the key mutex. */ + if (ok && (wp_lock(wp_mlkem_get_mutex(ctx->mlkem)) != 1)) { + ok = 0; + } if (ok) { int rc; MlKemKey* key = (MlKemKey*)wp_mlkem_get_key(ctx->mlkem); @@ -250,6 +255,7 @@ static int wp_mlkem_kem_encapsulate(wp_MlKemCtx* ctx, unsigned char* out, else { rc = wc_MlKemKey_Encapsulate(key, out, secret, &ctx->rng); } + wp_unlock(wp_mlkem_get_mutex(ctx->mlkem)); if (rc != 0) { ok = 0; } @@ -318,9 +324,15 @@ static int wp_mlkem_kem_decapsulate(wp_MlKemCtx* ctx, unsigned char* out, if (ok && (inLen != ctSize)) { ok = 0; } + /* The key may be shared by other contexts and threads, and wolfCrypt + * updates hash state inside it: hold the key mutex. */ + if (ok && (wp_lock(wp_mlkem_get_mutex(ctx->mlkem)) != 1)) { + ok = 0; + } if (ok) { int rc = wc_MlKemKey_Decapsulate( (MlKemKey*)wp_mlkem_get_key(ctx->mlkem), out, in, (word32)inLen); + wp_unlock(wp_mlkem_get_mutex(ctx->mlkem)); if (rc != 0) { ok = 0; } diff --git a/src/wp_mlkem_kmgmt.c b/src/wp_mlkem_kmgmt.c index 449dcbda..60b65b8f 100644 --- a/src/wp_mlkem_kmgmt.c +++ b/src/wp_mlkem_kmgmt.c @@ -65,7 +65,10 @@ struct wp_MlKem { const wp_MlKemData* data; #ifndef WP_SINGLE_THREADED - /** Mutex for reference count updating. */ + /** Mutex for reference count updating and for wolfCrypt calls on a key + * other contexts may share (encapsulate, decapsulate, encode for export, + * dup and match): MlKemKey holds SHA-3/SHAKE state those calls update. + * Import and keygen run before the key is shared and do not take it. */ wolfSSL_Mutex mutex; #endif /** Count of references to this object. */ @@ -176,6 +179,74 @@ void* wp_mlkem_get_key(wp_MlKem* mlkem) return &mlkem->key; } +/** + * Get the mutex from the wp_MlKem object. + * + * Hold it around wolfCrypt calls on a key that other contexts may share. + * MlKemKey carries SHA-3/SHAKE state that encapsulation, decapsulation and + * encoding update, and OpenSSL lets one key be used by several threads at + * once. Import and keygen run before the key is shared and do not take it. + * + * @param [in] mlkem ML-KEM key object. + * @return Pointer to wolfSSL mutex object, NULL in single-threaded builds. + */ +wolfSSL_Mutex* wp_mlkem_get_mutex(wp_MlKem* mlkem) +{ +#ifndef WP_SINGLE_THREADED + return &mlkem->mutex; +#else + (void)mlkem; + return NULL; +#endif +} + +/** + * Encode the public key of a possibly shared ML-KEM key object. + * + * Holds the key mutex: wolfCrypt may update the key's hash state and flags. + * + * @param [in] mlkem ML-KEM key object. + * @param [out] out Buffer to hold the encoded public key. + * @param [in] len Size of buffer in bytes. + * @return 0 on success, negative on failure. + */ +static int wp_mlkem_encode_pub_locked(const wp_MlKem* mlkem, + unsigned char* out, word32 len) +{ + wp_MlKem* key = (wp_MlKem*)mlkem; + int rc = BAD_MUTEX_E; + + if (wp_lock(wp_mlkem_get_mutex(key)) == 1) { + rc = wc_MlKemKey_EncodePublicKey(&key->key, out, len); + wp_unlock(wp_mlkem_get_mutex(key)); + } + return rc; +} + +/** + * Encode the private key of a possibly shared ML-KEM key object. + * + * Holds the key mutex: encoding the private key also encodes the public key, + * which may update the key's hash state and flags. + * + * @param [in] mlkem ML-KEM key object. + * @param [out] out Buffer to hold the encoded private key. + * @param [in] len Size of buffer in bytes. + * @return 0 on success, negative on failure. + */ +static int wp_mlkem_encode_priv_locked(const wp_MlKem* mlkem, + unsigned char* out, word32 len) +{ + wp_MlKem* key = (wp_MlKem*)mlkem; + int rc = BAD_MUTEX_E; + + if (wp_lock(wp_mlkem_get_mutex(key)) == 1) { + rc = wc_MlKemKey_EncodePrivateKey(&key->key, out, len); + wp_unlock(wp_mlkem_get_mutex(key)); + } + return rc; +} + /** * Get the parameter set data from the wp_MlKem object. * @@ -312,8 +383,7 @@ static wp_MlKem* wp_mlkem_dup(const wp_MlKem* src, int selection) ok = 0; } if (ok) { - rc = wc_MlKemKey_EncodePublicKey((MlKemKey*)&src->key, pubBuf, - pubLen); + rc = wp_mlkem_encode_pub_locked(src, pubBuf, pubLen); if (rc != 0) { ok = 0; } @@ -338,8 +408,7 @@ static wp_MlKem* wp_mlkem_dup(const wp_MlKem* src, int selection) ok = 0; } if (ok) { - rc = wc_MlKemKey_EncodePrivateKey((MlKemKey*)&src->key, privBuf, - privLen); + rc = wp_mlkem_encode_priv_locked(src, privBuf, privLen); if (rc != 0) { ok = 0; } @@ -446,13 +515,13 @@ static int wp_mlkem_match(const wp_MlKem* a, const wp_MlKem* b, int selection) ok = 0; } if (ok) { - rc = wc_MlKemKey_EncodePrivateKey((MlKemKey*)&a->key, bufA, lenA); + rc = wp_mlkem_encode_priv_locked(a, bufA, lenA); if (rc != 0) { ok = 0; } } if (ok) { - rc = wc_MlKemKey_EncodePrivateKey((MlKemKey*)&b->key, bufB, lenB); + rc = wp_mlkem_encode_priv_locked(b, bufB, lenB); if (rc != 0) { ok = 0; } @@ -472,13 +541,13 @@ static int wp_mlkem_match(const wp_MlKem* a, const wp_MlKem* b, int selection) ok = 0; } if (ok) { - rc = wc_MlKemKey_EncodePublicKey((MlKemKey*)&a->key, bufA, lenA); + rc = wp_mlkem_encode_pub_locked(a, bufA, lenA); if (rc != 0) { ok = 0; } } if (ok) { - rc = wc_MlKemKey_EncodePublicKey((MlKemKey*)&b->key, bufB, lenB); + rc = wp_mlkem_encode_pub_locked(b, bufB, lenB); if (rc != 0) { ok = 0; } @@ -681,7 +750,7 @@ static int wp_mlkem_export(wp_MlKem* mlkem, int selection, ok = 0; } if (ok) { - rc = wc_MlKemKey_EncodePublicKey(&mlkem->key, pubBuf, pubLen); + rc = wp_mlkem_encode_pub_locked(mlkem, pubBuf, pubLen); if (rc != 0) { ok = 0; } @@ -698,7 +767,7 @@ static int wp_mlkem_export(wp_MlKem* mlkem, int selection, ok = 0; } if (ok) { - rc = wc_MlKemKey_EncodePrivateKey(&mlkem->key, privBuf, privLen); + rc = wp_mlkem_encode_priv_locked(mlkem, privBuf, privLen); if (rc != 0) { ok = 0; } @@ -810,7 +879,7 @@ static int wp_mlkem_get_params(wp_MlKem* mlkem, OSSL_PARAM params[]) ok = 0; } else { - rc = wc_MlKemKey_EncodePublicKey(&mlkem->key, + rc = wp_mlkem_encode_pub_locked(mlkem, (unsigned char*)p->data, outLen); if (rc != 0) { ok = 0; @@ -838,7 +907,7 @@ static int wp_mlkem_get_params(wp_MlKem* mlkem, OSSL_PARAM params[]) ok = 0; } else { - rc = wc_MlKemKey_EncodePublicKey(&mlkem->key, + rc = wp_mlkem_encode_pub_locked(mlkem, (unsigned char*)p->data, outLen); if (rc != 0) { ok = 0; @@ -864,7 +933,7 @@ static int wp_mlkem_get_params(wp_MlKem* mlkem, OSSL_PARAM params[]) ok = 0; } else { - rc = wc_MlKemKey_EncodePrivateKey(&mlkem->key, + rc = wp_mlkem_encode_priv_locked(mlkem, (unsigned char*)p->data, outLen); if (rc != 0) { ok = 0; diff --git a/src/wp_mlx_kem.c b/src/wp_mlx_kem.c index 09d15140..61d7ff6e 100644 --- a/src/wp_mlx_kem.c +++ b/src/wp_mlx_kem.c @@ -301,6 +301,11 @@ static int wp_mlx_kem_encapsulate(wp_MlxCtx* ctx, unsigned char* out, classicalCt = out + (size_t)(1 - slot) * data->mlkemCtSize; classicalSs = secret + (size_t)(1 - slot) * WP_MLKEM_SS_SIZE; + /* The key may be shared by other contexts and threads, and wolfCrypt + * updates state inside it: hold the key mutex for both components. */ + if (wp_lock(wp_mlx_get_mutex(ctx->mlx)) != 1) { + return 0; + } rc = wc_MlKemKey_Encapsulate((MlKemKey*)wp_mlx_get_mlkem_key(ctx->mlx), mlkemCt, mlkemSs, &ctx->rng); if (rc != 0) { @@ -309,6 +314,7 @@ static int wp_mlx_kem_encapsulate(wp_MlxCtx* ctx, unsigned char* out, if (ok) { ok = wp_mlx_kem_classical_encap(ctx, classicalCt, classicalSs); } + wp_unlock(wp_mlx_get_mutex(ctx->mlx)); if (ok) { *outLen = ctSize; *secretLen = ssSize; @@ -448,6 +454,12 @@ static int wp_mlx_kem_decapsulate(wp_MlxCtx* ctx, unsigned char* out, classicalCt = in + (size_t)(1 - slot) * data->mlkemCtSize; classicalSs = out + (size_t)(1 - slot) * WP_MLKEM_SS_SIZE; + /* The key may be shared by other contexts and threads: wolfCrypt updates + * hash state in the ML-KEM key and the ECC path sets this context's RNG + * on the shared private key. Hold the key mutex for both components. */ + if (wp_lock(wp_mlx_get_mutex(ctx->mlx)) != 1) { + return 0; + } rc = wc_MlKemKey_Decapsulate((MlKemKey*)wp_mlx_get_mlkem_key(ctx->mlx), mlkemSs, mlkemCt, data->mlkemCtSize); if (rc != 0) { @@ -456,6 +468,7 @@ static int wp_mlx_kem_decapsulate(wp_MlxCtx* ctx, unsigned char* out, if (ok) { ok = wp_mlx_kem_classical_decap(ctx, classicalCt, classicalSs); } + wp_unlock(wp_mlx_get_mutex(ctx->mlx)); if (ok) { *outLen = ssSize; } diff --git a/src/wp_mlx_kmgmt.c b/src/wp_mlx_kmgmt.c index c9ed87a2..46105c12 100644 --- a/src/wp_mlx_kmgmt.c +++ b/src/wp_mlx_kmgmt.c @@ -110,7 +110,10 @@ struct wp_Mlx { const wp_MlxData* data; #ifndef WP_SINGLE_THREADED - /** Mutex for reference count updating. */ + /** Mutex for reference count updating and for wolfCrypt calls on a key + * other contexts may share (encapsulate, decapsulate, encode for export, + * dup and match): MlKemKey holds SHA-3/SHAKE state those calls update. + * Import and keygen run before the key is shared and do not take it. */ wolfSSL_Mutex mutex; #endif /** Count of references to this object. */ @@ -155,6 +158,27 @@ void* wp_mlx_get_mlkem_key(wp_Mlx* mlx) return &mlx->mlkem; } +/** + * Get the mutex from the hybrid object. + * + * Hold it around wolfCrypt calls on a key that other contexts may share. + * MlKemKey carries SHA-3/SHAKE state that encapsulation, decapsulation and + * encoding update, and OpenSSL lets one key be used by several threads at + * once. Import and keygen run before the key is shared and do not take it. + * + * @param [in] mlx Hybrid key object. + * @return Pointer to wolfSSL mutex object, NULL in single-threaded builds. + */ +wolfSSL_Mutex* wp_mlx_get_mutex(wp_Mlx* mlx) +{ +#ifndef WP_SINGLE_THREADED + return &mlx->mutex; +#else + (void)mlx; + return NULL; +#endif +} + /** * Get the classical (X25519/ECC) wolfSSL key from the hybrid object. * @@ -424,6 +448,65 @@ static int wp_mlx_classical_export_priv(wp_Mlx* mlx, unsigned char* out, return rc; } +/** + * Read both halves of the public key of a possibly shared hybrid key object. + * + * Holds the key mutex across both halves: encoding updates the ML-KEM key's + * hash state and flags, and an X25519 key imported from its private key alone + * derives and stores its public half on first export. + * + * @param [in] mlx Hybrid key object. + * @param [out] mlkemOut Buffer of mlkemPubSize bytes. + * @param [out] classicalOut Buffer for the classical public key. + * @param [in, out] classicalLen On in, buffer size; on out, bytes written. + * @return 0 on success, negative on failure. + */ +static int wp_mlx_read_pub(const wp_Mlx* mlx, unsigned char* mlkemOut, + unsigned char* classicalOut, word32* classicalLen) +{ + wp_Mlx* key = (wp_Mlx*)mlx; + int rc = BAD_MUTEX_E; + + if (wp_lock(wp_mlx_get_mutex(key)) == 1) { + rc = wc_MlKemKey_EncodePublicKey(&key->mlkem, mlkemOut, + key->data->mlkemPubSize); + if (rc == 0) { + rc = wp_mlx_classical_export_pub(key, classicalOut, classicalLen); + } + wp_unlock(wp_mlx_get_mutex(key)); + } + return rc; +} + +/** + * Read both halves of the private key of a possibly shared hybrid key object. + * + * Holds the key mutex across both: encoding the ML-KEM private key also + * encodes the public key, which updates the key's hash state and flags. + * + * @param [in] mlx Hybrid key object. + * @param [out] mlkemOut Buffer of mlkemPrivSize bytes. + * @param [out] classicalOut Buffer for the classical private key. + * @param [in, out] classicalLen On in, buffer size; on out, bytes written. + * @return 0 on success, negative on failure. + */ +static int wp_mlx_read_priv(const wp_Mlx* mlx, unsigned char* mlkemOut, + unsigned char* classicalOut, word32* classicalLen) +{ + wp_Mlx* key = (wp_Mlx*)mlx; + int rc = BAD_MUTEX_E; + + if (wp_lock(wp_mlx_get_mutex(key)) == 1) { + rc = wc_MlKemKey_EncodePrivateKey(&key->mlkem, mlkemOut, + key->data->mlkemPrivSize); + if (rc == 0) { + rc = wp_mlx_classical_export_priv(key, classicalOut, classicalLen); + } + wp_unlock(wp_mlx_get_mutex(key)); + } + return rc; +} + /** * Duplicate hybrid key object. * @@ -439,6 +522,7 @@ static wp_Mlx* wp_mlx_dup(const wp_Mlx* src, int selection) unsigned char* cPubBuf = NULL; unsigned char* cPrivBuf = NULL; word32 len; + word32 cLen; int rc; int ok = 1; int dupPub; @@ -464,8 +548,8 @@ static wp_Mlx* wp_mlx_dup(const wp_Mlx* src, int selection) ok = 0; } if (ok) { - rc = wc_MlKemKey_EncodePublicKey((MlKemKey*)&src->mlkem, mPubBuf, - len); + cLen = src->data->classicalPubSize; + rc = wp_mlx_read_pub(src, mPubBuf, cPubBuf, &cLen); if (rc != 0) { ok = 0; } @@ -477,14 +561,7 @@ static wp_Mlx* wp_mlx_dup(const wp_Mlx* src, int selection) } } if (ok) { - len = src->data->classicalPubSize; - rc = wp_mlx_classical_export_pub((wp_Mlx*)src, cPubBuf, &len); - if (rc != 0) { - ok = 0; - } - } - if (ok) { - rc = wp_mlx_classical_import_pub(dst, cPubBuf, len); + rc = wp_mlx_classical_import_pub(dst, cPubBuf, cLen); if (rc != 0) { ok = 0; } @@ -502,8 +579,8 @@ static wp_Mlx* wp_mlx_dup(const wp_Mlx* src, int selection) ok = 0; } if (ok) { - rc = wc_MlKemKey_EncodePrivateKey((MlKemKey*)&src->mlkem, mPrivBuf, - len); + cLen = src->data->classicalPrivSize; + rc = wp_mlx_read_priv(src, mPrivBuf, cPrivBuf, &cLen); if (rc != 0) { ok = 0; } @@ -514,20 +591,13 @@ static wp_Mlx* wp_mlx_dup(const wp_Mlx* src, int selection) ok = 0; } } - if (ok) { - len = src->data->classicalPrivSize; - rc = wp_mlx_classical_export_priv((wp_Mlx*)src, cPrivBuf, &len); - if (rc != 0) { - ok = 0; - } - } if (ok) { if (src->data->classicalType == WP_MLX_CLASSICAL_X25519) { - rc = wc_curve25519_import_private_ex(cPrivBuf, len, + rc = wc_curve25519_import_private_ex(cPrivBuf, cLen, &dst->classical.x25519, EC25519_LITTLE_ENDIAN); } else { - rc = wc_ecc_import_private_key_ex(cPrivBuf, len, NULL, 0, + rc = wc_ecc_import_private_key_ex(cPrivBuf, cLen, NULL, 0, &dst->classical.ecc, src->data->curveId); if (rc == 0) { rc = wc_ecc_make_pub(&dst->classical.ecc, NULL); @@ -633,34 +703,21 @@ static int wp_mlx_match(const wp_Mlx* a, const wp_Mlx* b, int selection) if ((bufA == NULL) || (bufB == NULL)) { ok = 0; } + /* Each key is read under its own mutex, one at a time. */ if (ok) { - lenA = a->data->mlkemPubSize; - rc = wc_MlKemKey_EncodePublicKey((MlKemKey*)&a->mlkem, bufA, lenA); - if (rc == 0) { - rc = wc_MlKemKey_EncodePublicKey((MlKemKey*)&b->mlkem, bufB, - lenA); - } - if (rc != 0) { - ok = 0; - } - } - if (ok && (XMEMCMP(bufA, bufB, lenA) != 0)) { - ok = 0; - } - if (ok) { + word32 mlkemSz = a->data->mlkemPubSize; + lenA = a->data->classicalPubSize; lenB = b->data->classicalPubSize; - rc = wp_mlx_classical_export_pub((wp_Mlx*)a, bufA, &lenA); + rc = wp_mlx_read_pub(a, bufA, bufA + mlkemSz, &lenA); if (rc == 0) { - rc = wp_mlx_classical_export_pub((wp_Mlx*)b, bufB, &lenB); + rc = wp_mlx_read_pub(b, bufB, bufB + mlkemSz, &lenB); } - if (rc != 0) { + if ((rc != 0) || (lenA != lenB) || + (XMEMCMP(bufA, bufB, mlkemSz + lenA) != 0)) { ok = 0; } } - if (ok && ((lenA != lenB) || (XMEMCMP(bufA, bufB, lenA) != 0))) { - ok = 0; - } OPENSSL_free(bufA); OPENSSL_free(bufB); } @@ -910,12 +967,8 @@ static int wp_mlx_encode_pub(wp_Mlx* mlx, unsigned char* out) size_t mlkemOff = (size_t)slot * mlx->data->classicalPubSize; size_t classicalOff = (size_t)(1 - slot) * mlx->data->mlkemPubSize; - rc = wc_MlKemKey_EncodePublicKey(&mlx->mlkem, out + mlkemOff, - mlx->data->mlkemPubSize); - if (rc == 0) { - len = mlx->data->classicalPubSize; - rc = wp_mlx_classical_export_pub(mlx, out + classicalOff, &len); - } + len = mlx->data->classicalPubSize; + rc = wp_mlx_read_pub(mlx, out + mlkemOff, out + classicalOff, &len); return rc; } @@ -934,12 +987,8 @@ static int wp_mlx_encode_priv(wp_Mlx* mlx, unsigned char* out) size_t mlkemOff = (size_t)slot * mlx->data->classicalPrivSize; size_t classicalOff = (size_t)(1 - slot) * mlx->data->mlkemPrivSize; - rc = wc_MlKemKey_EncodePrivateKey(&mlx->mlkem, out + mlkemOff, - mlx->data->mlkemPrivSize); - if (rc == 0) { - len = mlx->data->classicalPrivSize; - rc = wp_mlx_classical_export_priv(mlx, out + classicalOff, &len); - } + len = mlx->data->classicalPrivSize; + rc = wp_mlx_read_priv(mlx, out + mlkemOff, out + classicalOff, &len); return rc; } diff --git a/test/test_mldsa.c b/test/test_mldsa.c index dbec042a..6faaa78f 100644 --- a/test/test_mldsa.c +++ b/test/test_mldsa.c @@ -1512,4 +1512,147 @@ int test_mldsa_cross_provider_raw(void* data) return err; } +/* + * Concurrency: threads share one key, each operation with its own context. + * The wolfCrypt key holds SHAKE state that signing and verifying update, so + * the provider must hold the key mutex. Run this under ThreadSanitizer - a + * plain pass proves little on its own. + */ +#if defined(HAVE_PTHREAD) && !defined(WP_SINGLE_THREADED) + +#include + +#define WP_MLDSA_CONC_THREADS 4 +#define WP_MLDSA_CONC_ITERS 30 +#define WP_MLDSA_CONC_SIG_SZ 4096 /* > ML-DSA-65 signature */ + +/* Sign/verify API paths cycled by each worker (see wp_mldsa_conc_once). */ +static const int wp_mldsa_conc_modes[] = { + 0, 1, 3, +#ifdef WP_HAVE_SHA512 + 2, 4 /* HashML-DSA needs SHA-512 */ +#endif +}; +#define WP_MLDSA_CONC_MODES \ + (int)(sizeof(wp_mldsa_conc_modes) / sizeof(wp_mldsa_conc_modes[0])) + +/* Per-thread state; pkey is deliberately shared by every worker. */ +typedef struct { + EVP_PKEY* pkey; + int err; +} wp_mldsa_conc_arg; + +/* Sign then verify once with the shared key. Each API path takes the key + * mutex at its own call site. mode 0: streamed pure ML-DSA (EVP_DigestSign), + * 1: one-shot EVP_PKEY_sign, 2: HashML-DSA with a SHA-512 pre-hash; 3 and 4 + * repeat 1 and 2 deterministically, which takes the seeded signing branch. */ +static int wp_mldsa_conc_once(EVP_PKEY* pkey, int mode) +{ + static const unsigned char msg[] = "ML-DSA shared key"; + unsigned char sig[WP_MLDSA_CONC_SIG_SZ]; + size_t sigLen = sizeof(sig); + unsigned int det = 1; + OSSL_PARAM params[2]; + OSSL_PARAM* signParams = NULL; + const char* md; + int err; + + if (mode >= 3) { + params[0] = OSSL_PARAM_construct_uint( + OSSL_SIGNATURE_PARAM_DETERMINISTIC, &det); + params[1] = OSSL_PARAM_construct_end(); + signParams = params; + mode -= 2; + } + md = (mode == 2) ? "SHA-512" : NULL; + + if (mode == 1) { + EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new_from_pkey(wpLibCtx, pkey, NULL); + + err = (ctx == NULL) || (EVP_PKEY_sign_init_ex(ctx, signParams) != 1) || + (EVP_PKEY_sign(ctx, sig, &sigLen, msg, sizeof(msg)) != 1) || + (EVP_PKEY_verify_init(ctx) != 1) || + (EVP_PKEY_verify(ctx, sig, sigLen, msg, sizeof(msg)) != 1); + EVP_PKEY_CTX_free(ctx); + } + else { + EVP_MD_CTX* mdctx = EVP_MD_CTX_new(); + + err = (mdctx == NULL) || + (EVP_DigestSignInit_ex(mdctx, NULL, md, wpLibCtx, NULL, pkey, + signParams) != 1) || + (EVP_DigestSign(mdctx, sig, &sigLen, msg, sizeof(msg)) != 1) || + (EVP_DigestVerifyInit_ex(mdctx, NULL, md, wpLibCtx, NULL, pkey, + NULL) != 1) || + (EVP_DigestVerify(mdctx, sig, sigLen, msg, sizeof(msg)) != 1); + EVP_MD_CTX_free(mdctx); + } + return err; +} + +/* Cycle through the sign/verify API paths on the shared key. */ +static void* wp_mldsa_conc_worker(void* arg) +{ + wp_mldsa_conc_arg* a = (wp_mldsa_conc_arg*)arg; + int err = 0; + int i; + + for (i = 0; (err == 0) && (i < WP_MLDSA_CONC_ITERS); i++) { + err = wp_mldsa_conc_once(a->pkey, + wp_mldsa_conc_modes[i % WP_MLDSA_CONC_MODES]); + } + + a->err = err; + return NULL; +} + +int test_mldsa_concurrent_ops(void* data) +{ + int err; + EVP_PKEY* pkey = NULL; + wp_mldsa_conc_arg args[WP_MLDSA_CONC_THREADS]; + pthread_t threads[WP_MLDSA_CONC_THREADS]; + int created = 0; + int i; + + (void)data; + + PRINT_MSG("One ML-DSA-65 key shared by %d threads", WP_MLDSA_CONC_THREADS); + err = mldsa_keygen("ML-DSA-65", &pkey); + for (i = 0; (err == 0) && (i < WP_MLDSA_CONC_THREADS); i++) { + args[i].pkey = pkey; + args[i].err = 0; + if (pthread_create(&threads[i], NULL, wp_mldsa_conc_worker, + &args[i]) != 0) { + err = 1; + } + else { + created++; + } + } + for (i = 0; i < created; i++) { + pthread_join(threads[i], NULL); + if (args[i].err != 0) { + PRINT_ERR_MSG("Concurrent thread %d failed", i); + err = 1; + } + } + EVP_PKEY_free(pkey); + + return err; +} + +#else + +int test_mldsa_concurrent_ops(void* data) +{ + (void)data; + + PRINT_MSG("No pthreads in this build - ML-DSA concurrency test not run"); + + return 0; +} + +#endif /* HAVE_PTHREAD && !WP_SINGLE_THREADED */ + #endif /* WP_HAVE_MLDSA */ diff --git a/test/test_mlkem.c b/test/test_mlkem.c index 01589658..e3d7c7c8 100644 --- a/test/test_mlkem.c +++ b/test/test_mlkem.c @@ -1399,4 +1399,232 @@ int test_mlkem_cross_provider_raw(void* data) return err; } +/* Largest hybrid public key: ML-KEM-1024 plus a P-384 point. */ +#define WP_MLX_MATCH_PUB_SZ 2048 + +/* Import a public-only hybrid key from its encoding. */ +static int wp_test_mlx_import_pub(const char* name, unsigned char* pub, + size_t len, EVP_PKEY** pkey) +{ + int err; + EVP_PKEY_CTX* ctx; + OSSL_PARAM params[2]; + + params[0] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PUB_KEY, pub, + len); + params[1] = OSSL_PARAM_construct_end(); + ctx = EVP_PKEY_CTX_new_from_name(wpLibCtx, name, NULL); + err = (ctx == NULL) || (EVP_PKEY_fromdata_init(ctx) != 1) || + (EVP_PKEY_fromdata(ctx, pkey, EVP_PKEY_PUBLIC_KEY, params) != 1); + EVP_PKEY_CTX_free(ctx); + return err; +} + +/* + * EVP_PKEY_eq on hybrid keys: equal to a key holding the same public key, and + * unequal when only one component (ML-KEM or classical) differs. + */ +int test_mlx_match(void* data) +{ + /* Where the two components meet in each group's public encoding: + * X25519MLKEM768 puts ML-KEM first, the NIST-curve groups the point. */ + static const struct { + const char* name; + size_t split; + } groups[] = { + { "X25519MLKEM768", WC_ML_KEM_768_PUBLIC_KEY_SIZE }, + { "SecP256r1MLKEM768", 65 }, + { "SecP384r1MLKEM1024", 97 }, + }; + int err = 0; + size_t i; + + (void)data; + + for (i = 0; (err == 0) && (i < sizeof(groups) / sizeof(groups[0])); i++) { + EVP_PKEY* k = NULL; + EVP_PKEY* k2 = NULL; + unsigned char* pub = NULL; + unsigned char* pub2 = NULL; + size_t pubLen = 0; + size_t pub2Len = 0; + size_t split = groups[i].split; + unsigned char mix[WP_MLX_MATCH_PUB_SZ]; + int v; + + PRINT_MSG("EVP_PKEY_eq on %s", groups[i].name); + err = wp_test_mlx_keygen(groups[i].name, &k); + if (err == 0) { + err = wp_test_mlx_keygen(groups[i].name, &k2); + } + if (err == 0) { + err = mlkem_get_pub(k, &pub, &pubLen); + } + if (err == 0) { + err = mlkem_get_pub(k2, &pub2, &pub2Len); + } + if (err == 0) { + err = (pubLen != pub2Len) || (pubLen > sizeof(mix)) || + (split >= pubLen); + } + /* v 0: k's own public key; v 1 and 2: one component from k2. */ + for (v = 0; (err == 0) && (v < 3); v++) { + EVP_PKEY* m = NULL; + + memcpy(mix, pub, pubLen); + if (v == 1) { + memcpy(mix, pub2, split); + } + else if (v == 2) { + memcpy(mix + split, pub2 + split, pubLen - split); + } + err = wp_test_mlx_import_pub(groups[i].name, mix, pubLen, &m); + /* Exactly 1 for the same key, exactly 0 (not an error) else. */ + if ((err == 0) && (EVP_PKEY_eq(k, m) != ((v == 0) ? 1 : 0))) { + PRINT_ERR_MSG("%s: EVP_PKEY_eq wrong for variant %d", + groups[i].name, v); + err = 1; + } + EVP_PKEY_free(m); + } + + OPENSSL_free(pub); + OPENSSL_free(pub2); + EVP_PKEY_free(k2); + EVP_PKEY_free(k); + } + + return err; +} + +/* + * Concurrency: threads share one key, each operation with its own + * EVP_PKEY_CTX. The wolfCrypt key holds hash state that every operation + * updates, so the provider must hold the key mutex. Run this under + * ThreadSanitizer - a plain pass proves little on its own. + */ +#if defined(HAVE_PTHREAD) && !defined(WP_SINGLE_THREADED) + +#include + +#define WP_MLKEM_CONC_THREADS 4 +#define WP_MLKEM_CONC_ITERS 30 +/* Fits the largest ciphertext, secret and public key of the tested groups. */ +#define WP_MLKEM_CONC_BUF_SZ 2048 + +/* Per-thread state; pkey is deliberately shared by every worker. */ +typedef struct { + EVP_PKEY* pkey; + int err; +} wp_mlkem_conc_arg; + +/* Encapsulate and decapsulate with the shared key, then duplicate it, compare + * the copy and read the public key. Dup, match and get_params each encode the + * key through their own dispatch entry, and encoding updates the key too. */ +static void* wp_mlkem_conc_worker(void* arg) +{ + wp_mlkem_conc_arg* a = (wp_mlkem_conc_arg*)arg; + unsigned char ct[WP_MLKEM_CONC_BUF_SZ]; + unsigned char ss[WP_MLKEM_CONC_BUF_SZ]; + unsigned char ss2[WP_MLKEM_CONC_BUF_SZ]; + unsigned char pub[WP_MLKEM_CONC_BUF_SZ]; + int err = 0; + int i; + + for (i = 0; (err == 0) && (i < WP_MLKEM_CONC_ITERS); i++) { + EVP_PKEY_CTX* ctx; + size_t ctLen = sizeof(ct); + size_t ssLen = sizeof(ss); + size_t ss2Len = sizeof(ss2); + size_t pubLen = 0; + EVP_PKEY* dup = NULL; + + ctx = EVP_PKEY_CTX_new_from_pkey(wpLibCtx, a->pkey, NULL); + err = (ctx == NULL); + if (err == 0) { + err = (EVP_PKEY_encapsulate_init(ctx, NULL) != 1) || + (EVP_PKEY_encapsulate(ctx, ct, &ctLen, ss, &ssLen) != 1); + } + if (err == 0) { + err = (EVP_PKEY_decapsulate_init(ctx, NULL) != 1) || + (EVP_PKEY_decapsulate(ctx, ss2, &ss2Len, ct, ctLen) != 1); + } + if (err == 0) { + err = (ss2Len != ssLen) || (memcmp(ss, ss2, ssLen) != 0); + } + if (err == 0) { + dup = EVP_PKEY_dup(a->pkey); + err = (dup == NULL) || (EVP_PKEY_eq(a->pkey, dup) != 1); + } + if (err == 0) { + err = EVP_PKEY_get_octet_string_param(a->pkey, + OSSL_PKEY_PARAM_PUB_KEY, pub, sizeof(pub), &pubLen) != 1; + } + EVP_PKEY_free(dup); + EVP_PKEY_CTX_free(ctx); + } + + a->err = err; + return NULL; +} + +int test_mlkem_concurrent_ops(void* data) +{ + /* Pure ML-KEM, and both hybrid families: the classical half (ECC or + * X25519) also uses the shared key. */ + static const char* const names[] = { + "ML-KEM-768", "SecP256r1MLKEM768", "X25519MLKEM768" + }; + int err = 0; + size_t n; + + (void)data; + + for (n = 0; (err == 0) && (n < sizeof(names) / sizeof(names[0])); n++) { + EVP_PKEY* pkey = NULL; + wp_mlkem_conc_arg args[WP_MLKEM_CONC_THREADS]; + pthread_t threads[WP_MLKEM_CONC_THREADS]; + int created = 0; + int i; + + PRINT_MSG("One %s key shared by %d threads", names[n], + WP_MLKEM_CONC_THREADS); + err = wp_test_mlkem_keygen(names[n], &pkey); + for (i = 0; (err == 0) && (i < WP_MLKEM_CONC_THREADS); i++) { + args[i].pkey = pkey; + args[i].err = 0; + if (pthread_create(&threads[i], NULL, wp_mlkem_conc_worker, + &args[i]) != 0) { + err = 1; + } + else { + created++; + } + } + for (i = 0; i < created; i++) { + pthread_join(threads[i], NULL); + if (args[i].err != 0) { + PRINT_ERR_MSG("%s: concurrent thread %d failed", names[n], i); + err = 1; + } + } + EVP_PKEY_free(pkey); + } + + return err; +} + +#else + +int test_mlkem_concurrent_ops(void* data) +{ + (void)data; + + PRINT_MSG("No pthreads in this build - ML-KEM concurrency test not run"); + + return 0; +} + +#endif /* HAVE_PTHREAD && !WP_SINGLE_THREADED */ + #endif /* WP_HAVE_MLKEM */ diff --git a/test/unit.c b/test/unit.c index 80e13c8a..ce05fa0c 100644 --- a/test/unit.c +++ b/test/unit.c @@ -674,6 +674,8 @@ TEST_CASE test_case[] = { TEST_DECL(test_mlkem_import_mismatched_pubpriv, NULL), TEST_DECL(test_mlx_encap_decap, NULL), TEST_DECL(test_mlx_dup, NULL), + TEST_DECL(test_mlx_match, NULL), + TEST_DECL(test_mlkem_concurrent_ops, NULL), #endif #ifdef WP_HAVE_MLDSA @@ -700,6 +702,7 @@ TEST_CASE test_case[] = { TEST_DECL(test_mldsa_x509_sign_verify, NULL), TEST_DECL(test_mldsa_encoder_import_object, NULL), TEST_DECL(test_mldsa_cross_provider_raw, NULL), + TEST_DECL(test_mldsa_concurrent_ops, NULL), #endif #if defined(WP_HAVE_SLHDSA) && defined(WP_SLHDSA_TEST_SETS) diff --git a/test/unit.h b/test/unit.h index 66c5820a..cb204228 100644 --- a/test/unit.h +++ b/test/unit.h @@ -712,6 +712,8 @@ int test_mlkem_get_params(void *data); int test_mlkem_import_mismatched_pubpriv(void *data); int test_mlx_encap_decap(void *data); int test_mlx_dup(void *data); +int test_mlx_match(void *data); +int test_mlkem_concurrent_ops(void *data); #endif #ifdef WP_HAVE_MLDSA @@ -738,6 +740,7 @@ int test_mldsa_encode_decode(void *data); int test_mldsa_x509_sign_verify(void *data); int test_mldsa_encoder_import_object(void *data); int test_mldsa_cross_provider_raw(void *data); +int test_mldsa_concurrent_ops(void *data); #endif #if defined(WP_HAVE_SLHDSA) && defined(WP_SLHDSA_TEST_SETS)