e2ee-openpgp: unify JNI surface onto shared jni_fn! macro
- Commit
04a336f987194903fb8993aa4a52ed4d554bd104- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 19:30 UTC (4 days ago)
- Parents
4429568
LOC Phase 5 rollout. pgp_jni.rs was the one crate that already had a jni_fn! macro, private to the file and shaped for a single JByteArray argument; its two invocations and the seven hand-written functions all expand through the shared jni_bridge::jni_fn! now, and the private macro is deleted. The two former macro invocations spell out the exact bodies the private macro generated (inp / call / out_bytes with null on None); the seven hand-written bodies are verbatim, verified token-for-token. Handles and exception behaviour unchanged - every body still returns Ok with a null pointer on failure and LogErrorAndDefault still maps errors. Lines: pgp_jni.rs 182 -> 107 (-75, -41.2%); structure_check library/e2ee-openpgp module 1,289 after, structure: OK. Cargo.lock gains only the jni_bridge edge for e2ee_pgp. Verified: nm -D on libe2ee_pgp.so before/after - exported symbol set identical (9/9 Java_ PgpNative symbols); cargo test -p e2ee_pgp: 27 passed; JVM bridge tests :library:e2ee-openpgp:testDebugUnitTest PgpApiTest: 10/10 passed (run excludes the pre-existing rustFileLength ratchet); cargo fmt --check -p e2ee_pgp clean.
Diff
Cargo.lock
diff --git a/Cargo.lock b/Cargo.lockindex 7e13f36193..9269411764 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1667,6 +1667,7 @@ version = "0.1.0" dependencies = [ "base64 0.23.1", "jni",+ "jni_bridge", "pgp", "rand 0.8.6", "smallvec",
library/e2ee-openpgp/src/main/rust/Cargo.toml
diff --git a/library/e2ee-openpgp/src/main/rust/Cargo.toml b/library/e2ee-openpgp/src/main/rust/Cargo.tomlindex 6c94081745..02885aaafd 100644--- a/library/e2ee-openpgp/src/main/rust/Cargo.toml+++ b/library/e2ee-openpgp/src/main/rust/Cargo.toml@@ -11,6 +11,7 @@ crate-type = ["cdylib", "rlib"] [dependencies] jni.workspace = true+jni_bridge = { path = "../../../../../library/jni-bridge/src/main/rust" } # OpenPGP packet engine for SecureMail (generate/armor/encrypt/decrypt). # Same crate as the relay server would use; the JNI surface below exposes only # what the app needs (no WKD/SMTP — those stay in Kotlin via library:network).
library/e2ee-openpgp/src/main/rust/src/pgp_jni.rs
diff --git a/library/e2ee-openpgp/src/main/rust/src/pgp_jni.rs b/library/e2ee-openpgp/src/main/rust/src/pgp_jni.rsindex 99ef7821ba..627d4cef22 100644--- a/library/e2ee-openpgp/src/main/rust/src/pgp_jni.rs+++ b/library/e2ee-openpgp/src/main/rust/src/pgp_jni.rs@@ -6,7 +6,8 @@ use crate::pgp::*; use jni::objects::{JByteArray, JClass}; use jni::sys::{jbyteArray, jobjectArray};-use jni::{Env, EnvUnowned};+use jni::Env;+use jni_bridge::jni_fn; fn inp(env: &Env, a: &JByteArray) -> Option<Vec<u8>> { env.convert_byte_array(a).ok()@@ -30,153 +31,77 @@ fn out_array(env: &mut Env, items: &[&[u8]]) -> jobjectArray { } arr.into_raw() }-macro_rules! jni_fn {- ($name:ident, $call:expr) => {- #[no_mangle]- pub extern "system" fn $name<'l>(- mut env: EnvUnowned<'l>,- _c: JClass<'l>,- a: JByteArray<'l>,- ) -> jbyteArray {- env.with_env(|e| -> jni::errors::Result<jbyteArray> {- Ok(match inp(e, &a).and_then(|x| $call(&x)) {- Some(v) => out_bytes(e, v.as_bytes()),- None => std::ptr::null_mut(),- })- })- .resolve::<jni::errors::LogErrorAndDefault>()- }- };-}-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_e2ee_PgpNative_nativePgpGenerate<'l>(- mut env: EnvUnowned<'l>,- _c: JClass<'l>,- uid: JByteArray<'l>,- pw: JByteArray<'l>,-) -> jobjectArray {- env.with_env(|e| -> jni::errors::Result<jobjectArray> {- Ok(match (inp(e, &uid), inp(e, &pw)) {- (Some(a), Some(b)) => match generate_from_bytes(&a, &b) {- Some((s, p, f)) => out_array(e, &[s.as_bytes(), p.as_bytes(), f.as_bytes()]),- None => std::ptr::null_mut(),- },- _ => std::ptr::null_mut(),- })- })- .resolve::<jni::errors::LogErrorAndDefault>()-}-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_e2ee_PgpNative_nativePgpNormalizeKey<'l>(- mut env: EnvUnowned<'l>,- _c: JClass<'l>,- input: JByteArray<'l>,-) -> jbyteArray {- env.with_env(|e| -> jni::errors::Result<jbyteArray> {- Ok(match inp(e, &input).and_then(|b| pgp_normalize_key(&b)) {- Some(v) => out_bytes(e, v.as_bytes()),+jni_fn! { Java_com_vayunmathur_e2ee_PgpNative_nativePgpGenerate -> jobjectArray; (_c: JClass<'l>, uid: JByteArray<'l>, pw: JByteArray<'l>) |e| {+ Ok(match (inp(e, &uid), inp(e, &pw)) {+ (Some(a), Some(b)) => match generate_from_bytes(&a, &b) {+ Some((s, p, f)) => out_array(e, &[s.as_bytes(), p.as_bytes(), f.as_bytes()]), None => std::ptr::null_mut(),- })+ },+ _ => std::ptr::null_mut(), })- .resolve::<jni::errors::LogErrorAndDefault>()-}-jni_fn!(Java_com_vayunmathur_e2ee_PgpNative_nativePgpFingerprint, fingerprint_from_bytes);-jni_fn!(Java_com_vayunmathur_e2ee_PgpNative_nativePgpArmorMessage, armor_from_bytes);-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_e2ee_PgpNative_nativePgpEncrypt<'l>(- mut env: EnvUnowned<'l>,- _c: JClass<'l>,- armor: JByteArray<'l>,- plain: JByteArray<'l>,-) -> jbyteArray {- env.with_env(|e| -> jni::errors::Result<jbyteArray> {- Ok(match (inp(e, &armor), inp(e, &plain)) {- (Some(a), Some(p)) => match encrypt_from_bytes(&a, &p) {- Some(v) => out_bytes(e, &v),- None => std::ptr::null_mut(),- },- _ => std::ptr::null_mut(),- })+} }+jni_fn! { Java_com_vayunmathur_e2ee_PgpNative_nativePgpNormalizeKey -> jbyteArray; (_c: JClass<'l>, input: JByteArray<'l>) |e| {+ Ok(match inp(e, &input).and_then(|b| pgp_normalize_key(&b)) {+ Some(v) => out_bytes(e, v.as_bytes()),+ None => std::ptr::null_mut(), })- .resolve::<jni::errors::LogErrorAndDefault>()-}-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_e2ee_PgpNative_nativePgpDecrypt<'l>(- mut env: EnvUnowned<'l>,- _c: JClass<'l>,- secret: JByteArray<'l>,- pw: JByteArray<'l>,- ct: JByteArray<'l>,-) -> jbyteArray {- env.with_env(|e| -> jni::errors::Result<jbyteArray> {- Ok(match (inp(e, &secret), inp(e, &pw), inp(e, &ct)) {- (Some(a), Some(b), Some(c)) => match decrypt_from_bytes(&a, &b, &c) {- Some(v) => out_bytes(e, &v),- None => std::ptr::null_mut(),- },- _ => std::ptr::null_mut(),- })+} }+jni_fn! { Java_com_vayunmathur_e2ee_PgpNative_nativePgpFingerprint -> jbyteArray; (_c: JClass<'l>, a: JByteArray<'l>) |e| {+ Ok(match inp(e, &a).and_then(|x| fingerprint_from_bytes(&x)) {+ Some(v) => out_bytes(e, v.as_bytes()),+ None => std::ptr::null_mut(), })- .resolve::<jni::errors::LogErrorAndDefault>()-}-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_e2ee_PgpNative_nativePgpEncryptSigned<'l>(- mut env: EnvUnowned<'l>,- _c: JClass<'l>,- recip: JByteArray<'l>,- secret: JByteArray<'l>,- pw: JByteArray<'l>,- plain: JByteArray<'l>,-) -> jbyteArray {- env.with_env(|e| -> jni::errors::Result<jbyteArray> {- Ok(match (inp(e, &recip), inp(e, &secret), inp(e, &pw), inp(e, &plain)) {- (Some(a), Some(b), Some(c), Some(d)) => match encrypt_signed_from_bytes(&a, &b, &c, &d)- {- Some(v) => out_bytes(e, &v),- None => std::ptr::null_mut(),- },- _ => std::ptr::null_mut(),- })+} }+jni_fn! { Java_com_vayunmathur_e2ee_PgpNative_nativePgpArmorMessage -> jbyteArray; (_c: JClass<'l>, a: JByteArray<'l>) |e| {+ Ok(match inp(e, &a).and_then(|x| armor_from_bytes(&x)) {+ Some(v) => out_bytes(e, v.as_bytes()),+ None => std::ptr::null_mut(), })- .resolve::<jni::errors::LogErrorAndDefault>()-}-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_e2ee_PgpNative_nativePgpSignDetached<'l>(- mut env: EnvUnowned<'l>,- _c: JClass<'l>,- secret: JByteArray<'l>,- pw: JByteArray<'l>,- data: JByteArray<'l>,-) -> jbyteArray {- env.with_env(|e| -> jni::errors::Result<jbyteArray> {- Ok(match (inp(e, &secret), inp(e, &pw), inp(e, &data)) {- (Some(a), Some(b), Some(c)) => match sign_detached_from_bytes(&a, &b, &c) {- Some(v) => out_bytes(e, &v),- None => std::ptr::null_mut(),- },- _ => std::ptr::null_mut(),- })+} }+jni_fn! { Java_com_vayunmathur_e2ee_PgpNative_nativePgpEncrypt -> jbyteArray; (_c: JClass<'l>, armor: JByteArray<'l>, plain: JByteArray<'l>) |e| {+ Ok(match (inp(e, &armor), inp(e, &plain)) {+ (Some(a), Some(p)) => match encrypt_from_bytes(&a, &p) {+ Some(v) => out_bytes(e, &v),+ None => std::ptr::null_mut(),+ },+ _ => std::ptr::null_mut(), })- .resolve::<jni::errors::LogErrorAndDefault>()-}-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_e2ee_PgpNative_nativePgpDecryptVerify<'l>(- mut env: EnvUnowned<'l>,- _c: JClass<'l>,- secret: JByteArray<'l>,- pw: JByteArray<'l>,- ct: JByteArray<'l>,- verifier: JByteArray<'l>,-) -> jobjectArray {- env.with_env(|e| -> jni::errors::Result<jobjectArray> {- Ok(match (inp(e, &secret), inp(e, &pw), inp(e, &ct), inp(e, &verifier)) {- (Some(a), Some(b), Some(c), Some(d)) => match decrypt_verify_from_bytes(&a, &b, &c, &d)- {- Some((p, f)) => out_array(e, &[&p, f.as_bytes()]),- None => std::ptr::null_mut(),- },- _ => std::ptr::null_mut(),- })+} }+jni_fn! { Java_com_vayunmathur_e2ee_PgpNative_nativePgpDecrypt -> jbyteArray; (_c: JClass<'l>, secret: JByteArray<'l>, pw: JByteArray<'l>, ct: JByteArray<'l>) |e| {+ Ok(match (inp(e, &secret), inp(e, &pw), inp(e, &ct)) {+ (Some(a), Some(b), Some(c)) => match decrypt_from_bytes(&a, &b, &c) {+ Some(v) => out_bytes(e, &v),+ None => std::ptr::null_mut(),+ },+ _ => std::ptr::null_mut(), })- .resolve::<jni::errors::LogErrorAndDefault>()-}+} }+jni_fn! { Java_com_vayunmathur_e2ee_PgpNative_nativePgpEncryptSigned -> jbyteArray; (_c: JClass<'l>, recip: JByteArray<'l>, secret: JByteArray<'l>, pw: JByteArray<'l>, plain: JByteArray<'l>) |e| {+ Ok(match (inp(e, &recip), inp(e, &secret), inp(e, &pw), inp(e, &plain)) {+ (Some(a), Some(b), Some(c), Some(d)) => match encrypt_signed_from_bytes(&a, &b, &c, &d)+ {+ Some(v) => out_bytes(e, &v),+ None => std::ptr::null_mut(),+ },+ _ => std::ptr::null_mut(),+ })+} }+jni_fn! { Java_com_vayunmathur_e2ee_PgpNative_nativePgpSignDetached -> jbyteArray; (_c: JClass<'l>, secret: JByteArray<'l>, pw: JByteArray<'l>, data: JByteArray<'l>) |e| {+ Ok(match (inp(e, &secret), inp(e, &pw), inp(e, &data)) {+ (Some(a), Some(b), Some(c)) => match sign_detached_from_bytes(&a, &b, &c) {+ Some(v) => out_bytes(e, &v),+ None => std::ptr::null_mut(),+ },+ _ => std::ptr::null_mut(),+ })+} }+jni_fn! { Java_com_vayunmathur_e2ee_PgpNative_nativePgpDecryptVerify -> jobjectArray; (_c: JClass<'l>, secret: JByteArray<'l>, pw: JByteArray<'l>, ct: JByteArray<'l>, verifier: JByteArray<'l>) |e| {+ Ok(match (inp(e, &secret), inp(e, &pw), inp(e, &ct), inp(e, &verifier)) {+ (Some(a), Some(b), Some(c), Some(d)) => match decrypt_verify_from_bytes(&a, &b, &c, &d)+ {+ Some((p, f)) => out_array(e, &[&p, f.as_bytes()]),+ None => std::ptr::null_mut(),+ },+ _ => std::ptr::null_mut(),+ })+} }