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

@ -96,17 +96,19 @@ uint8 body[length]
- Other `version` values are a hard fail (close the stream).
- Length that does not match the bytes on the wire is a hard fail.
`length` still reveals the exact ciphertext size. Chat hides it by padding the
plaintext to a bucket before encryption (see [Length buckets](#length-buckets)).
Other frame types are not padded.
`length` still reveals the exact ciphertext size. Chat and `fil ` chunk bodies
hide it by padding the plaintext to a bucket before encryption (see
[Length buckets](#length-buckets)). Other frame types are not padded.
## Length buckets
Normative for chat. A chat line is padded to one fixed ladder **before** the
ratchet encrypts it, so the padding is inside ChaCha20-Poly1305, inside the
Noise session, and authenticated by both. An observer sees a ciphertext whose
length is one of the ladder values (plus constant overhead), never the body
length.
Normative for chat lines and `fil ` chunk bodies. A chat line is padded to one
fixed ladder **before** the ratchet encrypts it, so the padding is inside
ChaCha20-Poly1305, inside the Noise session, and authenticated by both. A `fil `
chunk body is padded with the same encoding on a longer ladder before its
one-shot Noise session encrypts it, so its padding is authenticated by that
session's tag. An observer sees a ciphertext whose length is one of the ladder
values (plus constant overhead), never the body length.
Ladder — padded-plaintext sizes in bytes:
@ -179,10 +181,36 @@ first hybrid ratchet step (`kem_pub[1184] || kem_ct[1088]`). Epoch 0 of a chat
is always classical, so the first `rat ` frame of a friend relationship uses
the 40-byte header.
`fil ` chunks are deliberately **not** padded. They are raw file bytes paced
through the accept token bucket, and padding them would either cut the usable
chunk size or spend the 65535 frame cap on fill; the chunk size stays as
specified under `fil ` — file chunk.
### `fil ` chunk bodies
A `fil ` chunk body — everything after the last newline of the frame, see
`fil ` — file chunk below — is padded with the chat encoding above and a longer
ladder:
```
512, 1024, 2048, 4096, 8192, 16384, 32768, 49152
```
- The rungs up to 32768 are the chat ladder's. The chat floor of 256 is not
used: every `fil ` frame already carries ~115 bytes of ASCII header, so a
256-byte rung would round almost every chunk up for no gain.
- 49152 (48 KiB) is the top rung. The doubled 65536 cannot fit one Noise body
(a plaintext is at most 65519 bytes, tag included), and 48 KiB leaves the
worst-case `fil ` header — 128-byte name, 5-digit `idx` and `total`, 243
bytes — more than 16 KiB of slack.
- The raw chunk cap is the top rung's body, 49150 bytes. A chunk therefore
never needs more than one rung and `pad_len` always fits a `uint16`. A chunk
body larger than 49150 bytes is a protocol error and MUST fail closed.
- The receiver rules above are unchanged: `pad_len` is the only authority for
the chunk body's length, an overrun is a hard failure and the chunk is
dropped whole, and the total padded length is not required to be a ladder
value.
This hides the chunk body length only. The *number* of `fil ` chunks still
reveals the file's size in buckets (a 1 MiB file is 22 chunks), and transfer is
still one Noise session per chunk, so timing, circuit patterns and retry
cadence remain visible exactly as they are for chat. Padding adds no persistent
session, no channel binding and no ratchet between chunks.
This hides length, not timing: one Noise session per message, retry cadence and
circuit timing are unchanged and remain accepted costs (see
@ -499,15 +527,27 @@ fails the transfer. No outbox, no resume, no continuation under a new
`xfer_id`.
```
fil <xfer_id_hex>\n<filename>\n<sha256_hex>\n<idx>/<total>\n<raw bytes>
fil <xfer_id_hex>\n<filename>\n<sha256_hex>\n<idx>/<total>\n<padded chunk body>
```
- `xfer_id` — 16 random bytes (32 hex)
- `filename` — 1–128 bytes, no `/`, NUL, newline, `.`, or `..`; no C0/C1 controls or bidi/format marks (U+202A–U+202E, U+2066–U+2069, U+200E, U+200F, U+200B, U+200C)
- `sha256` — SHA-256 of the **entire** file (64 hex), repeated on every chunk
- `idx` is 0-based; `total` ≥ 1; `idx < total`
- last line is raw chunk bytes (not hex), length chosen so the framed Noise
ciphertext stays ≤ 65535 (account for a 16-byte Poly1305 tag plus ASCII header)
- last line is the chunk body padded to a `fil ` length bucket (see
[Length buckets](#length-buckets)): `uint16 pad_len` big-endian, the raw
chunk bytes, then `pad_len` random fill bytes. The padded body MUST be
exactly the smallest `fil ` ladder value that is at least `2 + data_len`,
and `pad_len = bucket - 2 - data_len`.
- The raw chunk is at most **49150** bytes (the top rung's body). The ASCII
header, the padded body and the 16-byte Poly1305 tag together stay under the
65535 frame cap.
- The padding is inside the one-shot Noise session, so `pad_len` is
authenticated by that session's ChaCha20-Poly1305 tag — a tampered length
fails the session rather than a checksum and the chunk is never written.
`pad_len` is the only authority for the boundary: a receiver MUST NOT trim
trailing fill, and MUST reject a `pad_len` that overruns the body as a
protocol error, dropping the whole chunk instead of writing a truncated one.
Chunks MUST arrive in order for a given `xfer_id`. A bad chunk or hash
mismatch deletes the partial. Empty files are one chunk. Successful receive