mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-11-30 22:02:57 -08:00
Refactor KDB cipher and better tests
This commit is contained in:
1 parent
3bf0de3888
commit
874fdb7da0
11 files changed
+143
-200
No files matched your search
@@ -20,7 +20,7 @@
|
||||
package com.kunzisoft.keepass.database.crypto
|
||||
|
||||
|
||||
import com.kunzisoft.encrypt.CipherEngineFactory
|
||||
import com.kunzisoft.encrypt.CipherFactory
|
||||
import java.security.InvalidAlgorithmParameterException
|
||||
import java.security.InvalidKeyException
|
||||
import java.security.NoSuchAlgorithmException
|
||||
@@ -31,7 +31,7 @@ class AesEngine : CipherEngine() {
|
||||
|
||||
@Throws(NoSuchAlgorithmException::class, NoSuchPaddingException::class, InvalidKeyException::class, InvalidAlgorithmParameterException::class)
|
||||
override fun getCipher(opmode: Int, key: ByteArray, IV: ByteArray): Cipher {
|
||||
return CipherEngineFactory.getAES(opmode, key, IV)
|
||||
return CipherFactory.getAES(opmode, key, IV)
|
||||
}
|
||||
|
||||
override fun getEncryptionAlgorithm(): EncryptionAlgorithm {
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
*/
|
||||
package com.kunzisoft.keepass.database.crypto
|
||||
|
||||
import com.kunzisoft.encrypt.CipherEngineFactory
|
||||
import com.kunzisoft.encrypt.CipherFactory
|
||||
import java.security.InvalidAlgorithmParameterException
|
||||
import java.security.InvalidKeyException
|
||||
import java.security.NoSuchAlgorithmException
|
||||
@@ -34,7 +34,7 @@ class ChaCha20Engine : CipherEngine() {
|
||||
|
||||
@Throws(NoSuchAlgorithmException::class, NoSuchPaddingException::class, InvalidKeyException::class, InvalidAlgorithmParameterException::class)
|
||||
override fun getCipher(opmode: Int, key: ByteArray, IV: ByteArray): Cipher {
|
||||
return CipherEngineFactory.getChacha20(opmode, key, IV)
|
||||
return CipherFactory.getChacha20(opmode, key, IV)
|
||||
}
|
||||
|
||||
override fun getEncryptionAlgorithm(): EncryptionAlgorithm {
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
*/
|
||||
package com.kunzisoft.keepass.database.crypto
|
||||
|
||||
import com.kunzisoft.encrypt.CipherEngineFactory
|
||||
import com.kunzisoft.encrypt.CipherFactory
|
||||
import java.security.InvalidAlgorithmParameterException
|
||||
import java.security.InvalidKeyException
|
||||
import java.security.NoSuchAlgorithmException
|
||||
@@ -30,7 +30,7 @@ class TwofishEngine : CipherEngine() {
|
||||
|
||||
@Throws(NoSuchAlgorithmException::class, NoSuchPaddingException::class, InvalidKeyException::class, InvalidAlgorithmParameterException::class)
|
||||
override fun getCipher(opmode: Int, key: ByteArray, IV: ByteArray): Cipher {
|
||||
return CipherEngineFactory.getTwofish(opmode, key, IV)
|
||||
return CipherFactory.getTwofish(opmode, key, IV)
|
||||
}
|
||||
|
||||
override fun getEncryptionAlgorithm(): EncryptionAlgorithm {
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
|
||||
package com.kunzisoft.keepass.database.file.input
|
||||
|
||||
import com.kunzisoft.encrypt.CipherFactory
|
||||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.database.crypto.EncryptionAlgorithm
|
||||
import com.kunzisoft.keepass.database.element.Database
|
||||
@@ -35,12 +34,11 @@ import com.kunzisoft.keepass.database.file.DatabaseHeaderKDB
|
||||
import com.kunzisoft.keepass.stream.*
|
||||
import com.kunzisoft.keepass.tasks.ProgressTaskUpdater
|
||||
import java.io.*
|
||||
import java.security.*
|
||||
import java.security.DigestInputStream
|
||||
import java.security.MessageDigest
|
||||
import java.security.NoSuchAlgorithmException
|
||||
import java.util.*
|
||||
import javax.crypto.Cipher
|
||||
import javax.crypto.NoSuchPaddingException
|
||||
import javax.crypto.spec.IvParameterSpec
|
||||
import javax.crypto.spec.SecretKeySpec
|
||||
|
||||
|
||||
/**
|
||||
@@ -131,32 +129,14 @@ class DatabaseInputKDB(cacheDirectory: File)
|
||||
mDatabase.numberKeyEncryptionRounds)
|
||||
|
||||
progressTaskUpdater?.updateMessage(R.string.decrypting_db)
|
||||
// Initialize Rijndael algorithm
|
||||
val cipher: Cipher = try {
|
||||
// TODO Encapsulate
|
||||
when {
|
||||
mDatabase.encryptionAlgorithm === EncryptionAlgorithm.AESRijndael -> {
|
||||
CipherFactory.getInstance("AES/CBC/PKCS5Padding")
|
||||
}
|
||||
mDatabase.encryptionAlgorithm === EncryptionAlgorithm.Twofish -> {
|
||||
CipherFactory.getInstance("Twofish/CBC/PKCS7PADDING")
|
||||
}
|
||||
else -> throw IOException("Encryption algorithm is not supported")
|
||||
}
|
||||
} catch (e1: NoSuchAlgorithmException) {
|
||||
throw IOException("No such algorithm")
|
||||
} catch (e1: NoSuchPaddingException) {
|
||||
throw IOException("No such pdading")
|
||||
}
|
||||
|
||||
try {
|
||||
cipher.init(Cipher.DECRYPT_MODE,
|
||||
SecretKeySpec(mDatabase.finalKey, "AES"),
|
||||
IvParameterSpec(header.encryptionIV))
|
||||
} catch (e1: InvalidKeyException) {
|
||||
throw IOException("Invalid key")
|
||||
} catch (e1: InvalidAlgorithmParameterException) {
|
||||
throw IOException("Invalid algorithm parameter.")
|
||||
val cipher: Cipher = try {
|
||||
mDatabase.encryptionAlgorithm
|
||||
.cipherEngine.getCipher(Cipher.DECRYPT_MODE,
|
||||
mDatabase.finalKey ?: ByteArray(0),
|
||||
header.encryptionIV)
|
||||
} catch (e: Exception) {
|
||||
throw IOException("Algorithm not supported.", e)
|
||||
}
|
||||
|
||||
val messageDigest: MessageDigest
|
||||
|
||||
@@ -156,7 +156,7 @@ class DatabaseInputKDBX(cacheDirectory: File)
|
||||
val isPlain: InputStream
|
||||
if (mDatabase.kdbxVersion.toKotlinLong() < DatabaseHeaderKDBX.FILE_VERSION_32_4.toKotlinLong()) {
|
||||
|
||||
val decrypted = attachCipherStream(databaseInputStream, cipher)
|
||||
val decrypted = CipherInputStream(databaseInputStream, cipher)
|
||||
val dataDecrypted = LittleEndianDataInputStream(decrypted)
|
||||
val storedStartBytes: ByteArray?
|
||||
try {
|
||||
@@ -193,11 +193,10 @@ class DatabaseInputKDBX(cacheDirectory: File)
|
||||
|
||||
val hmIs = HmacBlockInputStream(isData, true, hmacKey)
|
||||
|
||||
isPlain = attachCipherStream(hmIs, cipher)
|
||||
isPlain = CipherInputStream(hmIs, cipher)
|
||||
}
|
||||
|
||||
val inputStreamXml: InputStream
|
||||
inputStreamXml = when (mDatabase.compressionAlgorithm) {
|
||||
val inputStreamXml: InputStream = when (mDatabase.compressionAlgorithm) {
|
||||
CompressionAlgorithm.GZip -> GZIPInputStream(isPlain)
|
||||
else -> isPlain
|
||||
}
|
||||
@@ -232,10 +231,6 @@ class DatabaseInputKDBX(cacheDirectory: File)
|
||||
return mDatabase
|
||||
}
|
||||
|
||||
private fun attachCipherStream(inputStream: InputStream, cipher: Cipher): InputStream {
|
||||
return CipherInputStream(inputStream, cipher)
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
private fun loadInnerHeader(inputStream: InputStream, header: DatabaseHeaderKDBX) {
|
||||
val lis = LittleEndianDataInputStream(inputStream)
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
*/
|
||||
package com.kunzisoft.keepass.database.file.output
|
||||
|
||||
import com.kunzisoft.encrypt.CipherFactory
|
||||
import com.kunzisoft.encrypt.UnsignedInt
|
||||
import com.kunzisoft.encrypt.stream.LittleEndianDataOutputStream
|
||||
import com.kunzisoft.encrypt.stream.NullOutputStream
|
||||
@@ -37,8 +36,7 @@ import java.security.*
|
||||
import java.util.*
|
||||
import javax.crypto.Cipher
|
||||
import javax.crypto.CipherOutputStream
|
||||
import javax.crypto.spec.IvParameterSpec
|
||||
import javax.crypto.spec.SecretKeySpec
|
||||
import javax.crypto.NoSuchPaddingException
|
||||
|
||||
class DatabaseOutputKDB(private val mDatabaseKDB: DatabaseKDB,
|
||||
outputStream: OutputStream)
|
||||
@@ -67,31 +65,27 @@ class DatabaseOutputKDB(private val mDatabaseKDB: DatabaseKDB,
|
||||
|
||||
val finalKey = getFinalKey(header)
|
||||
|
||||
val cipher: Cipher
|
||||
cipher = try {
|
||||
when {
|
||||
// TODO Encapsulate
|
||||
mDatabaseKDB.encryptionAlgorithm === EncryptionAlgorithm.AESRijndael->
|
||||
CipherFactory.getInstance("AES/CBC/PKCS5Padding")
|
||||
mDatabaseKDB.encryptionAlgorithm === EncryptionAlgorithm.Twofish ->
|
||||
CipherFactory.getInstance("Twofish/CBC/PKCS7PADDING")
|
||||
else ->
|
||||
throw Exception()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
throw DatabaseOutputException("Algorithm not supported.", e)
|
||||
val cipher: Cipher = try {
|
||||
mDatabaseKDB.encryptionAlgorithm
|
||||
.cipherEngine.getCipher(Cipher.ENCRYPT_MODE,
|
||||
finalKey ?: ByteArray(0),
|
||||
header.encryptionIV)
|
||||
} catch (e1: NoSuchAlgorithmException) {
|
||||
throw IOException("No such algorithm")
|
||||
} catch (e1: NoSuchPaddingException) {
|
||||
throw IOException("No such padding")
|
||||
} catch (e1: InvalidKeyException) {
|
||||
throw IOException("Invalid key")
|
||||
} catch (e1: InvalidAlgorithmParameterException) {
|
||||
throw IOException("Invalid algorithm parameter.")
|
||||
}
|
||||
|
||||
try {
|
||||
cipher.init(Cipher.ENCRYPT_MODE,
|
||||
SecretKeySpec(finalKey, "AES"),
|
||||
IvParameterSpec(header.encryptionIV))
|
||||
val cos = CipherOutputStream(mOutputStream, cipher)
|
||||
val bos = BufferedOutputStream(cos)
|
||||
outputPlanGroupAndEntries(bos)
|
||||
bos.flush()
|
||||
bos.close()
|
||||
|
||||
} catch (e: InvalidKeyException) {
|
||||
throw DatabaseOutputException("Invalid key", e)
|
||||
} catch (e: InvalidAlgorithmParameterException) {
|
||||
|
||||
Reference in new issue
Block a user