1340d93f5f95
fix: MF-5 constant-time unlock — derive duress hash + slot A concurrently (non-committing tryUnlockSlotA, mutex only on commit), branch after both complete; 3 SlotAUnlock tests (v2.2.2)
- time
- 2026-06-09 18:18 UTC · 5 months ago
- machine
- koh2
- agent
- claude-opus-4-8
- session
- audit fixes constant-time unlock
- parent
- c26787bb69af
↓ peach-android-1340d93f.tar.gzatlas at this save
files changed4 files
app/build.gradle+2 −2app/src/main/java/com/peachpasswords/ui/lock/LockViewModel.kt+25 −10app/src/main/java/com/peachpasswords/vault/VaultRepository.kt+51 −7app/src/test/java/com/peachpasswords/vault/SlotAUnlockTest.kt+69 −0app/build.gradle+2 −2
@@ -16,8 +16,8 @@1616 applicationId "com.peachpasswords"1717 minSdk 261818 targetSdk 3519- versionCode 20210020- versionName "2.2.1"19+ versionCode 20220020+ versionName "2.2.2"2121 2222 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"2323 vectorDrawables {app/src/main/java/com/peachpasswords/ui/lock/LockViewModel.kt+25 −10
@@ -16,11 +16,14 @@1616 import com.peachpasswords.vault.VaultRepository1717 import dagger.hilt.android.lifecycle.HiltViewModel1818 import dagger.hilt.android.qualifiers.ApplicationContext19+import kotlinx.coroutines.Dispatchers20+import kotlinx.coroutines.async1921 import kotlinx.coroutines.flow.MutableStateFlow2022 import kotlinx.coroutines.flow.StateFlow2123 import kotlinx.coroutines.flow.asStateFlow2224 import kotlinx.coroutines.flow.update2325 import kotlinx.coroutines.launch26+import kotlinx.coroutines.withContext2427 import javax.inject.Inject2528 2629 data class LockUiState(@@ -55,18 +58,30 @@5558 if (password.isBlank() || _state.value.loading) return5659 _state.update { it.copy(loading = true, error = null) }5760 viewModelScope.launch {58- // Duress check first: a matching duress password never reaches the real vault.59- when (val d = duress.verify(password)) {60- is DuressMatch.Match -> { handleDuress(d.action); return@launch }61- DuressMatch.None -> Unit61+ // Derive the duress hash and slot A CONCURRENTLY so the wall-clock time to62+ // reject/accept does not reveal whether the entered password was the real63+ // password, the duress password, or simply wrong (MF-5, mirrors browser64+ // DUR-002). Both Argon2id derivations always run to completion; the branch65+ // happens only after both finish. The slot-A derivation is non-committing66+ // (no mutex, no session mutation) so it is safe to discard when duress wins.67+ val (duressMatch, slotA) = withContext(Dispatchers.Default) {68+ val duressDeferred = async { duress.verify(password) }69+ val slotADeferred = async { vault.tryUnlockSlotA(password) }70+ duressDeferred.await() to slotADeferred.await()6271 }63- when (val r = vault.unlock(password)) {64- is UnlockResult.Success -> { lockTimer.start(); _state.update { it.copy(loading = false, unlocked = true) } }65- is UnlockResult.WrongPassword -> _state.update {66- it.copy(loading = false, error = "incorrect password", shakeNonce = it.shakeNonce + 1)72+ when {73+ // Duress takes precedence: a matching duress password never reaches the real vault.74+ duressMatch is DuressMatch.Match -> {75+ slotA?.let(vault::discardSlotAUnlock)76+ handleDuress(duressMatch.action)6777 }68- is UnlockResult.Error -> _state.update {69- it.copy(loading = false, error = r.reason, shakeNonce = it.shakeNonce + 1)78+ slotA != null -> {79+ vault.commitSlotAUnlock(slotA)80+ lockTimer.start()81+ _state.update { it.copy(loading = false, unlocked = true) }82+ }83+ else -> _state.update {84+ it.copy(loading = false, error = "incorrect password", shakeNonce = it.shakeNonce + 1)7085 }7186 }7287 }app/src/main/java/com/peachpasswords/vault/VaultRepository.kt+51 −7
@@ -134,14 +134,19 @@134134 /** Unlock body without acquiring the mutex — callers must already hold it. */135135 private fun unlockLocked(password: String): UnlockResult {136136 val slotA = store.loadSlotA() ?: return UnlockResult.Error("no_vault")137+ val key = VaultCrypto.deriveKey(password, unb64(slotA.salt))138+ val payload = try {139+ VaultCodec.decode(VaultCrypto.decrypt(unb64(slotA.blob), key, VaultCrypto.AAD_SLOT_A))140+ } catch (_: Exception) {141+ VaultCrypto.zero(key)142+ return UnlockResult.WrongPassword143+ }144+ return adoptSlotA(key, payload)145+ }146+147+ /** Adopt a derived slot-A [key]/[payload] as the live (real-vault) session. */148+ private fun adoptSlotA(key: ByteArray, payload: VaultPayload): UnlockResult {137149 return try {138- val key = VaultCrypto.deriveKey(password, unb64(slotA.salt))139- val payload = try {140- VaultCodec.decode(VaultCrypto.decrypt(unb64(slotA.blob), key, VaultCrypto.AAD_SLOT_A))141- } catch (_: Exception) {142- VaultCrypto.zero(key)143- return UnlockResult.WrongPassword144- }145150 derivedKey = key146151 decoyMode = false147152 entryList = payload.entries.toMutableList()@@ -158,6 +163,45 @@158163 UnlockResult.Error(e.message ?: "unlock_failed")159164 }160165 }166+167+ // ---- Constant-time unlock (MF-5) --------------------------------------168+169+ /**170+ * A slot-A unlock that has been derived but not yet adopted. Holds the live171+ * Argon2id key and decoded payload so the caller can derive several candidates172+ * concurrently and commit only the winner. The key is owned by this token until173+ * [commitSlotAUnlock] adopts it or [discardSlotAUnlock] zeroes it.174+ */175+ class SlotAUnlock internal constructor(176+ internal val key: ByteArray,177+ internal val payload: VaultPayload,178+ )179+180+ /**181+ * Derive the slot-A key and decrypt its payload WITHOUT mutating session state.182+ * Pure computation (no mutex), so it can run concurrently with other candidate183+ * derivations before the caller decides which to commit (MF-5). Returns null on184+ * no-vault or wrong password (the derived key is zeroed on a tag mismatch).185+ */186+ fun tryUnlockSlotA(password: String): SlotAUnlock? {187+ val slotA = store.loadSlotA() ?: return null188+ val key = VaultCrypto.deriveKey(password, unb64(slotA.salt))189+ return try {190+ val payload = VaultCodec.decode(VaultCrypto.decrypt(unb64(slotA.blob), key, VaultCrypto.AAD_SLOT_A))191+ SlotAUnlock(key, payload)192+ } catch (_: Exception) {193+ VaultCrypto.zero(key)194+ null195+ }196+ }197+198+ /** Adopt a previously-derived [SlotAUnlock] as the live session. */199+ suspend fun commitSlotAUnlock(unlock: SlotAUnlock): UnlockResult = mutex.withLock {200+ adoptSlotA(unlock.key, unlock.payload)201+ }202+203+ /** Zero a derived [SlotAUnlock] that will not be adopted. */204+ fun discardSlotAUnlock(unlock: SlotAUnlock) = VaultCrypto.zero(unlock.key)161205 162206 /**163207 * Unlock into the decoy (duress) vault. Creates an empty decoy on first use,app/src/test/java/com/peachpasswords/vault/SlotAUnlockTest.kt+69 −0
@@ -0,0 +1,69 @@1+package com.peachpasswords.vault2+3+import kotlinx.coroutines.runBlocking4+import org.junit.Assert.assertEquals5+import org.junit.Assert.assertFalse6+import org.junit.Assert.assertNotNull7+import org.junit.Assert.assertNull8+import org.junit.Assert.assertTrue9+import org.junit.Test10+11+/** In-memory [VaultStorage] for the non-committing dual-derivation path (MF-5). */12+private class SlotAFakeStorage : VaultStorage {13+ private var slotA: StoredVault? = null14+ private var slotB: String? = null15+ private var ver: Long = 016+ private var settings = VaultSettings()17+ override fun loadSlotA() = slotA18+ override fun hasVault() = slotA != null19+ override fun writeSlotA(slot: StoredVault): Boolean { slotA = slot; return true }20+ override fun writeSlotB(blobBase64: String): Boolean { slotB = blobBase64; return true }21+ private var decoy: StoredVault? = null22+ override fun loadDecoySlot() = decoy23+ override fun writeDecoySlot(slot: StoredVault): Boolean { decoy = slot; return true }24+ override fun hasDecoySlot() = decoy != null25+ override fun clearDecoySlot() { decoy = null }26+ override fun version() = ver27+ override fun stampLocalMutation(): Long { ver = maxOf(System.currentTimeMillis(), ver + 1); return ver }28+ override fun loadSettings() = settings29+ override fun writeSettings(s: VaultSettings): Boolean { settings = s; return true }30+ override fun clearVault() { slotA = null; slotB = null; ver = 0; settings = VaultSettings() }31+}32+33+class SlotAUnlockTest {34+ private fun freshRepo(): VaultRepository = VaultRepository(SlotAFakeStorage())35+36+ @Test fun try_unlock_does_not_mutate_session() = runBlocking {37+ val repo = freshRepo()38+ repo.createVault("correct horse battery staple")39+ repo.addEntry(VaultEntry(id = "", title = "GitHub", type = "login", password = "pw", createdAt = 0, modifiedAt = 0))40+ repo.lock()41+ assertFalse(repo.isUnlocked.value)42+43+ val derived = repo.tryUnlockSlotA("correct horse battery staple")44+ assertNotNull("right password derives a slot-A unlock", derived)45+ // Deriving alone must NOT unlock the live session — only commit does.46+ assertFalse("tryUnlockSlotA leaves the session locked", repo.isUnlocked.value)47+48+ assertEquals(UnlockResult.Success, repo.commitSlotAUnlock(derived!!))49+ assertTrue("commit adopts the session", repo.isUnlocked.value)50+ assertEquals(1, repo.getEntries().size)51+ }52+53+ @Test fun try_unlock_returns_null_for_wrong_password() = runBlocking {54+ val repo = freshRepo()55+ repo.createVault("the-real-password-here")56+ repo.lock()57+ assertNull("wrong password derives nothing", repo.tryUnlockSlotA("not-the-password"))58+ assertFalse(repo.isUnlocked.value)59+ }60+61+ @Test fun discard_does_not_unlock() = runBlocking {62+ val repo = freshRepo()63+ repo.createVault("correct horse battery staple")64+ repo.lock()65+ val derived = repo.tryUnlockSlotA("correct horse battery staple")!!66+ repo.discardSlotAUnlock(derived) // zero without adopting67+ assertFalse("discarded unlock never adopts the session", repo.isUnlocked.value)68+ }69+}