Serial interface and echo example

This commit is contained in:
Mark Qvist 2018-03-20 16:57:26 +01:00
parent d4be407821
commit e66622bd69
3 changed files with 62 additions and 38 deletions

View File

@ -104,7 +104,7 @@ class Identity:
announced_identity = Identity(public_only=True) announced_identity = Identity(public_only=True)
announced_identity.loadPublicKey(public_key) announced_identity.loadPublicKey(public_key)
if announced_identity.validate(signature, signed_data): if announced_identity.pub != None and announced_identity.validate(signature, signed_data):
FPE.log("Announce is valid", FPE.LOG_VERBOSE) FPE.log("Announce is valid", FPE.LOG_VERBOSE)
FPE.Identity.remember(FPE.Identity.fullHash(packet.raw), destination_hash, public_key) FPE.Identity.remember(FPE.Identity.fullHash(packet.raw), destination_hash, public_key)
FPE.log("Stored valid announce from "+FPE.prettyhexrep(destination_hash), FPE.LOG_INFO) FPE.log("Stored valid announce from "+FPE.prettyhexrep(destination_hash), FPE.LOG_INFO)
@ -158,9 +158,12 @@ class Identity:
self.updateHashes() self.updateHashes()
def loadPublicKey(self, key): def loadPublicKey(self, key):
try:
self.pub_bytes = key self.pub_bytes = key
self.pub = load_der_public_key(self.pub_bytes, backend=default_backend()) self.pub = load_der_public_key(self.pub_bytes, backend=default_backend())
self.updateHashes() self.updateHashes()
except Exception as e:
FPE.log("Error while loading public key, the contained exception was: "+str(e), FPE.LOG_ERROR)
def updateHashes(self): def updateHashes(self):
self.hash = Identity.truncatedHash(self.pub_bytes) self.hash = Identity.truncatedHash(self.pub_bytes)
@ -203,7 +206,8 @@ class Identity:
def decrypt(self, ciphertext): def decrypt(self, ciphertext):
if self.prv != None: if self.prv != None:
# TODO: Remove debug output print("Ciphertext length is "+str(len(ciphertext))+". ") plaintext = None
try:
chunksize = (Identity.KEYSIZE)/8 chunksize = (Identity.KEYSIZE)/8
chunks = int(math.ceil(len(ciphertext)/(float(chunksize)))) chunks = int(math.ceil(len(ciphertext)/(float(chunksize))))
@ -214,8 +218,6 @@ class Identity:
if (chunk+1)*chunksize > len(ciphertext): if (chunk+1)*chunksize > len(ciphertext):
end = len(ciphertext) end = len(ciphertext)
# TODO: Remove debug output print("Processing chunk "+str(chunk+1)+" of "+str(chunks)+". Starting at "+str(start)+" and stopping at "+str(end)+". The length is "+str(len(ciphertext[start:end])))
plaintext += self.prv.decrypt( plaintext += self.prv.decrypt(
ciphertext[start:end], ciphertext[start:end],
padding.OAEP( padding.OAEP(
@ -224,6 +226,9 @@ class Identity:
label=None label=None
) )
) )
except:
FPE.log("Decryption by "+FPE.prettyhexrep(self.hash)+" failed")
return plaintext; return plaintext;
else: else:
raise KeyError("Decryption failed because identity does not hold a private key") raise KeyError("Decryption failed because identity does not hold a private key")

View File

@ -4,11 +4,12 @@ from time import sleep
import sys import sys
import serial import serial
import threading import threading
import time
import FPE import FPE
class SerialInterface(Interface): class SerialInterface(Interface):
MAX_CHUNK = 32768 MAX_CHUNK = 32768
TIMEOUT_SECONDS = 0.15 TIMEOUT_SECONDS = 1.0
owner = None owner = None
port = None port = None
@ -26,6 +27,8 @@ class SerialInterface(Interface):
self.databits = databits self.databits = databits
self.parity = serial.PARITY_NONE self.parity = serial.PARITY_NONE
self.stopbits = stopbits self.stopbits = stopbits
self.timeout = 100
self.online = False
if parity.lower() == "e" or parity.lower() == "even": if parity.lower() == "e" or parity.lower() == "even":
self.parity = serial.PARITY_EVEN self.parity = serial.PARITY_EVEN
@ -41,9 +44,10 @@ class SerialInterface(Interface):
bytesize = self.databits, bytesize = self.databits,
parity = self.parity, parity = self.parity,
stopbits = self.stopbits, stopbits = self.stopbits,
timeout = SerialInterface.TIMEOUT_SECONDS,
xonxoff = False, xonxoff = False,
rtscts = False, rtscts = False,
timeout = 0,
inter_byte_timeout = None,
write_timeout = None, write_timeout = None,
dsrdtr = False, dsrdtr = False,
) )
@ -52,10 +56,11 @@ class SerialInterface(Interface):
raise e raise e
if self.serial.is_open: if self.serial.is_open:
sleep(0.5)
thread = threading.Thread(target=self.readLoop) thread = threading.Thread(target=self.readLoop)
thread.setDaemon(True) thread.setDaemon(True)
thread.start() thread.start()
sleep(0.5) self.online = True
FPE.log("Serial port "+self.port+" is now open") FPE.log("Serial port "+self.port+" is now open")
else: else:
raise IOError("Could not open serial port") raise IOError("Could not open serial port")
@ -66,18 +71,29 @@ class SerialInterface(Interface):
def processOutgoing(self,data): def processOutgoing(self,data):
if self.online:
written = self.serial.write(data) written = self.serial.write(data)
if written != len(data): if written != len(data):
raise IOError("Serial interface only wrote "+str(written)+" bytes of "+str(len(data))) raise IOError("Serial interface only wrote "+str(written)+" bytes of "+str(len(data)))
def readLoop(self): def readLoop(self):
#pass try:
data_buffer = ""
last_read_ms = int(time.time()*1000)
while self.serial.is_open: while self.serial.is_open:
data = self.serial.read(size=self.owner.__class__.MTU) if self.serial.in_waiting:
if not data == "": data = self.serial.read(size=self.serial.in_waiting)
self.processIncoming(data) data_buffer += data
last_read_ms = int(time.time()*1000)
else:
time_since_last = int(time.time()*1000) - last_read_ms
if len(data_buffer) > 0 and time_since_last > self.timeout:
self.processIncoming(data_buffer)
data_buffer = ""
sleep(0.08)
except Exception as e:
self.online = False
FPE.log("A serial port error occurred, the contained exception was: "+str(e), FPE.LOG_ERROR)
FPE.log("The interface "+str(self.name)+" is now offline. Restart FlexPE to attempt reconnection.", FPE.LOG_ERROR)

View File

@ -36,7 +36,7 @@ def server(configpath):
def announceLoop(destination): def announceLoop(destination):
# Let the user know that everything is ready # Let the user know that everything is ready
FPE.log("Echo server running, hit enter to send announce (Ctrl-C to quit)") FPE.log("Echo server "+FPE.prettyhexrep(destination.hash)+" running, hit enter to send announce (Ctrl-C to quit)")
# We enter a loop that runs until the users exits. # We enter a loop that runs until the users exits.
# If the user just hits enter, we will announce our server # If the user just hits enter, we will announce our server
@ -102,7 +102,7 @@ def client(destination_hexhash, configpath):
exit() exit()
# We must first initialise FlexPE # We must first initialise FlexPE
fpe = FPE.FlexPE() fpe = FPE.FlexPE(configpath)
# Randomly create a new identity for our echo server # Randomly create a new identity for our echo server
client_identity = FPE.Identity() client_identity = FPE.Identity()
@ -191,10 +191,13 @@ def clientProofCallback(proof_packet):
else: else:
rtt = round(rtt*1000, 3) rtt = round(rtt*1000, 3)
rttstring = str(rtt)+" milliseconds" rttstring = str(rtt)+" milliseconds"
FPE.log( FPE.log(
"Valid echo reply, proved by "+FPE.prettyhexrep(unproven_packet.destination.hash)+ "Valid echo reply, proved by "+FPE.prettyhexrep(unproven_packet.destination.hash)+
", round-trip time was "+rttstring ", round-trip time was "+rttstring
) )
sent_requests.remove(unproven_packet)
del unproven_packet
else: else:
FPE.log("Proof invalid") FPE.log("Proof invalid")