refactor keycrypters

This commit is contained in:
Craig Raw 2020-05-17 12:52:14 +02:00
parent 6b09dc0293
commit ae01fe9ee6
7 changed files with 20 additions and 13 deletions

2
drongo

@ -1 +1 @@
Subproject commit 312143cb611fefce4e75654266f90cfd3b37b09e
Subproject commit e20501d95422bb4ef76002cb7a42c46b856143d9

View file

@ -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 {

View file

@ -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;
}

View file

@ -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);
}

View file

@ -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)) {

View file

@ -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;

View file

@ -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());