Skip to content

Commit 3c7cc45

Browse files
SessionHero01SessionHero01
authored andcommitted
Add blinded pair binding
1 parent e192c9a commit 3c7cc45

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

library/src/main/cpp/blinded_key.cpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,15 @@
99
// Created by Thomas Ruffie on 29/7/2024.
1010
//
1111

12+
1213
extern "C"
1314
JNIEXPORT jobject JNICALL
1415
Java_network_loki_messenger_libsession_1util_util_BlindKeyAPI_blindVersionKeyPair(JNIEnv *env,
1516
jobject thiz,
1617
jbyteArray ed25519_secret_key) {
1718
return jni_utils::run_catching_cxx_exception_or_throws<jobject>(env, [=] {
1819
const auto [pk, sk] = session::blind_version_key_pair(util::vector_from_bytes(env, ed25519_secret_key));
19-
20-
jclass kp_class = env->FindClass("network/loki/messenger/libsession_util/util/KeyPair");
21-
jmethodID kp_constructor = env->GetMethodID(kp_class, "<init>", "([B[B)V");
22-
return env->NewObject(kp_class, kp_constructor, util::bytes_from_vector(env, {pk.data(), pk.data() + pk.size()}), util::bytes_from_vector(env, {sk.data(), sk.data() + sk.size()}));
20+
return jni_utils::new_key_pair(env, util::bytes_from_span(env, pk), util::bytes_from_span(env, sk));
2321
});
2422
}
2523
extern "C"
@@ -29,7 +27,11 @@ Java_network_loki_messenger_libsession_1util_util_BlindKeyAPI_blindVersionSign(J
2927
jbyteArray ed25519_secret_key,
3028
jlong timestamp) {
3129
return jni_utils::run_catching_cxx_exception_or_throws<jbyteArray>(env, [=] {
32-
auto bytes = session::blind_version_sign(util::vector_from_bytes(env, ed25519_secret_key), session::Platform::android, timestamp);
30+
auto bytes = session::blind_version_sign(
31+
jni_utils::JavaByteArrayRef(env, ed25519_secret_key).get(),
32+
session::Platform::android,
33+
timestamp
34+
);
3335
return util::bytes_from_vector(env, bytes);
3436
});
3537
}
@@ -46,16 +48,29 @@ Java_network_loki_messenger_libsession_1util_util_BlindKeyAPI_blindVersionSignRe
4648
return jni_utils::run_catching_cxx_exception_or_throws<jbyteArray>(env, [=] {
4749
auto methodC = util::string_from_jstring(env, method);
4850
auto pathC = util::string_from_jstring(env, path);
49-
auto keyBytes = util::vector_from_bytes(env, ed25519_secret_key);
50-
auto bodyBytes = body ? std::optional(util::vector_from_bytes(env, body)) : std::nullopt;
5151

5252
auto bytes = session::blind_version_sign_request(
53-
session::to_span(keyBytes),
53+
jni_utils::JavaByteArrayRef(env, ed25519_secret_key).get(),
5454
timestamp,
5555
methodC,
5656
pathC,
57-
body ? std::optional(session::to_span(*bodyBytes)) : std::nullopt
57+
body ? std::make_optional(jni_utils::JavaByteArrayRef(env, body).get()) : std::nullopt
5858
);
5959
return util::bytes_from_vector(env, bytes);
6060
});
61+
}
62+
63+
extern "C"
64+
JNIEXPORT jobject JNICALL
65+
Java_network_loki_messenger_libsession_1util_util_BlindKeyAPI_blind15KeyPair(JNIEnv *env,
66+
jobject thiz,
67+
jbyteArray ed25519_secret_key,
68+
jbyteArray server_pub_key) {
69+
return jni_utils::run_catching_cxx_exception_or_throws<jobject>(env, [=] {
70+
auto [pk, sk] = session::blind15_key_pair(
71+
jni_utils::JavaByteArrayRef(env, ed25519_secret_key).get(),
72+
jni_utils::JavaByteArrayRef(env, server_pub_key).get()
73+
);
74+
return jni_utils::new_key_pair(env, util::bytes_from_span(env, pk), util::bytes_from_span(env, sk));
75+
});
6176
}

library/src/main/cpp/jni_utils.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@ namespace jni_utils {
66
jmethodID constructor = env->GetMethodID(pair_class.get(), "<init>", "(Ljava/lang/Object;Ljava/lang/Object;)V");
77
return env->NewObject(pair_class.get(), constructor, first, second);
88
}
9+
10+
jobject new_key_pair(JNIEnv *env, jbyteArray pubKey, jbyteArray secKey) {
11+
auto kp_class = JavaLocalRef(env, env->FindClass("network/loki/messenger/libsession_util/util/KeyPair"));
12+
jmethodID kp_constructor = env->GetMethodID(kp_class.get(), "<init>", "([B[B)V");
13+
return env->NewObject(kp_class.get(), kp_constructor, pubKey, secKey);
14+
}
915
}

library/src/main/cpp/jni_utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ namespace jni_utils {
146146
*/
147147
jobject new_kotlin_pair(JNIEnv *env, jobject first, jobject second);
148148

149+
/**
150+
* Create a new KeyPair object
151+
*/
152+
jobject new_key_pair(JNIEnv *env, jbyteArray pubKey, jbyteArray secKey);
153+
149154
/**
150155
* A RAII wrapper for a Java byte array. This will automatically release the byte array when it goes out of scope.
151156
*/

library/src/main/java/network/loki/messenger/libsession_util/util/BlindKeyAPI.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,23 @@ object BlindKeyAPI {
1818
method: String,
1919
path: String,
2020
body: ByteArray?): ByteArray
21+
22+
/**
23+
* Generate a 15-blinded key pair
24+
*
25+
* @param ed25519SecretKey The Ed25519 secret key, 32/64 bytes
26+
* @param serverPubKey The server public key, 32 bytes
27+
*/
28+
external fun blind15KeyPair(
29+
ed25519SecretKey: ByteArray,
30+
serverPubKey: ByteArray,
31+
): KeyPair
32+
33+
/**
34+
* Generate a 15-blinded key pair, returning null on failure
35+
*/
36+
fun blind15KeyPairOrNull(
37+
ed25519SecretKey: ByteArray,
38+
serverPubKey: ByteArray,
39+
): KeyPair? = kotlin.runCatching { blind15KeyPair(ed25519SecretKey, serverPubKey) }.getOrNull()
2140
}

0 commit comments

Comments
 (0)