Make changes to the android code to use the new native libraries.

This commit is contained in:
Brian Pellin committed 2010-11-21 16:47:27 -06:00
1 parent 731d986fa8
commit 87e32b8eba
3 files changed
+41 -32

No files matched your search

@@ -83,7 +83,7 @@ public class NativeAESCipherSpi extends CipherSpi {
Reference<? extends NativeAESCipherSpi> ref = mQueue.remove();
long ctx = mCleanup.remove(ref);
nativeCleanup(ctx);
nCleanup(ctx);
Log.d("KeePassDroid", "Cleaned up cipher context: " + ctx);
} catch (InterruptedException e) {
@@ -94,14 +94,13 @@ public class NativeAESCipherSpi extends CipherSpi {
}
private static native void nativeCleanup(long ctxPtr);
private static native void nCleanup(long ctxPtr);
public NativeAESCipherSpi() {
if ( ! mIsStaticInit ) {
staticInit();
}
}
@Override
protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen)
@@ -139,9 +138,9 @@ public class NativeAESCipherSpi extends CipherSpi {
int outputSize = engineGetOutputSize(inputLen);
int updateAmt = nativeUpdate(mCtxPtr, input, inputOffset, inputLen, output, outputOffset, outputSize);
int updateAmt = nativeUpdateWrap(mCtxPtr, input, inputOffset, inputLen, output, outputOffset, outputSize);
int finalAmt = nativeDoFinal(mCtxPtr, output, outputOffset + updateAmt, outputSize - updateAmt);
int finalAmt = nativeDoFinalWrap(mCtxPtr, output, outputOffset + updateAmt, outputSize - updateAmt);
int out = updateAmt + finalAmt;
@@ -150,7 +149,16 @@ public class NativeAESCipherSpi extends CipherSpi {
return out;
}
private native int nativeDoFinal(long ctxPtr, byte[] output, int outputOffest, int outputSize);
private int nativeDoFinalWrap(long ctxPtr, byte[] output, int outputOffest, int outputSize) {
if (mEncrypting) {
return nEncryptFinal(ctxPtr, output, outputOffest, outputSize);
} else {
return nDecryptFinal(ctxPtr, output, outputOffest, outputSize);
}
}
private native int nEncryptFinal(long ctxPtr, byte[] output, int outputOffest, int outputSize);
private native int nDecryptFinal(long ctxPtr, byte[] output, int outputOffest, int outputSize);
@Override
protected int engineGetBlockSize() {
@@ -166,12 +174,6 @@ public class NativeAESCipherSpi extends CipherSpi {
protected int engineGetOutputSize(int inputLen) {
int totalLen = mBuffered + inputLen;
/*
if ( ! mPadding || ! mEncrypting ) {
return totalLen;
}
*/
int padLen = AES_BLOCK_SIZE - (totalLen % AES_BLOCK_SIZE);
// TODO: Round up to nearest full block (there's probably a better way to do this)
@@ -237,11 +239,21 @@ public class NativeAESCipherSpi extends CipherSpi {
mIV = params.getIV();
mEncrypting = opmode == Cipher.ENCRYPT_MODE;
mBuffered = 0;
mCtxPtr = nativeInit(mEncrypting, key.getEncoded(), mIV, mPadding);
mCtxPtr = nativeInitWrap(key.getEncoded(), mIV);
addToCleanupQueue(this, mCtxPtr);
}
private native long nativeInit(boolean encrypt, byte[] key, byte[] iv, boolean mPadding);
private long nativeInitWrap(byte[] key, byte[] iv) {
if (mEncrypting) {
return nEncryptInit(key, iv);
} else {
return nDecryptInit(key, iv);
}
}
private native long nEncryptInit(byte[] key, byte[] iv);
private native long nDecryptInit(byte[] key, byte[] iv);
@Override
protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
@@ -305,7 +317,7 @@ public class NativeAESCipherSpi extends CipherSpi {
int update(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) {
int outputSize = engineGetOutputSize(inputLen);
int out = nativeUpdate(mCtxPtr, input, inputOffset, inputLen, output, outputOffset, outputSize);
int out = nativeUpdateWrap(mCtxPtr, input, inputOffset, inputLen, output, outputOffset, outputSize);
mBuffered = (mBuffered + ((inputLen - out))) % AES_BLOCK_SIZE;
@@ -314,6 +326,15 @@ public class NativeAESCipherSpi extends CipherSpi {
}
private native int nativeUpdate(long ctxPtr, byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset, int outputSize);
private int nativeUpdateWrap(long ctxPtr, byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset, int outputSize) {
if (mEncrypting) {
return nEncryptUpdate(ctxPtr, input, inputOffset, inputLen, output, outputOffset, outputSize);
} else {
return nDecryptUpdate(ctxPtr, input, inputOffset, inputLen, output, outputOffset, outputSize);
}
}
private native int nEncryptUpdate(long ctxPtr, byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset, int outputSize);
private native int nDecryptUpdate(long ctxPtr, byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset, int outputSize);
}
@@ -34,13 +34,14 @@ public class NativeFinalKey extends FinalKey {
public byte[] transformMasterKey(byte[] seed, byte[] key, int rounds) throws IOException {
NativeLib.init();
return nativeTransformMasterKey(seed, key, rounds);
return nTransformMasterKey(seed, key, rounds);
}
private static native byte[] nativeTransformMasterKey(byte[] seed, byte[] key, int rounds);
private static native byte[] nTransformMasterKey(byte[] seed, byte[] key, int rounds);
// For testing
/*
public static byte[] reflect(byte[] key) {
NativeLib.init();
@@ -48,6 +49,7 @@ public class NativeFinalKey extends FinalKey {
}
private static native byte[] nativeReflect(byte[] key);
*/
}
@@ -39,20 +39,6 @@ public class FinalKeyTest extends TestCase {
mRand = new Random();
}
public void testReflect() {
boolean available = NativeFinalKey.availble();
assertTrue("NativeFinalKey library cannot be loaded", available);
byte[] key = new byte[32];
mRand.nextBytes(key);
byte[] out = NativeFinalKey.reflect(key);
assertArrayEquals("Array not reflected correctly", key, out);
}
public void testNativeAndroid() throws IOException {
// Test both an old and an even number to test my flip variable
testNativeFinalKey(5);