Skip to content

Commit 602cde7

Browse files
SessionHero01SessionHero01
andauthored
Add binding for ed25519 (#8)
Co-authored-by: SessionHero01 <[email protected]>
1 parent 7d21285 commit 602cde7

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

library/src/main/cpp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ set(SOURCES
4141
logging.cpp
4242
encryption.cpp
4343
jni_utils.cpp
44+
ed25519.cpp
4445
)
4546

4647
add_library( # Sets the name of the library.

library/src/main/cpp/ed25519.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "jni_utils.h"
2+
#include "util.h"
3+
4+
#include <session/ed25519.hpp>
5+
6+
extern "C"
7+
JNIEXPORT jbyteArray JNICALL
8+
Java_network_loki_messenger_libsession_1util_ED25519_sign(JNIEnv *env, jobject thiz,
9+
jbyteArray ed25519_private_key,
10+
jbyteArray message) {
11+
return jni_utils::run_catching_cxx_exception_or_throws<jbyteArray>(env, [=] {
12+
auto sigs = session::ed25519::sign(
13+
jni_utils::JavaByteArrayRef(env, ed25519_private_key).get(),
14+
jni_utils::JavaByteArrayRef(env, message).get());
15+
16+
return util::bytes_from_vector(env, sigs);
17+
});
18+
}
19+
20+
extern "C"
21+
JNIEXPORT jboolean JNICALL
22+
Java_network_loki_messenger_libsession_1util_ED25519_verify(JNIEnv *env, jobject thiz,
23+
jbyteArray ed25519_public_key,
24+
jbyteArray message,
25+
jbyteArray signature) {
26+
return jni_utils::run_catching_cxx_exception_or_throws<jboolean>(env, [=] {
27+
return session::ed25519::verify(
28+
jni_utils::JavaByteArrayRef(env, signature).get(),
29+
jni_utils::JavaByteArrayRef(env, ed25519_public_key).get(),
30+
jni_utils::JavaByteArrayRef(env, message).get()
31+
);
32+
});
33+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package network.loki.messenger.libsession_util
2+
3+
object ED25519 {
4+
/**
5+
* Sign a message using the ed25519 private key
6+
*
7+
* @param ed25519PrivateKey 64 bytes ed25519 private key
8+
* @param message Message to sign
9+
* @return 64 bytes signature
10+
*/
11+
external fun sign(
12+
ed25519PrivateKey: ByteArray,
13+
message: ByteArray,
14+
): ByteArray
15+
16+
/**
17+
* Verify a message using the ed25519 public key
18+
*
19+
* @param ed25519PublicKey 32 bytes ed25519 public key
20+
* @param message Message to verify
21+
* @param signature 64 bytes signature
22+
*/
23+
external fun verify(
24+
ed25519PublicKey: ByteArray,
25+
message: ByteArray,
26+
signature: ByteArray,
27+
): Boolean
28+
}

0 commit comments

Comments
 (0)