Skip to content
Snippets Groups Projects
Commit 6bfb2f9c authored by Mario Impesi's avatar Mario Impesi
Browse files

Implemented Wrapper and Link

parent 66ec6c5f
Branches
No related tags found
No related merge requests found
import argparse
import os
import sys
import subprocess
import string
import random
from enum import IntEnum
from dotenv.main import load_dotenv
import math
......@@ -11,9 +8,6 @@ import math
FILE_DIR = os.path.dirname(os.path.abspath(__file__))
BASE_DIR = os.path.join(FILE_DIR, "..")
sys.path.append(BASE_DIR)
# File with flight_id that will be incremented and used if no other
# flight_id is specified.
FLIGHT_ID_FILE = os.path.join(FILE_DIR, "default_flight_id.txt")
from drippy.gps_poll import GpsPoller
from drippy.util import astm_time, astm_float_to_int_bytes, hash
......@@ -60,7 +54,7 @@ def broadcast_message(payload: bytes, message_type: message_type, version):
def broadcast_authentication_message(message: bytes, sam_type: SAM_type):
"""Utility function to broadcast an authentication message of type ASTM Message 0x2, potentially divided into multiple pages.
The argument can be a DRIP Endorsement Structure containing a DRIP Manifest or Wrapper."""
The message argument can be a DRIP Endorsement Structure containing a DRIP Manifest or Wrapper, or the message can be a DRIP Link"""
# Maximum size defined in draft-ietf-drip-auth 40 3.2.3.1
length = len(message)
......@@ -223,7 +217,7 @@ def broadcast_self_id():
# DRIP Manifest Authentication Message
def broadcast_manifest(messages, previous_manifest_hash, drone_hi) -> bytes:
"""
Broadcast DRIP Manifest including the messages given as argument.
Broadcast DRIP Manifest including the hashes of the messages given as argument.
"""
print("broadcasting manifest")
......@@ -239,39 +233,55 @@ def broadcast_manifest(messages, previous_manifest_hash, drone_hi) -> bytes:
for message in messages:
message_hashes += hash(message)
message = previous_manifest_hash + current_manifest_hash + message_hashes
evidence = previous_manifest_hash + current_manifest_hash + message_hashes
current_manifest_hash = hash(message)
current_manifest_hash = hash(evidence)
message = previous_manifest_hash + current_manifest_hash + message_hashes
evidence = previous_manifest_hash + current_manifest_hash + message_hashes
broadcast_endorsement_structure(message, drone_hi, SAM_type.MANIFEST)
broadcast_endorsement_structure(evidence, drone_hi, SAM_type.MANIFEST)
return current_manifest_hash
# DRIP Wrapper Authentication Message
def broadcast_wrapper():
def broadcast_wrapper(messages, drone_hi):
"""
Broadcast DRIP Wrapper.
Broadcast DRIP Wrapper including the messages given as argument. The messages can be all ASTM messages except Authentication (0x02) and Message Pack (0xF)
and they MUST be in Message Type order.
"""
print("broadcasting wrapper")
if len(messages) > 4:
print(f"Tried to broadcast wrapper with {len(messages)} messages. Maximum messages in wrapper: 4.")
return
evidence = b''
# 4.3 in draft-ietf-drip-auth 40 specifies that the messages should be full 25 bytes messages, so we add some padding where necessary.
for message in messages:
if(len(message) > 25):
print(f"Tried to include a message longer than 25 bytes in a DRIP Wrapper.")
return
padding = b'\x00' * (25-len(message))
evidence += message + padding
broadcast_endorsement_structure(evidence, drone_hi, SAM_type.WRAPPER)
# DRIP Link Authentication Message
def broadcast_link():
def broadcast_link(broadcast_endorsement):
"""
Broadcast DRIP Link.
Broadcast DRIP Link which is a broadcast endorsement generated by the parent. It can be HDA on UA, RAA on HDA, Apex on RAA and IANA on UAS RID Apex.
This does not use the endorsement structure.
"""
broadcast_authentication_message(broadcast_endorsement, SAM_type.LINK)
DESCRIPTION = """Send ble advertisements using the specified Host Identity,
in accordance to ASTM F3411-19.
If iroha option with the private key file is specified,
location updates will be sent to Iroha blockchain as well.
The private key should belong to the same host specificed by
the host identity.
If no flight_id is specified, then a random flight id will be generated.
flight_id is only used if the --iroha option is specified.
DESCRIPTION = """
Send ble advertisements using the specified Host Identity,
in accordance to ASTM F3411-19.
"""
......@@ -283,18 +293,10 @@ def main():
parser.add_argument(
"-f", "--file",
dest="host_identity_file",
default="/usr/local/etc/hip/my_host_identities.xml",
default="/home/pi/tdde21-drip-2023/drip-general-master/identities/drone/drone_identities.xml",
type=str,
help="Specify the host identity xml file."
)
parser.add_argument(
"-r", "--registry_file", # this is for registry_identity_file
dest="registry_identity_file",
default="/usr/local/etc/hip/my_host_identities.xml",
type=str,
help="Specify the registry identity xml file."
)
parser.add_argument(
"-b", "--bluetooth",
dest="bluetooth_version",
......@@ -313,11 +315,6 @@ def main():
help="Turn on gps updates",
action="store_true"
)
parser.add_argument(
"--flight_id",
type=int,
help="Specify a flight_id for the location updates. Used to identify the flight path in the blockchain. This value is only used if the --iroha option is specified."
)
parser.add_argument(
"--run-scenario",
dest="run_scenario",
......@@ -331,8 +328,6 @@ def main():
# hi_file is read from identities/drone
hi_file = args.host_identity_file
# registry hi_file (actually identities/admin)
r_hi_file = args.registry_identity_file
is_bt5 = True if args.bluetooth_version == 5 else False
is_wifi= args.activate_wifi
......@@ -345,17 +340,9 @@ def main():
#HostIdentity object containing Drone DRIP credentials
drone_hi = HostIdentity.from_file(hi_file)
if args.flight_id:
flight_id = args.flight_id
else:
flight_id = ''.join(random.SystemRandom().choice(
string.hexdigits) for _ in range(64))
# Reset bt
subprocess.run("hcitool -i hci0 cmd 0x03 0x0003", shell=True)
# Set (Extended) Advertising Parameters
if not is_wifi:
reset_bluetooth()
initialize_bluetooth(is_bt5)
gpsd = None
......@@ -368,6 +355,7 @@ def main():
id4 = broadcast_basic_id_4(drone_hi)
location = broadcast_location(gpsd)
manifest_hash = broadcast_manifest([id1, id4, location], hash("nonce".encode('utf-8')), drone_hi)
broadcast_wrapper([id1, id4, location], drone_hi)
except KeyboardInterrupt:
# Reset bt
......
......@@ -55,8 +55,9 @@ class HostIdentity:
def get_serial_number(self):
return self._serial_number
# Verify ORCHID hash in HHIT
def verify_hhit(self):
CONTEXT_ID = "F0EFF02FBFF43D0FE7930C3C6E6174EA" #TODO: update context id to the one in the RFC
CONTEXT_ID = "00B5A69C795DF5D5F0087F56843F2C40" # From chapter 3 in RFC 9374
chash = hash(self._hhit[:8], CONTEXT_ID)
return chash == self._hhit[8:]
......
5be4df7282f47991fc806dfd24e593ad06aa7e13cc9c1082dd4093ff0b39da3d
\ No newline at end of file
3c0689f45d7bcbeb5c1d04072e8cfd912820b50fd8ba97788e79ba4bbf38eb4f
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<my_host_identities>
<host_identity alg="EdDSA" alg_id="13" length="128" anon="no" incoming="yes" hit_suite_id="5" r1count="10">
<name>menovo-1024</name>
<HID>0b3fa14c</HID>
<CURVE>1</CURVE>
<PRIV>CD:92:97:3C:C0:6A:B0:FD:2E:43:3C:FA:C8:36:C5:BF:B4:38:4A:CA:CE:D9:9E:AB:DA:85:8C:54:D0:D8:3C:76</PRIV>
<PUB>40:E1:02:22:DF:5D:67:4F:66:12:8D:D6:65:C0:3D:C4:50:20:D5:F9:32:D5:66:F3:59:FA:21:40:C0:39:10:E5</PUB>
<HIT>2001:25:b3f:a14c:422d:9778:15e6:912e</HIT>
<LSI>1.230.145.46</LSI>
</host_identity>
</my_host_identities>
from drippy.cSHAKE import cSHAKE128
from binascii import unhexlify
CONTEXT_ID = "F0EFF02FBFF43D0FE7930C3C6E6174EA"
CURVE_ID = "0001"
pub_key = "E32E3AC8C1F7A1BDFEA67E7539AB41E3383A47DBB16B327E1DE511B8FFBE0754"
input = "2001300b3fa14c05"
result = cSHAKE128(
unhexlify(input),
64,
"",
unhexlify(CONTEXT_ID)
)
print(result)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment