package com.vayunmathur.e2ee import javax.crypto.AEADBadTagException import kotlin.test.Test import kotlin.test.assertContentEquals import kotlin.test.assertEquals import kotlin.test.assertFailsWith import kotlin.test.assertFalse import kotlin.test.assertNotEquals import kotlin.test.assertTrue import kotlinx.coroutines.runBlocking /** HKDF vectors, AES-GCM round-trips, security codes, identity store lifecycle. */ class E2eeHkdfIdentityTest { private fun hex(s: String): ByteArray = ByteArray(s.length / 2) { s.substring(it * 2, it * 2 + 2).toInt(16).toByte() } private fun ByteArray.toHex(): String = joinToString("") { "%02x".format(it.toInt() and 0xFF) } // RFC 5869 Test Case 1 (SHA-256). @Test fun hkdfRfc5869TestCase1() { val ikm = ByteArray(22) { 0x0b } val salt = hex("000102030405060708090a0b0c") val info = hex("f0f1f2f3f4f5f6f7f8f9") val okm = Hkdf.derive(ikm, info, 42, salt) assertEquals( "3cb25f25faacd57a90434f64d0362f2a" + "2d2d0a90cf1a5a4c5db02d56ecc4c5bf" + "34007208d5b887185865", okm.toHex(), ) } @Test fun hkdfLengthsInfoSeparationAndLimit() { val ikm = "secret".encodeToByteArray() assertEquals(32, Hkdf.derive(ikm, "a".encodeToByteArray(), 32).size) assertEquals(16, Hkdf.derive(ikm, "a".encodeToByteArray(), 16).size) assertEquals(100, Hkdf.derive(ikm, "a".encodeToByteArray(), 100).size) // Different info strings derive independent keys (domain separation). val a = Hkdf.derive(ikm, "control".encodeToByteArray(), 32) val b = Hkdf.derive(ikm, "media".encodeToByteArray(), 32) assertFalse(a.contentEquals(b)) // Prefix property: shorter derivations are prefixes of longer ones. val full = Hkdf.derive(ikm, "x".encodeToByteArray(), 64) assertContentEquals(full.copyOf(32), Hkdf.derive(ikm, "x".encodeToByteArray(), 32)) // Default (zero) salt still works; explicit salt changes the output. assertFalse( Hkdf.derive(ikm, "x".encodeToByteArray(), 32) .contentEquals(Hkdf.derive(ikm, "x".encodeToByteArray(), 32, ByteArray(32) { 1 })), ) assertFailsWith { Hkdf.derive(ikm, "x".encodeToByteArray(), 255 * 32 + 1) } } @Test fun aesRoundTripTamperAndWrongKey() { val key = E2ee.newContentKey() assertEquals(32, key.size) assertFalse(E2ee.newContentKey().contentEquals(E2ee.newContentKey())) val plain = "vault contents — ütf8 ✓".encodeToByteArray() val ct = E2ee.aesEncrypt(key, plain) // IV (12) prepended + GCM tag (16) appended by the cipher. assertEquals(plain.size + 12 + 16, ct.size) assertContentEquals(plain, E2ee.aesDecrypt(key, ct)) assertContentEquals(ByteArray(0), E2ee.aesDecrypt(key, E2ee.aesEncrypt(key, ByteArray(0)))) // Tampered ciphertext fails authentication rather than decrypting. val tampered = ct.copyOf().also { it[it.size - 1] = (it[it.size - 1] + 1).toByte() } assertFailsWith { E2ee.aesDecrypt(key, tampered) } assertFailsWith { E2ee.aesDecrypt(E2ee.newContentKey(), ct) } // Random IV: same plaintext encrypts differently each time. assertFalse(ct.contentEquals(E2ee.aesEncrypt(key, plain))) } @Test fun securityCodeSymmetricFormattedAndKeySensitive() { val a = byteArrayOf(1, 2, 3, 4) val b = byteArrayOf(5, 6, 7, 8) val code = SecurityCode.compute(a, b) assertEquals(code, SecurityCode.compute(b, a)) // Six groups of five digits. assertTrue(code.matches(Regex("""\d{5}( \d{5}){5}""")), "unexpected format: $code") assertNotEquals(code, SecurityCode.compute(a, byteArrayOf(9, 9, 9))) assertEquals(code, SecurityCode.compute(a.copyOf(), b.copyOf())) } private class MemStore : E2eeKeyStore { val map = mutableMapOf() var writes = 0 override suspend fun getBytes(name: String): ByteArray? = map[name] override suspend fun setBytes(name: String, value: ByteArray, onlyIfAbsent: Boolean) { if (onlyIfAbsent && map.containsKey(name)) return map[name] = value writes++ } } @Test fun identityLoadOrCreatePersistsAndReloads() = runBlocking { // Native like PqcTest: the host lib is built for JVM tests, so an // unavailable lib is a build failure, not a silent pass. val nativeAvailable = runCatching { Pqc.generateKem() }.isSuccess assertTrue(nativeAvailable, "host e2ee_pqc lib unavailable: cargoBuildHost must produce it for JVM tests") val store = MemStore() val first = PqcIdentity.loadOrCreate(store) assertTrue(store.map.keys.containsAll(setOf("pqcKemPub", "pqcKemPriv", "pqcDsaPub", "pqcDsaPriv"))) assertTrue(first.publicBundle.isNotEmpty()) // Second load reuses the persisted identity, byte for byte. val second = PqcIdentity.loadOrCreate(store) assertContentEquals(first.publicBundle, second.publicBundle) // Custom prefix keeps stores apart. val other = PqcIdentity.loadOrCreate(store, prefix = "alt") assertFalse(first.publicBundle.contentEquals(other.publicBundle)) assertTrue(store.map.containsKey("altKemPub")) // Encrypt-to-self / sign-verify through the loaded identity. val ct = Pqc.encryptTo(first.publicBundle, "hello".encodeToByteArray()) assertContentEquals("hello".encodeToByteArray(), first.decrypt(ct)) val sig = first.sign("data".encodeToByteArray()) assertTrue(Pqc.verify(first.publicBundle, "data".encodeToByteArray(), sig)) } @Test fun identityIncompleteStoreRegenerates() = runBlocking { val nativeAvailable = runCatching { Pqc.generateKem() }.isSuccess assertTrue(nativeAvailable, "host e2ee_pqc lib unavailable: cargoBuildHost must produce it for JVM tests") val store = MemStore() // Only half an identity persisted: treated as absent, not as a broken identity. store.map["pqcKemPub"] = byteArrayOf(1, 2, 3) store.map["pqcKemPriv"] = byteArrayOf(4, 5, 6) val identity = PqcIdentity.loadOrCreate(store) // The onlyIfAbsent winner for KemPub is the stale value, so the re-read bundle // is built from persisted (stale-Kem + fresh-DSA) parts; what matters is that // loadOrCreate completes and persists a full set rather than erroring. assertTrue(store.map.containsKey("pqcDsaPub")) assertTrue(identity.publicBundle.isNotEmpty()) } private fun assertIdentityUsable(identity: PqcIdentity) { val payload = "pair-check payload".encodeToByteArray() val ciphertext = Pqc.encryptTo(identity.publicBundle, payload) assertContentEquals(payload, identity.decrypt(ciphertext)) val signed = "pair-check signed".encodeToByteArray() assertTrue(Pqc.verify(identity.publicBundle, signed, identity.sign(signed))) } @Test fun identityIncompleteStoreHealsToUsableIdentity() = runBlocking { val nativeAvailable = runCatching { Pqc.generateKem() }.isSuccess assertTrue(nativeAvailable, "host e2ee_pqc lib unavailable: cargoBuildHost must produce it for JVM tests") val store = MemStore() // Regression: previously the stale 3-byte halves won the onlyIfAbsent stores, so // the re-read identity was complete on paper but its KEM half could not even be // encapsulated to, and that state then persisted for every later load. store.map["pqcKemPub"] = byteArrayOf(1, 2, 3) store.map["pqcKemPriv"] = byteArrayOf(4, 5, 6) val identity = PqcIdentity.loadOrCreate(store) assertIdentityUsable(identity) // The healed identity is the one persisted: a reload yields it byte for byte. val reloaded = PqcIdentity.loadOrCreate(store) assertContentEquals(identity.publicBundle, reloaded.publicBundle) assertIdentityUsable(reloaded) } @Test fun identityLoneValidKemPubIsNotPairedWithFreshPrivate() = runBlocking { val nativeAvailable = runCatching { Pqc.generateKem() }.isSuccess assertTrue(nativeAvailable, "host e2ee_pqc lib unavailable: cargoBuildHost must produce it for JVM tests") val store = MemStore() // Regression: a crash after the first store kept this valid KEM public half; the // old code paired it with a different generation's private half, so ciphertexts // to the advertised bundle failed AES-GCM on decrypt. val (kemPub, _) = Pqc.generateKem() store.map["pqcKemPub"] = kemPub val identity = PqcIdentity.loadOrCreate(store) assertIdentityUsable(identity) val reloaded = PqcIdentity.loadOrCreate(store) assertContentEquals(identity.publicBundle, reloaded.publicBundle) } @Test fun identityMismatchedCompletePairsAreReplaced() = runBlocking { val nativeAvailable = runCatching { Pqc.generateKem() }.isSuccess assertTrue(nativeAvailable, "host e2ee_pqc lib unavailable: cargoBuildHost must produce it for JVM tests") val store = MemStore() // Regression: two interleaved first-time stores (or a torn write) can leave a // complete set whose public and private halves come from different generations. // Presence of all four entries must not be taken as a usable identity. val (kemPubA, _) = Pqc.generateKem() val (_, kemPrivB) = Pqc.generateKem() val (dsaPubA, _) = Pqc.generateDsa() val (_, dsaPrivB) = Pqc.generateDsa() store.map["pqcKemPub"] = kemPubA store.map["pqcKemPriv"] = kemPrivB store.map["pqcDsaPub"] = dsaPubA store.map["pqcDsaPriv"] = dsaPrivB val identity = PqcIdentity.loadOrCreate(store) assertIdentityUsable(identity) val reloaded = PqcIdentity.loadOrCreate(store) assertContentEquals(identity.publicBundle, reloaded.publicBundle) assertIdentityUsable(reloaded) } }