✏️ Diffie–Hellman: exercises and in depth
Overview · Belongs to 1. The mathematics, section 4 · Previous: ← RSA · Next: Elliptic curves in depth →
Diffie–Hellman is the idea that both SSH and TLS rely on for every connection. The formula is one line; the subtle part is which numbers you may use. This page lets you play the exchange, then attack it in three ways, each of which happened to real software. You need clock arithmetic and generators. Answers are hidden under Answer.
1. The exchange
Publicly agreed: a prime $p$ and a base $g$.
| Alice | everybody can see | Bob | |
|---|---|---|---|
| secret | $a$ | $b$ | |
| sends | $A = g^a \bmod p$ | $A$, $B$ | $B = g^b \bmod p$ |
| calculates | $K = B^a \bmod p$ | $K = A^b \bmod p$ |
Both get $K = g^{ab}$, since $(g^b)^a = g^{ba} = g^{ab} = (g^a)^b$.
Exercise 1.1. $p = 23$, $g = 5$. Play both sides for (a) $a = 4$, $b = 3$ and (b) $a = 9$, $b = 13$. Use the table of powers of 5 from part 1.
Answer
(a) $A = 5^4 \equiv 4$, $B = 5^3 \equiv 10$. Alice: $10^4 = 10000 \equiv 18$. Bob: $4^3 = 64 \equiv 18$. $K = 18$. Check in the table: $5^{12} \equiv 18$, and $ab = 12$ ✓
(b) $A = 5^9 \equiv 11$, $B = 5^{13} \equiv 21$. Both get $K = 17$. Check: $ab = 117$, $117 \bmod 22 = 7$, and $5^7 \equiv 17$ ✓ (exponents count modulo $p - 1 = 22$, by Fermat).
Exercise 1.2. A slightly bigger one: $p = 467$, $g = 2$, $a = 153$, $b = 197$. Calculate $A$, $B$ and $K$ with Python’s pow.
Answer
$A = 2^{153} \bmod 467 = $ 224, $B = 2^{197} \bmod 467 = $ 87, $K = 87^{153} = 224^{197} \bmod 467 = $ 367. (467 is a safe prime: $467 = 2 \cdot 233 + 1$ with 233 prime. More on that below.)
2. The group: why the order must be a large prime
The numbers $1 \dots p-1$ under multiplication modulo $p$ form a group with $p - 1$ elements. By Lagrange’s rule, every element has an order that divides $p - 1$. For $p = 23$, $p - 1 = 22 = 2 \cdot 11$:
| order | elements |
|---|---|
| 1 | 1 |
| 2 | 22 (that is, $-1$) |
| 11 | 2, 3, 4, 6, 8, 9, 12, 13, 16, 18 |
| 22 | 5, 7, 10, 11, 14, 15, 17, 19, 20, 21 |
The elements of order 11 together with 1 are exactly the squares modulo 23 (the quadratic residues). They form a subgroup of 11 elements: multiply two squares and you get a square again.
Exercise 2.1. With $g = 5$ (order 22), does $A = g^a$ leak anything about $a$? Hint: compute $A^{11}$.
Answer
$A^{11} = 5^{11a} = (5^{11})^a = (-1)^a$. So $A^{11} \equiv 1$ if $a$ is even and $-1$ if $a$ is odd: the listener learns the last bit of $a$. In general, every small factor of the group order leaks a bit of the secret. That is why careful protocols use $g$ of prime order $q$ (for $p = 23$: $g = 2$, order 11), and why safe primes $p = 2q + 1$ are popular: the only small factor is 2, and the subgroup of order $q$ is huge.
3. Attack 1: Pohlig–Hellman, when the group order has only small factors
If the group order splits into small primes, a discrete logarithm splits into small, easy pieces.
Worked example. $p = 31$, $g = 3$ (a generator, order $30 = 2 \cdot 3 \cdot 5$). Solve $3^x \equiv 22 \pmod{31}$.
The trick: raising to the power $30/q$ throws away everything except “$x \bmod q$”.
| $q$ | $g^{30/q}$ | $22^{30/q}$ | powers of $g^{30/q}$ | so $x \bmod q =$ |
|---|---|---|---|---|
| 2 | $3^{15} \equiv 30$ | $22^{15} \equiv 30$ | $1, 30$ | 1 |
| 3 | $3^{10} \equiv 25$ | $22^{10} \equiv 5$ | $1, 25, 5$ | 2 |
| 5 | $3^{6} \equiv 16$ | $22^{6} \equiv 8$ | $1, 16, 8, 4, 2$ | 2 |
Now find the $x$ below 30 with $x \equiv 1 \pmod 2$, $x \equiv 2 \pmod 3$, $x \equiv 2 \pmod 5$ (the Chinese remainder theorem): $x \equiv 2 \pmod{15}$ gives 2 or 17, and it must be odd, so $x = 17$. Check: $3^{17} \bmod 31 = 22$ ✓
Instead of searching 30 possibilities, we searched $2 + 3 + 5 = 10$. For a real prime, the cost is set by the largest prime factor of the group order. Hence the rule: the group order must have a large prime factor (at least 256 bits), and $g$ should live in that subgroup.
Exercise 3.1. Why is $p = 23$ a better choice than $p = 31$ for Diffie–Hellman (ignoring that both are tiny)?
Answer
$p - 1 = 22 = 2 \cdot 11$: the largest prime factor is 11, out of 22. For 31, $p - 1 = 30 = 2 \cdot 3 \cdot 5$: the largest factor is only 5. A safe prime $p = 2q + 1$ makes the largest factor as big as possible: about half of $p$.
4. Attack 2: small subgroup confinement
Bob does not check what Alice sends. Mallory replaces Alice’s $A$ by $22$ (that is, $-1$, the element of order 2).
Exercise 4.1. With $p = 23$: Bob calculates $K = 22^b$. Which values can $K$ have? What does Mallory learn?
Answer
$22^b \equiv (-1)^b$, so $K$ is 1 or 22. Mallory can guess the “secret” key in one try out of two, and learns the last bit of $b$ from which one works. Sending $A = 1$ is even worse: $K = 1$ always, and Bob’s whole session key is known.
The defence: check what you receive. Reject $A \le 1$ and $A \ge p - 1$, and if you use a prime-order subgroup, check $A^q \equiv 1$. Real cases: a 2017 study (Valenta et al.) found many TLS and IPsec servers that skipped these checks, and the Bluetooth pairing flaw of 2018 was the elliptic-curve version of the same mistake. Curve25519 makes this problem mostly disappear by design (see elliptic curves).
5. Attack 3: the man in the middle
Diffie–Hellman alone does not say who you are talking to. Mallory sits between Alice and Bob and runs two exchanges.
Exercise 5.1. $p = 23$, $g = 5$, Alice $a = 6$ ($A = 8$), Bob $b = 15$ ($B = 19$). Mallory picks $e = 9$, so $E = 5^9 \equiv 11$, and sends $E$ to both instead of $A$ and $B$. Which key does Alice share with Mallory, and which does Bob? Can Mallory read everything?
Answer
- Alice thinks $E$ is Bob’s value: $K_{A} = 11^6 \bmod 23 = $ 9. Mallory calculates the same: $A^e = 8^9 \equiv 9$.
- Bob thinks $E$ is Alice’s value: $K_{B} = 11^{15} \bmod 23 = $ 10. Mallory: $B^e = 19^9 \equiv 10$.
Mallory decrypts each message from Alice with key 9, reads (or changes) it, and re-encrypts it with key 10 for Bob. Neither notices anything. That is why the exchange is always signed: in SSH the server signs the exchange hash with its host key; in TLS with the key from its certificate. Mallory cannot make that signature.
6. Forward secrecy
Exercise 6.1. Mallory recorded the whole conversation of exercise 1.1(a) ($A = 4$, $B = 10$). A year later she breaks into Alice’s computer. Alice has deleted $a$ after the conversation, but Mallory gets Alice’s long-term signing key. Can she now read the recorded conversation?
Answer
No. The session key $K = 18$ depended only on $a$ and $b$, which both sides threw away. The signing key only proved who Alice was; it was never used to encrypt anything. To get $K$, Mallory still has to solve $5^a \equiv 4$. This is forward secrecy, and it is why TLS 1.3 removed the older “RSA key transport” mode, where the session key was encrypted with the server’s long-term RSA key: steal that key later, and every recorded session opens.
7. Real groups
Nobody picks their own $p$ any more; protocols use fixed, published groups that have been checked by many people:
| Group | Size | Used in |
|---|---|---|
| RFC 3526 group 14 | 2048-bit safe prime, $g = 2$ | diffie-hellman-group14-sha256 in SSH |
RFC 7919 ffdhe2048 … ffdhe8192 | safe primes, $g = 2$ | TLS 1.3 (rarely; elliptic curves are preferred) |
| RFC 2409 group 2 (Oakley) | 1024-bit | broken in practice: the Logjam research (2015) showed that a nation-state can precompute for one 1024-bit prime and then break every connection using it |
| Curve25519 (X25519) | 255-bit curve | almost everything modern: elliptic curves |
The fixed primes are also chosen in a “nothing up my sleeve” way. Group 14, for example, is
$$p = 2^{2048} - 2^{1984} - 1 + 2^{64} \cdot \left( \lfloor 2^{1918} \pi \rfloor + 124476 \right)$$The digits of $\pi$ make it impossible to have hidden a weakness in the choice. The offset $124476$ was chosen to make $p$ a safe prime.
You can see what your own SSH client offers:
ssh -Q kex # every key exchange your OpenSSH supports
Check yourself in Python
p, g = 23, 5
a, b = 9, 13
A, B = pow(g, a, p), pow(g, b, p) # 11, 21
pow(B, a, p), pow(A, b, p) # (17, 17)
{x: next(k for k in range(1, 23) if pow(x, k, 23) == 1) for x in range(1, 23)} # orders
[pow(22, b, 23) for b in range(1, 5)] # [22, 1, 22, 1]: small subgroup