Diffie-Hellman & Man-in-the-Middle
🤝 Diffie-Hellman and Man-in-the-Middle Attacks
Diffie-Hellman (DH) is a fundamental protocol for establishing a shared secret over an insecure channel. However, when used on its own, it is highly vulnerable to Man-in-the-Middle (MITM) attacks. This is where other public-key encryption schemes like RSA become essential to provide authentication and prevent such attacks.
🔑 Diffie-Hellman Key Exchange Refresher
The core idea of Diffie-Hellman involves two parties, Alice and Bob, agreeing on a shared secret key without ever directly exchanging the key itself.
Shared Parameters: They first agree on a generator
gand a large prime numbern.Private Values:
Alice generates a private value
a.Bob generates a private value
b.
Public Exchange:
Alice computes
g^a mod nand sends this public value to Bob.Bob computes
g^b mod nand sends this public value to Alice.
Shared Secret Calculation:
Alice receives
g^bfrom Bob and computes(g^b)^a mod n, which simplifies tog^(ba) mod n.Bob receives
g^afrom Alice and computes(g^a)^b mod n, which simplifies tog^(ab) mod n.
Result: Both Alice and Bob arrive at the same shared secret
g^(ab) mod n.
This process allows them to derive a secret key that an eavesdropper cannot easily compute, even if they intercept g^a and g^b, because they do not know the private values a or b.
😈 The Man-in-the-Middle (MITM) Problem with Diffie-Hellman Alone
Diffie-Hellman, by itself, does not provide any protection against an active attacker who can intercept and manipulate messages. This attacker is known as a Man-in-the-Middle (MITM).
🎭 Shawn's MITM Scenario
Let's introduce Shawn, a malicious actor who controls the network between Alice and Bob. Shawn can not only read messages but also intercept, modify, and retransmit them.
Alice Initiates: Alice wants to establish a shared secret with Bob. She computes
g^aand sends it towards Bob.Shawn Intercepts (Alice-Shawn Leg): Shawn intercepts
g^aand prevents it from reaching Bob. Shawn then pretends to be Bob.Shawn generates his own private key
s.Shawn computes
g^sand sends it to Alice, claiming it's from Bob.Alice, unaware, computes her shared secret with Shawn:
(g^s)^a = g^(as) mod n. Shawn now has a shared secret with Alice.
Shawn Initiates (Shawn-Bob Leg): Shawn then pretends to be Alice and initiates a key exchange with the real Bob.
Shawn sends
g^s(his own public value) to Bob, claiming it's from Alice.Bob, unaware, computes his shared secret with Shawn:
(g^s)^b = g^(sb) mod n. Shawn now has a shared secret with Bob.
Shawn Controls Communication: Shawn has successfully established two independent shared secrets:
One with Alice (
g^(as) mod n).One with Bob (
g^(sb) mod n).
Consequence: Any message Alice sends to Bob will be encrypted with the key derived from g^(as). Shawn can intercept this, decrypt it using his knowledge of s, read or modify the message, re-encrypt it with the key derived from g^(sb), and send it to Bob. Bob will then decrypt it, believing it came from Alice. Shawn acts as a transparent relay, completely compromising the communication.
⚠️ Diffie-Hellman is designed for two parties who trust each other to generate a shared secret. It offers no inherent mechanism to verify the identity of the other party.
🛡️ RSA and Public Key Cryptography to the Rescue
This is where RSA (or other public-key cryptosystems like DSA) comes into play. RSA provides authentication, ensuring that the parties communicating are indeed who they claim to be, thereby preventing MITM attacks on the Diffie-Hellman exchange.
🤝 Combining Diffie-Hellman with RSA for Authentication
Let's consider Bob as a server with a publicly known public key and a securely kept private key (e.g., an RSA key pair).
Alice Sends DH Parameter: Alice sends her Diffie-Hellman public value
g^ato Bob.Bob Responds with Signed DH Parameter: Bob computes his Diffie-Hellman public value
g^b. To prove his identity and prevent MITM, he does the following:He creates a digital signature of his
g^bvalue (e.g.,Hash(g^b)).He encrypts this hash with his private key (
K_private).He sends both
g^band the signed hash (Signed(Hash(g^b), K_private)) to Alice.
Alice Verifies Bob's Identity:
Alice receives
g^band the signed hash.She computes
Hash(g^b)herself.She then uses Bob's public key (
K_public) to decrypt the received signed hash.If her computed hash matches the decrypted hash, she knows that only Bob (who possesses the private key) could have sent that message. This is called signature verification.
Shared Secret Established: Once Alice verifies Bob's identity, she can confidently use
g^bto compute the shared secretg^(ab) mod n.
Result: Shawn cannot perform a MITM attack because he does not possess Bob's private key. If Shawn tries to send his own g^s and sign it, Alice's verification will fail because Shawn cannot produce a signature that can be successfully decrypted by Bob's public key.
This combined approach is a fundamental part of modern internet key exchanges, such as:
IKE protocol (used in VPNs)
TLS handshake (used in HTTPS)
For instance, when you visit an HTTPS website, the cipher suite established often involves both Diffie-Hellman and RSA. A typical example might be TLS 1.2 Elliptic Curve Diffie-Hellman RSA, indicating that ECDH is used for key exchange and RSA for authentication.
🔄 Why Use Both Diffie-Hellman and RSA? The Concept of Perfect Forward Secrecy
A common question is: if RSA can encrypt a shared key, why bother with Diffie-Hellman at all? Couldn't Alice just generate a symmetric key, encrypt it with Bob's RSA public key, and send it to him? Historically, this was done, but it introduces a significant security vulnerability related to Perfect Forward Secrecy (PFS).
🕰️ RSA-Only Key Exchange (Without PFS)
If only RSA were used for key exchange:
Alice generates a symmetric session key.
Alice encrypts this session key with Bob's RSA public key.
Alice sends the encrypted session key to Bob.
Bob decrypts it with his RSA private key.
They use this symmetric key for the rest of the session.
The Problem: If Bob's RSA private key is ever compromised (e.g., through a hack, theft, or legal compulsion), an attacker who has been recording all past encrypted traffic can then decrypt all historical communications. By decrypting the RSA-encrypted session keys from past handshakes, they can decrypt all the symmetric messages associated with those sessions.
🛡️ Diffie-Hellman with RSA (With PFS)
This is where the combination shines, providing Perfect Forward Secrecy:
RSA's Role: RSA (or similar public-key schemes) is primarily used for authentication. It verifies the identity of the server (Bob) and ensures that the Diffie-Hellman parameters exchanged are legitimate.
Diffie-Hellman's Role: Diffie-Hellman is used to generate an ephemeral (short-lived, per-session) shared secret key. This DH key is specific to that single communication session.
The Benefit of PFS: If an attacker compromises Bob's RSA private key at a later date, they cannot decrypt past communications. This is because the actual session key was derived using Diffie-Hellman parameters that were only used for that specific session and were never directly encrypted with Bob's long-term RSA private key. The DH parameters themselves are public, and the private DH values (a and b) are discarded after the session key is established. Even if the RSA private key is compromised, it only allows an attacker to impersonate Bob in future communications, not to decrypt past ones.
⚡ Performance Considerations
RSA: While secure for authentication, RSA is computationally slower for encrypting large amounts of data.
Symmetric Ciphers (e.g., AES): These are much faster for bulk data encryption.
Therefore, the common practice is to:
Use RSA for authentication (signing the Diffie-Hellman parameters).
Use Diffie-Hellman (often Elliptic Curve Diffie-Hellman - ECDH) to quickly establish a short-lived symmetric key for each session.
Use this symmetric key (e.g., a 2048-bit AES key) to encrypt the actual data traffic, leveraging its speed.
This hybrid approach provides both strong authentication, perfect forward secrecy, and efficient data encryption.

Ask anything
We've read this whole study set, so questions about it get the best answers.