Skip to main content
Cipher Visualizers home

The Invalid-Curve Attack: Breaking ECDH by Sending Bad Points

ECDH point arithmetic never uses the curve's b parameter, so a server that skips the on-curve check will do math with points from other curves. This guide sends small-order points from invalid curves to a static-key ECDH server, leaks the key one small prime at a time, and reassembles it with the Chinese Remainder Theorem.

Pashalis Laoutaris 15 min read

Try it

Interactive Invalid-Curve Attack on ECDH

🔐 Invalid-Curve Attack on ECDH

A toy ECDH server reuses one private key and never checks that incoming points lie on its curve. That one missing check leaks the key.
Enter text and click a button to start!
1. The server and the missing check
2. Points from other curves, each of small order
3. Each query leaks d modulo a small prime
4. The Chinese Remainder Theorem rebuilds d
Click the button to recover a static ECDH key by sending points off the curve.

The Invalid-Curve Attack: Breaking ECDH by Sending Bad Points

Introduction

ECDH key exchange has one validation step that is easy to forget and fatal to skip. When a peer sends an elliptic-curve point, the receiver is supposed to check that the point actually lies on the agreed curve before doing anything with it. The reason is a quirk of the arithmetic: the formulas for adding and doubling points on a short-Weierstrass curve y^2 = x^3 + a*x + b use the coefficient a, but never b. So the same code works, without complaint, on points from any curve that shares the same a.

An attacker exploits this against a server that reuses a static private key. By sending points from carefully chosen invalid curves, curves with the same a but a different b, picked so they contain points of small order, the attacker makes the server’s secret multiplication land in a tiny subgroup. Each query leaks the private key modulo a small prime, and the Chinese Remainder Theorem glues the pieces into the whole key. This guide runs the attack on a toy curve, reusing the small discrete logs of the Pohlig-Hellman breaker and the CRT.

Table of Contents

The Formula That Ignores b

Adding two points on y^2 = x^3 + a*x + b uses the slope between them, and doubling a point uses the slope of the tangent, which involves 3x^2 + a. Neither formula contains b. The b only decides which points are on the curve; once you have two points and apply the formulas, the result is computed the same way no matter what b was.

So if a server receives a point Q and computes d*Q with its static private key d, without first checking that Q is on its curve, it is really doing scalar multiplication on whatever curve Q happens to lie on. The attacker gets to choose that curve.

Small-Order Points on Invalid Curves

The real curve is chosen to have a large prime order, so the private key d is a hard discrete log on it. An invalid curve, with the same a but a different b', has a different order, and that order can be smooth, full of small prime factors. On such a curve the attacker can find a point P of small order r, meaning r*P is the point at infinity for a small r.

When the server computes S = d*P, the result lies in the subgroup generated by P, which has only r elements. So S is one of P, 2P, 3P, ..., (r-1)P, O, and which one it is reveals d mod r. The attacker learns this by trying each multiple of P and matching it against what the server returned, whether that is the shared point itself or, more realistically, a hash or a MAC computed from it. This is a tiny discrete log, solvable by inspection for small r, or with baby-step giant-step for larger ones.

Leaking the Key One Prime at a Time

One invalid curve gives d modulo one small prime. The attacker repeats with different invalid curves, each contributing a different small prime r_i, collecting:

d ≡ a_1 (mod r_1)
d ≡ a_2 (mod r_2)
   ...
d ≡ a_k (mod r_k)

Once the product r_1 * r_2 * ... * r_k exceeds the real curve’s order n, the Chinese Remainder Theorem determines d uniquely. A handful of queries is enough, one per small prime. The private key that is a hard discrete log on the real curve is read off cheaply, because the server did the hard part on the wrong curves. A final check that d*G equals the known public key confirms the result.

Interactive Visualizer

The visualizer above runs the attack on a toy curve small enough to see the whole thing. Pick the server’s static private key; the attacker never sees it. The server computes d*Q for each point sent and returns a hash of the shared secret, without checking that the point is on its curve.

The four cards show the missing on-curve check, the small-order points the attacker sends from invalid curves, each query leaking d modulo a small prime, and the CRT that rebuilds the key, verified against the public key. The same arithmetic runs in the Python below, and the two agree on every recovered residue and the final key.

A Worked Example

The real curve is y^2 = x^3 + x + 104 over the field of 1048583 elements, with prime order n = 1048601. The server’s static private key is 735419, which the attack must find.

The attacker sends seven points, each from a different invalid curve y^2 = x^3 + x + b' and each of small prime order: orders 3, 7, 2, 5, 149, 11, and 29. For each one, the server returns a hash of d times that point, and the attacker matches it against the few multiples of the point to read off d modulo that prime:

d ≡ 2 (mod 3),  6 (mod 7),  1 (mod 2),  4 (mod 5),  104 (mod 149),  3 (mod 11),  8 (mod 29)

The product of those orders is 9981510, larger than n, so the Chinese Remainder Theorem pins d exactly. It returns 735419, and d*G matches the public key. Seven queries, no access to the private key, and the static ECDH secret is recovered.

Python Implementation

This builds the toy curve, finds the invalid curves and their small-order points by counting points, runs the queries, and reassembles the key.

# invalid_curve.py
#
# The invalid-curve (small-subgroup) attack on ECDH. A server with a STATIC private key computes the
# shared secret d*Q for whatever point Q a peer sends. The short-Weierstrass formulas use only the
# curve's 'a', never 'b', so if the server does not check that Q lies on the real curve, an attacker
# sends points from other curves y^2 = x^3 + a*x + b' chosen to have small-order points. Each such
# point leaks d modulo a small prime; the Chinese Remainder Theorem then reassembles the whole key.
# Toy curve so the point counting runs in a second. Teaching code.

import hashlib
from math import prod

# ---- a toy curve over F_p; the REAL curve is y^2 = x^3 + A*x + B with a large prime order ----
P = 1048583
A = 1
B = 104                     # the real curve has prime order n (found by point counting)


def inv(a, m):
    return pow(a, -1, m)


def add(pt, qt):
    """Point addition on y^2 = x^3 + A*x + b. It never uses b: that b-independence is the whole weakness."""
    if pt is None:
        return qt
    if qt is None:
        return pt
    x1, y1 = pt
    x2, y2 = qt
    if x1 == x2 and (y1 + y2) % P == 0:
        return None
    if pt == qt:
        m = (3 * x1 * x1 + A) * inv(2 * y1, P) % P
    else:
        m = (y2 - y1) * inv((x2 - x1) % P, P) % P
    x3 = (m * m - x1 - x2) % P
    y3 = (m * (x1 - x3) - y1) % P
    return (x3, y3)


def mul(k, pt):
    r = None
    a = pt
    while k > 0:
        if k & 1:
            r = add(r, a)
        a = add(a, a)
        k >>= 1
    return r


# ---- point counting on the toy field, via a quadratic-residue sieve ----
def qr_table():
    t = [-1] * P
    t[0] = 0
    for i in range((P // 2) + 1):
        v = i * i % P
        if v:
            t[v] = 1
    return t


QR = qr_table()


def curve_order(b):
    s = 0
    for x in range(P):
        s += QR[(x * x * x + A * x + b) % P]
    return P + 1 + s


def is_prime(n):
    if n < 2:
        return False
    i = 2
    while i * i <= n:
        if n % i == 0:
            return False
        i += 1
    return True


def point_on(b):
    """Any point on y^2 = x^3 + A*x + b', found by scanning x until the right side is a square."""
    for x in range(2, P):
        rhs = (x * x * x + A * x + b) % P
        if QR[rhs] == 1:
            y = pow(rhs, (P + 1) // 4, P)     # P = 3 mod 4, so this is a square root
            if y * y % P == rhs:
                return (x, y)
    return None


def find_small_order_points(n):
    """For small primes r, an invalid curve b' whose order is divisible by r and a point of order r.

    Collect distinct primes until their product exceeds n, so the CRT fixes the key uniquely.
    """
    primes = [r for r in range(2, 200) if is_prime(r)]
    collected = []
    have = set()
    product = 1
    b = 0
    while product <= n:
        b += 1
        if b == B or (4 * A + 27 * b * b) % P == 0:
            continue
        o = curve_order(b)
        for r in primes:
            if r in have or o % r != 0:
                continue
            base = point_on(b)
            pt = mul(o // r, base)
            if pt is None:
                continue
            assert mul(r, pt) is None            # order exactly r
            collected.append({"r": r, "b": b, "P": pt})
            have.add(r)
            product *= r
            break
    return collected


def oracle_tag(secret_point):
    """What a server leaks: a hash of the shared secret (here the full point). The attacker checks it."""
    data = b"shared" if secret_point is None else ("%d,%d" % secret_point).encode()
    return hashlib.sha256(data).digest()


def crt(residues, moduli):
    x = 0
    M = prod(moduli)
    for a, m in zip(residues, moduli):
        Mi = M // m
        x = (x + a * Mi * inv(Mi, m)) % M
    return x, M


def attack(server_respond, subgroups):
    """server_respond(b, Q) returns the oracle tag for d * Q (the server runs the b-less formulas). Recover d mod each r, then CRT."""
    residues, moduli = [], []
    for sg in subgroups:
        r, b, Pt = sg["r"], sg["b"], sg["P"]
        target = server_respond(b, Pt)
        for i in range(r):                          # small discrete log: which multiple of P matches
            if oracle_tag(mul(i, Pt)) == target:
                residues.append(i)
                moduli.append(r)
                break
    d, _ = crt(residues, moduli)
    return d, residues, moduli


if __name__ == "__main__":
    n = curve_order(B)
    print("Toy ECDH: field F_%d, real curve y^2 = x^3 + %d x + %d, prime order n = %d." % (P, A, B, n))

    # the server's static private key, never revealed
    d = 735419 % n
    G = point_on(B)
    pub = mul(d, G)
    print("server static private key d =", d, "(the attack must find this)")

    # the server computes d*Q for any Q, and leaks a hash of it, without checking Q is on the real curve
    queries = [0]

    def server_respond(b, Q):
        queries[0] += 1
        return oracle_tag(mul(d, Q))

    print("\nprecomputing invalid curves with small-order points...")
    subgroups = find_small_order_points(n)
    print("small subgroups used:", [(sg["r"], "b'=%d" % sg["b"]) for sg in subgroups])
    print("product of orders =", prod(sg["r"] for sg in subgroups), "> n:", prod(sg["r"] for sg in subgroups) > n)

    recovered, residues, moduli = attack(server_respond, subgroups)
    print("\nd mod r for each subgroup:", dict(zip(moduli, residues)))
    print("server queries used:", queries[0])
    print("recovered d =", recovered)
    print("recovered == real key:", recovered == d)
    print("check: recovered key reproduces the public key:", mul(recovered, G) == pub)

Running it prints this:

Toy ECDH: field F_1048583, real curve y^2 = x^3 + 1 x + 104, prime order n = 1048601.
server static private key d = 735419 (the attack must find this)

precomputing invalid curves with small-order points...
small subgroups used: [(3, "b'=1"), (7, "b'=2"), (2, "b'=5"), (5, "b'=6"), (149, "b'=7"), (11, "b'=9"), (29, "b'=13")]
product of orders = 9981510 > n: True

d mod r for each subgroup: {3: 2, 7: 6, 2: 1, 5: 4, 149: 104, 11: 3, 29: 8}
server queries used: 7
recovered d = 735419
recovered == real key: True
check: recovered key reproduces the public key: True

I checked the code several ways. The point counting gives the real curve a prime order, so the key is a genuine discrete log there, and each small-order point is verified to have exactly its claimed order. The points the attacker sends are confirmed to be off the real curve but on their own invalid curves. The attack recovers the exact private key, and a final check confirms that the recovered key reproduces the public key. Across 30 random private keys, every one was recovered from the same seven queries. The JavaScript in the visualizer agrees with the Python on all seven residues and the recovered key for every tested key, and its SHA-256 and curve arithmetic match the Python.

Limitations

A static key is required. The attack needs the server to reuse one private key across many queries, so that each query leaks a piece of the same secret. An ephemeral ECDH key, fresh per handshake, gives the attacker only one query against each secret, which leaks almost nothing. Static or semi-static ECDH is the vulnerable setting.

A toy curve. The field here is about a million elements so the point counting runs in a second and the small subgroups are easy to see. On a real 256-bit curve the attacker precomputes invalid curves with smooth orders offline, a heavier but one-time job, and the online attack is the same handful of queries.

The server must leak something about the shared secret. The attacker needs to tell which multiple of the point the server reached. A returned point, a key confirmation, a MAC, or a distinguishable success or failure all work. The practical TLS attacks recovered the key from exactly such confirmation messages.

Teaching code. The point counting is a simple residue sum and the small discrete logs are done by scanning, which is clear but not fast. The method is identical at any size.

Security Status

The invalid-curve attack is real and has hit real software. Biehl, Meyer, and Müller described it in 2000, and in 2015 Jager, Schwenk, and Somorovsky recovered private keys from TLS servers whose libraries skipped the on-curve check, needing only a few thousand handshakes against a reused key. The fix is simple and mandatory: validate every incoming point. Confirm that its coordinates satisfy the curve equation, that it is not the point at infinity, and, where the curve has a cofactor, that it lies in the correct subgroup.

Several designs remove the trap by construction. Curve25519 and X25519 use a Montgomery ladder that depends only on the x-coordinate and is safe for every input, so there is nothing to validate. For the short-Weierstrass curves still used in TLS and many protocols, point validation is the defense, and it is one of the checks a cryptographic library must never omit. The lesson is that the arithmetic’s indifference to b is a feature for speed and a liability without validation.

FAQ

Why does ignoring b let the attack happen?

The add and double formulas compute with a alone, so they produce a well-defined result for any point, on the real curve or not. Without an on-curve check, the server runs its secret scalar multiplication on the attacker’s chosen curve, where the key is easy to extract.

How is this different from a reused or biased nonce?

Those attacks target ECDSA signatures and the per-signature nonce. This targets ECDH key exchange and the static private key, and it exploits missing input validation rather than a weak random value. The shared tool is the small discrete log plus the CRT.

Why must the key be static?

Each invalid-curve query leaks the key modulo one small prime. To combine several primes with the CRT, they must all constrain the same key, so the server has to reuse it. A fresh ephemeral key per exchange denies the attacker the repetition the CRT needs.

Does this affect X25519?

No, it does not. X25519 uses a Montgomery ladder on the x-coordinate that is defined and safe for every 32-byte input, including points on the curve’s twist, so there is no invalid-point trap to exploit. The attack targets short-Weierstrass ECDH without point validation.

What exactly stops it?

Validating each received point: check that it satisfies the curve equation, reject the point at infinity, and enforce the subgroup when there is a cofactor. That one check turns every invalid point into a rejected message, and the attack gets nothing.

References

  1. Biehl, Ingrid, Bernd Meyer, and Volker Müller. “Differential Fault Attacks on Elliptic Curve Cryptosystems.” Advances in Cryptology, CRYPTO 2000, Lecture Notes in Computer Science 1880, Springer, 2000, pp. 131-146.

  2. Lim, Chae Hoon, and Pil Joong Lee. “A Key Recovery Attack on Discrete Log-based Schemes Using a Prime Order Subgroup.” CRYPTO ’97, Lecture Notes in Computer Science 1294, 1997, pp. 249-263.

  3. Jager, Tibor, Jörg Schwenk, and Juraj Somorovsky. “Practical Invalid Curve Attacks on TLS-ECDH.” Computer Security, ESORICS 2015, Lecture Notes in Computer Science 9326, Springer, 2015, pp. 407-425.

  4. The ECDH guide on this site, whose key exchange this attack targets: /blog/ecdh_guide/

  5. The Pohlig-Hellman breaker on this site, whose small discrete logs and CRT this attack reuses: /blog/pohlig_hellman_breaker_guide/

A visual representation of a faulty RSA-CRT signature being factored by a single gcd
Modern & Applied Cryptography Breakers Advanced

The Bellcore Attack: One Fault Breaks RSA-CRT

Most RSA implementations sign with the Chinese Remainder Theorem for a 4x speedup. A single bit-flip during that computation produces a faulty signature that is correct modulo one prime and wrong modulo the other, and one gcd then factors the key. This guide runs the Bellcore attack on a real 128-byte RSA key.

12 min read

A visual representation of the Square attack recovering an AES round key from balanced sets
Modern & Applied Cryptography Breakers Advanced

The Square Attack: Breaking AES Reduced to Four Rounds

Full AES has no practical break, but reduced-round AES does. The Square attack feeds 256 chosen plaintexts through 4-round AES, uses a balance property that survives three rounds, and peels off the last round key one byte at a time, then runs the key schedule backwards to the master key.

16 min read

A visual representation of recovering a Merkle-Hellman private key from the public key with lattice reduction
Modern & Applied Cryptography Breakers Advanced

Breaking Merkle-Hellman: Recovering the Key from the Public Key

Shamir broke the basic Merkle-Hellman knapsack in 1982 without seeing a single ciphertext. This guide rebuilds his key-recovery attack with a small LLL lattice, finds a working private key for the guide's 32-bit key in milliseconds, and measures it on keys of up to 256 bits.

21 min read