ratchet: Double Ratchet state machine (symmetric + DH ratchet, bounded skipped keys) #85
Loading…
Reference in a new issue
No description provided.
Delete branch "wt/t_85aaf5a6"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Card: t_85aaf5a6 (onionwire core: Double Ratchet module).
New
src/ratchet.rs(wire-independent) andtests/ratchet.rs. No wiring intonode.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_RKcalls 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_convergesis 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 withAuthentication— 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 warningsclean. 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.