Jonathan S. Fisher
Jonathan S. Fisher

Reputation: 8876

How do you convert byte[] -> uint8_t[] without creating issues with JNI?

I'm having a bit of trouble. I'm trying to call a C-based crypto library and supply it a byte[] from Java code. It will only work if I change my Java signature to an int[]. What gives?

The actual C-library function I'm trying to call is this:

ecc_verify(const uint8_t p_publicKey[32], const uint8_t p_hash[32], const uint8_t p_signature[32*2]

My JNI class in Java has this:

public native static boolean jni_verify(byte[] publicKey, byte[] data, byte[] signature);

Which results in this header file signature:

JNIEXPORT jboolean JNICALL Java_com_something_eccjni_EccJniExport_jni_1verify (JNIEnv *, jclass, jbyteArray, jbyteArray, jbyteArray);

And my C implementation is:

JNIEXPORT jboolean JNICALL Java_com_something_eccjni_EccJniExport_ecdsa_1verify
(JNIEnv *jniEnv, jclass clazz, jbyteArray publicKeyArray, jbyteArray dataArray, jbyteArray signatureArray){
    jboolean isCopy;

    jbyte* publicKey = (*jniEnv)->GetByteArrayElements(jniEnv, publicKeyArray, &isCopy);
    jbyte* data = (*jniEnv)->GetByteArrayElements(jniEnv, dataArray, &isCopy);
    jbyte* signature = (*jniEnv)->GetByteArrayElements(jniEnv, signatureArray, &isCopy);

    int result = ecdsa_verify((uint8_t *) publicKey,(uint8_t *) data,(uint8_t *) signature);

    (*jniEnv)->ReleaseByteArrayElements(jniEnv, publicKeyArray, publicKey, JNI_ABORT);
    (*jniEnv)->ReleaseByteArrayElements(jniEnv, dataArray, data, JNI_ABORT);
    (*jniEnv)->ReleaseByteArrayElements(jniEnv, signatureArray, signature, JNI_ABORT);

    return result;
}

This never works. This often results in a message:

java(9966,0x7000012c8000) malloc: *** error for object 0x7fb7a0614758: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug

However, if I change everything to this, it works:

public native static boolean ecdsa_verify(int[] publicKey, int[] data, int[] signature);

JNIEXPORT jboolean JNICALL Java_com_something_eccjni_EccJniExport_ecdsa_1verify (JNIEnv *, jclass, jintArray, jintArray, jintArray);

JNIEXPORT jboolean JNICALL Java_com_something_eccjni_EccJniExport_ecdsa_1verify
(JNIEnv *jniEnv, jclass clazz, jintArray publicKeyArray, jintArray dataArray, jintArray signatureArray){
    jboolean isCopy;

    jint* publicKey = (*jniEnv)->GetIntArrayElements(jniEnv, publicKeyArray, &isCopy);
    jint* data = (*jniEnv)->GetIntArrayElements(jniEnv, dataArray, &isCopy);
    jint* signature = (*jniEnv)->GetIntArrayElements(jniEnv, signatureArray, &isCopy);

    int result = ecdsa_verify((uint8_t *) publicKey,(uint8_t *) data,(uint8_t *) signature);

    (*jniEnv)->ReleaseIntArrayElements(jniEnv, publicKeyArray, publicKey, JNI_ABORT);
    (*jniEnv)->ReleaseIntArrayElements(jniEnv, dataArray, data, JNI_ABORT);
    (*jniEnv)->ReleaseIntArrayElements(jniEnv, signatureArray, signature, JNI_ABORT);

    return result;
}

What's going on?

EDIT

Here's the Calling Java:

    System.load(EccJni.class.getResource("/EccJniExport.jnilib").getFile());
    byte[] publicKey = new byte[32];
    byte[] privateKey = new byte[32];
    EccJniExport.ecc_make_key(publicKey, privateKey);

    byte[] data = new byte[32];
    for (int i = 0; i < data.length; i++) {
        data[i] = (byte) i;
    }
    byte[] signature = new byte[32];
    EccJniExport.ecdsa_sign(privateKey, data, signature);

    System.out.println("result:" + EccJniExport.ecdsa_verify(publicKey, data, signature));

Again, if you change the byte[] to int[] everything works peachy

Upvotes: 2

Views: 1674

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596988

You are not allocating enough bytes for the signature array.

The C function expects 32*2 bytes for that parameter, but your Java code is allocating 32 bytes instead.

That explains why the code works when you use an int[] array instead of a byte[] array. You would be allocating 32*4 bytes in that situation.

Upvotes: 4

Related Questions