The persisted ratchet state had a version byte and typed errors but no
test that a format change would actually be caught. Add two dedicated
guards to tests/ratchet.rs that assert the frozen shape as literals
(0x01, 210-byte prefix) rather than via STATE_VERSION, so a bump breaks
them visibly, and pin that any foreign version byte is rejected with
Error::UnsupportedStateVersion before any field is read.
Document the frozen v1 state layout, the rat frame layout, and the
migration path a v2 must follow in references/ratchet-wire-frozen.md.
Verified by flipping STATE_VERSION to 2: both guards fail
("the frozen version byte changed"), then revert.
4.6 KiB
Ratchet wire format — frozen at v1
Status: frozen. A change to the persisted ratchet state layout, or to the
rat frame layout, is a wire-format break and needs a migration path (see
"Changing the format" below). The guard tests that enforce this live in
tests/ratchet.rs:
ratchet_state_blob_is_frozen_at_version_v1ratchet_state_foreign_version_is_rejected_not_misread
Both are written to fail visibly on a version bump or a field insertion:
they assert the literal byte 0x01 and the literal 210-byte prefix length,
not the STATE_VERSION constant, so a bump cannot quietly follow them.
Persisted state (ratchet_sessions.state)
Written by RatchetState::to_bytes, read by RatchetState::from_bytes
(src/ratchet.rs). The store treats the blob as opaque
(Store::load_ratchet / Store::save_ratchet, src/store.rs; the row is
ratchet_sessions(state BLOB NOT NULL) keyed by friend_id). There is no
public decode_state function in the crate: the persisted blob is decoded by
RatchetState::from_bytes, reached from ratchet_for in src/node.rs, and
the resulting version error is reported as Error::UnsupportedStateVersion.
Layout — version 0x01:
0 version byte 0x01 (STATE_VERSION)
1..33 root_key
33..65 local ratchet secret
65..97 local ratchet public
97..129 remote ratchet public
129..161 sending chain key
161..165 send_n u32 BE
165..197 receiving chain key
197..201 recv_n u32 BE
201..205 prev_send_len u32 BE
205 established (0 or 1)
206..210 skipped-key count u32 BE
210.. per skipped key, in insertion order: dh_pub[32] || n u32 BE || key[32]
The fixed prefix is 210 bytes (pinned as STATE_PREFIX_LEN in the test);
the skipped-key tail is 68 bytes per key, capped at MAX_SKIPPED_KEYS
(2000). The blob contains secret key material — the caller owns wiping it.
from_bytes is fail-closed on every malformed input it can see: a foreign
version byte returns Error::UnsupportedStateVersion(byte) before any field
is read, so a blob from another version is never decoded under the v1 layout;
empty, truncated, trailing-byte, bad-established-flag, bad skipped-count and
secret-does-not-match-its-public-key inputs return Error::BadState(&str).
None of them panic or partially apply.
Frames (rat chat frames)
Written by RatchetState::encrypt, parsed by Frame::parse
(src/ratchet.rs). Framing constants: FRAME_PREFIX = b"rat ",
FRAME_VERSION = 0x01, HEADER_LEN = 40, HEADER_OFFSET = 5.
0..4 b"rat "
4 version byte 0x01 (FRAME_VERSION)
5..45 header: dh_pub[32] || N u32 BE || PN u32 BE
45..57 nonce[12]
57.. ciphertext || Poly1305 tag[16] (ChaCha20-Poly1305)
The 40-byte header is also the AEAD associated data, so any header edit fails
authentication rather than being silently ignored. Frame::parse checks the
prefix, then the minimum length (HEADER_OFFSET + HEADER_LEN + 12 + 16 = 73),
then the version byte; a sub-73-byte frame is a length error whatever its
version byte says, and a frame of 73 bytes or more with a foreign version is a
version error. Rejected frames leave the state byte-identical.
Changing the format
There is no migration code today, and a version bump must not silently re-bootstrap a session: replacing an unreadable stored session with a fresh bootstrap produces traffic the peer cannot decrypt and loses the established epoch. The required shape for a v2 is:
- Bump
STATE_VERSIONand makefrom_byteskeep the v1 decoder reachable (match on the version byte, decode old blobs with the old layout, then re-serialize as v2). - Do not let an older build read a newer blob: it must keep rejecting
anything that is not its own version with
UnsupportedStateVersion, never decode it under the old layout. That is what the current code does, and it is why only the newer build may convert a blob in place. - Surface the refusal to the user. Today the message only reaches a log:
an outbound
Node::sendfails closed withErr(String)(ratchet state: unsupported ratchet state version N, fromratchet_for), and an inbound frame is dropped witheprintln!("rat dropped ({e})")inNode::handle_incoming'sKind::Ratchetarm. There is no dialog and no automatic recovery; a v2 must add the user-visible message rather than rely on stderr. - Keep the freeze tests green only by updating the pinned literals in
tests/ratchet.rsand the layout docs in this file in the same change.
An additive change with no version bump is not an option: the skipped-key tail is read by count, so appending a field changes what the tail means for every stored blob.