Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

meta-secret-org: android_jni #55

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = [
"core",
"core-swift-lib"
"core-swift-lib",
"core-jni-lib"
]
21 changes: 21 additions & 0 deletions core-jni-lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "meta-secret-core-jni"
version = "0.1.0"
edition = "2021"

[target.'cfg(target_os="android")'.dependencies]
jni = { version = "0.21.1", default-features = false }

[lib]
crate-type = ["staticlib", "cdylib"]
name = "meta_secret_core_jni"

[dependencies]
serde = { version = '1.0.118', features = ['derive'] }
toml = "0.7.2"
meta-secret-core = { path = "../core"}


[build-dependencies]
serde = { version = '1.0.118', features = ['derive'] }
toml = "0.7.2"
20 changes: 20 additions & 0 deletions core-jni-lib/src/android_to_rust.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#[cfg(target_os = "android")]
#[allow(non_snake_case)]
pub mod android {

extern crate jni;
use self::jni::JNIEnv;
use self::jni::objects::{JClass, JString};
use self::jni::sys::jstring;

#[no_mangle]
pub extern "system" fn Java_com_example_metasecret_android_RustWrapper_greet<'local>(
mut env: JNIEnv<'local>,
_class: JClass<'local>,
input: JString<'local>,
) -> jstring {
let input_str: String = env.get_string(&input).expect("Couldn't get java string!").into();
let output = env.new_string(&input_str).expect("Couldn't create Java String!");
output.into_raw()
}
}
3 changes: 3 additions & 0 deletions core-jni-lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extern crate core;

pub mod android_to_rust;
4 changes: 2 additions & 2 deletions core-swift-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib", "lib", "staticlib"]
crate-type = ["dylib"]
name = "meta_secret_core_swift"

[dependencies]
serde = { version = "1.0", features = ["derive"] }
meta-secret-core = { path = "../core"}
serde_json = "1.0.87"
serde = { version = "1.0", features = ["derive"] }
ed25519-dalek = "1.0"
crypto_box = "0.8.1"
base64 = "0.20.0-alpha.1"
Expand Down
26 changes: 16 additions & 10 deletions core-swift-lib/src/swift_to_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub extern "C" fn split_secret(strings_bytes: *const u8, string_len: SizeT) -> *
to_c_str(result_json)
}

// Generate password
#[no_mangle]
pub extern "C" fn generate_meta_password_id(password_id: *const u8, json_len: SizeT) -> *mut c_char {
let result_json = internal::generate_meta_password_id(password_id, json_len)
Expand All @@ -42,6 +43,7 @@ pub extern "C" fn generate_meta_password_id(password_id: *const u8, json_len: Si
to_c_str(result_json)
}

// Encrypt secret
#[no_mangle]
pub extern "C" fn encrypt_secret(json_bytes: *const u8, json_len: SizeT) -> *mut c_char {
let encrypted_shares_json = internal::encrypt_secret(json_bytes, json_len)
Expand All @@ -50,12 +52,14 @@ pub extern "C" fn encrypt_secret(json_bytes: *const u8, json_len: SizeT) -> *mut
to_c_str(encrypted_shares_json)
}

// Decript secret
#[no_mangle]
pub extern "C" fn decrypt_secret(json_bytes: *const u8, json_len: SizeT) -> *mut c_char {
let decrypted_shares_json = internal::decrypt_secret(json_bytes, json_len).unwrap();
to_c_str(decrypted_shares_json)
}

// Restore secret
#[no_mangle]
pub extern "C" fn restore_secret(bytes: *const u8, len: SizeT) -> *mut c_char {
let recovered_secret = internal::recover_secret(bytes, len)
Expand All @@ -65,6 +69,18 @@ pub extern "C" fn restore_secret(bytes: *const u8, len: SizeT) -> *mut c_char {
to_c_str(recovered_secret)
}

// Clear the memory
#[no_mangle]
pub extern "C" fn rust_string_free(s: *mut c_char) {
unsafe {
if s.is_null() {
return;
}
CString::from_raw(s)
};
}


fn to_c_str(str: String) -> *mut c_char {
CString::new(str)
.with_context(|| "String transformation error".to_string())
Expand Down Expand Up @@ -164,16 +180,6 @@ mod internal {
}
}

#[no_mangle]
pub extern "C" fn rust_string_free(s: *mut c_char) {
unsafe {
if s.is_null() {
return;
}
CString::from_raw(s)
};
}

fn data_to_string(bytes: *const u8, len: SizeT) -> CoreResult<String> {
// JSON parsing
let bytes_slice = unsafe { slice::from_raw_parts(bytes, len as usize) };
Expand Down