# Layered crypt plus KDF chain (v1 legacy, read-only) - Status: LEGACY since 0.2.0; v1 blobs stay readable, new stores write [AEAD](sec-future-crypto.md) only - Spec: SEC-01, SEC-02, SEC-03, PIN-08, D-19 - Sources: `tosv.cpp` leg_ plus derive plus crypt_buf, `archive/tosv.py` legacy, `ARCHITECTURE.md`, SPEC.md 6.3, elite report section 5 ## What it is v1 blobs were encrypted in 1 to 8 stacked layers (default 3). Layer keys derived as `K_i = leg_hash(auth concat K_{i-1})` with `K_0` 32 bytes, and each layer XORed the chunk at `(off+i) mod 32`. Decryption is the same walk: XOR is its own inverse, so store and fetch shared one primitive. No key was ever stored. M7 known-answer test proved `leg_hash` is not standard SHA-256 (T1/Ch read the wrong state words); the code is frozen byte-identical so old blobs keep decrypting. ## How TOSV does it `derive` builds the key vector from the `user:pass` string plus the seed, `crypt_buf` walks layers outer and bytes inner, and the v1 fetch path [streams](sec-streaming.md) 64KB pieces. Fetch derives from the same auth string, so wrong creds produce garbage bytes with no error oracle: the binary cannot tell wrong-key output from data. Tag dispatch (`blob_ver`) sends version-1 tags here and version-2 tags to secretstream. ## What it protects against Disk theft and blob exfiltration: stolen `.bin` files without creds are layered keystream noise. It also binds ciphertext to identity: each grant effectively re-keys the stream through its own auth. ## Attack realism High against theft, low against tampering. The realistic attacker steals files or buys a decommissioned disk; layered keystream stops both cold. The unrealistic-to-ignore attacker flips ciphertext bits: XOR malleability means flips survive decryption silently. v1 blobs keep this property until re-stored; v2 blobs fail closed instead (see [AEAD](sec-future-crypto.md)). ## Pros Tunable depth per vault. Zero stored keys to steal. No oracle: wrong creds look like data. Ten reviewable lines of primitive code. ## Cons and how the vault resolves them - Malleable, no integrity tag. Resolved by [AEAD](sec-future-crypto.md) (XChaCha20-Poly1305) for every blob stored since 0.2.0; legacy v1 blobs hold by scope honesty plus [audit](sec-audit.md) of every fetch, so silent mass-tamper still leaves a read trail. - Nonstandard construction (leg-hash-XOR, not a NIST mode, and the hash itself failed known-answer test). Resolved by freezing it for legacy read only; all new crypt uses audited libsodium primitives. - Keystream reuse if same auth plus same seed encrypts twice. Resolved by fresh `K_0` per store and per-grant auth separation; identical plaintexts under different grants share no keystream. - Length leaks. Accepted: padding to fixed sizes is tracked as a v2 option; v1 leaks no more than the filesystem already shows. ## Residual risk Malicious bit-flip tampering stays open for legacy v1 blobs until re-stored; closed for v2 blobs by AEAD (accepted ADR 0005). Accidental corruption closed in M5 by [integrity sidecars](sec-integrity.md): 500 plus audit on mismatch. M6: layer counts travel in the tag, so global layers changes never break old blobs (both generations proven in Gate 2). M7: layers inert for v2 (tags carry `0 2`); layers param kept for v1 read dispatch and DB compat. ## See also - [Security index](security.md), [memory](sec-memory.md), [streaming](sec-streaming.md), [future crypto](sec-future-crypto.md)