LockViewModel_unlock_integration.kt

4.2 KB · raw

1/*2 * D13 — LockViewModel.unlock duress integration (SNIPPET, NOT a drop-in file).3 *4 * This is the precise change for `LockViewModel.unlock(password)`. The5 * orchestrator integrates it by hand — D13 is the only item touching6 * LockViewModel, so it must merge cleanly with whatever else has landed.7 *8 * KEY DESIGN POINT9 * ----------------10 * The duress check happens BEFORE `vault.unlock(password)`. The duress password11 * is verified against DuressConfigStore (a separate EncryptedSharedPreferences12 * file), NOT against slot A — it must be checkable without the real master13 * password. On a match we never touch the real vault's derived key.14 *15 * The brief says "check derived key against duress hash". We deliberately verify16 * the PASSWORD, not the slot-A-derived key: the duress salt differs from the17 * vault salt, so the Argon2id outputs are not comparable. DuressConfigStore.save18 * stores its own salt; DuressConfigStore.verify re-derives over that salt. This19 * is the only correct way to compare two independently-salted Argon2id secrets.20 *21 * NEW CONSTRUCTOR DEP (Hilt-injected):22 *   private val duress: com.peachpasswords.vault.DuressConfigStore,23 *24 * NEW UI-STATE FLAGS on LockUiState:25 *   val duressDestroyed: Boolean = false,  // real vault wiped → route to CreateVault26 *   // decoy success reuses the existing `unlocked = true` path.27 */2829// ─── replace the body of unlock(password) with this ───────────────────────────3031fun unlock(password: String) {32    if (password.isBlank() || _state.value.loading) return33    _state.update { it.copy(loading = true, error = null) }34    viewModelScope.launch {35        // Duress check first: a matching duress password never reaches the real36        // vault. Verify is a full Argon2id derivation, so keep it inside the37        // coroutine like the normal unlock.38        when (val d = duress.verify(password)) {39            is DuressMatch.Match -> {40                handleDuress(d.action)41                return@launch42            }43            DuressMatch.None -> Unit // fall through to the real unlock44        }4546        when (val r = vault.unlock(password)) {47            is UnlockResult.Success -> _state.update { it.copy(loading = false, unlocked = true) }48            is UnlockResult.WrongPassword -> _state.update {49                it.copy(loading = false, error = "incorrect password", shakeNonce = it.shakeNonce + 1)50            }51            is UnlockResult.Error -> _state.update {52                it.copy(loading = false, error = r.reason, shakeNonce = it.shakeNonce + 1)53            }54        }55    }56}5758/**59 * Carry out the configured duress action.60 *61 *  - DECOY:   unlock into the decoy vault slot. The real slot A is left62 *             untouched and hidden. See VaultRepository.unlockDecoy (new).63 *  - DESTROY: irreversibly wipe the real vault, then route to CreateVault by64 *             clearing hasVault. destroyVault() preserves the Pro license.65 *66 * To an observer under coercion both paths look like an ordinary successful (or67 * fresh-install) unlock — no error, no shake.68 */69private suspend fun handleDuress(action: DuressAction) {70    when (action) {71        DuressAction.DECOY -> {72            when (vault.unlockDecoy()) {73                is UnlockResult.Success -> _state.update { it.copy(loading = false, unlocked = true) }74                // If the decoy can't open we still must not reveal the real vault;75                // present a generic incorrect-password to avoid an oracle.76                else -> _state.update {77                    it.copy(loading = false, error = "incorrect password", shakeNonce = it.shakeNonce + 1)78                }79            }80        }81        DuressAction.DESTROY -> {82            vault.destroyVault()83            duress.clear() // the duress config dies with the vault it protected84            _state.update {85                it.copy(loading = false, hasVault = false, duressDestroyed = true)86            }87        }88    }89}9091/*92 * REQUIRED IMPORTS to add to LockViewModel.kt:93 *   import com.peachpasswords.vault.DuressAction94 *   import com.peachpasswords.vault.DuressConfigStore95 *   import com.peachpasswords.vault.DuressMatch96 */