Revert as its unstable anyway.

This commit is contained in:
md_5 2014-02-12 18:02:02 +11:00
parent 3f7850dc5a
commit 0b7789035f
5 changed files with 54 additions and 71 deletions

View File

@ -3,59 +3,54 @@
#include <jni.h> #include <jni.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#define BYTE unsigned char #define BYTE unsigned char
jlong Java_net_md_15_bungee_NativeCipherImpl_initKey jlong Java_net_md_15_bungee_NativeCipherImpl_init
(JNIEnv* env, jobject obj, jbyteArray key) (JNIEnv* env, jobject obj, jbyteArray key)
{ {
AES_KEY *aes_key = malloc(sizeof(AES_KEY)); AES_KEY *aes_key = malloc(sizeof(AES_KEY));
jboolean isFieldCopy; jboolean isKeyCopy;
BYTE *key_bytes = (BYTE*)(*env)->GetByteArrayElements(env, key, &isFieldCopy); BYTE *key_bytes = (BYTE*)(*env)->GetByteArrayElements(env, key, &isKeyCopy);
int key_length = (*env)->GetArrayLength(env, key) * 8; // in bits int key_length = (*env)->GetArrayLength(env, key) * 8; // in bits
AES_set_encrypt_key(key_bytes, key_length, aes_key); AES_set_encrypt_key(key_bytes, key_length, aes_key);
if (isFieldCopy) { if (isKeyCopy) {
(*env)->ReleaseByteArrayElements(env, key, (jbyte*)key_bytes, JNI_ABORT); (*env)->ReleaseByteArrayElements(env, key, (jbyte*)key_bytes, JNI_ABORT);
} }
return (long) aes_key; return (long) aes_key;
} }
jlong Java_net_md_15_bungee_NativeCipherImpl_initIV
(JNIEnv* env, jobject obj, jbyteArray iv)
{
jboolean isFieldCopy;
BYTE *iv_bytes = (BYTE*)(*env)->GetByteArrayElements(env, iv, &isFieldCopy);
int iv_length = (*env)->GetArrayLength(env, iv);
BYTE* jni_iv = malloc(iv_length);
memcpy(jni_iv, iv_bytes, iv_length);
if (isFieldCopy) {
(*env)->ReleaseByteArrayElements(env, iv, (jbyte*)iv_bytes, JNI_ABORT);
}
return (long) jni_iv;
}
void Java_net_md_15_bungee_NativeCipherImpl_free void Java_net_md_15_bungee_NativeCipherImpl_free
(JNIEnv* env, jobject obj, jlong key, jlong iv) (JNIEnv* env, jobject obj, jlong key)
{ {
free((AES_KEY*)key); free((AES_KEY*)key);
free((BYTE*)iv);
} }
void Java_net_md_15_bungee_NativeCipherImpl_cipher void Java_net_md_15_bungee_NativeCipherImpl_cipher
(JNIEnv* env, jobject obj, jboolean forEncryption, jlong key, jlong iv, jlong in, jlong out, jint length) (JNIEnv* env, jobject obj, jboolean forEncryption, jlong key, jbyteArray iv, jlong in, jlong out, jint length)
{ {
AES_KEY *aes_key = (AES_KEY*)key;
size_t buffer_length = (size_t) length;
BYTE *input = (BYTE*) in;
BYTE *output = (BYTE*) out;
jboolean isCopy;
BYTE *iv_bytes = (BYTE*)(*env)->GetByteArrayElements(env, iv, &isCopy);
AES_cfb8_encrypt( AES_cfb8_encrypt(
(BYTE*) in, // input buffer input, // input buffer
(BYTE*) out, // output buffer output, // output buffer
(size_t) length, // readable bytes buffer_length, // readable bytes
(AES_KEY*) key, // encryption key aes_key, // encryption key
(BYTE*) iv, // IV iv_bytes, // IV
NULL, // not needed NULL, // not needed
forEncryption ? AES_ENCRYPT : AES_DECRYPT // encryption mode forEncryption ? AES_ENCRYPT : AES_DECRYPT // encryption mode
); );
// IV has changed, let's copy it back
if (isCopy) {
(*env)->ReleaseByteArrayElements(env, iv, (jbyte*)iv_bytes, 0);
}
} }

View File

@ -9,17 +9,10 @@ extern "C" {
#endif #endif
/* /*
* Class: net_md_5_bungee_NativeCipherImpl * Class: net_md_5_bungee_NativeCipherImpl
* Method: init_key * Method: init
* Signature: ([B)J * Signature: ([B)J
*/ */
JNIEXPORT jlong JNICALL Java_net_md_15_bungee_NativeCipherImpl_initKey JNIEXPORT jlong JNICALL Java_net_md_15_bungee_NativeCipherImpl_init
(JNIEnv *, jobject, jbyteArray);
/*
* Class: net_md_5_bungee_NativeCipherImpl
* Method: init_iv
* Signature: ([B)J
*/
JNIEXPORT jlong JNICALL Java_net_md_15_bungee_NativeCipherImpl_initIV
(JNIEnv *, jobject, jbyteArray); (JNIEnv *, jobject, jbyteArray);
/* /*
@ -28,7 +21,7 @@ JNIEXPORT jlong JNICALL Java_net_md_15_bungee_NativeCipherImpl_initIV
* Signature: (J)V * Signature: (J)V
*/ */
JNIEXPORT void JNICALL Java_net_md_15_bungee_NativeCipherImpl_free JNIEXPORT void JNICALL Java_net_md_15_bungee_NativeCipherImpl_free
(JNIEnv *, jobject, jlong, jlong); (JNIEnv *, jobject, jlong);
/* /*
* Class: net_md_5_bungee_NativeCipherImpl * Class: net_md_5_bungee_NativeCipherImpl
@ -36,10 +29,9 @@ JNIEXPORT void JNICALL Java_net_md_15_bungee_NativeCipherImpl_free
* Signature: (ZJ[BJJI)V * Signature: (ZJ[BJJI)V
*/ */
JNIEXPORT void JNICALL Java_net_md_15_bungee_NativeCipherImpl_cipher JNIEXPORT void JNICALL Java_net_md_15_bungee_NativeCipherImpl_cipher
(JNIEnv *, jobject, jboolean, jlong, jlong, jlong, jlong, jint); (JNIEnv *, jobject, jboolean, jlong, jbyteArray, jlong, jlong, jint);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif

View File

@ -16,12 +16,13 @@ public class NativeCipher implements BungeeCipher
{ {
@Getter @Getter
private final static NativeCipherImpl nativeCipher = new NativeCipherImpl(); private final NativeCipherImpl nativeCipher = new NativeCipherImpl();
private static boolean loaded;
/*============================================================================*/
private boolean forEncryption; private boolean forEncryption;
private long keyPointer; private byte[] iv;
private long ivPointer; /*============================================================================*/
private static boolean loaded;
private long pointer;
public static boolean isSupported() public static boolean isSupported()
{ {
@ -44,7 +45,6 @@ public class NativeCipher implements BungeeCipher
loaded = true; loaded = true;
} catch ( Throwable t ) } catch ( Throwable t )
{ {
t.printStackTrace();
} }
} }
@ -59,18 +59,23 @@ public class NativeCipher implements BungeeCipher
@Override @Override
public void init(boolean forEncryption, SecretKey key) throws GeneralSecurityException public void init(boolean forEncryption, SecretKey key) throws GeneralSecurityException
{ {
nativeCipher.free( this.keyPointer, this.ivPointer ); if ( pointer != 0 )
{
nativeCipher.free( pointer );
}
this.forEncryption = forEncryption; this.forEncryption = forEncryption;
this.iv = key.getEncoded(); // initialize the IV
byte[] encoded = key.getEncoded(); this.pointer = nativeCipher.init( key.getEncoded() );
this.keyPointer = nativeCipher.initKey( encoded );
this.ivPointer = nativeCipher.initIV( encoded );
} }
@Override @Override
public void free() public void free()
{ {
nativeCipher.free( keyPointer, ivPointer ); if ( pointer != 0 )
{
nativeCipher.free( pointer );
pointer = 0;
}
} }
@Override @Override
@ -79,8 +84,8 @@ public class NativeCipher implements BungeeCipher
// Smoke tests // Smoke tests
in.memoryAddress(); in.memoryAddress();
out.memoryAddress(); out.memoryAddress();
Preconditions.checkState( keyPointer != 0, "Invalid pointer to AES key!" ); Preconditions.checkState( pointer != 0, "Invalid pointer to AES key!" );
Preconditions.checkState( ivPointer != 0, "Invalid pointer to IV!" ); Preconditions.checkState( iv != null, "Invalid IV!" );
// Store how many bytes we can cipher // Store how many bytes we can cipher
int length = in.readableBytes(); int length = in.readableBytes();
@ -88,7 +93,7 @@ public class NativeCipher implements BungeeCipher
out.ensureWritable( length ); out.ensureWritable( length );
// Cipher the bytes // Cipher the bytes
nativeCipher.cipher( forEncryption, keyPointer, ivPointer, in.memoryAddress() + in.readerIndex(), out.memoryAddress() + out.writerIndex(), length ); nativeCipher.cipher( forEncryption, pointer, iv, in.memoryAddress() + in.readerIndex(), out.memoryAddress() + out.writerIndex(), length );
// Go to the end of the buffer, all bytes would of been read // Go to the end of the buffer, all bytes would of been read
in.readerIndex( in.writerIndex() ); in.readerIndex( in.writerIndex() );

View File

@ -9,33 +9,24 @@ class NativeCipherImpl
* @param key the key to for encryption * @param key the key to for encryption
* @return the pointer to key * @return the pointer to key
*/ */
native long initKey(byte[] key); native long init(byte[] key);
/**
* Initializes the iv.
*
* @param iv the iv
* @return the pointer to iv
*/
native long initIV(byte[] iv);
/** /**
* Frees the key. * Frees the key.
* *
* @param key the pointer to key * @param key the pointer to key
* @param iv the pointer to iv
*/ */
native void free(long key, long iv); native void free(long key);
/** /**
* This method will encrypt some data in AES-CFB8 using the specified key. * This method will encrypt some data in AES-CFB8 using the specified key.
* *
* @param forEncryption encryption / decryption mode * @param forEncryption encryption / decryption mode
* @param key the pointer to key * @param key the pointer to key
* @param iv the pointer to iv * @param iv the iv to use
* @param in the starting memory address for reading data * @param in the starting memory address for reading data
* @param out the starting memory address for writing data * @param out the starting memory address for writing data
* @param length the length of data to read / write * @param length the length of data to read / write
*/ */
native void cipher(boolean forEncryption, long key, long iv, long in, long out, int length); native void cipher(boolean forEncryption, long key, byte[] iv, long in, long out, int length);
} }