From 43a7df3838a935a11d513b78d4727705f9e25550 Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Sun, 31 Mar 2019 12:58:09 +0200 Subject: [PATCH] DeterministicKey: Decrypted deterministic keys must always be 32 bytes long, otherwise likely the encryption key was wrong. --- .../java/org/bitcoinj/crypto/DeterministicKey.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/crypto/DeterministicKey.java b/core/src/main/java/org/bitcoinj/crypto/DeterministicKey.java index ca5174df..fb059e05 100644 --- a/core/src/main/java/org/bitcoinj/crypto/DeterministicKey.java +++ b/core/src/main/java/org/bitcoinj/crypto/DeterministicKey.java @@ -391,8 +391,13 @@ public class DeterministicKey extends ECKey { // For when a key is encrypted, either decrypt our encrypted private key bytes, or work up the tree asking parents // to decrypt and re-derive. private BigInteger findOrDeriveEncryptedPrivateKey(KeyCrypter keyCrypter, KeyParameter aesKey) { - if (encryptedPrivateKey != null) - return new BigInteger(1, keyCrypter.decrypt(encryptedPrivateKey, aesKey)); + if (encryptedPrivateKey != null) { + byte[] decryptedKey = keyCrypter.decrypt(encryptedPrivateKey, aesKey); + if (decryptedKey.length != 32) + throw new KeyCrypterException.InvalidCipherText( + "Decrypted key must be 32 bytes long, but is " + decryptedKey.length); + return new BigInteger(1, decryptedKey); + } // Otherwise we don't have it, but maybe we can figure it out from our parents. Walk up the tree looking for // the first key that has some encrypted private key data. DeterministicKey cursor = parent; @@ -403,6 +408,9 @@ public class DeterministicKey extends ECKey { if (cursor == null) throw new KeyCrypterException("Neither this key nor its parents have an encrypted private key"); byte[] parentalPrivateKeyBytes = keyCrypter.decrypt(cursor.encryptedPrivateKey, aesKey); + if (parentalPrivateKeyBytes.length != 32) + throw new KeyCrypterException.InvalidCipherText( + "Decrypted key must be 32 bytes long, but is " + parentalPrivateKeyBytes.length); return derivePrivateKeyDownwards(cursor, parentalPrivateKeyBytes); }