DEV Community

Siftwright
Siftwright

Posted on

Is your server post-quantum? Test TLS for X25519MLKEM768 with ~50 lines of stdlib Python

Chrome, Firefox and Safari now offer the hybrid post-quantum group X25519MLKEM768 in every TLS 1.3 handshake. Whether your connection actually uses it depends on the server. If the server only knows X25519, the browser quietly falls back, and nothing in the padlock tells you.

Python can't answer the question with the ssl module: on most systems it links an OpenSSL older than 3.5, which doesn't know ML-KEM at all. But you don't need to complete a handshake to find out what a server supports. You only need to send a ClientHello and read the first message that comes back. That fits in about 50 lines of stdlib Python.

The idea

  1. Build a TLS 1.3 ClientHello that offers only X25519MLKEM768 as a key share (group 0x11EC), with X25519 listed in supported_groups as a fallback.
  2. Read the server's reply:
    • a ServerHello whose key_share names 0x11EC means post-quantum key exchange is on;
    • a HelloRetryRequest (a ServerHello with a fixed magic random value) asking for x25519 means the server doesn't do ML-KEM;
    • a protocol_version alert means no TLS 1.3, so no post-quantum key exchange is possible.
  3. Hang up. No keys are derived and no application data is sent.

The one fiddly part is the key share. The hybrid share is 1,216 bytes: a 1,184-byte ML-KEM-768 encapsulation key followed by a 32-byte X25519 public key (draft-ietf-tls-ecdhe-mlkem). We never decapsulate, so random bytes would almost do. But FIPS 203 requires servers to run a modulus check on the encapsulation key: every packed 12-bit coefficient must be below q = 3329. Random 12-bit values fail that about 19% of the time each, so a purely random key would be rejected. The script packs 768 random coefficients that are all < 3329, plus a 32-byte seed, and the result passes the check.

The script

"""Does a server negotiate post-quantum TLS (X25519MLKEM768)? Stdlib only."""
import os, socket, struct, sys

GROUPS = {0x11EC: "X25519MLKEM768", 0x001D: "x25519", 0x0017: "secp256r1", 0x6399: "X25519Kyber768Draft00"}
HRR_RANDOM = bytes.fromhex("cf21ad74e59a6111be1d8c021e65b891c2a211167abb8c5e079e09e2c8a8339c")

def mlkem768_shaped_key():
    # A well-formed ML-KEM-768 encapsulation key: 768 coefficients < q packed as 12-bit pairs + 32-byte seed.
    # We never decapsulate, so it doesn't need a private key; it only has to pass the server's modulus check.
    out = bytearray()
    for _ in range(384):
        a, b = (int.from_bytes(os.urandom(2), "big") % 3329 for _ in range(2))
        out += bytes([a & 0xFF, (a >> 8) | ((b & 0x0F) << 4), b >> 4])
    return bytes(out) + os.urandom(32)          # 1184 bytes

def ext(t, body): return struct.pack(">HH", t, len(body)) + body
def vec16(b): return struct.pack(">H", len(b)) + b

def client_hello(host):
    share = mlkem768_shaped_key() + os.urandom(32)   # ML-KEM part first, then X25519 (draft-kwiatkowski-tls-ecdhe-mlkem)
    exts = b"".join([
        ext(0x0000, vec16(b"\x00" + vec16(host.encode()))),                      # server_name
        ext(0x000A, vec16(struct.pack(">HH", 0x11EC, 0x001D))),                  # supported_groups
        ext(0x000D, vec16(struct.pack(">6H", 0x0403, 0x0804, 0x0401, 0x0503, 0x0805, 0x0807))),  # signature_algorithms
        ext(0x002B, b"\x02\x03\x04"),                                             # supported_versions: TLS 1.3
        ext(0x0033, vec16(struct.pack(">HH", 0x11EC, len(share)) + share)),       # key_share: hybrid only
    ])
    body = b"\x03\x03" + os.urandom(32) + b"\x20" + os.urandom(32) + vec16(struct.pack(">3H", 0x1301, 0x1302, 0x1303)) + b"\x01\x00" + vec16(exts)
    hs = b"\x01" + len(body).to_bytes(3, "big") + body
    return b"\x16\x03\x01" + vec16(hs)

def check(host, port=443, timeout=8):
    with socket.create_connection((host, port), timeout=timeout) as s:
        s.sendall(client_hello(host))
        hdr = s.recv(5, socket.MSG_WAITALL)
        if len(hdr) < 5: return "no response"
        rec = s.recv(struct.unpack(">H", hdr[3:5])[0], socket.MSG_WAITALL)
    if hdr[0] == 0x15:
        return {70: "no TLS 1.3 (protocol_version alert)", 40: "no common group (handshake_failure)"}.get(rec[1], f"TLS alert {rec[1]}")
    if hdr[0] != 0x16 or rec[0] != 0x02: return "unexpected reply"
    p = 4 + 2; random = rec[p:p + 32]; p += 32
    p += 1 + rec[p]; p += 3                                   # session id, cipher suite, compression
    end = p + 2 + struct.unpack(">H", rec[p:p + 2])[0]; p += 2
    while p < end:
        t, n = struct.unpack(">HH", rec[p:p + 4]); data = rec[p + 4:p + 4 + n]; p += 4 + n
        if t == 0x0033:
            g = struct.unpack(">H", data[:2])[0]; name = GROUPS.get(g, hex(g))
            return f"asks to retry with {name}" if random == HRR_RANDOM else f"negotiated {name}"
    return "TLS 1.2 or no key_share"

if __name__ == "__main__":
    for h in sys.argv[1:]:
        try: print(f"{h:28} {check(h)}")
        except OSError as e: print(f"{h:28} error: {e}")
Enter fullscreen mode Exit fullscreen mode

Usage:

python3 pqcheck.py example.com yourdomain.com
Enter fullscreen mode Exit fullscreen mode

Testing it without trusting the internet

I wanted to see all three outcomes against servers whose config I control. Node 22 ships with OpenSSL 3.5, which supports X25519MLKEM768, so a few lines make local test servers:

// srv.js: one server per group configuration (self-signed cert from `openssl req -x509 ...`)
const tls=require('tls'),fs=require('fs');
for (const [port,curve] of [[8443,'X25519MLKEM768:X25519'],[8444,'X25519'],[8445,'X25519:X25519MLKEM768']]) {
 tls.createServer({key:fs.readFileSync('k.pem'),cert:fs.readFileSync('c.pem'),ecdhCurve:curve,minVersion:'TLSv1.3'},s=>s.end()).listen(port);
 }
Enter fullscreen mode Exit fullscreen mode

Plus one TLS 1.2-only server (maxVersion: 'TLSv1.2') on port 8446. The script's real output against them:

localhost:8443   negotiated X25519MLKEM768
localhost:8444   asks to retry with x25519
localhost:8445   negotiated X25519MLKEM768
localhost:8446   no TLS 1.3 (protocol_version alert)
Enter fullscreen mode Exit fullscreen mode

Port 8445 is worth noticing. It lists X25519 first, but the client only sent an ML-KEM key share, so the server picks ML-KEM rather than paying for a retry round trip. That's why this test tells you what a server supports, not what it prefers. To check preference, send both key shares and see which one the server picks. A server that picks X25519 when a browser offers both leaves those visitors on classical crypto.

Gotcha: TLS-intercepting proxies

The first time I ran this from a sandbox, every host on the internet answered "asks to retry with x25519", including Google, which supports ML-KEM. The sandbox sent traffic through an egress proxy that terminates TLS, so I was measuring the proxy, not the sites. Corporate networks, antivirus "HTTPS scanning" and some CI runners do the same thing. If every host gives you the same answer, check who issued the certificate:

echo | openssl s_client -connect google.com:443 -servername google.com 2>/dev/null | grep issuer
Enter fullscreen mode Exit fullscreen mode

If the issuer is your company or your proxy and not a public CA, your results describe the middlebox.

What real sites look like today

From a network without interception, our hosted checker did live handshakes on 22 November 2026:

Host X25519MLKEM768 Picks PQ when offered both
google.com yes yes
github.com no no (x25519)
wikipedia.org no no (x25519)
amazon.com no no (x25519)

Sites behind Cloudflare get it by default. Most origins running a distro OpenSSL older than 3.5 don't.

Turning it on

You need a TLS library with ML-KEM support (OpenSSL 3.5+, BoringSSL, Go 1.24+, rustls with aws-lc-rs). Put the hybrid group first and keep classical groups as a fallback, for example nginx:

ssl_ecdh_curve X25519MLKEM768:X25519:prime256v1:secp384r1;
Enter fullscreen mode Exit fullscreen mode

Go 1.24+ offers X25519MLKEM768 by default when CurvePreferences is left empty. If you set the list yourself, add tls.X25519MLKEM768 to it.

Limits of the script

  • It checks one group (X25519MLKEM768). It doesn't test SecP256r1MLKEM768, SecP384r1MLKEM1024, pure ML-KEM or the retired X25519Kyber768Draft00 (0x6399). To test those, add them to supported_groups and key_share with the right share sizes.
  • It reads only the first TLS record. That's enough for every server I tried, but a server that splits its ServerHello across records would need a small read loop.
  • It doesn't check preference, the certificate or HSTS.

If you'd rather not run code, the free post-quantum TLS checker does the same handshake from our server. It also tests the other ML-KEM groups, the preference when both are offered, the certificate and HSTS, with no signup. For bulk checks there's a /v1/pqc-scan endpoint in the Siftwright API (up to 20 hosts per call).

Top comments (0)