ResearchCryptography

Elliptic-curve key validation from P-256 to X25519

A public key can have the expected length and still be unsuitable for ECDH. Follow a peer key through point validation and the TLS all-zero check.

ret2basic.eth4 min read
Elliptic-curve key validation from P-256 to X25519

An ECDH library hands the handshake a byte string. What justifies treating it as a shared secret? For P-256, part of the answer is a check on the peer’s public point. For X25519 in TLS 1.3, the protocol also specifies what the implementation must do if the computed shared secret is all zero. An API that says only “32 bytes in, 32 bytes out” leaves the caller guessing which guarantees it supplies.

The agreement behind ECDH#

Alice chooses a private scalar aa and publishes A=[a]GA=[a]G. Bob publishes B=[b]GB=[b]G. For valid keys in the agreed group, Alice computes [a]B[a]B and Bob computes [b]A[b]A. Both obtain [ab]G[ab]G. The group and the interpretation of the peer’s input are assumptions of that equality. Successful scalar multiplication alone does not establish where the input came from or authenticate the peer.

A subgroup is a set of points closed under the group operation. The order of a point is the smallest positive number of additions that returns it to the identity. If a curve group has order hnhn and the base point generates a subgroup of prime order nn, then hh is its cofactor. The intended public keys live in the subgroup generated by GG. A low-order input can restrict the possible shared results. An input outside the agreed curve violates an earlier assumption still. Those are the two distinct concerns behind small-subgroup and invalid-curve analysis.

What a P-256 key import has to establish#

For a conventional short-Weierstrass public key, full public-key validation checks that the point is not the identity, its coordinates represent field elements, it satisfies the equation of the named curve, and it belongs to the required subgroup. An encoded point can have the expected length and still fail a mathematical check. A parser also has to enforce the encoding permitted by the enclosing protocol.

P-256 has cofactor one. Once a finite point has been properly decoded and validated on that curve, it lies in the prime-order group. That explains why an implementation can establish subgroup membership there without an additional independent multiplication. The same inference needs re-examination on a curve with a larger cofactor. Use the named-group validation provided by the cryptographic library and check what its import API promises. NIST SP 800-56A Rev. 3, §5.6.2.3 · NIST curve parameters

Why X25519 has a different acceptance rule#

X25519 accepts a 32-byte encoded Montgomery uu coordinate and specifies its own decoding and scalar processing. Applying a P-256 affine-point parser to that input would implement a different interface. In particular, RFC 7748 specifies handling for non-canonical field encodings. A universal “reject every non-canonical encoding” policy would change interoperability for X25519.

The primitive and its caller have separate responsibilities. RFC 7748 describes checking whether the shared result is all zero. TLS 1.3 requires that check for X25519 and X448 and requires aborting the handshake on an all-zero result. A KDF will happily accept a string of zero bytes, so handing the result to a KDF cannot stand in for the TLS check. RFC 7748 §§5–6 · TLS 1.3 §7.4.2

Follow a key across the API boundary#

Question for the reviewerP-256 in TLS 1.3X25519 in TLS 1.3
What did the peer send?The TLS encoding of an uncompressed point on the negotiated groupA 32-byte key share with RFC 7748 decoding rules
Which layer establishes acceptance?Named-group point validation before the key is usedThe X25519 operation and the TLS all-zero-result check
What can the application do on failure?End this handshake without accepting session keysEnd this handshake without accepting session keys

For a concrete integration review, follow one valid handshake from decoding to key derivation. Then follow a library validation failure to the caller. In the first case both peers can reach the key schedule. In the second case the caller must propagate the failure before any session is accepted. A library that offers a high-level key-agreement API may already perform the required checks. An integration that selects a lower-level operation owns the checks left to the protocol. TLS 1.3 key shares

Worth sharing?

Send this analysis to another reviewer.

Share on X