From 87e32b8eba3bb3a4ac00f10dbb4495a5bbf07520 Mon Sep 17 00:00:00 2001 From: Brian Pellin Date: Sun, 21 Nov 2010 16:47:27 -0600 Subject: [PATCH] Make changes to the android code to use the new native libraries. --- .../crypto/NativeAESCipherSpi.java | 53 +++++++++++++------ .../crypto/finalkey/NativeFinalKey.java | 6 ++- .../tests/crypto/FinalKeyTest.java | 14 ----- 3 files changed, 41 insertions(+), 32 deletions(-) diff --git a/src/com/keepassdroid/crypto/NativeAESCipherSpi.java b/src/com/keepassdroid/crypto/NativeAESCipherSpi.java index 8eccbb01f..e4b6ec229 100644 --- a/src/com/keepassdroid/crypto/NativeAESCipherSpi.java +++ b/src/com/keepassdroid/crypto/NativeAESCipherSpi.java @@ -83,7 +83,7 @@ public class NativeAESCipherSpi extends CipherSpi { Reference 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); } \ No newline at end of file diff --git a/src/com/keepassdroid/crypto/finalkey/NativeFinalKey.java b/src/com/keepassdroid/crypto/finalkey/NativeFinalKey.java index c0c4149e6..ce5982950 100644 --- a/src/com/keepassdroid/crypto/finalkey/NativeFinalKey.java +++ b/src/com/keepassdroid/crypto/finalkey/NativeFinalKey.java @@ -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); + */ } diff --git a/tests/src/com/keepassdroid/tests/crypto/FinalKeyTest.java b/tests/src/com/keepassdroid/tests/crypto/FinalKeyTest.java index ebd4f7ccb..7692ef69b 100644 --- a/tests/src/com/keepassdroid/tests/crypto/FinalKeyTest.java +++ b/tests/src/com/keepassdroid/tests/crypto/FinalKeyTest.java @@ -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);