Section 19.1.5

Elliptic-curve Diffie–Hellman (ECDH)

Full name: Elliptic-curve Diffie–Hellman key agreement.

Definition

ECDH is Diffie–Hellman key agreement §19.1.4 on an elliptic curve: two parties exchange public points and compute the same secret. Commonly the curve is all pairs (x,y)(x, y) of non-negative whole numbers below a prime pp for which y2y^2 and x3+ax+bx^3 + ax + b, for fixed aa and bb, leave equal remainders on division by pp, plus one extra point acting as zero. A fixed rule adds two points to give a third. A private key is a number dd; its public key dGdG is the sum of dd copies of a published point GG.

The idea

The classic form of Diffie–Hellman key agreement, published by Whitfield Diffie and Martin Hellman in 1976 [433], computes with whole numbers modulo a prime, that is, reduced to their remainders after division by the prime. It needs very large numbers, because attacks exist that use the structure of those numbers: NIST rates a 3072-bit prime at a security strength of 128 bits, meaning that the best known attack needs about 21282^{128} steps. No comparable attack is known against the standardized elliptic curves, so such a curve over a 256-bit prime reaches the same strength. A public value shrinks from 384 bytes to 64 bytes. Bluetooth Low Energy §7.1, for example, uses the curve named P-256 in its pairing method LE Secure Connections §7.21; pairing is the procedure by which two devices establish shared keys.

The rule for adding points comes from a drawing of the curve over the real numbers. A straight line through two points of the curve, unless it is vertical, meets the curve in a third point, where a point at which the line touches the curve counts twice. Reflect the third point in the x-axis, and the result is defined to be the sum of the two points. To add a point to itself, use the tangent line at the point. A vertical line joins a point to its mirror image, and the sum of those two is defined to be the extra point, called the point at infinity and written 𝒪\mathcal{O}. It behaves like zero: adding it changes nothing. With numbers modulo pp there is no drawing, only the formulas that the drawing leads to, evaluated modulo pp.

Repeated addition plays the part that repeated multiplication plays in the classic form. The sum of dd copies of GG is written dGdG, and computing it is called scalar multiplication. It does not multiply the coordinates of GG by dd. Computing dGdG from dd is fast. Recovering dd from dGdG is the elliptic-curve form of the discrete logarithm problem §19.1.3 and is believed to be infeasible on a standardized curve such as P-256.

How it works

Modulo pp means that every result is replaced by its remainder after division by pp; the whole numbers 0 to p−1p - 1 with this arithmetic form a finite field §19.1.2. The public parameters name a curve and a starting point:

To add P=(x1,y1)P = (x_1, y_1) and Q=(x2,y2)Q = (x_2, y_2), first compute a slope ss, then the sum (x3,y3)(x_3, y_3): s=y2−y1x2−x1 if P≠Q,s=3x12+a2y1 if P=Q,s = \frac{y_2 - y_1}{x_2 - x_1} \ \text{ if } P \ne Q, \qquad s = \frac{3x_1^2 + a}{2y_1} \ \text{ if } P = Q, x3=s2−x1−x2,y3=s(x1−x3)−y1.x_3 = s^2 - x_1 - x_2, \qquad y_3 = s(x_1 - x_3) - y_1. Every operation is modulo pp, and each division is a multiplication by a modular inverse. Two cases come before these formulas. If the two points have the same xx and opposite yy, which includes a point with y=0y = 0 added to itself, the sum is 𝒪\mathcal{O}. And adding 𝒪\mathcal{O} to a point leaves the point unchanged.

The exchange between two parties, here called Alice and Bob, then has six steps.

  1. Alice picks a private key dAd_A at random from 1 to n−1n - 1, computes the public key QA=dAGQ_A = d_A G and sends it.

  2. Bob picks dBd_B the same way and sends QB=dBGQ_B = d_B G.

  3. Each validates the public key received, as described under the question below.

  4. Alice computes the point dAQBd_A Q_B. Bob computes dBQAd_B Q_A. Both are the point dAdBGd_A d_B G.

  5. If that point is 𝒪\mathcal{O}, the exchange is abandoned. Otherwise its x-coordinate is the shared secret ZZ.

  6. Each feeds ZZ into a key derivation function §5.20 (KDF), an algorithm §14.1 that turns a secret value into keys, to obtain the session keys, the keys that protect the messages of this one connection, and then erases ZZ.

NIST Special Publication 800-56A specifies step 4 with one more factor: each party computes hdAQBh d_A Q_B, where hh is the cofactor. On a curve with h=1h = 1, such as P-256, the factor changes nothing.

Worked example

The curve below is far too small to be secure. Take p=17p = 17, a=2a = 2 and b=2b = 2, so that the equation is y2=x3+2x+2y^2 = x^3 + 2x + 2 modulo 17, and take G=(5,1)G = (5, 1). The constants meet the condition on aa and bb: 4×23+27×22=140=8×17+44 \times 2^3 + 27 \times 2^2 = 140 = 8 \times 17 + 4, which is not a multiple of 17. GG is on the curve: 12=11^2 = 1, and 53+2×5+2=137=8×17+15^3 + 2 \times 5 + 2 = 137 = 8 \times 17 + 1. The curve has 18 points with coordinates, and with 𝒪\mathcal{O} it has 19 points. Since 19 is prime, n=19n = 19 and h=1h = 1.

Doubling GG. The slope is (3×52+2)/(2×1)=77/2(3 \times 5^2 + 2) / (2 \times 1) = 77 / 2. Modulo 17, 77 is 77−4×17=977 - 4 \times 17 = 9, and the inverse of 2 is 9, because 2×9=18=17+12 \times 9 = 18 = 17 + 1. So s=9×9=81=4×17+13s = 9 \times 9 = 81 = 4 \times 17 + 13, which is 13. Then x3=132−5−5=159=9×17+6x_3 = 13^2 - 5 - 5 = 159 = 9 \times 17 + 6, which is 6, and y3=13×(5−6)−1=−14y_3 = 13 \times (5 - 6) - 1 = -14, which is −14+17=3-14 + 17 = 3. So 2G=(6,3)2G = (6, 3).

Adding GG and 2G2G. The slope is (3−1)/(6−5)=2(3 - 1) / (6 - 5) = 2. Then x3=22−5−6=−7x_3 = 2^2 - 5 - 6 = -7, which is −7+17=10-7 + 17 = 10, and y3=2×(5−10)−1=−11y_3 = 2 \times (5 - 10) - 1 = -11, which is −11+17=6-11 + 17 = 6. So 3G=(10,6)3G = (10, 6). Continuing in the same way gives every multiple of GG:

kk kGkG kk kGkG kk kGkG
1 (5,1)(5, 1) 7 (0,6)(0, 6) 13 (16,4)(16, 4)
2 (6,3)(6, 3) 8 (13,7)(13, 7) 14 (9,1)(9, 1)
3 (10,6)(10, 6) 9 (7,6)(7, 6) 15 (3,16)(3, 16)
4 (3,1)(3, 1) 10 (7,11)(7, 11) 16 (10,11)(10, 11)
5 (9,16)(9, 16) 11 (13,10)(13, 10) 17 (6,14)(6, 14)
6 (16,13)(16, 13) 12 (0,11)(0, 11) 18 (5,16)(5, 16)

The next multiple, 19G19G, is 𝒪\mathcal{O}.

The exchange. Alice picks dA=5d_A = 5 and sends QA=5G=(9,16)Q_A = 5G = (9, 16). Bob picks dB=7d_B = 7 and sends QB=7G=(0,6)Q_B = 7G = (0, 6). Alice computes 5QB=35G5 Q_B = 35G, and Bob computes 7QA=35G7 Q_A = 35G. Since 19G=𝒪19G = \mathcal{O} and 35=19+1635 = 19 + 16, 35G=16G=(10,11)35G = 16G = (10, 11). The shared secret is the x-coordinate, Z=10Z = 10. The scalar wrapped round at 19, the order of GG, and not at 17, the prime of the coordinates.

An invalid public key. An attacker sends Bob the pair (5,0)(5, 0). It is not on the curve: 02=00^2 = 0, while the right-hand side is 1, as computed above. It lies on a different curve, the one with 1 in place of the constant b=2b = 2, because 53+2×5+1=136=8×175^3 + 2 \times 5 + 1 = 136 = 8 \times 17. The addition formulas never use bb, so they run without complaint. The pair is its own mirror image, because its yy is 0, so adding it to itself gives 𝒪\mathcal{O}: it behaves as a point of order 2. The true curve has no such point, since a group of 19 elements allows only the orders 1 and 19. Bob’s result 7×(5,0)7 \times (5, 0) is (5,0)(5, 0), because 7 is odd; for an even private key it would be 𝒪\mathcal{O}. So Bob’s shared secret is the x-coordinate 5 unless Bob’s private key is even. The attacker knows the secret that Bob will use without knowing Bob’s private key, and learns whether that key is even. Repeating the trick with pairs of other small orders yields further information about a private key that is used more than once.

Stated precisely

Correctness: for all whole numbers dAd_A and dBd_B, dA(dBG)=(dAdB)G=dB(dAG).d_A (d_B G) = (d_A d_B) G = d_B (d_A G). This holds in every group and needs no assumption.

Secrecy against an attacker who only observes QAQ_A and QBQ_B rests on two named conditions. The computational Diffie–Hellman problem, to compute dAdBGd_A d_B G from dAGd_A G and dBGd_B G, is hard in the subgroup generated by GG. And the private keys are uniformly random in the range 1 to n−1n - 1 and stay secret. Against an attacker who can also send a public key of its own making, a third condition is needed, so that the result reveals nothing about the receiver’s private key: each public key received has been validated.

NIST Special Publication 800-56A calls the checking of a received public key public-key validation. It defines full validation of a public key Q=(xQ,yQ)Q = (x_Q, y_Q) as four conditions, explained under the question below: Q≠𝒪,0≤xQ,yQ≤p−1,yQ2≡xQ3+axQ+b(modp),nQ=𝒪.Q \ne \mathcal{O}, \qquad 0 \le x_Q, y_Q \le p - 1, \qquad y_Q^2 \equiv x_Q^3 + a x_Q + b \pmod{p}, \qquad nQ = \mathcal{O}. Here u≡v(modp)u \equiv v \pmod{p} means that u−vu - v is a multiple of pp. Partial validation is the first three. When h=1h = 1, the fourth condition follows from the first three: the curve then has exactly nn points, nn is prime, and so every point other than 𝒪\mathcal{O} has order nn.

Limits and common mistakes

Validation shows that a public key is a usable point. It does not show who sent it. Without authentication §1.6, ECDH is open to the same man-in-the-middle attack §7.26 as the classic form: an attacker between the two parties runs a separate exchange with each.

The width of the coordinates is not the security strength. A 256-bit curve gives a strength of about 128 bits, because the best known attack needs about n\sqrt{n} steps.

Scalar multiplication must not reveal dd through its running time or its power use. A loop that does extra work for every 1 bit of dd can leak the key through a side channel §1.29.

The checks depend on the curve. The function X25519, defined in RFC 7748 [434], uses a curve with a different equation, sends one coordinate only and is specified to accept every 32-byte string as a public value. Its curve has cofactor 8, and an input of small order produces a shared secret of all zero bytes. Version 1.3 of Transport Layer Security (TLS), the Internet protocol that protects connections between clients and servers, requires implementations to detect that value and abort.

ECDH gives no protection against a large quantum computer. NIST warns that such machines will threaten Diffie–Hellman key agreement in both its classic form and its elliptic-curve form, and that both may then have to be replaced.

Questions answered

What must be checked about an input public key?

A received public key must be shown to be a point in the agreed encoding, other than the point at infinity, that lies on the agreed curve and in the agreed subgroup, before it is combined with the private key. In ECDH (elliptic-curve Diffie–Hellman key agreement) a public key is a point Q=(x,y)Q = (x, y) that should lie on an agreed curve y2=x3+ax+by^2 = x^3 + ax + b modulo a prime pp, and the point at infinity is the extra point of the curve that acts as zero. NIST Special Publication 800-56A lists four checks, for a curve whose base point has prime order nn.

  1. Not the identity. QQ is not the point at infinity. Otherwise the shared point is the point at infinity whatever the private key is.

  2. Coordinates in range. xx and yy are whole numbers from 0 to p−1p - 1, in the agreed encoding and length.

  3. On the curve. y2y^2 and x3+ax+bx^3 + ax + b leave the same remainder on division by pp. The addition formulas do not use bb, so a pair that fails this check is processed as a point of some other curve, where it can have a small order and leak part of the private key.

  4. In the subgroup. The sum of nn copies of QQ is the point at infinity. On a curve with cofactor 1, such as P-256, this follows from the first three checks; the cofactor is the number of points on the curve divided by nn. On a curve with a larger cofactor, the protocol either makes this check, or multiplies by the cofactor and rejects a result that is the point at infinity.

After the computation, the shared point must not be the point at infinity. For the key agreement function X25519, which uses a curve of a different form, the corresponding check is that the shared secret is not all zero bytes. The exact list belongs to the curve and the protocol. The Bluetooth specification, for example, requires every device to check that a received public key satisfies the equation of the curve P-256.

These checks concern arithmetic only. A public key that passes all of them can still come from an attacker, so the exchange must also be authenticated.

Related entries: Group, cyclic subgroup, generator, and order (§19.1.1), Authenticated key exchange and transcript binding (§19.1.6), LE Secure Connections (§7.21), Finite field and modular inverse (§19.1.2), Discrete logarithm and the Diffie–Hellman problem (§19.1.3), Diffie–Hellman key agreement (§19.1.4). Further reading: [18, 147, 152, 433, 434].