Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / signal / RustSignalCryptoSignalPass3Test.kt
RustSignalCryptoSignalPass3Test.kt86 lines · 4.0 KiBRaw
| 1 | package com.vayunmathur.communicate.signal |
| 2 | |
| 3 | import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 4 | import com.vayunmathur.communicate.data.signal.e2e.RustSignalCrypto |
| 5 | import com.vayunmathur.communicate.data.whatsapp.e2e.RustWhatsAppCrypto |
| 6 | import kotlin.test.Test |
| 7 | import kotlin.test.assertEquals |
| 8 | import kotlin.test.assertFalse |
| 9 | import kotlin.test.assertNotNull |
| 10 | import kotlin.test.assertFailsWith |
| 11 | import kotlin.test.assertNull |
| 12 | import kotlin.test.assertTrue |
| 13 | import org.junit.runner.RunWith |
| 14 | |
| 15 | /** |
| 16 | * Pass 3: RustSignalCrypto at the JNI boundary. The host library exports only |
| 17 | * processPreKeyBundle / markKyberPreKeyUsed / sealedSender* for this class |
| 18 | * (see lead-inbox note); the primitive key ops live on RustWhatsAppCrypto in |
| 19 | * the same crate and are used here to build bundle inputs. |
| 20 | */ |
| 21 | @RunWith(AndroidJUnit4::class) |
| 22 | class RustSignalCryptoSignalPass3Test { |
| 23 | |
| 24 | @Test fun libraryLoadsAndPrimitiveOpsWorkViaSharedCrate() { |
| 25 | assertTrue(RustSignalCrypto.isAvailable) |
| 26 | assertTrue(RustWhatsAppCrypto.isAvailable) |
| 27 | val pair = assertNotNull(RustWhatsAppCrypto.generateKeyPair()) |
| 28 | assertEquals(64, pair.size) |
| 29 | val priv = pair.copyOfRange(0, 32) |
| 30 | val pub = pair.copyOfRange(32, 64) |
| 31 | assertTrue(pub.contentEquals(RustWhatsAppCrypto.publicFromPrivate(priv))) |
| 32 | val sig = assertNotNull(RustWhatsAppCrypto.sign(priv, "m".encodeToByteArray())) |
| 33 | assertTrue(RustWhatsAppCrypto.verify(pub, "m".encodeToByteArray(), sig)) |
| 34 | } |
| 35 | |
| 36 | @Test fun kyberReplayGuardDeduplicates() { |
| 37 | val baseKey = ByteArray(32) { it.toByte() } |
| 38 | // First sight of a (kyberId, signedEcId, baseKey) tuple is accepted; |
| 39 | // the replay of the same tuple is rejected. |
| 40 | assertTrue(RustSignalCrypto.markKyberPreKeyUsed(9101, 7, baseKey)) |
| 41 | assertFalse(RustSignalCrypto.markKyberPreKeyUsed(9101, 7, baseKey)) |
| 42 | // A different base key or id is a different tuple. |
| 43 | assertTrue(RustSignalCrypto.markKyberPreKeyUsed(9101, 7, ByteArray(32) { 9 })) |
| 44 | assertTrue(RustSignalCrypto.markKyberPreKeyUsed(9102, 7, baseKey)) |
| 45 | } |
| 46 | |
| 47 | @Test fun processPreKeyBundleRejectsBadSignature() { |
| 48 | fun keyPair(): Pair<ByteArray, ByteArray> { |
| 49 | val blob = assertNotNull(RustWhatsAppCrypto.generateKeyPair()) |
| 50 | return blob.copyOfRange(0, 32) to blob.copyOfRange(32, 64) |
| 51 | } |
| 52 | val (alicePriv, alicePub) = keyPair() |
| 53 | val (bobIdentityPriv, bobIdentityPub) = keyPair() |
| 54 | val (bobSignedPriv, bobSignedPub) = keyPair() |
| 55 | val (_, bobOneTimePub) = keyPair() |
| 56 | // Signature over the signed prekey by the WRONG key (Alice, not Bob). |
| 57 | val badSig = assertNotNull(RustWhatsAppCrypto.sign(alicePriv, bobSignedPub)) |
| 58 | // The JNI layer surfaces libsignal's rejection as an exception. |
| 59 | val error = assertFailsWith<RuntimeException> { |
| 60 | RustSignalCrypto.processPreKeyBundle( |
| 61 | alicePriv, alicePub, |
| 62 | 1111, 2222, |
| 63 | 10, bobOneTimePub, |
| 64 | 2, bobSignedPub, badSig, |
| 65 | bobIdentityPub, |
| 66 | 5, ByteArray(1569), ByteArray(64), ByteArray(1568)) |
| 67 | } |
| 68 | assertTrue(error.message!!.contains("signature", ignoreCase = true), "${error.message}") |
| 69 | } |
| 70 | |
| 71 | @Test fun sealedSenderStubEncryptsButDecryptRejects() { |
| 72 | // The JNI sealed-sender stub produces ciphertext without any network |
| 73 | // or certificate fetch, and decrypts its own output back. |
| 74 | val out = RustSignalCrypto.sealedSenderEncrypt( |
| 75 | "hello".encodeToByteArray(), "+15550100", 1) |
| 76 | assertNotNull(out) |
| 77 | assertTrue(out.isNotEmpty()) |
| 78 | // The stub's decrypt side does not accept its own encrypt output |
| 79 | // (tag mismatch): it is a placeholder, not a round-trip path - the |
| 80 | // production path uses libsignal's SealedSessionCipher instead. |
| 81 | val error = assertFailsWith<RuntimeException> { |
| 82 | RustSignalCrypto.sealedSenderDecrypt(out) |
| 83 | } |
| 84 | assertTrue(error.message!!.contains("sealed", ignoreCase = true), "${error.message}") |
| 85 | } |
| 86 | } |