From 2e18beff275eddee4c5e93ac1db8cb3d52261e56 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Fri, 26 Mar 2021 16:06:28 +0100 Subject: [PATCH] Native AES call refactoring --- .../java/com/kunzisoft/encrypt/AESTest.kt | 10 +-- .../com/kunzisoft/encrypt/CipherFactory.kt | 11 ++- .../com/kunzisoft/encrypt/NativeBlockList.kt | 9 --- .../java/com/kunzisoft/encrypt/NativeLib.kt | 2 - .../encrypt/aes/AESKeyTransformerFactory.kt | 64 +++++++++++++++--- .../encrypt/aes/AndroidAESKeyTransformer.kt | 67 ------------------- .../kunzisoft/encrypt/aes/KeyTransformer.kt | 27 -------- .../encrypt/aes/NativeAESKeyTransformer.java | 23 +------ 8 files changed, 70 insertions(+), 143 deletions(-) delete mode 100644 encrypt/src/main/java/com/kunzisoft/encrypt/NativeBlockList.kt delete mode 100644 encrypt/src/main/java/com/kunzisoft/encrypt/aes/AndroidAESKeyTransformer.kt delete mode 100644 encrypt/src/main/java/com/kunzisoft/encrypt/aes/KeyTransformer.kt diff --git a/encrypt/src/androidTest/java/com/kunzisoft/encrypt/AESTest.kt b/encrypt/src/androidTest/java/com/kunzisoft/encrypt/AESTest.kt index 7b760effc..20dd53c2f 100644 --- a/encrypt/src/androidTest/java/com/kunzisoft/encrypt/AESTest.kt +++ b/encrypt/src/androidTest/java/com/kunzisoft/encrypt/AESTest.kt @@ -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) } diff --git a/encrypt/src/main/java/com/kunzisoft/encrypt/CipherFactory.kt b/encrypt/src/main/java/com/kunzisoft/encrypt/CipherFactory.kt index c9e74f2bb..c2830f814 100644 --- a/encrypt/src/main/java/com/kunzisoft/encrypt/CipherFactory.kt +++ b/encrypt/src/main/java/com/kunzisoft/encrypt/CipherFactory.kt @@ -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) } diff --git a/encrypt/src/main/java/com/kunzisoft/encrypt/NativeBlockList.kt b/encrypt/src/main/java/com/kunzisoft/encrypt/NativeBlockList.kt deleted file mode 100644 index 5496619bf..000000000 --- a/encrypt/src/main/java/com/kunzisoft/encrypt/NativeBlockList.kt +++ /dev/null @@ -1,9 +0,0 @@ -package com.kunzisoft.encrypt - -import android.os.Build - -object NativeBlockList { - val isBlocked: Boolean by lazy { - Build.MODEL == "A500" - } -} \ No newline at end of file diff --git a/encrypt/src/main/java/com/kunzisoft/encrypt/NativeLib.kt b/encrypt/src/main/java/com/kunzisoft/encrypt/NativeLib.kt index 13076f072..c743e4fa0 100644 --- a/encrypt/src/main/java/com/kunzisoft/encrypt/NativeLib.kt +++ b/encrypt/src/main/java/com/kunzisoft/encrypt/NativeLib.kt @@ -38,8 +38,6 @@ object NativeLib { isLoaded = true loadSuccess = true } - return loadSuccess } - } diff --git a/encrypt/src/main/java/com/kunzisoft/encrypt/aes/AESKeyTransformerFactory.kt b/encrypt/src/main/java/com/kunzisoft/encrypt/aes/AESKeyTransformerFactory.kt index 8734faaf1..84f9c61d8 100644 --- a/encrypt/src/main/java/com/kunzisoft/encrypt/aes/AESKeyTransformerFactory.kt +++ b/encrypt/src/main/java/com/kunzisoft/encrypt/aes/AESKeyTransformerFactory.kt @@ -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) } } \ No newline at end of file diff --git a/encrypt/src/main/java/com/kunzisoft/encrypt/aes/AndroidAESKeyTransformer.kt b/encrypt/src/main/java/com/kunzisoft/encrypt/aes/AndroidAESKeyTransformer.kt deleted file mode 100644 index 1fd0b370d..000000000 --- a/encrypt/src/main/java/com/kunzisoft/encrypt/aes/AndroidAESKeyTransformer.kt +++ /dev/null @@ -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 . - * - */ -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) - } -} \ No newline at end of file diff --git a/encrypt/src/main/java/com/kunzisoft/encrypt/aes/KeyTransformer.kt b/encrypt/src/main/java/com/kunzisoft/encrypt/aes/KeyTransformer.kt deleted file mode 100644 index f26a3a9f5..000000000 --- a/encrypt/src/main/java/com/kunzisoft/encrypt/aes/KeyTransformer.kt +++ /dev/null @@ -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 . - * - */ -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? -} \ No newline at end of file diff --git a/encrypt/src/main/java/com/kunzisoft/encrypt/aes/NativeAESKeyTransformer.java b/encrypt/src/main/java/com/kunzisoft/encrypt/aes/NativeAESKeyTransformer.java index 37759855d..a4e86bcd7 100644 --- a/encrypt/src/main/java/com/kunzisoft/encrypt/aes/NativeAESKeyTransformer.java +++ b/encrypt/src/main/java/com/kunzisoft/encrypt/aes/NativeAESKeyTransformer.java @@ -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); }