← Back to section · Tutorials & Labs

Generate an ML-KEM Key with genpkey and Verify It with an Encapsulation Round Trip

From OpenSSL 3.5, genpkey can generate ML-KEM key pairs directly. This article walks through generation, text inspection, and a full encapsulation/decapsulation check with pkeyutl to prove the key really works.

Published 22 November 2026 · 6 min read · PQ Frontier

Generate an ML-KEM Key with genpkey and Verify It with an Encapsulation Round Trip
AI-generated site artwork
Video explainer · OpenSSL practice: command line and API

The previous lab ran TLS with an ML-DSA certificate. This article returns to key encapsulation itself: with no library code at all, we generate an ML-KEM key pair purely with the OpenSSL command line and immediately verify that it can perform a real encapsulation and decapsulation. The same flow is the cheapest health check you can run before wiring ML-KEM into any system.

Step 0: Confirm the Version and That the Algorithm Is Listed

ML-KEM is natively supported only from OpenSSL 3.5 (released April 2025), in the default provider. Check the environment first:

openssl version
openssl list -kem-algorithms | grep -i ml-kem

The second command should list the three parameter sets ML-KEM-512, ML-KEM-768 and ML-KEM-1024 (possibly with an @default suffix indicating they come from the default provider). If the version is below 3.5 or the list is empty, do not keep wrestling with generation commands — the problem is the environment, not your syntax. See the previous article in this section on the provider architecture, or upgrade the distribution first.

Step 1: Generate the Key Pair

openssl genpkey -algorithm ML-KEM-768 -out mlkem768-key.pem
openssl pkey -in mlkem768-key.pem -pubout -out mlkem768-pub.pem

genpkey is OpenSSL’s generic entry point for asymmetric key generation — RSA, EC and ML-DSA all go through it, and ML-KEM is no exception. Choosing a parameter set: 512, 768 and 1024 correspond to NIST security levels 1, 3 and 5, and the default choice for production and experiments alike is 768 — it is what the TLS hybrid group X25519MLKEM768 uses. The result is a PEM private key file. Two things to note: first, FIPS 203 allows a private key to be stored as a “seed” or in “expanded” form, and OpenSSL 3.5 exports the seed form by default, so the file is noticeably smaller than the public key — that is normal, not a truncated key. Second, the private key file is the key: manage its permissions and storage location as sensitive material, and clean it up promptly after experiments.

Step 2: Text Inspection Against the FIPS 203 Sizes

Generated is not the same as generated correctly. Print the key with pkey -text and check it:

openssl pkey -in mlkem768-key.pem -text -noout
openssl pkey -pubin -in mlkem768-pub.pem -text -noout

The output shows the algorithm name and the public key in hexadecimal. Cross-check the sizes against the FIPS 203 parameter table — the most reliable self-check available:

Parameter set Public key (bytes) Ciphertext (bytes) Shared secret (bytes)
ML-KEM-512 800 768 32
ML-KEM-768 1184 1088 32
ML-KEM-1024 1568 1568 32

If you chose 768, the public key part should be 1184 bytes. A mismatch usually means you generated something else (for instance, a mistyped algorithm name silently falling back to another key type — genpkey normally errors out instead, but the checking habit is worth keeping).

Step 3: An Encapsulation–Decapsulation Round Trip with pkeyutl

A KEM’s actual job is encapsulation and decapsulation. OpenSSL 3.5 added the -encap and -decap options to pkeyutl (they do not exist before 3.5 — part of the version prerequisite), so the round trip can be verified purely on the command line:

# Encapsulating side: use the public key to produce a ciphertext and a shared secret
openssl pkeyutl -encap -pubin -inkey mlkem768-pub.pem \
  -out ct.bin -secret ss-encap.bin

# Decapsulating side: recover the shared secret from the ciphertext with the private key
openssl pkeyutl -decap -inkey mlkem768-key.pem \
  -in ct.bin -secret ss-decap.bin

# The shared secrets on both sides must match byte for byte
cmp ss-encap.bin ss-decap.bin && echo OK
ls -l ct.bin ss-encap.bin

There are three acceptance criteria: cmp prints nothing and OK appears (the two shared secrets agree), ct.bin is 1088 bytes (the ciphertext size for the 768 parameter set), and the shared secret file is 32 bytes. Only when all three pass is the key pair genuinely usable. You can also run a negative test: flip one byte of ct.bin and decapsulate again — the resulting shared secret should be different from the original, yet the command itself does not fail. That is ML-KEM’s implicit rejection by design: decapsulation of an invalid ciphertext returns a pseudorandom secret instead of an error. Application code must understand this; “the command did not complain” must never be read as “the ciphertext was valid”.

Choosing a Parameter Set, and Managing Key Files

Two questions worth settling before you generate anything. First, the selection logic: levels 1, 3 and 5 differ not in “speed” but in the trade-off between security margin and size — 512 has the smallest public keys and ciphertexts and suits bandwidth-sensitive scenarios benchmarked against AES-128; 768 is the balance point chosen jointly by NIST and the major TLS implementations, and the popularity of the X25519MLKEM768 hybrid group makes it the safest choice for interoperability; 1024 carries an obvious size cost and is only worth it in high-assurance scenarios benchmarked against AES-256. Absent an explicit size or compliance constraint, start experiments and production alike at 768 — which is why this article uses it throughout.

Second, day-to-day management of the key material. genpkey can also password-protect the private key (with -aes256 and -pass, used the same way as for RSA keys). But note: import and export of ML-KEM private keys across tools currently follows the two FIPS 203 representations — seed and expanded — and different implementations may prefer different private key file forms. When migrating a key to another tool, run the Step 3 encapsulation round trip on the new tool first, and only then delete the old copy. A KEM key also differs from a signature key in role: as a long-term static key it faces the “harvest now, decrypt later” time dimension, so its rotation period and storage should be managed at the sensitivity level of a decryption key, not a session key. And keys generated in a lab environment must not be repurposed for real use — a lab machine’s randomness environment and key custody usually do not meet production requirements; this discipline applies to classical algorithms just as much.

A frequently asked point: why does ML-KEM have no “encrypt this data” usage like RSA, forcing the encapsulation detour? Because a KEM’s semantics are precisely to negotiate a shared secret; it does not carry arbitrary messages directly. In real systems, the 32-byte secret from encapsulation is fed into a KDF to derive session keys, and symmetric encryption carries the data — which is exactly how the TLS 1.3 hybrid key exchange works. Once you understand this, you will stop hunting through pkeyutl for an “encrypt a file with ML-KEM” option: by design, it does not exist. Conversely, when evaluating a tool that claims ML-KEM support, the encapsulation round trip is the right litmus test: stricter than “it can generate a key”, lighter than “it runs a full protocol”, and when something fails you can localise it step by step with the four steps in this article.

A small note on file naming conventions: this article distinguishes private and public key files with -key.pem and -pub.pem. It looks trivial, but in scripts and tutorials it prevents the classic accidents of distributing a public key as if it were private, or uploading a private key to a server by mistake. Wherever automated flows handle key files, this naming discipline is worth fixing in place.

Summary

This pipeline is worth turning into a habit: first confirm the algorithm is listed with list, then generate with genpkey, check the sizes against the official parameter table with pkey -text, and finish with a pkeyutl encapsulation round trip closed out by cmp. Pass all four steps and both the key and the toolchain are verified; if any step fails, work backwards in order — version, provider, algorithm name, file permissions — the cause is almost never outside that list.

Tags: OpenSSLML-KEMgenpkeypkeyutl