DEV Community

Uri
Uri

Posted on

Adding post-quantum encryption (X-Wing) to an open-source AI agent chat network

People put far more sensitive data into AI agent chats than they ever put into normal messaging apps. Private codebases, API keys, financial documents, personal notes, and half-baked ideas all flow through agent conversations every day.

Most of those chats sit in plaintext on vendor servers. Telegram bots cannot use end-to-end encryption at all, and WhatsApp Business API terminates encryption on the business server. Even when an agent chat is encrypted with classical RSA or elliptic curves, any recorded ciphertext is still vulnerable to "harvest now, decrypt later" if someone stores traffic today and breaks classical key exchange later.

In Alice&Bot, an open-source chat network where humans and AI agents across different harnesses share encrypted group chats, we just added hybrid post-quantum encryption using X-Wing (ML-KEM-768 + X25519) across the protocol, mobile/desktop PWA, push notifications, webhooks, and MCP server.

Why X-Wing instead of standalone ML-KEM

X-Wing combines ML-KEM-768 (NIST FIPS 203 lattice KEM) with classical X25519 into one hybrid key encapsulation mechanism. If a mathematical flaw ever shows up in ML-KEM, you still have X25519. If quantum computers break X25519, ML-KEM holds.

From an implementation standpoint it is surprisingly compact. Using @noble/post-quantum:

  • Secret key: 32-byte seed
  • Public key: 1,216 bytes
  • Ciphertext: 1,120 bytes

Every message, file, and interactive canvas in Alice&Bot is already encrypted client-side with a per-conversation AES-256-GCM symmetric key. The asymmetric layer wraps that conversation key once per participant. Switching that key-wrapping layer to X-Wing keeps per-message overhead at zero while upgrading every new conversation key exchange to hybrid post-quantum security.

Additive rollout without breaking existing agents

Hard cutovers break running bots and old devices. Instead of replacing existing RSA keys, each identity now carries an optional publicPqEncryptKey (pq1:<base64>) alongside its legacy publicEncryptKey.

When wrapping a conversation key for a participant (wrapConversationKey in protocol/src/clientApi.ts):

  1. If the participant publishes a valid pq1: public key, encapsulate a shared secret with XWing.encapsulate(pk).
  2. Encrypt the conversation symmetric key with AES-256-GCM, passing the raw 1,120-byte KEM ciphertext as additionalData (AAD) so any tampering with the KEM header fails decryption cleanly.
  3. Prefix the combined kemBytes || iv || ciphertext blob with pq1:.
  4. If the participant only has a legacy RSA public key, fall back to RSA-2048-OAEP.

On the receiving side, unwrapConversationKey checks the pq1: prefix and routes to decryptPq or legacy RSA decryption automatically. That same helper runs everywhere: browser and mobile PWA, the Service Worker that decrypts encrypted push previews (sw.js), Ed25519-signed webhook handlers (handleWebhookUpdate), and the MCP server (npx -y alice-and-bot-mcp).

You can read the full 124-line X-Wing module directly in protocol/src/pqCrypto.ts.

What this does and does not claim

Crypto writeups lose credibility fast when they overstate what they do. Here is the exact scope:

  • What it protects: Conversation key exchange and stored ciphertexts against server compromise and harvest-now-decrypt-later attacks, across humans and bots in 1:1 and multi-agent group chats. Hosted messages and files also auto-delete after 30 days.
  • What it is not: It is static per-conversation key wrapping, not an interactive Double Ratchet protocol with per-message forward secrecy or post-compromise recovery. For stateless webhook bots and multi-device PWA + MCP clients, keeping key management simple and auditable is a deliberate tradeoff.

Try it or read the code

Everything is MIT open source and works in Node, Deno, Bun, Cloudflare Workers, and browsers:

Top comments (0)