Skip to content

Post-quantum content encryption: X-Wing keys throughout auth (protocol 5) - #43

Draft
holmesworcester wants to merge 7 commits into
mainfrom
feat/xwing-kem
Draft

holmesworcester wants to merge 7 commits into
mainfrom
feat/xwing-kem

Conversation

@holmesworcester

@holmesworcester holmesworcester commented Dec 3, 2026 •

Copy link
Copy Markdown

Replaces X25519 with X-Wing (ML-KEM-768 + X25519) everywhere auth encrypts to a public key. Today, someone who copies a team's graph, its lockboxes or a recorded invitation could open them once X25519 can be broken ("harvest now, decrypt later"). After this change they would also have to break ML-KEM-768.

This is a breaking release. The connection protocol moves from 4 to 5. Keysets, graph links, lockboxes and invitation acceptances all change format, and keys, ids, hashes and signatures are now written in base64url instead of base58. There is no migration: existing teams can't be read by this version.

What changes

@localfirst/crypto

  • libsodium.js ^0.7.13 → 0.8.4 (libsodium 1.0.19 → 1.0.22), pinned exactly. @types/libsodium-wrappers-sumo is removed, because 0.8 ships its own typings. Two call sites were adapted to the new typings; their behaviour is unchanged.
  • New kem module. It boxes a payload for an X-Wing public key, as crypto_box_seal does for X25519:
    1. X-Wing encapsulation gives a 32-byte shared secret.
    2. The box key is BLAKE2b keyed with that secret over 'lf/crypto/xwing-box/v1'.
    3. The payload is encrypted with XChaCha20-Poly1305, using a random nonce and the KEM ciphertext as associated data.
    4. The box is msgpack { kemCiphertext, nonce, message }.
    • A box doesn't say who made it.
    • kem.keyPair(seed) derives the X-Wing seed from stretch(seed) under its own context.
  • New signature context INVITATION_ACCEPTANCE.
  • base64url instead of base58 for keys, ciphertexts, signatures, hashes and ids.
    • base58 decoding takes time that grows with the square of the length. For an X-Wing public key that was 1.6 ms, or 28.5 ms without WebAssembly, and adding a member decoded one about 15 times.
    • New base64 helper: URL-safe, no padding, through libsodium. Decoding is strict: it rejects padding, + and /, and unused bits that aren't zero, so each byte string has exactly one encoding. Key ids hash the encoded string, so this keeps one key from having two ids.
    • keyToBytes, key pairs, hash, signatures, randomKey and the string forms of symmetric, asymmetric and kem use it.
    • Renamed: Base58 → Base64, Base58Keypair → Base64Keypair, keypairToBase58 → keypairToBase64, isBase58KeyOfLength → isBase64KeyOfLength.
    • base58 stays for strings people read or type: invitation seeds (randomSeed) and the automerge-repo share id, which is now the base58 of the team id's bytes.

@localfirst/crdx

  • X-Wing keysets. The encryption keypair of every keyset is now an X-Wing keypair. Its public key is 1,216 bytes and its secret key is the 32-byte X-Wing seed.
  • Key ids. A keyset is named by keyId(keys), a 32-byte BLAKE2b hash of its encryption public key. Keyrings are indexed by key id.
  • Graph links.
    • Link bodies are encrypted with the graph keyset's symmetric secretKey.
    • A link is now { encryptedBody, signature, keyId }; senderPublicKey and recipientPublicKey are gone.
    • Authorship was already proven only by the signature.
    • Building a link that members will decrypt now takes the team's secret key. Before, anyone who had seen one link could encrypt a body to the team's public key printed on it. That link was then rejected by the signature check, which still stops forged links; outsiders' bytes now stop at decryption instead.

@localfirst/auth

  • Lockboxes.
    • The payload is a kem box to the recipient's X-Wing public key. The ephemeral encryptionKey field is gone.
    • Recipient and contents manifests name keys by keyId; the manifest version is now 2.
    • lockbox.create needs the recipient's keys and no longer accepts a manifest.
    • lockbox.rotate takes the recipient's keys.
  • Invitations. roleGrantPublicKey becomes roleGrantKeyId. encryptionPublicKey stays a full X-Wing public key, because acceptances are encrypted to it.
  • Invitation acceptance.
    • Before, the X25519 box authenticated its sender. An X-Wing box can't do that, so the sending device (or server) now signs the payload. The signature covers the protocol version, the sending device's id and the ciphertext.
    • No new primitives: the box is the same kem box as lockboxes, and the signature is the same Ed25519 signatures.sign used for link signatures and identity challenges, under its own context.
    • The invitee checks that signature against the signing key the delivered graph registers for that device.
    • The outer payload is { version, senderDeviceId, encryptedAcceptance, signature }.
  • Session keys. Each side encrypts its random seed to the other's X-Wing public key. Each side's session key includes its own seed, which only the authenticated peer can read.
  • Key rotation (disabled in this release).
    • Rotating a role's keys delivers the new keys to the admin role too. The admin role's public key is no longer written on lockboxes, so only an admin can now build a role rotation.
    • Removal methods now refuse a disabled action before they try to rotate.
    • If rotation comes back, a non-admin who changes their keys will need the admin role's public key published somewhere.
  • Sync server team registration (POST /teams).
    • The client sends the serialized graph as base64url. It used to be a Uint8Array inside JSON.stringify, which JSON writes as one object entry per byte, about 11 times the graph's size.
    • The JSON body limit is raised from body-parser's 100 KB default to 10 MB. With X-Wing keys a team's graph grows by about 13 KB per member, so 10 MB fits several hundred members.
    • registerTeam now throws if the server refuses the registration. It used to ignore the response.

Cost

Team sizes and timings were measured with WebAssembly on a Linux x86-64 machine. "Before" is this PR's first commit, which added kem but kept X25519 keysets.

Before After
New team, serialized 4.2 KB 15.0 KB
One lockbox 0.9 KB 3.5 KB
A member's public keyset 0.2 KB 1.8 KB
Team with 20 members, serialized 79 KB 283 KB
Create a team 12–13 ms 11 ms
Add a member 2.7 ms 2.5 ms
Load a 20-member team 7–12 ms 9 ms

With base58, adding a member took 26 ms (about 0.4 s without WebAssembly), nearly all of it decoding keys. Most of the added size is the X-Wing public keys, as text. Each lockbox carries one inside its payload, as part of the keyset it delivers.

Single operations:

With WebAssembly (desktop, Android, servers) Without WebAssembly (node --jitless, the iOS path)
X-Wing key pair 0.17 ms 16 ms
Encrypt (encapsulate) 0.27 ms 48 ms
Decrypt (decapsulate) 0.21 ms 55 ms
X25519 crypto_box_easy, for comparison 0.08 ms 32 ms
base64url decode of an X-Wing public key 0.008 ms 0.18 ms
base58 decode of the same key, for comparison 1.6 ms 28.5 ms

Tests

The whole suite passes on Node 24.13 and Node 20.20: 881 passed and 87 skipped. main gives 816 passed and 87 skipped.

  • kem:
    • The three X-Wing spec vectors (draft-connolly-cfrg-xwing-kem-11, Appendix C). I also checked them locally against @noble/post-quantum.
    • A box built by hand from each spec ciphertext, which pins the format.
    • Round trips.
    • Flipped bits in every field, a spliced KEM ciphertext, malformed input, X25519 boxes and X25519 keys.
    • Seeded keys, pinned by hash.
  • Key ids and links:
    • Keysets are X-Wing and deterministic from a seed.
    • keyId is the same for a keyset, its public half and its public key.
    • Links carry only { encryptedBody, keyId, signature }.
    • A link built by an outsider is rejected both under its own key id and relabelled with the team's.
    • A link relabelled to another key id is rejected.
    • Forged-link tests now build links the new way.
  • Lockboxes:
    • Manifests carry no public keys.
    • A lockbox can't be addressed to a manifest.
    • Changing the recipient key id, the contents key id or the commitment is rejected after a cached open.
    • A new test checks that an identity lockbox naming other keys than the action declares is dropped.
  • Invitation acceptance:
    • The wire-format oracle is updated; it still imports nothing from the production opener.
    • Two new end-to-end tests: a welcome signed by a key other than the named device's, and an honest signature moved onto a different welcome. The invitee rejects both.
    • An impersonator who has a device's signing key but not its encryption key can't derive the session key.
  • Encoding:
    • base64url round trips for every length from 0 to 69 bytes, and strict decoding: padding, + and /, non-zero unused bits, whitespace and impossible lengths are rejected.
    • Every key in a keyset is base64url.
    • Invitation seeds are 16 base58 characters. The share id is the first 12 base58 characters of the team id's bytes. Both tests fail if those strings become base64url.
    • Pinned hashes, signatures and keys were re-encoded and checked to be the same bytes as before. Only the keyset commitment changed, because it hashes the encoded keys.
  • Sync server: an 80-member team registers with a real sync server, and the client then authenticates with it. That graph, as a JSON byte object, would be over 10 MB; the test fails if the old format comes back.
  • Rotation: only an admin can build a role rotation, and the admin role's copy is addressed by key id. A modified peer that skips the protocol gate still has its removals and rotations ignored.
  • Mutation checks, run locally. I disabled each of these in turn, and each made tests fail:
    • the acceptance signature check
    • the recipient key id check in CheckedKeyStore
    • the contents key id check in CheckedKeyStore
    • the key id comparison in lockbox authorization
  • Type check. tsc --noEmit on the auth test files reports 37 errors, all on lines this PR doesn't touch (for example the ADD_LOCKBOXES test actions). Vitest doesn't type-check, so they don't fail CI.

Before Quiet can take this

None of this is in this PR. These changes belong in TryQuiet/quiet:

  1. iOS has no WebAssembly.
    • libsodium.js 0.8's ES module build has no asm.js fallback. Webpack 5.75, the version Quiet's backend uses, picks that build, and under node --jitless its ready rejects.
    • The fix is a resolve.alias from libsodium-wrappers-sumo to the CommonJS entry. I verified it loads and runs X-Wing jitless.
  2. Quiet's native iOS libsodium (packages/backend/platform, pinned to 1.0.19) needs to be rebuilt at 1.0.22 with X-Wing. Otherwise iOS silently falls back to JS.
  3. Quiet's own boxes move to kem: DM key boxes and user-scope encryption in the backend.
  4. Pins. Quiet's desktop pin and the auth submodule need bumping. QSS needs X-Wing server keys and the same libsodium.
  5. base64url. Quiet code that uses the Base58 type, or checks or decodes auth keys, ids or hashes as base58, needs to change. Ids can now contain - and _. Invitation seeds stay base58.
  6. Measure on iPhone. These numbers are from a desktop machine.

Not in this PR

  • Smaller graphs:
    • store X-Wing public keys as bytes instead of base64url text (25% smaller)
    • leave the public key out of lockbox payloads, since it can be re-derived from the 32-byte secret key
  • The quiet-sandbox demo still calls X25519 boxes on keyset keys. Its build is already disabled ("unmaintained, not shipped, not consumed by Quiet").

🤖 Generated with Claude Code

holmes and others added 3 commits December 3, 2026 05:38
… to 0.8.4

- libsodium-wrappers-sumo ^0.7.13 -> 0.8.4 (libsodium 1.0.19 -> 1.0.22),
  pinned exactly. Drop @types/libsodium-wrappers-sumo: 0.8 ships its own
  typings.
- Adapt two call sites to those typings (secretstream pull, base64
  variants). Runtime behaviour is unchanged; a new test pins the stream
  error and passes on main too.
- Add `kem`: boxes made with the X-Wing KEM (ML-KEM-768 + X25519) and
  XChaCha20-Poly1305, random or seeded key pairs, and key pair validation.
  Nothing in auth uses it yet, so no wire format changes.
- Tests: the three X-Wing spec vectors, a box built by hand from a spec
  ciphertext (pins the format), tampering with each field, malformed and
  X25519 input, seeded keys.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every keyset's encryption keypair is now X-Wing (ML-KEM-768 + X25519),
used through `kem`. X25519 boxes are gone from the team protocol, so
nothing recorded today can be opened later by breaking X25519 alone.

- crdx: `createKeyset` makes X-Wing keypairs. A keyset is named by
  `keyId`, a 32-byte BLAKE2b hash of its encryption public key, and
  keyrings are indexed by it. Graph links are encrypted with the graph
  keyset's symmetric key and carry `{ encryptedBody, signature, keyId }`;
  `senderPublicKey` and `recipientPublicKey` are gone. Building a link a
  member will decrypt now takes the team's secret key.
- Lockboxes: the payload is a `kem` box to the recipient's X-Wing public
  key; the ephemeral `encryptionKey` field is gone. Recipient and
  contents manifests name keys by `keyId` (manifest version 2), and
  `lockbox.create` needs the recipient's keys, not a manifest.
  `lockbox.rotate` takes the recipient's keys.
- Invitations: `roleGrantPublicKey` becomes `roleGrantKeyId`;
  `encryptionPublicKey` stays a full X-Wing public key.
- Invitation acceptance: encrypted with `kem` to the invitation key, and
  signed by the sending device (or server) under the new
  INVITATION_ACCEPTANCE context. The invitee verifies the signature
  under the key the delivered graph registers for that device. The
  outer payload is `{ version, senderDeviceId, encryptedAcceptance,
  signature }`.
- Connection session keys: each side encrypts its seed to the other's
  X-Wing public key.
- Key rotation (disabled in this release): each role's new keys go to
  the admin role, whose public key only admins now hold, so only an
  admin can rotate role keys. Removal producers refuse disabled actions
  before rotating.
- CONNECTION_PROTOCOL_VERSION 4 -> 5, so older peers are refused at
  REQUEST_IDENTITY.
- Sync server: JSON body limit raised to 10 MB; a new team's graph is
  over the 100 KB default with X-Wing keys.

Breaking: no migration. Existing teams, graphs and lockboxes can't be
read by this version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An X-Wing public key is 1,660 base58 characters, and base58 decoding is
quadratic: about 1 ms with WebAssembly and about 28 ms without it (iOS).
The same public keys were decoded again on every keyset validation and
lockbox, which made adding a member ~10x slower than before X-Wing
(26 ms vs 2.7 ms). keyToBytes now keeps recent decodings of 128 to 2,048
character strings (public keys and short ciphertexts; secret keys are at
most 88 characters) and returns a copy each time. Adding a member: 9 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@holmesworcester holmesworcester changed the title Add X-Wing (post-quantum) boxes to @localfirst/crypto; bump libsodium to 0.8.4 Post-quantum content encryption: X-Wing keys throughout auth (protocol 5) Dec 3, 2026
holmes and others added 4 commits December 3, 2026 06:26
This reverts commit e3f4c17. Adding a
member in 26 ms instead of 9 ms is acceptable; the cache is not worth
its code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
base58 decoding takes time that grows with the square of the length.
For an X-Wing public key (1,216 bytes) it took 1.6 ms, or 28.5 ms
without WebAssembly (iOS), and adding a member decoded one about 15
times. base64url, through libsodium, takes 8 µs (0.18 ms without
WebAssembly). Adding a member drops from 26 ms to 2.5 ms, about what it
took before X-Wing.

- @localfirst/crypto: new `base64` (URL-safe, no padding, strict:
  padding, `+`/`/` and non-zero unused bits are rejected, so each byte
  string has one encoding). `keyToBytes`, key pairs, `hash`, signatures
  and the string forms of `symmetric`, `asymmetric` and `kem` use it;
  `randomKey` returns base64url. Renamed: `Base58` -> `Base64`,
  `Base58Keypair` -> `Base64Keypair`, `keypairToBase58` ->
  `keypairToBase64`, `isBase58KeyOfLength` -> `isBase64KeyOfLength`.
- base58 stays for strings people read or type: invitation seeds
  (`randomSeed`) and automerge-repo share ids, now the base58 of the
  team id's bytes.
- auth validators check ids, nonces and signatures with `base64.detect`.

Breaking: every key, id, hash and signature changes encoding.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- keyId hashes the public key on every call. Hashing a base64url key is
  cheap, and adding a member still takes 2.6 ms without the cache.
- The malformed-key tests in lockbox.test and keypairValidation.test
  still built their bad keys with base58. Those strings mostly failed to
  decode as base64url, so the tests passed without reaching the length
  and key-pair checks they name; and about 1 time in 8, a base58 string
  of 31 bytes decoded as a valid 32-byte key, which failed lockbox.test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Team registration (`POST /teams`) sent the serialized graph through
JSON.stringify as a Uint8Array, which JSON writes as an object with one
entry per byte: about 11 times the graph's size. With X-Wing keys a
20-member team's request was about 3.1 MB, and a team of about 65
members was over the server's 10 MB limit. As base64url the request is
about 1.33 times the graph, so 10 MB fits several hundred members.

- The client sends `serializedGraph` as base64url; the server decodes it
  with `base64.decode`. `objectToUint8Array` is gone, and
  `@localfirst/crypto` moves to the server's dependencies.
- `registerTeam` now throws if the server refuses the registration. It
  used to ignore the response, so a refused registration only showed up
  as the client never authenticating.

Breaking: a client and a sync server must both use this format.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant