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 and any two whole numbers and , . Call the two parties Alice and Bob. Alice chooses , does Alice’s half in private and sends the result . Bob chooses , does Bob’s half in private and sends . Each then applies their own half to what the other sent, and both hold . 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 of it, and the order of , which is a large prime. A group is a set with a multiplication-like operation. The order of is the number of different values that the powers of 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 for a large prime , multiplied modulo : every product is replaced by its remainder after division by . In the elliptic-curve §19.1.5 form it is a group of points on a curve.
Alice picks a private number at random from 1 to , computes the public value and sends .
Bob picks a private number the same way, computes and sends .
Each checks the value received: it must be an element of the agreed group whose order is .
Alice computes . Bob computes . Both results equal , the shared secret.
Each feeds 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.
Each erases , 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 , and : 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.
Alice picks and sends .
Bob picks and sends , which is 13.
Alice computes . First , giving 8. Then , giving .
Bob computes . First , so is 18. Then , so is 2. Since , is , giving .
Both hold 12, and neither private number crossed the channel. The value 12 is reduced modulo 23, since .
The same exchange with an intermediary. An attacker, here called Mallory, controls the channel. Mallory picks a private number , with public value , which is 9. Mallory keeps Alice’s 8 and Bob’s 13 and sends 9 to both.
Alice computes , so Alice’s secret is 16. Mallory computes : is 2 as above, and .
Bob computes . First , so is 12. Then , so is 6. Then is , so Bob’s secret is 4. Mallory computes : is 8 as above, so is , which is 18 as above, and .
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 be an element of prime order in a group. For private numbers and and public values and , This equality holds in every group and needs no assumption.
The secrecy claim is narrower and conditional. An attacker who only observes and cannot compute , provided that all of the following hold.
The computational Diffie–Hellman problem §19.1.3 is hard in the chosen group: no feasible algorithm computes from and .
The private numbers and are chosen uniformly at random, are kept secret and are not leaked by the implementation.
The attacker only reads messages, and does not alter, replace or insert them.
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 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 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 satisfies and that leaves remainder 1 on division by . NIST also requires the computation to stop with an error when the shared value comes out as 0, 1 or .
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 .
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].