Diffie-Hellman Key Exchange
Key agreement over a public channel: computational assumptions protect against passive eavesdropping; authentication addresses active impersonation.
Exchange a value, then ask who sent it
Toy teaching model — no real security. We fix public parameters p = 23 and g = 5. Choose each participant’s private exponent and watch what they publish and calculate. All values are displayed for learning; these tiny numbers protect nothing.
Run the example, then change one private exponent.
Source for the construction: Whitfield Diffie and Martin Hellman, “New Directions in Cryptography” (1976), §III. This invented small-number exercise adds no identity verification or key derivation.
Why This Matters
Diffie–Hellman showed how two parties can agree on a secret over a public channel without first transporting that secret. The unauthenticated exchange protects against a passive eavesdropper under its computational assumptions. A secure protocol must also authenticate the participants or their exchanged values to resist an active intermediary.
Diffie and Hellman published ‘New Directions in Cryptography' in IEEE Transactions on Information Theory, November 1976. The paper proposed both public-key encryption (as a goal) and the key-exchange protocol (as a working example). Three British researchers at GCHQ — James Ellis, Clifford Cocks, and Malcolm Williamson — developed classified public-key ideas during 1970–1974 but classification kept their work secret until 1997. The 1976 paper directly inspired Rivest, Shamir, and Adleman to find RSA the following year.
Both parties agree on a large prime p and a generator g. Alice picks a secret integer a and sends ga mod p. Bob picks a secret integer b and sends gb mod p. Each computes the shared value K = gab mod p — Alice raises Bob's value to her secret, Bob raises Alice's value to his. An eavesdropper sees p, g, A = ga and B = gb. Recovering an exponent a or b is a discrete logarithm problem (DLP). Computing only the shared value K is the computational Diffie–Hellman problem (CDH). A DLP solver gives a CDH solver: recover a, then compute Ba. The converse is not established for arbitrary groups. See Reduction & Proof for why the direction of an implication matters.
With p = 23, g = 5, A = 8 and B = 19, a discrete-log solver finds a = 6 because 56 mod 23 = 8. It can then compute the shared value 196 mod 23 = 2. These tiny parameters are for arithmetic only.
Source: Dan Boneh and Victor Shoup, A Graduate Course in Applied Cryptography, version 0.6 (January 2023), §10.4.2. Learning whether the value looks random is a further security question, distinct from merely computing it.
Which oracle makes the reverse reduction possible?
A DLP solver gives a CDH solver. The reverse direction needs additional, group-specific conditions. A full two-input DH oracle accepts two freely chosen points [a]G, [b]G and returns [ab]G, multiplying hidden scalars. Ordinary point addition gives [a+b]G. A fixed-secret service that only returns [s]P is a different capability and does not supply the paper's efficient method.
| Offline work | Online work | Required access |
|---|---|---|
| About 3.0 TB auxiliary codebook; about one week on a dual AMD EPYC 7742 | 23,656 simulated full-oracle calls in 32 seconds | The experiment knew discrete logs to simulate the full oracle; the authors report no real-world instantiation |
These results do not establish that P256 was broken in 32 seconds. Other curves' figures are partly estimates, and the largest codebooks are unrealizable. The oracle, simulation status, offline cost and online time are inseparable parts of the result. Source: Dlog is Practically as Hard (or Easy) as DH, pp. 2–3, 7, 12–16.
From a shared value to keys for different purposes
HKDF-SHA-256 first Extracts a pseudorandom key (PRK) from input key material (IKM): PRK = HMAC-SHA-256(salt, IKM). It then Expands that PRK with an info byte string and requested output length. A protocol can use different labels for its two traffic directions.
Reproducible fixture: IKM is the 32 bytes 00 01 … 1f (hexadecimal), salt is 32 zero bytes, and labels below are UTF-8. These published bytes have no secrecy; they are separate from the tiny DH arithmetic above. Extract gives:
46bd320605c5a6b6163ab70bc6345b92a5f908e79fe58979c23ebb47d1a5e307
| Info label | Bytes | Output key material |
|---|---|---|
museum-demo/client-to-server | 16 | a6d093bc8d19224f252d50a07c32bd77 |
museum-demo/client-to-server | 32 | a6d093bc8d19224f252d50a07c32bd778b08327a06548f672adda01ae4a9d1f2 |
museum-demo/server-to-client | 32 | cefaddbcf88b04083d511449e158fc5cfc33dcf08759adda9bb09759e02ff728 |
Identical inputs reproduce identical outputs. Changing only the requested length keeps a common prefix; use purpose labels rather than length alone to separate uses. Changing the direction label changes this example's output. Salt and info need not be secret. HKDF does not create new input entropy or make a guessable password or tiny DH group secure; it is not a password-stretching function. Authentication and group validation remain necessary.
Change these inputs in the existing Crypto Lab: KDF Chain, under HKDF: Extract and Expand, to inspect PRK, output key material and expansion blocks.
Source: David Wong, Real-World Cryptography (2021 edition), §8.6, “Key derivation with HKDF,” PDF pp. 349–356 in the reflowed edition, which has no printed pagination. The fixed fixture is a museum example, not a protocol key schedule.
Resistance to recovering the shared value rests on the computational Diffie–Hellman assumption in the selected group; discrete-log hardness alone does not establish that assumption. Parameter size, subgroup choice and public-key validation all matter; a bit length alone does not establish security. The 2015 Logjam work demonstrated a downgrade to export-grade 512-bit finite-field DH and studied the benefits of precomputation for widely shared groups. Suitable group sizes and validation matter; moving to an elliptic curve also requires a specified secure construction. DH is vulnerable to active man-in-the-middle attacks because the exchanged values are unauthenticated — protocols that need protection from active attackers must combine DH with authentication (for example signatures, certificates or a suitable pre-shared-key protocol). A human password requires a properly designed password-authenticated key exchange, not an improvised password check.
Two input-validation failures are often merged and should not be. Forcing a peer’s shared secret into a small subgroup compromises that session and can leak a few bits; recovering a server’s static private exponent additionally requires that one exponent be reused across many such queries. A 2016 measurement study found that none of more than twenty surveyed libraries validated subgroup orders by default before January 2016, yet feasible full key recovery was confined to particular applications. The elliptic-curve counterpart — altering only the y coordinate of a peer’s public point — broke Bluetooth pairing in 2018. Both are laid out with their access and assumptions in what an attack result actually shows.
| Where | How Diffie-Hellman is Used |
|---|---|
| TLS 1.3 (HTTPS) | TLS 1.3 supports ephemeral finite-field DH, ECDH and pre-shared-key modes; PSK-only exchange does not provide the same forward-secrecy guarantee |
| Signal | X3DH and the classical Double Ratchet are historical foundations. Signal later introduced PQXDH and the Triple Ratchet with a continuing post-quantum ratchet |
| SSH | DH and ECDH are among SSH key-exchange families; negotiated choices vary |
| WireGuard / Tor | WireGuard uses Curve25519; Tor’s ntor handshake also uses Curve25519. Their complete protocols differ. |
| IPsec / IKEv2 | IKEv2 negotiates key-exchange groups; no single group set applies to every VPN |
| Era | Modern · 1976 |
| Status | Widely used; finite-field DH and elliptic-curve variants such as X25519 have different parameter choices |
| Origin | Whitfield Diffie & Martin Hellman, ‘New Directions in Cryptography' (1976); Ralph Merkle’s related public-key research |
| Year | 1976 |
| Type | Public-key key-agreement protocol — not a cipher (does not encrypt data) |
| Modern Role | Contributes shared secrets to TLS, SSH, Signal and WireGuard; some protocols also combine post-quantum mechanisms |
Primary Sources
RFC 8446 (TLS 1.3, 2018), key-exchange modes; Signal: SPQR and the Triple Ratchet (2 October 2025); Signal ratchet specification. Protocols feed DH outputs through a key derivation function; compare the shared-value arithmetic with the worked HKDF example above.