From 6d65f6a4db5c22291e3d37c6a9d4c3b2ba491342 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20Veres-Szentkir=C3=A1lyi?= Date: Sun, 7 Feb 2021 21:56:00 +0100 Subject: [PATCH] [enh] replaced SodiumJNI with artisan bindings --- src/jni.c | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/makefile | 2 +- 2 files changed, 113 insertions(+), 1 deletion(-) diff --git a/src/jni.c b/src/jni.c index 2d7e64f..81e36d3 100644 --- a/src/jni.c +++ b/src/jni.c @@ -1,5 +1,6 @@ #include #include "sphinx.h" +#include JNIEXPORT void JNICALL Java_org_hsbp_androsphinx_Sphinx_challenge(JNIEnv *env, jobject ignore, jbyteArray pwd, jbyteArray salt, jbyteArray bfac, jbyteArray chal) { jbyte* bufferPtrPwd = (*env)->GetByteArrayElements(env, pwd, NULL); @@ -53,3 +54,114 @@ JNIEXPORT jbyteArray JNICALL Java_org_hsbp_androsphinx_Sphinx_finish(JNIEnv *env return result ? NULL : rwd; } + +JNIEXPORT jbyteArray JNICALL Java_org_hsbp_androsphinx_Sodium_genericHash(JNIEnv *env, jobject ignore, jbyteArray msg, jbyteArray salt) { + jbyte* bufferPtrMsg = (*env)->GetByteArrayElements(env, msg, NULL); + jbyte* bufferPtrSalt = (*env)->GetByteArrayElements(env, salt, NULL); + jsize msgLen = (*env)->GetArrayLength(env, msg); + jsize saltLen = (*env)->GetArrayLength(env, salt); + + jbyteArray hash = (*env)->NewByteArray(env, crypto_generichash_BYTES); + jbyte* bufferPtrHash = (*env)->GetByteArrayElements(env, hash, NULL); + + crypto_generichash(bufferPtrHash, crypto_generichash_BYTES, + bufferPtrMsg, msgLen, bufferPtrSalt, saltLen); + + (*env)->ReleaseByteArrayElements(env, msg, bufferPtrMsg, JNI_ABORT); + (*env)->ReleaseByteArrayElements(env, salt, bufferPtrSalt, JNI_ABORT); + (*env)->ReleaseByteArrayElements(env, hash, bufferPtrHash, 0); + + return hash; +} + +JNIEXPORT jbyteArray JNICALL Java_org_hsbp_androsphinx_Sodium_randomBytes(JNIEnv *env, jobject ignore, jint length) { + jbyteArray result = (*env)->NewByteArray(env, length); + jbyte* bufferPtrResult = (*env)->GetByteArrayElements(env, result, NULL); + + randombytes_buf(bufferPtrResult, length); + + (*env)->ReleaseByteArrayElements(env, result, bufferPtrResult, 0); + + return result; +} + +JNIEXPORT jbyteArray JNICALL Java_org_hsbp_androsphinx_Sodium_cryptoSignSeedKeypair(JNIEnv *env, jobject ignore, jbyteArray seed) { + unsigned char ignored_pk[crypto_sign_PUBLICKEYBYTES]; + + jbyteArray result = (*env)->NewByteArray(env, crypto_sign_SECRETKEYBYTES); + jbyte* bufferPtrResult = (*env)->GetByteArrayElements(env, result, NULL); + jbyte* bufferPtrSeed = (*env)->GetByteArrayElements(env, seed, NULL); + + crypto_sign_seed_keypair(ignored_pk, bufferPtrResult, bufferPtrSeed); + + (*env)->ReleaseByteArrayElements(env, result, bufferPtrResult, 0); + (*env)->ReleaseByteArrayElements(env, seed, bufferPtrSeed, JNI_ABORT); + + return result; +} + +JNIEXPORT jbyteArray JNICALL Java_org_hsbp_androsphinx_Sodium_cryptoSignEd25519SkToPk(JNIEnv *env, jobject ignore, jbyteArray sk) { + jbyteArray result = (*env)->NewByteArray(env, crypto_sign_PUBLICKEYBYTES); + jbyte* bufferPtrResult = (*env)->GetByteArrayElements(env, result, NULL); + jbyte* bufferPtrSk = (*env)->GetByteArrayElements(env, sk, NULL); + + crypto_sign_ed25519_sk_to_pk(bufferPtrResult, bufferPtrSk); + + (*env)->ReleaseByteArrayElements(env, result, bufferPtrResult, 0); + (*env)->ReleaseByteArrayElements(env, sk, bufferPtrSk, JNI_ABORT); + + return result; +} + +JNIEXPORT jbyteArray JNICALL Java_org_hsbp_androsphinx_Sodium_cryptoSignDetached(JNIEnv *env, jobject ignore, jbyteArray sk, jbyteArray msg) { + jbyteArray result = (*env)->NewByteArray(env, crypto_sign_BYTES); + jbyte* bufferPtrResult = (*env)->GetByteArrayElements(env, result, NULL); + jbyte* bufferPtrSk = (*env)->GetByteArrayElements(env, sk, NULL); + jbyte* bufferPtrMsg = (*env)->GetByteArrayElements(env, msg, NULL); + jsize msgLen = (*env)->GetArrayLength(env, msg); + + unsigned long long ignored_siglen = crypto_sign_BYTES; + + crypto_sign_detached(bufferPtrResult, &ignored_siglen, bufferPtrMsg, msgLen, bufferPtrSk); + + (*env)->ReleaseByteArrayElements(env, result, bufferPtrResult, 0); + (*env)->ReleaseByteArrayElements(env, sk, bufferPtrSk, JNI_ABORT); + (*env)->ReleaseByteArrayElements(env, msg, bufferPtrMsg, JNI_ABORT); + + return result; +} + +JNIEXPORT jbyteArray JNICALL Java_org_hsbp_androsphinx_Sodium_cryptoSecretboxEasy(JNIEnv *env, jobject ignore, jbyteArray key, jbyteArray msg) { + jbyte* bufferPtrKey = (*env)->GetByteArrayElements(env, key, NULL); + jbyte* bufferPtrMsg = (*env)->GetByteArrayElements(env, msg, NULL); + jsize msgLen = (*env)->GetArrayLength(env, msg); + + jbyteArray result = (*env)->NewByteArray(env, crypto_secretbox_NONCEBYTES + crypto_secretbox_MACBYTES + msgLen); + jbyte* bufferPtrResult = (*env)->GetByteArrayElements(env, result, NULL); + + randombytes_buf(bufferPtrResult, crypto_secretbox_NONCEBYTES); + crypto_secretbox_easy(bufferPtrResult + crypto_secretbox_NONCEBYTES, bufferPtrMsg, msgLen, bufferPtrResult, bufferPtrKey); + + (*env)->ReleaseByteArrayElements(env, result, bufferPtrResult, 0); + (*env)->ReleaseByteArrayElements(env, msg, bufferPtrMsg, JNI_ABORT); + (*env)->ReleaseByteArrayElements(env, key, bufferPtrKey, JNI_ABORT); + + return result; +} + +JNIEXPORT jbyteArray JNICALL Java_org_hsbp_androsphinx_Sodium_cryptoSecretboxOpenEasy(JNIEnv *env, jobject ignore, jbyteArray key, jbyteArray msg) { + jbyte* bufferPtrKey = (*env)->GetByteArrayElements(env, key, NULL); + jbyte* bufferPtrMsg = (*env)->GetByteArrayElements(env, msg, NULL); + jsize msgLen = (*env)->GetArrayLength(env, msg); + + jbyteArray result = (*env)->NewByteArray(env, msgLen - (crypto_secretbox_NONCEBYTES + crypto_secretbox_MACBYTES)); + jbyte* bufferPtrResult = (*env)->GetByteArrayElements(env, result, NULL); + + int sodium_result = crypto_secretbox_open_easy(bufferPtrResult, bufferPtrMsg + crypto_secretbox_NONCEBYTES, msgLen - crypto_secretbox_NONCEBYTES, bufferPtrMsg, bufferPtrKey); + + (*env)->ReleaseByteArrayElements(env, result, bufferPtrResult, sodium_result ? JNI_ABORT : 0); + (*env)->ReleaseByteArrayElements(env, msg, bufferPtrMsg, JNI_ABORT); + (*env)->ReleaseByteArrayElements(env, key, bufferPtrKey, JNI_ABORT); + + return sodium_result ? NULL : result; +} diff --git a/src/makefile b/src/makefile index bd9aa46..473826a 100644 --- a/src/makefile +++ b/src/makefile @@ -20,7 +20,7 @@ win: win/libsodium-win64 exe libsphinx.$(SOEXT) tests$(EXT) exe: bin/challenge$(EXT) bin/respond$(EXT) bin/derive$(EXT) android: INC=-I$(SODIUM) -I$(SODIUM)/sodium -android: LIBS=-Wl,-Bstatic -Wl,-Bdynamic -lsodiumjni -L. +android: LIBS=-Wl,-Bstatic -Wl,-Bdynamic -lsodium -L. android: EXTRA_OBJECTS=jni.o android: jni.o libsphinx.so