renamed methods for having all native methods in once class (required

by Java that one library loads into one class only).
Also added what I believe to be sane and efficient array access for
CryptoBox (use same approach for other calls as we write the JNI
interfaces).
This commit is contained in:
gardners 2011-10-23 15:53:44 +10:30
parent 62e3931498
commit 77757f550d

43
jni.c
View File

@ -9,7 +9,7 @@ JNIEXPORT jint JNICALL Java_to_yp_cr_NaCl_moose
}
// Lto/yp/cr/NaCl$CryptoBoxKeypair;.method ([B[B)I
JNIEXPORT jint JNICALL Java_to_yp_cr_NaCl_00024CryptoBoxKeypair_method
JNIEXPORT jint JNICALL Java_to_yp_cr_NaCl_nativeCryptoBoxKeypair
(JNIEnv *env, jobject obj, jbyteArray jsk, jbyteArray jpk)
{
unsigned char pk[crypto_box_curve25519xsalsa20poly1305_ref_PUBLICKEYBYTES];
@ -24,3 +24,44 @@ JNIEXPORT jint JNICALL Java_to_yp_cr_NaCl_00024CryptoBoxKeypair_method
return 0;
}
JNIEXPORT jint JNICALL Java_to_yp_cr_NaCl_nativeCryptoBox
(JNIEnv *env, jobject obj, jbyteArray jpk, jbyteArray jsk,jbyteArray jn,jbyteArray jm,jint jmlen,jbyteArray jc)
{
/* XXX Assumes that the first 32 bytes of jm are all zeroes so that the authenticator can be written in */
int i;
if ((*env)->GetArrayLength(env, jpk)!=crypto_box_curve25519xsalsa20poly1305_ref_PUBLICKEYBYTES) return -2;
if ((*env)->GetArrayLength(env, jsk)!=crypto_box_curve25519xsalsa20poly1305_ref_SECRETKEYBYTES) return -3;
if ((*env)->GetArrayLength(env, jn)!=crypto_box_curve25519xsalsa20poly1305_ref_NONCEBYTES) return -4;
if ((*env)->GetArrayLength(env, jm)!=jmlen) return -5;
if ((*env)->GetArrayLength(env, jc)!=jmlen) return -6;
/* Get inputs */
jbyte *pk = (*env)->GetPrimitiveArrayCritical(env, jpk, NULL);
jbyte *sk = (*env)->GetPrimitiveArrayCritical(env, jsk, NULL);
jbyte *n = (*env)->GetPrimitiveArrayCritical(env, jn, NULL);
jbyte *m = (*env)->GetPrimitiveArrayCritical(env, jm, NULL);
jbyte *c = (*env)->GetPrimitiveArrayCritical(env, jc, NULL);
int r=-1;
if (pk&&sk&&n&&m&&c&&(jmlen>=0&&jmlen<=1048576))
{
/* Make sure that space for authenticator is free */
for(i=0;i<crypto_box_curve25519xsalsa20poly1305_ref_ZEROBYTES;i++)
{ if (m[i]) return -7; }
r=crypto_box_curve25519xsalsa20poly1305_ref(c,m,jmlen,n,pk,sk);
}
/* do these really keep any changes made? */
if (pk) (*env)->ReleasePrimitiveArrayCritical(env, jpk, pk, 0);
if (sk) (*env)->ReleasePrimitiveArrayCritical(env, jsk, sk, 0);
if (n) (*env)->ReleasePrimitiveArrayCritical(env, jn, n, 0);
if (m) (*env)->ReleasePrimitiveArrayCritical(env, jm, m, 0);
if (c) (*env)->ReleasePrimitiveArrayCritical(env, jc, c, 0);
return r;
}