chat: pad plaintext to a normative length ladder
All checks were successful
ci / test (pull_request) Successful in 15m47s
ci / sdk (pull_request) Successful in 1m31s

Chat plaintext is padded inside the ratchet AEAD to one of
256/512/1024/2048/4096/8192/16384/32768 bytes, with an explicit big-endian
uint16 pad_len followed by the body and random fill. An observer sees only
the bucket; the padding is authenticated by ChaCha20-Poly1305 and by the
Noise session. The receiver trusts pad_len only, rejects an overrun before
appending or persisting anything, and never infers a boundary from trailing
bytes. fil chunks are unchanged.

docs/PROTOCOL.md gains a normative Length buckets section, THREAT_MODEL
replaces the no-padding limit, README notes the bucketed chat length.
This commit is contained in:
agent 2026-10-20 12:38:04 -04:00
commit 7fa3edcdb4
No known key found for this signature in database
8 changed files with 517 additions and 11 deletions

View file

@ -96,6 +96,75 @@ 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 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.
Ladder — padded-plaintext sizes in bytes:
```
256, 512, 1024, 2048, 4096, 8192, 16384, 32768
```
Ratchet plaintext encoding:
```
uint16 pad_len big-endian
uint8 body[..] the chat line
uint8 fill[pad_len] random bytes, contents ignored
```
- The padded plaintext MUST be exactly the smallest ladder value that is at
least `2 + body_len`. `pad_len = bucket - 2 - body_len`.
- Empty and short bodies both pad to 256; a 254-byte body fits the 256 bucket
with `pad_len = 0`.
- `fill` is fresh random bytes. Its contents carry no meaning.
- A body larger than 32766 bytes does not fit the ladder and MUST fail closed
(it is a protocol error, never queued).
Receiver rules:
- The total decrypted length is **not** required to be a ladder value. Only the
header is normative, so a peer with a different ladder still interoperates.
- `pad_len` is the only authority for the body length. A receiver MUST NOT trim
trailing zeros, strip a repeated fill byte, or otherwise infer the boundary
from content: a body that legitimately ends in the same byte as the fill
round-trips unchanged.
- A plaintext shorter than 2 bytes, or one whose `pad_len` exceeds
`len - 2`, MUST be rejected as a hard failure before anything is appended,
rendered, or persisted. The ratchet state is not advanced.
- Unpadded frames are not accepted as if they were a bucket: if the first two
bytes do not describe a consistent `pad_len`, the frame fails closed.
- The `rat ` frame version stays `0x01`; padding is a plaintext-layer change.
Minimum observable size: a 256-byte bucket plus the `rat ` frame overhead
(`rat ` 4 + version 1 + header 40 + nonce 12 + tag 16 = 73) is a 329-byte
ratchet frame; the Noise transport adds its 16-byte tag and the framing header
adds 5 bytes, so the **smallest chat frame on the wire is 350 bytes**, whatever
the body length. The 32768 bucket is a 32862-byte frame, under the 65535 cap.
The ML-KEM-768 ciphertext is confined to the one-shot handshake prelude
(`version = 2`, 1095 bytes on the wire once per session). It is not part of a
`rat ` header: ratchet keys in a chat frame are X25519 (32 bytes), so the
hybrid transport does not widen this floor.
`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.
This hides length, not timing: one Noise session per message, retry cadence and
circuit timing are unchanged and remain accepted costs (see
`docs/THREAT_MODEL.md`).
## Handshake
Noise pattern: `Noise_IK_25519_ChaChaPoly_BLAKE2s`
@ -396,8 +465,9 @@ is a chat line `[file] name (N bytes)`, never the raw frame.
A chat send is ratcheted: the wire form of a chat message is a `rat ` frame
(see “Ratchet (v1 chat layer)”), and the plaintext inside that frame is the chat
line below. A bare chat frame with no typed prefix is still accepted inbound as
a chat line (older peers), but MUST NOT be emitted.
line below, padded to a length bucket first (see
[Length buckets](#length-buckets)). A bare chat frame with no typed prefix is
still accepted inbound as a chat line (older peers), but MUST NOT be emitted.
UTF-8 is not enforced for chat text: invalid bytes become U+FFFD (the
implementation does not drop the row). Control and bidi formatting
@ -406,7 +476,9 @@ tab and newline, DEL (0x7F), C1 U+0080–U+009F, U+202A–U+202E,
U+2066–U+2069, U+200E, U+200F, U+200B, U+200C. U+200D (ZWJ) is kept
so emoji sequences stay intact. `fil ` filenames must not contain
those characters (tab and newline included). Displayed as a line.
No length limit beyond the frame cap.
A chat line is at most 32766 bytes (the largest bucket minus the 2-byte
`pad_len` header); a longer body is a protocol error, not a queued message. See
[Length buckets](#length-buckets).
## Ratchet (v1 chat layer)
@ -441,7 +513,10 @@ ciphertext n+16 ChaCha20-Poly1305 ciphertext || tag
therefore fails closed.
- The ChaCha20-Poly1305 key is the message key from `KDF_CK`. The 12-byte nonce
is random per message, not a chain counter.
- Minimum frame (empty plaintext) is 73 bytes: 5 + 40 + 12 + 16.
- Minimum *ratchet* frame is 73 bytes: 5 + 40 + 12 + 16. A chat frame is larger
because its plaintext is padded to a bucket (see
[Length buckets](#length-buckets)); an empty chat line is a 329-byte `rat `
frame.
- A receiver MUST reject a frame without the `rat ` prefix, a version other than
`0x01`, or less than 73 bytes.
- The frame must fit the Noise body cap (65535 bytes, including the 16-byte