From cb55189e5cfba2c2478170206b2563293cd7cae2 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Thu, 6 Oct 2022 23:14:32 +0200 Subject: [PATCH] Truncate name_hash to 80 bits. Take all array slices from Identity.NAME_HASH_LENGTH constant. --- RNS/Destination.py | 6 ++---- RNS/Identity.py | 13 +++++++------ RNS/Transport.py | 4 ++-- tests/link.py | 14 +++++++------- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/RNS/Destination.py b/RNS/Destination.py index 190a4f4..ec8d0ca 100755 --- a/RNS/Destination.py +++ b/RNS/Destination.py @@ -94,7 +94,7 @@ class Destination: """ :returns: A destination name in adressable hash form, for an app_name and a number of aspects. """ - name_hash = RNS.Identity.full_hash(Destination.expand_name(None, app_name, *aspects).encode("utf-8")) + name_hash = RNS.Identity.full_hash(Destination.expand_name(None, app_name, *aspects).encode("utf-8"))[:(RNS.Identity.NAME_HASH_LENGTH//8)] addr_hash_material = name_hash if identity != None: addr_hash_material += identity.hash @@ -146,7 +146,7 @@ class Destination: # Generate the destination address hash self.hash = Destination.hash(self.identity, app_name, *aspects) - self.name_hash = RNS.Identity.full_hash(self.expand_name(None, app_name, *aspects).encode("utf-8")) + self.name_hash = RNS.Identity.full_hash(self.expand_name(None, app_name, *aspects).encode("utf-8"))[:(RNS.Identity.NAME_HASH_LENGTH//8)] self.hexhash = self.hash.hex() self.default_app_data = None @@ -185,8 +185,6 @@ class Destination: if isinstance(returned_app_data, bytes): app_data = returned_app_data - # TODO: It is probably possible truncate the name_hash to 16 bytes to - # save bandwidth without any practical impact on collision resistance signed_data = self.hash+self.identity.get_public_key()+self.name_hash+random_hash if app_data != None: signed_data += app_data diff --git a/RNS/Identity.py b/RNS/Identity.py index d7a35a4..105759f 100644 --- a/RNS/Identity.py +++ b/RNS/Identity.py @@ -58,6 +58,7 @@ class Identity: HASHLENGTH = 256 # In bits SIGLENGTH = KEYSIZE # In bits + NAME_HASH_LENGTH = 80 TRUNCATED_HASHLENGTH = RNS.Reticulum.TRUNCATED_HASHLENGTH """ Constant specifying the truncated hash length (in bits) used by Reticulum @@ -214,16 +215,16 @@ class Identity: if packet.packet_type == RNS.Packet.ANNOUNCE: destination_hash = packet.destination_hash public_key = packet.data[:Identity.KEYSIZE//8] - name_hash = packet.data[Identity.KEYSIZE//8:Identity.KEYSIZE//8+Identity.HASHLENGTH//8] - random_hash = packet.data[Identity.KEYSIZE//8+Identity.HASHLENGTH//8:Identity.KEYSIZE//8+Identity.HASHLENGTH//8+10] - signature = packet.data[Identity.KEYSIZE//8+Identity.HASHLENGTH//8+10:Identity.KEYSIZE//8+Identity.HASHLENGTH//8+10+Identity.SIGLENGTH//8] + name_hash = packet.data[Identity.KEYSIZE//8:Identity.KEYSIZE//8+Identity.NAME_HASH_LENGTH//8] + random_hash = packet.data[Identity.KEYSIZE//8+Identity.NAME_HASH_LENGTH//8:Identity.KEYSIZE//8+Identity.NAME_HASH_LENGTH//8+10] + signature = packet.data[Identity.KEYSIZE//8+Identity.NAME_HASH_LENGTH//8+10:Identity.KEYSIZE//8+Identity.NAME_HASH_LENGTH//8+10+Identity.SIGLENGTH//8] app_data = b"" - if len(packet.data) > Identity.KEYSIZE//8+Identity.HASHLENGTH//8+10+Identity.SIGLENGTH//8: - app_data = packet.data[Identity.KEYSIZE//8+Identity.HASHLENGTH//8+10+Identity.SIGLENGTH//8:] + if len(packet.data) > Identity.KEYSIZE//8+Identity.NAME_HASH_LENGTH//8+10+Identity.SIGLENGTH//8: + app_data = packet.data[Identity.KEYSIZE//8+Identity.NAME_HASH_LENGTH//8+10+Identity.SIGLENGTH//8:] signed_data = destination_hash+public_key+name_hash+random_hash+app_data - if not len(packet.data) > Identity.KEYSIZE//8+Identity.HASHLENGTH//8+10+Identity.SIGLENGTH//8: + if not len(packet.data) > Identity.KEYSIZE//8+Identity.NAME_HASH_LENGTH//8+10+Identity.SIGLENGTH//8: app_data = None announced_identity = Identity(create_keys=False) diff --git a/RNS/Transport.py b/RNS/Transport.py index cd61275..96368b8 100755 --- a/RNS/Transport.py +++ b/RNS/Transport.py @@ -1084,7 +1084,7 @@ class Transport: if (not any(packet.destination_hash == d.hash for d in Transport.destinations) and packet.hops < Transport.PATHFINDER_M+1): announce_emitted = Transport.announce_emitted(packet) - random_blob = packet.data[RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8:RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8+10] + random_blob = packet.data[RNS.Identity.KEYSIZE//8+RNS.Identity.NAME_HASH_LENGTH//8:RNS.Identity.KEYSIZE//8+RNS.Identity.NAME_HASH_LENGTH//8+10] random_blobs = [] if packet.destination_hash in Transport.destination_table: random_blobs = Transport.destination_table[packet.destination_hash][4] @@ -2054,7 +2054,7 @@ class Transport: @staticmethod def announce_emitted(packet): - random_blob = packet.data[RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8:RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8+10] + random_blob = packet.data[RNS.Identity.KEYSIZE//8+RNS.Identity.NAME_HASH_LENGTH//8:RNS.Identity.KEYSIZE//8+RNS.Identity.NAME_HASH_LENGTH//8+10] announce_emitted = int.from_bytes(random_blob[5:10], "big") return announce_emitted diff --git a/tests/link.py b/tests/link.py index 9a7641d..5368858 100644 --- a/tests/link.py +++ b/tests/link.py @@ -89,7 +89,7 @@ class TestLink(unittest.TestCase): dest = RNS.Destination(id1, RNS.Destination.OUT, RNS.Destination.SINGLE, APP_NAME, "link", "establish") - self.assertEqual(dest.hash, bytes.fromhex("46238cb662b2fc7342de77d7c84abb5c")) + self.assertEqual(dest.hash, bytes.fromhex("fb48da0e82e6e01ba0c014513f74540d")) l1 = RNS.Link(dest) time.sleep(0.5) @@ -109,7 +109,7 @@ class TestLink(unittest.TestCase): dest = RNS.Destination(id1, RNS.Destination.OUT, RNS.Destination.SINGLE, APP_NAME, "link", "establish") - self.assertEqual(dest.hash, bytes.fromhex("46238cb662b2fc7342de77d7c84abb5c")) + self.assertEqual(dest.hash, bytes.fromhex("fb48da0e82e6e01ba0c014513f74540d")) l1 = RNS.Link(dest) time.sleep(0.5) @@ -176,7 +176,7 @@ class TestLink(unittest.TestCase): dest = RNS.Destination(id1, RNS.Destination.OUT, RNS.Destination.SINGLE, APP_NAME, "link", "establish") - self.assertEqual(dest.hash, bytes.fromhex("46238cb662b2fc7342de77d7c84abb5c")) + self.assertEqual(dest.hash, bytes.fromhex("fb48da0e82e6e01ba0c014513f74540d")) l1 = RNS.Link(dest) time.sleep(0.5) @@ -211,7 +211,7 @@ class TestLink(unittest.TestCase): dest = RNS.Destination(id1, RNS.Destination.OUT, RNS.Destination.SINGLE, APP_NAME, "link", "establish") - self.assertEqual(dest.hash, bytes.fromhex("46238cb662b2fc7342de77d7c84abb5c")) + self.assertEqual(dest.hash, bytes.fromhex("fb48da0e82e6e01ba0c014513f74540d")) l1 = RNS.Link(dest) time.sleep(0.5) @@ -245,7 +245,7 @@ class TestLink(unittest.TestCase): self.assertEqual(id1.hash, bytes.fromhex(fixed_keys[0][1])) dest = RNS.Destination(id1, RNS.Destination.OUT, RNS.Destination.SINGLE, APP_NAME, "link", "establish") - self.assertEqual(dest.hash, bytes.fromhex("46238cb662b2fc7342de77d7c84abb5c")) + self.assertEqual(dest.hash, bytes.fromhex("fb48da0e82e6e01ba0c014513f74540d")) l1 = RNS.Link(dest) time.sleep(0.5) @@ -284,7 +284,7 @@ class TestLink(unittest.TestCase): self.assertEqual(id1.hash, bytes.fromhex(fixed_keys[0][1])) dest = RNS.Destination(id1, RNS.Destination.OUT, RNS.Destination.SINGLE, APP_NAME, "link", "establish") - self.assertEqual(dest.hash, bytes.fromhex("46238cb662b2fc7342de77d7c84abb5c")) + self.assertEqual(dest.hash, bytes.fromhex("fb48da0e82e6e01ba0c014513f74540d")) l1 = RNS.Link(dest) time.sleep(0.5) @@ -322,7 +322,7 @@ class TestLink(unittest.TestCase): self.assertEqual(id1.hash, bytes.fromhex(fixed_keys[0][1])) dest = RNS.Destination(id1, RNS.Destination.OUT, RNS.Destination.SINGLE, APP_NAME, "link", "establish") - self.assertEqual(dest.hash, bytes.fromhex("46238cb662b2fc7342de77d7c84abb5c")) + self.assertEqual(dest.hash, bytes.fromhex("fb48da0e82e6e01ba0c014513f74540d")) l1 = RNS.Link(dest) time.sleep(0.5)