Skip to content

Commit 0a0c980

Browse files
committed
Fix for ML-DSA to fall back when no context is provided
1 parent 977bf18 commit 0a0c980

2 files changed

Lines changed: 114 additions & 4 deletions

File tree

src/wh_server_crypto.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4213,11 +4213,17 @@ static int _HandleMlDsaSign(whServerContext* ctx, uint16_t magic, int devId,
42134213
key, req_context, (byte)contextSz, res_out, &res_len,
42144214
in, in_len, preHashType, ctx->crypto->rng);
42154215
}
4216-
else {
4216+
else if (contextSz > 0) {
42174217
ret = wc_MlDsaKey_SignCtx(
42184218
key, req_context, (byte)contextSz, res_out, &res_len,
42194219
in, in_len, ctx->crypto->rng);
42204220
}
4221+
else {
4222+
/* Fall back to legacy method if no context is provided */
4223+
ret = wc_MlDsaKey_Sign(
4224+
key, res_out, &res_len,
4225+
in, in_len, ctx->crypto->rng);
4226+
}
42214227
}
42224228
wc_MlDsaKey_Free(key);
42234229
}
@@ -4318,11 +4324,17 @@ static int _HandleMlDsaVerify(whServerContext* ctx, uint16_t magic, int devId,
43184324
key, req_sig, sig_len, req_context, (byte)contextSz,
43194325
req_hash, hash_len, preHashType, &result);
43204326
}
4321-
else {
4327+
else if (contextSz > 0) {
43224328
ret = wc_MlDsaKey_VerifyCtx(
43234329
key, req_sig, sig_len, req_context, (byte)contextSz,
43244330
req_hash, hash_len, &result);
43254331
}
4332+
else {
4333+
/* Fall back to legacy method if no context is provided */
4334+
ret = wc_MlDsaKey_Verify(
4335+
key, req_sig, sig_len,
4336+
req_hash, hash_len, &result);
4337+
}
43264338
}
43274339
wc_MlDsaKey_Free(key);
43284340
}
@@ -5455,12 +5467,18 @@ static int _HandleMlDsaSignDma(whServerContext* ctx, uint16_t magic, int devId,
54555467
sigAddr, &sigLen, msgAddr, req.msg.sz,
54565468
preHashType, ctx->crypto->rng);
54575469
}
5458-
else {
5470+
else if (contextSz > 0) {
54595471
ret = wc_MlDsaKey_SignCtx(
54605472
key, req_context, (byte)contextSz,
54615473
sigAddr, &sigLen, msgAddr, req.msg.sz,
54625474
ctx->crypto->rng);
54635475
}
5476+
else {
5477+
/* Fall back to legacy method if no context is provided */
5478+
ret = wc_MlDsaKey_Sign(
5479+
key, sigAddr, &sigLen,
5480+
msgAddr, req.msg.sz, ctx->crypto->rng);
5481+
}
54645482

54655483
if (ret == 0) {
54665484
/* Post-write processing of signature buffer */
@@ -5592,11 +5610,17 @@ static int _HandleMlDsaVerifyDma(whServerContext* ctx, uint16_t magic,
55925610
key, sigAddr, req.sig.sz, req_context, (byte)contextSz,
55935611
msgAddr, req.msg.sz, preHashType, &verified);
55945612
}
5595-
else {
5613+
else if (contextSz > 0) {
55965614
ret = wc_MlDsaKey_VerifyCtx(
55975615
key, sigAddr, req.sig.sz, req_context, (byte)contextSz,
55985616
msgAddr, req.msg.sz, &verified);
55995617
}
5618+
else {
5619+
/* Fall back to legacy method if no context is provided */
5620+
ret = wc_MlDsaKey_Verify(
5621+
key, sigAddr, req.sig.sz,
5622+
msgAddr, req.msg.sz, &verified);
5623+
}
56005624

56015625
if (ret == 0) {
56025626
/* Post-read processing of signature buffer */

test/wh_test_crypto.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4182,6 +4182,88 @@ static int whTestCrypto_Cmac(whClientContext* ctx, int devId, WC_RNG* rng)
41824182
#if !defined(WOLFSSL_DILITHIUM_NO_VERIFY) && \
41834183
!defined(WOLFSSL_DILITHIUM_NO_SIGN) && \
41844184
!defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY) && !defined(WOLFSSL_NO_ML_DSA_44)
4185+
/* Test that a signature created locally with wc_MlDsaKey_Sign (basic API,
4186+
* no FIPS 204 context) can be verified through the wolfHSM server using
4187+
* wh_Client_MlDsaVerify. This is the flow that wolfBoot uses: sign at build
4188+
* time with wolfCrypt, verify at boot through the HSM crypto callback. */
4189+
static int whTestCrypto_MlDsaLocalSignServerVerify(whClientContext* ctx,
4190+
int devId, WC_RNG* rng)
4191+
{
4192+
(void)devId;
4193+
4194+
int ret = 0;
4195+
int verified = 0;
4196+
MlDsaKey key[1];
4197+
byte msg[] = "wolfBoot-style local sign, HSM verify";
4198+
byte sig[DILITHIUM_ML_DSA_44_SIG_SIZE];
4199+
word32 sigSz = sizeof(sig);
4200+
4201+
/* Initialize and generate a key pair locally */
4202+
ret = wc_MlDsaKey_Init(key, NULL, INVALID_DEVID);
4203+
if (ret != 0) {
4204+
WH_ERROR_PRINT("Failed to init ML-DSA key: %d\n", ret);
4205+
return ret;
4206+
}
4207+
4208+
ret = wc_MlDsaKey_SetParams(key, WC_ML_DSA_44);
4209+
if (ret != 0) {
4210+
WH_ERROR_PRINT("Failed to set ML-DSA params: %d\n", ret);
4211+
wc_MlDsaKey_Free(key);
4212+
return ret;
4213+
}
4214+
4215+
ret = wc_MlDsaKey_MakeKey(key, rng);
4216+
if (ret != 0) {
4217+
WH_ERROR_PRINT("Failed to generate ML-DSA key: %d\n", ret);
4218+
wc_MlDsaKey_Free(key);
4219+
return ret;
4220+
}
4221+
4222+
/* Sign locally using the basic wolfCrypt API (no context, no HSM) */
4223+
ret = wc_MlDsaKey_Sign(key, sig, &sigSz, msg, sizeof(msg), rng);
4224+
if (ret != 0) {
4225+
WH_ERROR_PRINT("Failed to sign locally with ML-DSA: %d\n", ret);
4226+
wc_MlDsaKey_Free(key);
4227+
return ret;
4228+
}
4229+
4230+
/* Now verify through the wolfHSM server using the client API.
4231+
* The server must use wc_MlDsaKey_Verify (not VerifyCtx) when
4232+
* contextSz == 0 and preHashType == WC_HASH_TYPE_NONE. */
4233+
ret = wh_Client_MlDsaVerify(ctx, sig, sigSz, msg, sizeof(msg), &verified,
4234+
key, NULL, 0, WC_HASH_TYPE_NONE);
4235+
if (ret != 0) {
4236+
WH_ERROR_PRINT("Failed to verify via HSM: %d\n", ret);
4237+
wc_MlDsaKey_Free(key);
4238+
return ret;
4239+
}
4240+
4241+
if (!verified) {
4242+
WH_ERROR_PRINT("ML-DSA local-sign/HSM-verify: signature invalid\n");
4243+
wc_MlDsaKey_Free(key);
4244+
return -1;
4245+
}
4246+
4247+
/* Tampered signature should fail */
4248+
sig[0] ^= 0xFF;
4249+
ret = wh_Client_MlDsaVerify(ctx, sig, sigSz, msg, sizeof(msg), &verified,
4250+
key, NULL, 0, WC_HASH_TYPE_NONE);
4251+
if (ret != 0) {
4252+
WH_ERROR_PRINT("Failed to call verify with tampered sig: %d\n", ret);
4253+
wc_MlDsaKey_Free(key);
4254+
return ret;
4255+
}
4256+
if (verified) {
4257+
WH_ERROR_PRINT("ML-DSA local-sign/HSM-verify: accepted tampered sig\n");
4258+
wc_MlDsaKey_Free(key);
4259+
return -1;
4260+
}
4261+
4262+
WH_TEST_PRINT("ML-DSA Local-Sign/HSM-Verify SUCCESS\n");
4263+
wc_MlDsaKey_Free(key);
4264+
return 0;
4265+
}
4266+
41854267
static int whTestCrypto_MlDsaWolfCrypt(whClientContext* ctx, int devId,
41864268
WC_RNG* rng)
41874269
{
@@ -5946,6 +6028,10 @@ int whTest_CryptoClientConfig(whClientConfig* config)
59466028
}
59476029
}
59486030

6031+
if (ret == 0) {
6032+
ret = whTestCrypto_MlDsaLocalSignServerVerify(client, WH_DEV_ID, rng);
6033+
}
6034+
59496035
if (ret == 0) {
59506036
ret = whTestCrypto_MlDsaClient(client, WH_DEV_ID, rng);
59516037
}

0 commit comments

Comments
 (0)