← Wiki Index / Computation & Algorithms / Diffie–Hellman Key Exchange
💻 Computation & Algorithms

Diffie–Hellman Key Exchange

Diffie–Hellman key exchange is a foundational public-key cryptographic protocol introduced by Whitfield Diffie and Martin Hellman in 1976 that enables two parties with no prior shared secrets to establish a common secret key over an insecure channel using the commutativity of modular exponentiation and the hardness of the discrete logarithm problem.

Diffie–Hellman Key Exchange (DH)—also credited as Diffie–Hellman–Merkle key exchange in recognition of Ralph Merkle's independent work on cryptographic puzzles—is a foundational asymmetric cryptographic protocol in computation and algorithms that allows two parties possessing no prior shared secret to establish a shared secret key over a publicly monitored communication channel. Published by Whitfield Diffie and Martin E. Hellman in their landmark November 1976 paper "New Directions in Cryptography" (and independently discovered in classified work by Malcolm J. Williamson at GCHQ in 1974), the protocol initiated the era of modern public-key cryptography that subsequently yielded the RSA cryptosystem and elliptic-curve cryptography.

Within computational complexity theory, classical symmetric encryption requires both parties to share a secret key beforehand via a trusted courier, creating an O(N2) key-distribution bottleneck across N communicating nodes. Diffie and Hellman eliminated this physical key-distribution requirement by exploiting a one-way trapdoor-free group action in a finite cyclic group G = ⟨g⟩: computing modular exponentiation ga mod p from (g, a, p) requires only O(log a) multiplications via binary square-and-multiply, whereas inverting the operation to recover the secret exponent a from (g, ga mod p, p)—the Discrete Logarithm Problem (DLP)—is computationally infeasible for suitably chosen parameters, enabling two parties to combine their private exponents via the commutative identity (ga)b = gab = (gb)a mod p.

Finite Cyclic Group Formulation and Protocol Mechanics

Multiplicative Subgroup Construction and Safe Primes

The classical Finite-Field Diffie–Hellman (FFDH) protocol operates in the multiplicative group of integers modulo a large prime p, denoted ℤp× = {1, 2, ..., p − 1} under multiplication modulo p (whose prime status is verified via the Miller–Rabin primality test). By Lagrange's theorem, the order of ℤp× is p − 1.

To prevent Pohlig–Hellman small-subgroup confinement attacks—which factor p − 1 = ∏ qiei and solve discrete logarithms in each prime-power subgroup separately using Pollard's rho algorithm before recombining via the Chinese Remainder Theorem—the public domain parameters (p, q, g) are selected as follows:

  • Safe Prime Construction: Choose p = 2q + 1 where both p (e.g., 2,048 to 4,096 bits) and q (a Sophie Germain prime) are prime, or choose a Schnorr subgroup where a 256-bit prime q divides p − 1.
  • Subgroup Generator g: Choose an element g ∈ ℤp× of prime multiplicative order q (so the cyclic subgroup G = {1, g, g2, ..., gq−1} mod p has no non-trivial proper subgroups).

Four-Step Ephemeral Key Exchange Execution

Given the public domain parameters (p, q, g) known to Alice, Bob, and any passive eavesdropper Eve:

  1. Alice's Ephemeral Key Generation: Alice samples a secret integer a uniformly at random from {1, 2, ..., q − 1}, computes her public ephemeral share A in O((log q)(log p)2) bit operations via binary exponentiation:
A = ga mod p

and transmits A to Bob over the public channel.

  1. Bob's Ephemeral Key Generation: Bob independently samples a secret integer b uniformly at random from {1, 2, ..., q − 1}, computes his public ephemeral share B:
B = gb mod p

and transmits B to Alice over the public channel.

  1. Shared Secret Computation:
    • Upon receiving B (and verifying 1 < B < p − 1 and Bq ≡ 1 mod p), Alice raises B to her private exponent a:
KAlice = Ba mod p = (gb)a mod p = gab mod p
  • Upon receiving A, Bob raises A to his private exponent b:
KBob = Ab mod p = (ga)b mod p = gab mod p
  1. Key Derivation Function (KDF): Because the raw group element gab mod p is not uniformly distributed across all bit strings, Alice and Bob pass K = gab mod p through a cryptographic hash function key derivation function (such as HKDF-SHA256) to derive symmetric Advanced Encryption Standard (AES-GCM) session keys.

Hardness Assumptions: DLP, CDH, and DDH

The Hierarchy of Diffie–Hellman Complexity Assumptions

A passive eavesdropper Eve observes the public tuple (p, q, g, A = ga, B = gb). The cryptographic security of Diffie–Hellman rests on three progressively stronger computational problems in the cyclic group G = ⟨g⟩ of prime order q:

  1. Discrete Logarithm Problem (DLP): Given (g, ga ∈ G), compute the exponent a ∈ ℤq.
  2. Computational Diffie–Hellman Problem (CDH): Given (g, ga, gb ∈ G) for uniform a, b ∈ ℤq, compute the group element gab ∈ G. (Clearly CDH ≤P DLP: an oracle that solves DLP extracts a from ga and computes (gb)a = gab.)
  3. Decisional Diffie–Hellman Problem (DDH): Given a tuple (g, ga, gb, Z ∈ G), distinguish with non-negligible advantage whether Z = gab (a true Diffie–Hellman tuple) or Z = gc for an independent uniform c ∈ ℤq.

In order for the shared secret gab to be indistinguishable from a truly random key (semantic security), the DDH assumption must hold in G (DDH ≤P CDH ≤P DLP). Notably, DDH fails in the full multiplicative group ℤp× of order p − 1 (even when CDH is hard!) because the Legendre symbol (gx / p) leaks the parity (x mod 2) of the exponent: if both a and b are even, ab is even, allowing Eve to distinguish gab from a random element with 75% accuracy. Restricting the protocol to the prime-order subgroup G of order q = (p − 1)/2 (the subgroup of quadratic residues modulo a safe prime p) eliminates Legendre parity leakage and restores DDH security.

Diffie–Hellman Group FamilyBest Classical Attack AlgorithmAsymptotic DLP Attack ComplexityParameter Size for 128-Bit Classical SecurityPublic Key Size (Bytes)Quantum Resistance (Shor's Algorithm)
Multiplicative Field ℤp× (FFDH)General Number Field Sieve (GNFS)Lp[1/3, (64/9)1/3] = exp((1.923 + o(1))(ln p)1/3(ln ln p)2/3)3,072-bit prime p (256-bit q)384 bytesvulnerable (O((log p)3) quantum gates)
Elliptic Curve E(𝔽p) (ECDH)Pollard's rho algorithm (Generic group)O(√q) = O(2b/2) group operations256-bit curve (Curve25519, P-256)32 bytesvulnerable (O((log q)3) quantum gates)
Supersingular Isogeny (SIDH/CSIDH)Castryck–Decru / Kuperberg sieveClassical polynomial on torsion points (SIDH broken 2022)— (Deprecated)——
Lattice Module-LWE (ML-KEM / Kyber)Block Korkine–Zolotarev (BKZ) lattice reduction20.292 β (O(2λ))ML-KEM-768 (n = 256, k = 3)1,184 bytesQuantum-resistant (NIST FIPS 203)

Elliptic-Curve Diffie–Hellman (ECDH), Forward Secrecy, and Man-in-the-Middle Defenses

Elliptic-Curve Diffie–Hellman (ECDH / X25519)

In Finite-Field Diffie–Hellman over ℤp×, the index-calculus General Number Field Sieve (GNFS) solves discrete logarithms in sub-exponential time Lp[1/3, 1.923] = exp((1.923 + o(1))(ln p)1/3(ln ln p)2/3), forcing prime moduli p to grow to 2,048–3,072 bits (as demonstrated by the 2015 Logjam attack by Adrian et al., which precomputed GNFS tables for shared 512-bit and 768-bit primes).

By replacing the multiplicative group ℤp× with the additive group of rational points E(𝔽p) on an elliptic curve (Neal Koblitz, 1985; Victor S. Miller, 1985), Elliptic-Curve Cryptography Diffie–Hellman (ECDH) computes scalar point multiplications A = [a]G and B = [b]G yielding shared point K = [ab]G. Because generic elliptic curves lack the smooth-number factorization structure required by index-calculus sieves, the fastest classical attack is Pollard's rho algorithm in O(√q) steps—allowing Daniel J. Bernstein's X25519 (Curve25519 over p = 2255 − 19) to achieve ~128-bit classical security with 32-byte public keys and constant-time Montgomery ladder execution.

Authenticated Ephemeral Diffie–Hellman (DHE / ECDHE) and TLS 1.3

Unauthenticated Diffie–Hellman is inherently vulnerable to an active Man-in-the-Middle (MitM) attack: an active interceptor Mallory can intercept Alice's ga and Bob's gb, substitute her own share gm to both parties, and establish two separate shared secrets gam (with Alice) and gbm (with Bob), decrypting and re-encrypting all traffic in between.

Modern internet security protocols (TLS 1.3, SSH, IPsec IKEv2, and the Signal Double Ratchet protocol) eliminate MitM attacks and provide Perfect Forward Secrecy (PFS) through Authenticated Ephemeral Diffie–Hellman (ECDHE):

  1. Authentication: The server (and optionally the client) signs the transcript containing the ephemeral shares (ga, gb) using a long-term RSA cryptosystem, ECDSA, Ed25519, or post-quantum ML-DSA digital signature verified against an X.509 certificate chain.
  2. Forward Secrecy: Because the ephemeral exponents a and b are generated fresh for every single session and erased from RAM directly after deriving the session key, even a future compromise of the server's long-term private signing key cannot decrypt historical network traffic recorded earlier. (To protect against future quantum computers running Shor's algorithm, TLS 1.3 deployments increasingly use hybrid X25519MLKEM768 key exchange combining ECDHE with NIST FIPS 203 lattice key encapsulation.)

Frequently Asked Questions

Why does static RSA key transport lack Forward Secrecy whereas Ephemeral Diffie–Hellman (ECDHE) provides it?

In legacy TLS 1.2 RSA key transport, the client encrypts the pre-master secret directly under the server's long-term RSA public key and sends the ciphertext over the wire. Any adversary who records encrypted traffic today and steals (or cracks) the server's single long-term RSA private key years later can decrypt every past session. In ECDHE, the long-term key only signs the ephemeral handshake and never encrypts the session key; once the ephemeral scalars a and b are zeroized at the end of the handshake, the session key cannot be recovered even if the server's long-term signing key is later exposed.

How did the 2015 Logjam attack break 512-bit Diffie–Hellman in milliseconds?

In the General Number Field Sieve (GNFS) for discrete logarithms modulo a prime p, over 99% of the computation consists of a one-time precomputation phase (constructing a factor base of small primes and linear algebra relations) that depends only on the prime p and not on the individual session shares ga or gb. Because millions of TLS servers hardcoded the precise same 512-bit Apache/OpenSSL prime p, precomputing the GNFS log database for that single prime once allowed individual session discrete logarithms to be solved in ~70 seconds.

References

  1. Diffie, W., & Hellman, M. E. (1976). "New directions in cryptography." IEEE Transactions on Information Theory, 22(6), 644–654. https://doi.org/10.1109/TIT.1976.1055638
  2. Merkle, R. C. (1978). "Secure communications over insecure channels." Communications of the ACM, 21(4), 294–299. https://doi.org/10.1145/359460.359473
  3. Boneh, D. (1998). "The decision Diffie–Hellman problem." Proceedings of the Third Algorithmic Number Theory Symposium (ANTS-III), LNCS Vol. 1423, 48–63. https://doi.org/10.1007/BFb0054851
  4. Bernstein, D. J. (2006). "Curve25519: New Diffie–Hellman speed records." Public Key Cryptography — PKC 2006, LNCS Vol. 3958, 207–228. https://doi.org/10.1007/11745853_14
  5. Adrian, D., Bhargavan, K., Durumeric, Z., Gaudry, P., Green, M., Halderman, J. A., Heninger, N., Springall, D., Thomé, E., Valenta, L., VanderSloot, B., Wustrow, E., Zanella-Béguelin, S., & Zimmermann, P. (2015). "Imperfect forward secrecy: How Diffie–Hellman fails in practice." Proceedings of the 22nd ACM SIGSAC Conference on Computer and Communications Security (CCS '15), 5–17. https://doi.org/10.1145/2810103.2813707
🧬 Support Open Science
Explore interactive science games on LowKey Games or browse scientific apparel & accessories in the CRV Science Shop.
💡 Spot an inaccuracy or have primary literature to expand this article?
Submit a citation or revision note using your shared CRV Science / LowKey Games profile ID.
Suggest Revision