DIFFIE-HELLMAN

Shared secret over a public channel

L215 MINSECURITY
PREDICT

Alice sends Bob her public value A, and Bob sends back his public value B. Each combines what it receives with its own secret (a for Alice, b for Bob) to get a key. Mallory sits on the path and replaces A with her own value on the way to Bob, and B with her own value on the way to Alice. What happens?

Alice and Bob agree on a public prime p and generator g. Alice picks a secret a and sends A = g^a mod p. Bob picks b and sends B = g^b mod p. Each raises what they received to their own secret, and both get g^(ab) mod p. An eavesdropper who sees A and B would need a discrete logarithm to get there.

Nothing in that exchange says who sent A or B. An attacker who can change messages swaps in her own values and runs two exchanges, one with each side. The fix is to sign the share with a key the other side already trusts. In TLS 1.3 and SSH the server signs the handshake, shares included. Mutual authentication adds a client signature, and the sim below signs both sides.

▶ SIMULATION
ALICE
secret: ? · public: —
received: —
key: —
idle
WIRE (Eve listens)
seen: —
BOB
secret: ? · public: —
received: —
key: —
idle
publicp = 23, g = 5
✓ any key in use is shared by Alice and Bob, and only them
SCHEDULE
01[SETUP]plain DH: public values travel unsigned
02[ALICE]Alice picks secret a, computes A = g^a mod p
03[BOB]Bob picks secret b, computes B = g^b mod p
04[ALICE]Alice → Bob: A
05[BOB]Bob → Alice: B
06[ALICE]Alice checks what she received and derives K = B^a mod p
07[BOB]Bob checks what he received and derives K = A^b mod p
08[ALICE]Alice encrypts "PIN 4921" under her K and sends it
Start of eavesdropper only scenario. 8 steps.
Diffie-Hellman (unauthenticated)
  public: prime p, generator g
  Alice: secret a, sends A = g^a mod p
  Bob:   secret b, sends B = g^b mod p
  Alice: K = B^a = g^(ab) mod p      Bob: K = A^b = g^(ab) mod p
  Eve sees g, p, A, B. Getting g^(ab) needs a discrete log.

Man in the middle
  Mallory replaces A with M1 = g^m1 and B with M2 = g^m2.
  Alice: K1 = M2^a = A^m2       Bob: K2 = M1^b = B^m1
  Mallory knows K1 and K2 and re-encrypts between them.
  Nothing in the math tells Alice that M2 did not come from Bob.

Authenticated DH
  TLS 1.3 and SSH: the server signs the handshake transcript (both key
  shares and both nonces) with a long-term key the client already
  trusts (certificate, pinned host key). Mutual authentication (mTLS)
  adds a client signature too. This sim models the mutual case: each
  side signs its share.
  The receiver verifies with THAT key, never a key the message carries,
  and the signed data includes a fresh nonce so old signatures cannot be
  replayed.

Real sizes: X25519 (ECDH), or 2048-bit+ finite-field groups (RFC 7919).

Watch the key boxes and the invariant strip. In MAN IN THE MIDDLE, Alice and Bob each derive a key, and both keys sit in Mallory’s list. In FIX: SIGNED DH vs MITM, Mallory tries the same swap, the signatures do not verify, and both sides abort before using a key.

BREAK IT NOT YET

Plain DH with only a passive eavesdropper. Put Mallory on the wire at a point where Alice ends up using a key Mallory knows.

GOAL: Alice or Bob accepts a key Mallory knows

FIX IT NOT YET

Bob receives Alice's signed key share. Return true only if msg.sig is a valid signature over `${msg.share}|${nonce}` under peerKey, the key Bob already trusts for Alice. nonce is the fresh value Bob sent for this session. A signature verifies when tools.modpow(sig, key.e, key.n) === tools.hash(data) % key.n.

Mental model:

Diffie-Hellman gives you a key shared with whoever answered. It defeats someone who only listens. It does nothing about someone who can change messages. Pair it with authentication: a signature over the share and this session’s nonce, checked against a key you already trust. In TLS 1.3 and SSH the server signs; mutual authentication adds the client. The exchange gives you secrecy. The signature tells you who the secret is shared with.

IN PRODUCTION

The ephemeral key exchange in every TLS 1.3 handshake (X25519 key_share, signed by CertificateVerify), SSH (host key signs the exchange, and the “host key changed” warning is this lesson), Signal's X3DH, and WireGuard (the last two authenticate with a static key the peer already trusts, through DH rather than a signature). Disabling certificate or host-key checks turns any of them back into unauthenticated DH.

IN INTERVIEWS

“Walk me through Diffie-Hellman. Is it secure?” Do the math, say it resists eavesdropping because of the discrete log problem, then say it does not resist a man in the middle on its own, and explain how TLS fixes that: the server signs the transcript with its certificate key, and the client checks the certificate.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: TIMING ATTACK →