From 6f267a6aba52ee68f88e437abf2ce3253bd16e17 Mon Sep 17 00:00:00 2001 From: agent Date: Thu, 17 Sep 2026 20:41:48 -0400 Subject: [PATCH] tests: assert the DH ratchet step in lockstep on a bidirectional burst `round_trip_across_two_dh_ratchet_steps` only asserts that neither sending ratchet key stayed put; it would still pass if the two sides stepped onto unconnected epochs, and it never checks that either peer adopted the key the other advertised. The new test reads dh_pub out of the wire header and pins the adoption per step across an A->B / B->A burst: - round 1 rotates only the lower-key (establishing) peer, so one round cannot lockstep both sides -- the intermediate state is asserted, not assumed; - rounds 2 and 3 step each side in turn, asserting each receiver adopts exactly the key the frame advertised; - the three-frame burst each way asserts chain reads (not extra steps), zero skipped keys, and that the peer that received last is keyed to the other's current key. tests/ratchet.rs only; no src change, no new dependency. --- tests/ratchet.rs | 124 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) diff --git a/tests/ratchet.rs b/tests/ratchet.rs index 3407afa..67a4095 100644 --- a/tests/ratchet.rs +++ b/tests/ratchet.rs @@ -147,6 +147,130 @@ fn round_trip_across_two_dh_ratchet_steps() { assert_eq!(a.decrypt(&m4).expect("decrypt m4"), b"m4"); } +/// The ratchet key a frame advertises: `dh_pub[0..32]` of the header. +fn dh_pub(frame: &[u8]) -> [u8; 32] { + header_at(frame)[..32].try_into().expect("32") +} + +/// Lockstep on a bidirectional burst: every frame after a peer has stepped +/// carries the key that peer is now sending with, and the receiver *adopts +/// exactly that key* before its sending chain rekeys. +/// +/// `round_trip_across_two_dh_ratchet_steps` only asserts that neither sending +/// ratchet stayed put; it would still pass if the two sides stepped onto +/// unconnected epochs, and it never checks that either side adopted the key the +/// other advertised. This test reads the key out of the wire header and asserts +/// the adoption, per step, on a burst in both directions. +/// +/// The bootstrap tie-break means one A→B/B→A round cannot step both sides: only +/// the lower-key peer rotates its sending chain on establishment, and the other +/// peer has still only ever seen bootstrap keys. The burst therefore drives the +/// second and third rounds, and pins the intermediate state at each one. +#[test] +fn bidirectional_burst_keeps_the_dh_ratchet_in_lockstep() { + let (mut a, mut b) = peers(); + let (a0, b0) = (a.public(), b.public()); + assert_eq!(a.remote_pub(), b0, "a starts keyed to b's bootstrap key"); + assert_eq!(b.remote_pub(), a0, "b starts keyed to a's bootstrap key"); + + // Round 1 — A→B then B→A. Only a rotates here (it sorts lower). + let m1 = a.encrypt(b"a/1").expect("encrypt"); + assert_eq!( + dh_pub(&m1), + a0, + "a's first frame advertises its bootstrap key" + ); + assert_eq!(b.decrypt(&m1).expect("b decrypts a/1"), b"a/1"); + assert_eq!(b.public(), b0, "b has no new key to adopt yet"); + assert_eq!(b.remote_pub(), a0); + + let m2 = b.encrypt(b"b/1").expect("encrypt"); + assert_eq!( + dh_pub(&m2), + b0, + "b's first frame advertises its bootstrap key" + ); + assert_eq!(a.decrypt(&m2).expect("a decrypts b/1"), b"b/1"); + let a1 = a.public(); + assert_ne!(a1, a0, "round 1 must rotate a's sending ratchet key"); + assert_eq!(a.remote_pub(), b0, "a is keyed to the key b advertised"); + + // Round 2 — a's fresh key reaches b, which is the step the one-round + // exchange never takes. + let m3 = a.encrypt(b"a/2").expect("encrypt"); + assert_eq!(dh_pub(&m3), a1, "a advertises the key it just ratcheted to"); + assert_eq!(b.decrypt(&m3).expect("b decrypts a/2"), b"a/2"); + let b1 = b.public(); + assert_ne!(b1, b0, "b must DH-ratchet on a's new key"); + assert_eq!(b.remote_pub(), a1, "b adopted exactly the key a advertised"); + + // Round 3 — the mirror image: b's fresh key reaches a. + let m4 = b.encrypt(b"b/2").expect("encrypt"); + assert_eq!(dh_pub(&m4), b1, "b advertises the key it just ratcheted to"); + assert_eq!(a.decrypt(&m4).expect("a decrypts b/2"), b"b/2"); + let a2 = a.public(); + assert_ne!(a2, a1, "a stepped onto a fresh key of its own"); + assert_eq!(a.remote_pub(), b1, "a adopted exactly the key b advertised"); + + // The burst proper: three frames each way on the epoch both sides now hold. + let to_b: Vec> = (0..3) + .map(|i| { + a.encrypt(format!("a/burst{i}").as_bytes()) + .expect("encrypt") + }) + .collect(); + for frame in &to_b { + assert_eq!(dh_pub(frame), a2, "the burst stays on a's current key"); + } + for (i, frame) in to_b.iter().enumerate() { + assert_eq!( + b.decrypt(frame).expect("burst a→b"), + format!("a/burst{i}").as_bytes() + ); + } + let b2 = b.public(); + assert_ne!( + b2, b1, + "b's first burst frame is a full step, not a chain read" + ); + assert_eq!(b.remote_pub(), a2, "b adopted a's burst key"); + assert_eq!(b.recv_index(), 3, "all three burst frames were consumed"); + assert_eq!(b.skipped_len(), 0, "in-order burst skipped nothing"); + + let to_a: Vec> = (0..3) + .map(|i| { + b.encrypt(format!("b/burst{i}").as_bytes()) + .expect("encrypt") + }) + .collect(); + for frame in &to_a { + assert_eq!(dh_pub(frame), b2, "the burst stays on b's current key"); + } + for (i, frame) in to_a.iter().enumerate() { + assert_eq!( + a.decrypt(frame).expect("burst b→a"), + format!("b/burst{i}").as_bytes() + ); + } + + // Lockstep after the burst: the peer that received last is keyed to the + // other's *current* key, and the sender's own key is the one its burst + // advertised. (b's view of a is a2 — a's step generated a3, which has not + // been sent yet; that lag is the ratchet's normal one-header asymmetry.) + assert_eq!(a.remote_pub(), b2); + assert_eq!(a.remote_pub(), b.public(), "a is keyed to b's current key"); + assert_eq!(b.remote_pub(), a2, "b is keyed to a's last advertised key"); + assert_ne!(a.public(), a2, "a stepped again on b's burst"); + assert_ne!(a.public(), a0, "a never ratcheted across the whole burst"); + assert_ne!(b.public(), b0, "b never ratcheted across the whole burst"); + assert_eq!(a.skipped_len(), 0); + assert_eq!(a.recv_index(), 3); + + // And the conversation still flows on the epoch the burst left behind. + let after = a.encrypt(b"a/after").expect("encrypt"); + assert_eq!(b.decrypt(&after).expect("a/after"), b"a/after"); +} + #[test] fn out_of_order_within_one_chain() { let (mut a, mut b) = peers();