tcrypto.pw_decode: fix one case of raising incorrect exception - electrum - Electrum Bitcoin wallet
HTML git clone https://git.parazyd.org/electrum
DIR Log
DIR Files
DIR Refs
DIR Submodules
---
DIR commit 1ea89af0129aa00aff8ed05c09d2712c275719bc
DIR parent 789b78cab5218630b0c8e1c16494611860f101d4
HTML Author: SomberNight <somber.night@protonmail.com>
Date: Wed, 8 Apr 2020 12:49:50 +0200
crypto.pw_decode: fix one case of raising incorrect exception
Diffstat:
M electrum/crypto.py | 5 ++++-
M electrum/tests/test_bitcoin.py | 5 +++++
2 files changed, 9 insertions(+), 1 deletion(-)
---
DIR diff --git a/electrum/crypto.py b/electrum/crypto.py
t@@ -229,7 +229,10 @@ def pw_decode(data: str, password: Union[bytes, str, None], *, version: int) ->
if password is None:
return data
plaintext_bytes = pw_decode_bytes(data, password, version=version)
- plaintext_str = to_string(plaintext_bytes, "utf8")
+ try:
+ plaintext_str = to_string(plaintext_bytes, "utf8")
+ except UnicodeDecodeError as e:
+ raise InvalidPassword() from e
return plaintext_str
DIR diff --git a/electrum/tests/test_bitcoin.py b/electrum/tests/test_bitcoin.py
t@@ -254,6 +254,11 @@ class Test_bitcoin(ElectrumTestCase):
enc = crypto.pw_encode(payload, password, version=version)
with self.assertRaises(InvalidPassword):
crypto.pw_decode(enc, wrong_password, version=version)
+ # sometimes the PKCS7 padding gets removed cleanly,
+ # but then UnicodeDecodeError gets raised (internally):
+ enc = 'smJ7j6ccr8LnMOlx98s/ajgikv9s3R1PQuG3GyyIMmo='
+ with self.assertRaises(InvalidPassword):
+ crypto.pw_decode(enc, wrong_password, version=1)
@needs_test_with_all_chacha20_implementations
def test_chacha20_poly1305_encrypt(self):