refactor chacha
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Torma Kristóf 2021-04-26 18:31:50 +02:00
parent ce4fd0cd30
commit 78d597425d
Signed by: tormakris
GPG Key ID: DC83C4F2C41B1047

View File

@ -7,7 +7,6 @@ import pyDH
from Crypto.Hash import SHA512 from Crypto.Hash import SHA512
from Crypto.Cipher import ChaCha20_Poly1305, PKCS1_OAEP from Crypto.Cipher import ChaCha20_Poly1305, PKCS1_OAEP
from Crypto.PublicKey.RSA import RsaKey from Crypto.PublicKey.RSA import RsaKey
from Crypto.Random import get_random_bytes
from netsim import network_interface from netsim import network_interface
@ -66,32 +65,10 @@ class NetWrapper:
def authenticate(self, password: str): def authenticate(self, password: str):
message = f"LIN {self.username} {password}".encode('UTF-8') message = f"LIN {self.username} {password}".encode('UTF-8')
cipher = ChaCha20_Poly1305.new(key=self.cipherkey) self.sendTypedMessage(message, "AUT")
header = json.dumps({'type': 'AUT', 'source': self.network.own_addr}).encode('UTF-8') plaintext = self.recieveTypedMessage("AUT").decode('UTF-8')
cipher.update(header) if plaintext != "OK":
ciphertext, tag = cipher.encrypt_and_digest(message) print("Authentication error")
nonce = b64encode(cipher.nonce).decode('UTF-8')
ct = b64encode(ciphertext).decode('UTF-8')
b64tag = b64encode(tag).decode('UTF-8')
sendjson = json.dumps(
{'header': b64encode(header).decode('UTF-8'), 'nonce': nonce, 'message': ct, 'tag': b64tag}).encode(
'UTF-8')
self.network.send_msg(self.serverAddr, sendjson)
try:
status, msg = self.network.receive_msg(blocking=True)
if not status:
raise Exception('Network error during connection.')
b64 = json.loads(msg.decode('UTF-8'))
retnonce = b64decode(b64['nonce'])
retciphertext = b64decode(b64['message'])
retcipher = ChaCha20_Poly1305.new(key=self.cipherkey, nonce=retnonce)
retcipher.update(b64decode(b64['header']))
retheader = json.loads(b64decode(b64['header']).decode('UTF-8'))
plaintext = retcipher.decrypt_and_verify(retciphertext, b64decode(b64['tag'])).decode('UTF-8')
if plaintext != "OK" or not (retheader['source'] == self.serverAddr and retheader['type'] == 'AUT'):
raise Exception('Authentication error')
except Exception as e:
print(e)
def connectToServer(self): def connectToServer(self):
identStatus = self.identifyServer() identStatus = self.identifyServer()
@ -102,9 +79,17 @@ class NetWrapper:
pw = input() pw = input()
self.authenticate(pw) self.authenticate(pw)
def sendMessage(self, message: bytes): def sendMessage(self, message: bytes) -> None:
try:
self.sendTypedMessage(message, "CMD")
except Exception:
print("Could not send message")
def sendTypedMessage(self, message: bytes, type: str) -> None:
if not (type == "AUT" or type == "CMD"):
raise Exception('Unknown message type')
cipher = ChaCha20_Poly1305.new(key=self.cipherkey) cipher = ChaCha20_Poly1305.new(key=self.cipherkey)
header = json.dumps({'type': 'CMD', 'source': self.network.own_addr}).encode('UTF-8') header = json.dumps({'type': type, 'source': self.network.own_addr}).encode('UTF-8')
cipher.update(header) cipher.update(header)
ciphertext, tag = cipher.encrypt_and_digest(message) ciphertext, tag = cipher.encrypt_and_digest(message)
nonce = b64encode(cipher.nonce).decode('UTF-8') nonce = b64encode(cipher.nonce).decode('UTF-8')
@ -115,6 +100,11 @@ class NetWrapper:
self.network.send_msg(self.serverAddr, sendjson) self.network.send_msg(self.serverAddr, sendjson)
def recieveMessage(self) -> bytes: def recieveMessage(self) -> bytes:
return self.recieveTypedMessage("CMD")
def recieveTypedMessage(self, type: str) -> bytes:
if not (type == "AUT" or type == "CMD"):
raise Exception('Unknown message type')
try: try:
status, msg = self.network.receive_msg(blocking=True) status, msg = self.network.receive_msg(blocking=True)
if not status: if not status:
@ -126,7 +116,7 @@ class NetWrapper:
retcipher.update(b64decode(b64['header'])) retcipher.update(b64decode(b64['header']))
plaintext = retcipher.decrypt_and_verify(retciphertext, b64decode(b64['tag'])) plaintext = retcipher.decrypt_and_verify(retciphertext, b64decode(b64['tag']))
retheader = json.loads(b64decode(b64['header']).decode('UTF-8')) retheader = json.loads(b64decode(b64['header']).decode('UTF-8'))
if not (retheader['source'] == self.serverAddr and retheader['type'] == 'CMD'): if not (retheader['source'] == self.serverAddr and retheader['type'] == type):
return "ERROR".encode('UTF-8') return "ERROR".encode('UTF-8')
return plaintext return plaintext
except Exception: except Exception: