Introduction

Last week a security researcher reported something that sounds like a joke. One of the private keys belonging to eMudhra, a certificate authority trusted by every major browser and operating system, was the number 4. Not a 384-bit random integer. The digit four. Seven of its sibling intermediate keys were almost as bad: 56, 66, 115, 155, 156, 230 and 237. Every one of them was smaller than 256, so anyone could recover them in well under a second.

A certificate authority’s private key is the one secret the whole trust model rests on. If you know it, you can issue a certificate for any domain you like and a victim’s browser or tool will treat it as genuine. So a key of 4 is not a curiosity. It is close to a skeleton key for the web.

And it is probably sitting on your machine right now. The affected root ships in the trust store of essentially every desktop and server operating system. On a Mac you can look for it yourself:

$ security find-certificate -a -p -c "emSign" \
    /System/Library/Keychains/SystemRootCertificates.keychain \
    | openssl x509 -noout -subject

On my fully updated laptop that prints two certificates:

subject=C=IN, OU=emSign PKI, O=eMudhra Technologies Limited, CN=emSign Root CA - G1
subject=C=IN, OU=emSign PKI, O=eMudhra Technologies Limited, CN=emSign ECC Root CA - G3

The second one, the ECC root, is the family at the centre of this story. And this reaches far beyond web browsers. The same certificates and trust stores secure curl, wget, git, your package manager, language runtimes like Python, Node and Go and every Docker base image. All of them lean on the operating-system CA bundle, not a browser’s. Browsers reacted within days. Chrome and Mozilla blocked the bad certificates on 17 October, Apple on the 19th and Microsoft on the 20th. Mozilla removed the emSign ECC root outright. But the operating-system trust stores that everything else depends on update far more slowly, which is exactly why both roots are still in my keychain a week later. Apple blocked the certificates through a separate list rather than removing the root. Its own advisory even lists the manual steps that curl, git and python3 still need.

Rather than just enjoy the headline, let us treat this as a learning opportunity. The same few lines of code that an attacker would run turn into a tool you can use to check your own certificates.


A thirty-second refresher on TLS and ECC

When you open an HTTPS site, TLS does three jobs before any data flows:

  • Authentication. You know you are really talking to your bank and not an impostor.
  • Integrity. Nobody can quietly alter the bytes on the way.
  • Encryption. Nobody can read them either.

Of the three, authentication is the one that rests on public-key cryptography and a chain of certificates. It is also the part this story is about. Integrity and encryption of the actual data are handled separately, by symmetric keys that the authenticated handshake agrees on, so a weak certificate key does not by itself expose your traffic. Your browser trusts a small set of root authorities that ship with your operating system or browser. A root signs intermediate authorities. An intermediate signs the leaf certificate your bank actually presents. If every signature in that chain is valid and the top of the chain is a root you already trust, the connection is accepted.

Each certificate carries a public key. Two systems are in common use. RSA rests on the difficulty of factoring large integers. Elliptic-curve cryptography, or ECC, rests on the difficulty of the discrete logarithm problem on an elliptic curve. New authorities tend to prefer ECC because the keys are far smaller for the same strength. A 384-bit ECC key is roughly as strong as a 7680-bit RSA key. The same curves are used by banks, by governments and by Bitcoin. eMudhra’s broken keys were on the standard NIST curve P-384.

The one fact to carry forward is simple. On a fixed curve with a fixed public base point \( G \), a private key is just an integer \( d \) and the public key is the point \( Q = d \cdot G \).

Remark. One detail about how these pieces fit together matters, because it is easy to overstate the danger. The certificate’s key is only ever used to sign, which is to say to authenticate. It never establishes the session key. Modern TLS derives that from a fresh Diffie-Hellman exchange that the certificate merely signs. The ephemeral keys are thrown away after the handshake. This is forward secrecy. So a stolen authority key lets someone forge certificates and impersonate a site in real time, which is an active man-in-the-middle attack. It does not let them decrypt traffic captured earlier, because that would need the ephemeral keys that no longer exist.


Why a key of 4 is catastrophic

There is nothing wrong with the number 4 as a point on the curve. The value \( 4 \cdot G \) is a perfectly valid public key. The problem is entropy. A P-384 private key is supposed to be chosen uniformly from about \( 2^{384} \) values, which is roughly \( 10^{115} \). If the key is instead an integer below 256, there are only 256 possible values and anyone can try them all. You compute \( 1 \cdot G, 2 \cdot G, 3 \cdot G \) and so on and compare each against the public key in the certificate. The moment one matches, you have recovered \( d \) and with it the authority’s signing key. The curve never failed here. The randomness did, which is true of almost every real key compromise.


Detecting it: a small Go tool, no optimization

The way to catch this is the same search an attacker would run, used defensively. Given a certificate, pull out its ECDSA public key \( Q \) and look for a small \( d \) with \( d \cdot G = Q \). For the demonstration everything we need is in the Go standard library. The package crypto/x509 parses the certificate and crypto/elliptic gives us the curve.

The heart of it is the scan. Because we try \( d = 1, 2, 3 \) and so on in order, each key differs from the last by exactly one copy of the generator, so \( P_{d+1} = P_d + G \). Each step is a single point addition rather than a fresh multiplication.

// scanSmall returns a small d with d*G == (qx, qy), if one exists.
func scanSmall(curve elliptic.Curve, qx, qy *big.Int, limit int64) (int64, bool) {
    gx, gy := curve.Params().Gx, curve.Params().Gy
    x, y := new(big.Int).Set(gx), new(big.Int).Set(gy) // d = 1, so P_1 = G
    for d := int64(1); d <= limit; d++ {
        if x.Cmp(qx) == 0 && y.Cmp(qy) == 0 {
            return d, true
        }
        x, y = curve.Add(x, y, gx, gy)                  // P_{d+1} = P_d + G
    }
    return 0, false
}

Wrapping that in a certificate parser gives the whole check.

func inspect(cert *x509.Certificate, limit int64) bool {
    pub, ok := cert.PublicKey.(*ecdsa.PublicKey)
    if !ok {
        return false // not an ECDSA key
    }
    if d, found := scanSmall(pub.Curve, pub.X, pub.Y, limit); found {
        fmt.Printf("  [WEAK] %s (%s): recovered private key = %d (NOT secret)\n",
            cert.Subject.CommonName, pub.Curve.Params().Name, d)
        return true
    }
    fmt.Printf("  [ok]   %s: no small key up to %d\n", cert.Subject.CommonName, limit)
    return false
}

Running it against a forged weak certificate, then against a live host, looks like this. The complete program is listed at the end of this post.

$ certcheck -make-weak 4
wrote weak-p384-d4.pem (self-signed P-384 CA, private key = 4)

$ certcheck -check weak-p384-d4.pem
  [WEAK] weak-demo-d4 (P-384): recovered private key = 4 (NOT secret)

$ certcheck -fetch example.com:443
checking 4 certificate(s) presented by example.com:443
  [ok]   example.com (P-256): no small key up to 1000000
  [ok]   Cloudflare TLS Issuing ECC CA 3 (P-256): no small key up to 1000000
  [ok]   SSL.com TLS Transit ECC CA R2 (P-384): no small key up to 1000000
  [ok]   SSL.com TLS ECC Root CA 2022 (P-384): no small key up to 1000000

The fetch mode opens a TLS connection only to read the chain the server presents, then checks every certificate in it. The root keys are usually fine. It was the intermediates that eMudhra signed that were weak, so checking the whole chain matters.

None of this is tied to P-384. The scan reads the curve out of the certificate, so the same check finds a weak P-256 or P-521 key with no change at all. The function scanSmall only needs the base point and the addition law. The standard library covers the NIST curves. A secp256k1 key, the one Bitcoin uses, would just mean swapping in the btcec package, which offers the same operations.


Optimization 1: only the powers of two

The scan above is already cheap per step, because consecutive keys differ by one addition. The trouble comes when you need \( d \cdot G \) for a \( d \) you cannot reach by adding \( G \) to where you already are. Two cases bring this up. The first is giving each parallel worker the start of its block, since those starts are far apart. The second is checking a candidate that is not next to the last one, for instance when a broken generator could have produced a key anywhere in a range like \( [0, 2^{40}) \). Either way, doubling up from \( G \) every time is wasteful.

This is the elliptic-curve version of a trick you probably already know from ordinary arithmetic. To compute a large power like \( a^{d} \) you do not multiply \( a \) by itself \( d \) times. You square repeatedly to get \( a, a^2, a^4, a^8 \) and so on, then multiply together the ones that the binary digits of \( d \) switch on. On an elliptic curve the group operation is addition rather than multiplication, so squaring becomes doubling and multiplying becomes adding, but the idea is exactly the same.

So the fix is to precompute the powers of two once, meaning \( G, 2G, 4G, 8G \) and so on, then build any \( d \cdot G \) by adding together the ones that the binary digits of \( d \) pick out. There are no doublings at scan time and only about half as many additions as there are digits.

// precompute 2^0*G, 2^1*G, ... once
func precomputePowers(curve elliptic.Curve) [][2]*big.Int {
    gx, gy := curve.Params().Gx, curve.Params().Gy
    table := make([][2]*big.Int, curve.Params().N.BitLen())
    x, y := new(big.Int).Set(gx), new(big.Int).Set(gy)
    for i := range table {
        table[i] = [2]*big.Int{new(big.Int).Set(x), new(big.Int).Set(y)}
        x, y = curve.Double(x, y)
    }
    return table
}

// d*G = sum of 2^i*G over the set bits of d
func mulFromTable(curve elliptic.Curve, table [][2]*big.Int, d *big.Int) (x, y *big.Int) {
    for i := 0; i < d.BitLen(); i++ {
        if d.Bit(i) == 0 {
            continue
        }
        if x == nil {
            x, y = new(big.Int).Set(table[i][0]), new(big.Int).Set(table[i][1])
        } else {
            x, y = curve.Add(x, y, table[i][0], table[i][1])
        }
    }
    return x, y
}

Optimization 2: parallel compute

Every candidate is checked completely independently of the others, so there is nothing to share and nothing to coordinate. That is the easiest possible case for parallelism and Go makes it almost free. We cut the range from 1 to the limit into one block per core, start a goroutine for each, let it jump to the start of its block once, then walk the block with the one-addition step. This is where the previous section pays off. Each worker reaches its start point with the precomputed table, which is sub-linear. That is what makes sharding worthwhile. If the only way to reach a block’s start were to add \( G \) one step at a time, computing the start points alone would cost as much as the whole scan we are trying to split.

func scanSmallParallel(curve elliptic.Curve, qx, qy *big.Int, limit int64, workers int) (int64, bool) {
    gx, gy := curve.Params().Gx, curve.Params().Gy
    table := precomputePowers(curve)
    found := make(chan int64, 1)
    var wg sync.WaitGroup
    per := limit / int64(workers)
    for w := 0; w < workers; w++ {
        lo := int64(w)*per + 1
        hi := lo + per
        if w == workers-1 {
            hi = limit + 1
        }
        wg.Add(1)
        go func(lo, hi int64) {
            defer wg.Done()
            x, y := mulFromTable(curve, table, big.NewInt(lo)) // lo*G, via the table
            for d := lo; d < hi; d++ {
                if x.Cmp(qx) == 0 && y.Cmp(qy) == 0 {
                    select {
                    case found <- d:
                    default:
                    }
                    return
                }
                x, y = curve.Add(x, y, gx, gy)
            }
        }(lo, hi)
    }
    go func() { wg.Wait(); close(found) }()
    d, ok := <-found
    return d, ok
}

On my ten-core laptop the standard library P-384 arithmetic, which is deliberately constant time rather than fast, manages about 58,000 candidates per second on one core and about 435,000 per second across all ten. The scale is worth keeping in mind though. eMudhra’s keys were all below 256, so for this incident parallelism never mattered. Two hundred and fifty-six trials finish before you lift your finger off the return key. Parallelism earns its place only when you widen the bound, where a generator that can reach \( 2^{30} \) turns a multi-hour single-core sweep into a few minutes, or when you sweep many hosts at once.


Going further

There are a few directions I did not need here, but that matter the moment you run this at scale.

  • A faster inner formula. Doing the additions in Jacobian coordinates and sharing one field inversion across a whole batch, which is Montgomery’s trick, takes a from-scratch scanner from tens of thousands to millions of keys per second per core.
  • A database of known-weak keys. Small integers, published brainwallet hashes and the eight eMudhra values, stored so that checking a certificate becomes a lookup instead of a scan.
  • When the key might not be small. The two-sum trick still works here and does not even need the key to be near zero. We pick a random \( d \), compute \( dG \) and store it in a hash. Then we check whether \( P – dG \) is already in the hash. If it is, matching some earlier \( d’G \), then \( P = (d + d’)G \), so the secret key is \( d + d’ \) and we are done. If not, we drop \( dG \) into the hash and try another \( d \). Exactly like two-sum, one side is hashed and the other probes it. A collision shows up after about \( \sqrt{N} \) tries instead of \( N \). It never threatens a properly generated key; it only widens the range we can realistically sweep.

None of it was needed to catch a private key of 4. All of it is what you reach for when the haystack gets real.


The whole tool in one file

Putting the pieces together, here is the complete detector. It is standard library only, so go build is all it needs. The -check mode reads a PEM certificate and -fetch pulls the live chain from a running server and checks every certificate in it.

// certcheck: detect low-entropy ECDSA keys in X.509 certificates.
// Standard library only. Build: go build .  Run: ./certcheck -fetch host:443
package main

import (
    "crypto/ecdsa"
    "crypto/elliptic"
    "crypto/rand"
    "crypto/tls"
    "crypto/x509"
    "crypto/x509/pkix"
    "encoding/pem"
    "flag"
    "fmt"
    "math/big"
    "os"
    "runtime"
    "sync"
    "time"
)

// precomputePowers builds the table G, 2G, 4G, ... once, with one doubling each.
func precomputePowers(curve elliptic.Curve) [][2]*big.Int {
    gx, gy := curve.Params().Gx, curve.Params().Gy
    table := make([][2]*big.Int, curve.Params().N.BitLen())
    x, y := new(big.Int).Set(gx), new(big.Int).Set(gy)
    for i := range table {
        table[i] = [2]*big.Int{new(big.Int).Set(x), new(big.Int).Set(y)}
        x, y = curve.Double(x, y)
    }
    return table
}

// mulFromTable computes d*G as the sum of the table entries for the set bits of d.
func mulFromTable(curve elliptic.Curve, table [][2]*big.Int, d *big.Int) (x, y *big.Int) {
    for i := 0; i < d.BitLen(); i++ {
        if d.Bit(i) == 0 {
            continue
        }
        if x == nil {
            x, y = new(big.Int).Set(table[i][0]), new(big.Int).Set(table[i][1])
        } else {
            x, y = curve.Add(x, y, table[i][0], table[i][1])
        }
    }
    return x, y
}

// scanSmallParallel looks for a small d with d*G == (qx, qy), sharding [1, limit]
// across workers. The precomputed table lets each worker jump to the start of its
// block cheaply; from there it advances with the one-addition step P_{d+1} = P_d + G.
func scanSmallParallel(curve elliptic.Curve, qx, qy *big.Int, limit int64, workers int) (int64, bool) {
    gx, gy := curve.Params().Gx, curve.Params().Gy
    table := precomputePowers(curve)
    found := make(chan int64, 1)
    var wg sync.WaitGroup
    per := limit / int64(workers)
    for w := 0; w < workers; w++ {
        lo := int64(w)*per + 1
        hi := lo + per
        if w == workers-1 {
            hi = limit + 1
        }
        wg.Add(1)
        go func(lo, hi int64) {
            defer wg.Done()
            x, y := mulFromTable(curve, table, big.NewInt(lo)) // lo*G, via the table
            for d := lo; d < hi; d++ {
                if x.Cmp(qx) == 0 && y.Cmp(qy) == 0 {
                    select {
                    case found <- d:
                    default:
                    }
                    return
                }
                x, y = curve.Add(x, y, gx, gy)
            }
        }(lo, hi)
    }
    go func() { wg.Wait(); close(found) }()
    d, ok := <-found
    return d, ok
}

// inspect checks one certificate's ECDSA key and prints a verdict.
func inspect(cert *x509.Certificate, limit int64, workers int) {
    pub, ok := cert.PublicKey.(*ecdsa.PublicKey)
    if !ok {
        fmt.Printf("  [skip] %s: not an ECDSA key\n", cert.Subject.CommonName)
        return
    }
    name, curve := cert.Subject.CommonName, pub.Curve.Params().Name
    if d, found := scanSmallParallel(pub.Curve, pub.X, pub.Y, limit, workers); found {
        fmt.Printf("  [WEAK] %s (%s): recovered private key = %d (NOT secret)\n", name, curve, d)
    } else {
        fmt.Printf("  [ok]   %s (%s): no small key up to %d\n", name, curve, limit)
    }
}

// makeWeak writes a self-signed P-384 certificate whose private key is exactly d,
// so the detection can be demonstrated without a real weak certificate.
func makeWeak(d int64, path string) error {
    curve := elliptic.P384()
    priv := &ecdsa.PrivateKey{D: big.NewInt(d)}
    priv.Curve = curve
    priv.X, priv.Y = curve.ScalarBaseMult(big.NewInt(d).Bytes())
    tmpl := &x509.Certificate{
        SerialNumber:          big.NewInt(d),
        Subject:               pkix.Name{CommonName: fmt.Sprintf("weak-demo-d%d", d)},
        NotBefore:             time.Now().Add(-time.Hour),
        NotAfter:              time.Now().Add(24 * time.Hour),
        IsCA:                  true,
        BasicConstraintsValid: true,
    }
    der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &priv.PublicKey, priv)
    if err != nil {
        return err
    }
    out, err := os.Create(path)
    if err != nil {
        return err
    }
    defer out.Close()
    return pem.Encode(out, &pem.Block{Type: "CERTIFICATE", Bytes: der})
}

func main() {
    check := flag.String("check", "", "PEM certificate file to inspect")
    fetch := flag.String("fetch", "", "host:port to pull the live TLS chain from")
    mkWeak := flag.Int64("make-weak", -1, "write a demo P-384 cert with this private key")
    limit := flag.Int64("limit", 1_000_000, "largest private key to test for")
    workers := flag.Int("workers", runtime.NumCPU(), "parallel workers")
    flag.Parse()

    switch {
    case *mkWeak >= 0:
        path := fmt.Sprintf("weak-p384-d%d.pem", *mkWeak)
        if err := makeWeak(*mkWeak, path); err != nil {
            fmt.Fprintln(os.Stderr, err)
            os.Exit(1)
        }
        fmt.Printf("wrote %s (self-signed P-384 CA, private key = %d)\n", path, *mkWeak)

    case *check != "":
        data, err := os.ReadFile(*check)
        if err != nil {
            fmt.Fprintln(os.Stderr, err)
            os.Exit(1)
        }
        block, _ := pem.Decode(data)
        cert, err := x509.ParseCertificate(block.Bytes)
        if err != nil {
            fmt.Fprintln(os.Stderr, err)
            os.Exit(1)
        }
        inspect(cert, *limit, *workers)

    case *fetch != "":
        conn, err := tls.Dial("tcp", *fetch, &tls.Config{InsecureSkipVerify: true}) // read the chain, do not trust it
        if err != nil {
            fmt.Fprintln(os.Stderr, err)
            os.Exit(1)
        }
        defer conn.Close()
        chain := conn.ConnectionState().PeerCertificates
        fmt.Printf("checking %d certificate(s) presented by %s\n", len(chain), *fetch)
        for _, c := range chain {
            inspect(c, *limit, *workers)
        }

    default:
        fmt.Fprintln(os.Stderr, "usage: certcheck -make-weak N | -check cert.pem | -fetch host:443")
        os.Exit(1)
    }
}

What we can take away

On Thursday, five days after the report, eMudhra’s board decided to leave the global TLS business entirely. A curve with \( 2^{384} \) possible keys was defeated by a key generation process that reached for a number a child could guess. The curve was never the weak point and neither was the browser. The weak point was the one place no amount of standardisation can reach, which is the moment a secret is created.

So the prevention advice splits in two. If you run an authority or issue certificates, generate keys on a vetted HSM and scan every key you publish before it leaves the building. A single addition per candidate would have caught all eight of these in microseconds. If you are a user, do not assume a browser update covers you. Check your non-browser clients, meaning your build images, your language runtimes and your devices, because that is where a distrusted but not yet removed root tends to linger.

The takeaway is small and practical. Check where your own trust is anchored, especially outside the browser. And if you generate keys for anything, spend the microsecond it takes to prove they were not simply the number 4. If any of this touches your own work, I am happy to talk.