docs: ratchet wire spec + forward-secrecy/PCS threat-model claims #89

Merged
sirius merged 1 commit from wt/t_29a71a55 into main 2026-09-16 23:46:00 +00:00
Owner

What this is

Docs-only: docs/PROTOCOL.md, docs/THREAT_MODEL.md, README.md. No src/,
tests/, Cargo.toml, crates/, android/ or .forgejo/ change.

Base is main (the parent cards, PR #87 wt/t_dcafd1c8 and PR #88
wt/t_95ff7563, are both merged onto it — this card was stacked and no longer
needs the parent branch base).

PROTOCOL.md — ## Ratchet (v1 chat layer)

New section, written so a second implementation can interoperate from the text
alone:

  • byte-exact frame: rat prefix, version 0x01, 40-byte header
    dh_pub[32] || N u32 BE || PN u32 BE, 12-byte random nonce,
    ChaCha20-Poly1305 ciphertext + tag; min 73 bytes; the 40 header bytes are the
    AEAD associated data.
  • the header is inside the Noise transport message, with the one-sentence
    reason the plaintext-on-the-wire header was rejected for this architecture
    (the Noise session already hides the frame; a plaintext header would only
    hand an observer counters and ratchet-key changes).
  • KDFs byte-exact: HKDF-SHA256(salt = root, ikm = dh_out, info = "onionwire-ratchet-v1-kdfrk") -> 64 bytes = new_root || chain_key;
    KDF_CK(ck) = HMAC-SHA256(ck, 0x01) || HMAC-SHA256(ck, 0x02); the
    two-call epoch_chains rule with lo/hi by public-key order.
  • bootstrap: the derivation the implementation actually chose (prekey X25519 DH
    -> HKDF salt onionwire-ratchet-v1-bootstrap, root, and per-side
    ratchet-key || pubkey), plus the convergence argument and the bootstrap
    tie-break (lower ratchet key rotates, and only after it has received).
  • receive rules, the full DH ratchet step (retire via PN, receive half,
    sending half), MAX_SKIP = 2000, MAX_SKIPPED_KEYS = 2000 oldest-inserted
    eviction, replay fails closed, bounds validated before any mutation.
  • ratchet_sessions at rest in the SQLCipher store, device-local, never
    transmitted.
  • what is not ratcheted (loc , prf , inv , rcp , tip , png ,
    fil and file chunks), and the prekey-change reset paragraph.
  • prefix table gained the rat row, classified before the [a-z]{3}
    catch-all; Versioning notes the rat version byte is its own break.

THREAT_MODEL.md

New section "Chat is ratcheted on top of the one-shot Noise session" with
gains (per-chain FS, PCS after a DH step, header not on the wire) and honest
limits (skipped-key window, prekey-bound bootstrap epoch, no deniability and no
non-repudiation, no one-time prekeys, compromised endpoint out of scope, no
metadata gain beyond Noise, and the no-server-side-outbox lock is not
re-opened). The at-rest section now lists the ratchet session blobs with the
rest of the ciphertext.

README.md

Operator-visible section "Chat ratchet (per friend, on device)": chat is
ratcheted inside Noise; only chat is; /wipe keeps the ratchet state while
/wipe-all removes it; there is no reset command in v0.2.1 and a peer prekey
change (refreshed invite, F3) is the only recovery; an unauthenticated
inbound frame is dropped with a stderr line and no TUI marker, so a desync is
diagnosed from the terminal.

Deviation from the card brief, named

  1. Bootstrap. The brief assumed the ratchet initial key would be carried by
    or derived from the session handshake. It is not: the Noise handshake is
    frozen and session.rs is off limits, so the implementation derives the
    bootstrap epoch from the two signed prekeys. The doc documents the
    derivation, not the brief's assumption.
  2. Prekey compromise. The brief's limit said compromise of both prekey
    statics does not reveal message keys. That is true for stored traffic (the
    outer one-shot Noise ephemerals are independent) and true from the first DH
    step on, but the bootstrap epoch's root and both initial ratchet secrets
    are HKDF outputs of that same prekey DH with public pubkeys mixed in, so an
    attacker holding both prekey secrets can reconstruct the first epoch. The
    doc says exactly that instead of the stronger claim.

Known bug found while verifying (out of scope here)

A 4-byte frame rat panics Frame::parse (src/ratchet.rs:693,
frame[FRAME_PREFIX.len()] on a 4-byte slice), and it is reachable from a peer
inside an authenticated Noise session. Reproduced out of tree with a temporary
test (deleted again, not part of this PR):

thread 'four_byte_prefix_frame_does_not_panic' panicked at src/ratchet.rs:693:15:
index out of bounds: the len is 4 but the index is 4

Filed as a follow-up card rather than fixed here, because this card forbids
src/. The spec keeps the MUST-reject wording that the fix should satisfy.

## What this is Docs-only: `docs/PROTOCOL.md`, `docs/THREAT_MODEL.md`, `README.md`. No `src/`, `tests/`, `Cargo.toml`, `crates/`, `android/` or `.forgejo/` change. Base is `main` (the parent cards, PR #87 `wt/t_dcafd1c8` and PR #88 `wt/t_95ff7563`, are both merged onto it — this card was stacked and no longer needs the parent branch base). ## PROTOCOL.md — `## Ratchet (v1 chat layer)` New section, written so a second implementation can interoperate from the text alone: - byte-exact frame: `rat ` prefix, version `0x01`, 40-byte header `dh_pub[32] || N u32 BE || PN u32 BE`, 12-byte random nonce, ChaCha20-Poly1305 ciphertext + tag; min 73 bytes; the 40 header bytes are the AEAD associated data. - the header is **inside** the Noise transport message, with the one-sentence reason the plaintext-on-the-wire header was rejected for this architecture (the Noise session already hides the frame; a plaintext header would only hand an observer counters and ratchet-key changes). - KDFs byte-exact: `HKDF-SHA256(salt = root, ikm = dh_out, info = "onionwire-ratchet-v1-kdfrk") -> 64 bytes = new_root || chain_key`; `KDF_CK(ck) = HMAC-SHA256(ck, 0x01) || HMAC-SHA256(ck, 0x02)`; the two-call `epoch_chains` rule with lo/hi by public-key order. - bootstrap: the derivation the implementation actually chose (prekey X25519 DH -> HKDF salt `onionwire-ratchet-v1-bootstrap`, `root`, and per-side `ratchet-key || pubkey`), plus the convergence argument and the bootstrap tie-break (lower ratchet key rotates, and only after it has received). - receive rules, the full DH ratchet step (retire via `PN`, receive half, sending half), `MAX_SKIP = 2000`, `MAX_SKIPPED_KEYS = 2000` oldest-inserted eviction, replay fails closed, bounds validated before any mutation. - `ratchet_sessions` at rest in the SQLCipher store, device-local, never transmitted. - what is **not** ratcheted (`loc `, `prf `, `inv `, `rcp `, `tip `, `png `, `fil ` and file chunks), and the prekey-change reset paragraph. - prefix table gained the `rat ` row, classified before the `[a-z]{3} ` catch-all; `Versioning` notes the `rat ` version byte is its own break. ## THREAT_MODEL.md New section "Chat is ratcheted on top of the one-shot Noise session" with gains (per-chain FS, PCS after a DH step, header not on the wire) and honest limits (skipped-key window, prekey-bound bootstrap epoch, no deniability and no non-repudiation, no one-time prekeys, compromised endpoint out of scope, no metadata gain beyond Noise, and the no-server-side-outbox lock is not re-opened). The at-rest section now lists the ratchet session blobs with the rest of the ciphertext. ## README.md Operator-visible section "Chat ratchet (per friend, on device)": chat is ratcheted inside Noise; only chat is; `/wipe` keeps the ratchet state while `/wipe-all` removes it; there is no reset command in v0.2.1 and a peer prekey change (refreshed invite, `F3`) is the only recovery; an unauthenticated inbound frame is dropped with a stderr line and no TUI marker, so a desync is diagnosed from the terminal. ## Deviation from the card brief, named 1. **Bootstrap.** The brief assumed the ratchet initial key would be carried by or derived from the session handshake. It is not: the Noise handshake is frozen and `session.rs` is off limits, so the implementation *derives* the bootstrap epoch from the two signed prekeys. The doc documents the derivation, not the brief's assumption. 2. **Prekey compromise.** The brief's limit said compromise of both prekey statics does not reveal message keys. That is true for stored traffic (the outer one-shot Noise ephemerals are independent) and true from the first DH step on, but the *bootstrap* epoch's root and both initial ratchet secrets are HKDF outputs of that same prekey DH with public pubkeys mixed in, so an attacker holding both prekey secrets can reconstruct the first epoch. The doc says exactly that instead of the stronger claim. ## Known bug found while verifying (out of scope here) A 4-byte frame `rat ` panics `Frame::parse` (`src/ratchet.rs:693`, `frame[FRAME_PREFIX.len()]` on a 4-byte slice), and it is reachable from a peer inside an authenticated Noise session. Reproduced out of tree with a temporary test (deleted again, not part of this PR): ``` thread 'four_byte_prefix_frame_does_not_panic' panicked at src/ratchet.rs:693:15: index out of bounds: the len is 4 but the index is 4 ``` Filed as a follow-up card rather than fixed here, because this card forbids `src/`. The spec keeps the MUST-reject wording that the fix should satisfy.
PROTOCOL.md gains a 'Ratchet (v1 chat layer)' section written so a second
implementation can interoperate from the text alone: byte-exact frame layout
(rat , 0x01, dh_pub||N||PN as header and AAD, 12-byte nonce,
ChaCha20-Poly1305), the KDF_RK/KDF_CK constructions and the role-separated
epoch rule, the derived bootstrap epoch, the receive and DH-step rules, the
MAX_SKIP/MAX_SKIPPED_KEYS bounds with the replay rule, the SQLCipher
ratchet_sessions row, and what stays unratcheted.

THREAT_MODEL.md gains the corresponding gains (per-chain forward secrecy, PCS
after a DH step, header not on the wire) and honest limits (skipped-key
window, the prekey-bound bootstrap epoch, no deniability, no one-time
prekeys, compromised endpoint out of scope, no metadata gain, the no-server-
outbox lock untouched) plus the amended transport wording.

README.md documents the operator-visible consequence: no reset command yet,
prekey change / invite re-paste is the only recovery, /wipe keeps the state,
and a desync is currently stderr-only.
sirius merged commit cf77116143 into main 2026-09-16 23:46:00 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
sirius/onionwire!89
No description provided.