- Introduced by
- Whitfield Diffie, Martin Hellman
- Year
- 1976
- Type
- Asymmetric key exchange algorithm
- Family
- Key agreement protocol
What does it do?
The Diffie-Hellman key exchange, introduced by Whitfield Diffie and Martin Hellman in 1976, lets two parties establish a shared secret over a channel that an eavesdropper can observe.
The parties need no prior communication or shared secret. The agreed value is typically used to derive a symmetric encryption key, and an observer who sees only the exchanged public values cannot easily compute it.
The intuition behind the method
Security rests on the discrete logarithm problem in the multiplicative group modulo a prime p: given a generator g and a value g^a mod p, recovering the exponent a is believed to be computationally infeasible when p and g are well chosen. Both parties agree on p and g. Each picks a private random exponent, computes a public value by exponentiation modulo p, and sends that value. Because the exponents multiply in either order, each party can raise the other's public value to its own private exponent and obtain the same result, g^(ab) mod p. An observer who sees only A and B cannot easily reproduce that result.
History
Diffie and Hellman published their work in 1976, proposing key agreement that reduces the need for secure key distribution channels. The scheme rests on the discrete logarithm problem, for which no efficient solution is known for well-chosen parameters. RSA, published in 1977, provided a different public-key approach and was not derived from Diffie-Hellman. Authenticated variants and elliptic-curve instantiations (ECDH) build on the same underlying idea.
When to use it
Choose Diffie-Hellman when two parties must agree on a symmetric session key over a channel that may be observed, and they can verify each other's identity by a separate mechanism such as certificates or digital signatures. It is a key agreement method, not an encryption method: data is protected only after a symmetric cipher uses the agreed key. Do not rely on it alone against an active attacker who can alter messages, because the unauthenticated exchange cannot show Alice whom she is talking to. For new designs, elliptic-curve variants (ECDH) are the usual choice because they reach comparable security with smaller keys. If the parties already share a secret key, an exchange is unnecessary.
Limitations and considerations
- The unauthenticated exchange is open to man-in-the-middle attacks; signatures or certificates are needed to bind the public values to the parties' identities.
- Classical DH needs large moduli to reach modern security levels, which makes it slower than elliptic-curve alternatives.
- Static, long-term keys give no forward secrecy: if a long-term private key is later compromised, shared secrets from captured sessions can be recovered. Ephemeral variants (DHE) avoid this by using a fresh key pair per session.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Allows two parties with no prior shared secret or secure channel to agree on a key, because the exchange itself takes place over the open channel.
- Passive eavesdropping does not reveal the shared secret: recovering it from A and B requires solving the discrete logarithm problem, for which no efficient classical algorithm is known when parameters are well chosen.
- The public values A and B can be signed or certified, so an authenticated variant can be built by adding authentication around the same exponentiation step.
Applications
- TLS/SSL: establishing session keys during the handshake, including ephemeral (EC)DHE modes
- IPsec: creating shared keys for encrypted virtual private networks
- Signal and WhatsApp: ECDH-based key agreement as one component of larger messaging protocols
- SSH: key agreement for secure remote system access
- OpenVPN and WireGuard: establishing keys for VPN tunnel encryption
How it works
How it works
The parties agree on public parameters: a large prime p and a generator g of the multiplicative group modulo p. These values need not be secret, but they must be chosen with care, because poorly chosen parameters weaken the problem the protocol relies on. In practice, standardized named groups are preferred to ad hoc parameters. A large prime modulus is required for modern security levels, and the generator should produce a subgroup of large prime order. These properties should be verified rather than assumed.
Alice picks a random private exponent a and computes her public value A = g^a mod p. Bob independently picks a random private exponent b and computes his public value B = g^b mod p. The private exponents stay with their owners.
Alice sends A to Bob and Bob sends B to Alice over a channel that an attacker can read and modify. An eavesdropper observes A and B but not a or b. Nothing in this step authenticates who sent each value.
Alice computes S = B^a mod p and Bob computes S = A^b mod p. Both equal g^(ab) mod p = g^(ba) mod p, so the two parties hold the same value. In practice S is processed by a key derivation step before it is used as an encryption key.
Common pitfalls
- Running the exchange without authentication. Always bind the public values to identities with digital signatures or certificates.
- Using small or non-standard prime groups. Use standardized parameters, or generate new ones with care, because weak groups are easier to attack.
- Using static keys where forward secrecy is required. Use ephemeral DHE so that each session has a fresh key pair.
- Using the raw shared value S directly for several purposes. Derive separate, session-specific keys from it.
- Accepting received public values without checking them. Validate that each received value lies in the valid range and in the intended subgroup before computing the shared secret.
Frequently asked questions
If I see A and B on the wire, why can't I compute the shared secret?
To compute the shared secret from A and B alone, you would need to find either a such that g^a ≡ A (mod p) or b such that g^b ≡ B (mod p). This is the discrete logarithm problem. For large primes and well-chosen generators, no efficient classical algorithm is known, so solving it is computationally infeasible.
What is a man-in-the-middle attack on Diffie-Hellman?
An attacker intercepts and modifies the public values exchanged. The attacker can pose as Alice to Bob, establishing a shared secret with Bob, and as Bob to Alice, establishing a separate shared secret with Alice. The attacker can then decrypt and re-encrypt all messages without detection, unless authentication is present.
Why do modern systems prefer elliptic curve DH (ECDH) over DH?
ECDH reaches a comparable security level with much smaller keys than classical DH, which lowers computation, bandwidth and storage costs. Both are considered secure against known classical attacks when parameters are well chosen, but a sufficiently large quantum computer would break both.
What is ephemeral Diffie-Hellman (DHE) and why is it important?
DHE generates a new Diffie-Hellman key pair for each session instead of reusing the same pair. If a long-term private key is ever compromised, DHE ensures that past session keys remain secure, a property called forward secrecy. TLS 1.3 key exchange is based on ephemeral (EC)DH.
Related methods
Sources
Diffie, W., & Hellman, M. E. (1976). New directions in cryptography. IEEE Transactions on Information Theory, 22(6), 644–654.
Open DOI referenceMenezes, A. J., van Oorschot, P. C., & Vanstone, S. A. (1997). Handbook of Applied Cryptography. CRC Press.
Open publication referenceBoyd, C., & Mathuria, A. (2003). Protocols for Authentication and Key Establishment. Springer-Verlag.
Open DOI reference