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 : recovering the secret from is computationally intractable for large , while computing from 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 , defining the multiplicative group .
- A generator , a primitive root modulo .
is a primitive root modulo iff every coprime to satisfies for some integer , called the discrete logarithm of to base .
For prime , the values generate , in some order, via .
Choosing as a primitive root maximises the order of the group it generates to , so its powers span every non-zero residue class modulo and stay in no small subgroup, where the discrete logarithm would be easy to solve.
Choosing a Modulus
is chosen as a safe prime: a prime of the form where is also prime.
Here:
- : the Sophie Germain prime paired with
Reasons:
- Pohlig-Hellman reduces the discrete logarithm modulo to one discrete logarithm per prime factor of . A safe prime forces , leaving only the factors and the large prime .
- No small-order subgroup exists, so an attacker cannot confine the shared secret to a short list of values.
- Every has order or , so almost any candidate generates a large group.
Choosing a Generator
Factor into its distinct prime factors . Then is a primitive root modulo iff
Here:
- : a distinct prime factor of
Test in turn. Primitive roots are dense, so a small passes after few tries. Each candidate costs modular exponentiations.
Factoring is the hard step, so is chosen in advance to keep it known.
A safe prime is a prime where is also prime. Then has 2 prime factors, so is a primitive root iff and . Published standards use safe primes and usually take .
For , . Testing gives and . Neither is , so is a primitive root. fails, since .
Key Pair Generation
Each party chooses a secret integer and publishes its exponentiation modulo .
- Alice chooses secret , computes public .
- Bob chooses secret , computes public .
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 . Bob computes . Both arrive at the same value since .
Worked Example
, , , .
- .
- .
- Alice computes .
- Bob computes .
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 or 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 or 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 with her long-term private signing key, sends .
- Bob verifies the signature against Alice’s long-term public key, then proceeds with exactly as in plain DH.
Why forward secrecy holds:
- and are generated fresh per session and destroyed once is computed.
- Recovering from the recorded public 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 or .
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 and 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 . - 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 and together and encrypts the signature under the just-derived shared key. Mallory’s substituted keys cannot produce a valid signature over the real and . - 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.