Skip to content

Commit

Permalink
Fix destination hash construction and random blob extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
markqvist committed Oct 4, 2022
1 parent 1166750 commit 7997fd1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
38 changes: 16 additions & 22 deletions RNS/Destination.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,17 @@ def expand_name(identity, app_name, *aspects):
return name


# @staticmethod
# def hash(identity, app_name, *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)
# hash_material = RNS.Identity.full_hash(base_name.encode("utf-8"))

# if identity != None:
# hash_material += identity.hash
@staticmethod
def hash(identity, app_name, *aspects):
"""
: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"))
addr_hash_material = name_hash
if identity != None:
addr_hash_material += identity.hash

# # Create a digest for the destination
# return RNS.Identity.full_hash(hash_material)[:RNS.Reticulum.TRUNCATED_HASHLENGTH//8]
return RNS.Identity.full_hash(addr_hash_material)[:RNS.Reticulum.TRUNCATED_HASHLENGTH//8]

@staticmethod
def app_and_aspects_from_name(full_name):
Expand All @@ -117,8 +115,8 @@ def hash_from_name_and_identity(full_name, identity):
: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)
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):
# Check input values and build name string
Expand All @@ -144,15 +142,11 @@ def __init__(self, identity, direction, type, app_name, *aspects):
raise TypeError("Selected destination type PLAIN cannot hold an identity")

self.identity = identity
self.full_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
self.name = Destination.expand_name(identity, app_name, *aspects)

# 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.default_app_data = None
Expand All @@ -166,7 +160,7 @@ def __str__(self):
"""
: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):
Expand Down
3 changes: 2 additions & 1 deletion RNS/Identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def validate_announce(packet):
signature = packet.data[Identity.KEYSIZE//8+Identity.HASHLENGTH//8+10:Identity.KEYSIZE//8+Identity.HASHLENGTH//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+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

Expand All @@ -245,6 +245,7 @@ def validate_announce(packet):
return True

else:
RNS.log("Received invalid announce for "+RNS.prettyhexrep(destination_hash)+". Destination mismatch.", RNS.LOG_DEBUG)
return False

else:
Expand Down
10 changes: 4 additions & 6 deletions RNS/Transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ def inbound(raw, interface=None):
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.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 = []
if packet.destination_hash in Transport.destination_table:
random_blobs = Transport.destination_table[packet.destination_hash][4]
Expand Down Expand Up @@ -1798,14 +1798,12 @@ def request_path(destination_hash, on_interface=None, tag=None, recursive=False)

queued_announces = True if len(on_interface.announce_queue) > 0 else False
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_DEBUG)
RNS.log("Blocking recursive path request on "+str(on_interface)+" due to queued announces", RNS.LOG_EXTREME)
return
else:
now = time.time()
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_DEBUG)
RNS.log("Blocking recursive path request on "+str(on_interface)+" due to active announce cap", RNS.LOG_EXTREME)
return
else:
tx_time = ((len(path_request_data)+RNS.Reticulum.HEADER_MINSIZE)*8) / on_interface.bitrate
Expand Down Expand Up @@ -2058,7 +2056,7 @@ def drop_announce_queues():

@staticmethod
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")

return announce_emitted
Expand Down

0 comments on commit 7997fd1

Please sign in to comment.