preserve id when encrypting or decrypting seeds or private keys

This commit is contained in:
Craig Raw 2021-06-14 14:53:53 +02:00
parent 485e8c825b
commit f407547c47
2 changed files with 13 additions and 4 deletions

View file

@ -184,6 +184,7 @@ public class DeterministicSeed extends Persistable implements EncryptableItem {
Arrays.fill(mnemonicBytes != null ? mnemonicBytes : new byte[0], (byte)0); Arrays.fill(mnemonicBytes != null ? mnemonicBytes : new byte[0], (byte)0);
DeterministicSeed seed = new DeterministicSeed(encryptedMnemonic, needsPassphrase, creationTimeSeconds, type); DeterministicSeed seed = new DeterministicSeed(encryptedMnemonic, needsPassphrase, creationTimeSeconds, type);
seed.setId(getId());
seed.setPassphrase(passphrase); seed.setPassphrase(passphrase);
return seed; return seed;
@ -209,6 +210,7 @@ public class DeterministicSeed extends Persistable implements EncryptableItem {
KeyDeriver keyDeriver = getEncryptionType().getDeriver().getKeyDeriver(encryptedMnemonicCode.getKeySalt()); KeyDeriver keyDeriver = getEncryptionType().getDeriver().getKeyDeriver(encryptedMnemonicCode.getKeySalt());
Key key = keyDeriver.deriveKey(password); Key key = keyDeriver.deriveKey(password);
DeterministicSeed seed = decrypt(key); DeterministicSeed seed = decrypt(key);
seed.setId(getId());
key.clear(); key.clear();
return seed; return seed;
@ -225,6 +227,7 @@ public class DeterministicSeed extends Persistable implements EncryptableItem {
Arrays.fill(decrypted, (byte)0); Arrays.fill(decrypted, (byte)0);
DeterministicSeed seed = new DeterministicSeed(mnemonic, needsPassphrase, creationTimeSeconds, type); DeterministicSeed seed = new DeterministicSeed(mnemonic, needsPassphrase, creationTimeSeconds, type);
seed.setId(getId());
seed.setPassphrase(passphrase); seed.setPassphrase(passphrase);
return seed; return seed;

View file

@ -81,7 +81,10 @@ public class MasterPrivateExtendedKey extends Persistable implements Encryptable
EncryptedData encryptedKeyData = keyCrypter.encrypt(secretBytes, null, key); EncryptedData encryptedKeyData = keyCrypter.encrypt(secretBytes, null, key);
Arrays.fill(secretBytes != null ? secretBytes : new byte[0], (byte)0); Arrays.fill(secretBytes != null ? secretBytes : new byte[0], (byte)0);
return new MasterPrivateExtendedKey(encryptedKeyData); MasterPrivateExtendedKey mpek = new MasterPrivateExtendedKey(encryptedKeyData);
mpek.setId(getId());
return mpek;
} }
public MasterPrivateExtendedKey decrypt(CharSequence password) { public MasterPrivateExtendedKey decrypt(CharSequence password) {
@ -91,10 +94,11 @@ public class MasterPrivateExtendedKey extends Persistable implements Encryptable
KeyDeriver keyDeriver = getEncryptionType().getDeriver().getKeyDeriver(encryptedKey.getKeySalt()); KeyDeriver keyDeriver = getEncryptionType().getDeriver().getKeyDeriver(encryptedKey.getKeySalt());
Key key = keyDeriver.deriveKey(password); Key key = keyDeriver.deriveKey(password);
MasterPrivateExtendedKey seed = decrypt(key); MasterPrivateExtendedKey mpek = decrypt(key);
mpek.setId(getId());
key.clear(); key.clear();
return seed; return mpek;
} }
public MasterPrivateExtendedKey decrypt(Key key) { public MasterPrivateExtendedKey decrypt(Key key) {
@ -105,7 +109,9 @@ public class MasterPrivateExtendedKey extends Persistable implements Encryptable
KeyCrypter keyCrypter = getEncryptionType().getCrypter().getKeyCrypter(); KeyCrypter keyCrypter = getEncryptionType().getCrypter().getKeyCrypter();
byte[] decrypted = keyCrypter.decrypt(encryptedKey, key); byte[] decrypted = keyCrypter.decrypt(encryptedKey, key);
try { try {
return new MasterPrivateExtendedKey(Arrays.copyOfRange(decrypted, 0, 32), Arrays.copyOfRange(decrypted, 32, 64)); MasterPrivateExtendedKey mpek = new MasterPrivateExtendedKey(Arrays.copyOfRange(decrypted, 0, 32), Arrays.copyOfRange(decrypted, 32, 64));
mpek.setId(getId());
return mpek;
} finally { } finally {
Arrays.fill(decrypted, (byte)0); Arrays.fill(decrypted, (byte)0);
} }