Skip to content
Merged
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
17 changes: 16 additions & 1 deletion .github/workflows/sanitizers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions include/wolfprovider/alg_funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
23 changes: 22 additions & 1 deletion src/wp_mldsa_kmgmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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.
*
Expand Down
68 changes: 55 additions & 13 deletions src/wp_mldsa_sig.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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. */
Expand All @@ -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;
}
Expand Down Expand Up @@ -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(
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 12 additions & 0 deletions src/wp_mlkem_kem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
Loading
Loading