Skip to content

feat: add ECDSA and ECDH over P-256, P-384, P-521 and secp256k1 - #192

Open
WingZer0o wants to merge 2 commits into
mainfrom
feat/162-ecdsa-ecdh
Open

WingZer0o wants to merge 2 commits into
mainfrom
feat/162-ecdsa-ecdh

Conversation

@WingZer0o

@WingZer0o WingZer0o commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Closes #162.

What

A new ecc module with ECDSA and ECDH over NIST P-256, P-384, P-521 and secp256k1, built on RustCrypto p256 / p384 / p521 / k256 0.14 and ecdsa 0.17.

ECDSA (ecc::cas_ecdsa::CASEcdsa): CASEcdsaP256 (ES256), CASEcdsaP384 (ES384), CASEcdsaP521 (ES512), CASEcdsaSecp256k1 (ES256K)

  • generate_key_pair, sign / verify (IEEE P1363 r || s, as JOSE, COSE and WebCrypto use) and sign_der / verify_der (strict ASN.1 DER, as X.509, TLS and OpenSSL use).
  • Each function hashes the message with the curve's own hash, so callers pass the message, not a digest.
  • Signing is deterministic (RFC 6979).
  • verify* returns Ok(false) for any bad or malformed signature. It returns Err(InvalidPemKey) only when the public key can't be parsed or is for another curve.
  • secp256k1: sign always produces low-S signatures. verify / verify_der follow standard ECDSA and also accept high-S. The extra verify_low_s / verify_der_low_s reject high-S, as BIP-62 / BIP-146 require.

ECDH (ecc::cas_ecdh::CASEcdh): CASEcdhP256, CASEcdhP384, CASEcdhP521, CASEcdhSecp256k1

  • generate_key_pair and diffie_hellman(my_private_pem, peer_public_pem), which returns the raw x-coordinate Z (the same value WebCrypto deriveBits and OpenSSL EVP_PKEY_derive return).
  • The peer key is validated: on the curve, not the identity. Compressed points are accepted.
  • The docs recommend passing the shared secret through HKDF, and the example does that.

Keys are PEM: private keys PKCS#8 (SEC1 EC PRIVATE KEY is also accepted), public keys SPKI. Shared code is in ecc/keys.rs, and ecc/types.rs defines EcKeyPair.

Conventions followed

  • Trait in cas_<module>.rs, impl in <algorithm>.rs, declared in lib.rs with no re-exports.
  • CasResult everywhere and no panics. No new CasError variants; the module reuses InvalidPemKey, KeyGenerationFailed and SigningFailed.
  • The private key PEM passed in is zeroized once parsed. The parsed SecretKey / SigningKey / SharedSecret zeroize on drop. EcKeyPair redacts private_key in Debug and implements Zeroize, like the PQC structs.

Tests

  • Wycheproof (tests/wycheproof.rs):
    • ECDSA: ecdsa_secp{256r1_sha256,384r1_sha384,521r1_sha512,256k1_sha256}_test.json and their _p1363 variants, plus ecdsa_secp256k1_sha256_bitcoin_test.json run against verify_der_low_s.
    • ECDH: the ecdh_secp{256r1,384r1,521r1}_pem_test.json files and ecdh_secp256k1_test.json.
  • NIST CAVP (tests/data/ecc/, consumed by tests/ecc.rs): FIPS 186-4 SigVer.rsp and PKV.rsp, and SP 800-56A KAS_ECC_CDH_PrimitiveTest.txt, trimmed to the supported curves and hashes. The provenance and SHA-256 checksums are in tests/data/ecc/README.md.
  • RFC 6979 A.2.5–A.2.7 deterministic signatures.
  • Round trips, wrong-curve and malformed keys, malformed signatures, SEC1 input, high-S behavior, and Debug / zeroize in tests/zeroize.rs.

cargo test --locked passes locally.

Notes for reviewers

  • The ECDH shared secret is returned as a plain Vec<u8>, not Zeroizing. The issue asked for Zeroizing. This follows the CLAUDE.md convention that returned secrets stay plain buffers for the FFI layers to zero, and it matches X25519's diffie_hellman. The internal SharedSecret zeroizes on drop.
  • For the NIST curves, the ECDH Wycheproof tests use the _pem files instead of the plain ecdh_secp*_test.json. They hold the same cases in the form the API takes. secp256k1 has no _pem file, so it uses the plain file, and the test wraps the keys in PEM. This is documented in tests/data/wycheproof/README.md.
  • Wycheproof files for other hashes (SHA-3, SHAKE, mismatched SHA-2) are skipped, since each curve is bound to one hash. NIST has no secp256k1 KATs.
  • New dependencies: p256, p384, p521, k256, ecdsa, elliptic-curve, all with default features off.
  • Also fixes RUSTSEC-2026-0620 (published today, and failing cargo audit on main too). curve25519-dalek 5.0.0, pulled in by ed25519-dalek, x25519-dalek and hpke, computes wrong scalar multiplication results in its AVX-512 backend. The lockfile moves to 5.1.0 with a targeted cargo update -p curve25519-dalek, and the crate moves from dev-dependencies to dependencies with a 5.1.0 floor so downstream crates can't resolve 5.0.0. It's in this PR, so the squash commit won't get a separate Security line in the changelog; consider noting it in the release PR. cargo audit --deny unsound --deny yanked passes locally.
  • Downstream: feat: export ECDSA and ECDH (P-256, P-384, P-521, secp256k1) over the C FFI cas-core-lib#157, feat: ECDSA and ECDH (P-256, P-384, P-521, secp256k1) cas-dotnet-sdk#265, feat: ECDSA and ECDH (P-256, P-384, P-521, secp256k1) cas-typescript-sdk#200.

🤖 Generated with Claude Code

WingZer0o and others added 2 commits November 27, 2026 21:48
curve25519-dalek 5.0.0 computes wrong scalar multiplication results in
its AVX-512 backend. It reaches cas-lib through ed25519-dalek,
x25519-dalek and hpke. Update the lockfile with a targeted
`cargo update -p curve25519-dalek`, and move the crate from
dev-dependencies to dependencies with a 5.1.0 floor so downstream crates
(cas-core-lib and the SDKs) can't resolve the vulnerable version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

ECDSA and ECDH over P-256, P-384, P-521 and secp256k1

1 participant