Skip to content

fix(ext/node): DSA keygen with arbitrary modulusLength - #34206

Merged
littledivy merged 2 commits into
mainfrom
orch/divybot-135
May 18, 2026
Merged

littledivy merged 2 commits into
mainfrom
orch/divybot-135

Conversation

@divybot

@divybot divybot commented May 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

crypto.generateKeyPair('dsa', { modulusLength: N }) previously only worked for the four fixed sizes the Rust dsa crate exposes via KeySize (1024/160, 2048/224, 2048/256, 3072/256). Anything else — including the 2049/256 pair Node.js's test suite exercises — was rejected with TypeError: Invalid modulusLength+divisorLength combination.

This patch:

  • Vendors the (p, q, g) parameter generation in ext/node_crypto/keys.rs so it accepts arbitrary L and N. The algorithm is the same one the dsa crate uses internally (FIPS 186-4 §A.1.1.2 for the prime pair, Appendix A.2.1 for the unverified generator). The resulting components are handed to dsa::Components::from_components — that constructor is public, so we sidestep the crate's pub(crate) KeySize fields without forking the crate or pinning on an upstream PR.
  • Adds basic input validation (L > N ≥ 2, both fit in u32).
  • Enables parallel/test-crypto-keygen-bit-length.js in tests/node_compat/config.jsonc.

Verified locally: DSA 2049/256 keygen succeeds (~1.7s), sign/verify round-trips, and PKCS#8 PEM export works.

Closes denoland/orchid#135

Test plan

  • cargo test --test node_compat -- test-crypto-keygen-bit-length passes
  • cargo clippy -p deno_node_crypto --all-targets clean
  • DSA 2049/256 sign+verify round-trip works
  • DSA 2049/256 PKCS#8 PEM export works

The `dsa` crate only exposes a fixed set of `KeySize` variants
(1024/160, 2048/224, 2048/256, 3072/256), so
`crypto.generateKeyPair('dsa', { modulusLength: 2049, divisorLength: 256 })`
rejected anything outside that list. Node accepts arbitrary bit
lengths.

Generate the (p, q, g) parameters in-tree by mirroring the algorithm
the `dsa` crate uses internally (FIPS 186-4 §A.1.1.2 for p/q,
Appendix A.2.1 for g), then hand them to `Components::from_components`
which has public visibility. This sidesteps the crate's
`pub(crate)` `KeySize` fields without forking it.

Enables `parallel/test-crypto-keygen-bit-length.js` in the
node compat suite.

Co-Authored-By: Divy Srivastava <me@littledivy.com>
@divybot divybot changed the title node compat: DSA key generation with arbitrary bit lengths fix(ext/node): DSA keygen with arbitrary modulusLength May 17, 2026

@littledivy littledivy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@littledivy

Copy link
Copy Markdown
Member

@divybot resolve conflicts

Co-Authored-By: Divy Srivastava <me@littledivy.com>
@littledivy
littledivy enabled auto-merge (squash) May 18, 2026 03:35
@littledivy
littledivy merged commit f68849e into main May 18, 2026
136 checks passed
@littledivy
littledivy deleted the orch/divybot-135 branch May 18, 2026 03:43
littledivy added a commit to crowlKats/deno that referenced this pull request Jun 10, 2026
## Summary

`crypto.generateKeyPair('dsa', { modulusLength: N })` previously only
worked for the four fixed sizes the Rust `dsa` crate exposes via
`KeySize` (1024/160, 2048/224, 2048/256, 3072/256). Anything else —
including the 2049/256 pair Node.js's test suite exercises — was
rejected with `TypeError: Invalid modulusLength+divisorLength
combination`.

This patch:

- Vendors the `(p, q, g)` parameter generation in
`ext/node_crypto/keys.rs` so it accepts arbitrary `L` and `N`. The
algorithm is the same one the `dsa` crate uses internally (FIPS 186-4
§A.1.1.2 for the prime pair, Appendix A.2.1 for the unverified
generator). The resulting components are handed to
`dsa::Components::from_components` — that constructor is public, so we
sidestep the crate's `pub(crate)` `KeySize` fields without forking the
crate or pinning on an upstream PR.
- Adds basic input validation (L > N ≥ 2, both fit in `u32`).
- Enables `parallel/test-crypto-keygen-bit-length.js` in
`tests/node_compat/config.jsonc`.

Verified locally: DSA 2049/256 keygen succeeds (~1.7s), sign/verify
round-trips, and PKCS#8 PEM export works.

Closes denoland/orchid#135

## Test plan

- [x] `cargo test --test node_compat -- test-crypto-keygen-bit-length`
passes
- [x] `cargo clippy -p deno_node_crypto --all-targets` clean
- [x] DSA 2049/256 sign+verify round-trip works
- [x] DSA 2049/256 PKCS#8 PEM export works

Co-authored-by: divybot <divybot@users.noreply.github.com>
Co-authored-by: Divy Srivastava <me@littledivy.com>
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.

2 participants