Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / library / e2ee-p2p / src / test / java / com / vayunmathur / e2ee / PqcTest.kt

PqcTest.kt60 lines · 2.3 KiBRaw
1package com.vayunmathur.e2ee
2
3import kotlin.test.BeforeTest
4import kotlin.test.Test
5import kotlin.test.assertContentEquals
6import kotlin.test.assertFalse
7import kotlin.test.assertTrue
8
9/**
10 * These exercise the real native PQC (libe2ee_pqc.so), so they only run on-device
11 * / when the native lib is loadable. Byte-level interop with the previously-
12 * deployed Bouncy Castle encoding is covered by the Rust crate's own tests
13 * (library/e2ee-p2p/src/main/rust/src/tests.rs) against captured BC vectors.
14 */
15class PqcTest {
16 private var nativeAvailable = false
17
18 @BeforeTest
19 fun checkNative() {
20 nativeAvailable = runCatching { Pqc.generateKem() }.isSuccess
21 // The host lib is built for JVM tests (cargoBuildHost, like every other
22 // Rust module): unavailable here is a build failure, not a skip.
23 assertTrue(nativeAvailable, "host e2ee_pqc lib unavailable: cargoBuildHost must produce it for JVM tests")
24 }
25
26 @Test
27 fun ml_kem_encrypt_decrypt_roundtrip() {
28 assertTrue(nativeAvailable)
29 val (kemPub, kemPriv) = Pqc.generateKem()
30 val (dsaPub, _) = Pqc.generateDsa()
31 val bundle = Pqc.bundle(kemPub, dsaPub)
32 val msg = "post-quantum hello — a payload comfortably longer than one KEM ciphertext".encodeToByteArray()
33 val ct = Pqc.encryptTo(bundle, msg)
34 assertContentEquals(msg, Pqc.decrypt(kemPriv, ct))
35 }
36
37 @Test
38 fun ml_dsa_sign_verify() {
39 assertTrue(nativeAvailable)
40 val (kemPub, _) = Pqc.generateKem()
41 val (dsaPub, dsaPriv) = Pqc.generateDsa()
42 val bundle = Pqc.bundle(kemPub, dsaPub)
43 val data = "authenticate me".encodeToByteArray()
44 val sig = Pqc.signWith(dsaPriv, data)
45 assertTrue(Pqc.verify(bundle, data, sig))
46 assertFalse(Pqc.verify(bundle, "tampered".encodeToByteArray(), sig))
47
48 val (ok, od) = Pqc.generateKem().first to Pqc.generateDsa().first
49 val other = Pqc.bundle(ok, od)
50 assertFalse(Pqc.verify(other, data, sig))
51 }
52
53 @Test
54 fun security_code_matches_both_sides() {
55 assertTrue(nativeAvailable)
56 val a = Pqc.bundle(Pqc.generateKem().first, Pqc.generateDsa().first)
57 val b = Pqc.bundle(Pqc.generateKem().first, Pqc.generateDsa().first)
58 assertTrue(Pqc.securityCode(a, b) == Pqc.securityCode(b, a)) // order-independent
59 }
60}