# Establish a shared secret with ML-KEM and OpenSSL

Run a disposable ML-KEM-768 encapsulation and decapsulation round trip, compare the derived secrets and capture evidence for the next compatibility test.

Author: [Neeraj](https://www.linkedin.com/in/neerajsabharwal/)
Published: 2026-11-28T17:04:04.582Z
Updated: 2026-11-28T17:04:04.582Z
Canonical page: https://www.quantumcomputing.ai/blog/qcai-editorial-2026-11-19-ml-kem-shared-secret-openssl

## Generate a disposable recipient key and compare the two derived secrets

An ML-KEM round trip has an observable result. Encapsulation produces a shared secret and a ciphertext for a recipient. Decapsulation uses the recipient’s private key and that ciphertext to recover the corresponding secret. Compare the two local outputs to check this basic path before building a wider compatibility experiment.

NIST FIPS 203 specifies ML-KEM, a key-encapsulation mechanism used to establish a shared secret that can support symmetric encryption and authentication. This lab stops at the shared-secret comparison. Its commands use ML-KEM-768 as an example parameter set. [NIST FIPS 203](https://csrc.nist.gov/pubs/fips/203/final)

## 1. Prepare an isolated lab

Use a POSIX shell and an OpenSSL build with ML-KEM available. OpenSSL added this functionality in version 3.5. Record the version your installation reports and confirm the provider configuration used by your eventual test environment. [OpenSSL ML-KEM documentation](https://docs.openssl.org/3.5/man7/EVP_KEM-ML-KEM/)

Run the following as one shell script. It creates disposable files with restrictive permissions in a temporary directory and removes that directory on exit. The files contain a private key and derived secrets, so keep them within this local test.

```sh
set -eu
lab_dir=$(mktemp -d)
trap 'rm -rf "$lab_dir"' EXIT
cd "$lab_dir"
umask 077

openssl version
openssl genpkey -algorithm ML-KEM-768 -out recipient.pem
openssl pkey -in recipient.pem -pubout -out recipient-public.pem
openssl pkeyutl -encap -pubin -inkey recipient-public.pem -out capsule.bin -secret sender.secret
openssl pkeyutl -decap -inkey recipient.pem -in capsule.bin -secret recipient.secret
cmp -s sender.secret recipient.secret
printf 'Shared-secret comparison passed\n'
```

A failed command stops the script before the final message. Inspect its diagnostic and record the environment before attempting a correction.

## 2. Follow the artifacts through the round trip

The recipient key-generation step creates a private key and exports its public component. Encapsulation uses that public key and writes two outputs. The ciphertext goes to `capsule.bin`, while `sender.secret` contains the locally derived shared secret. Decapsulation reads the ciphertext with the recipient’s private key and writes `recipient.secret`. These command roles follow OpenSSL’s documented encapsulation and decapsulation interfaces. [OpenSSL pkeyutl documentation](https://docs.openssl.org/3.5/man1/openssl-pkeyutl/)

The comparison checks whether the two secret files contain identical bytes. A success message gives your reviewer a concise observation to retain without including the secret values in a report.

## 3. Record the observed outcome

The commands were run locally on November 19, 2026 using OpenSSL 4.1.0. The comparison succeeded. The temporary key, ciphertext and secret files were removed when the script exited.

This observation covers one local installation and one round trip. The example provides no measurements of latency, throughput or interoperability between different implementations.

## 4. Define the next compatibility question

For a broader pilot, identify the actual producer and recipient, their implementations and versions, how the recipient’s public key is authenticated, and the protocol that carries the ciphertext. Ask reviewers to approve the parameter set, key lifecycle, required failure cases and evidence-handling process.

Then write a bounded question that those participants can answer, such as whether the selected sender and recipient derive matching secrets under the intended configuration. Keep the observation, environment and open questions together in your [pilot brief](/blog/qcai-editorial-2026-11-03-choose-first-pqc-pilot). Engineering and security reviewers can use that record to decide what to test next.


## Primary references

- [FIPS 203: Module-Lattice-Based Key-Encapsulation Mechanism Standard](https://csrc.nist.gov/pubs/fips/203/final)
- [ML-KEM support and parameters](https://docs.openssl.org/3.5/man7/EVP_KEM-ML-KEM/)
- [pkeyutl encapsulation and decapsulation](https://docs.openssl.org/3.5/man1/openssl-pkeyutl/)

Educational guidance. Confirm system-specific choices with qualified security and engineering reviewers.
