From c119ef427335ff12a975f063ae1b97236391c683 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Sun, 8 Sep 2024 20:33:35 +0200 Subject: [PATCH] Standardised ratchet id getter --- RNS/Destination.py | 3 ++- RNS/Identity.py | 10 +++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/RNS/Destination.py b/RNS/Destination.py index deda821..f0dd761 100755 --- a/RNS/Destination.py +++ b/RNS/Destination.py @@ -269,6 +269,7 @@ class Destination: if self.ratchets != None: self.rotate_ratchets() ratchet = RNS.Identity._ratchet_public_bytes(self.ratchets[0]) + RNS.Identity._remember_ratchet(self.hash, ratchet) if app_data == None and self.default_app_data != None: if isinstance(self.default_app_data, bytes): @@ -573,7 +574,7 @@ class Destination: if self.type == Destination.SINGLE and self.identity != None: selected_ratchet = RNS.Identity.get_ratchet(self.hash) if selected_ratchet: - self.latest_ratchet_id = RNS.Identity.truncated_hash(selected_ratchet) + self.latest_ratchet_id = RNS.Identity._get_ratchet_id(selected_ratchet) return self.identity.encrypt(plaintext, ratchet=selected_ratchet) if self.type == Destination.GROUP: diff --git a/RNS/Identity.py b/RNS/Identity.py index 73070fc..dbf6841 100644 --- a/RNS/Identity.py +++ b/RNS/Identity.py @@ -249,7 +249,11 @@ class Identity: if ratchet == None: return None else: - return Identity.truncated_hash(ratchet) + return Identity._get_ratchet_id(ratchet) + + @staticmethod + def _get_ratchet_id(ratchet_pub_bytes): + return Identity.full_hash(ratchet_pub_bytes)[:Identity.NAME_HASH_LENGTH//8] @staticmethod def _ratchet_public_bytes(ratchet): @@ -264,7 +268,7 @@ class Identity: @staticmethod def _remember_ratchet(destination_hash, ratchet): # TODO: Remove at some point, and only log new ratchets - RNS.log(f"Remembering ratchet {RNS.prettyhexrep(Identity.truncated_hash(ratchet))} for {RNS.prettyhexrep(destination_hash)}", RNS.LOG_EXTREME) + RNS.log(f"Remembering ratchet {RNS.prettyhexrep(Identity._get_ratchet_id(ratchet))} for {RNS.prettyhexrep(destination_hash)}", RNS.LOG_EXTREME) try: Identity.known_ratchets[destination_hash] = ratchet @@ -673,7 +677,7 @@ class Identity: for ratchet in ratchets: try: ratchet_prv = X25519PrivateKey.from_private_bytes(ratchet) - ratchet_id = Identity.truncated_hash(ratchet_prv.public_key().public_bytes()) + ratchet_id = Identity._get_ratchet_id(ratchet_prv.public_key().public_bytes()) shared_key = ratchet_prv.exchange(peer_pub) derived_key = RNS.Cryptography.hkdf( length=32,