Skip to content

Support non-empty context strings in ML-DSA EVP sign/verify#3135

Open
jakemas wants to merge 2 commits intoaws:mainfrom
jakemas:mldsa-evp-context-string
Open

Support non-empty context strings in ML-DSA EVP sign/verify#3135
jakemas wants to merge 2 commits intoaws:mainfrom
jakemas:mldsa-evp-context-string

Conversation

@jakemas
Copy link
Copy Markdown
Contributor

@jakemas jakemas commented Mar 31, 2026

Issues:

Addresses:

Description of changes:

Plumb FIPS 204 context strings through the EVP_DigestSign/EVP_DigestVerify code path for ML-DSA. The lower-level ml_dsa_*_sign/verify functions already accept ctx_string parameters but they were hardcoded to (NULL, 0) in the EVP layer.

Call-outs:

  • Extend PQDSA_PKEY_CTX with a context[255] buffer and context_len
  • Add pkey_pqdsa_ctrl handling EVP_PKEY_CTRL_SIGNING_CONTEXT and EVP_PKEY_CTRL_GET_SIGNING_CONTEXT (reusing the existing generic EVP_PKEY_CTX_set_signature_context API from Ed25519ph)
  • Add pkey_pqdsa_copy to support EVP_PKEY_CTX_dup with context state
  • Pass dctx->context/context_len to pqdsa_sign_message and pqdsa_verify_message instead of NULL, 0
  • Update Wycheproof test helpers to use EVP_PKEY_CTX_set_signature_context via EVP_DigestSign/Verify instead of manually computing ExternalMu
  • Add ContextString unit test covering: round-trip sign+verify with context, mismatched context failure, empty-context backward compatibility, >255 byte rejection, and max-length (255 byte) acceptance

All three ML-DSA variants (44, 65, 87) are covered. Default behavior (empty context string) is unchanged.

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.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 31, 2026

Codecov Report

❌ Patch coverage is 70.52632% with 28 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.95%. Comparing base (a75e930) to head (384cc82).

Files with missing lines Patch % Lines
crypto/fipsmodule/evp/p_pqdsa.c 41.46% 24 Missing ⚠️
crypto/evp_extra/p_pqdsa_test.cc 92.59% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3135      +/-   ##
==========================================
- Coverage   77.95%   77.95%   -0.01%     
==========================================
  Files         689      689              
  Lines      122506   122554      +48     
  Branches    17095    17092       -3     
==========================================
+ Hits        95505    95538      +33     
- Misses      26102    26120      +18     
+ Partials      899      896       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jakemas jakemas marked this pull request as ready for review March 31, 2026 20:56
@jakemas jakemas requested a review from a team as a code owner March 31, 2026 20:56
Plumb FIPS 204 context strings through the EVP_DigestSign/EVP_DigestVerify
code path for ML-DSA. The lower-level ml_dsa_*_sign/verify functions already
accept ctx_string parameters but they were hardcoded to (NULL, 0) in the
EVP layer.

Changes:
- Extend PQDSA_PKEY_CTX with a context[255] buffer and context_len
- Add pkey_pqdsa_ctrl handling EVP_PKEY_CTRL_SIGNING_CONTEXT and
  EVP_PKEY_CTRL_GET_SIGNING_CONTEXT (reusing the existing generic
  EVP_PKEY_CTX_set_signature_context API from Ed25519ph)
- Add pkey_pqdsa_copy to support EVP_PKEY_CTX_dup with context state
- Pass dctx->context/context_len to pqdsa_sign_message and
  pqdsa_verify_message instead of NULL, 0
- Update Wycheproof test helpers to use EVP_PKEY_CTX_set_signature_context
  via EVP_DigestSign/Verify instead of manually computing ExternalMu
- Add ContextString unit test covering: round-trip sign+verify with context,
  mismatched context failure, empty-context backward compatibility,
  >255 byte rejection, and max-length (255 byte) acceptance

All three ML-DSA variants (44, 65, 87) are covered. Default behavior
(empty context string) is unchanged.
@jakemas jakemas force-pushed the mldsa-evp-context-string branch from de832df to 8676c3d Compare March 31, 2026 21:48
jakemas added a commit to jakemas/aws-lc-rs that referenced this pull request Mar 31, 2026
Add sign_with_context and verify_sig_with_context methods to
PqdsaKeyPair and PqdsaVerificationAlgorithm respectively, allowing
callers to specify FIPS 204 context strings (up to 255 bytes) for
ML-DSA operations.

These methods use the existing EVP_PKEY_CTX_set_signature_context FFI
binding (already available in aws-lc-sys) via the EVP_PKEY_CTX_consumer
closure pattern. Empty contexts are equivalent to the existing
context-free sign/verify methods.

Update the mldsa_sigver_test macro to exercise verify_sig_with_context
with test vector context strings. Add dedicated test covering
round-trip sign+verify with context, mismatched context failure,
empty context backward compatibility, >255 byte rejection, and
max-length (255 byte) acceptance.

Depends on aws/aws-lc#3135.
GUARD_PTR(dctx);
GUARD_PTR(sctx);

dctx->pqdsa = sctx->pqdsa;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does a shallow copy. It works in this case because the object has static-storage (returned by PQDSA_find_dsa_by_nid()). But in general this pattern doesn't work.

Can leave as-is, but it's worth making a code comment about it - which would also help guide agents and silence bad tooling.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 384cc82

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants