b14d0aa0b5d3

fix: PCH-02-002 Noise XX message 1 empty-payload mixHash — wire change, ships with browser v4.0.1 (v2.3.0)

time
2026-06-12 20:44 UTC · 5 months ago
machine
koh2
agent
gpt-5
session
pch-02 integration
parent
81c0378cca35
2 files app/src/main/java/com/peachpasswords/sync/NoiseXX.kt+86 −0app/src/test/java/com/peachpasswords/CrossPlatformKATTest.kt+8 −0
app/src/main/java/com/peachpasswords/sync/NoiseXX.kt+86 −0
@@ -79,6 +79,14 @@7979     )8080 }8181 82+private fun keyPairFromSeed(seed: ByteArray): X25519KeyPair {83+    val priv = X25519PrivateKeyParameters(seed, 0)84+    return X25519KeyPair(85+        publicKey = priv.generatePublicKey().encoded,86+        secretKey = priv.encoded,87+    )88+}89+8290 private fun sharedKey(privateKey: ByteArray, publicKey: ByteArray): ByteArray {8391     val priv = X25519PrivateKeyParameters(privateKey, 0)8492     val pub = X25519PublicKeyParameters(publicKey, 0)@@ -109,6 +117,78 @@109117     }110118 }111119 120+internal fun noiseXXDeterministicHandshakeHashForTest(): ByteArray {121+    val initiatorStatic = keyPairFromSeed(ByteArray(32) { 0xa1.toByte() })122+    val responderStatic = keyPairFromSeed(ByteArray(32) { 0xb2.toByte() })123+    val initiatorEphemeral = keyPairFromSeed(ByteArray(32) { 0x01 })124+    val responderEphemeral = keyPairFromSeed(ByteArray(32) { 0x02 })125+126+    var hI = initHash()127+    var ckI = hI.copyOf()128+    var hR = initHash()129+    var ckR = hR.copyOf()130+131+    // Message 1: -> e132+    val msg1 = initiatorEphemeral.publicKey133+    hI = mixHash(hI, msg1)134+    hI = mixHash(hI, ByteArray(0))135+    hR = mixHash(hR, msg1)136+    hR = mixHash(hR, ByteArray(0))137+138+    // Message 2: <- e, ee, s, es139+    hR = mixHash(hR, responderEphemeral.publicKey)140+    val eeR = sharedKey(responderEphemeral.secretKey, msg1)141+    val (ckR1, kR1) = hkdf2(ckR, eeR)142+    ckR = ckR1143+    val encryptedResponderStatic = chachaSeal(kR1, nonceBytes(0L), responderStatic.publicKey, hR)144+    hR = mixHash(hR, encryptedResponderStatic)145+146+    val esR = sharedKey(responderStatic.secretKey, msg1)147+    val (ckR2, kR2) = hkdf2(ckR, esR)148+    ckR = ckR2149+    val encryptedPayload = chachaSeal(kR2, nonceBytes(0L), ByteArray(0), hR)150+    hR = mixHash(hR, encryptedPayload)151+152+    val remoteEphemeral = responderEphemeral.publicKey153+    hI = mixHash(hI, remoteEphemeral)154+    val eeI = sharedKey(initiatorEphemeral.secretKey, remoteEphemeral)155+    val (ckI1, kI1) = hkdf2(ckI, eeI)156+    ckI = ckI1157+    val remoteStaticPlain = chachaOpen(kI1, nonceBytes(0L), encryptedResponderStatic, hI)158+        ?: throw IllegalStateException("Noise XX KAT: msg2 static auth failed")159+    hI = mixHash(hI, encryptedResponderStatic)160+161+    val esI = sharedKey(initiatorEphemeral.secretKey, remoteStaticPlain)162+    val (ckI2, kI2) = hkdf2(ckI, esI)163+    ckI = ckI2164+    chachaOpen(kI2, nonceBytes(0L), encryptedPayload, hI)165+        ?: throw IllegalStateException("Noise XX KAT: msg2 payload auth failed")166+    hI = mixHash(hI, encryptedPayload)167+168+    // Message 3: -> s, se169+    val encryptedInitiatorStatic = chachaSeal(kI2, nonceBytes(1L), initiatorStatic.publicKey, hI)170+    hI = mixHash(hI, encryptedInitiatorStatic)171+    val seI = sharedKey(initiatorStatic.secretKey, remoteEphemeral)172+    val (ckI3, kI3) = hkdf2(ckI, seI)173+    ckI = ckI3174+    val encryptedFinalPayload = chachaSeal(kI3, nonceBytes(0L), ByteArray(0), hI)175+    hI = mixHash(hI, encryptedFinalPayload)176+177+    val initiatorStaticPlain = chachaOpen(kR2, nonceBytes(1L), encryptedInitiatorStatic, hR)178+        ?: throw IllegalStateException("Noise XX KAT: msg3 static auth failed")179+    hR = mixHash(hR, encryptedInitiatorStatic)180+    val seR = sharedKey(responderEphemeral.secretKey, initiatorStaticPlain)181+    val (ckR3, kR3) = hkdf2(ckR, seR)182+    ckR = ckR3183+    chachaOpen(kR3, nonceBytes(0L), encryptedFinalPayload, hR)184+        ?: throw IllegalStateException("Noise XX KAT: msg3 final payload auth failed")185+    hR = mixHash(hR, encryptedFinalPayload)186+187+    check(hI.contentEquals(hR)) { "Noise XX KAT sides diverged" }188+    check(ckI.contentEquals(ckR)) { "Noise XX KAT chaining keys diverged" }189+    return hI190+}191+112192 suspend fun noiseXXInitiator(113193     ourStaticPrivateKey: ByteArray,114194     ourStaticPublicKey: ByteArray,@@ -120,6 +200,10 @@120200 121201     val e = generateKeyPair()122202     h = mixHash(h, e.publicKey)203+    // PCH-02-002: canonical Noise still runs EncryptAndHash(empty) for msg1.204+    // No bytes are added to the wire; the transcript hash moves in lockstep205+    // with peach-browser.206+    h = mixHash(h, ByteArray(0))123207     send(e.publicKey)124208 125209     val msg2 = receive()@@ -197,6 +281,8 @@197281     require(msg1.size >= 32) { "Noise XX: msg1 too short" }198282     val remoteEphemeral = msg1.copyOfRange(0, 32)199283     h = mixHash(h, remoteEphemeral)284+    // PCH-02-002: mirror the initiator's empty-payload MixHash for msg1.285+    h = mixHash(h, ByteArray(0))200286 201287     val e = generateKeyPair()202288     h = mixHash(h, e.publicKey)

view file

app/src/test/java/com/peachpasswords/CrossPlatformKATTest.kt+8 −0
@@ -2,6 +2,7 @@22 33 import com.peachpasswords.sync.SASDerivation44 import com.peachpasswords.sync.computeVaultId5+import com.peachpasswords.sync.noiseXXDeterministicHandshakeHashForTest56 import com.peachpasswords.sync.noiseNKHkdf2ForTest67 import com.peachpasswords.sync.noiseNKInitHashForTest78 import com.peachpasswords.sync.noiseXXHkdf2ForTest@@ -96,5 +97,12 @@9697         val (nk1, nk2) = noiseNKHkdf2ForTest(ck, ikm)9798         assertArrayEquals(xk1, nk1)9899         assertArrayEquals(xk2, nk2)100+    }101+102+    @Test fun noise_xx_handshake_hash_includes_canonical_msg1_empty_payload_hash() {103+        assertEquals(104+            "505b6dcb59838e5d163f4fd4897cfff87c20ecca23aa993b416206aba25ee71c",105+            hex(noiseXXDeterministicHandshakeHashForTest()),106+        )99107     }100108 }

view file