1# C1 — Crypto & Model Deltas (Recovery Key Escrow)23This file describes the EXACT data-model and crypto changes the orchestrator4must apply to the real source. It does **not** edit the shared files itself.5Everything here is reconciled against the real `VaultCrypto.kt`, `VaultStore.kt`,6`RecoveryWords.kt` and `VaultRepository.kt` (read v2.1.38).78---910## 0. Where the brief is wrong (read this first)1112The brief invents several APIs. Real ground truth:1314| Brief assumed | Reality |15|---|---|16| `data class StoredVault(slotA, slotDuress, recoveryEscrow, …)` | `StoredVault` is just `(version: Int, salt: String, blob: String)` — ONE encrypted slot. The duress/decoy slot is a **separate** `StoredVault` under a different prefs key (`KEY_SLOT_DURESS`), not a field. There is no `slotA`/`slotDuress`/`recoveryEscrow` field. |17| `VaultCrypto.encryptRaw` / `decryptRaw` / `encryptSlot` | Do not exist. Real API is `encrypt(plaintext, key, aad): ByteArray` and `decrypt(data, key, aad): ByteArray`. Both manage a 12-byte prepended nonce; `encrypt` output is `nonce(12) || ciphertext||tag`. AAD is a **required** `String` arg. |18| `deriveRecoveryKey(wordMaterial: String, salt)` using **Argon2id** via `VaultCrypto.deriveKey` | `deriveRecoveryKey` ALREADY EXISTS in `RecoveryWords.kt`, signature `deriveRecoveryKey(wordMaterial: ByteArray, salt: ByteArray): SecretKeySpec`, and it uses **HKDF-SHA256** with info `"peach-recovery-v1"`, NOT Argon2id. The `wordMaterial` is the 16-byte entropy from `wordsToRootKeyMaterial(words)`, NOT the joined word string. |19| `secureRandom(16)` helper | No such helper. Use `VaultCrypto.generateSalt()` (16 bytes) or `VaultCrypto.generateNoise()`. |20| `currentVaultKey()` is the thing to escrow | `currentVaultKey()` returns the **Argon2id-derived password key** (`derivedKey`), which ROTATES on every password change. Escrowing it would break after a password change. We must escrow something password-independent. See §2. |21| BIP39 list lives in `res/raw/bip39_english.txt` | It does **not**. The 2048-word list is a Kotlin `val WORD_LIST` in `RecoveryWordList.kt`. (Note quirk: it has 2052 entries because index 0 and 1 are both `"abandon"` — `rootKeyToWords`/`wordsToRootKeyMaterial` already account for this; do not "fix" it.) Helpers `getWordSuggestions`, `findInvalidWords`, `wordsToRootKeyMaterial` (checksum-validating) already exist. |22| `blake3(...)` / `recoveryWordsHash` to add | `recoveryWordsHash(words)` already exists in `RecoveryWords.kt`. `setRecoveryWords` already stores it. |2324---2526## 1. What "the vault master key" actually is here2728There are THREE distinct keys in play; the brief conflates them:29301. **`derivedKey`** — Argon2id(password, slotA.salt). Wraps slot A. Rotates on31 every `changePassword`. NOT stable.322. **`rootKey`** (`VaultPayload.rootKeyB64`) — the stable 32-byte sync master key.33 Stable across password changes. This is what the 12 recovery words are34 DERIVED from (`rootKeyToWords`). But it is NOT the key that decrypts slot A.353. The **slot-A plaintext** (`VaultPayload` JSON: entries, rootKey, flags). This36 is what we ultimately need to recover a vault and re-seal it under a new37 password.3839The recovery flow needs to produce a usable, unlocked session AND a re-wrapped40slot A under the new password. That requires the **slot-A plaintext**, not just41the rootKey. So the escrow seals the slot-A plaintext envelope.4243> Why not escrow `derivedKey`? It rotates on password change, so the escrow44> would silently go stale (brief §3b's own concern). Why not escrow only45> `rootKey`? rootKey alone cannot decrypt slot A — you would still be locked46> out of the entries. Sealing the plaintext envelope is the only choice that47> makes recovery actually unlock the vault without the password.4849---5051## 2. Data-model delta — add the escrow to `StoredVault`5253The escrow MUST be readable without the password (that is the entire point), so54it cannot live inside the AES-GCM `blob`. The clean home is a new **nullable**55field on `StoredVault` (the same record `writeSlotA` already persists, and whose56`salt` is already plaintext at rest). Being nullable + `ignoreUnknownKeys` keeps57old vaults and the JSON codec backward compatible.5859Apply this delta to `VaultStore.kt`:6061```kotlin62@Serializable63data class StoredVault(64 val version: Int = 1,65 val salt: String,66 val blob: String,67 val recoveryEscrow: RecoveryEscrow? = null, // NEW — null until recovery words set68)6970/**71 * A second, password-independent copy of the slot-A plaintext envelope, sealed72 * under a key derived from the 12 BIP39 recovery words. Lets the vault be73 * recovered (and re-wrapped under a new password) without the old password.74 *75 * [salt] (base64, 16B) is the HKDF salt for deriveRecoveryKey.76 * [blob] (base64) is VaultCrypto.encrypt(slotAPlaintext, recoveryKey, AAD_RECOVERY)77 * i.e. nonce(12) || AES-256-GCM ciphertext||tag.78 */79@Serializable80data class RecoveryEscrow(81 val salt: String,82 val blob: String,83)84```8586Because `StoredVault` is `@Serializable` and `VaultCodec` uses87`ignoreUnknownKeys = true; encodeDefaults = true`, both encode and decode88continue to work; a vault written before C1 simply decodes with89`recoveryEscrow = null`.9091> Note for the decoy/duress slot: it is also a `StoredVault`, so it will gain a92> (always-null) `recoveryEscrow` field too. That is harmless — recovery is never93> offered for the decoy. Leave it null.9495---9697## 3. Add a recovery AAD constant9899Add to `VaultCrypto.kt` alongside the other AAD constants:100101```kotlin102const val AAD_RECOVERY = "recovery_escrow_v1"103```104105This is the AAD passed to `encrypt`/`decrypt` for the escrow blob. It is106distinct from `AAD_SLOT_A` so an escrow blob can never be confused with /107substituted for a slot-A blob.108109---110111## 4. Seal / open the escrow — the real calls112113`deriveRecoveryKey` already exists and takes the **16-byte entropy** material,114not the joined string. The entropy is obtained from the words via115`wordsToRootKeyMaterial(words)` (which also validates the BIP39 checksum and116throws on a bad phrase). So:117118### Seal (in setRecoveryWords)119120```kotlin121// payloadPlain: ByteArray = VaultCodec.encode(currentPayload()) // slot-A plaintext122val material = wordsToRootKeyMaterial(words) // 16B entropy (throws if checksum bad)123val salt = VaultCrypto.generateSalt() // 16B124val recoveryKey = deriveRecoveryKey(material, salt) // SecretKeySpec (AES-256, HKDF)125val sealed = VaultCrypto.encrypt(payloadPlain, recoveryKey.encoded, VaultCrypto.AAD_RECOVERY)126val escrow = RecoveryEscrow(salt = b64(salt), blob = b64(sealed))127VaultCrypto.zero(material)128VaultCrypto.zero(recoveryKey.encoded) // SecretKeySpec.encoded returns a copy; zero it129VaultCrypto.zero(payloadPlain)130```131132> `deriveRecoveryKey` returns a `SecretKeySpec`; `VaultCrypto.encrypt` wants a133> `ByteArray` key. Use `recoveryKey.encoded` (a fresh copy of the 32 key bytes)134> and zero it afterwards.135136### Open (in unlockWithRecoveryWords)137138```kotlin139val escrow = storedVault.recoveryEscrow ?: return null140val material = try { wordsToRootKeyMaterial(words) } catch (_: Exception) { return null }141val recoveryKey = deriveRecoveryKey(material, unb64(escrow.salt))142return try {143 val payloadPlain = VaultCrypto.decrypt(unb64(escrow.blob), recoveryKey.encoded, VaultCrypto.AAD_RECOVERY)144 // payloadPlain is the slot-A VaultPayload JSON — decode + adopt it as the live session145 VaultCodec.decode(payloadPlain)146} catch (e: javax.crypto.AEADBadTagException) {147 null // wrong words (GCM tag mismatch)148} finally {149 VaultCrypto.zero(material)150 VaultCrypto.zero(recoveryKey.encoded)151}152```153154> `wordsToRootKeyMaterial` throws `IllegalArgumentException` on a bad checksum or155> unknown word; a GCM mismatch throws `AEADBadTagException`. Both mean "wrong156> words" → return null. The repo method in `C1-repo-additions.kt` catches both.157158---159160## 5. Password-change re-seal (brief §3b)161162The brief's §3b reasoning is WRONG for this codebase: it assumes the escrow seals163"the vault master key" which doesn't change. Here the escrow seals the slot-A164**plaintext**, and that plaintext is logically unchanged by a password change165(same entries, same rootKey) — `changePassword` only re-wraps it under a new166Argon2id key. **Therefore the existing `recoveryEscrow` remains valid after a167password change with no re-seal needed** — as long as `changePassword` PRESERVES168the existing `recoveryEscrow` field when it writes the new `StoredVault`.169170The real `changePassword` builds a brand-new `StoredVault(version, salt, blob)`171and drops any extra fields. So the ONE required change is to carry the escrow172forward. See the exact edit in `C1-repo-additions.kt` §3b. (Recovery words are173derived from `rootKey`, which `changePassword` preserves inside the re-encrypted174payload, so the words themselves also stay valid — consistent with the existing175`recovery_words_set_verify_and_persist` behaviour.)