From ae01fe9ee68283fbdb738b2f44fff44d18858d1f Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Sun, 17 May 2020 12:52:14 +0200 Subject: [PATCH] refactor keycrypters --- drongo | 2 +- .../com/sparrowwallet/sparrow/AppController.java | 2 +- .../sparrowwallet/sparrow/io/ECIESInputStream.java | 2 +- .../java/com/sparrowwallet/sparrow/io/Electrum.java | 12 +++++++++--- .../sparrow/wallet/SettingsController.java | 4 ++-- .../com/sparrowwallet/sparrow/wallet/WalletForm.java | 4 ++-- .../com/sparrowwallet/sparrow/io/StorageTest.java | 7 ++++--- 7 files changed, 20 insertions(+), 13 deletions(-) diff --git a/drongo b/drongo index 312143cb..e20501d9 160000 --- a/drongo +++ b/drongo @@ -1 +1 @@ -Subproject commit 312143cb611fefce4e75654266f90cfd3b37b09e +Subproject commit e20501d95422bb4ef76002cb7a42c46b856143d9 diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index 8888e0a5..bfc13981 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -247,7 +247,7 @@ public class AppController implements Initializable { } password = optionalPassword.get(); - ECKey encryptionFullKey = ECIESKeyCrypter.deriveECKey(password); + ECKey encryptionFullKey = Pbkdf2KeyDeriver.DEFAULT_INSTANCE.deriveECKey(password); wallet = Storage.getStorage().loadWallet(file, encryptionFullKey); encryptionPubKey = ECKey.fromPublicOnly(encryptionFullKey); } else { diff --git a/src/main/java/com/sparrowwallet/sparrow/io/ECIESInputStream.java b/src/main/java/com/sparrowwallet/sparrow/io/ECIESInputStream.java index 082d940a..c152ddab 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/ECIESInputStream.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/ECIESInputStream.java @@ -43,7 +43,7 @@ public class ECIESInputStream extends FilterInputStream { byte[] encryptedBytes = ByteStreams.toByteArray(in); in.close(); ECIESKeyCrypter keyCrypter = new ECIESKeyCrypter(); - byte[] decryptedBytes = keyCrypter.decrypt(new EncryptedData(encryptionMagic, encryptedBytes, null), decryptionKey); + byte[] decryptedBytes = keyCrypter.decrypt(new EncryptedData(encryptionMagic, encryptedBytes, null, null), decryptionKey); in = new ByteArrayInputStream(decryptedBytes); decrypted = true; } diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Electrum.java b/src/main/java/com/sparrowwallet/sparrow/io/Electrum.java index 4f6fb9da..cb763a94 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Electrum.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Electrum.java @@ -56,7 +56,7 @@ public class Electrum implements KeystoreFileImport, WalletImport, WalletExport public Wallet importWallet(InputStream inputStream, String password) throws ImportException { Reader reader; if(password != null) { - ECKey decryptionKey = ECIESKeyCrypter.deriveECKey(password); + ECKey decryptionKey = Pbkdf2KeyDeriver.DEFAULT_INSTANCE.deriveECKey(password); reader = new InputStreamReader(new InflaterInputStream(new ECIESInputStream(inputStream, decryptionKey))); } else { reader = new InputStreamReader(inputStream); @@ -163,9 +163,15 @@ public class Electrum implements KeystoreFileImport, WalletImport, WalletExport } private String decrypt(String encrypted, String password) { - byte[] passwordHash = Utils.sha256sha256(password.getBytes(StandardCharsets.UTF_8)); + KeyDeriver keyDeriver = new DoubleSha256KeyDeriver(); + Key key = keyDeriver.deriveKey(password); byte[] encryptedBytes = Base64.getDecoder().decode(encrypted); - byte[] decrypted = Utils.decryptAesCbcPkcs7(Arrays.copyOfRange(encryptedBytes, 0, 16), Arrays.copyOfRange(encryptedBytes, 16, encryptedBytes.length), passwordHash); + + KeyCrypter keyCrypter = new AESKeyCrypter(); + byte[] initializationVector = Arrays.copyOfRange(encryptedBytes, 0, 16); + byte[] cipher = Arrays.copyOfRange(encryptedBytes, 16, encryptedBytes.length); + EncryptedData data = new EncryptedData(initializationVector, cipher, null, keyDeriver.getDeriverType(), keyCrypter.getCrypterType()); + byte[] decrypted = keyCrypter.decrypt(data, key); return new String(decrypted, StandardCharsets.UTF_8); } diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java index 9ee4d041..5cff1f6b 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java @@ -1,8 +1,8 @@ package com.sparrowwallet.sparrow.wallet; import com.google.common.eventbus.Subscribe; -import com.sparrowwallet.drongo.crypto.ECIESKeyCrypter; import com.sparrowwallet.drongo.crypto.ECKey; +import com.sparrowwallet.drongo.crypto.Pbkdf2KeyDeriver; import com.sparrowwallet.drongo.policy.Policy; import com.sparrowwallet.drongo.policy.PolicyType; import com.sparrowwallet.drongo.protocol.ScriptType; @@ -273,7 +273,7 @@ public class SettingsController extends WalletFormController implements Initiali return Optional.of(WalletForm.NO_PASSWORD_KEY); } - ECKey encryptionFullKey = ECIESKeyCrypter.deriveECKey(password.get()); + ECKey encryptionFullKey = Pbkdf2KeyDeriver.DEFAULT_INSTANCE.deriveECKey(password.get()); ECKey encryptionPubKey = ECKey.fromPublicOnly(encryptionFullKey); if(existingPubKey != null && !WalletForm.NO_PASSWORD_KEY.equals(existingPubKey) && !existingPubKey.equals(encryptionPubKey)) { diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java index 4d7c5616..16da437f 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java @@ -1,7 +1,7 @@ package com.sparrowwallet.sparrow.wallet; -import com.sparrowwallet.drongo.crypto.ECIESKeyCrypter; import com.sparrowwallet.drongo.crypto.ECKey; +import com.sparrowwallet.drongo.crypto.Pbkdf2KeyDeriver; import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.sparrow.io.Storage; @@ -9,7 +9,7 @@ import java.io.File; import java.io.IOException; public class WalletForm { - public static final ECKey NO_PASSWORD_KEY = ECKey.fromPublicOnly(ECIESKeyCrypter.deriveECKey("")); + public static final ECKey NO_PASSWORD_KEY = ECKey.fromPublicOnly(Pbkdf2KeyDeriver.DEFAULT_INSTANCE.deriveECKey("")); private final File walletFile; private ECKey encryptionPubKey; diff --git a/src/test/java/com/sparrowwallet/sparrow/io/StorageTest.java b/src/test/java/com/sparrowwallet/sparrow/io/StorageTest.java index 6bf43ab0..1b3c0302 100644 --- a/src/test/java/com/sparrowwallet/sparrow/io/StorageTest.java +++ b/src/test/java/com/sparrowwallet/sparrow/io/StorageTest.java @@ -3,6 +3,7 @@ package com.sparrowwallet.sparrow.io; import com.sparrowwallet.drongo.Utils; import com.sparrowwallet.drongo.crypto.ECIESKeyCrypter; import com.sparrowwallet.drongo.crypto.ECKey; +import com.sparrowwallet.drongo.crypto.Pbkdf2KeyDeriver; import com.sparrowwallet.drongo.policy.PolicyType; import com.sparrowwallet.drongo.protocol.ScriptType; import com.sparrowwallet.drongo.wallet.MnemonicException; @@ -15,14 +16,14 @@ import java.io.*; public class StorageTest extends IoTest { @Test public void loadWallet() throws IOException { - ECKey decryptionKey = ECIESKeyCrypter.deriveECKey("pass"); + ECKey decryptionKey = Pbkdf2KeyDeriver.DEFAULT_INSTANCE.deriveECKey("pass"); Wallet wallet = Storage.getStorage().loadWallet(getFile("sparrow-single-wallet"), decryptionKey); Assert.assertTrue(wallet.isValid()); } @Test public void loadSeedWallet() throws IOException, MnemonicException { - ECKey decryptionKey = ECIESKeyCrypter.deriveECKey("pass"); + ECKey decryptionKey = Pbkdf2KeyDeriver.DEFAULT_INSTANCE.deriveECKey("pass"); Wallet wallet = Storage.getStorage().loadWallet(getFile("sparrow-single-seed-wallet"), decryptionKey); Assert.assertTrue(wallet.isValid()); @@ -41,7 +42,7 @@ public class StorageTest extends IoTest { @Test public void saveWallet() throws IOException { - ECKey decryptionKey = ECIESKeyCrypter.deriveECKey("pass"); + ECKey decryptionKey = Pbkdf2KeyDeriver.DEFAULT_INSTANCE.deriveECKey("pass"); Wallet wallet = Storage.getStorage().loadWallet(getFile("sparrow-single-wallet"), decryptionKey); Assert.assertTrue(wallet.isValid());