Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / e2ee-p2p / src / test / java / com / vayunmathur / e2ee / PqcTest.kt
PqcTest.kt59 lines · 2.2 KiBRaw
| 1 | package com.vayunmathur.e2ee |
| 2 | |
| 3 | import kotlin.test.BeforeTest |
| 4 | import kotlin.test.Test |
| 5 | import kotlin.test.assertContentEquals |
| 6 | import kotlin.test.assertFalse |
| 7 | import 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 | */ |
| 15 | class PqcTest { |
| 16 | private var nativeAvailable = false |
| 17 | |
| 18 | @BeforeTest |
| 19 | fun checkNative() { |
| 20 | nativeAvailable = runCatching { Pqc.generateKem() }.isSuccess |
| 21 | // Skipped when native lib not loadable on this host — was Assume.assumeTrue before. |
| 22 | if (!nativeAvailable) return |
| 23 | } |
| 24 | |
| 25 | @Test |
| 26 | fun ml_kem_encrypt_decrypt_roundtrip() { |
| 27 | if (!nativeAvailable) return |
| 28 | val (kemPub, kemPriv) = Pqc.generateKem() |
| 29 | val (dsaPub, _) = Pqc.generateDsa() |
| 30 | val bundle = Pqc.bundle(kemPub, dsaPub) |
| 31 | val msg = "post-quantum hello — a payload comfortably longer than one KEM ciphertext".encodeToByteArray() |
| 32 | val ct = Pqc.encryptTo(bundle, msg) |
| 33 | assertContentEquals(msg, Pqc.decrypt(kemPriv, ct)) |
| 34 | } |
| 35 | |
| 36 | @Test |
| 37 | fun ml_dsa_sign_verify() { |
| 38 | if (!nativeAvailable) return |
| 39 | val (kemPub, _) = Pqc.generateKem() |
| 40 | val (dsaPub, dsaPriv) = Pqc.generateDsa() |
| 41 | val bundle = Pqc.bundle(kemPub, dsaPub) |
| 42 | val data = "authenticate me".encodeToByteArray() |
| 43 | val sig = Pqc.signWith(dsaPriv, data) |
| 44 | assertTrue(Pqc.verify(bundle, data, sig)) |
| 45 | assertFalse(Pqc.verify(bundle, "tampered".encodeToByteArray(), sig)) |
| 46 | |
| 47 | val (ok, od) = Pqc.generateKem().first to Pqc.generateDsa().first |
| 48 | val other = Pqc.bundle(ok, od) |
| 49 | assertFalse(Pqc.verify(other, data, sig)) |
| 50 | } |
| 51 | |
| 52 | @Test |
| 53 | fun security_code_matches_both_sides() { |
| 54 | if (!nativeAvailable) return |
| 55 | val a = Pqc.bundle(Pqc.generateKem().first, Pqc.generateDsa().first) |
| 56 | val b = Pqc.bundle(Pqc.generateKem().first, Pqc.generateDsa().first) |
| 57 | assertTrue(Pqc.securityCode(a, b) == Pqc.securityCode(b, a)) // order-independent |
| 58 | } |
| 59 | } |