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.
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 (i.e. ) 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. are a Sophie-Germain prime pair.
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
- 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
- a CA-issued digital certificate.
Shared Secret Key
Alice computes . Bob computes . Both arrive at the same value since .
Ephemeral Diffie-Hellman
Also known as DHE. Runs a new DH exchange for every session.
Session Lifecycle
- Alice generates a fresh DH exponent , computes
- Bob generates a fresh DH exponent , computes
- Both derive and use a key derivation function to obtain session traffic keys.
- Both delete and 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 or 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 or 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 , sends .
- Bob verifies the signature with , then proceeds with exactly as in plain DH.
ECDHE
Also known as Elliptic-Curve Ephemeral Diffie-Hellman. Uses an elliptic-curve group instead of the multiplicative group .
- The curve and base point are public parameters.
- Alice chooses a fresh private scalar and sends the public point .
- Bob chooses a fresh private scalar and sends the public point .
- Alice computes .
- Bob computes .
Both obtain . A key derivation function converts into session traffic keys. Recovering from requires solving the Elliptic-Curve Discrete Logarithm Problem.
ECDHE keeps the same roles as DHE. The ephemeral scalars derive . 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.
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.
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
Select the safe prime from , , , , and .
Evaluate every candidate before revealing the solution.
Solution Reveal Hide
- is composite.
- is prime, but is composite.
- is prime, but is composite.
- is prime and is prime.
- is prime, but is composite.
Select .
Choosing
Let . Select the value of satisfying from , , , , and .
Evaluate every candidate before revealing the solution.
Solution Reveal Hide
- gives , not .
- gives , not .
- gives .
- gives , not .
- gives , not .
Select .
Alice’s Calculations
Let , , , and . Select Alice’s public key and shared secret :
Compute both values before revealing the solution.
Solution Reveal Hide
- .
- .
- .
- .
- .
- .
The computed pair is .
- has the wrong public key.
- is correct.
- has the wrong shared secret.
- has the wrong public key.
- has the wrong shared secret.
Bob’s Calculations
Let , , , and . Select Bob’s public key and shared secret :
Compute both values before revealing the solution.
Solution Reveal Hide
- .
- .
- .
- .
- .
- .
- .
- .
The computed pair is .
- has the wrong public key.
- is correct.
- has the wrong shared secret.
- has the wrong public key.
- has the wrong shared secret.