CSEC3616Cybersecurity Engineering

    Diffie-Hellman key exchange

    How two strangers agree on a shared secret over a public channel using nothing but a prime, a generator and one exponentiation each, and why the exchange alone cannot tell them who they are really talking to.

    • Compute both public values and the shared secret given p, g, a and b, showing every step.
    • Explain algebraically why Alice's and Bob's independent computations land on the same key.
    • Trace a person-in-the-middle attack step by step and state what the attacker ends up holding.
    • Explain why digital signatures close the gap that makes the attack possible.

    20 min read

    Intuition

    Symmetric encryption needs both sides to already hold the same key. RSA solves that by letting anyone encrypt with a public key that only one person can decrypt, but that means committing to a whole public-key infrastructure just to move a session key. Diffie-Hellman does something narrower and, for this one job, simpler: two people who share nothing in advance can still walk away from a conversation an eavesdropper heard in full, holding a secret number the eavesdropper cannot compute.

    Mechanism

    Alice and Bob start from two values that are public from the beginning: a prime pp, and gg, a primitive root (generator) of pp. Anyone, including an attacker, may know both.

    AliceBob
    1. Pick a private valuerandom a<pa < prandom b<pb < p
    2. Compute a public valueX=gamodpX = g^{a} \bmod pY=gbmodpY = g^{b} \bmod p
    3. Send itsends XX to Bobsends YY to Alice
    4. Compute the shared secretK=YamodpK = Y^{a} \bmod pK=XbmodpK = X^{b} \bmod p

    aa and bb never leave their owner. Only pp, gg, XX and YY cross the channel, and all four are things an eavesdropper is allowed to see without threatening the outcome.

    Why the two computations agree. Substitute what XX and YY actually are:

    K=Yamodp=(gb)amodp=gbamodpK = Y^{a} \bmod p = (g^{b})^{a} \bmod p = g^{ba} \bmod p K=Xbmodp=(ga)bmodp=gabmodpK = X^{b} \bmod p = (g^{a})^{b} \bmod p = g^{ab} \bmod p

    Since ba=abba = ab, both sides land on the identical value gabmodpg^{ab} \bmod p, computed two different ways, without either exponent ever being transmitted.

    Why an eavesdropper is stuck. Someone who only watches the exchange knows pp, gg, XX and YY, but not aa or bb. Recovering either one from its public value (finding aa such that X=gamodpX = g^a \bmod p) is the discrete logarithm problem from the earlier page on trapdoor functions, believed infeasible for a large enough pp. Diffie-Hellman’s security is that problem, applied directly: a passive listener is locked out.

    Diffie–Hellman key exchange

    Diffie–Hellman key exchangepublic: p, gpick private aX = gᵃ mod ppick private bY = gᵇ mod psends Xsends YK = Yᵃ mod pK = Xᵇ mod psame K

    Alice and Bob agree on p and g in public, each picks a private exponent, and each publishes gᵃ mod p or gᵇ mod p. Both then raise the other's public value to their own private exponent and land on the same K — because (gᵃ)ᵇ ≡ (gᵇ)ᵃ ≡ gᵃᵇ (mod p) — without either exponent ever crossing the wire. Security rests on the discrete logarithm problem: recovering a from gᵃ mod p is believed infeasible for a large enough prime p.

    Worked example

    Answerp=23, g=5, a=6, b=15: Alice sends X=8, Bob sends Y=19, and both independently compute the shared secret K=2.

    1. Public parameters: p=23p = 23, g=5g = 5.
    2. Alice picks a=6a = 6. Bob picks b=15b = 15. Neither value is sent.
    3. Alice computes X=56mod23X = 5^{6} \bmod 23 by repeated squaring: 51=55^1 = 5, 52=25mod23=25^2 = 25 \bmod 23 = 2, 54=22=45^4 = 2^2 = 4. 6=4+26 = 4+2, so X=54×52mod23=4×2=8X = 5^4 \times 5^2 \bmod 23 = 4 \times 2 = 8.

    4. Bob computes Y=515mod23Y = 5^{15} \bmod 23. Continuing the same powers: 58=42=165^8 = 4^2 = 16. 15=8+4+2+115 = 8+4+2+1, so Y=58×54×52×51mod23Y = 5^8 \times 5^4 \times 5^2 \times 5^1 \bmod 23: 16×4=64mod23=1816 \times 4 = 64 \bmod 23 = 18, then 18×2=36mod23=1318 \times 2 = 36 \bmod 23 = 13, then 13×5=65mod23=1913 \times 5 = 65 \bmod 23 = 19. So Y=19Y = 19.

    5. Alice sends X=8X = 8 to Bob. Bob sends Y=19Y = 19 to Alice. Both values cross the open channel.
    6. Alice computes K=Yamodp=196mod23K = Y^{a} \bmod p = 19^{6} \bmod 23. 192=361mod23=1619^2 = 361 \bmod 23 = 16, 194=162=256mod23=319^4 = 16^2 = 256 \bmod 23 = 3. 196=194×192mod23=3×16=48mod23=219^6 = 19^4 \times 19^2 \bmod 23 = 3 \times 16 = 48 \bmod 23 = 2.

    7. Bob computes K=Xbmodp=815mod23K = X^{b} \bmod p = 8^{15} \bmod 23. 82=64mod23=188^2 = 64 \bmod 23 = 18, 84=182=324mod23=28^4 = 18^2 = 324 \bmod 23 = 2, 88=22=48^8 = 2^2 = 4. 15=8+4+2+115 = 8+4+2+1, so 815=88×84×82×81mod238^{15} = 8^8 \times 8^4 \times 8^2 \times 8^1 \bmod 23: 4×2=84 \times 2 = 8, then 8×18=144mod23=68 \times 18 = 144 \bmod 23 = 6, then 6×8=48mod23=26 \times 8 = 48 \bmod 23 = 2.

    8. Both sides reach K=2K = 2, computed independently, with aa and bb never sent.

    Diffie-Hellman exchange

    Watch Alice and Bob agree on a shared secret in public, then switch on a person-in-the-middle to see it fail silently.

    Threat

    Person-in-the-middle. The exchange authenticates nobody: a public value is accepted regardless of who actually sent it. An active attacker sitting between Alice and Bob intercepts both public values and runs two independent Diffie-Hellman exchanges: one posing as Bob to Alice, one posing as Alice to Bob. Each exchange completes normally and produces a valid shared key, so the attacker ends up holding two working keys: one matching what Alice thinks she shares with Bob, one matching what Bob thinks he shares with Alice, while Alice and Bob each believe they are talking directly to the other.

    Diffie–Hellman under a person-in-the-middle attack

    Diffie–Hellman — person-in-the-middledirect exchange — interceptedAlice: pick aX = gᵃ mod pMallory: pick a′Xₘ = gᵃ′ mod pMallory: pick b′Yₘ = gᵇ′ mod pBob: pick bY = gᵇ mod preal X, to Malloryreal Y, to MalloryXₘ, posing as BobYₘ, posing as AliceK1 = Xₘᵃ mod pthinks: shared with BobK1 = Xᵃ′ mod pmatches Alice's K1K2 = Yᵇ′ mod pmatches Bob's K2K2 = Yₘᵇ mod pthinks: shared with AliceMallory relays: decrypts with K1, re-encrypts with K2

    Passive eavesdropping cannot recover K without solving the discrete logarithm problem, but Diffie-Hellman on its own authenticates no one. Mallory runs two separate exchanges — one impersonating Bob to Alice, one impersonating Alice to Bob — and ends up holding both resulting keys, invisibly relaying (and reading) every message. The fix is to sign each side's public value, so a substituted Xₘ or Yₘ fails verification.

    Worked example

    AnswerWith Eve using private value 9 against Alice (a=6) and Bob (b=15), Alice ends up sharing key 9 with Eve and Bob ends up sharing key 10 with Eve: two different keys, and neither notices.

    Same p=23p = 23, g=5g = 5, Alice’s a=6a = 6, Bob’s b=15b = 15. Eve intercepts the exchange and uses her own private value 99 in both directions.

    1. Eve and Alice. Alice still computes her real X=56mod23=8X = 5^6 \bmod 23 = 8 (as above) and sends it, straight to Eve, who intercepts it. Eve computes a public value using her own private 99: 59mod235^9 \bmod 23. Continuing the powers from the previous derivation: 59=58×51mod23=16×5=80mod23=115^9 = 5^8 \times 5^1 \bmod 23 = 16 \times 5 = 80 \bmod 23 = 11. Eve sends 1111 to Alice, posing as Bob’s YY.

    2. Alice computes what she believes is the shared secret: 116mod2311^{6} \bmod 23. 112=121mod23=611^2 = 121 \bmod 23 = 6, 114=62=36mod23=1311^4 = 6^2 = 36 \bmod 23 = 13. 116=114×112mod23=13×6=78mod23=911^6 = 11^4 \times 11^2 \bmod 23 = 13 \times 6 = 78 \bmod 23 = 9. Alice’s key is 99.

    3. Eve computes the same leg from her side: X9mod23=89mod23X^{9} \bmod 23 = 8^{9} \bmod 23. From the earlier powers of 88: 88=48^8 = 4, so 89=88×81mod23=4×8=32mod23=98^9 = 8^8 \times 8^1 \bmod 23 = 4 \times 8 = 32 \bmod 23 = 9. Eve gets 99 too. This leg of the exchange is internally consistent, exactly like a real one.

    4. Eve and Bob. Eve reuses her public value 1111 (still 59mod235^9 \bmod 23) and sends it to Bob, posing as Alice’s XX. Bob still computes his real Y=515mod23=19Y = 5^{15} \bmod 23 = 19 (as above) and sends it, straight to Eve.

    5. Bob computes what he believes is the shared secret: 199mod2319^{9} \bmod 23. Continuing from 196=219^6 = 2: 197=2×19=38mod23=1519^7 = 2 \times 19 = 38 \bmod 23 = 15, 198=15×19=285mod23=919^8 = 15 \times 19 = 285 \bmod 23 = 9, 199=9×19=171mod23=1019^9 = 9 \times 19 = 171 \bmod 23 = 10. Bob’s key is 1010.

    6. Eve computes the same leg from her side: 1115mod2311^{15} \bmod 23. Continuing from 116=911^6 = 9 by repeated multiplication: 117=9×11=99mod23=711^7 = 9 \times 11 = 99 \bmod 23 = 7, 118=7×11=77mod23=811^8 = 7 \times 11 = 77 \bmod 23 = 8, 119=8×11=88mod23=1911^9 = 8 \times 11 = 88 \bmod 23 = 19, 1110=19×11=209mod23=211^{10} = 19 \times 11 = 209 \bmod 23 = 2, 1111=2×11=2211^{11} = 2 \times 11 = 22, 1112=22×11=242mod23=1211^{12} = 22 \times 11 = 242 \bmod 23 = 12, 1113=12×11=132mod23=1711^{13} = 12 \times 11 = 132 \bmod 23 = 17, 1114=17×11=187mod23=311^{14} = 17 \times 11 = 187 \bmod 23 = 3, 1115=3×11=33mod23=1011^{15} = 3 \times 11 = 33 \bmod 23 = 10. Eve gets 1010 too.

    7. Eve now holds two working keys: 99, shared with Alice, and 1010, shared with Bob. Alice’s key (99) and Bob’s key (1010) do not match each other. Neither Alice nor Bob can tell, because each of them completed a valid-looking exchange and reached a key that is genuinely shared, just with Eve, not with each other.

    Control

    Digital signatures. Have each party sign their own public value with a private signing key established out of band, and have the other party verify that signature before trusting the value. Eve can still intercept XX and YY, but she cannot produce a signature over her own substituted public value that verifies against Alice’s or Bob’s real signing key: she does not hold either one. The Diffie-Hellman exchange itself is unchanged; what changes is that a forged public value now fails a check before it is ever used to compute a key.

    Exam detail

    The exam pairs this attack with its fix as a single question shape: name the vulnerability (no authentication of the public values), describe the attack (two independent exchanges, two different keys, both parties deceived), and name the control (signed public values). Diffie-Hellman is secure against a passive eavesdropper because of the discrete logarithm problem; it is not secure against an active attacker on its own, because the protocol as given has no way to check who sent a public value.

    Pitfall

    Do not describe the person-in-the-middle attack as “breaking” the discrete logarithm problem. Eve never computes a private exponent from a public value. She does not need to, because she generates her own valid key pair and runs two ordinary exchanges. The failure is entirely about missing authentication, not about the underlying maths giving way.

    Recall

    Alice and Bob complete a Diffie-Hellman exchange and both compute the same K. Does this prove they were talking to each other, and not to an attacker in the middle?

    No. Each side only proves that some exchange completed correctly on their end. The maths guarantees agreement with whoever sent the public value they received, not that the sender was who they think it was. Diffie-Hellman on its own authenticates nobody, which is exactly what makes the person-in-the-middle attack possible.