Skip to content

Commit 4d1cd02

Browse files
feggehanno-becker
authored andcommitted
Reject MLD_PREHASH_NONE in prehash APIs
The internal pre-hash sign/verify entry points now return MLD_ERR_FAIL when called with MLD_PREHASH_NONE, matching the documented set of accepted hash algorithms. Doc comments in mldsa_native.h and sign.h are updated to state the rejection explicitly. test_sign_pre_hash is extended to cover both rejection paths. Signed-off-by: Fredrik Dahlgren <fredrik.dahlgren@trailofbits.com> [hbecker: squashed fixup commits, rewrote subject/body, folded the rejection checks into test_sign_pre_hash] Signed-off-by: Hanno Becker <beckphan@amazon.co.uk>
1 parent bdad2c5 commit 4d1cd02

4 files changed

Lines changed: 27 additions & 0 deletions

File tree

mldsa/mldsa_native.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,8 @@ int MLD_API_NAMESPACE(verify_extmu)(
525525
* MLD_PREHASH_SHA3_224, MLD_PREHASH_SHA3_256, MLD_PREHASH_SHA3_384,
526526
* MLD_PREHASH_SHA3_512, MLD_PREHASH_SHAKE_128, MLD_PREHASH_SHAKE_256.
527527
*
528+
* MLD_PREHASH_NONE is rejected by this API.
529+
*
528530
* @warning This is an unstable API that may change in the future. If you need
529531
* a stable API use signature_pre_hash_shake256.
530532
*
@@ -581,6 +583,8 @@ int MLD_API_NAMESPACE(signature_pre_hash_internal)(
581583
* MLD_PREHASH_SHA3_224, MLD_PREHASH_SHA3_256, MLD_PREHASH_SHA3_384,
582584
* MLD_PREHASH_SHA3_512, MLD_PREHASH_SHAKE_128, MLD_PREHASH_SHAKE_256.
583585
*
586+
* MLD_PREHASH_NONE is rejected by this API.
587+
*
584588
* @warning This is an unstable API that may change in the future. If you need
585589
* a stable API use verify_pre_hash_shake256.
586590
*

mldsa/src/sign.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,12 @@ int mld_sign_signature_pre_hash_internal(
13361336
size_t pre_len;
13371337
int ret;
13381338

1339+
if (hashalg == MLD_PREHASH_NONE)
1340+
{
1341+
ret = MLD_ERR_FAIL;
1342+
goto cleanup;
1343+
}
1344+
13391345
pre_len = mld_prepare_domain_separation_prefix(pre, ph, phlen, ctx, ctxlen,
13401346
hashalg);
13411347
if (pre_len == 0)
@@ -1378,6 +1384,12 @@ int mld_sign_verify_pre_hash_internal(
13781384
size_t pre_len;
13791385
int ret;
13801386

1387+
if (hashalg == MLD_PREHASH_NONE)
1388+
{
1389+
ret = MLD_ERR_FAIL;
1390+
goto cleanup;
1391+
}
1392+
13811393
pre_len = mld_prepare_domain_separation_prefix(pre, ph, phlen, ctx, ctxlen,
13821394
hashalg);
13831395
if (pre_len == 0)

mldsa/src/sign.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,8 @@ __contract__(
488488
* MLD_PREHASH_SHA3_224, MLD_PREHASH_SHA3_256, MLD_PREHASH_SHA3_384,
489489
* MLD_PREHASH_SHA3_512, MLD_PREHASH_SHAKE_128, MLD_PREHASH_SHAKE_256.
490490
*
491+
* MLD_PREHASH_NONE is rejected by this API.
492+
*
491493
* @warning This is an unstable API that may change in the future. If you need
492494
* a stable API use mld_sign_signature_pre_hash_shake256.
493495
*
@@ -553,6 +555,8 @@ __contract__(
553555
* MLD_PREHASH_SHA3_224, MLD_PREHASH_SHA3_256, MLD_PREHASH_SHA3_384,
554556
* MLD_PREHASH_SHA3_512, MLD_PREHASH_SHAKE_128, MLD_PREHASH_SHAKE_256.
555557
*
558+
* MLD_PREHASH_NONE is rejected by this API.
559+
*
556560
* @warning This is an unstable API that may change in the future. If you need
557561
* a stable API use mld_sign_verify_pre_hash_shake256.
558562
*

test/src/test_mldsa.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ static int test_sign_pre_hash(void)
152152
CHECK(mld_sign_verify_pre_hash_shake256(sig, siglen, m, MLEN, ctx, CTXLEN,
153153
pk) == 0);
154154

155+
/* MLD_PREHASH_NONE must be rejected by the internal prehash APIs. */
156+
CHECK(mld_sign_verify_pre_hash_internal(sig, siglen, m, MLEN, ctx, CTXLEN, pk,
157+
MLD_PREHASH_NONE) == MLD_ERR_FAIL);
158+
CHECK(mld_sign_signature_pre_hash_internal(sig, &siglen, m, MLEN, ctx, CTXLEN,
159+
rnd, sk,
160+
MLD_PREHASH_NONE) == MLD_ERR_FAIL);
161+
155162
return 0;
156163
}
157164
#endif /* !MLD_CONFIG_NO_KEYPAIR_API && !MLD_CONFIG_NO_SIGN_API && \

0 commit comments

Comments
 (0)