Remove NullOutputStream

This commit is contained in:
J-Jamet
2021-03-24 19:50:56 +01:00
parent 50d3282a65
commit 87858762d4
7 changed files with 18 additions and 107 deletions

View File

@@ -36,12 +36,12 @@ class EncryptionTest {
@Test @Test
fun testCipherFactory() { fun testCipherFactory() {
val key = ByteArray(32) val key = ByteArray(32)
rand.nextBytes(key)
val iv = ByteArray(16) val iv = ByteArray(16)
rand.nextBytes(iv)
val plaintext = ByteArray(1024) val plaintext = ByteArray(1024)
rand.nextBytes(key)
rand.nextBytes(iv)
rand.nextBytes(plaintext) rand.nextBytes(plaintext)
val aes = EncryptionAlgorithm.AESRijndael.cipherEngine val aes = EncryptionAlgorithm.AESRijndael.cipherEngine
@@ -59,12 +59,12 @@ class EncryptionTest {
val length = 1024 val length = 1024
val key = ByteArray(32) val key = ByteArray(32)
rand.nextBytes(key)
val iv = ByteArray(16) val iv = ByteArray(16)
rand.nextBytes(iv)
val plaintext = ByteArray(length) val plaintext = ByteArray(length)
rand.nextBytes(key)
rand.nextBytes(iv)
rand.nextBytes(plaintext) rand.nextBytes(plaintext)
val aes = EncryptionAlgorithm.AESRijndael.cipherEngine val aes = EncryptionAlgorithm.AESRijndael.cipherEngine

View File

@@ -20,10 +20,8 @@
package com.kunzisoft.keepass.database.crypto package com.kunzisoft.keepass.database.crypto
import com.kunzisoft.encrypt.UnsignedLong import com.kunzisoft.encrypt.UnsignedLong
import com.kunzisoft.encrypt.stream.NullOutputStream import com.kunzisoft.encrypt.stream.uLongTo8Bytes
import com.kunzisoft.encrypt.stream.write8BytesLong
import java.io.IOException import java.io.IOException
import java.security.DigestOutputStream
import java.security.InvalidKeyException import java.security.InvalidKeyException
import java.security.MessageDigest import java.security.MessageDigest
import java.security.NoSuchAlgorithmException import java.security.NoSuchAlgorithmException
@@ -53,15 +51,8 @@ object HmacBlock {
} catch (e: NoSuchAlgorithmException) { } catch (e: NoSuchAlgorithmException) {
throw RuntimeException(e) throw RuntimeException(e)
} }
hash.update(uLongTo8Bytes(blockIndex))
val digestOutputStream = DigestOutputStream(NullOutputStream(), hash) hash.update(key)
try {
digestOutputStream.write8BytesLong(blockIndex)
digestOutputStream.write(key)
digestOutputStream.close()
} catch (e: IOException) {
throw RuntimeException(e)
}
return hash.digest() return hash.digest()
} }
} }

View File

@@ -20,7 +20,6 @@
package com.kunzisoft.keepass.database.element.database package com.kunzisoft.keepass.database.element.database
import com.kunzisoft.encrypt.aes.AESKeyTransformerFactory import com.kunzisoft.encrypt.aes.AESKeyTransformerFactory
import com.kunzisoft.encrypt.stream.NullOutputStream
import com.kunzisoft.keepass.database.crypto.EncryptionAlgorithm import com.kunzisoft.keepass.database.crypto.EncryptionAlgorithm
import com.kunzisoft.keepass.database.crypto.kdf.KdfEngine import com.kunzisoft.keepass.database.crypto.kdf.KdfEngine
import com.kunzisoft.keepass.database.crypto.kdf.KdfFactory import com.kunzisoft.keepass.database.crypto.kdf.KdfFactory
@@ -33,7 +32,6 @@ import com.kunzisoft.keepass.database.element.node.NodeIdUUID
import com.kunzisoft.keepass.database.element.node.NodeVersioned import com.kunzisoft.keepass.database.element.node.NodeVersioned
import java.io.IOException import java.io.IOException
import java.io.InputStream import java.io.InputStream
import java.security.DigestOutputStream
import java.security.MessageDigest import java.security.MessageDigest
import java.security.NoSuchAlgorithmException import java.security.NoSuchAlgorithmException
import java.util.* import java.util.*
@@ -153,12 +151,9 @@ class DatabaseKDB : DatabaseVersioned<Int, UUID, GroupKDB, EntryKDB>() {
throw IOException("SHA-256 not implemented here.") throw IOException("SHA-256 not implemented here.")
} }
val nos = NullOutputStream()
val dos = DigestOutputStream(nos, messageDigest)
// Encrypt the master key a few times to make brute-force key-search harder // Encrypt the master key a few times to make brute-force key-search harder
dos.write(masterSeed) messageDigest.update(masterSeed)
dos.write(AESKeyTransformerFactory.transformMasterKey(masterSeed2, masterKey, numRounds) ?: ByteArray(0)) messageDigest.update(AESKeyTransformerFactory.transformMasterKey(masterSeed2, masterKey, numRounds) ?: ByteArray(0))
finalKey = messageDigest.digest() finalKey = messageDigest.digest()
} }

View File

@@ -21,10 +21,8 @@ package com.kunzisoft.keepass.database.file
import com.kunzisoft.encrypt.CrsAlgorithm import com.kunzisoft.encrypt.CrsAlgorithm
import com.kunzisoft.encrypt.UnsignedInt import com.kunzisoft.encrypt.UnsignedInt
import com.kunzisoft.encrypt.UnsignedLong
import com.kunzisoft.encrypt.stream.* import com.kunzisoft.encrypt.stream.*
import com.kunzisoft.keepass.database.action.node.NodeHandler import com.kunzisoft.keepass.database.action.node.NodeHandler
import com.kunzisoft.keepass.database.crypto.HmacBlock
import com.kunzisoft.keepass.database.crypto.VariantDictionary import com.kunzisoft.keepass.database.crypto.VariantDictionary
import com.kunzisoft.keepass.database.crypto.kdf.AesKdf import com.kunzisoft.keepass.database.crypto.kdf.AesKdf
import com.kunzisoft.keepass.database.crypto.kdf.KdfFactory import com.kunzisoft.keepass.database.crypto.kdf.KdfFactory
@@ -42,7 +40,6 @@ import java.io.InputStream
import java.security.DigestInputStream import java.security.DigestInputStream
import java.security.MessageDigest import java.security.MessageDigest
import java.security.NoSuchAlgorithmException import java.security.NoSuchAlgorithmException
import javax.crypto.Mac
class DatabaseHeaderKDBX(private val databaseV4: DatabaseKDBX) : DatabaseHeader() { class DatabaseHeaderKDBX(private val databaseV4: DatabaseKDBX) : DatabaseHeader() {
var innerRandomStreamKey: ByteArray = ByteArray(32) var innerRandomStreamKey: ByteArray = ByteArray(32)
@@ -322,12 +319,5 @@ class DatabaseHeaderKDBX(private val databaseV4: DatabaseKDBX) : DatabaseHeader(
fun matchesHeader(sig1: UnsignedInt, sig2: UnsignedInt): Boolean { fun matchesHeader(sig1: UnsignedInt, sig2: UnsignedInt): Boolean {
return sig1 == PWM_DBSIG_1 && (sig2 == DBSIG_PRE2 || sig2 == DBSIG_2) return sig1 == PWM_DBSIG_1 && (sig2 == DBSIG_PRE2 || sig2 == DBSIG_2)
} }
@Throws(IOException::class)
fun computeHeaderHmac(header: ByteArray, key: ByteArray): ByteArray {
val blockKey = HmacBlock.getHmacKey64(key, UnsignedLong.MAX)
val hmac: Mac = HmacBlock.getHmacSha256(blockKey)
return hmac.doFinal(header)
}
} }
} }

View File

@@ -20,7 +20,6 @@
package com.kunzisoft.keepass.database.file.output package com.kunzisoft.keepass.database.file.output
import com.kunzisoft.encrypt.UnsignedInt import com.kunzisoft.encrypt.UnsignedInt
import com.kunzisoft.encrypt.stream.NullOutputStream
import com.kunzisoft.encrypt.stream.write2BytesUShort import com.kunzisoft.encrypt.stream.write2BytesUShort
import com.kunzisoft.encrypt.stream.write4BytesUInt import com.kunzisoft.encrypt.stream.write4BytesUInt
import com.kunzisoft.keepass.database.crypto.EncryptionAlgorithm import com.kunzisoft.keepass.database.crypto.EncryptionAlgorithm
@@ -138,10 +137,8 @@ class DatabaseOutputKDB(private val mDatabaseKDB: DatabaseKDB,
throw DatabaseOutputException("SHA-256 not implemented here.", e) throw DatabaseOutputException("SHA-256 not implemented here.", e)
} }
var nos = NullOutputStream()
val headerDos = DigestOutputStream(nos, headerDigest)
// Output header for the purpose of calculating the header checksum // Output header for the purpose of calculating the header checksum
val headerDos = DigestOutputStream(NullOutputStream(), headerDigest)
var pho = DatabaseHeaderOutputKDB(header, headerDos) var pho = DatabaseHeaderOutputKDB(header, headerDos)
try { try {
pho.outputStart() pho.outputStart()
@@ -155,8 +152,7 @@ class DatabaseOutputKDB(private val mDatabaseKDB: DatabaseKDB,
headerHashBlock = getHeaderHashBuffer(headerHash) headerHashBlock = getHeaderHashBuffer(headerHash)
// Output database for the purpose of calculating the content checksum // Output database for the purpose of calculating the content checksum
nos = NullOutputStream() val dos = DigestOutputStream(NullOutputStream(), messageDigest)
val dos = DigestOutputStream(nos, messageDigest)
val bos = BufferedOutputStream(dos) val bos = BufferedOutputStream(dos)
try { try {
outputPlanGroupAndEntries(bos) outputPlanGroupAndEntries(bos)
@@ -184,7 +180,10 @@ class DatabaseOutputKDB(private val mDatabaseKDB: DatabaseKDB,
return header return header
} }
@Suppress("CAST_NEVER_SUCCEEDS") class NullOutputStream : OutputStream() {
override fun write(oneByte: Int) {}
}
@Throws(DatabaseOutputException::class) @Throws(DatabaseOutputException::class)
fun outputPlanGroupAndEntries(outputStream: OutputStream) { fun outputPlanGroupAndEntries(outputStream: OutputStream) {

View File

@@ -19,9 +19,6 @@
*/ */
package com.kunzisoft.encrypt package com.kunzisoft.encrypt
import com.kunzisoft.encrypt.stream.NullOutputStream
import java.io.IOException
import java.security.DigestOutputStream
import java.security.MessageDigest import java.security.MessageDigest
import java.security.NoSuchAlgorithmException import java.security.NoSuchAlgorithmException
@@ -42,17 +39,7 @@ object HashManager {
} catch (e: NoSuchAlgorithmException) { } catch (e: NoSuchAlgorithmException) {
throw RuntimeException(e) throw RuntimeException(e)
} }
hash.update(data, offset, count)
val nos = NullOutputStream()
val dos = DigestOutputStream(nos, hash)
try {
dos.write(data, offset, count)
dos.close()
} catch (e: IOException) {
throw RuntimeException(e)
}
return hash.digest() return hash.digest()
} }
} }

View File

@@ -1,51 +0,0 @@
/*
* Copyright 2017 Brian Pellin, 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.stream
import java.io.IOException
import java.io.OutputStream
class NullOutputStream : OutputStream() {
@Throws(IOException::class)
override fun close() {
super.close()
}
@Throws(IOException::class)
override fun flush() {
super.flush()
}
@Throws(IOException::class)
override fun write(buffer: ByteArray, offset: Int, count: Int) {
super.write(buffer, offset, count)
}
@Throws(IOException::class)
override fun write(buffer: ByteArray) {
super.write(buffer)
}
@Throws(IOException::class)
override fun write(oneByte: Int) {
}
}