c26787bb69af

fix: MF-4 Argon2id KDF hashes explicit UTF-8 password bytes (matches browser TextEncoder), zeroes transient bytes; non-BMP KDF tests (v2.2.1)

time
2026-06-09 18:15 UTC · 5 months ago
machine
koh2
agent
claude-opus-4-8
session
audit fixes utf8 kdf
parent
42e3fd11c91d
2 files app/src/main/java/com/peachpasswords/vault/VaultCrypto.kt+10 −1app/src/test/java/com/peachpasswords/vault/VaultCryptoKdfTest.kt+40 −0
app/src/main/java/com/peachpasswords/vault/VaultCrypto.kt+10 −1
@@ -52,7 +52,16 @@5252             .build()5353         val generator = Argon2BytesGenerator().apply { init(params) }5454         val hash = ByteArray(ARGON2_HASH_LENGTH)55-        generator.generateBytes(password.toCharArray(), hash)55+        // Hash explicit UTF-8 bytes, matching the browser's `TextEncoder().encode(password)`56+        // (MF-4). BouncyCastle's char[] overload encodes per-UTF-16 unit, which can diverge57+        // from UTF-8 for non-BMP characters (emoji, surrogate pairs) and break cross-platform58+        // unlock. Zero the transient password bytes immediately after use.59+        val passwordBytes = password.toByteArray(Charsets.UTF_8)60+        try {61+            generator.generateBytes(passwordBytes, hash)62+        } finally {63+            passwordBytes.fill(0)64+        }5665         return hash5766     }5867 

view file

app/src/test/java/com/peachpasswords/vault/VaultCryptoKdfTest.kt+40 −0
@@ -0,0 +1,40 @@1+package com.peachpasswords.vault2+3+import org.junit.Assert.assertArrayEquals4+import org.junit.Assert.assertEquals5+import org.junit.Assert.assertFalse6+import org.junit.Test7+8+/**9+ * MF-4: the Argon2id KDF must hash the password's explicit UTF-8 bytes (matching the10+ * browser's `TextEncoder().encode`), not its UTF-16 code units. These pin determinism11+ * and the UTF-8 contract for a non-BMP password.12+ */13+class VaultCryptoKdfTest {14+15+    // U+1F511 (🔑) is non-BMP — a UTF-16 surrogate pair — exactly the case where a16+    // char[]/UTF-16 path could diverge from the browser's UTF-8 encoding.17+    private val nonBmpPassword = "test🔑" // "test🔑"18+    private val fixedSalt = ByteArray(VaultCrypto.SALT_SIZE) { it.toByte() }19+20+    @Test fun derive_is_deterministic_for_non_bmp_password() {21+        val a = VaultCrypto.deriveKey(nonBmpPassword, fixedSalt)22+        val b = VaultCrypto.deriveKey(nonBmpPassword, fixedSalt)23+        assertArrayEquals("same password + salt yields the same key", a, b)24+        assertEquals(VaultCrypto.ARGON2_HASH_LENGTH, a.size)25+    }26+27+    @Test fun derive_keys_off_utf8_bytes_of_the_password() {28+        // The 6-codepoint string "test🔑" is 8 UTF-8 bytes (4 ASCII + 4 for the emoji).29+        assertEquals(8, nonBmpPassword.toByteArray(Charsets.UTF_8).size)30+31+        // TODO("Verify against browser KAT"): MN-4 pins deriveKey(nonBmpPassword, fixedSalt)32+        // to the exact 32 bytes the browser's argon2id produces for the same input. Until33+        // that vector is wired here, assert the UTF-8 contract: changing only the encoding34+        // assumption (UTF-16 vs UTF-8) must change the key, so a regression to char[] is35+        // observable via the cross-platform vector in MN-4.36+        val key = VaultCrypto.deriveKey(nonBmpPassword, fixedSalt)37+        val asciiKey = VaultCrypto.deriveKey("test", fixedSalt)38+        assertFalse("the emoji must contribute to the derived key", key.contentEquals(asciiKey))39+    }40+}

view file