Diffie-Hellman Key Exchange

Diffie-Hellman (DH), invented in 1976 by Whitfield Diffie and Martin Hellman, lets 2 parties agree on a shared secret key using only publicly exchanged information. Security rests on the difficulty of computing discrete logarithms.

This is the Discrete Logarithm Problem (DLP) over the finite field Zp∗\mathbb{Z}_p^*: recovering the secret aa from ga mod pg^a \bmod p is computationally intractable for large pp, while computing ga mod pg^a \bmod p from aa is cheap.

The interactive guide to Diffie-Hellman key exchange walks through the exchange and the discrete logarithm problem behind it.

Public Parameters

  • A large prime modulus pp, defining the multiplicative group Zp∗\mathbb{Z}_p^*.
  • A generator gg, a primitive root modulo pp.

gg is a primitive root modulo pp iff every aa coprime to pp (i.e. gcd⁡(a,p)=1\gcd(a,p) = 1) satisfies a≡gi(modp)a \equiv g^i \pmod p for some integer ii, called the discrete logarithm of aa to base gg.

For prime pp, the p−1p - 1 values i=1,…,p−1i = 1, \dots, p-1 generate {1,…,p−1}\{1, \dots, p-1\}, in some order, via gi mod pg^i \bmod p.

Choosing gg as a primitive root maximises the order of the group it generates to p−1p - 1, so its powers span every non-zero residue class modulo pp and stay in no small subgroup, where the discrete logarithm would be easy to solve.

Choosing a Modulus

pp is chosen as a safe prime: a prime of the form p=2q+1p = 2q + 1 where qq is also prime. (p,q)(p,q) are a Sophie-Germain prime pair.

Reasons:

  • Pohlig-Hellman reduces the discrete logarithm modulo pp to one discrete logarithm per prime factor of p−1p - 1. A safe prime forces p−1=2qp - 1 = 2q, leaving only the factors 22 and the large prime qq.
  • No small-order subgroup exists, so an attacker cannot confine the shared secret to a short list of values.
  • Every g≠±1g \neq \pm 1 has order qq or 2q2q, so almost any candidate generates a large group.

Choosing a Generator

Factor p−1p - 1 into its distinct prime factors q1,…,qkq_1, \dots, q_k. Then gg is a primitive root modulo pp iff

g(p−1)/qi≢1(modp)for every ig^{(p-1)/q_i} \not\equiv 1 \pmod p \quad \text{for every } i
Here:
qiq_i
a distinct prime factor of p−1p - 1

Test g=2,3,5,…g = 2, 3, 5, \dots in turn. Primitive roots are dense, so a small gg passes after few tries. Each candidate costs kk modular exponentiations.

Factoring p−1p - 1 is the hard step, so pp is chosen in advance to keep it known.

A safe prime is a prime p=2q+1p = 2q + 1 where qq is also prime. Then p−1=2qp - 1 = 2q has 2 prime factors, so gg is a primitive root iff g2≢1(modp)g^2 \not\equiv 1 \pmod p and gq≢1(modp)g^q \not\equiv 1 \pmod p. Published standards use safe primes and usually take g=2g = 2.

For p=23p = 23, p−1=22=2⋅11p - 1 = 22 = 2 \cdot 11. Testing g=5g = 5 gives 52≡25^2 \equiv 2 and 511≡225^{11} \equiv 22. Neither is 11, so 55 is a primitive root. g=2g = 2 fails, since 211≡1(mod23)2^{11} \equiv 1 \pmod{23}.

Key Pair Generation

Each party chooses a secret integer and publishes its exponentiation modulo pp.

  • Alice chooses secret a∈Zp∗a \in \mathbb{Z}_p^*, computes public A≡ga(modp)A \equiv g^a \pmod p.
  • Bob chooses secret b∈Zp∗b \in \mathbb{Z}_p^*, computes public B≡gb(modp)B \equiv g^b \pmod p.

Exchange of Public Keys

Public keys need no confidentiality, only authenticity and integrity, since a forged public key lets an attacker substitute their own key undetected.

Methods:

  • a trusted public key server
  • in-person or verified exchange
  • a CA-issued digital certificate.

Shared Secret Key

Alice computes k=Ba mod p=gba mod pk = B^a \bmod p = g^{ba} \bmod p. Bob computes k=Ab mod p=gab mod pk = A^b \bmod p = g^{ab} \bmod p. Both arrive at the same value since gab≡gba(modp)g^{ab} \equiv g^{ba} \pmod p.

Ephemeral Diffie-Hellman

Also known as DHE. Runs a new DH exchange for every session.

Session Lifecycle

  • Alice generates a fresh DH exponent asa_s, computes As=gas mod pA_s = g^{a_s} \bmod p
  • Bob generates a fresh DH exponent bsb_s, computes Bs=gbs mod pB_s = g^{b_s} \bmod p
  • Both derive ks=gasbs mod pk_s = g^{a_sb_s} \bmod p and use a key derivation function to obtain session traffic keys.
  • Both delete asa_s and bsb_s after the exchange.

This gives perfect forward secrecy. If an attacker steals a long-term authentication key later, past session keys remain unavailable because the ephemeral private exponents no longer exist. Recovering asa_s or bsb_s from recorded public values requires solving the DLP.

Normal DH reuses the same private exponents across sessions. A compromised static private exponent exposes every session derived from it. DHE adds fresh public-key generation for every session. Both forms still require 1 shared-secret exponentiation per party per session.

Authenticating the Ephemeral Keys

Raw DHE has no identity binding. A fresh, unsigned AsA_s or BsB_s is exactly what a man-in-the-middle substitutes. DHE provides forward secrecy, not authentication.

A protocol combines DHE with long-term signatures or certificates to authenticate the ephemeral public keys. The authentication key proves the sender’s identity. The ephemeral private exponent derives the session secret.

  • Alice signs the exchange with SKASK_A, sends (As,signature)(A_s, \text{signature}).
  • Bob verifies the signature with PKAPK_A, then proceeds with AsA_s exactly as in plain DH.

ECDHE

Also known as Elliptic-Curve Ephemeral Diffie-Hellman. Uses an elliptic-curve group instead of the multiplicative group Zp∗\mathbb{Z}_p^*.

  • The curve and base point GG are public parameters.
  • Alice chooses a fresh private scalar asa_s and sends the public point As=asGA_s = a_sG.
  • Bob chooses a fresh private scalar bsb_s and sends the public point Bs=bsGB_s = b_sG.
  • Alice computes S=asBsS = a_sB_s.
  • Bob computes S=bsAsS = b_sA_s.

Both obtain S=asbsGS = a_sb_sG. A key derivation function converts SS into session traffic keys. Recovering asa_s from AsA_s requires solving the Elliptic-Curve Discrete Logarithm Problem.

ECDHE keeps the same roles as DHE. The ephemeral scalars derive SS. Long-term keys authenticate the exchanged points. ECDHE uses smaller public keys for comparable security.

Man-in-the-Middle Attack

The basic DH protocol has no authentication of public keys, so it is vulnerable to a man-in-the-middle attack.

Alice Mallory Bob A = gᵃ intercept M₁ = gᵐ¹ claiming to be Alice B = gᵇ intercept M₂ = gᵐ² claiming to be Bob Alice derives k₁ = M₂ᵃ Mallory derives the same k₁ from A Bob derives k₂ = M₁ᵇ Mallory derives the same k₂ from B

Preventing the Attack

The attack works because AA and BB carry no proof of who sent them. Every fix binds the exchanged keys to a verified identity.

  • Digital signature
    Sign the exchanged public key with a long-term signing key, as in ephemeral DH authentication. Bob verifies the signature before trusting AA.
  • Certificate
    A CA-issued certificate binds a long-term public key to an identity, letting Bob verify Alice’s signing key without a prior direct exchange with her.
  • Station-to-Station protocol
    Each party signs both exchanged values AA and BB together and encrypts the signature under the just-derived shared key. Mallory’s substituted keys cannot produce a valid signature over the real AA and BB.
  • Out-of-band verification
    Compare a short fingerprint of the exchanged keys over a separate trusted channel, e.g. reading it aloud on a phone call.

Every fix needs a trust anchor Mallory cannot also forge or intercept, a long-term signing key, a CA, or a side channel. Plain DH supplies none of these on its own.

Worked Examples

Choosing pp

Select the safe prime pp from 1515, 1717, 1919, 2323, and 2929.

Evaluate every candidate before revealing the solution.

Solution Reveal Hide
  • 1515 is composite.
  • 1717 is prime, but 17−12=8\dfrac{17 - 1}{2} = 8 is composite.
  • 1919 is prime, but 19−12=9\dfrac{19 - 1}{2} = 9 is composite.
  • 2323 is prime and 23−12=11\dfrac{23 - 1}{2} = 11 is prime.
  • 2929 is prime, but 29−12=14\dfrac{29 - 1}{2} = 14 is composite.

Select p=23p = 23.

Choosing qq

Let p=23p = 23. Select the value of qq satisfying p=2q+1p = 2q + 1 from 77, 99, 1111, 1313, and 1515.

Evaluate every candidate before revealing the solution.

Solution Reveal Hide
  • q=7q = 7 gives 2q+1=152q + 1 = 15, not 2323.
  • q=9q = 9 gives 2q+1=192q + 1 = 19, not 2323.
  • q=11q = 11 gives 2q+1=232q + 1 = 23.
  • q=13q = 13 gives 2q+1=272q + 1 = 27, not 2323.
  • q=15q = 15 gives 2q+1=312q + 1 = 31, not 2323.

Select q=11q = 11.

Alice’s Calculations

Let p=23p = 23, g=5g = 5, a=6a = 6, and B=19B = 19. Select Alice’s public key AA and shared secret kk:

  • (A,k)=(4,2)(A,k) = (4,2)
  • (A,k)=(8,2)(A,k) = (8,2)
  • (A,k)=(8,4)(A,k) = (8,4)
  • (A,k)=(16,2)(A,k) = (16,2)
  • (A,k)=(8,19)(A,k) = (8,19)

Compute both values before revealing the solution.

Solution Reveal Hide
  • 52 mod 23=25^2 \bmod 23 = 2.
  • 54 mod 23=22 mod 23=45^4 \bmod 23 = 2^2 \bmod 23 = 4.
  • A=56 mod 23=54⋅52 mod 23=4⋅2 mod 23=8A = 5^6 \bmod 23 = 5^4 \cdot 5^2 \bmod 23 = 4 \cdot 2 \bmod 23 = 8.
  • 192 mod 23=1619^2 \bmod 23 = 16.
  • 194 mod 23=162 mod 23=319^4 \bmod 23 = 16^2 \bmod 23 = 3.
  • k=196 mod 23=194⋅192 mod 23=3⋅16 mod 23=2k = 19^6 \bmod 23 = 19^4 \cdot 19^2 \bmod 23 = 3 \cdot 16 \bmod 23 = 2.

The computed pair is (A,k)=(8,2)(A,k) = (8,2).

  • (4,2)(4,2) has the wrong public key.
  • (8,2)(8,2) is correct.
  • (8,4)(8,4) has the wrong shared secret.
  • (16,2)(16,2) has the wrong public key.
  • (8,19)(8,19) has the wrong shared secret.

Bob’s Calculations

Let p=23p = 23, g=5g = 5, b=15b = 15, and A=8A = 8. Select Bob’s public key BB and shared secret kk:

  • (B,k)=(8,2)(B,k) = (8,2)
  • (B,k)=(19,2)(B,k) = (19,2)
  • (B,k)=(19,4)(B,k) = (19,4)
  • (B,k)=(16,2)(B,k) = (16,2)
  • (B,k)=(19,8)(B,k) = (19,8)

Compute both values before revealing the solution.

Solution Reveal Hide
  • 52 mod 23=25^2 \bmod 23 = 2.
  • 54 mod 23=45^4 \bmod 23 = 4.
  • 58 mod 23=165^8 \bmod 23 = 16.
  • B=515 mod 23=58⋅54⋅52⋅5 mod 23=16⋅4⋅2⋅5 mod 23=19B = 5^{15} \bmod 23 = 5^8 \cdot 5^4 \cdot 5^2 \cdot 5 \bmod 23 = 16 \cdot 4 \cdot 2 \cdot 5 \bmod 23 = 19.
  • 82 mod 23=188^2 \bmod 23 = 18.
  • 84 mod 23=182 mod 23=28^4 \bmod 23 = 18^2 \bmod 23 = 2.
  • 88 mod 23=22 mod 23=48^8 \bmod 23 = 2^2 \bmod 23 = 4.
  • k=815 mod 23=88⋅84⋅82⋅8 mod 23=4⋅2⋅18⋅8 mod 23=2k = 8^{15} \bmod 23 = 8^8 \cdot 8^4 \cdot 8^2 \cdot 8 \bmod 23 = 4 \cdot 2 \cdot 18 \cdot 8 \bmod 23 = 2.

The computed pair is (B,k)=(19,2)(B,k) = (19,2).

  • (8,2)(8,2) has the wrong public key.
  • (19,2)(19,2) is correct.
  • (19,4)(19,4) has the wrong shared secret.
  • (16,2)(16,2) has the wrong public key.
  • (19,8)(19,8) has the wrong shared secret.
Written by November 4, 2026 7 min read
Was this helpful?