This is one of the most elegant ideas in all of computer science. Two people can agree on a shared secret while communicating over a completely public channel, and an eavesdropper who sees every message they exchange still canāt figure out the secret.
Imagine Alice and Bob want to agree on a secret color. Theyāre communicating by shouting across a crowded room. Everyone can hear them.
Hereās how they do it:
They both arrived at the same color. But an eavesdropper only heard āyellow,ā āorange,ā and āgreen.ā To figure out the final color, theyād need to un-mix the colors to find Aliceās red or Bobās blue. And un-mixing paint is really hard.
Thatās Diffie-Hellman. Replace ācolorsā with ānumbersā and āmixingā with āmodular exponentiation,ā and you have the real algorithm.
The actual math uses modular arithmetic. Hereās the concept without getting into the equations:
p and a generator g. These are public.
Everyone knows them.a. She computes
g^a mod p and sends the result to Bob. Call this
A.b. He computes
g^b mod p and sends the result to Alice. Call this
B.B and computes
B^a mod p. This gives her the shared secret.A and computes
A^b mod p. This gives him the same shared secret.Both arrive at g^(ab) mod p. But an eavesdropper who
knows g, p, A, and B
would need to figure out a or b to compute the
shared secret. This is the discrete logarithm problem, and for large
enough numbers, itās computationally infeasible.
sequenceDiagram
participant A as Alice
participant E as Eavesdropper
participant B as Bob
Note over A,B: Public: g and p (everyone knows these)
A->>A: Pick secret a
A->>B: A = g^a mod p
Note over E: Sees A, but can't find a
B->>B: Pick secret b
B->>A: B = g^b mod p
Note over E: Sees B, but can't find b
A->>A: Shared secret = B^a mod p
B->>B: Shared secret = A^b mod p
Note over A,B: Both have the same shared secret
Note over E: Can't compute the shared secret
Hereās where it gets really important for TLS.
In the basic version, Alice and Bob could reuse their secret numbers
a and b for multiple sessions. But thatās
dangerous. If an attacker ever discovers a (through a
server compromise, a bug, or a future breakthrough), they can go back
and decrypt every session that used that a.
Ephemeral Diffie-Hellman (DHE) solves this. āEphemeralā means temporary. Both sides generate new random numbers for every single session. After the session ends, the ephemeral keys are discarded.
This means even if an attacker compromises the server and steals everything on it, they canāt decrypt past sessions. The ephemeral keys are gone. Each sessionās secret dies with the session.
This property is called forward secrecy (sometimes called perfect forward secrecy). Itās one of the most important security properties in TLS, and itās the reason TLS 1.3 mandates ephemeral key exchange.
Classic Diffie-Hellman uses modular exponentiation with large prime numbers. It works, but the numbers need to be very large (2048+ bits) for adequate security, which makes the computation slower.
ECDHE (Elliptic Curve Diffie-Hellman Ephemeral) uses elliptic curve math instead. It provides the same security with much smaller numbers. A 256-bit elliptic curve provides roughly the same security as a 3072-bit classic DH group.
Smaller numbers mean faster computation and less data to transmit. This is why ECDHE is the standard key exchange in modern TLS. The most common curves are:
When you see a cipher suite with āECDHEā in the name, it means the key exchange uses Elliptic Curve Diffie-Hellman with ephemeral keys. This gives you forward secrecy.
Diffie-Hellman is the foundation of key exchange in TLS. Every modern TLS connection uses some form of it. Understanding how it works, and especially why ephemeral keys matter, is essential for understanding the handshake, forward secrecy, and the quantum threat.
Speaking of which: the security of Diffie-Hellman depends on the difficulty of the discrete logarithm problem. Quantum computers can solve this problem efficiently using Shorās algorithm. Thatās why post-quantum key exchange algorithms are being developed. Weāll cover that in Part X.
Next: Digital Signatures