package com.peachpasswords import com.peachpasswords.sync.DEVICE_MANIFEST_VERSION import com.peachpasswords.sync.DeviceManifestBody import com.peachpasswords.sync.DeviceRevocation import com.peachpasswords.sync.ManifestDeviceEntry import com.peachpasswords.sync.SASDerivation import com.peachpasswords.sync.canonicalDeviceManifestBody import com.peachpasswords.sync.computeVaultId import com.peachpasswords.sync.noiseNKHkdf2ForTest import com.peachpasswords.sync.noiseNKInitHashForTest import com.peachpasswords.vault.VaultCrypto import org.junit.Assert.assertArrayEquals import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNotEquals import org.junit.Test import java.text.Normalizer /** * MN-4: cross-platform known-answer tests. These pin the byte-level output of the * primitives Android shares with the browser, so a regression on either side * (encoding, params, algorithm) is caught immediately. The Android implementations * are exact ports of the browser source, so these golden vectors hold on both. * * Inputs are fixed and printable; expected outputs are frozen below. */ class CrossPlatformKATTest { private fun hex(b: ByteArray) = b.joinToString("") { "%02x".format(it) } // Argon2id over the non-BMP password "test🔑" (U+1F511, UTF-8) and a fixed salt. private val argonSalt = ByteArray(VaultCrypto.SALT_SIZE) { it.toByte() } @Test fun argon2id_non_bmp_password_vector() { // password "test🔑" (UTF-8) + salt bytes 0x00..0x0f, Argon2id m=64MiB t=3 p=4. // Generated from peach-browser argon2.ts deriveKey after the LV-1 NFC fix — // the emoji is NFC-stable, so this vector holds across the normalization fix. val key = VaultCrypto.deriveKey("test🔑", argonSalt) assertEquals("a519dd4e695d25c6cd69a2f89d51ddcabd952c979f5714ab29fb77f6d1ad3728", hex(key)) } @Test fun nfc_normalization_makes_composed_and_decomposed_match() { // MF-4 / LV-1: "café" decomposed (e + U+0301) and composed (U+00E9) must // derive the same key, matching the browser's password.normalize("NFC"). val nfd = Normalizer.normalize("café", Normalizer.Form.NFD) val nfc = Normalizer.normalize("café", Normalizer.Form.NFC) assertNotEquals(nfd, nfc) assertArrayEquals(VaultCrypto.deriveKey(nfd, argonSalt), VaultCrypto.deriveKey(nfc, argonSalt)) } @Test fun deriveKeyRaw_does_not_normalize() { val nfd = Normalizer.normalize("café", Normalizer.Form.NFD) val nfc = Normalizer.normalize("café", Normalizer.Form.NFC) assertFalse( VaultCrypto.deriveKeyRaw(nfd, argonSalt).contentEquals(VaultCrypto.deriveKeyRaw(nfc, argonSalt)), ) } @Test fun compute_vault_id_vector() { // Blake3-256( masterKey(0x00..0x1f) || "peach-vault-id" ), hex. val masterKey = ByteArray(32) { it.toByte() } assertEquals("ae0ec54041c9ba3e20c0c2d33bdeb2552f3b9a3fb4be3286ec0ef0a61be2fc9c", computeVaultId(masterKey)) } @Test fun device_manifest_v5_canonical_form_vector() { // Pinned byte-for-byte against peach-browser's canonicalise() in // device-manifest.test.ts. If either side reorders a key, renames a // field, or changes number formatting, this string moves and the two // stop verifying each other's signatures. val body = DeviceManifestBody( manifestVersion = DEVICE_MANIFEST_VERSION, issuerDeviceId = "issuer-1", issuedAt = 1_700_000_000_000L, devices = listOf( ManifestDeviceEntry( deviceId = "peer-1", deviceName = "Peer One", deviceType = "android", longTermPublicKeyHex = "aa".repeat(32), signingPublicKeyHex = "bb".repeat(32), wsPort = 49142, relayUrl = "wss://relay.peachpasswords.com", pairedAt = 1_700_000_000_001L, ), ), revoked = listOf( DeviceRevocation( deviceId = "gone-1", longTermPublicKeyHex = "cc".repeat(32), revokedAt = 1_700_000_000_002L, revokedByDeviceId = "issuer-1", ), ), ) val expected = "{\"devices\":[{\"deviceId\":\"peer-1\",\"deviceName\":\"Peer One\"," + "\"deviceType\":\"android\",\"longTermPublicKeyHex\":\"" + "aa".repeat(32) + "\"," + "\"pairedAt\":1700000000001,\"relayUrl\":\"wss://relay.peachpasswords.com\"," + "\"signingPublicKeyHex\":\"" + "bb".repeat(32) + "\"," + "\"vouchDepth\":0,\"wsPort\":49142}],\"issuedAt\":1700000000000,\"issuerDeviceId\":\"issuer-1\"," + "\"manifestVersion\":5,\"revoked\":[{\"deviceId\":\"gone-1\"," + "\"longTermPublicKeyHex\":\"" + "cc".repeat(32) + "\",\"recordVersion\":2," + "\"revokedAt\":1700000000002,\"revokedByDeviceId\":\"issuer-1\",\"signatureHex\":\"\"}]}" assertEquals(expected, canonicalDeviceManifestBody(body)) } @Test fun sas_derivation_vector() { // First 8 bytes are unsigned big-endian, modulo 100_000_000. val handshakeHash = byteArrayOf(0x12, 0x34, 0x56, 0x78) + ByteArray(28) assertEquals("67721216", SASDerivation.derive(handshakeHash)) } // PCH-01-002: the Noise protocol-name init hash and the MixKey/Split HKDF now // derive over BLAKE2s to match the declared pattern name. These vectors are // pinned IDENTICALLY in peach-browser's noise-blake2s-kat.test.ts — a mismatch // here means the two repos' Noise wire derivations have diverged (which would // break cross-device pairing/sync). A SHA-256 revert on either side also fails. @Test fun noise_nk_init_hash_is_blake2s_vector() { assertEquals( "e01688d376ef280a27a8af5c559024a02017585b3763763e4f3c7118a979b09e", hex(noiseNKInitHashForTest()), ) } @Test fun noise_hkdf2_blake2s_vector() { // ck = 0x00..0x1f, ikm = 0x20..0x3f. val ck = ByteArray(32) { it.toByte() } val ikm = ByteArray(32) { (it + 32).toByte() } val (nk1, nk2) = noiseNKHkdf2ForTest(ck, ikm) assertEquals("6a96444e20e8d4c1cee974416acae1c10b3c92886010e54ed94dafb2c3b80ea0", hex(nk1)) assertEquals("57af120b0de7acbe7907ec149c5ae870a2dbb74232b65777ba4123f1f7f888f5", hex(nk2)) } @Test fun device_manifest_v5_canonical_form_omits_null_ws_port_and_relay_url() { // Pinned against peach-browser canonicalise() in device-manifest.test.ts. // Android's manifestEntryMap (DeviceManifestSupport.kt:194) omits wsPort // and relayUrl when null; the browser's canonicalise skips null values // for the same reason (v4.7.4). Both sides must reach the same canonical // bytes — a future regression that re-introduces the null key on either // side will fail this KAT and the browser's mirror test. val body = DeviceManifestBody( manifestVersion = DEVICE_MANIFEST_VERSION, issuerDeviceId = "issuer-1", issuedAt = 1_700_000_000_000L, devices = listOf( ManifestDeviceEntry( deviceId = "peer-1", deviceName = "Peer One", deviceType = "extension", longTermPublicKeyHex = "aa".repeat(32), signingPublicKeyHex = "bb".repeat(32), wsPort = null, relayUrl = null, pairedAt = 1_700_000_000_001L, ), ), revoked = listOf( DeviceRevocation( deviceId = "gone-1", longTermPublicKeyHex = "cc".repeat(32), revokedAt = 1_700_000_000_002L, revokedByDeviceId = "issuer-1", ), ), ) val canonical = canonicalDeviceManifestBody(body) // Canonical does NOT mention wsPort or relayUrl at all. assertFalse("wsPort must be omitted when null but got: $canonical", canonical.contains("wsPort")) assertFalse("relayUrl must be omitted when null but got: $canonical", canonical.contains("relayUrl")) // Pinned byte-for-byte — moving a key or changing any escaping fails this. val expected = "{\"devices\":[{\"deviceId\":\"peer-1\",\"deviceName\":\"Peer One\"," + "\"deviceType\":\"extension\",\"longTermPublicKeyHex\":\"" + "aa".repeat(32) + "\"," + "\"pairedAt\":1700000000001," + "\"signingPublicKeyHex\":\"" + "bb".repeat(32) + "\",\"vouchDepth\":0}]," + "\"issuedAt\":1700000000000,\"issuerDeviceId\":\"issuer-1\"," + "\"manifestVersion\":5," + "\"revoked\":[{\"deviceId\":\"gone-1\"," + "\"longTermPublicKeyHex\":\"" + "cc".repeat(32) + "\",\"recordVersion\":2," + "\"revokedAt\":1700000000002,\"revokedByDeviceId\":\"issuer-1\",\"signatureHex\":\"\"}]}" assertEquals(expected, canonical) } }