keycrypter changes

This commit is contained in:
Craig Raw
2020-05-11 17:40:19 +02:00
parent 9241f4381f
commit d7c6f5d587
13 changed files with 46 additions and 16 deletions
@@ -1,7 +1,7 @@
package com.sparrowwallet.sparrow.control;
import com.google.gson.JsonParseException;
import com.sparrowwallet.drongo.crypto.ECKey;
import com.sparrowwallet.drongo.crypto.ECIESKeyCrypter;
import com.sparrowwallet.drongo.wallet.Keystore;
import com.sparrowwallet.drongo.wallet.Wallet;
import com.sparrowwallet.sparrow.EventManager;
@@ -83,7 +83,7 @@ public class FileKeystoreImportPane extends KeystoreImportPane {
if(e.getCause() != null && e.getCause().getMessage() != null && !e.getCause().getMessage().isEmpty()) {
errorMessage = e.getCause().getMessage();
}
if(e instanceof ECKey.InvalidPasswordException || e.getCause() instanceof ECKey.InvalidPasswordException) {
if(e instanceof ECIESKeyCrypter.InvalidPasswordException || e.getCause() instanceof ECIESKeyCrypter.InvalidPasswordException) {
errorMessage = "Invalid wallet password";
}
if(e instanceof JsonParseException || e.getCause() instanceof JsonParseException) {
@@ -2,6 +2,7 @@ package com.sparrowwallet.sparrow.io;
import com.sparrowwallet.drongo.crypto.ChildNumber;
import com.sparrowwallet.drongo.wallet.Bip39MnemonicCode;
import com.sparrowwallet.drongo.wallet.DeterministicSeed;
import com.sparrowwallet.drongo.wallet.Keystore;
import com.sparrowwallet.drongo.wallet.WalletModel;
@@ -27,7 +28,7 @@ public class Bip39 implements KeystoreMnemonicImport {
public Keystore getKeystore(List<ChildNumber> derivation, List<String> mnemonicWords, String passphrase) throws ImportException {
try {
Bip39MnemonicCode.INSTANCE.check(mnemonicWords);
byte[] seed = Bip39MnemonicCode.toSeed(mnemonicWords, passphrase);
DeterministicSeed seed = new DeterministicSeed(mnemonicWords, null, passphrase, System.currentTimeMillis());
return Keystore.fromSeed(seed, derivation);
} catch (Exception e) {
throw new ImportException(e);
@@ -1,7 +1,9 @@
package com.sparrowwallet.sparrow.io;
import com.google.common.io.ByteStreams;
import com.sparrowwallet.drongo.crypto.ECIESKeyCrypter;
import com.sparrowwallet.drongo.crypto.ECKey;
import com.sparrowwallet.drongo.crypto.EncryptedData;
import java.io.*;
import java.nio.charset.StandardCharsets;
@@ -38,9 +40,11 @@ public class ECIESInputStream extends FilterInputStream {
private synchronized void ensureDecrypted() throws IOException {
if(!decrypted) {
byte[] encrypted = ByteStreams.toByteArray(in);
byte[] encryptedBytes = ByteStreams.toByteArray(in);
in.close();
in = new ByteArrayInputStream(decryptionKey.decryptEcies(encrypted, encryptionMagic));
ECIESKeyCrypter keyCrypter = new ECIESKeyCrypter();
byte[] decryptedBytes = keyCrypter.decrypt(new EncryptedData(encryptionMagic, encryptedBytes), decryptionKey);
in = new ByteArrayInputStream(decryptedBytes);
decrypted = true;
}
}
@@ -1,7 +1,9 @@
package com.sparrowwallet.sparrow.io;
import com.google.common.io.ByteStreams;
import com.sparrowwallet.drongo.crypto.ECIESKeyCrypter;
import com.sparrowwallet.drongo.crypto.ECKey;
import com.sparrowwallet.drongo.crypto.EncryptedData;
import java.io.*;
import java.nio.charset.StandardCharsets;
@@ -32,8 +34,9 @@ public class ECIESOutputStream extends FilterOutputStream {
public void close() throws IOException {
super.close();
byte[] unencrypted = ((ByteArrayOutputStream)out).toByteArray();
byte[] encryptedWallet = encryptionKey.encryptEcies(unencrypted, encryptionMagic);
ByteStreams.copy(new ByteArrayInputStream(encryptedWallet), encryptedStream);
ECIESKeyCrypter keyCrypter = new ECIESKeyCrypter();
EncryptedData encryptedData = keyCrypter.encrypt(unencrypted, encryptionMagic, encryptionKey);
ByteStreams.copy(new ByteArrayInputStream(encryptedData.getEncryptedBytes()), encryptedStream);
encryptedStream.flush();
encryptedStream.close();
}
@@ -5,6 +5,7 @@ import com.google.gson.reflect.TypeToken;
import com.sparrowwallet.drongo.ExtendedKey;
import com.sparrowwallet.drongo.KeyDerivation;
import com.sparrowwallet.drongo.Utils;
import com.sparrowwallet.drongo.crypto.ECIESKeyCrypter;
import com.sparrowwallet.drongo.crypto.ECKey;
import com.sparrowwallet.drongo.policy.Policy;
import com.sparrowwallet.drongo.policy.PolicyType;
@@ -57,7 +58,7 @@ public class Electrum implements KeystoreFileImport, SinglesigWalletImport, Mult
public Wallet importWallet(InputStream inputStream, String password) throws ImportException {
Reader reader;
if(password != null) {
ECKey decryptionKey = ECKey.createKeyPbkdf2HmacSha512(password);
ECKey decryptionKey = ECIESKeyCrypter.deriveECKey(password);
reader = new InputStreamReader(new InflaterInputStream(new ECIESInputStream(inputStream, decryptionKey)));
} else {
reader = new InputStreamReader(inputStream);
@@ -79,7 +79,7 @@ public class Storage {
}
private static byte[] getEncryptionMagic() {
return "SPRW1".getBytes(StandardCharsets.UTF_8);
return "BIE1".getBytes(StandardCharsets.UTF_8);
}
public File getWalletFile(String walletName) {
@@ -1,6 +1,7 @@
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.policy.Policy;
import com.sparrowwallet.drongo.policy.PolicyType;
@@ -279,7 +280,7 @@ public class SettingsController extends WalletFormController implements Initiali
return Optional.of(WalletForm.NO_PASSWORD_KEY);
}
ECKey encryptionFullKey = ECKey.createKeyPbkdf2HmacSha512(password.get());
ECKey encryptionFullKey = ECIESKeyCrypter.deriveECKey(password.get());
ECKey encryptionPubKey = ECKey.fromPublicOnly(encryptionFullKey);
if(existingPubKey != null) {
if(WalletForm.NO_PASSWORD_KEY.equals(existingPubKey) || existingPubKey.equals(encryptionPubKey)) {
@@ -1,5 +1,6 @@
package com.sparrowwallet.sparrow.wallet;
import com.sparrowwallet.drongo.crypto.ECIESKeyCrypter;
import com.sparrowwallet.drongo.crypto.ECKey;
import com.sparrowwallet.drongo.wallet.Wallet;
import com.sparrowwallet.sparrow.io.Storage;
@@ -8,7 +9,7 @@ import java.io.File;
import java.io.IOException;
public class WalletForm {
public static final ECKey NO_PASSWORD_KEY = ECKey.fromPublicOnly(ECKey.createKeyPbkdf2HmacSha512(""));
public static final ECKey NO_PASSWORD_KEY = ECKey.fromPublicOnly(ECIESKeyCrypter.deriveECKey(""));
private File walletFile;
private ECKey encryptionPubKey;