Diffie-Hellman key exchange
Two people who have never met can agree on a shared secret key while an eavesdropper hears everything they say, and the secret itself is never sent.
The paint-mixing analogy
Analogy, not real cryptographyMixing paint is easy, but separating a mixture back into its colours is practically impossible. Real Diffie-Hellman uses a mathematical operation with the same property.
Real key agreement with ECDH
Genuine ECDH P-256 Simulated networkECDH (Elliptic-Curve Diffie-Hellman) is the modern form of Diffie-Hellman used in HTTPS. Every key below is genuinely generated by your browser.
Attack: Mallory in the middle
Genuine ECDH and AES-GCM Simulated interceptionEve only listens. Mallory is worse: she sits between Alice and Bob and can replace the messages they exchange.
Key ideas
Key agreement, not key transport
Nobody chooses the key and sends it. Both sides compute the same secret from their own private value and the other side's public value.
Public values
The agreed parameters (here the curve P-256) and each side's public value can be seen by anyone.
Private values
Each side's private value never leaves its device. Without at least one private value, the shared secret cannot be computed.
Ephemeral keys
HTTPS generates new Diffie-Hellman values for every connection. Even if a server's long-term key is stolen later, recorded traffic stays protected (forward secrecy).
No authentication
Basic Diffie-Hellman does not tell you who you share the secret with. A man in the middle can run one exchange with each side. The public values must be authenticated with signatures and certificates.
What is ECDH?
ECDH is Diffie-Hellman built on elliptic curves. Same idea, much shorter keys. The original 1976 version uses large numbers instead.
Common misconceptions
Where you meet Diffie-Hellman
HTTPS (TLS 1.3)
Every HTTPS connection starts with an ephemeral (EC)DH exchange. The server signs it so the browser knows the exchange is really with that server.
VPNs
IPsec (IKE) and WireGuard use Diffie-Hellman to agree on tunnel keys.
Messaging apps
Signal, WhatsApp and others use repeated Diffie-Hellman exchanges so every message gets new keys.
SSH
Each SSH connection agrees on session keys with Diffie-Hellman, and the server's host key authenticates it. That is why SSH warns you when the host key changes.
Challenges
1. Eve's notebook
After the ECDH exchange, list everything in Eve's notebook. What is missing for her to compute the secret?
Show answer
2. Spot the man in the middle
In the attack scenario, compare the secret Alice computed with the one Bob computed. How could they notice the attack?
Show answer
3. New connection, new secret
Restart the ECDH exchange several times. Is the shared secret the same each time? Why is this useful?