Skip to content

Commit 5bc7a2f

Browse files
authored
Merge pull request #1584 from HackTricks-wiki/update_We_found_cryptography_bugs_in_the_elliptic_library_20251118_123829
We found cryptography bugs in the elliptic library using Wyc...
2 parents f994872 + 3e28fd6 commit 5bc7a2f

File tree

1 file changed

+32
-1
lines changed
  • src/crypto-and-stego/cryptographic-algorithms

1 file changed

+32
-1
lines changed

src/crypto-and-stego/cryptographic-algorithms/README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,38 @@ Check **3 comparisons to recognise it**:
180180

181181
![](<../../images/image (430).png>)
182182

183-
{{#include ../../banners/hacktricks-training.md}}
183+
## Elliptic-Curve Signature Implementation Bugs
184+
185+
### EdDSA scalar range enforcement (HashEdDSA malleability)
186+
187+
- FIPS 186-5 §7.8.2 requires HashEdDSA verifiers to split a signature `sig = R || s` and reject any scalar with `s \geq n`, where `n` is the group order. The `elliptic` JS library skipped that bound check, so any attacker that knows a valid pair `(msg, R || s)` can forge alternate signatures `s' = s + k·n` and keep re-encoding `sig' = R || s'`.
188+
- The verification routines only consume `s mod n`, therefore all `s'` congruent to `s` are accepted even though they are different byte strings. Systems treating signatures as canonical tokens (blockchain consensus, replay caches, DB keys, etc.) can be desynchronized because strict implementations will reject `s'`.
189+
- When auditing other HashEdDSA code, ensure the parser validates both the point `R` and the scalar length; try appending multiples of `n` to a known-good `s` to confirm the verifier fails closed.
190+
191+
### ECDSA truncation vs. leading-zero hashes
192+
193+
- ECDSA verifiers must use only the leftmost `log2(n)` bits of the message hash `H`. In `elliptic`, the truncation helper computed `delta = (BN(msg).byteLength()*8) - bitlen(n)`; the `BN` constructor drops leading zero octets, so any hash that begins with ≥4 zero bytes on curves like secp192r1 (192-bit order) appeared to be only 224 bits instead of 256.
194+
- The verifier right-shifted by 32 bits instead of 64, producing an `E` that does not match the value used by the signer. Valid signatures on those hashes therefore fail with probability ≈`2^-32` for SHA-256 inputs.
195+
- Feed both the “all good” vector and leading-zero variants (e.g., Wycheproof `ecdsa_secp192r1_sha256_test.json` case `tc296`) to a target implementation; if the verifier disagrees with the signer, you found an exploitable truncation bug.
184196

197+
### Exercising Wycheproof vectors against libraries
198+
- Wycheproof ships JSON test sets that encode malformed points, malleable scalars, unusual hashes and other corner cases. Building a harness around `elliptic` (or any crypto library) is straightforward: load the JSON, deserialize each test case, and assert that the implementation matches the expected `result` flag.
185199

200+
```javascript
201+
for (const tc of ecdsaVectors.testGroups) {
202+
const curve = new EC(tc.curve);
203+
const pub = curve.keyFromPublic(tc.key, 'hex');
204+
const ok = curve.verify(tc.msg, tc.sig, pub, 'hex', tc.msgSize);
205+
assert.strictEqual(ok, tc.result === 'valid');
206+
}
207+
```
186208

209+
- Failures should be triaged to distinguish spec violations from false positives. For the two bugs above, the failing Wycheproof cases immediately pointed at missing scalar range checks (EdDSA) and incorrect hash truncation (ECDSA).
210+
- Integrate the harness into CI so that regressions in scalar parsing, hash handling, or coordinate validity trigger tests as soon as they are introduced. This is especially useful for high-level languages (JS, Python, Go) where subtle bignum conversions are easy to get wrong.
211+
212+
## References
213+
214+
- [Trail of Bits - We found cryptography bugs in the elliptic library using Wycheproof](https://blog.trailofbits.com/2025/11/18/we-found-cryptography-bugs-in-the-elliptic-library-using-wycheproof/)
215+
- [Wycheproof Test Suite](https://github.com/C2SP/wycheproof)
216+
217+
{{#include ../../banners/hacktricks-training.md}}

0 commit comments

Comments
 (0)