/* * D13 — LockViewModel.unlock duress integration (SNIPPET, NOT a drop-in file). * * This is the precise change for `LockViewModel.unlock(password)`. The * orchestrator integrates it by hand — D13 is the only item touching * LockViewModel, so it must merge cleanly with whatever else has landed. * * KEY DESIGN POINT * ---------------- * The duress check happens BEFORE `vault.unlock(password)`. The duress password * is verified against DuressConfigStore (a separate EncryptedSharedPreferences * file), NOT against slot A — it must be checkable without the real master * password. On a match we never touch the real vault's derived key. * * The brief says "check derived key against duress hash". We deliberately verify * the PASSWORD, not the slot-A-derived key: the duress salt differs from the * vault salt, so the Argon2id outputs are not comparable. DuressConfigStore.save * stores its own salt; DuressConfigStore.verify re-derives over that salt. This * is the only correct way to compare two independently-salted Argon2id secrets. * * NEW CONSTRUCTOR DEP (Hilt-injected): * private val duress: com.peachpasswords.vault.DuressConfigStore, * * NEW UI-STATE FLAGS on LockUiState: * val duressDestroyed: Boolean = false, // real vault wiped → route to CreateVault * // decoy success reuses the existing `unlocked = true` path. */ // ─── replace the body of unlock(password) with this ─────────────────────────── fun unlock(password: String) { if (password.isBlank() || _state.value.loading) return _state.update { it.copy(loading = true, error = null) } viewModelScope.launch { // Duress check first: a matching duress password never reaches the real // vault. Verify is a full Argon2id derivation, so keep it inside the // coroutine like the normal unlock. when (val d = duress.verify(password)) { is DuressMatch.Match -> { handleDuress(d.action) return@launch } DuressMatch.None -> Unit // fall through to the real unlock } when (val r = vault.unlock(password)) { is UnlockResult.Success -> _state.update { it.copy(loading = false, unlocked = true) } is UnlockResult.WrongPassword -> _state.update { it.copy(loading = false, error = "incorrect password", shakeNonce = it.shakeNonce + 1) } is UnlockResult.Error -> _state.update { it.copy(loading = false, error = r.reason, shakeNonce = it.shakeNonce + 1) } } } } /** * Carry out the configured duress action. * * - DECOY: unlock into the decoy vault slot. The real slot A is left * untouched and hidden. See VaultRepository.unlockDecoy (new). * - DESTROY: irreversibly wipe the real vault, then route to CreateVault by * clearing hasVault. destroyVault() preserves the Pro license. * * To an observer under coercion both paths look like an ordinary successful (or * fresh-install) unlock — no error, no shake. */ private suspend fun handleDuress(action: DuressAction) { when (action) { DuressAction.DECOY -> { when (vault.unlockDecoy()) { is UnlockResult.Success -> _state.update { it.copy(loading = false, unlocked = true) } // If the decoy can't open we still must not reveal the real vault; // present a generic incorrect-password to avoid an oracle. else -> _state.update { it.copy(loading = false, error = "incorrect password", shakeNonce = it.shakeNonce + 1) } } } DuressAction.DESTROY -> { vault.destroyVault() duress.clear() // the duress config dies with the vault it protected _state.update { it.copy(loading = false, hasVault = false, duressDestroyed = true) } } } } /* * REQUIRED IMPORTS to add to LockViewModel.kt: * import com.peachpasswords.vault.DuressAction * import com.peachpasswords.vault.DuressConfigStore * import com.peachpasswords.vault.DuressMatch */