Diffie-Hellman & Key Exchange

How two parties derive a shared secret over a wiretapped channel, why it needs authentication to mean anything, and the parameter mistakes — small groups, static exponents, non-prime-order subgroups — that broke it in production.

Diffie–Hellman (1976) was the first published solution to a problem that had looked impossible: two parties who have never met, communicating entirely over a channel an adversary can read, end up holding the same secret. It is the mechanism behind forward secrecy in TLS, the handshake in Signal and WireGuard, and — with the group swapped for an elliptic curve — essentially every session key negotiated on the internet today.

The protocol #

Agree publicly on a large prime pp and a generator gg. These are not secret and are typically shared by everyone.

Alice picks secret a, sends A=ga mod pBob picks secret b, sends B=gb mod pAlice computes Ba=gba,Bob computes Ab=gab\begin{aligned} &\text{Alice picks secret } a, \text{ sends } A = g^a \bmod p \\ &\text{Bob picks secret } b, \text{ sends } B = g^b \bmod p \\ &\text{Alice computes } B^a = g^{ba}, \quad \text{Bob computes } A^b = g^{ab} \end{aligned}

Both hold gab mod pg^{ab} \bmod p. An eavesdropper has p,g,A,Bp, g, A, B and must compute gabg^{ab} from gag^a and gbg^b — the Computational Diffie–Hellman problem. Solving the discrete logarithm would break it; whether the two are equivalent is still open.

Unauthenticated DH is trivially MITM'd #

DH gives you a shared secret with somebody. It says nothing about who.

Mallory runs two exchanges and relays. Neither side detects anything. The fix is always to authenticate the exchange: sign the DH share (digital signatures + a certificate, as TLS does), or bind it to a pre-shared identity key (Signal's X3DH, WireGuard's Noise handshake). Anonymous DH ciphersuites were removed from TLS for exactly this reason.

Why ephemeral: forward secrecy #

If both sides reuse long-term DH secrets (static DH), one later key compromise decrypts every past session an attacker recorded. With ephemeral exponents — fresh aa, bb per handshake, discarded afterwards (DHE/ECDHE) — a compromised long-term signing key lets an attacker impersonate you going forward but cannot retroactively decrypt captured traffic. This is forward secrecy, and it is the single strongest argument for DH over RSA key transport. TLS 1.3 made it mandatory by deleting RSA key exchange from the protocol.

Parameter choice is where it breaks #

Requirement Why Failure if ignored
pp large: 2048 bits minimum index calculus is sub-exponential 512-bit groups broken in minutes (Logjam)
p−1p-1 has a large prime factor; ideally safe prime p=2q+1p = 2q+1 Pohlig–Hellman reduces DLP to the prime factors of the group order smooth p−1p-1 → discrete log becomes easy
Validate received shares small-subgroup confinement attacker sends a low-order element and forces a guessable shared secret
Fresh exponents per session forward secrecy one key compromise unravels all recorded traffic

Practical guidance today: use X25519 (RFC 7748) unless a compliance regime forces finite-field DH; if it does, use RFC 7919 named groups at 2048+ bits and never a self-generated or hardcoded prime of unknown provenance.

Key agreement is a protocol, not one multiplication #

NIST SP 800-56A Rev. 3 makes explicit what textbook DH leaves implicit. A conforming key-establishment scheme includes approved domain parameters, key-pair generation, assurances about private and public keys, a shared-secret computation, a key-derivation method, identifiers/context, error handling, and—where required—key confirmation.

The derived keying material should bind the parties and protocol purpose through FixedInfo or an equivalent transcript/context input. Otherwise the same mathematical shared secret can be interpreted under different roles or protocols. Key confirmation adds a MAC under derived confirmation material so one party proves it actually derived the same key; it is distinct from identity authentication.

Static and ephemeral keys also change the guarantees:

Pairing Typical property
ephemeral–ephemeral forward secrecy, but needs separate authentication
static–ephemeral authenticates the static-key holder when correctly bound; asymmetric forward-secrecy properties
static–static no forward secrecy; compromise exposes recorded exchanges

The publication was issued in 2018, and NIST announced in January 2026 that a revision is planned to align it with SP 800-186 and newer requirements. Use Rev. 3 for the current complete scheme mechanics while checking the NIST publication page for transition updates.

ElGamal: DH turned into an encryption scheme #

ElGamal (1985) is Diffie–Hellman restructured so the sender needn't wait for a reply. The recipient publishes (p,g,y)(p, g, y) where y=gx mod py = g^x \bmod p and xx is private. To encrypt, the sender picks a random kk and sends the pair:

(c1,c2)=(gk mod p, m⋅yk mod p)(c_1, c_2) = \left(g^k \bmod p,\ m \cdot y^k \bmod p\right)

The recipient computes c1x=gkx=ykc_1^x = g^{kx} = y^k and divides it out. In practice yky^k is used as a symmetric key for a real cipher rather than multiplied into the message, which makes it a hybrid scheme.

Source claims audit #

Seeded from a university lab manual (Diffie–Hellman / ElGamal lab). Status of its load-bearing claims:

Claim from source Status Notes
DH protocol, gabg^{ab} derivation, reliance on discrete log hardness current Matches the 1976 paper
"Equivalence of CDH and DLP is not proven" current Still open
Requirement that p−1p-1 contain a large prime factor; p=2q+1p = 2q+1 construction current Correct and under-taught; Pohlig–Hellman is the reason
"Users may share the same pp and gg; enables precomputation to speed things up" needs correction — inverted The source frames shared parameters as an optimization for the user. Logjam showed the same precomputation is the attacker's leverage. Shared primes are now a documented liability
ElGamal: key setup, encryption/decryption steps, need to erase kk current Correctly emphasizes fresh kk per message
Requirement to use a true RNG or CSPRNG, not a plain PRNG current Understated if anything — this is the #1 practical break
"ElGamal underlies the digital signature standards of the USA and Russia" needs nuance DSA descends from ElGamal/Schnorr; Russia's GOST R 34.10 is now elliptic-curve. Neither is "ElGamal"
Channel described as "protected from substitution" current but easy to miss This is the authentication requirement — the source never explains MITM. Added above
Source omits key sizes, forward secrecy, share validation, Logjam — Added; parameter choice is where deployments actually fail

Sources

  1. paperNew Directions in Cryptography (Diffie & Hellman, 1976)
  2. paperImperfect Forward Secrecy: How Diffie-Hellman Fails in Practice (Adrian et al., CCS 2015)
  3. docsRFC 7919 — Negotiated Finite Field DHE Parameters for TLS
  4. docsRFC 7748 — Elliptic Curves for Security (X25519)
  5. docsNIST SP 800-56A Rev. 3 — Pair-Wise Key Establishment Using Discrete Logarithm Cryptographypdfnotes

Searches titles, aliases, headings, summaries, and full text.↑↓ move ↵ open esc close