Planning documents only: CLAUDE.md rules, README with decisions and architecture, docs/protocol.md transcribed from the Windows source with citations, docs/topo.md, TODO.md milestones and the first-wave feature briefs (bootstrap, packet-codec, cipher-stream), plus tools/guard.sh. Co-Authored-By: Claude Fable 5.1 <[email protected]>
79 lines
3.5 KiB
Markdown
79 lines
3.5 KiB
Markdown
# cipher-stream
|
||
|
||
## Goal
|
||
|
||
The two cryptographic primitives the wire needs, in `MWBCore` with
|
||
CommonCrypto and CryptoKit only: the magic-number hash of the security
|
||
key (`docs/protocol.md` §2.1) and the per-direction AES-256-CBC stream
|
||
with its cleartext salt+IV header and sacrificial first block (§2.2–2.3).
|
||
Streaming, allocation-light, and never able to leak key material into a
|
||
log. Wire compatibility with the Windows `Encryption.cs` is the whole
|
||
point; there is no "better" cipher choice to make here.
|
||
|
||
## Assumptions
|
||
|
||
- **CryptoKit cannot do CBC**, so AES goes through `CCCryptorCreate`
|
||
with `options = 0` (no padding, CBC) and `CCCryptorUpdate` per chunk.
|
||
Zero padding on the Windows side never emits bytes because every write
|
||
is a multiple of 16; we therefore do *no* padding at all and treat a
|
||
non-multiple-of-16 total as a stream error at close.
|
||
- **PBKDF2 goes through `CCKeyDerivationPBKDF`** with
|
||
`kCCPRFHmacAlgSHA512`, 100 000 rounds, 32 bytes. Password bytes are
|
||
UTF-8 of the key string — `Rfc2898DeriveBytes.Pbkdf2(string, …)` uses
|
||
UTF-8 (documented .NET behaviour). Pinned by a Python
|
||
`hashlib.pbkdf2_hmac('sha512', …)` vector.
|
||
- **The magic hash truncates each UTF-16 code unit to a byte**
|
||
(`(byte)st[i]`, `Encryption.cs:184`) and hashes a fixed 32-byte
|
||
buffer; keys longer than 32 characters contribute only their first 32.
|
||
The `<< 23` overlap is reproduced with `UInt32` wrapping arithmetic in
|
||
the same order as the C# expression.
|
||
- **Salt and IV are fresh per direction and never reused**; Windows
|
||
tests pin uniqueness (`EncryptionTests.cs:59-68`) and so do ours.
|
||
- **The sacrificial block is part of the cipher type's contract**: the
|
||
writer emits it right after the header, the reader discards the first
|
||
16 plaintext bytes. Callers never see it (topo.md handshake 2).
|
||
- A wrong key is not detectable here — CBC has no authentication. The
|
||
reader returns garbage plaintext and the framing layer's magic/checksum
|
||
rejects it, matching Windows (`InvalidKey` after >10 bad packets).
|
||
|
||
## Handshakes
|
||
|
||
### Promised (topo.md handshake 2, to control-channel and clipboard-bulk)
|
||
|
||
```swift
|
||
public enum MagicHash { public static func magicNumber(for key: String) -> UInt32 }
|
||
public struct CipherWriter {
|
||
public init(key: String, rng: RandomBytes = .system) throws // derives, builds header + sacrificial block
|
||
public mutating func header() -> Data // 32 bytes cleartext + 16 bytes ciphertext, once
|
||
public mutating func write(_ plaintext: Data) throws -> Data // ciphertext for the full blocks now available
|
||
}
|
||
public struct CipherReader {
|
||
public init(key: String)
|
||
public mutating func read(_ ciphertext: Data) throws -> Data // consumes header on first bytes; returns plaintext after the sacrificial block
|
||
}
|
||
```
|
||
|
||
`RandomBytes` is injectable so tests can pin whole streams.
|
||
|
||
### Consumed
|
||
|
||
- Nothing from other lanes.
|
||
|
||
## Status / TODO
|
||
|
||
- [ ] `Sources/MWBCore/MagicHash.swift` + `Tests/MWBCoreTests/MagicHashTests.swift`
|
||
with `Tests/Fixtures/magic_reference.py` (run once; vector committed).
|
||
- [ ] `Sources/MWBCore/CipherStream.swift`: key derivation, writer, reader.
|
||
- [ ] `Tests/MWBCoreTests/CipherStreamTests.swift`: PBKDF2 vector;
|
||
round-trip; byte-at-a-time reader equals all-at-once; unique
|
||
header per instance; wrong key ⇒ garbage without throwing;
|
||
descriptions omit secrets.
|
||
- [ ] Note in `docs/perf/README.md`: PBKDF2 at 100 000 rounds SHA-512
|
||
costs ~N ms per connection open (measure; it is per direction).
|
||
|
||
## Work log
|
||
|
||
## QA log
|
||
|
||
## Review
|