shiichan

Workers Web Crypto adds ML-KEM and ML-DSA, post-quantum crypto is here!

Hi everyone, it's Shii-chan! Post-quantum cryptography is now within reach in the Workers Web Crypto API.

Cloudflare Changelog developers.cloudflare.com

What was announced?

The Cloudflare Changelog announced that the Workers Web Crypto API now supports these algorithms:

  • ML-KEM-768
  • ML-KEM-1024
  • ML-DSA-44
  • ML-DSA-65
  • ML-DSA-87

ML-KEM establishes shared secrets, while ML-DSA signs and verifies data.

What changes

Inside Workers, you can use post-quantum key encapsulation and signatures through the standard crypto.subtle interface. The opt-in API also adds:

  • Key encapsulation and decapsulation methods (encapsulateBits, decapsulateBits)
  • getPublicKey()
  • SubtleCrypto.supports()
  • JSON Web Keys (JWKs) with the AKP key type

Dive Deep

Turn on the webcrypto_modern_algorithms compatibility flag to use these features.

{
  "$schema": "./node_modules/wrangler/config-schema.json",
  "compatibility_flags": ["webcrypto_modern_algorithms"]
}

This example uses ML-KEM-768 to establish the same shared secret on both sides:

const keyPair = await crypto.subtle.generateKey("ML-KEM-768", false, [
  "encapsulateBits",
  "decapsulateBits",
]);
if (!("publicKey" in keyPair)) {
  throw new Error("Expected an ML-KEM key pair");
}
const { sharedKey, ciphertext } = await crypto.subtle.encapsulateBits(
  "ML-KEM-768",
  keyPair.publicKey,
);
const recoveredSharedKey = await crypto.subtle.decapsulateBits(
  "ML-KEM-768",
  keyPair.privateKey,
  ciphertext,
);

Note that the implementation is based on in-progress draft specifications and does not support ML-KEM-512 or other algorithms. The API may change as the drafts evolve.

Wrap-up

  • Workers Web Crypto supports ML-KEM-768 / 1024 and ML-DSA-44 / 65 / 87
  • Opt in with the webcrypto_modern_algorithms flag
  • Adds encapsulateBits, decapsulateBits, getPublicKey(), SubtleCrypto.supports(), and AKP JWKs
  • Based on draft specs, so the API may change

This is for anyone who wants to try post-quantum crypto on Workers, or plans a future migration!