CrossPlatformKATTest.kt

2.9 KB · raw

1package com.peachpasswords23import com.peachpasswords.sync.SASDerivation4import com.peachpasswords.sync.computeVaultId5import com.peachpasswords.vault.VaultCrypto6import org.junit.Assert.assertArrayEquals7import org.junit.Assert.assertEquals8import org.junit.Assert.assertFalse9import org.junit.Assert.assertNotEquals10import org.junit.Test11import java.text.Normalizer1213/**14 * MN-4: cross-platform known-answer tests. These pin the byte-level output of the15 * primitives Android shares with the browser, so a regression on either side16 * (encoding, params, algorithm) is caught immediately. The Android implementations17 * are exact ports of the browser source, so these golden vectors hold on both.18 *19 * Inputs are fixed and printable; expected outputs are frozen below.20 */21class CrossPlatformKATTest {2223    private fun hex(b: ByteArray) = b.joinToString("") { "%02x".format(it) }2425    // Argon2id over the non-BMP password "test🔑" (U+1F511, UTF-8) and a fixed salt.26    private val argonSalt = ByteArray(VaultCrypto.SALT_SIZE) { it.toByte() }2728    @Test fun argon2id_non_bmp_password_vector() {29        // 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.32        val key = VaultCrypto.deriveKey("test🔑", argonSalt)33        assertEquals("a519dd4e695d25c6cd69a2f89d51ddcabd952c979f5714ab29fb77f6d1ad3728", hex(key))34    }3536    @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    }4445    @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        )51    }5253    @Test fun compute_vault_id_vector() {54        // Blake3-256( masterKey(0x00..0x1f) || "peach-vault-id" ), hex.55        val masterKey = ByteArray(32) { it.toByte() }56        assertEquals("ae0ec54041c9ba3e20c0c2d33bdeb2552f3b9a3fb4be3286ec0ef0a61be2fc9c", computeVaultId(masterKey))57    }5859    @Test fun sas_derivation_vector() {60        // First 4 bytes 0x12345678 = 305419896; 305419896 mod 1_000_000 = 419896.61        val handshakeHash = byteArrayOf(0x12, 0x34, 0x56, 0x78) + ByteArray(28)62        assertEquals("419896", SASDerivation.derive(handshakeHash))63    }64}