Each `fil ` chunk carried its raw bytes in a one-shot Noise session, so the frame length was the exact chunk size: the tail chunk of every file told an observer the file's size, and the chunk count told them the rest. Pad the chunk body with the chat encoding (uint16 pad_len || data || random fill) on a `fil ` ladder that continues past chat's 32 KiB ceiling — 512..49152 — because a chunk is bounded by the frame budget (~64 KiB of plaintext), not by a line of human text. The top rung is a half-step: 65536 cannot fit one Noise body, and 48 KiB leaves the worst-case header (128-byte name, 5-digit counters) more than 16 KiB of slack. The raw chunk cap becomes the top rung's body (49150 bytes), so a 1 MiB file is 22 one-shot sessions instead of 17. `pad_len` is authenticated by the session's ChaCha20-Poly1305 tag; `decode_checked` treats an overrunning `pad_len` as a protocol error and drops the chunk whole rather than writing a truncated one. `total_chunks`/`data_cap` stay the reassembly authority and the hash check is untouched. PROTOCOL.md gains the ladder in “Length buckets” and the padded `fil ` frame layout, and states the honest residual: padding hides per-chunk size only — the chunk count still reveals total size in buckets, and timing, circuit patterns and retry cadence remain visible. No persistent session, no channel binding, no ratcheting of chunks.
314 lines
11 KiB
Rust
314 lines
11 KiB
Rust
//! Normative chat length buckets: the ladder, the plaintext encoding, and the
|
|
//! receiver's bounds. The wire behaviour these prove (a `rat ` frame of a
|
|
//! bucketed size, a tampered field failing closed) lives in `src/node.rs`'s
|
|
//! unit tests, because `tests/*.rs` cannot build a `Node`.
|
|
|
|
use onionwire::padding::{self, BUCKETS, FIL_BUCKETS, FIL_MAX_BODY, 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() {
|
|
assert_eq!(BUCKETS, [256, 512, 1024, 2048, 4096, 8192, 16384, 32768]);
|
|
assert_eq!(PAD_HEADER_LEN, 2);
|
|
assert_eq!(MAX_BODY, 32766);
|
|
}
|
|
|
|
#[test]
|
|
fn buckets_follow_the_body_plus_the_two_byte_header() {
|
|
let cases = [
|
|
(0, Some(256)),
|
|
(1, Some(256)),
|
|
(253, Some(256)),
|
|
(254, Some(256)),
|
|
(255, Some(512)),
|
|
(510, Some(512)),
|
|
(511, Some(1024)),
|
|
(1022, Some(1024)),
|
|
(1023, Some(2048)),
|
|
(2046, Some(2048)),
|
|
(2047, Some(4096)),
|
|
(4094, Some(4096)),
|
|
(4095, Some(8192)),
|
|
(8190, Some(8192)),
|
|
(8191, Some(16384)),
|
|
(16382, Some(16384)),
|
|
(16383, Some(32768)),
|
|
(32766, Some(32768)),
|
|
(32767, None),
|
|
(65535, None),
|
|
];
|
|
for (body_len, want) in cases {
|
|
assert_eq!(
|
|
padding::bucket_for(body_len),
|
|
want,
|
|
"bucket for a {body_len}-byte body"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_one_byte_message_round_trips_in_the_smallest_bucket() {
|
|
let padded = padding::encode(b"x").expect("encode");
|
|
assert_eq!(padded.len(), 256);
|
|
assert_eq!(padding::decode(&padded).expect("decode"), b"x");
|
|
}
|
|
|
|
#[test]
|
|
fn an_empty_message_round_trips_in_the_smallest_bucket() {
|
|
let padded = padding::encode(b"").expect("encode");
|
|
assert_eq!(padded.len(), 256);
|
|
assert_eq!(padding::decode(&padded).expect("decode"), b"");
|
|
}
|
|
|
|
#[test]
|
|
fn an_exactly_bucket_sized_message_round_trips_with_no_pad() {
|
|
let body = vec![b'a'; 254];
|
|
let padded = padding::encode(&body).expect("encode");
|
|
assert_eq!(padded.len(), 256);
|
|
assert_eq!(&padded[..2], &[0u8, 0u8], "pad_len is zero at an exact fit");
|
|
assert_eq!(padding::decode(&padded).expect("decode"), &body[..]);
|
|
}
|
|
|
|
#[test]
|
|
fn an_oversize_message_fails_closed() {
|
|
let err = padding::encode(&vec![b'a'; MAX_BODY + 1]).expect_err("must not fit a bucket");
|
|
assert!(err.contains("too large"), "{err}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_body_ending_in_the_pad_fill_byte_round_trips() {
|
|
// Hand-built: `pad_len` is authoritative, so trailing bytes are never
|
|
// trimmed and a body that ends in the same value as the fill survives.
|
|
// The total is deliberately not a ladder value: receivers accept any size.
|
|
let mut padded = Vec::new();
|
|
padded.extend_from_slice(&3u16.to_be_bytes());
|
|
padded.extend_from_slice(b"body\xaa");
|
|
padded.extend_from_slice(&[0xaa; 3]);
|
|
assert_eq!(padded.len(), 2 + 5 + 3);
|
|
assert_eq!(padding::decode(&padded).expect("decode"), b"body\xaa");
|
|
}
|
|
|
|
#[test]
|
|
fn a_pad_len_that_overruns_the_plaintext_is_rejected() {
|
|
// 16 bytes total, pad_len says 15: one byte of body plus header is already
|
|
// 3, so it overruns by 1.
|
|
let mut bad = Vec::new();
|
|
bad.extend_from_slice(&15u16.to_be_bytes());
|
|
bad.extend_from_slice(&[0u8; 14]);
|
|
let err = padding::decode(&bad).expect_err("must fail closed");
|
|
assert!(err.contains("overrun"), "{err}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_truncated_padding_header_is_rejected() {
|
|
assert!(padding::decode(b"").is_err());
|
|
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 the_fil_ladder_continues_past_the_chat_ceiling_and_fits_one_noise_body() {
|
|
// `fil ` chunk bodies are bounded by the frame budget (~64 KiB plaintext),
|
|
// not by a line of human text, so the ladder cannot stop at chat's 32 KiB:
|
|
// that would double the chunk count of a 1 MiB file and spend twice the
|
|
// one-shot sessions. The top rung is a half-step because the next doubling
|
|
// (65536) does not fit one Noise body.
|
|
assert_eq!(
|
|
FIL_BUCKETS,
|
|
[512, 1024, 2048, 4096, 8192, 16384, 32768, 49152]
|
|
);
|
|
assert_eq!(FIL_MAX_BODY, 49150);
|
|
assert_eq!(
|
|
FIL_BUCKETS[FIL_BUCKETS.len() - 1] - PAD_HEADER_LEN,
|
|
FIL_MAX_BODY
|
|
);
|
|
// Strictly increasing rungs, same 2-byte header as chat.
|
|
for w in FIL_BUCKETS.windows(2) {
|
|
assert!(w[1] > w[0], "ladder must increase: {w:?}");
|
|
}
|
|
// Worst-case `fil ` header (128-byte name, 5-digit idx/total) plus the top
|
|
// rung plus the Noise tag must still fit one frame body.
|
|
let worst_header = 4 + 32 + 1 + 128 + 1 + 64 + 1 + 5 + 1 + 5 + 1;
|
|
assert!(
|
|
worst_header + FIL_BUCKETS[FIL_BUCKETS.len() - 1] + 16 <= onionwire::frame::MAX_FRAME,
|
|
"the top rung must fit one Noise body"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn fil_buckets_follow_the_body_plus_the_two_byte_header() {
|
|
let cases = [
|
|
(0, Some(512)),
|
|
(1, Some(512)),
|
|
(510, Some(512)),
|
|
(511, Some(1024)),
|
|
(1022, Some(1024)),
|
|
(1023, Some(2048)),
|
|
(32766, Some(32768)),
|
|
(32767, Some(49152)),
|
|
(49150, Some(49152)),
|
|
(49151, None),
|
|
];
|
|
for (body_len, want) in cases {
|
|
assert_eq!(
|
|
padding::fil_bucket_for(body_len),
|
|
want,
|
|
"fil bucket for a {body_len}-byte body"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_fil_chunk_body_round_trips_and_an_oversized_one_fails_closed() {
|
|
for body in [&b""[..], &b"x"[..], &[0u8; 510][..], &[7u8; 49150][..]] {
|
|
let padded = padding::encode_fil(body).expect("encode");
|
|
let bucket = padding::fil_bucket_for(body.len()).expect("bucket");
|
|
assert_eq!(padded.len(), bucket);
|
|
assert_eq!(padding::decode(&padded).expect("decode"), body);
|
|
}
|
|
let err = padding::encode_fil(&vec![0u8; FIL_MAX_BODY + 1]).expect_err("must not fit");
|
|
assert!(err.contains("too large"), "{err}");
|
|
}
|
|
|
|
/// `fil ` bodies reuse the chat encoding verbatim: the same `uint16 pad_len`,
|
|
/// the same "overrun is a hard failure" rule. The ladders differ, the bytes do
|
|
/// not.
|
|
#[test]
|
|
fn fil_and_chat_share_the_same_encoding() {
|
|
let padded = padding::encode_fil(b"hello").expect("encode");
|
|
assert_eq!(padded.len(), 512);
|
|
assert_eq!(padded[..2], [0x01, 0xf9], "pad_len = 512 - 2 - 5 = 505");
|
|
assert_eq!(padding::decode(&padded).expect("decode"), b"hello");
|
|
|
|
let mut bad = Vec::new();
|
|
bad.extend_from_slice(&15u16.to_be_bytes());
|
|
bad.extend_from_slice(&[0u8; 14]);
|
|
assert!(padding::decode(&bad).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn a_receiver_accepts_a_padded_plaintext_of_any_total_length() {
|
|
// Interop: another implementation's ladder is not ours, and the receiver
|
|
// must not require a bucket-sized total, only a consistent pad_len.
|
|
let mut padded = Vec::new();
|
|
padded.extend_from_slice(&30u16.to_be_bytes());
|
|
padded.extend_from_slice(&[b'z'; 268]);
|
|
padded.extend_from_slice(&[0u8; 30]);
|
|
assert_eq!(padded.len(), 300);
|
|
assert_eq!(padding::decode(&padded).expect("decode").len(), 268);
|
|
}
|