X-Wing
Header: #include <cryptopp/xwing.h> | Namespace: CryptoPP
Since: cryptopp-modern 2026.3.0
Thread Safety: Treat instances as not thread-safe; use separate instances per thread
X-Wing is a hybrid key encapsulation mechanism that combines X25519 (classical ECDH) with ML-KEM-768 (post-quantum KEM). The shared secret is designed to remain secure if either of the two underlying algorithms is broken. X-Wing is specified in an Internet-Draft, not yet an RFC. The wire sizes and combiner are specified in draft-connolly-cfrg-xwing-kem-09.
Key Features
- Hybrid security - Combines classical and post-quantum cryptography
- Defence in depth - Designed to stay secure if either X25519 or ML-KEM-768 is broken
- Fixed design - One concrete combination, with no parameters to choose
- Simple API - Single encapsulate/decapsulate interface
- Combiner - SHA3-256 over both shared secrets, the X25519 ciphertext and public key, and a label
Key Sizes
Wire encoding (per the X-Wing draft):
| Component | Size |
|---|---|
| Decapsulation Key (seed) | 32 bytes |
| Public Key | 1216 bytes (1184 ML-KEM-768 + 32 X25519) |
| Ciphertext | 1120 bytes (1088 ML-KEM-768 + 32 X25519) |
| Shared Secret | 32 bytes |
In-memory expanded key (Crypto++ internal storage):
| Component | Size |
|---|---|
| Private Key | 2464 bytes (2400 ML-KEM-768 + 32 X25519 sk + 32 X25519 pk) |
The library stores the expanded form internally for efficient decapsulation. The 32-byte seed can be used with GenerateFromSeed() for deterministic key generation.
Note: ML-KEM is often represented as a 64-byte seed; X-Wing defines its own 32-byte seed for deriving the hybrid key material.
Security Levels
The X-Wing draft states its target as “128 bits” security (NIST PQC level 1) for the construction as a whole. Its components are:
- X25519: about 128-bit classical security, none against a quantum computer
- ML-KEM-768: NIST security category 3
Quick Example
#include <cryptopp/xwing.h>
#include <cryptopp/osrng.h>
#include <cryptopp/secblock.h>
#include <iostream>
int main() {
using namespace CryptoPP;
AutoSeededRandomPool rng;
// === Recipient generates key pair ===
XWingDecapsulator recipient(rng);
// Extract public key to share with sender
SecByteBlock publicKey(recipient.GetKey().GetPublicKeySize());
recipient.GetKey().GetPublicKey(publicKey.begin());
// === Sender encapsulates with recipient's public key ===
XWingEncapsulator sender(publicKey.begin(), publicKey.size());
SecByteBlock ciphertext(sender.CiphertextLength());
SecByteBlock senderSecret(sender.SharedSecretLength());
sender.Encapsulate(rng, ciphertext.begin(), senderSecret.begin());
// === Recipient decapsulates to get shared secret ===
SecByteBlock recipientSecret(recipient.SharedSecretLength());
recipient.Decapsulate(ciphertext.begin(), recipientSecret.begin());
// Both parties now have the same shared secret
bool same = (senderSecret == recipientSecret);
std::cout << "Shared secrets match: " << (same ? "YES" : "NO") << std::endl;
return same ? 0 : 1;
}Usage Guidelines
Do:
- Use X-Wing where you want a ready-made hybrid of X25519 and ML-KEM-768
- Pass the shared secret through a KDF, with protocol-specific context, to derive session keys
- Keep private keys in
SecByteBlock - Authenticate the exchange; a KEM by itself does not identify the other party
Avoid:
- Treating a successful
Decapsulate()call as proof that the ciphertext was valid - Designing your own hybrid combiner when X-Wing fits
- Assuming a peer speaks X-Wing because it supports “X25519 plus ML-KEM-768”; other hybrids of the same two algorithms are not compatible
When to Use X-Wing
Use X-Wing when:
- You want protection against both classical and quantum attacks on key establishment
- Confidentiality must outlast the arrival of quantum computers
- You want security to hold if lattice cryptography is later broken
- Both ends of the protocol are yours, or the peer implements the same draft
ML-KEM is the right pick when a post-quantum-only design is acceptable or FIPS 203 by itself is required.
X25519 still suits use cases where quantum adversaries are not in the threat model and size matters (32-byte keys versus 1,216 bytes).
Class: XWingEncapsulator
Encapsulate (generate shared secret and ciphertext) using recipient’s public key.
Constants
CRYPTOPP_CONSTANT(PUBLIC_KEYLENGTH = 1216);
CRYPTOPP_CONSTANT(CIPHERTEXT_LENGTH = 1120);
CRYPTOPP_CONSTANT(SHARED_SECRET_LENGTH = 32);Constructors
Default Constructor
XWingEncapsulator();Create an encapsulator with no key. Set the public key before calling Encapsulate().
Constructor with Public Key
XWingEncapsulator(const byte* publicKey, size_t publicKeyLen);Create an encapsulator from recipient’s public key.
Parameters:
publicKey- Pointer to 1216-byte public keypublicKeyLen- Must be 1216; any other length throwsInvalidArgument
Example:
// Receive public key from recipient
SecByteBlock publicKey = /* received from recipient */;
XWingEncapsulator encapsulator(publicKey.begin(), publicKey.size());Methods
Encapsulate
void Encapsulate(RandomNumberGenerator& rng,
byte* ciphertext,
byte* sharedSecret) const;Generate a random shared secret and encapsulate it for the recipient. Throws InvalidArgument if the recipient’s ML-KEM public key fails the FIPS 203 encapsulation-key check or its X25519 public key is a small-order point.
Parameters:
rng- Random number generatorciphertext- Output buffer for ciphertext (1120 bytes)sharedSecret- Output buffer for shared secret (32 bytes)
Details:
- Performs ML-KEM-768 encapsulation to get
ct_Mandss_M - Generates ephemeral X25519 key pair and computes DH with recipient’s X25519 public key to get
ss_X - Combines:
SHA3-256(ss_M || ss_X || ct_X || pk_X || label)
Example:
SecByteBlock ciphertext(encapsulator.CiphertextLength());
SecByteBlock sharedSecret(encapsulator.SharedSecretLength());
encapsulator.Encapsulate(rng, ciphertext.begin(), sharedSecret.begin());
// Send ciphertext to recipient
AccessPublicKey / GetPublicKey
XWingPublicKey& AccessPublicKey();
const XWingPublicKey& GetPublicKey() const;Access the public key for setting or retrieving.
CiphertextLength / SharedSecretLength
size_t CiphertextLength() const; // Returns 1120
size_t SharedSecretLength() const; // Returns 32
Class: XWingDecapsulator
Generate key pairs and decapsulate (recover shared secret from ciphertext).
Constants
CRYPTOPP_CONSTANT(SECRET_KEYLENGTH = 2464);
CRYPTOPP_CONSTANT(PUBLIC_KEYLENGTH = 1216);
CRYPTOPP_CONSTANT(CIPHERTEXT_LENGTH = 1120);
CRYPTOPP_CONSTANT(SHARED_SECRET_LENGTH = 32);Constructors
Default Constructor
XWingDecapsulator();Create a decapsulator with no key. Generate or load a key before calling Decapsulate().
Constructor with RNG (Generate New Key Pair)
XWingDecapsulator(RandomNumberGenerator& rng);Generate a new key pair.
Example:
AutoSeededRandomPool rng;
XWingDecapsulator decapsulator(rng);
// Share public key with senders
Methods
Decapsulate
bool Decapsulate(const byte* ciphertext, byte* sharedSecret) const;Recover the shared secret from a ciphertext.
Parameters:
ciphertext- Ciphertext from encapsulator, exactly 1120 bytes. The function takes no length and reads 1120 bytes, so check the length of a received ciphertext firstsharedSecret- Output buffer for shared secret (32 bytes)
Returns: true on normal completion. An invalid ML-KEM ciphertext goes through ML-KEM’s implicit rejection, so the result is a shared secret the sender does not have, not an error. Detect a bad ciphertext through the protocol, for example a failed AEAD tag under the derived keys.
Details:
- Parses ciphertext into ML-KEM-768 ciphertext and X25519 ephemeral public key
- Performs ML-KEM-768 decapsulation to get
ss_M - Performs X25519 DH with ephemeral public key to get
ss_X - Combines:
SHA3-256(ss_M || ss_X || ct_X || pk_X || label)
Example:
// ciphertext arrived from the sender: check its length before use
if (ciphertext.size() != decapsulator.CiphertextLength())
throw std::runtime_error("X-Wing: wrong ciphertext length");
SecByteBlock sharedSecret(decapsulator.SharedSecretLength());
decapsulator.Decapsulate(ciphertext.begin(), sharedSecret.begin());
// Use sharedSecret with a KDF
AccessPrivateKey / GetPrivateKey / GetKey
XWingPrivateKey& AccessPrivateKey();
const XWingPrivateKey& GetPrivateKey() const;
const XWingPrivateKey& GetKey() const;Access the private key.
Class: XWingPrivateKey
Holds the secret key material for decapsulation.
Methods
GenerateRandom
void GenerateRandom(RandomNumberGenerator& rng, const NameValuePairs& params);Generate a new random key pair.
Example:
XWingPrivateKey privateKey;
privateKey.GenerateRandom(rng, g_nullNameValuePairs);GenerateFromSeed
void GenerateFromSeed(const byte seed[32]);Generate key pair from a 32-byte seed (deterministic).
Example:
byte seed[32] = /* ... */;
XWingPrivateKey privateKey;
privateKey.GenerateFromSeed(seed);GetPublicKey
void GetPublicKey(byte* publicKey) const;Copy the public key (1216 bytes) to the output buffer.
Example:
SecByteBlock publicKey(XWingPrivateKey::PUBLIC_KEYLENGTH);
privateKey.GetPublicKey(publicKey.begin());GetPublicKeySize / GetPrivateKeySize
size_t GetPublicKeySize() const; // Returns 1216
size_t GetPrivateKeySize() const; // Returns 2464
Validate
bool Validate(RandomNumberGenerator& rng, unsigned int level) const;Check the key for validity.
Class: XWingPublicKey
Holds the public key material for encapsulation.
Methods
SetPublicKey
void SetPublicKey(const byte* key, size_t len);Set the public key bytes (must be 1216 bytes).
Example:
XWingPublicKey publicKey;
publicKey.SetPublicKey(keyData, 1216);GetPublicKeyBytePtr / GetPublicKeySize
const byte* GetPublicKeyBytePtr() const;
size_t GetPublicKeySize() const;Get the public key bytes.
GetMLKEMPublicKeyPtr / GetX25519PublicKeyPtr
const byte* GetMLKEMPublicKeyPtr() const; // First 1184 bytes
const byte* GetX25519PublicKeyPtr() const; // Last 32 bytes
Access the individual component public keys.
Validate
bool Validate(RandomNumberGenerator& rng, unsigned int level) const;Check the key for validity.
Key Serialization
Requires: #include <cryptopp/files.h> and #include <cryptopp/filters.h>
Saving Keys
XWingDecapsulator decapsulator(rng);
const auto& privKey = decapsulator.GetKey();
// Get public key to share
SecByteBlock publicKey(privKey.GetPublicKeySize());
privKey.GetPublicKey(publicKey.begin());
// Save public key to file or send to partner
FileSink pubFile("xwing_public.key");
pubFile.Put(publicKey.begin(), publicKey.size());Loading Keys
// Load public key for encapsulation
std::string stored;
FileSource("xwing_public.key", true, new StringSink(stored));
// A short file or one with trailing bytes is not a public key
if (stored.size() != XWING_Constants::PUBLIC_KEY_SIZE)
throw std::runtime_error("xwing_public.key: wrong length");
XWingEncapsulator encapsulator((const byte*)stored.data(), stored.size());Using with a KDF
Derive keys from the shared secret using a KDF with context:
#include <cryptopp/xwing.h>
#include <cryptopp/hkdf.h>
#include <cryptopp/sha3.h>
#include <cryptopp/osrng.h>
#include <cryptopp/secblock.h>
#include <iostream>
int main() {
using namespace CryptoPP;
AutoSeededRandomPool rng;
// Recipient key pair; sender encapsulates to its public key
XWingDecapsulator recipient(rng);
SecByteBlock publicKey(recipient.GetKey().GetPublicKeySize());
recipient.GetKey().GetPublicKey(publicKey.begin());
XWingEncapsulator sender(publicKey.begin(), publicKey.size());
SecByteBlock ciphertext(sender.CiphertextLength());
SecByteBlock senderSecret(sender.SharedSecretLength());
sender.Encapsulate(rng, ciphertext.begin(), senderSecret.begin());
SecByteBlock recipientSecret(recipient.SharedSecretLength());
recipient.Decapsulate(ciphertext.begin(), recipientSecret.begin());
// Each side derives a 32-byte key. The info string names the
// application and the key's purpose.
const byte info[] = "example v1 session key";
SecByteBlock senderKey(32), recipientKey(32);
HKDF<SHA3_256> hkdf;
hkdf.DeriveKey(senderKey.begin(), senderKey.size(),
senderSecret.begin(), senderSecret.size(),
NULLPTR, 0, info, sizeof(info) - 1);
hkdf.DeriveKey(recipientKey.begin(), recipientKey.size(),
recipientSecret.begin(), recipientSecret.size(),
NULLPTR, 0, info, sizeof(info) - 1);
bool same = (senderKey == recipientKey);
std::cout << "Derived keys match: " << (same ? "YES" : "NO") << std::endl;
return same ? 0 : 1;
}X-Wing Combiner Details
The shared secret is computed using the X-Wing combiner (draft-connolly-cfrg-xwing-kem-09, section 5.3):
SharedSecret = SHA3-256(ss_M || ss_X || ct_X || pk_X || label)Where:
ss_M= ML-KEM-768 shared secret (32 bytes)ss_X= X25519 shared secret (32 bytes)ct_X= X25519 ephemeral public key / ciphertext (32 bytes)pk_X= Recipient’s X25519 public key (32 bytes)label= 6-byte X-Wing domain separator:"\./"+"/^\\"(hex:5c 2e 2f 2f 5e 5c)
The label separates X-Wing’s use of SHA3-256 from other uses of the hash.
Comparison with Pure ML-KEM
| Aspect | X-Wing | ML-KEM-768 |
|---|---|---|
| Public Key | 1216 bytes | 1184 bytes |
| Decapsulation Key (seed) | 32 bytes | 64 bytes |
| Ciphertext | 1120 bytes | 1088 bytes |
| Security against a quantum attacker | From ML-KEM-768 | Yes |
| Security if ML-KEM is broken | Classical only, from X25519 | No |
| Security if X25519 is broken | From ML-KEM-768 | Not applicable |
| Specification | Internet-Draft | FIPS 203 |
Use X-Wing when you want defence in depth and control both ends; use ML-KEM where a published standard is required.
Performance
An X-Wing operation is one ML-KEM-768 operation plus X25519 operations and one SHA3-256 call. cryptest b measures it on your own hardware.
Security Considerations
Hybrid Guarantee - X-Wing is designed to be secure if either X25519 or ML-KEM-768 is secure. If only X25519 holds, the security is classical only.
Shared Secret Usage - Prefer deriving keys from the shared secret with a KDF and protocol-specific context.
Domain Separation - The X-Wing combiner includes a fixed label that separates it from other uses of SHA3-256.
Forward Secrecy - For forward secrecy, use an ephemeral recipient key pair per session and discard the private key afterwards.
Binding - The combiner hashes the X25519 ciphertext and public key along with the two shared secrets. The ML-KEM ciphertext and public key are not hashed again; the draft relies on ML-KEM’s own construction to bind them.
Authentication - A KEM gives confidentiality only. Use signatures or another authenticated protocol to know who holds the other key.
Exceptions
InvalidArgument- A public key is not 1216 bytes, the ML-KEM public key fails the FIPS 203 encapsulation-key check, or the recipient’s X25519 public key is a small-order point
Specification Compliance
The wire sizes and combiner are specified in draft-connolly-cfrg-xwing-kem-09:
- Combiner: SHA3-256 with 6-byte domain separation label (appended at end)
- Components: X25519 + ML-KEM-768
- Label:
"\./"+"/^\\"(hex:5c 2e 2f 2f 5e 5c)
The release ships the test vectors from Appendix C of the draft as TestVectors/xwing.txt. The validation suite (cryptest v) checks key generation and encapsulation and decapsulation round trips; it does not run those vectors.
Note: X-Wing is still an Internet-Draft, an individual submission discussed in the IRTF Crypto Forum Research Group. The construction, identifiers and encodings may change before publication; check the current draft.
See Also
- ML-KEM - Underlying post-quantum KEM
- X25519 - Classical key exchange
- PQC Overview - Algorithm comparison and guidance