Skip to content

Commit d218743

Browse files
committed
fix: SMP failure and re-smp verification errors
1 parent af3aa3b commit d218743

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

core/trad_crypto.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def derive_key_argon2id(password: bytes, salt: bytes = None, output_length: int
6767
), salt
6868

6969

70-
def encrypt_xchacha20poly1305(key: bytes, plaintext: bytes, nonce: bytes = None, counter: int = None, counter_safety: int = 2 ** 32) -> tuple[bytes, bytes]:
70+
def encrypt_xchacha20poly1305(key: bytes, plaintext: bytes, nonce: bytes = None, counter: int = None, counter_safety: int = 255) -> tuple[bytes, bytes]:
7171
"""
7272
Encrypt plaintext using ChaCha20Poly1305.
7373
@@ -90,7 +90,7 @@ def encrypt_xchacha20poly1305(key: bytes, plaintext: bytes, nonce: bytes = None,
9090
if counter > counter_safety:
9191
raise ValueError("ChaCha counter has overflowen")
9292

93-
nonce = nonce[:XCHACHA20POLY1305_NONCE_LEN - 4] + counter.to_bytes(4, "big")
93+
nonce = nonce[:XCHACHA20POLY1305_NONCE_LEN - 1] + counter.to_bytes(1, "big")
9494

9595
ciphertext = bindings.crypto_aead_xchacha20poly1305_ietf_encrypt(plaintext, None, nonce, key)
9696

logic/background_worker.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,10 @@ def background_worker(user_data, user_data_lock, ui_queue, stop_flag):
137137
blob_plaintext = decrypt_xchacha20poly1305(chacha_key, contact_next_strand_nonce, blob)
138138
except Exception as e:
139139
logger.error(
140-
"Impossible error: Failed to decrypt blob from contact (%s)"
141-
"We dont know what caused this, maybe the contact is trying to denial-of-service you"
142-
". Skipping data because of error: %s", sender, str(e)
140+
"Failed to decrypt blob from contact (%s)"
141+
"We dont know what caused this except maybe a re-SMP verification. error: %s", sender, str(e)
143142
)
144-
continue
143+
blob_plaintext = blob
145144
else:
146145
logger.debug("Contact (%s) not saved.. we just gonna assume blob_plaintext = blob", sender)
147146
blob_plaintext = blob

logic/pfs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def pfs_data_handler(user_data: dict, user_data_lock: threading.Lock, user_data_
203203
user_data_lock (threading.Lock): Lock protecting shared state.
204204
user_data_copied (dict): A read-only copy of user_data for consistency.
205205
ui_queue (queue.Queue): UI queue for notifications/errors.
206-
message (dict): Incoming PFS message from the server.
206+
pfs_plaintext (bytes): Decrypted Incoming PFS plaintext from the server.
207207
208208
Returns:
209209
None

logic/smp.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,25 @@ def smp_failure_notify_contact(user_data, user_data_lock, contact_id, ui_queue)
483483
server_url = user_data["server_url"]
484484
auth_token = user_data["token"]
485485

486+
tmp_key = b64decode(user_data["contacts"][contact_id]["lt_sign_key_smp"]["tmp_key"])
487+
486488
smp_failure(user_data, user_data_lock, contact_id, ui_queue)
487489

490+
# it can be any number other than 2, we chose 7 because failure is *technically* the 7th smp step.
491+
ciphertext_nonce, ciphertext_blob = encrypt_xchacha20poly1305(
492+
tmp_key,
493+
SMP_TYPE + b"failure",
494+
counter = 7
495+
)
488496
try:
489-
http_request(f"{server_url}/smp/failure", "POST", payload = {"recipient": contact_id}, auth_token=auth_token)
490-
except Exception:
491-
logger.error("Failed to send SMP failure to server, either you are offline or the server is down")
497+
http_request(f"{server_url}/data/send", "POST", metadata = {
498+
"recipient": contact_id
499+
},
500+
blob = ciphertext_nonce + ciphertext_blob,
501+
auth_token = auth_token
502+
)
503+
except Exception as e:
504+
logger.error("Failed to send SMP failure to contact (%s), either you are offline or the server is down. Error: %s", contact_id, str(e))
492505
pass
493506

494507

@@ -516,12 +529,12 @@ def smp_data_handler(user_data, user_data_lock, user_data_copied, ui_queue, cont
516529

517530

518531

519-
"""
520-
if "failure" in message:
532+
533+
if message == b"failure":
521534
# Delete SMP state for contact
522535
smp_failure(user_data, user_data_lock, contact_id, ui_queue)
523536
return
524-
"""
537+
525538

526539
# Check if we don't have this contact saved
527540
if contact_id not in user_data_copied["contacts"]:

0 commit comments

Comments
 (0)