Skip to Content
oxcrypto

ECDH

Elliptic-curve Diffie-Hellman over the NIST, secp256k1 and Brainpool curves.

from oxcrypto.exchange.ecdh import ECDH

alice = ECDH.P256.generate()
bob = ECDH.P256.generate()
shared = alice.exchange(bob.public_key())
assert shared == bob.exchange(alice.public_key())

ECDH has P224, P256, P384, P521, SECP256K1, BRAINPOOL_P256R1, BRAINPOOL_P384R1, BRAINPOOL_P256T1 and BRAINPOOL_P384T1. exchange returns the x-coordinate of the shared point (the SEC 1 section 3.3.1 primitive, NIST SP 800-56A "ECC CDH"), as many bytes as the field size.

Keys use the same formats as ECDSA keys: raw scalars and SEC1 points, PKCS#8 (optionally PBES2-encrypted with password) and SEC1 private keys, SPKI public keys, each in DER and PEM, with compressed or uncompressed points. A key for one family is read by the other family's loaders, since the encodings are the same. Encrypted PKCS#8 keys are subject to the same key-derivation limits.

Upstream

CurveCrateStandardAudit
P-224, P-256, P-384, P-521p224 0.14.0-rc.15 (pre-release), p256/p384/p521 0.14.0NIST SP 800-56A, SEC 1No published audit
secp256k1k256 0.14.0SEC 1NCC Group review of an earlier release
brainpoolP256r1, brainpoolP384r1, brainpoolP256t1, brainpoolP384t1bp256/bp384 0.14.0RFC 5639No published audit

All curves use elliptic-curve 0.14.3, which builds with Rust 1.88. The crates are written to perform secret-dependent operations in constant time; oxcrypto has not verified this independently.

Tests use the C2SP Wycheproof ECDH vectors (SPKI-encoded peer keys, including invalid points and wrong curves) and the NIST CAVP KAS ECC CDH primitive vectors for P-224 to P-521. Wycheproof and CAVP have no brainpool t1 vectors, so those curves are tested against shared secrets computed with OpenSSL 3.0.2.

See Key exchange for zeroization notes.

On this page