Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / rcs / e2e / RustMlsCrypto.kt
RustMlsCrypto.kt91 lines · 3.3 KiBRaw
| 1 | package com.vayunmathur.communicate.data.rcs.e2e |
| 2 | |
| 3 | import com.vayunmathur.library.log.NativeLoader |
| 4 | |
| 5 | |
| 6 | /** |
| 7 | * JNI bridge to the `communicate_mls` Rust crate (OpenMLS, RFC 9420). |
| 8 | * |
| 9 | * Flat byte protocol: every call takes opaque blobs in and returns |
| 10 | * `Array<ByteArray>` (`[storage_out, result…]`) or null on error. Kotlin |
| 11 | * persists `storage_out` (provider snapshot) in Room alongside conversation |
| 12 | * rows; identity bytes live in the MLS identity table. The crate holds no |
| 13 | * state across calls. |
| 14 | * |
| 15 | * MLS follows GSMA Universal Profile 3.0 (standard MLS — the same profile |
| 16 | * Google Messages uses). Key discovery stays closed-loop in v1 (our own RCS |
| 17 | * content-types instead of a federated directory); the crypto itself is |
| 18 | * interop-shaped. |
| 19 | */ |
| 20 | object RustMlsCrypto { |
| 21 | |
| 22 | private const val TAG = "RustMlsCrypto" |
| 23 | |
| 24 | val isAvailable: Boolean = NativeLoader.load("communicate_mls", TAG) |
| 25 | |
| 26 | /** Generate an identity for `e164` UTF-8 bytes. Returns `[empty, identity]`. */ |
| 27 | @JvmStatic external fun generateIdentity(e164: ByteArray): Array<ByteArray>? |
| 28 | |
| 29 | /** Build a key package. `(storage, identity)` → `[storage_out, key_package_tls]`. */ |
| 30 | @JvmStatic external fun buildKeyPackage(storage: ByteArray, identity: ByteArray): Array<ByteArray>? |
| 31 | |
| 32 | /** Create a group. `(storage, identity)` → `[storage_out, group_id]`. */ |
| 33 | @JvmStatic external fun createGroup(storage: ByteArray, identity: ByteArray): Array<ByteArray>? |
| 34 | |
| 35 | /** |
| 36 | * Add members. `(storage, identity, group_id, key_packages)` → |
| 37 | * `[storage_out, commit_tls, welcome_framed]`. Send commit to the group, |
| 38 | * welcome to the joiners (both over RCS). |
| 39 | */ |
| 40 | @JvmStatic external fun addMembers( |
| 41 | storage: ByteArray, |
| 42 | identity: ByteArray, |
| 43 | groupId: ByteArray, |
| 44 | keyPackages: Array<ByteArray>, |
| 45 | ): Array<ByteArray>? |
| 46 | |
| 47 | /** |
| 48 | * Join from a Welcome envelope. `(storage, welcome_framed)` → |
| 49 | * `[storage_out, group_id]`. |
| 50 | */ |
| 51 | @JvmStatic external fun joinGroup(storage: ByteArray, welcome: ByteArray): Array<ByteArray>? |
| 52 | |
| 53 | /** |
| 54 | * Encrypt. `(storage, identity, group_id, plaintext)` → |
| 55 | * `[storage_out, framed_payload]`. Send the payload over RCS. |
| 56 | */ |
| 57 | @JvmStatic external fun encrypt( |
| 58 | storage: ByteArray, |
| 59 | identity: ByteArray, |
| 60 | groupId: ByteArray, |
| 61 | plaintext: ByteArray, |
| 62 | ): Array<ByteArray>? |
| 63 | |
| 64 | /** |
| 65 | * Decrypt. `(storage, group_id, framed_payload)` → |
| 66 | * `[storage_out, is_application(1B), plaintext]`. |
| 67 | */ |
| 68 | @JvmStatic external fun decrypt( |
| 69 | storage: ByteArray, |
| 70 | groupId: ByteArray, |
| 71 | payload: ByteArray, |
| 72 | ): Array<ByteArray>? |
| 73 | |
| 74 | /** |
| 75 | * Remove members by leaf index. `(storage, identity, group_id, indices_csv)` → |
| 76 | * `[storage_out, commit_tls]`. Indices are ASCII decimal, comma-separated. |
| 77 | */ |
| 78 | @JvmStatic external fun removeMembers( |
| 79 | storage: ByteArray, |
| 80 | identity: ByteArray, |
| 81 | groupId: ByteArray, |
| 82 | indicesCsv: ByteArray, |
| 83 | ): Array<ByteArray>? |
| 84 | |
| 85 | /** Framing probe: 0=welcome, 1=message, 2=unknown. */ |
| 86 | @JvmStatic external fun payloadKind(payload: ByteArray): Int |
| 87 | |
| 88 | /** Magic prefixes for Kotlin-side routing without a JNI round trip. */ |
| 89 | @JvmStatic external fun welcomeMagic(): ByteArray? |
| 90 | @JvmStatic external fun messageMagic(): ByteArray? |
| 91 | } |