wire-core
Goal
The bytes of the protocol as Swift value types in MWBCore, byte-exact
with docs/protocol.md §1–§2: the 32/64-byte DATA packet and its
union views, the PackageType numbering and big/small rule, the ASCII
machine name, the magic + checksum framing that overwrites bytes 1–3,
the magic-number hash of the security key, PBKDF2 key derivation, and
the per-direction AES-256-CBC stream with its cleartext salt+IV header
and sacrificial first block. No I/O. Every later lane reads and writes
the wire through this code, and wire compatibility with the unmodified
Windows build is the whole point (CLAUDE.md invariant 1).
Assumptions
- Little-endian, natural alignment, no padding, because the Windows
side marshals an explicit-layout struct (
DATA.cs:135-149). Offsets come fromdocs/protocol.md§1.1, not recomputed. Typeis one byte on the wire. Stored asUInt8at offset 0;Framing.openzeroes bytes 1–3 exactly asProcessReceivedDataExdoes (SocketStuff.cs:531-556).- The checksum covers bytes 2..31 only, even on 64-byte packets
(
SocketStuff.cs:510-513). The cipher, not the checksum, protects the name half. Say so in a comment at the loop. - Unknown type values decode as
.unknown(UInt8), not an error, so a newer Windows build cannot desynchronise framing; their size follows the0x80rule. - Machine names are ASCII, ≤ 32, space-padded (
Common.cs:397-405,DATA.cs:75-94); non-ASCII is a construction error; decode trims trailing spaces and NULs. - CryptoKit cannot do CBC, so AES is
CCCryptorCreatewithoptions = 0(CBC, no padding) andCCCryptorUpdateper chunk. Zero padding on Windows never emits bytes because every write is a multiple of 16; we do no padding. - PBKDF2 is
CCKeyDerivationPBKDFwithkCCPRFHmacAlgSHA512, 100 000 rounds, 32 bytes, password = UTF-8 of the key (Encryption.cs:107-115). - The magic hash truncates each UTF-16 unit to a byte and hashes a
fixed 32-byte buffer (
Encryption.cs:180-186); the<< 23overlap is reproduced withUInt32wrapping arithmetic in the C# order. - Salt and IV are fresh per direction; the sacrificial block is
inside the cipher types' contract: the writer emits it after the
header, the reader discards the first 16 plaintext bytes
(
Common.cs:1528-1558). - A wrong key is not detectable here (no MAC). The reader returns
garbage and framing rejects it, matching Windows (
InvalidKeyafter more than 10 bad packets).
Handshakes
Promised (topo.md handshakes 1 and 2, to session-matrix, control-channel, clipboard)
public enum PackageType: RawRepresentable, Sendable { case hi, hello, …, unknown(UInt8); var rawValue: UInt8; var isBig: Bool }
public struct Packet: Sendable {
public var type: PackageType; public var id: Int32; public var src: UInt32; public var des: UInt32
public var mouse: MouseData // x, y, wheelDelta, flags at 16..31
public var keyboard: KeyboardData // dateTime at 16, vk at 24, flags at 28
public var nonce: (UInt32, UInt32, UInt32, UInt32)
public var postAction: UInt32
public var machineName: String? // nil on small packets
public var payload48: Data // bytes 16..63 for clipboard chunks
public init(type:) // zeroed buffer of the right size
public init?(bytes: Data) // 32 or 64 bytes, no framing check
public var bytes: Data // 32 or 64
}
public enum Framing {
public static func seal(_ p: Packet, magic: UInt32) -> Data
public static func open(_ bytes: Data, magic: UInt32) -> Packet // .invalid on bad framing
}
public enum MagicHash { public static func magicNumber(for key: String) -> UInt32 }
public enum SecurityKey { public static func isValid(_:) -> Bool; public static func random() -> String; public static func displayText(_:) -> String }
public struct CipherWriter { public init(key: String) throws; public mutating func header() -> Data; public mutating func write(_ plaintext: Data) throws -> Data }
public struct CipherReader { public init(key: String); public mutating func read(_ ciphertext: Data) throws -> Data }
Exact spelling may change during the lane; this README is updated in the same change and later lanes code against it.
Consumed
- Nothing. Foundation, CommonCrypto, CryptoKit only.
Status / TODO
Sources/MWBCore/Packet.swift:PackageType(every value §1.2, cited),isBig,Packetwith union views, machine name rules.Sources/MWBCore/Framing.swift:seal/openper §1.3, each constant cited toSocketStuff.cs.Sources/MWBCore/MagicHash.swift:magicNumber(for:)§2.1 cited toEncryption.cs:173-200;SecurityKeyrules §2.4 (Encryption.cs:207-242,:65-79).Sources/MWBCore/CipherStream.swift: PBKDF2,CipherWriter,CipherReader(header, sacrificial block, streaming CBC), cited toEncryption.csandCommon.cs:1528-1558; the CommonCrypto wrapper lives in this one file.docs/protocol.md: add anything learned while porting, cited.