367fb3e550bb

fix: PCH-01-002 Noise initHash + hkdf2 derive over BLAKE2s in NoiseXX.kt + NoiseNK.kt (BouncyCastle Blake2sDigest/HKDFBytesGenerator) to match the declared pattern name — coordinated breaking wire change with peach-browser; cross-platform KAT pins byte-identical initHash + HKDF-BLAKE2s vectors; bump v2.2.9

time
2026-06-10 20:58 UTC · 5 months ago
machine
koh2
agent
claude-opus-4-8
session
pch-01-002 noise blake2s
parent
dfb877a1cd2e
4 files app/build.gradle+2 −2app/src/main/java/com/peachpasswords/sync/NoiseNK.kt+13 −5app/src/main/java/com/peachpasswords/sync/NoiseXX.kt+13 −5app/src/test/java/com/peachpasswords/CrossPlatformKATTest.kt+36 −0
app/build.gradle+2 −2
@@ -16,8 +16,8 @@1616         applicationId "com.peachpasswords"1717         minSdk 261818         targetSdk 3519-        versionCode 20280020-        versionName "2.2.8"19+        versionCode 20290020+        versionName "2.2.9"2121 2222         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"2323         vectorDrawables {

view file

app/src/main/java/com/peachpasswords/sync/NoiseNK.kt+13 −5
@@ -1,7 +1,6 @@11 package com.peachpasswords.sync22 33 import org.bouncycastle.crypto.digests.Blake2sDigest4-import org.bouncycastle.crypto.digests.SHA256Digest54 import org.bouncycastle.crypto.generators.HKDFBytesGenerator65 import org.bouncycastle.crypto.modes.ChaCha20Poly130576 import org.bouncycastle.crypto.params.AEADParameters@@ -31,10 +30,13 @@3130     if (nameBytes.size <= 32) {3231         return ByteArray(32).also { nameBytes.copyInto(it) }3332     }34-    val sha = SHA256Digest()35-    sha.update(nameBytes, 0, nameBytes.size)33+    // PCH-01-002: the named pattern hash is BLAKE2s; the protocol-name init hash34+    // MUST use it too (Noise spec §5.2), not SHA-256. Coordinated wire change with35+    // peach-browser noise-js.ts.36+    val blake = Blake2sDigest(256)37+    blake.update(nameBytes, 0, nameBytes.size)3638     val out = ByteArray(32)37-    sha.doFinal(out, 0)39+    blake.doFinal(out, 0)3840     return out3941 }4042 @@ -48,12 +50,18 @@4850 }4951 5052 private fun hkdf2(ck: ByteArray, ikm: ByteArray): Pair<ByteArray, ByteArray> {51-    val generator = HKDFBytesGenerator(SHA256Digest())53+    // PCH-01-002: HKDF must run over the named hash (BLAKE2s), not SHA-256.54+    val generator = HKDFBytesGenerator(Blake2sDigest(256))5255     generator.init(HKDFParameters(ikm, ck, ByteArray(0)))5356     val out = ByteArray(64)5457     generator.generateBytes(out, 0, out.size)5558     return out.copyOfRange(0, 32) to out.copyOfRange(32, 64)5659 }60+61+// PCH-01-002: test-only seams for the cross-platform BLAKE2s vectors pinned in62+// CrossPlatformKATTest.kt (must stay byte-identical to peach-browser noise-js.ts).63+internal fun noiseNKInitHashForTest(): ByteArray = initHash()64+internal fun noiseNKHkdf2ForTest(ck: ByteArray, ikm: ByteArray): Pair<ByteArray, ByteArray> = hkdf2(ck, ikm)5765 5866 private fun nonceBytes(n: Long): ByteArray {5967     val buf = ByteArray(12)

view file

app/src/main/java/com/peachpasswords/sync/NoiseXX.kt+13 −5
@@ -2,7 +2,6 @@22 33 import org.bouncycastle.crypto.InvalidCipherTextException44 import org.bouncycastle.crypto.digests.Blake2sDigest5-import org.bouncycastle.crypto.digests.SHA256Digest65 import org.bouncycastle.crypto.generators.HKDFBytesGenerator76 import org.bouncycastle.crypto.modes.ChaCha20Poly130587 import org.bouncycastle.crypto.params.AEADParameters@@ -21,10 +20,13 @@2120     if (nameBytes.size <= 32) {2221         return ByteArray(32).also { nameBytes.copyInto(it) }2322     }24-    val sha = SHA256Digest()25-    sha.update(nameBytes, 0, nameBytes.size)23+    // PCH-01-002: the named pattern hash is BLAKE2s; the protocol-name init hash24+    // MUST use it too (Noise spec §5.2), not SHA-256. Coordinated wire change with25+    // peach-browser noise-xx.ts.26+    val blake = Blake2sDigest(256)27+    blake.update(nameBytes, 0, nameBytes.size)2628     val out = ByteArray(32)27-    sha.doFinal(out, 0)29+    blake.doFinal(out, 0)2830     return out2931 }3032 @@ -38,12 +40,18 @@3840 }3941 4042 private fun hkdf2(ck: ByteArray, ikm: ByteArray): Pair<ByteArray, ByteArray> {41-    val generator = HKDFBytesGenerator(SHA256Digest())43+    // PCH-01-002: HKDF must run over the named hash (BLAKE2s), not SHA-256.44+    val generator = HKDFBytesGenerator(Blake2sDigest(256))4245     generator.init(HKDFParameters(ikm, ck, ByteArray(0)))4346     val out = ByteArray(64)4447     generator.generateBytes(out, 0, out.size)4548     return out.copyOfRange(0, 32) to out.copyOfRange(32, 64)4649 }50+51+// PCH-01-002: test-only seams for the cross-platform BLAKE2s vectors pinned in52+// CrossPlatformKATTest.kt (must stay byte-identical to peach-browser noise-xx.ts).53+internal fun noiseXXInitHashForTest(): ByteArray = initHash()54+internal fun noiseXXHkdf2ForTest(ck: ByteArray, ikm: ByteArray): Pair<ByteArray, ByteArray> = hkdf2(ck, ikm)4755 4856 private fun nonceBytes(n: Long): ByteArray {4957     val buf = ByteArray(12)

view file

app/src/test/java/com/peachpasswords/CrossPlatformKATTest.kt+36 −0
@@ -2,6 +2,10 @@22 33 import com.peachpasswords.sync.SASDerivation44 import com.peachpasswords.sync.computeVaultId5+import com.peachpasswords.sync.noiseNKHkdf2ForTest6+import com.peachpasswords.sync.noiseNKInitHashForTest7+import com.peachpasswords.sync.noiseXXHkdf2ForTest8+import com.peachpasswords.sync.noiseXXInitHashForTest59 import com.peachpasswords.vault.VaultCrypto610 import org.junit.Assert.assertArrayEquals711 import org.junit.Assert.assertEquals@@ -60,5 +64,37 @@6064         // First 4 bytes 0x12345678 = 305419896; 305419896 mod 1_000_000 = 419896.6165         val handshakeHash = byteArrayOf(0x12, 0x34, 0x56, 0x78) + ByteArray(28)6266         assertEquals("419896", SASDerivation.derive(handshakeHash))67+    }68+69+    // PCH-01-002: the Noise protocol-name init hash and the MixKey/Split HKDF now70+    // derive over BLAKE2s to match the declared pattern name. These vectors are71+    // pinned IDENTICALLY in peach-browser's noise-blake2s-kat.test.ts — a mismatch72+    // here means the two repos' Noise wire derivations have diverged (which would73+    // break cross-device pairing/sync). A SHA-256 revert on either side also fails.74+    @Test fun noise_xx_init_hash_is_blake2s_vector() {75+        assertEquals(76+            "1ceedd81c5f458b225923dc2507787bf156f9251fc17c45af63263a929fc1ed2",77+            hex(noiseXXInitHashForTest()),78+        )79+    }80+81+    @Test fun noise_nk_init_hash_is_blake2s_vector() {82+        assertEquals(83+            "e01688d376ef280a27a8af5c559024a02017585b3763763e4f3c7118a979b09e",84+            hex(noiseNKInitHashForTest()),85+        )86+    }87+88+    @Test fun noise_hkdf2_blake2s_vector() {89+        // ck = 0x00..0x1f, ikm = 0x20..0x3f.90+        val ck = ByteArray(32) { it.toByte() }91+        val ikm = ByteArray(32) { (it + 32).toByte() }92+        val (xk1, xk2) = noiseXXHkdf2ForTest(ck, ikm)93+        assertEquals("6a96444e20e8d4c1cee974416acae1c10b3c92886010e54ed94dafb2c3b80ea0", hex(xk1))94+        assertEquals("57af120b0de7acbe7907ec149c5ae870a2dbb74232b65777ba4123f1f7f888f5", hex(xk2))95+        // hkdf2 is duplicated in NoiseXX.kt and NoiseNK.kt — they must agree.96+        val (nk1, nk2) = noiseNKHkdf2ForTest(ck, ikm)97+        assertArrayEquals(xk1, nk1)98+        assertArrayEquals(xk2, nk2)6399     }64100 }

view file