ratchet: freeze the v1 state/frame wire format with guard tests
All checks were successful
ci / test (pull_request) Successful in 4m47s
ci / sdk (pull_request) Successful in 57s

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.
This commit is contained in:
agent 2026-09-17 20:43:31 -04:00
commit 1aec007601
No known key found for this signature in database
2 changed files with 176 additions and 0 deletions

View file

@ -372,6 +372,81 @@ fn state_round_trips_and_rejects_garbage() {
assert!(RatchetState::from_bytes(&huge_count).is_err());
}
/// Fixed size of a v1 state blob's prefix: the version byte, the root key, the
/// local secret and public, the remote public, both chain keys, both counters,
/// `prev_send_len`, the established flag and the skipped-key count.
/// Documented in `src/ratchet.rs`'s `to_bytes` layout.
const STATE_PREFIX_LEN: usize = 1 + 32 + 32 + 32 + 32 + 32 + 4 + 32 + 4 + 4 + 1 + 4;
/// Freeze guard for the persisted wire format. A stored session is only
/// meaningful if both sides agree byte for byte on the layout, so a version
/// bump or a field insertion must break this test on purpose.
///
/// `0x01` is asserted as a literal: asserting `STATE_VERSION` would silently
/// follow a bump instead of catching it, which is the whole point of the
/// guard.
#[test]
fn ratchet_state_blob_is_frozen_at_version_v1() {
let local = RatchetKeyPair::from_secret([0x11u8; 32]);
let remote = RatchetKeyPair::from_secret([0x22u8; 32]);
let state = RatchetState::bootstrap(ROOT, local, remote.public());
let bytes = state.to_bytes();
assert_eq!(
STATE_VERSION, 1,
"the frozen version byte changed: this is a wire-format break, not a refactor"
);
assert_eq!(
bytes[0], 0x01,
"a state blob must start with the v1 version byte"
);
assert_eq!(
bytes.len(),
STATE_PREFIX_LEN,
"the fixed part of the v1 layout changed (a field was added, removed or resized)"
);
assert_eq!(
bytes.len(),
210,
"the documented v1 prefix is 210 bytes; a different number means the \
format moved and stored sessions need a migration path"
);
}
/// A blob written by another version must come back as a typed
/// `UnsupportedStateVersion`, never as a panic and never as a state decoded
/// under the wrong layout. The guard must also not be over-eager: the
/// untouched v1 blob still loads.
#[test]
fn ratchet_state_foreign_version_is_rejected_not_misread() {
let (mut a, mut b) = peers();
// Decrypting m2 first leaves m0/m1 in the skipped store, so the blob is
// longer than the fixed prefix and the check cannot pass by luck.
let frames: Vec<Vec<u8>> = (0..3)
.map(|i| a.encrypt(format!("m{i}").as_bytes()).expect("encrypt"))
.collect();
assert_eq!(b.decrypt(&frames[2]).expect("m2"), b"m2");
let bytes = b.to_bytes();
assert!(
bytes.len() > STATE_PREFIX_LEN,
"expected skipped keys in the blob, got {} bytes",
bytes.len()
);
for version in [0x00u8, 0x02, 0x7f, 0xff] {
let mut foreign = bytes.clone();
foreign[0] = version;
assert_eq!(
RatchetState::from_bytes(&foreign).expect_err("a foreign version must be rejected"),
Error::UnsupportedStateVersion(version),
"version {version:#04x} must be rejected under its own code, not misread or panicked on"
);
}
let reloaded = RatchetState::from_bytes(&bytes).expect("the untouched v1 blob still loads");
assert_eq!(reloaded.to_bytes(), bytes, "v1 round trip is not stable");
}
/// Mirrors the private `MIN_FRAME_LEN` in `src/ratchet.rs`
/// (`HEADER_OFFSET + HEADER_LEN + NONCE_LEN + TAG_LEN`), which the crate does
/// not export.