So far every attacker in this chapter has been like Eve: she copies everything Alice and Bob send, but she never changes anything. That kind of attacker is called passive. Diffie–Hellman protects Alice and Bob from Eve, because she would have to solve a discrete log problem.
Now meet Mallory (from “malicious”). Mallory is an active attacker. She sits on the path between Alice and Bob and can intercept a message, block it, change it, or send a brand new one pretending to be someone else. This isn’t far-fetched. Whoever runs the Wi-Fi network you’re on, a hacked router, or a fake hotspot named “Airport Free WiFi” is in exactly that position.
What Diffie–Hellman Doesn’t Check¶
When Bob receives Alice’s public value , it’s just a number. Nothing about it proves it came from Alice. Diffie–Hellman protects a secret from someone who listens, but it has no way to prove who sent each public value.
Security people separate two goals:
Confidentiality: nobody else can read the message.
Authentication: you know who you’re talking to.
The Attack¶
Use the same numbers as the Diffie–Hellman example: , , Alice’s secret and Bob’s secret . Mallory picks her own secret, , and computes her own public value
Swap Alice’s value. Alice sends toward Bob. Mallory catches it and keeps a copy. In its place she sends Bob her own . Bob has no way to tell that 8 didn’t come from Alice.
Swap Bob’s value. Bob replies with . Mallory catches that too and sends Alice the same . Now Alice thinks 8 is Bob’s value, and Bob thinks 8 is Alice’s.
Two secrets, not one. Each person follows the protocol exactly:
Alice computes .
Bob computes .
Mallory computes both: and .
Here’s the whole attack as a diagram. Each arrow is a number sent across the network, and Mallory catches every one of them.
Without Mallory, Alice and Bob would have shared the secret 18. Instead there are two secrets, and Mallory knows both of them.
p, g = 23, 5
a, b, m = 4, 3, 6 # Alice's, Bob's and Mallory's secrets
A = pow(g, a, p) # Alice sends this, but Mallory catches it
B = pow(g, b, p) # Bob sends this, but Mallory catches it
M = pow(g, m, p) # Mallory sends this to both of them
print("Alice's key:", pow(M, a, p))
print("Bob's key: ", pow(M, b, p))
print("Mallory's keys:", pow(A, m, p), "with Alice and", pow(B, m, p), "with Bob")
print("Without Mallory they would share", pow(B, a, p))Alice's key: 2
Bob's key: 6
Mallory's keys: 2 with Alice and 6 with Bob
Without Mallory they would share 18
What Mallory Can Do Now¶
Alice encrypts a message with key 2 and sends it. Mallory decrypts it with 2, reads it, and changes it if she wants. Then she encrypts it again with key 6 and forwards it to Bob. Bob decrypts with 6 and everything looks normal. The same thing works in the other direction.
Mallory can’t just pass Alice’s ciphertext along unchanged, because Bob’s key is 6, not 2: the message would decrypt to garbage and Bob would get suspicious. So she has to stay in the middle for the whole conversation, decrypting and re-encrypting everything. That’s where the name man-in-the-middle attack comes from.
Why the Math Didn’t Help¶
The discrete log problem is still hard, and Mallory never touched it. She ran two honest Diffie–Hellman exchanges, one with Alice and one with Bob. A bigger prime wouldn’t stop her, and neither would a different generator or fresh secrets for each conversation. The weakness is that Alice and Bob had no way to check who they were talking to. Strong math protects a secret from listeners, but it can’t tell you whose number you received.
ElGamal has the same problem. If Mallory can swap Bob’s public key for her own before Alice encrypts, Alice will encrypt her message for Mallory. Every public key system needs a way to know that a public key really belongs to the person you think it does.
Stopping Mallory¶
Real systems check that a public key belongs to who it claims in three main ways, from simplest to most automatic.
Compare fingerprints. Messaging apps like Signal show a “safety number” (WhatsApp calls it a security code) that two people can compare in person or over a video call. If Mallory were in the middle, the numbers wouldn’t match.
Digital signatures. The person signs their public key with a key you already trust. You’ll build signatures with RSA in Digital Signatures and Hashing.
Certificates. On the web, a certificate authority signs each website’s public key, and your browser checks that signature. That’s what the padlock in the address bar means.