Native AES call refactoring

This commit is contained in:
J-Jamet committed 2021-03-26 16:06:28 +01:00
1 parent 25e0cec2cc
commit 2e18beff27
8 files changed
+70 -143

No files matched your search

@@ -19,8 +19,7 @@
*/
package com.kunzisoft.encrypt
import com.kunzisoft.encrypt.aes.AndroidAESKeyTransformer
import com.kunzisoft.encrypt.aes.NativeAESKeyTransformer
import com.kunzisoft.encrypt.aes.AESKeyTransformerFactory
import org.junit.Assert.assertArrayEquals
import org.junit.Test
import java.io.ByteArrayInputStream
@@ -105,11 +104,8 @@ class AESTest {
mRand.nextBytes(key)
val rounds = 60000L
val androidAESKey = AndroidAESKeyTransformer()
val androidKey = androidAESKey.transformMasterKey(seed, key, rounds)
val nativeAESKey = NativeAESKeyTransformer()
val nativeKey = nativeAESKey.transformMasterKey(seed, key, rounds)
val androidKey = AESKeyTransformerFactory.transformMasterKeyInJVM(seed, key, rounds)
val nativeKey = AESKeyTransformerFactory.transformMasterKey(seed, key, rounds)
assertArrayEquals("Does not match", androidKey, nativeKey)
}
@@ -1,5 +1,6 @@
package com.kunzisoft.encrypt
import android.util.Log
import com.kunzisoft.encrypt.aes.AESProvider
import org.bouncycastle.jce.provider.BouncyCastleProvider
import java.security.InvalidAlgorithmParameterException
@@ -21,8 +22,14 @@ object CipherFactory {
@Throws(NoSuchAlgorithmException::class, NoSuchPaddingException::class, InvalidKeyException::class, InvalidAlgorithmParameterException::class)
fun getAES(opmode: Int, key: ByteArray, IV: ByteArray, forceNative: Boolean = false): Cipher {
val transformation = "AES/CBC/PKCS5Padding"
val cipher = if (forceNative || (!NativeBlockList.isBlocked && NativeLib.loaded())) {
Cipher.getInstance(transformation, AESProvider())
val cipher = if (forceNative || NativeLib.loaded()) {
// Try native implementation
try {
Cipher.getInstance(transformation, AESProvider())
} catch (exception: Exception) {
Log.e(CipherFactory::class.java.simpleName, "Unable to retrieve native AES cipher", exception)
Cipher.getInstance(transformation)
}
} else {
Cipher.getInstance(transformation)
}
@@ -1,9 +0,0 @@
package com.kunzisoft.encrypt
import android.os.Build
object NativeBlockList {
val isBlocked: Boolean by lazy {
Build.MODEL == "A500"
}
}
@@ -38,8 +38,6 @@ object NativeLib {
isLoaded = true
loadSuccess = true
}
return loadSuccess
}
}
@@ -19,17 +19,65 @@
*/
package com.kunzisoft.encrypt.aes
import com.kunzisoft.encrypt.NativeBlockList
import android.annotation.SuppressLint
import android.util.Log
import com.kunzisoft.encrypt.HashManager
import com.kunzisoft.encrypt.NativeLib
import java.io.IOException
import java.security.InvalidKeyException
import javax.crypto.Cipher
import javax.crypto.ShortBufferException
import javax.crypto.spec.SecretKeySpec
object AESKeyTransformerFactory : KeyTransformer() {
override fun transformMasterKey(seed: ByteArray?, key: ByteArray?, rounds: Long?): ByteArray? {
object AESKeyTransformerFactory {
fun transformMasterKey(seed: ByteArray?, key: ByteArray?, rounds: Long?): ByteArray? {
// Prefer the native final key implementation
val keyTransformer = if (!NativeBlockList.isBlocked && NativeAESKeyTransformer.available()) {
NativeAESKeyTransformer()
} else {
return try {
NativeLib.init()
NativeAESKeyTransformer.nTransformMasterKey(seed, key, rounds!!)
} catch (exception: Exception) {
Log.e(AESKeyTransformerFactory::class.java.simpleName, "Unable to perform native AES key transformation", exception)
// Fall back on the android crypto implementation
AndroidAESKeyTransformer()
transformMasterKeyInJVM(seed, key, rounds)
}
return keyTransformer.transformMasterKey(seed, key, rounds)
}
@SuppressLint("GetInstance")
@Throws(IOException::class)
fun transformMasterKeyInJVM(seed: ByteArray?, key: ByteArray?, rounds: Long?): ByteArray {
val cipher: Cipher = try {
Cipher.getInstance("AES/ECB/NoPadding")
} catch (e: Exception) {
throw IOException("Unable to get the cipher", e)
}
try {
cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec(seed, "AES"))
} catch (e: InvalidKeyException) {
throw IOException("Unable to init the cipher", e)
}
if (key == null) {
throw IOException("Invalid key")
}
if (rounds == null) {
throw IOException("Invalid rounds")
}
// Encrypt key rounds times
val keyLength = key.size
val newKey = ByteArray(keyLength)
System.arraycopy(key, 0, newKey, 0, keyLength)
val destKey = ByteArray(keyLength)
for (i in 0 until rounds) {
try {
cipher.update(newKey, 0, newKey.size, destKey, 0)
System.arraycopy(destKey, 0, newKey, 0, newKey.size)
} catch (e: ShortBufferException) {
throw IOException("Short buffer", e)
}
}
// Hash the key
return HashManager.hashSha256(newKey)
}
}
@@ -1,67 +0,0 @@
/*
* Copyright 2020 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePassDX.
*
* KeePassDX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePassDX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePassDX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.encrypt.aes
import com.kunzisoft.encrypt.HashManager
import java.io.IOException
import java.security.InvalidKeyException
import java.security.MessageDigest
import javax.crypto.Cipher
import javax.crypto.ShortBufferException
import javax.crypto.spec.SecretKeySpec
class AndroidAESKeyTransformer : KeyTransformer() {
@Throws(IOException::class)
override fun transformMasterKey(seed: ByteArray?, key: ByteArray?, rounds: Long?): ByteArray? {
val cipher: Cipher = try {
Cipher.getInstance("AES/ECB/NoPadding")
} catch (e: Exception) {
throw IOException("Unable to get the cipher", e)
}
try {
cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec(seed, "AES"))
} catch (e: InvalidKeyException) {
throw IOException("Unable to init the cipher", e)
}
if (key == null) {
throw IOException("Invalid key")
}
if (rounds == null) {
throw IOException("Invalid rounds")
}
// Encrypt key rounds times
val keyLength = key.size
val newKey = ByteArray(keyLength)
System.arraycopy(key, 0, newKey, 0, keyLength)
val destKey = ByteArray(keyLength)
for (i in 0 until rounds) {
try {
cipher.update(newKey, 0, newKey.size, destKey, 0)
System.arraycopy(destKey, 0, newKey, 0, newKey.size)
} catch (e: ShortBufferException) {
throw IOException("Short buffer", e)
}
}
// Hash the key
return HashManager.hashSha256(newKey)
}
}
@@ -1,27 +0,0 @@
/*
* Copyright 2020 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePassDX.
*
* KeePassDX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePassDX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePassDX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.encrypt.aes
import java.io.IOException
abstract class KeyTransformer {
@Throws(IOException::class)
abstract fun transformMasterKey(seed: ByteArray?, key: ByteArray?, rounds: Long?): ByteArray?
}
@@ -19,26 +19,7 @@
*/
package com.kunzisoft.encrypt.aes;
import com.kunzisoft.encrypt.NativeLib;
public class NativeAESKeyTransformer {
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
public class NativeAESKeyTransformer extends KeyTransformer {
public static boolean available() {
return NativeLib.INSTANCE.init();
}
@Nullable
@Override
public byte[] transformMasterKey(@Nullable byte[] seed, @Nullable byte[] key, @Nullable Long rounds) throws IOException {
NativeLib.INSTANCE.init();
return nTransformMasterKey(seed, key, rounds);
}
private static native byte[] nTransformMasterKey(byte[] seed, byte[] key, long rounds);
public static native byte[] nTransformMasterKey(byte[] seed, byte[] key, long rounds);
}