b121245408d2

fix: MN-1 size-bucket pad sealed plaintext (slot A, duress slot B, recovery escrow quantise to equal lengths; decode strips NUL pad, legacy blobs still decode); noise slot B matches slot A length; 3 tests (v2.2.3)

time
2026-06-09 18:41 UTC · 5 months ago
machine
koh2
agent
claude-opus-4-8
session
audit fixes slot padding
parent
28099c39fff3
4 files app/build.gradle+2 −2app/src/main/java/com/peachpasswords/vault/VaultRepository.kt+4 −1app/src/main/java/com/peachpasswords/vault/VaultStore.kt+10 −2app/src/test/java/com/peachpasswords/vault/SlotPaddingTest.kt+62 −0
app/build.gradle+2 −2
@@ -16,8 +16,8 @@1616         applicationId "com.peachpasswords"1717         minSdk 261818         targetSdk 3519-        versionCode 20220020-        versionName "2.2.2"19+        versionCode 20230020+        versionName "2.2.3"2121 2222         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"2323         vectorDrawables {

view file

app/src/main/java/com/peachpasswords/vault/VaultRepository.kt+4 −1
@@ -908,7 +908,10 @@908908     }909909 910910     private fun writeMatchingNoiseSlotB(slotA: StoredVault) {911-        val approxBytes = (slotA.blob.length / 2).coerceAtLeast(VaultCrypto.NONCE_SIZE + 16)911+        // Size the noise so its base64 length matches slot A's blob (MN-1): with the912+        // plaintext now bucket-padded, an at-rest observer sees two equal-length slots913+        // and cannot pick the real one by size. b64(N bytes) ≈ N*4/3 chars.914+        val approxBytes = (slotA.blob.length * 3 / 4).coerceAtLeast(VaultCrypto.NONCE_SIZE + 16)912915         val noise = ByteArray(approxBytes).also(rng::nextBytes)913916         store.writeSlotB(b64(noise))914917     }

view file

app/src/main/java/com/peachpasswords/vault/VaultStore.kt+10 −2
@@ -93,8 +93,16 @@9393 /** Pure JSON codec for the slot-A plaintext envelope — usable without Android. */9494 object VaultCodec {9595     private val json = Json { ignoreUnknownKeys = true; encodeDefaults = true }96-    fun encode(payload: VaultPayload): ByteArray = json.encodeToString(payload).toByteArray(Charsets.UTF_8)97-    fun decode(bytes: ByteArray): VaultPayload = json.decodeFromString(String(bytes, Charsets.UTF_8))96+97+    // MN-1: pad every sealed plaintext to a size bucket before encryption so that98+    // slot A (real) and slot B (duress decoy) — and the recovery escrow — quantise99+    // to the same lengths and an at-rest observer cannot tell which slot is which100+    // from its size. Padding is trailing NULs (never valid inside JSON); decode101+    // strips them, so an unpadded legacy blob (no trailing NULs) still decodes.102+    fun encode(payload: VaultPayload): ByteArray =103+        VaultCrypto.padToBucket(json.encodeToString(payload).toByteArray(Charsets.UTF_8))104+    fun decode(bytes: ByteArray): VaultPayload =105+        json.decodeFromString(String(bytes, Charsets.UTF_8).trimEnd('\u0000'))98106     fun encodeSettings(s: VaultSettings): String = json.encodeToString(s)99107     fun decodeSettings(s: String): VaultSettings? = runCatching { json.decodeFromString<VaultSettings>(s) }.getOrNull()100108     fun encodeSlot(s: StoredVault): String = json.encodeToString(s)

view file

app/src/test/java/com/peachpasswords/vault/SlotPaddingTest.kt+62 −0
@@ -0,0 +1,62 @@1+package com.peachpasswords.vault2+3+import kotlinx.coroutines.runBlocking4+import org.junit.Assert.assertEquals5+import org.junit.Assert.assertTrue6+import org.junit.Test7+8+/** Exposes the raw stored slots so the padding lengths (MN-1) can be inspected. */9+private class PaddingFakeStorage : VaultStorage {10+    var slotA: StoredVault? = null11+    var slotBNoise: String? = null12+    var decoy: StoredVault? = null13+    private var ver: Long = 014+    private var settings = VaultSettings()15+    override fun loadSlotA() = slotA16+    override fun hasVault() = slotA != null17+    override fun writeSlotA(slot: StoredVault): Boolean { slotA = slot; return true }18+    override fun writeSlotB(blobBase64: String): Boolean { slotBNoise = blobBase64; return true }19+    override fun loadDecoySlot() = decoy20+    override fun writeDecoySlot(slot: StoredVault): Boolean { decoy = slot; return true }21+    override fun hasDecoySlot() = decoy != null22+    override fun clearDecoySlot() { decoy = null }23+    override fun version() = ver24+    override fun stampLocalMutation(): Long { ver = maxOf(System.currentTimeMillis(), ver + 1); return ver }25+    override fun loadSettings() = settings26+    override fun writeSettings(s: VaultSettings): Boolean { settings = s; return true }27+    override fun clearVault() { slotA = null; slotBNoise = null; ver = 0; settings = VaultSettings() }28+}29+30+class SlotPaddingTest {31+    @Test fun payload_round_trips_through_bucket_padding() {32+        val payload = VaultPayload(33+            entries = listOf(VaultEntry(id = "1", title = "GitHub", type = "login", password = "pw", createdAt = 0, modifiedAt = 0)),34+            rootKeyB64 = "AAAA",35+        )36+        val padded = VaultCodec.encode(payload)37+        assertEquals("padded to the smallest bucket", VaultCrypto.PADDING_BUCKETS.first(), padded.size)38+        val decoded = VaultCodec.decode(padded)39+        assertEquals("padding is transparent to decode", "GitHub", decoded.entries.single().title)40+        assertEquals("AAAA", decoded.rootKeyB64)41+    }42+43+    @Test fun legacy_unpadded_blob_still_decodes() {44+        // A blob written before MN-1 has no trailing NUL padding — must still decode.45+        val raw = "{\"entries\":[],\"rootKeyB64\":\"X\"}".toByteArray(Charsets.UTF_8)46+        assertEquals("X", VaultCodec.decode(raw).rootKeyB64)47+    }48+49+    @Test fun real_and_duress_slots_share_a_bucket_for_small_vaults() = runBlocking {50+        val storage = PaddingFakeStorage()51+        val repo = VaultRepository(storage)52+        repo.createVault("the-real-password-123")53+        repo.addEntry(VaultEntry(id = "", title = "Bank", type = "login", password = "pw", createdAt = 0, modifiedAt = 0))54+        assertTrue(repo.setDuressPassphrase("open-the-decoy-456"))55+56+        val slotALen = storage.slotA!!.blob.length57+        val decoyLen = storage.decoy!!.blob.length58+        assertEquals("a small real vault and its decoy quantise to the same slot length", slotALen, decoyLen)59+        // And the always-present noise slot matches slot A's length too (±base64 rounding).60+        assertTrue("noise slot matches slot A length", kotlin.math.abs(storage.slotBNoise!!.length - slotALen) <= 4)61+    }62+}

view file