Skip to content

Commit 31cb153

Browse files
authored
Verify size of mlen in ML-DSA external mu mode (#2841)
### Issues: Resolves #N/A ### Description of changes: When signing/verifying in ML-DSA the caller provides `mlen` the the length of the message `m` that is being signed/verified. Currently, we do no validation on the size of `mlen` when in pre-hash mode (called "external mu" in ML-DSA). Since the pre-hash is the output of a SHAKE256 hash function, it is of fixed size `ML_DSA_CRHBYTES`. This adds validation to check that, returning `-1` if not true to match the case where the context `ctx` has length too large. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license.
1 parent 2929213 commit 31cb153

File tree

1 file changed

+14
-5
lines changed
  • crypto/fipsmodule/ml_dsa/ml_dsa_ref

1 file changed

+14
-5
lines changed

crypto/fipsmodule/ml_dsa/ml_dsa_ref/sign.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ int ml_dsa_keypair(ml_dsa_params *params, uint8_t *pk, uint8_t *sk, uint8_t *see
161161
* - uint8_t *sk: pointer to bit-packed secret key
162162
* - int external_mu: indicates input message m is to be processed as mu
163163
*
164-
* Returns 0 (success) or -1 (context string too long)
164+
* Returns 0 (success) or -1 (context string too long or incorrect mlen in external mu)
165165
**************************************************/
166166
int ml_dsa_sign_internal(ml_dsa_params *params,
167167
uint8_t *sig,
@@ -184,6 +184,10 @@ int ml_dsa_sign_internal(ml_dsa_params *params,
184184
ml_dsa_poly cp;
185185
KECCAK1600_CTX state;
186186

187+
if (external_mu && mlen != ML_DSA_CRHBYTES) {
188+
return -1;
189+
}
190+
187191
rho = seedbuf;
188192
tr = rho + ML_DSA_SEEDBYTES;
189193
key = tr + ML_DSA_TRBYTES;
@@ -346,12 +350,12 @@ int ml_dsa_sign(ml_dsa_params *params,
346350
if (!RAND_bytes(rnd, ML_DSA_RNDBYTES)) {
347351
return -1;
348352
}
349-
ml_dsa_sign_internal(params, sig, siglen, m, mlen, pre, 2 + ctxlen, rnd, sk, 0);
353+
int ret = ml_dsa_sign_internal(params, sig, siglen, m, mlen, pre, 2 + ctxlen, rnd, sk, 0);
350354

351355
/* FIPS 204. Section 3.6.3 Destruction of intermediate values. */
352356
OPENSSL_cleanse(pre, sizeof(pre));
353357
OPENSSL_cleanse(rnd, sizeof(rnd));
354-
return 0;
358+
return ret;
355359
}
356360

357361
/*************************************************
@@ -380,11 +384,11 @@ int ml_dsa_extmu_sign(ml_dsa_params *params,
380384
if (!RAND_bytes(rnd, ML_DSA_RNDBYTES)) {
381385
return -1;
382386
}
383-
ml_dsa_sign_internal(params, sig, siglen, mu, mulen, NULL, 0, rnd, sk, 1);
387+
int ret = ml_dsa_sign_internal(params, sig, siglen, mu, mulen, NULL, 0, rnd, sk, 1);
384388

385389
/* FIPS 204. Section 3.6.3 Destruction of intermediate values. */
386390
OPENSSL_cleanse(rnd, sizeof(rnd));
387-
return 0;
391+
return ret;
388392
}
389393

390394
/*************************************************
@@ -469,6 +473,11 @@ int ml_dsa_verify_internal(ml_dsa_params *params,
469473
if(siglen != params->bytes) {
470474
return -1;
471475
}
476+
477+
if (external_mu && mlen != ML_DSA_CRHBYTES) {
478+
return -1;
479+
}
480+
472481
/* FIPS 204: line 1 */
473482
ml_dsa_unpack_pk(params, rho, &t1, pk);
474483
/* FIPS 204: line 2 */

0 commit comments

Comments
 (0)