Diffie-Hellman Key Exchange

Work in progress. This note is still being written and incomplete.

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 gamodpg^a \bmod p is computationally intractable for large pp, while computing gamodpg^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.

The protocol has 4 parts: define public parameters, generate key pairs, exchange public keys, compute the shared secret.

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 satisfies agi(modp)a \equiv g^i \pmod p for some integer ii, called the discrete logarithm of aa to base gg.

For prime pp, the p1p - 1 values i=1,,p1i = 1, \dots, p-1 generate {1,,p1}\{1, \dots, p-1\}, in some order, via gimodpg^i \bmod p.

Choosing gg as a primitive root maximises the order of the group it generates to p1p - 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.

Here:

  • qq: the Sophie Germain prime paired with pp

Reasons:

  • Pohlig-Hellman reduces the discrete logarithm modulo pp to one discrete logarithm per prime factor of p1p - 1. A safe prime forces p1=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 p1p - 1 into its distinct prime factors q1,,qkq_1, \dots, q_k. Then gg is a primitive root modulo pp iff

g(p1)/qi≢1(modp)for every i.g^{(p-1)/q_i} \not\equiv 1 \pmod p \quad \text{for every } i.

Here:

  • qiq_i: a distinct prime factor of p1p - 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 p1p - 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 p1=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, p1=22=211p - 1 = 22 = 2 \cdot 11. Testing g=5g = 5 gives 5225^2 \equiv 2 and 511225^{11} \equiv 22. Neither is 11, so 55 is a primitive root. g=2g = 2 fails, since 2111(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 aZpa \in \mathbb{Z}_p^*, computes public Aga(modp)A \equiv g^a \pmod p.
  • Bob chooses secret bZpb \in \mathbb{Z}_p^*, computes public Bgb(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, or a CA-issued digital certificate.

Shared Secret Key

Alice computes k=Bamodp=gbamodpk = B^a \bmod p = g^{ba} \bmod p. Bob computes k=Abmodp=gabmodpk = A^b \bmod p = g^{ab} \bmod p. Both arrive at the same value since gabgba(modp)g^{ab} \equiv g^{ba} \pmod p.

Worked Example

p=23p = 23, g=5g = 5, a=6a = 6, b=15b = 15.

  • A=56mod23A = 5^6 \bmod 23.
  • B=515mod23B = 5^{15} \bmod 23.
  • Alice computes k=B6mod23=2k = B^6 \bmod 23 = 2.
  • Bob computes k=A15mod23=2k = A^{15} \bmod 23 = 2.

Ephemeral Diffie-Hellman

Ephemeral Diffie-Hellman (DHE) generates a fresh key pair for every session and deletes both private keys once the shared secret is set up. Long-term keys authenticate the exchange only, they do not derive the session key.

This gives perfect forward secrecy: if the long-term keys are stolen later, past session keys stay safe, because the private keys that produced them no longer exist and cannot be recovered from the recorded public values.

Static DH

Both parties reuse the same key pair across every session instead of generating a fresh one.

  • A single compromised private key exposes every past and future session, since the same aa or bb derives every session’s shared secret.
  • Costs 1 exponentiation total per party, instead of 1 per session.

Authenticating the Ephemeral Keys

Raw DH has no identity binding, so a fresh, unsigned AA or BB is exactly what a man-in-the-middle substitutes. DHE fixes this by signing the ephemeral public key with a separate long-term signing key, not by deriving the session key from it.

  • Alice signs AA with her long-term private signing key, sends (A,signature)(A, \text{signature}).
  • Bob verifies the signature against Alice’s long-term public key, then proceeds with AA exactly as in plain DH.

Why forward secrecy holds:

  • aa and bb are generated fresh per session and destroyed once kk is computed.
  • Recovering aa from the recorded public AA requires solving the DLP, not merely knowing the long-term signing key.
  • A long-term key compromised later lets an attacker forge future signatures, but supplies no way to recompute a past session’s aa or bb.

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.

Mallory now holds a separate shared key with each party, and can decrypt, inspect, modify, or inject messages in the supposedly secure channel between Alice and Bob.

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.
Written by September 16, 2026 7 min read
Was this helpful?