File tree Expand file tree Collapse file tree 4 files changed +49
-0
lines changed
java/network/loki/messenger/libsession_util Expand file tree Collapse file tree 4 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -24,4 +24,15 @@ Java_network_loki_messenger_libsession_1util_Curve25519_pubKeyFromED25519(JNIEnv
24
24
auto pk = session::curve25519::to_curve25519_pubkey (jni_utils::JavaByteArrayRef (env, ed25519_public_key).get ());
25
25
return util::bytes_from_span (env, pk);
26
26
});
27
+ }
28
+
29
+ extern " C"
30
+ JNIEXPORT jobject JNICALL
31
+ Java_network_loki_messenger_libsession_1util_Curve25519_generateKeyPair (JNIEnv *env, jobject thiz) {
32
+ return jni_utils::run_catching_cxx_exception_or_throws<jobject>(env, [=] {
33
+ auto [sk, pk] = session::curve25519::curve25519_key_pair ();
34
+ return jni_utils::new_key_pair (env,
35
+ util::bytes_from_span (env, sk),
36
+ util::bytes_from_span (env, pk));
37
+ });
27
38
}
Original file line number Diff line number Diff line change 4
4
5
5
#include " jni_utils.h"
6
6
7
+ #include < sodium.h>
8
+
7
9
using jni_utils::JavaByteArrayRef;
8
10
9
11
extern " C"
@@ -125,4 +127,33 @@ Java_network_loki_messenger_libsession_1util_SessionEncrypt_decryptOnsResponse(J
125
127
126
128
return util::jstringFromOptional (env, data);
127
129
});
130
+ }
131
+
132
+ extern " C"
133
+ JNIEXPORT jbyteArray JNICALL
134
+ Java_network_loki_messenger_libsession_1util_SessionEncrypt_calculateECHDAgreement (JNIEnv *env,
135
+ jobject thiz,
136
+ jbyteArray x25519_pub_key,
137
+ jbyteArray x25519_priv_key) {
138
+ return jni_utils::run_catching_cxx_exception_or_throws<jbyteArray>(env, [=] {
139
+ JavaByteArrayRef sk (env, x25519_priv_key);
140
+ JavaByteArrayRef pk (env, x25519_pub_key);
141
+
142
+ if (sk.get ().size () != crypto_scalarmult_SCALARBYTES) {
143
+ throw std::invalid_argument{" Invalid x25519_priv_key: expected 32 bytes" };
144
+ }
145
+
146
+ if (pk.get ().size () != crypto_scalarmult_BYTES) {
147
+ throw std::invalid_argument{" Invalid x25519_pub_key: expected 32 bytes" };
148
+ }
149
+
150
+ std::array<unsigned char , crypto_scalarmult_BYTES> shared_secret {0 };
151
+ if (crypto_scalarmult (shared_secret.data (),sk.get ().data (), pk.get ().data ()) != 0 ) {
152
+ throw std::runtime_error{" An error occurred while attempting to calculate the shared "
153
+ " secret; is the key valid?" };
154
+ }
155
+
156
+ return util::bytes_from_span (env, shared_secret);
157
+ });
158
+
128
159
}
Original file line number Diff line number Diff line change @@ -17,4 +17,6 @@ object Curve25519 : LibSessionUtilCApi() {
17
17
external fun pubKeyFromED25519 (
18
18
ed25519PublicKey : ByteArray ,
19
19
): ByteArray
20
+
21
+ external fun generateKeyPair (): KeyPair
20
22
}
Original file line number Diff line number Diff line change @@ -77,4 +77,9 @@ object SessionEncrypt : LibSessionUtilCApi() {
77
77
ciphertext : ByteArray ,
78
78
nonce : ByteArray? ,
79
79
): SessionId
80
+
81
+ external fun calculateECHDAgreement (
82
+ x25519PubKey : ByteArray ,
83
+ x25519PrivKey : ByteArray ,
84
+ ): ByteArray
80
85
}
You can’t perform that action at this time.
0 commit comments