Skip to content

Commit

Permalink
[enh] replaced SodiumJNI with artisan bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
dnet committed Feb 7, 2021
1 parent cc7923b commit 6d65f6a
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
112 changes: 112 additions & 0 deletions src/jni.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <jni.h>
#include "sphinx.h"
#include <sodium.h>

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);
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 6d65f6a

Please sign in to comment.