Skip to content
Merged
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
11 changes: 11 additions & 0 deletions library/src/main/cpp/curve25519.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,15 @@ Java_network_loki_messenger_libsession_1util_Curve25519_pubKeyFromED25519(JNIEnv
auto pk = session::curve25519::to_curve25519_pubkey(jni_utils::JavaByteArrayRef(env, ed25519_public_key).get());
return util::bytes_from_span(env, pk);
});
}

extern "C"
JNIEXPORT jobject JNICALL
Java_network_loki_messenger_libsession_1util_Curve25519_generateKeyPair(JNIEnv *env, jobject thiz) {
return jni_utils::run_catching_cxx_exception_or_throws<jobject>(env, [=] {
auto [sk, pk] = session::curve25519::curve25519_key_pair();
return jni_utils::new_key_pair(env,
util::bytes_from_span(env, sk),
util::bytes_from_span(env, pk));
});
}
31 changes: 31 additions & 0 deletions library/src/main/cpp/encryption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "jni_utils.h"

#include <sodium.h>

using jni_utils::JavaByteArrayRef;

extern "C"
Expand Down Expand Up @@ -125,4 +127,33 @@ Java_network_loki_messenger_libsession_1util_SessionEncrypt_decryptOnsResponse(J

return util::jstringFromOptional(env, data);
});
}

extern "C"
JNIEXPORT jbyteArray JNICALL
Java_network_loki_messenger_libsession_1util_SessionEncrypt_calculateECHDAgreement(JNIEnv *env,
jobject thiz,
jbyteArray x25519_pub_key,
jbyteArray x25519_priv_key) {
return jni_utils::run_catching_cxx_exception_or_throws<jbyteArray>(env, [=] {
JavaByteArrayRef sk(env, x25519_priv_key);
JavaByteArrayRef pk(env, x25519_pub_key);

if (sk.get().size() != crypto_scalarmult_SCALARBYTES) {
throw std::invalid_argument{"Invalid x25519_priv_key: expected 32 bytes"};
}

if (pk.get().size() != crypto_scalarmult_BYTES) {
throw std::invalid_argument{"Invalid x25519_pub_key: expected 32 bytes"};
}

std::array<unsigned char, crypto_scalarmult_BYTES> shared_secret {0};
if (crypto_scalarmult(shared_secret.data(),sk.get().data(), pk.get().data()) != 0) {
throw std::runtime_error{"An error occurred while attempting to calculate the shared "
"secret; is the key valid?"};
}

return util::bytes_from_span(env, shared_secret);
});

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ object Curve25519 : LibSessionUtilCApi() {
external fun pubKeyFromED25519(
ed25519PublicKey: ByteArray,
): ByteArray

external fun generateKeyPair(): KeyPair
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,9 @@ object SessionEncrypt : LibSessionUtilCApi() {
ciphertext: ByteArray,
nonce: ByteArray?,
): SessionId

external fun calculateECHDAgreement(
x25519PubKey: ByteArray,
x25519PrivKey: ByteArray,
): ByteArray
}