package com.peachpasswords.vault import android.content.Context import androidx.security.crypto.EncryptedSharedPreferences import androidx.security.crypto.MasterKey import dagger.hilt.android.qualifiers.ApplicationContext import org.json.JSONObject import java.util.Base64 import javax.inject.Inject import javax.inject.Singleton /** * The action Peach takes when the duress password is entered at the lock screen. * * - [DECOY] open a separate empty/minimal "decoy" vault; the real vault stays * sealed and hidden. * - [DESTROY] irreversibly wipe the real vault, then drop to CreateVault. * * Persisted as the lowercase [wire] string so a future schema migration sees a * stable token, never an ordinal. */ enum class DuressAction(val wire: String) { DECOY("decoy"), DESTROY("destroy"); companion object { fun fromWire(s: String?): DuressAction = entries.firstOrNull { it.wire == s } ?: DECOY } } /** * What [DuressConfigStore.verify] reports for an entered lock-screen password. * [None] means "not the duress password" — the caller falls through to the * normal vault unlock path. The verifier never decides policy; it only matches. */ sealed interface DuressMatch { data object None : DuressMatch data class Match(val action: DuressAction) : DuressMatch } /** * Stores the duress password as an Argon2id hash (never plaintext) plus the * selected [DuressAction], in a dedicated EncryptedSharedPreferences file under * the single key "duress_config". * * The hash is the Argon2id output of the duress password over a freshly-minted * 16-byte salt, using the SAME parameters as the vault KDF ([VaultCrypto]). The * salt is stored alongside the hash (base64) — it is not secret; it only forces * the verifier to re-derive rather than compare a precomputed digest. This * mirrors the at-rest model of slot A, whose salt is likewise in the clear. * * Why a separate prefs file (not the slot-A payload): the duress password must be * verifiable BEFORE the real vault is unlocked. It cannot live inside the * AES-GCM blob that only the real master password can open. The license store * uses the same EncryptedSharedPreferences pattern (see LicenseRepository). */ @Singleton class DuressConfigStore @Inject constructor( @ApplicationContext context: Context, ) { private val masterKey = MasterKey.Builder(context) .setKeyScheme(MasterKey.KeyScheme.AES256_GCM) .build() private val prefs = EncryptedSharedPreferences.create( context, PREFS_FILE, masterKey, EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM, ) fun isConfigured(): Boolean = prefs.contains(KEY_DURESS_CONFIG) fun storedAction(): DuressAction? = load()?.let { DuressAction.fromWire(it.optString(FIELD_ACTION)) } /** * Hash [password] with Argon2id over a fresh salt and persist {salt, hash, * action}. Overwrites any prior duress config. Returns false if the value * could not be committed. */ fun save(password: String, action: DuressAction): Boolean { val salt = VaultCrypto.generateSalt() val hash = VaultCrypto.deriveKey(password, salt) val json = JSONObject().apply { put(FIELD_VERSION, 1) put(FIELD_SALT, b64(salt)) put(FIELD_HASH, b64(hash)) put(FIELD_ACTION, action.wire) } VaultCrypto.zero(hash) return prefs.edit().putString(KEY_DURESS_CONFIG, json.toString()).commit() } fun clear(): Boolean = prefs.edit().remove(KEY_DURESS_CONFIG).commit() /** * Check [password] against the stored duress hash. Re-derives the Argon2id * key over the stored salt and constant-time-compares against the stored * hash. Returns [DuressMatch.None] when no config exists or the password is * not the duress password. */ fun verify(password: String): DuressMatch { val json = load() ?: return DuressMatch.None val salt = runCatching { unb64(json.getString(FIELD_SALT)) }.getOrNull() ?: return DuressMatch.None val expected = runCatching { unb64(json.getString(FIELD_HASH)) }.getOrNull() ?: return DuressMatch.None val derived = VaultCrypto.deriveKey(password, salt) val matches = constantTimeEquals(derived, expected) VaultCrypto.zero(derived) return if (matches) { DuressMatch.Match(DuressAction.fromWire(json.optString(FIELD_ACTION))) } else { DuressMatch.None } } private fun load(): JSONObject? = prefs.getString(KEY_DURESS_CONFIG, null)?.let { runCatching { JSONObject(it) }.getOrNull() } private fun constantTimeEquals(a: ByteArray, b: ByteArray): Boolean { if (a.size != b.size) return false var diff = 0 for (i in a.indices) diff = diff or (a[i].toInt() xor b[i].toInt()) return diff == 0 } private fun b64(bytes: ByteArray): String = Base64.getEncoder().withoutPadding().encodeToString(bytes) private fun unb64(s: String): ByteArray = Base64.getDecoder().decode(s) companion object { private const val PREFS_FILE = "peach_duress_prefs" private const val KEY_DURESS_CONFIG = "duress_config" private const val FIELD_VERSION = "v" private const val FIELD_SALT = "salt" private const val FIELD_HASH = "hash" private const val FIELD_ACTION = "action" } }