C1-crypto-notes.md
C1 — Crypto & Model Deltas (Recovery Key Escrow)
This file describes the EXACT data-model and crypto changes the orchestrator
must apply to the real source. It does not edit the shared files itself.
Everything here is reconciled against the real VaultCrypto.kt, VaultStore.kt,
RecoveryWords.kt and VaultRepository.kt (read v2.1.38).
0. Where the brief is wrong (read this first)
The brief invents several APIs. Real ground truth:
| Brief assumed | Reality |
|---|---|
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. |
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) |
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. |
secureRandom(16) helper |
No such helper. Use VaultCrypto.generateSalt() (16 bytes) or VaultCrypto.generateNoise(). |
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. |
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. |
blake3(...) / recoveryWordsHash to add |
recoveryWordsHash(words) already exists in RecoveryWords.kt. setRecoveryWords already stores it. |
1. What "the vault master key" actually is here
There are THREE distinct keys in play; the brief conflates them:
derivedKey— Argon2id(password, slotA.salt). Wraps slot A. Rotates on everychangePassword. NOT stable.rootKey(VaultPayload.rootKeyB64) — the stable 32-byte sync master key. Stable across password changes. This is what the 12 recovery words are DERIVED from (rootKeyToWords). But it is NOT the key that decrypts slot A.- The slot-A plaintext (
VaultPayloadJSON: entries, rootKey, flags). This is what we ultimately need to recover a vault and re-seal it under a new password.
The recovery flow needs to produce a usable, unlocked session AND a re-wrapped slot A under the new password. That requires the slot-A plaintext, not just the rootKey. So the escrow seals the slot-A plaintext envelope.
Why not escrow
derivedKey? It rotates on password change, so the escrow would silently go stale (brief §3b's own concern). Why not escrow onlyrootKey? rootKey alone cannot decrypt slot A — you would still be locked out of the entries. Sealing the plaintext envelope is the only choice that makes recovery actually unlock the vault without the password.
2. Data-model delta — add the escrow to StoredVault
The escrow MUST be readable without the password (that is the entire point), so
it cannot live inside the AES-GCM blob. The clean home is a new nullable
field on StoredVault (the same record writeSlotA already persists, and whose
salt is already plaintext at rest). Being nullable + ignoreUnknownKeys keeps
old vaults and the JSON codec backward compatible.
Apply this delta to VaultStore.kt:
@Serializable
data class StoredVault(
val version: Int = 1,
val salt: String,
val blob: String,
val recoveryEscrow: RecoveryEscrow? = null, // NEW — null until recovery words set
)
/**
* A second, password-independent copy of the slot-A plaintext envelope, sealed
* under a key derived from the 12 BIP39 recovery words. Lets the vault be
* recovered (and re-wrapped under a new password) without the old password.
*
* [salt] (base64, 16B) is the HKDF salt for deriveRecoveryKey.
* [blob] (base64) is VaultCrypto.encrypt(slotAPlaintext, recoveryKey, AAD_RECOVERY)
* i.e. nonce(12) || AES-256-GCM ciphertext||tag.
*/
@Serializable
data class RecoveryEscrow(
val salt: String,
val blob: String,
)
Because StoredVault is @Serializable and VaultCodec uses
ignoreUnknownKeys = true; encodeDefaults = true, both encode and decode
continue to work; a vault written before C1 simply decodes with
recoveryEscrow = null.
Note for the decoy/duress slot: it is also a
StoredVault, so it will gain a (always-null)recoveryEscrowfield too. That is harmless — recovery is never offered for the decoy. Leave it null.
3. Add a recovery AAD constant
Add to VaultCrypto.kt alongside the other AAD constants:
const val AAD_RECOVERY = "recovery_escrow_v1"
This is the AAD passed to encrypt/decrypt for the escrow blob. It is
distinct from AAD_SLOT_A so an escrow blob can never be confused with /
substituted for a slot-A blob.
4. Seal / open the escrow — the real calls
deriveRecoveryKey already exists and takes the 16-byte entropy material,
not the joined string. The entropy is obtained from the words via
wordsToRootKeyMaterial(words) (which also validates the BIP39 checksum and
throws on a bad phrase). So:
Seal (in setRecoveryWords)
// payloadPlain: ByteArray = VaultCodec.encode(currentPayload()) // slot-A plaintext
val material = wordsToRootKeyMaterial(words) // 16B entropy (throws if checksum bad)
val salt = VaultCrypto.generateSalt() // 16B
val recoveryKey = deriveRecoveryKey(material, salt) // SecretKeySpec (AES-256, HKDF)
val sealed = VaultCrypto.encrypt(payloadPlain, recoveryKey.encoded, VaultCrypto.AAD_RECOVERY)
val escrow = RecoveryEscrow(salt = b64(salt), blob = b64(sealed))
VaultCrypto.zero(material)
VaultCrypto.zero(recoveryKey.encoded) // SecretKeySpec.encoded returns a copy; zero it
VaultCrypto.zero(payloadPlain)
deriveRecoveryKeyreturns aSecretKeySpec;VaultCrypto.encryptwants aByteArraykey. UserecoveryKey.encoded(a fresh copy of the 32 key bytes) and zero it afterwards.
Open (in unlockWithRecoveryWords)
val escrow = storedVault.recoveryEscrow ?: return null
val material = try { wordsToRootKeyMaterial(words) } catch (_: Exception) { return null }
val recoveryKey = deriveRecoveryKey(material, unb64(escrow.salt))
return try {
val payloadPlain = VaultCrypto.decrypt(unb64(escrow.blob), recoveryKey.encoded, VaultCrypto.AAD_RECOVERY)
// payloadPlain is the slot-A VaultPayload JSON — decode + adopt it as the live session
VaultCodec.decode(payloadPlain)
} catch (e: javax.crypto.AEADBadTagException) {
null // wrong words (GCM tag mismatch)
} finally {
VaultCrypto.zero(material)
VaultCrypto.zero(recoveryKey.encoded)
}
wordsToRootKeyMaterialthrowsIllegalArgumentExceptionon a bad checksum or unknown word; a GCM mismatch throwsAEADBadTagException. Both mean "wrong words" → return null. The repo method inC1-repo-additions.ktcatches both.
5. Password-change re-seal (brief §3b)
The brief's §3b reasoning is WRONG for this codebase: it assumes the escrow seals
"the vault master key" which doesn't change. Here the escrow seals the slot-A
plaintext, and that plaintext is logically unchanged by a password change
(same entries, same rootKey) — changePassword only re-wraps it under a new
Argon2id key. Therefore the existing recoveryEscrow remains valid after a
password change with no re-seal needed — as long as changePassword PRESERVES
the existing recoveryEscrow field when it writes the new StoredVault.
The real changePassword builds a brand-new StoredVault(version, salt, blob)
and drops any extra fields. So the ONE required change is to carry the escrow
forward. See the exact edit in C1-repo-additions.kt §3b. (Recovery words are
derived from rootKey, which changePassword preserves inside the re-encrypted
payload, so the words themselves also stay valid — consistent with the existing
recovery_words_set_verify_and_persist behaviour.)