0007: bounded per-blob key cache
- Status: accepted (implemented 2026-10-31, SPEC 0.3.0)
- Date: 2026-10-31
- Decision makers: retoor
- Spec IDs: D-20, SEC-02, NFR-04
Context
The M8 soak proved 64 concurrent fetches queue behind the serialized Argon2id KDF (median 4.0s, zero errors). Lowering KDF cost would weaken offline-guessing resistance for everyone to speed up a load case. Memoizing derived keys within a bounded cryptoperiod keeps full KDF strength per guess while removing the repeated cost. Research: key-cache report.
Decision (accepted)
Cache v2 fetch keys in process: index by blob path, entry holds
header salt plus a sodium_malloc 32B key plus last-use time.
Bounds: TTL 300s (codebase window idiom), cap 128 LRU (64-conn
worst case twice over), wipe-on-evict via sodium_free, flush
on duress wipe, expiry swept on every insert (no new thread).
Lookup validates TTL plus salt match against the freshly read
header, so a two-way overwrite can never serve a stale key.
Double-checked insert (lookup, unlock, KDF, re-lookup, insert);
salt change during KDF fails closed exactly like the existing
no-cache race. The cache memoizes the KDF only: verification
pre-pass still runs on every fetch. Store path never consults
(fresh salt always misses); v1 never cached (microsecond KDF).
Consequences
Good: 64-user soak median 4.0s to 6ms, p95 8ms, throughput 10 to 68 req/s, server RSS avg 235MB to 31MB, zero failures throughout. Per-guess KDF cost unchanged for attackers.
Bad: derived keys live up to 300s in RAM instead of milliseconds (accepted: locked, guarded, wiped, documented cryptoperiod). One-time thundering herd per blob per TTL window (accepted: bounded, then hits).
Links
- Key-cache research, memory, shipped crypto
- SPEC.md SEC-02, NFR-04, D-20