SlotPaddingTest.kt

7.6 KB · raw

1package com.peachpasswords.vault23import kotlinx.coroutines.runBlocking4import org.junit.Assert.assertEquals5import org.junit.Assert.assertTrue6import org.junit.Test78/** Exposes the raw stored slots so the padding lengths (MN-1) can be inspected. */9private 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}2930class 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    }4243    @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    }4849    @Test fun above_top_bucket_vault_codec_round_trip_preserves_payload() {50        val notes = "n".repeat(300_000)51        val payload = VaultPayload(52            entries = listOf(53                VaultEntry(54                    id = "large",55                    title = "Large note",56                    type = "note",57                    notes = notes,58                    createdAt = 0,59                    modifiedAt = 0,60                ),61            ),62            rootKeyB64 = "root",63        )6465        val encoded = VaultCodec.encode(payload)66        assertEquals(327680, encoded.size)67        assertEquals(notes, VaultCodec.decode(encoded).entries.single().notes)68    }6970    @Test fun real_and_duress_slots_share_a_bucket_for_small_vaults() = runBlocking {71        val storage = PaddingFakeStorage()72        val repo = VaultRepository(storage)73        repo.createVault("the-real-password-123")74        repo.addEntry(VaultEntry(id = "", title = "Bank", type = "login", password = "pw", createdAt = 0, modifiedAt = 0))75        assertTrue(repo.setDuressPassphrase("open-the-decoy-456"))7677        val slotALen = storage.slotA!!.blob.length78        val decoyLen = storage.decoy!!.blob.length79        assertEquals("a small real vault and its decoy quantise to the same slot length", slotALen, decoyLen)80        // And the always-present noise slot matches slot A's length too (±base64 rounding).81        assertTrue("noise slot matches slot A length", kotlin.math.abs(storage.slotBNoise!!.length - slotALen) <= 4)82    }8384    /** Mirrors VaultStore.writeAligned: write one record, re-align both. */85    private class AlignedSlotRecords {86        var a: String? = null87        var d: String? = null88        fun writeSlotA(slot: StoredVault) {89            val (aligned, sibling) = VaultCodec.alignSlotRecords(VaultCodec.encodeSlot(slot), d)90            a = aligned91            if (sibling != null) d = sibling92        }93        fun writeDecoy(slot: StoredVault) {94            val (aligned, sibling) = VaultCodec.alignSlotRecords(VaultCodec.encodeSlot(slot), a)95            d = aligned96            if (sibling != null) a = sibling97        }98    }99100    private fun slotWithBlob(blobLength: Int) = StoredVault(101        version = 1,102        salt = "c2FsdHNhbHRzYWx0c2FsdA==",103        blob = "A".repeat(blobLength),104    )105106    /**107     * PAI-2026-004: a real decoy's blob is frozen at the size it had when108     * duress was configured, because re-sealing it would need the duress109     * passphrase. Walking slot A up through every padding bucket used to leave110     * the decoy record visibly shorter, which told an at-rest observer both111     * that duress was configured and how far the real vault had grown.112     */113    @Test fun slot_records_stay_aligned_as_the_vault_crosses_every_bucket() {114        val slots = AlignedSlotRecords()115        slots.writeSlotA(slotWithBlob(VaultCrypto.PADDING_BUCKETS.first()))116        // Duress is configured once, at the smallest bucket, and frozen there.117        val frozenDecoy = slotWithBlob(VaultCrypto.PADDING_BUCKETS.first())118        slots.writeDecoy(frozenDecoy)119        val configuredAt = slots.d!!.length120121        for (bucket in VaultCrypto.PADDING_BUCKETS) {122            slots.writeSlotA(slotWithBlob(bucket))123            assertEquals("bucket $bucket: the two records are the same length", slots.a!!.length, slots.d!!.length)124            assertEquals("bucket $bucket: padding is transparent to decode", bucket, VaultCodec.decodeSlot(slots.a!!)!!.blob.length)125            assertEquals("bucket $bucket: the decoy still decodes", frozenDecoy, VaultCodec.decodeSlot(slots.d!!))126        }127        assertTrue("alignment actually did work at the top bucket", slots.a!!.length > configuredAt)128    }129130    @Test fun slot_records_stay_aligned_when_the_vault_shrinks_below_the_decoy() {131        val slots = AlignedSlotRecords()132        slots.writeSlotA(slotWithBlob(VaultCrypto.PADDING_BUCKETS.first()))133        slots.writeDecoy(slotWithBlob(VaultCrypto.PADDING_BUCKETS.last()))134        slots.writeSlotA(slotWithBlob(VaultCrypto.PADDING_BUCKETS.first()))135        assertEquals(slots.a!!.length, slots.d!!.length)136    }137138    /**139     * The target is the longer TRUE record, not the longer stored one, so a140     * vault that grows and then shrinks is not pinned at a high-water mark.141     */142    @Test fun a_fake_decoy_lets_the_pair_follow_the_vault_back_down() {143        val slots = AlignedSlotRecords()144        slots.writeSlotA(slotWithBlob(VaultCrypto.PADDING_BUCKETS.first()))145        slots.writeDecoy(slotWithBlob(VaultCrypto.PADDING_BUCKETS.first()))146        val small = slots.a!!.length147148        slots.writeSlotA(slotWithBlob(VaultCrypto.PADDING_BUCKETS.last()))149        assertTrue("grows with the vault", slots.a!!.length > small)150        assertEquals(slots.a!!.length, slots.d!!.length)151152        // Unconfigured: a fresh fake is generated at the new, smaller size.153        slots.writeSlotA(slotWithBlob(VaultCrypto.PADDING_BUCKETS.first()))154        slots.writeDecoy(slotWithBlob(VaultCrypto.PADDING_BUCKETS.first()))155        assertEquals("back to the small record length", small, slots.a!!.length)156        assertEquals(slots.a!!.length, slots.d!!.length)157    }158159    @Test fun a_slot_record_with_no_sibling_is_never_padded() {160        val record = VaultCodec.encodeSlot(slotWithBlob(64))161        val (aligned, sibling) = VaultCodec.alignSlotRecords(record, null)162        assertEquals(record, aligned)163        assertEquals(null, sibling)164    }165}