ratchet: hybrid ML-KEM steps (KDF_RK v2, 2312-byte header, state v2)

Every ratchet keypair is {x25519, ml-kem-768}. On a rotation the stepper
encapsulates to the receiver's current ratchet KEM key; both the X25519 DH
and the ML-KEM secret feed KDF_RK under the new onionwire-ratchet-v2-kdfrk
domain, so a later X25519 break does not open a chain established by a
hybrid step.

Epoch 0 stays classical by construction: the bootstrap root must be a pure
function of the two long-lived prekeys for both-send-first convergence, and
a per-session ss_kem differs for every one-shot Noise dial. The first
rotation (including the bootstrap tie-break rotation) is hybrid, so the
residual HNDL window is exactly the epoch-0 chain. docs/THREAT_MODEL.md
states that window; no full PQ forward secrecy is claimed.

Header 0x02 adds kem_pub[1184] || kem_ct[1088] to the 40-byte classical
header and is used on every frame of a hybrid chain, so a lost first frame
is harmless. A step with no KEM section is rejected (hybrid step required)
and a hybrid step at a classical friend is rejected (unexpected hybrid
step): no classical fallback.

State moves to blob v0.2: fixed 211-byte prefix plus flagged KEM fields.
A v1 blob is dropped and re-bootstrapped (hard cut, like F8); a corrupt v2
or a newer version is an error, not a silent reset.

Tests: hybrid root agreement on both sides, both-send-first convergence
with hybrid keys, no-downgrade, tampered kem_pub/kem_ct fail closed,
MAX_SKIP on a hybrid chain, classical friends still step classically,
state freeze at v0.2 and the node-level hard cut.
This commit is contained in:
rust-dev 2026-10-20 12:54:30 -04:00 • committed by agent
commit b22abd7b2b
No known key found for this signature in database
7 changed files with 1198 additions and 168 deletions

View file

@ -135,13 +135,22 @@ device.
below.
- *Post-compromise security after a DH ratchet step.* An attacker holding a
snapshot of one side's state cannot read the conversation past the next DH
ratchet step: that step feeds a fresh X25519 output into the root, and the
snapshot stops producing chain keys. The window is not zero — it lasts until
the next step — but no attacker can prevent it closing.
- *The ratchet header is not on the wire.* `dh_pub`, `N` and `PN` ride inside
the Noise ciphertext (authenticated as AEAD associated data), so message
counts and ratchet-key changes are not observable, and a tampered header fails
closed.
ratchet step: that step feeds a fresh X25519 output — and, from the first
rotation, a fresh ML-KEM-768 secret — into the root, and the snapshot stops
producing chain keys. The window is not zero — it lasts until the next step —
but no attacker can prevent it closing.
- *Quantum-resistant chains from the first rotation.* Every ratchet step mixes a
fresh ML-KEM-768 secret into `KDF_RK` alongside the X25519 output
(`dh_out || ss_kem`, info `onionwire-ratchet-v2-kdfrk`). A harvest-now
adversary who records a hybrid chain and later breaks X25519 still needs the
ML-KEM-768 secret to walk that chain, which breaking X25519 does not give.
Ratchet keypairs are generated as `{x25519, ml-kem}` with the KEM seed derived
from the X25519 secret, so the KEM half adds no state to protect.
- *The ratchet header is not on the wire.* `dh_pub`, `N`, `PN` and, on hybrid
chains, `kem_pub`/`kem_ct` ride inside the Noise ciphertext (authenticated as
AEAD associated data), so message counts and ratchet-key changes are not
observable, and a tampered header fails closed. A missing KEM section on a
step is a hard `hybrid step required` rejection, never a classical fallback.
**Honest limits.**
@ -151,16 +160,38 @@ device.
oldest-first. A snapshot also carries the current sending and receiving chain
keys, which read the current chain until it advances. This is the price of
out-of-order delivery, and it is per friend.
- *The bootstrap epoch is prekey-bound.* The initial root and both initial
ratchet secrets are HKDF outputs of the X25519 DH between the two long-lived
signed prekeys, and the identity pubkeys that separate them are public. An
attacker who has both peers' prekey statics can therefore reconstruct the
**first** epoch. What that does not buy is a recorded message: every `rat `
frame travels inside a one-shot Noise session whose ephemerals are not derived
from the prekeys, so the prekeys alone do not open stored traffic. From the
first DH ratchet step on, fresh X25519 secrets enter and the prekeys derive
nothing — the bootstrap epoch is the exception, not the rule, and it is the
reason the state is dropped when a peer's prekey changes.
- *Epoch 0 is classical — this is the remaining harvest-now window.* The
bootstrap root and both initial ratchet secrets are HKDF outputs of the
X25519 DH between the two long-lived signed prekeys, and the identity pubkeys
that separate them are public. A per-session KEM secret cannot be folded into
epoch 0: each chat message is its own one-shot Noise session, so `ss_kem`
differs per dial and per peer, and a root bound to it would make the two peers
(or even message 1 and message 2) derive different chains. Epoch 0 is
therefore derived with the classical `KDF_RK`, and the ML-KEM secret enters at
the **first rotation** — two round trips in, at the latest.
What that leaves exposed, precisely:
- The epoch-0 *chain keys* are not post-quantum. A future X25519 break
reconstructs the prekey DH, hence the epoch-0 root and every message key on
that chain. This covers everything sent before the first hybrid step,
including any frame that was encoded on epoch 0 and left in the outbox.
- It does **not** by itself open a recorded v2 transcript, because a v2 friend
wraps every `rat ` frame in the hybrid Noise session (ML-KEM-768 prelude).
The epoch-0 chain keys are a second layer; breaking X25519 alone still leaves
that outer layer closed. The exposure becomes real the moment the rat frame
itself is accessible — a state snapshot taken at epoch 0, an old client that
logged it, or a peer running the classical transport.
- For a **classical (v1) friend** there is no PQ layer anywhere: transport and
every ratchet step are X25519-only, so the whole conversation is
harvest-now-decrypt-later exposed. Hybrid ratchet steps require `kem_pk` on
the roster row, which a legacy invite does not carry.
- From the first hybrid step on, a state snapshot plus a future X25519 break is
not enough: the chain also needs the ML-KEM-768 secret, and the next rotation
replaces it. We do **not** claim full post-quantum forward secrecy, because
epoch 0 is outside that guarantee.
The bootstrap epoch is still the reason state is dropped when a peer's prekey
changes, and an attacker holding both prekey statics can reconstruct epoch 0
directly (no break needed). A recorded message is not opened by the prekeys
alone while the one-shot Noise session an ML-KEM friend used remains intact.
- *No deniability and no non-repudiation claim.* Chat frames are not signed and
both parties derive the same chain keys, so nothing here would let a third
party decide which of the two authored a chat line. Identity is proven per
@ -182,9 +213,12 @@ device.
32 KiB — `docs/PROTOCOL.md`, “Length buckets”), so an observer sees only the
bucket and not the body length. That is all it hides: message timing, retry
cadence, the one-shot-per-message circuits, and the timing of the handshake
are unchanged, and no cover traffic or jitter is generated. Non-chat frames
(`loc `, `prf `, `inv `, `rcp `, `tip `, `fil `) are not padded and still
reveal their own sizes.
are unchanged, and no cover traffic or jitter is generated. The frame header
is not padded: a classical `rat ` frame is 350 bytes at its smallest, while a
hybrid step's 2312-byte ML-KEM header makes its smallest frame 2622 bytes on
the wire. That is a size class, not a message count, and it does not leak the
body length inside the bucket. Non-chat frames (`loc `, `prf `, `inv `,
`rcp `, `tip `, `fil `) are not padded and still reveal their own sizes.
- *The ratchet does not re-open the "no server-side outbox" lock.* The state is
device-local, one row per friend in the encrypted store, and nothing about it
is transmitted. The local retry queue's two accepted consequences (device