DIFFIE-HELLMAN
Shared secret over a public channel
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.
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.
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
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.
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.
“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.