Skip to content

[Fix] verify: use the leftmost bits of the hash for DSA, like sign - #103

Open
bardrop wants to merge 1 commit into
browserify:mainfrom
bardrop:fix-dsa-verify-hash-truncation
Open

bardrop wants to merge 1 commit into
browserify:mainfrom
bardrop:fix-dsa-verify-hash-truncation

Conversation

@bardrop

@bardrop bardrop commented Nov 12, 2026

Copy link
Copy Markdown

Fixes #102.

dsaSign reduces the hash with bits2int(hash, q) (keep the leftmost N bits, N = bit length of q), but dsaVerify used the whole digest: new BN(hash).mul(w).mod(q). So when the hash is longer than q, valid signatures were rejected, both the ones this package makes and the ones node makes:

  • 1024/160 key: DSA-SHA224, DSA-SHA256, DSA-SHA384, DSA-SHA512 all failed
  • 2048/256 key: DSA-SHA384 and DSA-SHA512 failed

Now dsaVerify does the same truncation before using the hash (FIPS 186-4, 4.7).

Tests: a new test signs and verifies with both DSA fixtures (160-bit and 256-bit q) for SHA-224/256/384/512, checks a wrong message is still rejected, and cross-checks with node in both directions. 12 of its asserts fail on main and pass with the fix. npm test is green (100% coverage), and I also ran tape on node 0.12, 16, 22 and 26.

When the hash is longer than q (DSA-SHA256 with a 160-bit q, or
SHA-384/512 with a 224/256-bit q), dsaVerify used the whole digest
while dsaSign uses bits2int, so valid signatures from this package and
from node were rejected.

Fixes browserify#102
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.

DSA verify does not truncate the digest to the bit length of q, so valid signatures are rejected

1 participant