Revert as its unstable anyway.
This commit is contained in:
parent
3f7850dc5a
commit
0b7789035f
@ -3,59 +3,54 @@
|
||||
#include <jni.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#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)
|
||||
{
|
||||
AES_KEY *aes_key = malloc(sizeof(AES_KEY));
|
||||
|
||||
jboolean isFieldCopy;
|
||||
BYTE *key_bytes = (BYTE*)(*env)->GetByteArrayElements(env, key, &isFieldCopy);
|
||||
jboolean isKeyCopy;
|
||||
BYTE *key_bytes = (BYTE*)(*env)->GetByteArrayElements(env, key, &isKeyCopy);
|
||||
int key_length = (*env)->GetArrayLength(env, key) * 8; // in bits
|
||||
|
||||
AES_set_encrypt_key(key_bytes, key_length, aes_key);
|
||||
|
||||
if (isFieldCopy) {
|
||||
if (isKeyCopy) {
|
||||
(*env)->ReleaseByteArrayElements(env, key, (jbyte*)key_bytes, JNI_ABORT);
|
||||
}
|
||||
|
||||
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
|
||||
(JNIEnv* env, jobject obj, jlong key, jlong iv)
|
||||
(JNIEnv* env, jobject obj, jlong key)
|
||||
{
|
||||
free((AES_KEY*)key);
|
||||
free((BYTE*)iv);
|
||||
}
|
||||
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(
|
||||
(BYTE*) in, // input buffer
|
||||
(BYTE*) out, // output buffer
|
||||
(size_t) length, // readable bytes
|
||||
(AES_KEY*) key, // encryption key
|
||||
(BYTE*) iv, // IV
|
||||
input, // input buffer
|
||||
output, // output buffer
|
||||
buffer_length, // readable bytes
|
||||
aes_key, // encryption key
|
||||
iv_bytes, // IV
|
||||
NULL, // not needed
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -9,17 +9,10 @@ extern "C" {
|
||||
#endif
|
||||
/*
|
||||
* Class: net_md_5_bungee_NativeCipherImpl
|
||||
* Method: init_key
|
||||
* Method: init
|
||||
* Signature: ([B)J
|
||||
*/
|
||||
JNIEXPORT jlong JNICALL Java_net_md_15_bungee_NativeCipherImpl_initKey
|
||||
(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
|
||||
JNIEXPORT jlong JNICALL Java_net_md_15_bungee_NativeCipherImpl_init
|
||||
(JNIEnv *, jobject, jbyteArray);
|
||||
|
||||
/*
|
||||
@ -28,7 +21,7 @@ JNIEXPORT jlong JNICALL Java_net_md_15_bungee_NativeCipherImpl_initIV
|
||||
* Signature: (J)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_net_md_15_bungee_NativeCipherImpl_free
|
||||
(JNIEnv *, jobject, jlong, jlong);
|
||||
(JNIEnv *, jobject, jlong);
|
||||
|
||||
/*
|
||||
* Class: net_md_5_bungee_NativeCipherImpl
|
||||
@ -36,10 +29,9 @@ JNIEXPORT void JNICALL Java_net_md_15_bungee_NativeCipherImpl_free
|
||||
* Signature: (ZJ[BJJI)V
|
||||
*/
|
||||
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
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
@ -16,12 +16,13 @@ public class NativeCipher implements BungeeCipher
|
||||
{
|
||||
|
||||
@Getter
|
||||
private final static NativeCipherImpl nativeCipher = new NativeCipherImpl();
|
||||
private static boolean loaded;
|
||||
/*============================================================================*/
|
||||
private final NativeCipherImpl nativeCipher = new NativeCipherImpl();
|
||||
private boolean forEncryption;
|
||||
private long keyPointer;
|
||||
private long ivPointer;
|
||||
private byte[] iv;
|
||||
/*============================================================================*/
|
||||
private static boolean loaded;
|
||||
|
||||
private long pointer;
|
||||
|
||||
public static boolean isSupported()
|
||||
{
|
||||
@ -44,7 +45,6 @@ public class NativeCipher implements BungeeCipher
|
||||
loaded = true;
|
||||
} catch ( Throwable t )
|
||||
{
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@ -59,18 +59,23 @@ public class NativeCipher implements BungeeCipher
|
||||
@Override
|
||||
public void init(boolean forEncryption, SecretKey key) throws GeneralSecurityException
|
||||
{
|
||||
nativeCipher.free( this.keyPointer, this.ivPointer );
|
||||
if ( pointer != 0 )
|
||||
{
|
||||
nativeCipher.free( pointer );
|
||||
}
|
||||
this.forEncryption = forEncryption;
|
||||
|
||||
byte[] encoded = key.getEncoded();
|
||||
this.keyPointer = nativeCipher.initKey( encoded );
|
||||
this.ivPointer = nativeCipher.initIV( encoded );
|
||||
this.iv = key.getEncoded(); // initialize the IV
|
||||
this.pointer = nativeCipher.init( key.getEncoded() );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void free()
|
||||
{
|
||||
nativeCipher.free( keyPointer, ivPointer );
|
||||
if ( pointer != 0 )
|
||||
{
|
||||
nativeCipher.free( pointer );
|
||||
pointer = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -79,8 +84,8 @@ public class NativeCipher implements BungeeCipher
|
||||
// Smoke tests
|
||||
in.memoryAddress();
|
||||
out.memoryAddress();
|
||||
Preconditions.checkState( keyPointer != 0, "Invalid pointer to AES key!" );
|
||||
Preconditions.checkState( ivPointer != 0, "Invalid pointer to IV!" );
|
||||
Preconditions.checkState( pointer != 0, "Invalid pointer to AES key!" );
|
||||
Preconditions.checkState( iv != null, "Invalid IV!" );
|
||||
|
||||
// Store how many bytes we can cipher
|
||||
int length = in.readableBytes();
|
||||
@ -88,7 +93,7 @@ public class NativeCipher implements BungeeCipher
|
||||
out.ensureWritable( length );
|
||||
|
||||
// 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
|
||||
in.readerIndex( in.writerIndex() );
|
||||
|
@ -9,33 +9,24 @@ class NativeCipherImpl
|
||||
* @param key the key to for encryption
|
||||
* @return the pointer to key
|
||||
*/
|
||||
native long initKey(byte[] key);
|
||||
|
||||
/**
|
||||
* Initializes the iv.
|
||||
*
|
||||
* @param iv the iv
|
||||
* @return the pointer to iv
|
||||
*/
|
||||
native long initIV(byte[] iv);
|
||||
native long init(byte[] key);
|
||||
|
||||
/**
|
||||
* Frees the 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.
|
||||
*
|
||||
* @param forEncryption encryption / decryption mode
|
||||
* @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 out the starting memory address for writing data
|
||||
* @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);
|
||||
}
|
||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user