From ffb6e009a89504d3137e5c0bae5b992ddad4ab1c Mon Sep 17 00:00:00 2001 From: PuPha Date: Wed, 7 Aug 2024 15:28:29 +0700 Subject: [PATCH 1/2] Revert "Merge pull request #14 from bitmark-inc/Sang/security_audit/fix/did_signature" This reverts commit 2b04cd9a005ef31cb0b774eb356238e191f48414, reversing changes made to aee87f20cd3e4b288ac0b3e93f9388382d641bdc. --- .../bitmark/libauk/storage/WalletStorage.kt | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/libauk/src/main/java/com/bitmark/libauk/storage/WalletStorage.kt b/libauk/src/main/java/com/bitmark/libauk/storage/WalletStorage.kt index d95419a..a34de0f 100644 --- a/libauk/src/main/java/com/bitmark/libauk/storage/WalletStorage.kt +++ b/libauk/src/main/java/com/bitmark/libauk/storage/WalletStorage.kt @@ -42,7 +42,7 @@ import kotlin.Pair const val SEED_FILE_NAME = "libauk_seed.dat" const val ETH_KEY_INFO_FILE_NAME = "libauk_eth_key_info.dat" const val SEED_PUBLIC_DATA_FILE_NAME = "libauk_seed_public_data.dat" -const val PRE_GENERATE_ADDRESS_LIMIT = 10 +const val PRE_GENERATE_ADDRESS_LIMIT = 3 interface WalletStorage { fun createKey(passphrase: String? = "", name: String, isPrivate: Boolean): Completable fun importKey( @@ -78,6 +78,7 @@ interface WalletStorage { ): Single fun readOnFilesDir(name: String): Single + fun writeOnFilesDir(name: String, data: ByteArray, isPrivate: Boolean) fun encryptFile(input: File, output: File): Completable fun decryptFile(input: File, output: File, usingLegacy: Boolean): Completable @@ -176,8 +177,7 @@ internal class WalletStorageImpl(private val secureFileStorage: SecureFileStorag /* accountDidKey */ val accountDID = generateAccountDID(seed) - val seedByte = getSeedBytes(seed) - val accountDidPrivateKey = Bip32ECKeyPair.generateKeyPair(seedByte) + val accountDidPrivateKey = Bip32ECKeyPair.generateKeyPair(seed.data) /* pre-generate 100 eth addresses */ val preGenerateEthAddresses = preGenerateETHAddresses(seed, 0, PRE_GENERATE_ADDRESS_LIMIT) @@ -218,12 +218,6 @@ internal class WalletStorageImpl(private val secureFileStorage: SecureFileStorag newGsonInstance().fromJson(String(json)) } - private fun getSeedBytes(walletSeed: Seed): ByteArray { - val mnemonic = MnemonicUtils.generateMnemonic(walletSeed.data) - val seed = MnemonicUtils.generateSeed(mnemonic, walletSeed.passphrase ?: "") - return seed - } - private fun getSeedWithoutAuthentication(): Single = Single.fromCallable( {secureFileStorage.readOnFilesDirWithoutAuthentication(SEED_FILE_NAME)} ).map { json -> @@ -240,7 +234,9 @@ internal class WalletStorageImpl(private val secureFileStorage: SecureFileStorag } private fun generateAccountDID(walletSeed: Seed) : String { - val seed = getSeedBytes(walletSeed) + val mnemonic = MnemonicUtils.generateMnemonic(walletSeed.data) + + val seed = MnemonicUtils.generateSeed(mnemonic, walletSeed.passphrase ?: "") val masterKeypair = Bip32ECKeyPair.generateKeyPair(seed) val bip44Keypair = Bip32ECKeyPair.deriveKeyPair(masterKeypair, ACCOUNT_DERIVATION_PATH) val prefix: ByteArray = listOf(231, 1).map { it.toByte() }.toByteArray() @@ -268,8 +264,7 @@ internal class WalletStorageImpl(private val secureFileStorage: SecureFileStorag } }.onErrorResumeNext { error -> getSeed().map { seed -> - val seedByte = getSeedBytes(seed) - Bip32ECKeyPair.generateKeyPair(seedByte) + Bip32ECKeyPair.generateKeyPair(seed.data) }} .map { masterKeypair -> val bip44Keypair = Bip32ECKeyPair.deriveKeyPair(masterKeypair, ACCOUNT_DERIVATION_PATH) @@ -386,8 +381,9 @@ internal class WalletStorageImpl(private val secureFileStorage: SecureFileStorag } private fun generateEncryptKey(walletSeed: Seed): ByteArray { - val seedB = getSeedBytes(walletSeed) - val masterKeypair = Bip32ECKeyPair.generateKeyPair(seedB) + val mnemonic = MnemonicUtils.generateMnemonic(walletSeed.data) + val seed = MnemonicUtils.generateSeed(mnemonic, walletSeed.passphrase ?: "") + val masterKeypair = Bip32ECKeyPair.generateKeyPair(seed) val bip44Keypair = Bip32ECKeyPair.deriveKeyPair(masterKeypair, ENCRYPT_KEY_DERIVATION_PATH) return Numeric.toBytesPadded(bip44Keypair.privateKey, 32) } @@ -608,7 +604,8 @@ internal class WalletStorageImpl(private val secureFileStorage: SecureFileStorag } private fun generateETHCredentialWithIndex(seed: Seed, index: Int): Credentials { - val seedB = getSeedBytes(seed) + val mnemonic = MnemonicUtils.generateMnemonic(seed.data) + val seedB = MnemonicUtils.generateSeed(mnemonic, seed.passphrase ?: "") val masterKeypair = Bip32ECKeyPair.generateKeyPair(seedB) val path = intArrayOf( 44 or Bip32ECKeyPair.HARDENED_BIT, From 3096c23140d96e5629559727b6c2f7c512058633 Mon Sep 17 00:00:00 2001 From: PuPha Date: Wed, 7 Aug 2024 15:28:50 +0700 Subject: [PATCH 2/2] Revert "Merge pull request #13 from bitmark-inc/security_audit_biometric" This reverts commit aee87f20cd3e4b288ac0b3e93f9388382d641bdc, reversing changes made to 444e75bf67479213b7182a3fdb2371bb99caea43. --- .idea/compiler.xml | 2 +- .idea/gradle.xml | 2 +- .idea/misc.xml | 3 +- app/build.gradle | 2 +- libauk/build.gradle | 1 - .../libauk/storage/SecureFileStorageTest.kt | 10 +- .../java/com/bitmark/libauk/model/Keys.kt | 84 +--- .../libauk/storage/SecureFileStorage.kt | 109 +---- .../bitmark/libauk/storage/WalletStorage.kt | 462 ++++++------------ .../com/bitmark/libauk/util/BiometricUtil.kt | 80 --- .../com/bitmark/libauk/util/SecurityFile.kt | 47 -- .../libauk/storage/WalletStorageUnitTest.kt | 170 +++++-- 12 files changed, 323 insertions(+), 649 deletions(-) delete mode 100644 libauk/src/main/java/com/bitmark/libauk/util/BiometricUtil.kt delete mode 100644 libauk/src/main/java/com/bitmark/libauk/util/SecurityFile.kt diff --git a/.idea/compiler.xml b/.idea/compiler.xml index b589d56..fb7f4a8 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index cb236bc..75955c7 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -8,7 +8,7 @@