Skip to content
Snippets Groups Projects
Commit 81456593 authored by Abdullah Bin Zubair's avatar Abdullah Bin Zubair
Browse files

Moved to iroha-drip-general-master/drippy folder.

parent 09a4db62
No related branches found
No related tags found
No related merge requests found
import sys
from binascii import hexlify, unhexlify
from bs4 import BeautifulSoup
from nacl.signing import SigningKey
from .util import *
from .cSHAKE import cSHAKE128
CONTEXT_ID = "F0EFF02FBFF43D0FE7930C3C6E6174EA"
CURVE_ID = "0001"
class HostIdentity:
def __init__(self, hhit, hid, priv_key, pub_key):
self._hhit = hhit
self._hid = hid
self._priv_key = priv_key
self._pub_key = pub_key
if not self.verify_hhit():
print("The public key does not produce the same hash in hhit.")
else:
print("hhit verified")
@staticmethod
def from_file(file_name):
bs = None
try:
with open(file_name, 'r') as f:
bs = BeautifulSoup(f.read(), 'lxml')
except PermissionError as e:
print(e)
sys.exit(1)
host_id = bs.find("host_identity") # use first host id
priv_key = ''.join(host_id.find("priv").string.split(':'))
pub_key = ''.join(host_id.find("pub").string.split(':'))
hid = host_id.find("hid").string
hhit = ''.join(["{:>04}".format(byte_group) for byte_group in host_id.find("hit").string.split(':')])
return HostIdentity(hhit, hid, priv_key, pub_key)
def get_hhit(self):
return self._hhit
def get_hid(self):
return self._hid
def get_pub_key(self):
return self._pub_key
def get_priv_key(self):
return self._priv_key
def verify_hhit(self):
chash = cSHAKE128(
unhexlify(CONTEXT_ID + CURVE_ID + self._pub_key),
64,
"",
unhexlify(CONTEXT_ID))
return chash.decode("utf-8") == self._hhit[16:]
def generate_self_attestation(self, expiration):
if expiration <= astm_time():
print("expiration timestamp has already expired.")
return None
message = self._hhit + self._pub_key + "{:>08x}".format(convert_be_to_le(expiration))
sign_key = SigningKey(unhexlify(self._priv_key))
return hexlify(sign_key.sign(unhexlify(message)))
##############################
## Function for DRIP_Wrapper
##############################
def generate_self_attestation_Wrapper(self,payload, expiration):
if expiration <= astm_time():
print("expiration timestamp has already expired.")
return None
#hash of SAM Type for Wrapper, which is 0x02
DET = self._hhit
hash_SAM = cSHAKE128( '0x02'.encode('utf-8'), 1,"","") # 1 byte at the start as SAM type
DET_hash = cSHAKE128( DET.encode('utf-8'), 16,"","") # Draft auth17 requirement of 16 bytes of DET
NBT_hash = cSHAKE128( "{:>08x}".format(convert_be_to_le(int(astm_time()))).encode('utf-8'), 4,"","") #Not before timestamp 4bytes
NAT_hash = cSHAKE128( "{:>08x}".format(convert_be_to_le(expiration)).encode('utf-8'), 4,"","") #Not after timestamp 4bytes
message = hash_SAM + DET_hash + payload + NBT_hash + NAT_hash
sign_key = SigningKey(unhexlify(self._priv_key))
return hexlify(sign_key.sign(unhexlify(message)))
#######################################################
## Function for DRIP_Manifests according to auth17 draft
#######################################################
def generate_self_attestation_Manifests(self, prev_hashed_loc, hashed_loc, expiration): #hashed_loc is hashes of location for now its single, but it can be upto 11(Each 8 byte)
if expiration <= astm_time():
print("expiration timestamp has already expired.")
return None
DET = self._hhit
#hash of SAM Type for Manifests, which is 0x03
hash_SAM = cSHAKE128( '0x03'.encode('utf-8'), 1,"","")
DET_hash = cSHAKE128( DET.encode('utf-8'), 16,"","") # Draft auth17 requirement of 16 bytes of DET
NBT_hash = cSHAKE128( "{:>08x}".format(convert_be_to_le(int(astm_time()))).encode('utf-8'), 4,"","") #Not before timestamp 4bytes
NAT_hash = cSHAKE128( "{:>08x}".format(convert_be_to_le(expiration)).encode('utf-8'), 4,"","") #Not after timestamp 4bytes
message = hash_SAM + DET_hash + prev_hashed_loc + hashed_loc + NBT_hash + NAT_hash
sign_key = SigningKey(unhexlify(self._priv_key))
return hexlify(sign_key.sign(unhexlify(message)))
###################################################################
## Function for DRIP_Endorsement_Broadcast according to auth17 draft
###################################################################
def generate_attestation_Endorsement_Broadcast(self, Drone_DET, Drone_hid, expiration):
if expiration <= astm_time():
print("expiration timestamp has already expired.")
return None
DET_DIME = self._hhit
DET_DIME_hash = cSHAKE128( DET_DIME.encode('utf-8'), 16,"","") # Draft auth17 requirement of 16 bytes of DET
DET_DRONE_hash = cSHAKE128( Drone_DET.encode('utf-8'), 16,"","") # Draft auth17 requirement of 16 bytes of DET
HID_DRONE_hash = cSHAKE128( Drone_hid.encode('utf-8'), 32,"","") # Draft auth17 requirement of 32 bytes of DET
NBT_hash = cSHAKE128( "{:>08x}".format(convert_be_to_le(int(astm_time()))).encode('utf-8'), 4,"","") #Not before timestamp 4bytes
NAT_hash = cSHAKE128( "{:>08x}".format(convert_be_to_le(expiration)).encode('utf-8'), 4,"","") #Not after timestamp 4bytes
message = DET_DIME_hash + DET_DRONE_hash + HID_DRONE_hash + NBT_hash + NAT_hash
sign_key = SigningKey(unhexlify(self._priv_key))
return sign_key.sign(unhexlify(message))
#This function was being used for draft auth01 version but no more being used
def generate_certificate(self, other, expiration):
signing_ts = astm_time()
if expiration <= signing_ts:
print("expiration timestamp has already expired.")
return None
ass_x = self.generate_self_attestation(expiration).decode("utf-8")
ass_y = other.generate_self_attestation(expiration).decode("utf-8")
len_ass_x = hex(struct.unpack("!2H", struct.pack("!i", len(ass_x)))[1])[2:]
while len(len_ass_x) < 4:
len_ass_x = '0' + len_ass_x
len_ass_y = hex(struct.unpack("!2H", struct.pack("!i", len(ass_y)))[1])[2:]
while len(len_ass_y) < 4:
len_ass_y = '0' + len_ass_y
message = len_ass_x + len_ass_y \
+ ass_x + ass_y \
+ hexlify(int(signing_ts).to_bytes(4, 'little')).decode("utf-8") \
+ hexlify(int(expiration).to_bytes(4, 'little')).decode("utf-8")
sign_key = SigningKey(unhexlify(self._priv_key))
return hexlify(sign_key.sign(unhexlify(message)))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment