fil: pad chunk bodies to a length ladder, document the residual
All checks were successful
ci / test (pull_request) Successful in 21m34s
ci / sdk (pull_request) Successful in 2m17s

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.
This commit is contained in:
agent 2026-10-22 22:46:28 -04:00
commit 30f438c394
No known key found for this signature in database
6 changed files with 383 additions and 54 deletions

View file

@ -3,7 +3,7 @@
//! 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, MAX_BODY, PAD_HEADER_LEN};
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,
@ -221,6 +221,86 @@ fn a_classical_frame_keeps_the_329_byte_floor() {
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