From 76fd4366d266fcdfe888057c944ce202887b78b8 Mon Sep 17 00:00:00 2001 From: Guoliang Chen Date: Wed, 16 Sep 2026 00:11:36 -0700 Subject: [PATCH] protocol: wire-core lane notes (invalid packets are 64 bytes, magic overflow, 48-byte preamble) --- docs/protocol.md | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/docs/protocol.md b/docs/protocol.md index f013be9..dccbc8f 100644 --- a/docs/protocol.md +++ b/docs/protocol.md @@ -450,3 +450,57 @@ salt/IV per stream (`EncryptionTests.cs`), received-file staging **No tests exist for packet layout, framing, handshake, matrix logic, coordinate normalization or clipboard chunking** — the port has no upstream oracle for those and must build its own (`interop-vectors`). + +--- + +## 10. Lane notes + +Findings a lane made while porting, kept here so the next reader of §1–§9 +sees them. One subsection per lane, appended, never rewritten. + +### 10.1 wire-core (2026-09-16) + +1. **A rejected packet still consumes 64 bytes.** `Error` (0xFE) and + `Invalid` (0xFF) both have the `Matrix` bit 0x80 set, so + `DATA.IsBigPackage` (`DATA.cs:131`) is true for them. The receive loop + builds the `DATA` *after* `ProcessReceivedDataEx` has overwritten byte 0 + with 0xFF, then reads 32 more bytes because the packet "is big" + (`SocketStuff.cs:578-600`). A receiver that treats a framing failure as a + 32-byte event desynchronises against a real Windows sender that has a + 64-byte packet in flight. `PackageType.isBig` reproduces this. +2. **`Type == 0` throws on Windows** (`DATA.cs:126-127`). We decode it as a + small `.unknown(0)` instead: throwing inside a receive path would be a + worse failure than dropping one packet, and 0 is not a legal type. +3. **The magic-number expression overflows `int` and is meant to.** + `(hashValue[0] << 23) + (hashValue[1] << 16) + ...` (`Encryption.cs:199`) + reaches 2 155 806 720 for large digest bytes, past `int.MaxValue`, in C#'s + default unchecked context; it wraps and is then reinterpreted by the + `(uint)` cast. Reproduce with wrapping 32-bit addition — `&+` in Swift, not + `+`, which would trap. **[verified]** against an independent SHA-512 + implementation: key `abcdefghijklmnop` ⇒ magic `334517251` (0x13EC_4F03). +4. **The stream preamble is 48 bytes, not 32.** 16-byte salt and 16-byte IV in + the clear (`Encryption.cs:124-129`), then one 16-byte *ciphertext* block — + the sacrificial block is random plaintext written *through* the cipher + (`Common.cs:1536-1537` on the stream returned by `GetEncryptedStream`, + wired at `SocketStuff.cs:105, 119`). The reader discards the first 16 bytes + of plaintext, not of ciphertext. +5. **PBKDF2's password is the UTF-8 encoding of the key.** .NET's + `Rfc2898DeriveBytes.Pbkdf2(string, ...)` (`Encryption.cs:109`) documents + UTF-8 for the string overload. **[verified]** against an independent + PBKDF2-HMAC-SHA512: key `abcdefghijklmnop`, salt 16×0x01, 100 000 rounds ⇒ + `6c30ca8b15c27f6e…14ae2a4da14728e1`. The whole reader path (48-byte + preamble, AES-256-CBC, sacrificial discard) was checked against a stream + produced by `openssl enc -aes-256-cbc -nopad`. +6. **The security key is whitespace-stripped where it is typed**, not where it + is validated: `Regex.Replace(text, @"\s+", "")` + (`Form/Settings/SetupPage2a.cs:92`). `IsKeyValid` only measures length + (`Encryption.cs:235-242`), which is why the grouped display form + (`Encryption.cs:65-79`, four characters then two spaces) can be copied + between machines. +7. **`MachineName` is trimmed with `String.Trim()`** (`DATA.cs:83`), which + strips whitespace but *not* NUL. Windows pads with spaces + (`DATA.cs:88`), so this only matters for a zero-filled field; we trim NUL + as well so such a field reads as empty. +8. **The 48-byte clipboard chunk starts at offset 16**, i.e. + `PACKAGE_SIZE_EX - DATA_SIZE` (`Clipboard.cs:46, 274`), and the last chunk + is zero-filled by clearing the whole buffer first (`Clipboard.cs:281`).