diff --git a/src/node.rs b/src/node.rs index 4685828..86b6412 100644 --- a/src/node.rs +++ b/src/node.rs @@ -3264,14 +3264,17 @@ pub(crate) mod tests { .expect("node kem"); } - // The ML-KEM ciphertext is a handshake prelude, not part of the `rat ` - // header: the chat frame still fits the 256-byte bucket. + // Epoch 0 is always classical, even for a KEM friend: the first `rat ` + // frame carries the 40-byte header and the PQ prelude lives in the + // one-shot handshake. The 2312-byte hybrid header only appears on the + // first rotation (`tests/padding.rs` pins that 2601-byte floor). let frame = encode_chat_frame(&peer_store, &peer, &node.identity_pk(), b"pq").expect("encode"); + assert_eq!(frame[4], crate::ratchet::FRAME_VERSION); assert_eq!( frame.len(), RAT_OVERHEAD + 256, - "the hybrid prelude does not widen the ratchet frame" + "epoch 0 is classical, so the hybrid prelude does not widen it" ); let nk = node_kem_pk(&node); deliver_frame(&node, &peer, &frame, Some(&nk)) diff --git a/tests/padding.rs b/tests/padding.rs index 27d8aeb..c9464a9 100644 --- a/tests/padding.rs +++ b/tests/padding.rs @@ -4,6 +4,12 @@ //! unit tests, because `tests/*.rs` cannot build a `Node`. use onionwire::padding::{self, BUCKETS, MAX_BODY, PAD_HEADER_LEN}; +use onionwire::ratchet::{ + FRAME_VERSION, FRAME_VERSION_HYBRID, HEADER_LEN, HEADER_LEN_HYBRID, RatchetKeyPair, + RatchetState, +}; + +const ROOT: [u8; 32] = [0x7au8; 32]; #[test] fn the_ladder_is_the_documented_one() { @@ -104,6 +110,117 @@ fn a_truncated_padding_header_is_rejected() { assert!(padding::decode(b"\x00").is_err()); } +/// Two hybrid peers from one bootstrap root. `a` holds the lower ratchet key, +/// so the documented tie-break (`a` rotates first) is deterministic instead of +/// a coin flip per run. +fn hybrid_peers() -> (RatchetState, RatchetState) { + let k1 = RatchetKeyPair::from_secret([0x11u8; 32]); + let k2 = RatchetKeyPair::from_secret([0x22u8; 32]); + let (lo, hi) = if k1.public() < k2.public() { + (k1, k2) + } else { + (k2, k1) + }; + let (lo_pub, hi_pub) = (lo.public(), hi.public()); + let (lo_kem, hi_kem) = (lo.kem_public().to_vec(), hi.kem_public().to_vec()); + ( + RatchetState::bootstrap(ROOT, lo, hi_pub, Some(&hi_kem)), + RatchetState::bootstrap(ROOT, hi, lo_pub, Some(&lo_kem)), + ) +} + +/// Burn the bootstrap epoch so the next `a` send is its first hybrid step. +fn hybrid_step() -> (RatchetState, RatchetState) { + let (mut a, mut b) = hybrid_peers(); + let a1 = a.encrypt(b"bootstrap").expect("encrypt"); + let b1 = b.encrypt(b"bootstrap").expect("encrypt"); + assert_eq!(b.decrypt(&a1).expect("b bootstrap"), b"bootstrap"); + assert_eq!(a.decrypt(&b1).expect("a bootstrap"), b"bootstrap"); + (a, b) +} + +/// Padding sits *inside* the AEAD, so the bucket is unchanged by the header +/// version; the header sits outside it, so a hybrid step adds 2272 bytes to +/// the same padded plaintext. This is the true hybrid floor: 2601 in Noise +/// (2622 on the wire), against 329 (350) for the classical case. +#[test] +fn a_padded_plaintext_rides_inside_a_hybrid_header() { + let (mut a, mut b) = hybrid_step(); + let padded = padding::encode(b"pq").expect("encode"); + assert_eq!(padded.len(), 256, "a short body still pads to the floor"); + + let frame = a.encrypt(&padded).expect("encrypt"); + assert_eq!( + frame[4], FRAME_VERSION_HYBRID, + "the first rotation is a hybrid step" + ); + assert_eq!( + frame.len(), + 4 + 1 + HEADER_LEN_HYBRID + 12 + 256 + 16, + "hybrid rat frame" + ); + assert_eq!(frame.len(), 2601); + // The Noise transport adds its 16-byte tag and the framing header 5 bytes. + assert_eq!(frame.len() + 16 + 5, 2622, "hybrid frame on the wire"); + assert_eq!( + HEADER_LEN_HYBRID - HEADER_LEN, + 2272, + "a hybrid step's header is 2272 bytes larger" + ); + + assert_eq!( + padding::decode(&b.decrypt(&frame).expect("decrypt")).expect("decode"), + b"pq" + ); +} + +/// The ceiling: largest bucket (32768) plus the 2312-byte hybrid header plus +/// the frame's fixed overhead still fits one Noise body. +#[test] +fn the_largest_bucket_still_fits_inside_a_hybrid_frame() { + let (mut a, mut b) = hybrid_step(); + let body = vec![b'a'; MAX_BODY]; + let padded = padding::encode(&body).expect("encode"); + assert_eq!(padded.len(), 32768); + + let frame = a.encrypt(&padded).expect("encrypt"); + assert_eq!(frame[4], FRAME_VERSION_HYBRID); + assert_eq!(frame.len(), 4 + 1 + HEADER_LEN_HYBRID + 12 + 32768 + 16); + assert_eq!(frame.len(), 35113, "largest hybrid rat frame"); + assert!( + frame.len() + 16 <= onionwire::frame::MAX_FRAME, + "a hybrid frame must fit one Noise body" + ); + assert!(frame.len() <= 65519, "documented rat frame maximum"); + + assert_eq!( + padding::decode(&b.decrypt(&frame).expect("decrypt")) + .expect("decode") + .len(), + MAX_BODY + ); +} + +/// A classical friend's floor is unchanged by the hybrid work: 329 in Noise. +#[test] +fn a_classical_frame_keeps_the_329_byte_floor() { + let padded = padding::encode(b"").expect("encode"); + let k1 = RatchetKeyPair::from_secret([0x11u8; 32]); + let k2 = RatchetKeyPair::from_secret([0x22u8; 32]); + let (lo, hi) = if k1.public() < k2.public() { + (k1, k2) + } else { + (k2, k1) + }; + let hi_pub = hi.public(); + let mut a = RatchetState::bootstrap(ROOT, lo, hi_pub, None); + let frame = a.encrypt(&padded).expect("encrypt"); + assert_eq!(frame[4], FRAME_VERSION, "classical friend, classical frame"); + assert_eq!(frame.len(), 4 + 1 + HEADER_LEN + 12 + 256 + 16); + assert_eq!(frame.len(), 329); + assert_eq!(frame.len() + 16 + 5, 350, "classical frame on the wire"); +} + #[test] fn a_receiver_accepts_a_padded_plaintext_of_any_total_length() { // Interop: another implementation's ladder is not ours, and the receiver