Expand description
ECDH key agreement over the NIST prime-order curves (SP 800-56A), as the Web Cryptography API serves it.
Keys returned here drive key-agreement.secret-key.agree, exactly as
X25519’s do: the derive-input it returns feeds derive-bits, the
key-minting interfaces’ derive-key, and the per-hash KDF prepare
interfaces. The shared secret is the x-coordinate of the agreed point
(SP 800-56A’s Z), so the natural output length is the curve’s field
size: 32 bytes for P-256, 48 bytes for P-384. Keys report
algorithm-name "ECDH" regardless of curve, and agree fails
error.invalid-key on a curve-mismatched peer — the same derive-time
check the key-agreement kind documents for algorithm-mismatched
peers.
Formats follow the format-admission rule (see README.md, “Design
notes”): every format is one a platform-backed host passes to the
platform verbatim. The public key travels as an uncompressed SEC1
point (WebCrypto’s public-only raw format for EC keys); the secret
key travels as PKCS#8 or an EC private JWK. Bare secret scalars have
no platform door and are not a format here.
Security:
- Public imports are strict, unlike X25519’s deliberately permissive
raw import: a point not on the declared variant’s curve fails
error.invalid-keyat import, as it does on the platform. A valid point multiplied by a valid scalar on these prime-order curves cannot produce the point at infinity, so a degenerate peer is rejected where it enters rather than surfacing atagree(the kind’s contributory check stays satisfied by construction).
Enums§
- Ecdh
Variant - The served curves. (Each algorithm interface names its closed set
of parameterizations
<algorithm>-variant; ECDH’s parameter is the curve alone.)
Functions§
- generate_
key - Generate a fresh key pair on the declared variant’s curve.
- import_
public_ key_ jwk - Import a public key as an EC public JWK (
kty: "EC", withcrv,x, andy).jwkis the JWK as JSON text; seemac-key.export-key-jwkfor the package-wide JWK contract (algis ignored entirely, WebCrypto’s rule for the ECDH family). The JWK’scrvmust match the declared variant’s curve (error.invalid-keyotherwise), and the encoded point is admitted exactly asimport-public-key-rawadmits it. - import_
public_ key_ raw - Import a public key as an uncompressed SEC1 point (
04 ‖ x ‖ y; 65 bytes for P-256, 97 bytes for P-384 — WebCrypto’srawformat). Anything else — including compressed points and points not on the declared variant’s curve — fails witherror.invalid-key.public-key.export-key-rawreturns this same form. - import_
public_ key_ spki - Import a public key as an X.509 SubjectPublicKeyInfo (DER). The
curve must be named by OID and must match the declared variant’s,
or the import fails with
error.invalid-key: an encoding that carries explicit ECParameters instead of the named-curve OID is rejected even when the parameters describe the variant’s curve. A point not on the curve is always rejected. An uncompressed point is always accepted; whether a compressed encoding is accepted is implementation-defined, as it is across WebCrypto engines — do not rely on either behavior. - import_
secret_ key_ jwk - Import a static secret key as an EC private JWK (
kty: "EC", withcrv,d, and the public coordinatesx/y, which RFC 7518 makes mandatory — this is inherently the public+private form).jwkis the JWK as JSON text; seemac-key.export-key-jwkfor the package-wide JWK contract, includingextvalidation against the options’ extractability.dis the curve’s scalar and must lie in[1, n-1](error.invalid-keyotherwise). - import_
secret_ key_ pkcs8 - Import a static secret key as a PKCS#8 PrivateKeyInfo (DER, the
RFC 5915 ECPrivateKey body). The encoded curve must match the
declared variant’s (
error.invalid-key), and the scalar must lie in[1, n-1]; an embedded public key, when present, is validated against the scalar and never trusted on its own. - unwrap_
secret_ key_ jwk - Mint a static secret key from unwrapped key material (see the
wrappinginterface):input’s bytes are read as an EC private JWK, subject toimport-secret-key-jwk’s contract plus the unwrap-pathuse/key_opschecks (seeREADME.md, “JWK contract”).inputis consumed. - unwrap_
secret_ key_ pkcs8 - Mint a static secret key from unwrapped key material read as a
PKCS#8 PrivateKeyInfo, subject to
import-secret-key-pkcs8’s contract.inputis consumed; seeunwrap-secret-key-jwkfor the options model.