diff --git a/docs/design/pq-bootstrap-epoch.md b/docs/design/pq-bootstrap-epoch.md new file mode 100644 index 0000000..602dc93 --- /dev/null +++ b/docs/design/pq-bootstrap-epoch.md @@ -0,0 +1,301 @@ +# ADR: a post-quantum bootstrap epoch (epoch 0) + +**Status:** proposed. This is a decision record: it recommends a direction and +changes no code. The protocol and threat-model wording it depends on is already +on `main`; this file does not restate it. + +**Decision:** accept the window (Option C). Revisit when the one-shot session +model changes, or when a one-time prekey can reach the peer without a new wire +field. + +Related: [PROTOCOL.md](../PROTOCOL.md) ("Hybrid ratchet steps, and why epoch 0 +stays classical") and [THREAT_MODEL.md](../THREAT_MODEL.md) ("Epoch 0 is +classical — this is the remaining harvest-now window"). + +## 1. Context: the invariant, stated exactly + +Epoch 0 is the ratchet state a friend pair starts from. It is derived, not +negotiated: + +1. **Each chat message is its own one-shot Noise session.** `Node::send` encodes + exactly one `rat ` frame and hands those bytes to the outbox + (`src/node.rs:1905`, `src/outbox.rs:168`). Sending again opens a new Noise + session to the same onion. There is no connection, stream, or session object + that outlives one application frame. +2. **The Noise handshake carries no application payload.** It is frozen at + `Noise_IK_25519_ChaChaPoly_BLAKE2s`, so no field can hand the peer an initial + ratchet key (`src/node.rs:2316`). +3. **Either peer may send first.** Two peers can each build a sending chain + before either has received anything. The resolution is per-direction chains + over the *unordered pair* of ratchet public keys, so authorship of a direction + is decided by key order rather than by who spoke first + (`src/ratchet.rs:94-116`). +4. **Therefore epoch 0 must be a pure function of material both sides already + have**: the X25519 DH of the two long-lived signed prekeys, with each side's + initial ratchet keypair seeded by + `HKDF(info = "ratchet-key" || pubkey)` + (`src/node.rs:2325-2370`). No wire field, no round trip, no per-session + secret. This is the invariant the three options are measured against. + +The consequence, already documented and not re-argued here: epoch 0 is derived +with the classical `KDF_RK` and the ML-KEM secret enters at the **first +rotation** (`src/ratchet.rs:66-73`, `docs/PROTOCOL.md:683`). Every frame sent +before that point is X25519-only at the ratchet layer. + +Two facts bound how much that costs, and they matter for the ranking: + +- **For a v2 friend the `rat ` frame rides inside a hybrid Noise session.** A + recorded transcript is not opened by an X25519 break alone; the ML-KEM-768 + prelude has to be broken too, and it is per-dial. Epoch 0 becomes the exposed + layer when the rat frame is separated from the session that carried it — a + state snapshot, an outbox row at rest, a logging client, or a classical peer + (`docs/THREAT_MODEL.md:163-192`). +- **The ML-KEM half of a ratchet keypair is derived from the X25519 secret** + (`src/ratchet.rs:56-63`, `src/ratchet.rs:326`). It adds no independent secret + to epoch 0, so "every keypair is hybrid" does not mean the bootstrap epoch is. + +## 2. Options + +Each subsection has the same shape: mechanism, wire cost, convergence, effect on +the outbox and stored/replayed frames, test surface, and cost in slice-days. One +slice-day is one focused working day for one agent in this repo, including tests +and the doc updates the change would force. + +### Option A — one-time prekey batch in the invite + +**Mechanism.** The invite carries N ML-KEM-768 one-time prekeys (OPKs) in +addition to the long-lived `kem=`. The peer consumes one per dial or per epoch, +depending on which layer the OPK is meant to protect — that ambiguity is the +first finding below. The threat model already lists "No one-time prekeys" as an +honest limit (`docs/THREAT_MODEL.md:199-203`), so this is the option that would +close a named gap; the size and convergence findings are what kill it. + +**What the invite gains.** A new version, necessarily. The v2 signature domain is +`onionwire-invite-v2 || 0x00 || k || 0x00 || o || 0x00 || spk || 0x00 || kem` +(`src/qr.rs:38`), and `parse_fields` rejects any unrecognised field with +`unknown field` (`src/qr.rs`, `parse_fields`). So an `opk=` field on a v2 string +is a decode error, not an ignored extra: the batch is `onionwire:v3`, a new +signature domain, and a new field in the signed message. + +**Size, numerically.** A v2 invite is 1930 characters for a 56-character v3 +onion. Each ML-KEM-768 OPK is 1184 bytes, which is 1579 base64url characters +plus a 5-character `:opk=` prefix — **1584 characters each**. The decoder caps +input at `MAX_INVITE = 4096` (`src/qr.rs:30`), and the TUI renders the invite +through `qrcode` 0.14.1 at its default `EcLevel::M` (`src/tui.rs:2818`), whose +byte capacity is **2331 characters** (measured against the pinned crate, not +recalled). Therefore: + +| Invite | chars | fits `MAX_INVITE` | fits one QR at M | +|---|---|---|---| +| v2 today | 1930 | yes | yes | +| v3, 1 OPK | 3514 | yes | **no** | +| v3, 2 OPKs | 5098 | **no** | no | +| v3, 4 OPKs | 8266 | no | no | + +A batch that could survive more than one use cannot be carried by this invite at +all: two OPKs already exceed the decoder's own cap, and one already exceeds what +the QR path can render. The invite would have to move to a file/NFC transfer, or +the QR would have to be dropped, before the batch is even representable. + +**What is consumed, and when.** This is where the option loses its premise. If +the OPK is consumed at the **transport** layer (one per Noise dial), it is one +per message: a retry after a queued send is a new dial and burns another, and a +batch of N is exhausted after N dials with no replenishment path — the peer has +the invite, not a directory, so the next invite paste is the only refill. If it +is consumed at the **ratchet** layer, it is one per epoch, and the secret has to +reach the peer: ML-KEM encapsulation is randomised, so the sender must transmit +a 1088-byte ciphertext. That is exactly Option B's wire field. **A does not fix +epoch 0 without B**; on its own it only improves the transport handshake, which +already carries a per-dial ML-KEM secret in the v2 prelude. + +**Unused prekeys.** They are decapsulation keys stored for the life of the +install, with no expiry and no ratchet benefit. A state snapshot hands them over, +so they become an additional long-lived secret rather than a forward-secrecy +win. The roster already drops state when a peer's prekey changes +(`src/node.rs:2385-2395`); a batch would need its own consumption cursor, its own +versioned blob, and a rule for when a partly-consumed batch is abandoned. + +**Wire cost.** Invite only, if the OPK is used at the transport layer (+1584 +characters per prekey, and the QR stops working at one). Invite *and* frame +(+1088 header bytes per epoch-0 frame, repeated until the first rotation) if used +at the ratchet layer — at which point it is B. + +**Convergence.** At the transport layer, unchanged: each Noise session's two +endpoints both know that session's `ss_kem`, so per-dial secrets are fine there. +At the ratchet layer, A *breaks* convergence on its own: both peers encapsulate +to the same OPK, get different secrets (fresh randomness on each side), and +derive different epoch-0 roots. Only one direction's ciphertext can be the +authoritative one, and carrying it is a wire field. + +**Outbox and replayed frames.** A stored `rat ` frame is replayed byte-for-byte +over a fresh Noise session (`src/outbox.rs:161-175`; the retry loop must not +advance the sending chain twice, so the frame is encoded once). Transport-layer +OPK consumption therefore drains the batch on retries, not on delivered +messages. Ratchet-layer consumption requires the receiver to retain the OPK +private key until every queued row encrypted under it is delivered or +terminally failed — unbounded retention, which contradicts "one-time". + +**Test surface.** Invite v3 round-trip and signature domain; rejection of `opk=` +on v2; batch exhaustion and the fallback to `kem=`; QR capacity (the invite +render path currently has no graceful path for "too long for one symbol"); +retry-burns-a-prekey; a both-send-first test showing the two roots diverge if the +ratchet layer is attempted without a field. + +**Cost (estimate).** 5–7 slice-days for the invite/transport version of A (invite v3, +signed field, store pool, consumption cursor, tests, docs). It buys a +transport-layer improvement that is already covered by the per-dial prelude, and +leaves epoch 0 exactly as it is. The ratchet-layer variant is B plus this cost. + +### Option B — per-session epoch derivation with an extra wire field + +**Mechanism.** Add material to the ratchet header and derive the epoch from it. +The natural shape is a new version byte (`0x03` classical + field, `0x04` hybrid ++ field) because `0x01` and `0x02` are already taken and the header is the AEAD +associated data, so its exact shape is load-bearing +(`src/ratchet.rs:1-40`, `src/ratchet.rs:585-601`). The field would be a 32-byte +salt, or a 1088-byte ML-KEM ciphertext if it carries a prekey secret. + +**Wire cost.** 32 bytes per frame (salt) or 1088 bytes per frame (ciphertext), +repeated on every frame of the epoch-0 chain because the first frame can be lost. +The classical header goes 40 → 72 bytes, the hybrid header 2312 → 3400, and the +documented minimum frame sizes (350 classical, 2622 hybrid, +`docs/THREAT_MODEL.md:217-220`) all move. The minimum-size tests and the padding +ladder's interaction with the floor would have to be re-derived, not just +re-numbered. + +**Convergence.** This is the option's fatal case. The field arrives *in* the +frame, so neither side has the other's field before its first send. Two designs +are available and neither restores the invariant: + +- *Sender-chosen salt, per direction.* Each side derives its own sending chain + from its own salt. That works for the first message in each direction, but the + epoch's shared root advance now depends on two salts, and the side that rotates + first — the tie-break side rotates on its first *decrypt* + (`src/ratchet.rs:624-630`) — may have seen only one of them. The root is no + longer a pure function of the pair, so the lockstep argument in + `src/ratchet.rs:100-116` no longer holds and the peers can diverge at the + first rotation. +- *Salt derived from the Noise session secret.* Then the two peers' first sends + ride different sessions with different secrets, and message 2 rides a third + session, so a stored and retried frame cannot be re-derived at all — see the + outbox row. + +A public salt in the header adds no secrecy, because it is transmitted in clear +inside the Noise transport. It would only bind the chain to a random value, +which is not the threat. So B is either a no-op against harvest-now, or it is a +salt that breaks convergence, or it is a KEM ciphertext that reintroduces A's +consumption and retention problems. + +**Outbox and replayed frames.** A salt lives in the stored frame, so a verbatim +replay is consistent. A ciphertext likewise survives the replay, but the +receiver must still hold the matching private key when the retry lands — so the +"one-time" key is retained until the outbox row is delivered or fails, and +`DEFAULT_MAX_ATTEMPTS` (8) with a 30-minute backoff cap means that retention is +measured in hours at minimum. This is the same retention conflict as A, now on +the frame path. + +**Test surface.** New version bytes and header layout; AAD coverage of the new +field; the both-send-first matrix re-derived (the existing convergence tests in +`src/ratchet.rs` and `tests/ratchet_wire.rs` assert the current argument, not just +the happy path); `STATE_VERSION` 2 → 3 with a v2 blob dropped exactly like v1 +(`src/ratchet.rs:163`, `src/node.rs:2385-2395`); cross-version interop, since a +friend with a v2 invite has no field to send — downgrade is decided by the +roster, never by what arrives on the wire, so these are two different epoch +shapes that must never be negotiated dynamically; padding floor and minimum-frame +constants. + +**Cost (estimate).** 8–12 slice-days, and it leaves the ambiguous root-advance rule open +(that is the item in "Not solved by this doc"). + +### Option C — accept the window, keep the honesty bounds + +**Mechanism.** No change. Epoch 0 stays prekey-derived and classical; the KEM +secret enters at the first rotation; the docs keep saying so in the three places +they already do (`src/ratchet.rs:66-73`, `docs/PROTOCOL.md:683-720`, +`docs/THREAT_MODEL.md:163-192`). + +**What has to be true for this to stay acceptable.** These are the conditions the +decision rests on; if one stops holding, the trigger in §3 fires. + +- *The first rotation is prompt.* It happens on the first inbound frame after + `established`, and the lower-key side also rotates immediately on that decrypt + (`src/ratchet.rs:624-630`). A peer that sends and never receives stays on + epoch 0 by definition; the window closes with the first reply, not with time. +- *Nothing claims otherwise.* "Post-quantum transport, classical bootstrap + epoch" is the ceiling. `docs/THREAT_MODEL.md:25` and + `docs/THREAT_MODEL.md:163` are the sentences that must not drift, and + README/marketing copy inherits them. +- *A state snapshot is already the whole game.* The ratchet state lives in the + same SQLCipher file as the plaintext history, and `docs/THREAT_MODEL.md:205-209` + says a snapshot carries both chain keys, the skipped keys, and the history. + Epoch 0 is therefore not the weakest link at rest: an attacker with the store + reads the messages, not the chain. The epoch-0 exposure is about frames outside + the store — captured traffic and delivered outbox rows — not about the device. +- *Outbox exposure stays bounded by delivery.* A row encoded on epoch 0 carries + epoch-0 keys until it is delivered or terminally fails + (`docs/THREAT_MODEL.md:176`). The queue is local, bounded by + `DEFAULT_MAX_ATTEMPTS`, and never handed to a third party, so this is the same + window as a live epoch-0 message, not a new one. +- *The roster still decides downgrade.* A v1 friend has no PQ layer anywhere; + that is a roster fact, not a wire negotiation + (`docs/THREAT_MODEL.md:183-187`). + +**Wire cost.** Zero. **Convergence.** Unchanged, and covered by the existing +both-send-first and cross-peer tests. **Outbox and stored frames.** Unchanged; +the verbatim-replay property stays exact. **Test surface.** No new tests; the +existing ones already pin the invariant. **Cost.** 0 slice-days. The standing +cost is one honest sentence in the docs, which is already paid. + +## 3. Comparison and decision + +| | A — OPK batch | B — epoch field | C — accept | +|---|---|---|---| +| Wire cost | +1584 chars/invite (QR dies at 1) | +32 or +1088 B/frame, new versions | 0 | +| Fixes epoch 0? | no (transport only; needs B for ratchet) | only as a public salt, which is not the threat | n/a | +| Convergence | breaks it at the ratchet layer | ambiguous root advance; breaks lockstep | unchanged | +| Outbox / replay | drains batch on retries; unbounded OPK retention | verbatim replay OK, retention unbounded | unchanged, bounded by delivery | +| Test surface | invite v3, pool, QR capacity, divergence | header, state v3, both-send-first matrix, floors | none | +| Slice-days (est.) | 5–7 (and buys no epoch-0 fix) | 8–12 | 0 | + +**Decision: C.** A does not make epoch 0 post-quantum — it adds invite mass and a +consumption problem, and the part that would touch the ratchet is B's wire field, +which cannot converge under both-send-first without an ambiguous root rule. C +costs nothing, keeps the one property the design actually relies on (a root that +is a pure function of the prekey pair), and the exposure it accepts is already +shadowed for v2 friends by the per-dial hybrid Noise session. + +**Revisit triggers**, in priority order. Any one of these reopens this ADR; none +is a reason to start work now: + +1. **The one-shot session model changes.** If chat ever gets a session that + outlives one frame, the blocker in §1 disappears and epoch 0 can take a + per-session secret. This is the real trigger. +2. **A delivery path for a prekey ciphertext exists without a new field.** E.g. + the handshake gains an application payload. Then A becomes viable at the + ratchet layer. +3. **The invite stops being QR-first.** If invites move to file/NFC and the + `MAX_INVITE` cap can move with them, a batch is at least representable — still + subject to the replenishment problem, so this alone is not enough. +4. **Any doc or release note claims post-quantum messaging.** That is a + documentation failure to fix immediately, not a reason to ship A or B. + +**Consequences.** No code changes. The next card on this topic is not A or B; if +one is proposed anyway, it must answer B's root-advance question and the QR +capacity finding in A before it can be scoped. The docs on `main` continue to be +the authority on what is and is not post-quantum. + +## 4. Not solved by this doc + +- **In-band revocation / unfriend.** There is no revoke frame; the roster drops + a friend locally, and the peer keeps the current onion until F4 + (`docs/THREAT_MODEL.md:19`). +- **Prekey or KEM rotation without a new invite.** F8 rotates the signed prekey + and the ML-KEM install key, and every friend must paste the new invite; there + is no push path (`docs/THREAT_MODEL.md:21-24`). Option A makes this worse, not + better: a batch has no refill channel, so it inherits the same hard cut. +- **Option B's open question.** Which input advances the epoch root when two + per-direction salts exist and the first rotator has seen only one of them. A + future B proposal has to answer that before it can be implemented; this doc + does not. +- **Guard discovery.** The long-lived v3 onion and Arti's vanguards-lite are a + separate opsec gap and are not addressed by any of the three options.