Fix destination hash construction and random blob extraction

This commit is contained in:
Mark Qvist 2022-10-04 09:11:20 +02:00
parent 11667504b2
commit 7997fd104e
3 changed files with 22 additions and 29 deletions

View File

@ -89,19 +89,17 @@ class Destination:
return name return name
# @staticmethod @staticmethod
# def hash(identity, app_name, *aspects): def hash(identity, app_name, *aspects):
# """ """
# :returns: A destination name in adressable hash form, for an app_name and a number of aspects. :returns: A destination name in adressable hash form, for an app_name and a number of aspects.
# """ """
# base_name = Destination.expand_name(None, app_name, *aspects) name_hash = RNS.Identity.full_hash(Destination.expand_name(None, app_name, *aspects).encode("utf-8"))
# hash_material = RNS.Identity.full_hash(base_name.encode("utf-8")) addr_hash_material = name_hash
if identity != None:
addr_hash_material += identity.hash
# if identity != None: return RNS.Identity.full_hash(addr_hash_material)[:RNS.Reticulum.TRUNCATED_HASHLENGTH//8]
# hash_material += identity.hash
# # Create a digest for the destination
# return RNS.Identity.full_hash(hash_material)[:RNS.Reticulum.TRUNCATED_HASHLENGTH//8]
@staticmethod @staticmethod
def app_and_aspects_from_name(full_name): def app_and_aspects_from_name(full_name):
@ -117,8 +115,8 @@ class Destination:
:returns: A destination name in adressable hash form, for a full name string and Identity instance. :returns: A destination name in adressable hash form, for a full name string and Identity instance.
""" """
app_name, aspects = Destination.app_and_aspects_from_name(full_name) app_name, aspects = Destination.app_and_aspects_from_name(full_name)
aspects.append(identity.hexhash)
return Destination.hash(app_name, *aspects) return Destination.hash(identity, app_name, *aspects)
def __init__(self, identity, direction, type, app_name, *aspects): def __init__(self, identity, direction, type, app_name, *aspects):
# Check input values and build name string # Check input values and build name string
@ -144,15 +142,11 @@ class Destination:
raise TypeError("Selected destination type PLAIN cannot hold an identity") raise TypeError("Selected destination type PLAIN cannot hold an identity")
self.identity = identity self.identity = identity
self.full_name = Destination.expand_name(identity, app_name, *aspects) self.name = Destination.expand_name(identity, app_name, *aspects)
self.name_hash = RNS.Identity.full_hash(self.expand_name(None, app_name, *aspects).encode("utf-8"))
self.addr_hash_material = self.name_hash
if self.identity != None:
self.addr_hash_material += self.identity.hash
# Generate the destination address hash # Generate the destination address hash
self.hash = RNS.Identity.full_hash(self.addr_hash_material)[:RNS.Reticulum.TRUNCATED_HASHLENGTH//8] 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.hexhash = self.hash.hex() self.hexhash = self.hash.hex()
self.default_app_data = None self.default_app_data = None
@ -166,7 +160,7 @@ class Destination:
""" """
:returns: A human-readable representation of the destination including addressable hash and full name. :returns: A human-readable representation of the destination including addressable hash and full name.
""" """
return "<"+self.full_name+"/"+self.hexhash+">" return "<"+self.name+"/"+self.hexhash+">"
def announce(self, app_data=None, path_response=False, send=True): def announce(self, app_data=None, path_response=False, send=True):

View File

@ -219,7 +219,7 @@ class Identity:
signature = packet.data[Identity.KEYSIZE//8+Identity.HASHLENGTH//8+10:Identity.KEYSIZE//8+Identity.HASHLENGTH//8+10+Identity.SIGLENGTH//8] signature = packet.data[Identity.KEYSIZE//8+Identity.HASHLENGTH//8+10:Identity.KEYSIZE//8+Identity.HASHLENGTH//8+10+Identity.SIGLENGTH//8]
app_data = b"" app_data = b""
if len(packet.data) > Identity.KEYSIZE//8+Identity.HASHLENGTH//8+10+Identity.SIGLENGTH//8: if len(packet.data) > Identity.KEYSIZE//8+Identity.HASHLENGTH//8+10+Identity.SIGLENGTH//8:
app_data = packet.data[Identity.KEYSIZE//8+10+Identity.KEYSIZE//8:] app_data = packet.data[Identity.KEYSIZE//8+Identity.HASHLENGTH//8+10+Identity.SIGLENGTH//8:]
signed_data = destination_hash+public_key+name_hash+random_hash+app_data signed_data = destination_hash+public_key+name_hash+random_hash+app_data
@ -245,6 +245,7 @@ class Identity:
return True return True
else: else:
RNS.log("Received invalid announce for "+RNS.prettyhexrep(destination_hash)+". Destination mismatch.", RNS.LOG_DEBUG)
return False return False
else: else:

View File

@ -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): 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) announce_emitted = Transport.announce_emitted(packet)
random_blob = packet.data[RNS.Identity.KEYSIZE//8:RNS.Identity.KEYSIZE//8+RNS.Reticulum.TRUNCATED_HASHLENGTH//8] random_blob = packet.data[RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8:RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8+10]
random_blobs = [] random_blobs = []
if packet.destination_hash in Transport.destination_table: if packet.destination_hash in Transport.destination_table:
random_blobs = Transport.destination_table[packet.destination_hash][4] random_blobs = Transport.destination_table[packet.destination_hash][4]
@ -1798,14 +1798,12 @@ class Transport:
queued_announces = True if len(on_interface.announce_queue) > 0 else False queued_announces = True if len(on_interface.announce_queue) > 0 else False
if queued_announces: if queued_announces:
# TODO: Reset to extra level, probably RNS.log("Blocking recursive path request on "+str(on_interface)+" due to queued announces", RNS.LOG_EXTREME)
RNS.log("Blocking recursive path request on "+str(on_interface)+" due to queued announces", RNS.LOG_DEBUG)
return return
else: else:
now = time.time() now = time.time()
if now < on_interface.announce_allowed_at: if now < on_interface.announce_allowed_at:
# TODO: Reset to extra level, probably RNS.log("Blocking recursive path request on "+str(on_interface)+" due to active announce cap", RNS.LOG_EXTREME)
RNS.log("Blocking recursive path request on "+str(on_interface)+" due to active announce cap", RNS.LOG_DEBUG)
return return
else: else:
tx_time = ((len(path_request_data)+RNS.Reticulum.HEADER_MINSIZE)*8) / on_interface.bitrate tx_time = ((len(path_request_data)+RNS.Reticulum.HEADER_MINSIZE)*8) / on_interface.bitrate
@ -2058,7 +2056,7 @@ class Transport:
@staticmethod @staticmethod
def announce_emitted(packet): def announce_emitted(packet):
random_blob = packet.data[RNS.Identity.KEYSIZE//8:RNS.Identity.KEYSIZE//8+RNS.Reticulum.TRUNCATED_HASHLENGTH//8] random_blob = packet.data[RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8:RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8+10]
announce_emitted = int.from_bytes(random_blob[5:10], "big") announce_emitted = int.from_bytes(random_blob[5:10], "big")
return announce_emitted return announce_emitted