Skip to content

Commit

Permalink
add init keyed and init derive_key
Browse files Browse the repository at this point in the history
  • Loading branch information
sken committed Mar 26, 2020
1 parent b8f357d commit 1f21ceb
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
*.o
*.class
target/
*.log
Binary file modified natives/linux_64/libblake3.so
Binary file not shown.
Binary file modified natives/windows_64/blake3.dll
Binary file not shown.
43 changes: 35 additions & 8 deletions src/main/c/org_scash_JNI.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,37 @@ JNIEXPORT void JNICALL Java_org_scash_JNI_blake3_1hasher_1init
return;
}

JNIEXPORT void JNICALL Java_org_scash_JNI_blake3_1hasher_1init_1keyed
(JNIEnv *env, jclass classObject, jlong hp, jobject byteBuffer)
{
blake3_hasher *hasher = (blake3_hasher*)(uintptr_t)hp;
uint8_t *key = (uint8_t*) (*env)->GetDirectBufferAddress(env, byteBuffer);

uint8_t keyarr[BLAKE3_KEY_LEN];

for (size_t i = 0; i < BLAKE3_KEY_LEN; i++) {
keyarr[i] = key[i];
}

blake3_hasher_init_keyed(hasher, keyarr);

(void)classObject;
return;
}

JNIEXPORT void JNICALL Java_org_scash_JNI_blake3_1hasher_1init_1derive_1key
(JNIEnv *env, jclass classObject, jlong hp, jstring ctx)
{
blake3_hasher *hasher = (blake3_hasher*)(uintptr_t)hp;

const char *context = (*env)->GetStringUTFChars(env, ctx, 0);

blake3_hasher_init_derive_key(hasher, context);

(*env)->ReleaseStringUTFChars(env, ctx, context);
(void)classObject;
}

JNIEXPORT void JNICALL Java_org_scash_JNI_blake3_1hasher_1update
(JNIEnv *env, jclass classObject, jlong hp, jobject byteBuffer, jint input_len)
{
Expand All @@ -49,20 +80,16 @@ JNIEXPORT void JNICALL Java_org_scash_JNI_blake3_1hasher_1update
return;
}

JNIEXPORT jbyteArray JNICALL Java_org_scash_JNI_blake3_1hasher_1finalize
(JNIEnv *env, jclass classObject, jlong hp, jint output_len)
JNIEXPORT void JNICALL Java_org_scash_JNI_blake3_1hasher_1finalize
(JNIEnv *env, jclass classObject, jlong hp, jobject byteBuffer, jint output_len)
{
blake3_hasher *hasher = (blake3_hasher*)(uintptr_t)hp;

jbyteArray retArray;
uint8_t output[output_len];
uint8_t *output = (uint8_t*) (*env)->GetDirectBufferAddress(env, byteBuffer);

blake3_hasher_finalize(hasher, output, output_len);

retArray = (*env)->NewByteArray(env, output_len);
(*env)->SetByteArrayRegion(env, retArray, 0, output_len, (jbyte*)output);

(void)classObject;

return retArray;
return;
}
22 changes: 19 additions & 3 deletions src/main/c/org_scash_JNI.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions src/main/java/org/scash/JNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ class JNI {

static native void blake3_hasher_init(long hasher);

static native void blake3_hasher_update(long hasher, ByteBuffer byteBuff, long input_len);
static native void blake3_hasher_init_keyed(long hasher, ByteBuffer byteBuff);

static native byte[] blake3_hasher_finalize(long hasher, long out_len);
static native void blake3_hasher_init_derive_key(long hasher, String context);

static native void blake3_hasher_update(long hasher, ByteBuffer byteBuff, int input_len);

static native void blake3_hasher_finalize(long hasher, ByteBuffer byteBuff, int out_len);
}
63 changes: 57 additions & 6 deletions src/main/java/org/scash/NativeBLAKE3.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ public static boolean isEnabled() {

public NativeBLAKE3() throws IllegalStateException {
checkState(enabled);
long initHasher = JNI.create_hasher();
long initHasher;

w.lock();
try {
initHasher = JNI.create_hasher();
} finally {
w.unlock();
}

checkState(initHasher != 0);
hasher = initHasher;
}
Expand All @@ -68,7 +76,35 @@ public void initDefault() {
}
}

public void update(byte[] data) {
public void initKeyed(byte[] key) {
checkArgument(key.length == KEY_LEN);
ByteBuffer byteBuff = nativeByteBuffer.get();
if (byteBuff == null || byteBuff.capacity() < key.length) {
byteBuff = ByteBuffer.allocateDirect(key.length);
byteBuff.order(ByteOrder.nativeOrder());
nativeByteBuffer.set(byteBuff);
}
byteBuff.rewind();
byteBuff.put(key);

w.lock();
try {
JNI.blake3_hasher_init_keyed(getHasher(), byteBuff);
} finally {
w.unlock();
}
}

public void initDeriveKey(String context) {
w.lock();
try {
JNI.blake3_hasher_init_derive_key(getHasher(), context);
} finally {
w.unlock();
}
}

public void update(byte[] data) throws InvalidNativeOutput {
ByteBuffer byteBuff = nativeByteBuffer.get();

if (byteBuff == null || byteBuff.capacity() < data.length) {
Expand All @@ -86,19 +122,34 @@ public void update(byte[] data) {
}
}

public byte[] getOutput() throws Exception {
public byte[] getOutput() throws InvalidNativeOutput {
return getOutput(OUT_LEN);
}

public byte[] getOutput(int outputLength) throws Exception {
byte[] retByteArray;
public byte[] getOutput(int outputLength) throws InvalidNativeOutput {
ByteBuffer byteBuff = nativeByteBuffer.get();

if (byteBuff == null || byteBuff.capacity() < outputLength) {
byteBuff = ByteBuffer.allocateDirect(outputLength);
byteBuff.order(ByteOrder.nativeOrder());
nativeByteBuffer.set(byteBuff);
}
byteBuff.rewind();

r.lock();
try {
retByteArray = JNI.blake3_hasher_finalize(getHasher(), outputLength);
JNI.blake3_hasher_finalize(getHasher(), byteBuff, outputLength);
} finally {
r.unlock();
}

byte[] retByteArray = new byte[outputLength];
byteBuff.get(retByteArray);

nativeByteBuffer.set(byteBuff.clear());

checkOutput(retByteArray.length == outputLength, "Output size produced by lib doesnt match:" + retByteArray.length + " expected:" + outputLength);

return retByteArray;
}

Expand Down
Loading

0 comments on commit 1f21ceb

Please sign in to comment.