dfb877a1cd2e

fix: HKDF null-byte separator in VaultSyncAdapter.deriveEntryKey per-entry info (entryId\u0000sync-entry-v1) — coordinated wire change with peach-browser, legacy-fails-closed test + bump v2.2.8

time
2026-06-10 19:42 UTC · 5 months ago
machine
koh2
agent
claude-opus-4-8
session
hkdf entry-key separator
parent
cd2893fec452
3 files app/build.gradle+2 −2app/src/main/java/com/peachpasswords/sync/VaultSyncAdapter.kt+6 −2app/src/test/java/com/peachpasswords/sync/VaultSyncAdapterTest.kt+34 −0
app/build.gradle+2 −2
@@ -16,8 +16,8 @@1616         applicationId "com.peachpasswords"1717         minSdk 261818         targetSdk 3519-        versionCode 20270020-        versionName "2.2.7"19+        versionCode 20280020+        versionName "2.2.8"2121 2222         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"2323         vectorDrawables {

view file

app/src/main/java/com/peachpasswords/sync/VaultSyncAdapter.kt+6 −2
@@ -20,7 +20,7 @@2020  * this boundary and nowhere else.2121  *2222  * Envelope cipher: AES-GCM under a PER-ENTRY key23- *   HKDF-SHA256(ikm = syncMasterKey, salt = "", info = entryId + "sync-entry-v1")23+ *   HKDF-SHA256(ikm = syncMasterKey, salt = "", info = entryId + "\u0000" + "sync-entry-v1")2424  * with a random 12-byte nonce and aad = "peach-sync-entry-v1". Binding the key2525  * to the entry id means a ciphertext minted for one slot cannot be replayed2626  * into another — [applyEntries] rejects any envelope whose decrypted id does@@ -87,7 +87,11 @@8787         repo.syncMasterKey() ?: throw VaultLockedException()8888 8989     private fun deriveEntryKey(masterKey: ByteArray, entryId: String): ByteArray {90-        val info = (entryId + ENTRY_KEY_INFO_SUFFIX).toByteArray(Charsets.UTF_8)90+        // Null-byte separator (HKDF hardening): entryId cannot contain U+0000, so91+        // the (entryId, suffix) boundary is unambiguous regardless of future id92+        // formats. Coordinated wire change — peach-browser deriveEntryKey produces93+        // the identical info bytes; the two MUST stay byte-for-byte in lockstep.94+        val info = (entryId + "\u0000" + ENTRY_KEY_INFO_SUFFIX).toByteArray(Charsets.UTF_8)9195         val gen = HKDFBytesGenerator(SHA256Digest())9296         gen.init(HKDFParameters(masterKey, ByteArray(0), info))9397         return ByteArray(32).also { gen.generateBytes(it, 0, it.size) }

view file

app/src/test/java/com/peachpasswords/sync/VaultSyncAdapterTest.kt+34 −0
@@ -67,4 +67,38 @@6767         assertNull(repo.getEntry("attacker-chosen-id"))6868         assertEquals(1, repo.getEntries().size)6969     }70+71+    // HKDF separator (coordinated with peach-browser): an envelope minted under72+    // the OLD, unseparated info (`entryId + suffix`) must no longer decrypt. This73+    // locks in the conscious wire break and asserts byte-for-byte parity with the74+    // browser's matching `legacy_info_envelope_fails_closed` test.75+    @Test fun legacy_info_envelope_fails_closed() = runBlocking {76+        val (repo, adapter) = newAdapter()77+        val master = repo.syncMasterKey()!!78+        val entryId = "11111111-2222-4333-8444-555555555555"79+        val entry = VaultEntry(id = entryId, title = "LegacyOnly", createdAt = 0, modifiedAt = 0, password = "pw")80+81+        // Legacy info: no U+0000 separator.82+        val legacyInfo = (entryId + "sync-entry-v1").toByteArray(Charsets.UTF_8)83+        val gen = org.bouncycastle.crypto.generators.HKDFBytesGenerator(org.bouncycastle.crypto.digests.SHA256Digest())84+        gen.init(org.bouncycastle.crypto.params.HKDFParameters(master, ByteArray(0), legacyInfo))85+        val key = ByteArray(32).also { gen.generateBytes(it, 0, it.size) }86+87+        val json = kotlinx.serialization.json.Json { encodeDefaults = false; ignoreUnknownKeys = true }88+        val plaintext = json.encodeToString(VaultEntry.serializer(), entry).toByteArray(Charsets.UTF_8)89+        val nonce = ByteArray(12).also { java.security.SecureRandom().nextBytes(it) }90+        val cipher = javax.crypto.Cipher.getInstance("AES/GCM/NoPadding")91+        cipher.init(92+            javax.crypto.Cipher.ENCRYPT_MODE,93+            javax.crypto.spec.SecretKeySpec(key, "AES"),94+            javax.crypto.spec.GCMParameterSpec(128, nonce),95+        )96+        cipher.updateAAD("peach-sync-entry-v1".toByteArray(Charsets.UTF_8))97+        val ct = cipher.doFinal(plaintext)98+        val legacyEnvelope = java.util.Base64.getEncoder().encodeToString(nonce + ct)99+100+        adapter.applyEntries(mapOf(entryId to legacyEnvelope))101+        // Patched adapter derives with the separated info → decrypt fails → dropped.102+        assertNull(repo.getEntry(entryId))103+    }70104 }

view file