Section 19.1.4

Diffie–Hellman key agreement

Definition

Diffie–Hellman key agreement (DH) is a method by which two parties who share no secret in advance each send one public value over a channel anyone can read, and compute the same secret. Each party combines its own private random number with the other’s public value. With standardized parameters, an eavesdropper who sees both public values has no known feasible way to compute the secret. Whitfield Diffie and Martin Hellman published the method in 1976 [433]. It protects against eavesdropping only: neither party learns who sent the public value it received.

The idea

Encryption with a shared key is fast, but both sides must hold the key first. Before this method, the key had to travel by a protected route, such as a courier. A phone and a headset that have never met, or a browser and a web server on opposite sides of the world, have no such route. Everything they send can be recorded.

The core idea is a calculation that can be done in two halves, in either order, with the same result. Raising to a power has that property: for a public value gg and any two whole numbers aa and bb, (ga)b=gab=(gb)a(g^a)^b = g^{ab} = (g^b)^a. Call the two parties Alice and Bob. Alice chooses aa, does Alice’s half in private and sends the result gag^a. Bob chooses bb, does Bob’s half in private and sends gbg^b. Each then applies their own half to what the other sent, and both hold gabg^{ab}. An eavesdropper holds two half-finished results and neither private number. The method is called key agreement because both parties contribute to the secret and neither can choose it alone. It is also called Diffie–Hellman key exchange.

A common picture uses paint. Both parties start from one public color. Each stirs in a private color and sends the mixture, and each then stirs that same private color into the mixture received. Both end with the same three-color mix, which an onlooker cannot unmix. The picture stops being accurate in one respect: unmixing paint is physically hard, while reversing the calculation is only believed to be hard, and only in groups chosen to resist every known attack.

How it works

The two parties first agree on public parameters, for example by naming a standard set: a group, an element gg of it, and the order qq of gg, which is a large prime. A group is a set with a multiplication-like operation. The order of gg is the number of different values that the powers of gg take, and those values form the cyclic subgroup §19.1.1 in which the exchange takes place. In the classic form the group is the whole numbers 1 to p−1p - 1 for a large prime pp, multiplied modulo pp: every product is replaced by its remainder after division by pp. In the elliptic-curve §19.1.5 form it is a group of points on a curve.

  1. Alice picks a private number aa at random from 1 to q−1q - 1, computes the public value A=gaA = g^a and sends AA.

  2. Bob picks a private number bb the same way, computes B=gbB = g^b and sends BB.

  3. Each checks the value received: it must be an element of the agreed group whose order is qq.

  4. Alice computes Z=BaZ = B^a. Bob computes Z=AbZ = A^b. Both results equal gabg^{ab}, the shared secret.

  5. Each feeds ZZ into a key derivation function §5.20 (KDF), an algorithm §14.1 that turns a secret value into keys of the needed length, together with public context such as both public values. The outputs are the session keys, the keys that protect the messages of this one connection.

  6. Each erases ZZ, and erases its private number if that number was made for this one exchange.

A private number made for one exchange is called ephemeral. One kept for many exchanges is called static. Either kind must come from a CSPRNG §5.27 (cryptographically secure pseudorandom number generator), a generator whose output an attacker cannot predict.

Worked example

The numbers below are far too small to be secure. They are chosen so that every step can be checked by hand. The public parameters are p=23p = 23, g=2g = 2 and q=11q = 11: the powers of 2 modulo 23 take 11 different values. Modulo 23 means that every product is replaced by its remainder after division by 23.

Both hold 12, and neither private number crossed the channel. The value 12 is 2212^{21} reduced modulo 23, since 3×7=213 \times 7 = 21.

The same exchange with an intermediary. An attacker, here called Mallory, controls the channel. Mallory picks a private number m=5m = 5, with public value 25=32=23+92^5 = 32 = 23 + 9, which is 9. Mallory keeps Alice’s 8 and Bob’s 13 and sends 9 to both.

Alice shares 16 with Mallory, and Bob shares 4 with Mallory. Alice and Bob share nothing, and neither of them can tell.

Stated precisely

Let gg be an element of prime order qq in a group. For private numbers aa and bb and public values A=gaA = g^a and B=gbB = g^b, Ba=(gb)a=gab=(ga)b=Ab.B^a = (g^b)^a = g^{ab} = (g^a)^b = A^b. This equality holds in every group and needs no assumption.

The secrecy claim is narrower and conditional. An attacker who only observes AA and BB cannot compute Z=gabZ = g^{ab}, provided that all of the following hold.

For the derived keys to be indistinguishable from random keys, one of two further conditions is needed. Either the stronger decisional Diffie–Hellman assumption holds in the group, which says that gabg^{ab} cannot be told apart from a random element, and the key derivation function turns a random group element into keys that look random; or the key derivation function is treated in the proof as an ideal random function, one whose output for each new input is an independent random value.

Nothing is claimed about identity. NIST Special Publication 800-56A says of the schemes in which both parties use only ephemeral values that they give neither party any assurance of the accuracy of an identifier for the other.

Limits and common mistakes

Diffie–Hellman key agreement authenticates nobody. The answer below describes the attack, and the entry on authenticated key exchange §19.1.6 describes the repair.

The shared secret ZZ is not a key. It is a group element with mathematical structure, not a string of uniformly random bits. NIST Special Publication 800-56A permits it to be used only as the input of a key derivation method.

A received public value must be checked. If an attacker sends the value 1, the shared secret is 1 whatever the private number is, and values of small order leave only a few possible secrets. For the classic form, NIST’s full check is that the received value yy satisfies 2≤y≤p−22 \le y \le p - 2 and that yqy^q leaves remainder 1 on division by pp. NIST also requires the computation to stop with an error when the shared value comes out as 0, 1 or p−1p - 1.

The private numbers carry all the secrecy. A predictable random number generator gives the secret away with no attack on the mathematics.

Using ephemeral private numbers and erasing them after the exchange gives forward secrecy §5.33: a later theft of a device’s long-lived secrets does not reveal the keys of past sessions. Static private numbers do not give it.

Sizes matter. The example group has 11 elements. The smallest groups of the classic form that NIST approves use a 2048-bit prime pp.

The secrecy rests on problems that a large quantum computer could solve. NIST warns that such machines will threaten Diffie–Hellman key agreement, which may then have to be replaced.

Questions answered

Why is unauthenticated DH vulnerable to an active intermediary?

Unauthenticated DH is vulnerable to an active intermediary because nothing in the exchange shows who sent a public value, so an attacker who can intercept and replace messages can complete one separate exchange with each party. DH is short for Diffie–Hellman key agreement, in which two parties each send a public value and compute a shared secret from their own private number and the value received. Unauthenticated means that the public values travel with no signature or other proof of origin. Each party then knows only that it shares a secret with whoever produced the value it received.

The attack runs as follows, with the two parties called Alice and Bob. Alice sends a public value toward Bob. The attacker, here called Mallory, keeps it and sends Bob a public value of Mallory’s own instead. Bob replies with a public value. Mallory keeps that too and sends Alice another value of Mallory’s own. Alice now shares one secret with Mallory, and Bob shares a different secret with Mallory. When Alice sends an encrypted message, Mallory decrypts it with the first key, reads or changes it, encrypts it with the second key and passes it on. Both parties see a working encrypted connection. This is called a man-in-the-middle attack §7.26, abbreviated MITM attack.

Mallory solves no hard problem. She takes part in two honest exchanges, and the mathematics works as designed in each. The defect is the missing link between a public value and an identity. The repair is to authenticate the public values. Each party can attach a digital signature §5.14 (a value that only the holder of a private key can compute and that anyone can check with the matching public key), made with a long-term private key whose public half the other party already trusts. Or each can compute a message authentication code §5.7 (MAC), a short keyed check value, over the public values with a key shared in advance. Or each device can derive a short code from both public values and display it, and the user checks by eye that the two codes match, a comparison that an attacker on the channel cannot alter; the Bluetooth method named Numeric Comparison §7.24 does this with a six-digit number.

Related entries: Discrete logarithm and the Diffie–Hellman problem (§19.1.3), Authenticated key exchange and transcript binding (§19.1.6), Key derivation function (KDF) (§5.20), Group, cyclic subgroup, generator, and order (§19.1.1), Elliptic-curve Diffie–Hellman (ECDH) (§19.1.5), MITM attack (§7.26), Forward secrecy (§5.33). Further reading: [18, 147, 152, 433].