8a011af3d0e0
fix: MF-4/LV-1 NFC-normalize master password before Argon2id (Normalizer.NFC + UTF-8) + deriveKeyRaw legacy-unlock fallback with NFC heal; cross-platform KAT a519… matches browser; v2.2.4 (135 tests)
- time
- 2026-06-10 03:51 UTC · 5 months ago
- machine
- koh2
- agent
- claude-opus-4-8
- session
- android nfc parity
- parent
- ef4c0d8e0d57
↓ peach-android-8a011af3.tar.gzatlas at this save
files changed4 files
app/build.gradle+2 −2app/src/main/java/com/peachpasswords/vault/VaultCrypto.kt+26 −6app/src/main/java/com/peachpasswords/vault/VaultRepository.kt+54 −0app/src/test/java/com/peachpasswords/CrossPlatformKATTest.kt+23 −0app/build.gradle+2 −2
@@ -16,8 +16,8 @@1616 applicationId "com.peachpasswords"1717 minSdk 261818 targetSdk 3519- versionCode 20230020- versionName "2.2.3"19+ versionCode 20240020+ versionName "2.2.4"2121 2222 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"2323 vectorDrawables {app/src/main/java/com/peachpasswords/vault/VaultCrypto.kt+26 −6
@@ -3,6 +3,7 @@33 import org.bouncycastle.crypto.generators.Argon2BytesGenerator44 import org.bouncycastle.crypto.params.Argon2Parameters55 import java.security.SecureRandom6+import java.text.Normalizer67 import javax.crypto.Cipher78 import javax.crypto.spec.GCMParameterSpec89 import javax.crypto.spec.SecretKeySpec@@ -43,7 +44,30 @@4344 private val secureRandom = SecureRandom()4445 private var _lastTimestamp = 0L4546 46- fun deriveKey(password: String, salt: ByteArray): ByteArray {47+ /**48+ * MF-4 / LV-1: NFC-normalize the password, then hash explicit UTF-8 bytes —49+ * byte-for-byte identical to the browser's `password.normalize("NFC")` fed50+ * to Argon2id (`argon2.ts` deriveKey). NFC normalization makes a visually51+ * identical password typed in a different normalization form (NFD on macOS52+ * dead-keys, some IMEs) derive the same key on every platform. UTF-8 (not53+ * BouncyCastle's per-UTF-16-unit char[] path) keeps non-BMP characters54+ * (emoji, surrogate pairs) consistent across platforms.55+ */56+ fun deriveKey(password: String, salt: ByteArray): ByteArray =57+ deriveKeyFromBytes(58+ Normalizer.normalize(password, Normalizer.Form.NFC).toByteArray(Charsets.UTF_8),59+ salt,60+ )61+62+ /**63+ * LV-1 legacy fallback ONLY: derive WITHOUT NFC normalization (raw UTF-864+ * bytes), for unlocking a vault sealed before normalization existed with a65+ * non-NFC password. Every normal path uses [deriveKey].66+ */67+ fun deriveKeyRaw(password: String, salt: ByteArray): ByteArray =68+ deriveKeyFromBytes(password.toByteArray(Charsets.UTF_8), salt)69+70+ private fun deriveKeyFromBytes(passwordBytes: ByteArray, salt: ByteArray): ByteArray {4771 val params = Argon2Parameters.Builder(Argon2Parameters.ARGON2_id)4872 .withSalt(salt)4973 .withParallelism(ARGON2_PARALLELISM)@@ -52,14 +76,10 @@5276 .build()5377 val generator = Argon2BytesGenerator().apply { init(params) }5478 val hash = ByteArray(ARGON2_HASH_LENGTH)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)6079 try {6180 generator.generateBytes(passwordBytes, hash)6281 } finally {82+ // Zero the transient password bytes immediately after use.6383 passwordBytes.fill(0)6484 }6585 return hashapp/src/main/java/com/peachpasswords/vault/VaultRepository.kt+54 −0
@@ -6,6 +6,7 @@66 import kotlinx.coroutines.sync.Mutex77 import kotlinx.coroutines.sync.withLock88 import java.security.SecureRandom9+import java.text.Normalizer910 import java.util.Base641011 import java.util.UUID1112 import javax.inject.Inject@@ -136,9 +137,62 @@136137 VaultCodec.decode(VaultCrypto.decrypt(unb64(slotA.blob), key, VaultCrypto.AAD_SLOT_A))137138 } catch (_: Exception) {138139 VaultCrypto.zero(key)140+ // LV-1: a vault sealed before NFC normalization with a non-NFC141+ // password derives a different key now. If the supplied password is142+ // not already in NFC form, retry with the raw (un-normalized) bytes.143+ if (password != Normalizer.normalize(password, Normalizer.Form.NFC)) {144+ val legacy = tryLegacyRawUnlock(password, slotA)145+ if (legacy != null) return legacy146+ }139147 return UnlockResult.WrongPassword140148 }141149 return adoptSlotA(key, payload)150+ }151+152+ /**153+ * LV-1 legacy fallback: unlock a slot-A vault that was sealed under the raw154+ * (un-normalized) password bytes. On success the slot is HEALED — re-sealed155+ * under the NFC-normalized key — so every future unlock takes the normal156+ * path. Returns null when the raw key also fails (genuinely wrong password).157+ */158+ private fun tryLegacyRawUnlock(password: String, slotA: StoredVault): UnlockResult? {159+ val rawKey = VaultCrypto.deriveKeyRaw(password, unb64(slotA.salt))160+ val payload = try {161+ VaultCodec.decode(VaultCrypto.decrypt(unb64(slotA.blob), rawKey, VaultCrypto.AAD_SLOT_A))162+ } catch (_: Exception) {163+ VaultCrypto.zero(rawKey)164+ return null165+ }166+ // Heal: re-seal slot A under the NFC key (mirrors changePassword's167+ // re-seal). Escrows carry forward unchanged — only the wrapping rotates.168+ val newSalt = VaultCrypto.generateSalt()169+ val newKey = VaultCrypto.deriveKey(password, newSalt)170+ val healed = try {171+ val blob = b64(VaultCrypto.encrypt(VaultCodec.encode(payload), newKey, VaultCrypto.AAD_SLOT_A))172+ val next = StoredVault(173+ version = slotA.version,174+ salt = b64(newSalt),175+ blob = blob,176+ recoveryEscrow = slotA.recoveryEscrow,177+ rootKeyEscrow = slotA.rootKeyEscrow,178+ )179+ if (store.writeSlotA(next)) {180+ writeMatchingNoiseSlotB(next)181+ store.stampLocalMutation()182+ true183+ } else {184+ false185+ }186+ } catch (_: Exception) {187+ false188+ }189+ return if (healed) {190+ VaultCrypto.zero(rawKey)191+ adoptSlotA(newKey, payload) // session key now matches the re-sealed slot192+ } else {193+ VaultCrypto.zero(newKey)194+ adoptSlotA(rawKey, payload) // heal failed — keep using the raw key195+ }142196 }143197 144198 /** Adopt a derived slot-A [key]/[payload] as the live (real-vault) session. */app/src/test/java/com/peachpasswords/CrossPlatformKATTest.kt+23 −0
@@ -3,8 +3,12 @@33 import com.peachpasswords.sync.SASDerivation44 import com.peachpasswords.sync.computeVaultId55 import com.peachpasswords.vault.VaultCrypto6+import org.junit.Assert.assertArrayEquals67 import org.junit.Assert.assertEquals8+import org.junit.Assert.assertFalse9+import org.junit.Assert.assertNotEquals710 import org.junit.Test11+import java.text.Normalizer812 913 /**1014 * MN-4: cross-platform known-answer tests. These pin the byte-level output of the@@ -23,8 +27,27 @@2327 2428 @Test fun argon2id_non_bmp_password_vector() {2529 // password "test🔑" (UTF-8) + salt bytes 0x00..0x0f, Argon2id m=64MiB t=3 p=4.30+ // Generated from peach-browser argon2.ts deriveKey after the LV-1 NFC fix —31+ // the emoji is NFC-stable, so this vector holds across the normalization fix.2632 val key = VaultCrypto.deriveKey("test🔑", argonSalt)2733 assertEquals("a519dd4e695d25c6cd69a2f89d51ddcabd952c979f5714ab29fb77f6d1ad3728", hex(key))34+ }35+36+ @Test fun nfc_normalization_makes_composed_and_decomposed_match() {37+ // MF-4 / LV-1: "café" decomposed (e + U+0301) and composed (U+00E9) must38+ // derive the same key, matching the browser's password.normalize("NFC").39+ val nfd = Normalizer.normalize("café", Normalizer.Form.NFD)40+ val nfc = Normalizer.normalize("café", Normalizer.Form.NFC)41+ assertNotEquals(nfd, nfc)42+ assertArrayEquals(VaultCrypto.deriveKey(nfd, argonSalt), VaultCrypto.deriveKey(nfc, argonSalt))43+ }44+45+ @Test fun deriveKeyRaw_does_not_normalize() {46+ val nfd = Normalizer.normalize("café", Normalizer.Form.NFD)47+ val nfc = Normalizer.normalize("café", Normalizer.Form.NFC)48+ assertFalse(49+ VaultCrypto.deriveKeyRaw(nfd, argonSalt).contentEquals(VaultCrypto.deriveKeyRaw(nfc, argonSalt)),50+ )2851 }2952 3053 @Test fun compute_vault_id_vector() {