ratchet: Double Ratchet state machine (symmetric + DH ratchet, bounded skipped keys) #85

Merged
sirius merged 1 commit from wt/t_85aaf5a6 into main 2026-09-16 22:17:11 +00:00
Owner

Card: t_85aaf5a6 (onionwire core: Double Ratchet module).

New src/ratchet.rs (wire-independent) and tests/ratchet.rs. No wiring into node.rs — that is the next card.

Convergent initiation (the design gate)

An epoch is the unordered pair of the two current ratchet public keys. Its two chain keys come from two KDF_RK calls on that pair's shared secret: the first yields the chain for the "lo to hi" direction (lower public key to higher), the second — on the first call's root — the chain for "hi to lo", and the second root is the epoch's new root. Which side authored which direction is decided by public-key order, never by who spoke first, so both peers derive both chains for the same pair and each sends on its own direction immediately.

Invariant: the root and the chain keys are a pure function of the sequence of ratchet-key pairs, and both peers walk that sequence in the same causal order — a DH ratchet step happens only on receiving a not-yet-adopted ratchet key, so at most one rotation is ever in flight. The bootstrap epoch (both keys known from the session bootstrap) uses one deterministic tie-break: the peer whose ratchet key sorts lower rotates, the other adopts and only then rotates. From the second epoch on, every rotation is a reply to an adopted key.

both_peers_send_first_converges is the gate: both peers send before either receives, both then decrypt, and a further exchange both ways still decrypts. Mutation check: letting both peers rotate at bootstrap makes that test and its receive-order variant fail with Authentication — the crossing rotation is what it catches.

Bounds, replay, tamper

MAX_SKIP 2000 per chain jump, MAX_SKIPPED_KEYS 2000 with oldest-first eviction, and a skipped key is deleted on use. Bounds are checked in a pure plan_* step and applied only after the AEAD opens, so a rejected or tampered frame leaves the state byte-identical (asserted in the MAX_SKIP and header-tamper tests).

Tests (13 in tests/ratchet.rs)

KDF_CK vector (with the python3 command that produced it), prefix/version, both-send-first gate plus reverse receive order, two DH ratchet steps round trip, out-of-order inside one chain (index ends at 5, store empty), skipped key across a DH ratchet step via PN, N beyond MAX_SKIP with byte-identical state, replay, skipped-store cap (3 x 1400 skipped keys, cap held, oldest evicted), header tamper on dh_pub/N/PN, state round trip plus garbage/truncation/trailing/version, zeroize wipe.

Evidence

  • cargo test --locked --target x86_64-unknown-linux-gnu -> 258 passed; 0 failed; 4 ignored (29 suites, exit 0).
  • cargo clippy --locked --all-targets --target x86_64-unknown-linux-gnu -- -D warnings -> clean.
  • cd crates/onionwire-sdk && cargo test --locked -> 29 tests, 0 failed; cargo clippy --locked --all-targets -- -D warnings clean. SDK lockfile regenerated for hkdf/hmac.
  • rustfmt --edition 2024 --check src/ratchet.rs tests/ratchet.rs -> clean.

Scope

Cargo.toml (hkdf/hmac lines), Cargo.lock, crates/onionwire-sdk/Cargo.lock, src/lib.rs (pub mod ratchet), src/ratchet.rs, tests/ratchet.rs. Nothing else.

Card: t_85aaf5a6 (onionwire core: Double Ratchet module). New `src/ratchet.rs` (wire-independent) and `tests/ratchet.rs`. No wiring into `node.rs` — that is the next card. ### Convergent initiation (the design gate) An epoch is the unordered pair of the two current ratchet public keys. Its two chain keys come from two `KDF_RK` calls on that pair's shared secret: the first yields the chain for the "lo to hi" direction (lower public key to higher), the second — on the first call's root — the chain for "hi to lo", and the second root is the epoch's new root. Which side authored which direction is decided by public-key order, never by who spoke first, so both peers derive both chains for the same pair and each sends on its own direction immediately. Invariant: the root and the chain keys are a pure function of the sequence of ratchet-key pairs, and both peers walk that sequence in the same causal order — a DH ratchet step happens only on receiving a not-yet-adopted ratchet key, so at most one rotation is ever in flight. The bootstrap epoch (both keys known from the session bootstrap) uses one deterministic tie-break: the peer whose ratchet key sorts lower rotates, the other adopts and only then rotates. From the second epoch on, every rotation is a reply to an adopted key. `both_peers_send_first_converges` is the gate: both peers send before either receives, both then decrypt, and a further exchange both ways still decrypts. Mutation check: letting both peers rotate at bootstrap makes that test and its receive-order variant fail with `Authentication` — the crossing rotation is what it catches. ### Bounds, replay, tamper MAX_SKIP 2000 per chain jump, MAX_SKIPPED_KEYS 2000 with oldest-first eviction, and a skipped key is deleted on use. Bounds are checked in a pure `plan_*` step and applied only after the AEAD opens, so a rejected or tampered frame leaves the state byte-identical (asserted in the MAX_SKIP and header-tamper tests). ### Tests (13 in tests/ratchet.rs) KDF_CK vector (with the python3 command that produced it), prefix/version, both-send-first gate plus reverse receive order, two DH ratchet steps round trip, out-of-order inside one chain (index ends at 5, store empty), skipped key across a DH ratchet step via PN, N beyond MAX_SKIP with byte-identical state, replay, skipped-store cap (3 x 1400 skipped keys, cap held, oldest evicted), header tamper on dh_pub/N/PN, state round trip plus garbage/truncation/trailing/version, zeroize wipe. ### Evidence - `cargo test --locked --target x86_64-unknown-linux-gnu` -> 258 passed; 0 failed; 4 ignored (29 suites, exit 0). - `cargo clippy --locked --all-targets --target x86_64-unknown-linux-gnu -- -D warnings` -> clean. - `cd crates/onionwire-sdk && cargo test --locked` -> 29 tests, 0 failed; `cargo clippy --locked --all-targets -- -D warnings` clean. SDK lockfile regenerated for hkdf/hmac. - `rustfmt --edition 2024 --check src/ratchet.rs tests/ratchet.rs` -> clean. ### Scope Cargo.toml (hkdf/hmac lines), Cargo.lock, crates/onionwire-sdk/Cargo.lock, src/lib.rs (pub mod ratchet), src/ratchet.rs, tests/ratchet.rs. Nothing else.
ratchet: Double Ratchet state machine for chat frames
All checks were successful
ci / sdk (pull_request) Successful in 1m25s
android-ci / android (pull_request) Successful in 9m12s
ci / test (pull_request) Successful in 4m35s
8b2e834616
New wire-independent `src/ratchet.rs` + `tests/ratchet.rs`: KDF_RK/KDF_CK as in
the Signal spec, symmetric ratchet, DH ratchet, skipped-key store with the
MAX_SKIP / MAX_SKIPPED_KEYS bounds, versioned state serialization and zeroize
of every superseded secret.

Initiation gate: per-direction chains seeded by ratchet-key ordering (option
(a)), so both peers can build a sending chain before either has received, and a
single deterministic tie-break (lower ratchet key rotates first) keeps exactly
one rotation in flight. Documented under "Initiation and convergence" and
covered by both_peers_send_first_converges, which fails on a crossing rotation.

Adds hkdf 0.12 + hmac 0.12 (0.13 pulls digest 0.11 and will not build against
sha2 0.10) and regenerates crates/onionwire-sdk/Cargo.lock.
sirius merged commit 5a36338178 into main 2026-09-16 22:17:11 +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!85
No description provided.