Skip to content

Commit fdb4b25

Browse files
SessionHero01SessionHero01
authored andcommitted
Add more curve25519 operations
1 parent 81b2127 commit fdb4b25

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

library/src/main/cpp/curve25519.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,15 @@ Java_network_loki_messenger_libsession_1util_Curve25519_pubKeyFromED25519(JNIEnv
2424
auto pk = session::curve25519::to_curve25519_pubkey(jni_utils::JavaByteArrayRef(env, ed25519_public_key).get());
2525
return util::bytes_from_span(env, pk);
2626
});
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+
});
2738
}

library/src/main/cpp/encryption.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "jni_utils.h"
66

7+
#include <sodium.h>
8+
79
using jni_utils::JavaByteArrayRef;
810

911
extern "C"
@@ -125,4 +127,33 @@ Java_network_loki_messenger_libsession_1util_SessionEncrypt_decryptOnsResponse(J
125127

126128
return util::jstringFromOptional(env, data);
127129
});
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+
128159
}

library/src/main/java/network/loki/messenger/libsession_util/Curve25519.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ object Curve25519 : LibSessionUtilCApi() {
1717
external fun pubKeyFromED25519(
1818
ed25519PublicKey: ByteArray,
1919
): ByteArray
20+
21+
external fun generateKeyPair(): KeyPair
2022
}

library/src/main/java/network/loki/messenger/libsession_util/SessionEncrypt.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,9 @@ object SessionEncrypt : LibSessionUtilCApi() {
7777
ciphertext: ByteArray,
7878
nonce: ByteArray?,
7979
): SessionId
80+
81+
external fun calculateECHDAgreement(
82+
x25519PubKey: ByteArray,
83+
x25519PrivKey: ByteArray,
84+
): ByteArray
8085
}

0 commit comments

Comments
 (0)