Path: blob/main/foundations/05-discrete-log-diffie-hellman/break/small-subgroup-attack.ipynb
483 views
Break: Small Subgroup Attack on Diffie-Hellman
Module 05 | Breaking Weak Parameters
If we don't validate public keys, an attacker can confine the shared secret to a tiny subgroup.
Why This Matters
Diffie-Hellman key exchange assumes both parties send legitimate public keys of the form . But what if an attacker sends a malicious value instead?
If is not a safe prime, then has small factors, and the group contains small subgroups. An attacker can:
Find an element of small order (where )
Send as their "public key" instead of
The victim computes as the shared secret --- but this value is confined to only possibilities
The attacker brute-forces all values to find the shared secret
This attack requires no knowledge of the victim's secret exponent . The attacker only needs to search a space of size instead of size .
The Scenario
Alice and Bob agree on DH parameters where is a prime but not a safe prime. This means has small factors. Eve (the attacker) intercepts and replaces one of the public keys with an element of small order.
Let's pick . Check the factorization of :
Step 3: Eve Constructs a Malicious Public Key
Eve picks a small factor of and finds an element of order .
To find an element of order : take any generator and compute . By Lagrange's theorem, has order .
Eve sends to Alice as if it were Bob's public key.
Step 5: Eve Brute-Forces the Shared Secret
Eve doesn't know Alice's secret , but she knows the shared secret must be one of only values. She simply tries all of them.
In a real protocol, the shared secret is used to derive a symmetric key. Eve can test each candidate by trying to decrypt the first message.
The Fix: Public Key Validation
There are two complementary defenses:
Use safe primes: If with prime, the only subgroup orders are . The only "small" subgroup has order 2 (elements ), which is trivially checkable.
Validate received public keys: Check that the received value satisfies:
(not 0, 1, or )
(for safe prime , this confirms is in the subgroup of order )
Exercises
Larger prime: Try where . Find elements of order 5 and 7. How many attempts does Eve need for each?
Combined attack: Use the subgroup of order to learn . Then use order 16 to learn . Can you combine these with CRT to narrow down even further?
Active vs. passive: In this attack, Eve must be an active (man-in-the-middle) attacker. Why can't she mount this attack by just eavesdropping?
Summary
| Concept | Detail |
|---|---|
| Attack | Send an element of small order as a public key |
| Effect | Shared secret is confined to possible values |
| Cost to attacker | brute-force attempts |
| Prerequisite | has small factors (not a safe prime) |
| Fix | Use safe primes + validate public keys |
Key takeaways:
The small subgroup attack is an active attack: Eve replaces a public key in transit.
The victim's computation is "trapped" in a subgroup of size .
Safe primes eliminate all dangerous small subgroups.
Even with safe primes, public key validation is essential defense-in-depth.