9. Diffie-Hellman Key Exchange
Agreeing on a secret over a public channel
In 1976, Whitfield Diffie and Martin Hellman published a protocol that seems impossible: two people can agree on a shared secret by communicating entirely in the open. An eavesdropper who sees every message they exchange still can't figure out the secret. This is the Diffie-Hellman key exchange — the foundation of every secure connection on the internet.
The Paint Mixing Analogy
Before diving into the math, consider this analogy:
Agreeing on a color without revealing it
Public: Alice and Bob agree on a common color: YELLOW
Secret: Alice picks a secret color: RED
Bob picks a secret color: BLUE
Step 1: Alice mixes YELLOW + RED → ORANGE (sends to Bob)
Bob mixes YELLOW + BLUE → GREEN (sends to Alice)
Step 2: Alice mixes GREEN + RED → BROWN
Bob mixes ORANGE + BLUE → BROWN
Both arrive at BROWN — the same color!
An eavesdropper sees: YELLOW, ORANGE, GREEN
But can't unmix the paints to find RED or BLUE,
so can't compute BROWN.
The key insight: mixing paint is easy, but unmixing it is practically impossible. Diffie-Hellman uses a mathematical operation with the same property: modular exponentiation.
The Protocol
-
Agree on public parameters
Choose a large prime
pand a generatorg(a number whose powers produce all values mod p). These are public. -
Each party picks a secret
Alice chooses random secret
a. Bob chooses random secretb. These are never shared. -
Compute and exchange public values
Alice computes
A = g^a mod pand sends A to Bob.
Bob computesB = g^b mod pand sends B to Alice. -
Compute the shared secret
Alice computes
s = B^a mod p = g^(ab) mod p.
Bob computess = A^b mod p = g^(ab) mod p.
Both arrive at the same value!
A Small Worked Example
Toy Diffie-Hellman
Public parameters: p = 23, g = 5
Alice's secret: a = 6
Bob's secret: b = 15
Alice computes: A = 5^6 mod 23 = 15625 mod 23 = 8
Bob computes: B = 5^15 mod 23 = ... = 19
Alice sends 8 to Bob. Bob sends 19 to Alice.
Alice computes: s = 19^6 mod 23 = 2
Bob computes: s = 8^15 mod 23 = 2
Shared secret: s = 2 ✓ (Both get the same value!)
An eavesdropper sees: p=23, g=5, A=8, B=19
To find 's', they'd need to find 'a' from 5^a ≡ 8 (mod 23)
— the discrete logarithm problem.
Why It's Secure: The Discrete Logarithm Problem
Easy Forward, Hard Backward
Computing g^a mod p is fast (using repeated squaring). But given
g, p, and g^a mod p, finding a is the
discrete logarithm problem — believed to be computationally infeasible
for large primes (2048+ bits).
Easy (microseconds)
Given g=5, a=6, p=23:
Compute 56 mod 23 = 8
Hard (infeasible)
Given g=5, result=8, p=23:
Find a such that 5a ≡ 8 (mod 23)
For the toy example above, you could just try all values of a from 1 to 22. But when p is a 2048-bit prime (a number with 617 digits), there are more possibilities than atoms in the observable universe. The best known algorithms (General Number Field Sieve) would take longer than the age of the universe.
The Man-in-the-Middle Problem
Diffie-Hellman has one critical weakness: it provides no authentication. An attacker (Eve) can intercept the exchange and perform two separate key exchanges:
Man-in-the-middle attack
Alice ──A──→ Eve ──E₁──→ Bob
Alice ←─E₂── Eve ←──B── Bob
Eve performs DH with Alice using (E₂, a) → shared secret s₁
Eve performs DH with Bob using (E₁, b) → shared secret s₂
Alice thinks she's talking to Bob, but Eve can read everything.
Eve decrypts with s₁, re-encrypts with s₂, and forwards.
The solution: authenticate the exchange using digital signatures (RSA or ECDSA) or certificates. This is exactly what TLS does — it combines Diffie-Hellman for key exchange with RSA/ECDSA signatures for authentication.
Forward Secrecy
Why DH Matters More Than RSA for Key Exchange
With RSA key exchange, if an attacker records encrypted traffic and later steals the server's private key, they can decrypt all past sessions. With ephemeral Diffie-Hellman (DHE or ECDHE), each session generates fresh random secrets that are discarded afterward. Even if the long-term key is compromised, past sessions remain safe. This property is called forward secrecy.
RSA Key Exchange
Server key compromised → all past sessions decryptable. No forward secrecy.
Ephemeral DH (DHE/ECDHE)
Server key compromised → past sessions still safe. Forward secrecy.
This is why TLS 1.3 requires ephemeral Diffie-Hellman and completely removed RSA key exchange. Forward secrecy is no longer optional.
DH Groups and Parameter Sizes
| Group | Prime size | Security level | Status |
|---|---|---|---|
| Group 1 | 768 bits | ~60 bits | Broken |
| Group 2 | 1024 bits | ~80 bits | Deprecated (likely broken by nation-states) |
| Group 14 | 2048 bits | ~112 bits | Minimum acceptable |
| Group 16 | 4096 bits | ~152 bits | Conservative choice |
| ECDHE (P-256) | 256 bits | ~128 bits | Preferred — see next page |
Notice that ECDHE achieves 128-bit security with a 256-bit key, while classic DH needs 3072+ bits. This efficiency is why elliptic curve Diffie-Hellman has largely replaced traditional DH.
Real-World Usage
| Protocol | How it uses DH |
|---|---|
| TLS 1.3 | ECDHE is mandatory for all key exchanges |
| SSH | DH or ECDH for session key agreement |
| Signal Protocol | X3DH (Extended Triple DH) for initial key agreement, Double Ratchet for ongoing |
| IPsec / IKE | DH key exchange in IKE phase 1 |
| WireGuard | ECDH with Curve25519 (Noise protocol framework) |
Try It Yourself — Diffie-Hellman Key Exchange
Choose public parameters and secret values for Alice and Bob, then watch them arrive at the same shared secret without ever revealing their secrets.
Eavesdropper's View
Eve is listening to the entire exchange. Here's what she can and cannot see.
Discrete Logarithm Challenge
Given the public values, try to find the secret exponent by brute force. Enter a target value (a public value from the exchange above) and watch the search.
Key Takeaways
- Diffie-Hellman lets two parties compute a shared secret over a public channel
- Security relies on the discrete logarithm problem: ga mod p is easy to compute but hard to reverse
- DH provides no authentication — must be combined with signatures to prevent man-in-the-middle attacks
- Ephemeral DH (DHE/ECDHE) provides forward secrecy: past sessions stay safe even if long-term keys are compromised
- TLS 1.3 requires ECDHE — forward secrecy is now mandatory for web traffic
Classic Diffie-Hellman uses modular arithmetic with large primes, requiring 2048+ bit numbers for adequate security. Elliptic curve cryptography achieves the same security with numbers 10x smaller — and opens the door to elegant new constructions.