From 5a437fb0c60b768b8030d6db0046f519cc5371bd Mon Sep 17 00:00:00 2001 From: monty Date: Wed, 5 Jun 2024 12:18:50 +0100 Subject: [PATCH] made decrypt exception more specific --- my_scratch.py | 6 +++--- src/monstr/encrypt.py | 13 ++++++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/my_scratch.py b/my_scratch.py index 7a2dfa8..955151a 100644 --- a/my_scratch.py +++ b/my_scratch.py @@ -21,8 +21,8 @@ async def get_key() -> str: return input('keystore key: ') my_enc = KeyDataEncrypter(get_key=get_key) - new_store = FileKeyStore(new_file, - encrypter=my_enc) + new_store = SQLiteKeyStore(new_file, + encrypter=None) await new_store.convert_memstore(old_file) @@ -50,7 +50,7 @@ async def get_key() -> str: # from monstr.encrypt import Keys # await new_store.update(Keys(), 'monty_test') -asyncio.run(test_store()) +asyncio.run(convert_store()) diff --git a/src/monstr/encrypt.py b/src/monstr/encrypt.py index 9c264c6..e392990 100644 --- a/src/monstr/encrypt.py +++ b/src/monstr/encrypt.py @@ -268,6 +268,9 @@ def decrypt_event(self, evt: Event) -> Event: return ret +class DecryptionException(Exception): + pass + class NIP4Encrypt(Encrypter): def __init__(self, key: Keys | str): @@ -434,13 +437,13 @@ def _decode_payload(payload) -> tuple[bytes, bytes, bytes]: # TODO: size limits should be being calculated from MIN/MAX PAD if p_size < 132 or p_size > 87472: - raise Exception(f'invalid payload size {p_size}') + raise DecryptionException(f'invalid payload size {p_size}') data = base64.b64decode(payload) d_size = len(data) if d_size < 99 or d_size > 65603: - raise Exception(f'invalid payload size {p_size}') + raise DecryptionException(f'invalid payload size {p_size}') version = data[0] nonce = data[1:33] @@ -449,7 +452,7 @@ def _decode_payload(payload) -> tuple[bytes, bytes, bytes]: # only current/supported version if version != 2: - raise ValueError(f'nip44_encrypt unsupported version {version}') + raise DecryptionException(f'nip44_encrypt unsupported version {version}') return nonce, cipher_text, mac @@ -467,7 +470,7 @@ def _get_conversation_key(self, for_pub_k: str) -> bytes: def _get_message_key(conversion_key: bytes, nonce: bytes) -> tuple[bytes, bytes, bytes]: if len(nonce) != 32: - raise ValueError('NIP44Encrypt:: _get_message_key nonce is not 32 bytes long') + raise DecryptionException('NIP44Encrypt:: _get_message_key nonce is not 32 bytes long') msg_key = NIP44Encrypt._hkdf_expand(prk=conversion_key, info=nonce, @@ -536,7 +539,7 @@ def decrypt(self, payload: str, for_pub_k: str) -> str: hash_function=NIP44Encrypt.V2_HASH) if calculated_mac != mac: - raise ValueError('invalid MAC') + raise DecryptionException('invalid MAC') padded = NIP44Encrypt._do_decrypt(ciper_text=ciper_text, key=chacha_key,