Implement sendMessage and receiveMessage

This commit is contained in:
hinuiiik
2026-10-22 23:21:13 -04:00
parent 0fcc0267dc
commit ec5b6322df
2 changed files with 24 additions and 7 deletions
+2 -2
View File
@@ -54,8 +54,8 @@ print("Testing Reporting")
content = "inappropriate message contents" content = "inappropriate message contents"
reportPT, reportCT = alice.report("Bob", content) reportPT, reportCT = alice.report("Bob", content)
decryptedReport = server.decryptReport(reportCT) decryptedReport = server.decryptReport(reportCT)
print(reportPT) # print(reportPT)
print(decryptedReport) # print(decryptedReport)
if decryptedReport != reportPT: if decryptedReport != reportPT:
error("report did not decrypt properly") error("report did not decrypt properly")
print(reportPT) print(reportPT)
+22 -5
View File
@@ -3,7 +3,9 @@ import pickle
import string import string
import hmac import hmac
from dataclasses import dataclass from dataclasses import dataclass
from types import SimpleNamespace
from cryptography.exceptions import InvalidTag
from cryptography.hazmat.primitives import hashes, serialization from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import ec from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives.ciphers.aead import AESGCM from cryptography.hazmat.primitives.ciphers.aead import AESGCM
@@ -170,12 +172,28 @@ class MessengerClient:
self.certs[name] = public_key self.certs[name] = public_key
def sendMessage(self, name, message): def sendMessage(self, name, message):
raise Exception("not implemented!") state = self.conns.get(name)
return if state is None:
state = SimpleNamespace()
self.conns[name] = state
initial_secret = DH(self.dh, self.certs[name])
self.RatchetInitAlice(state, initial_secret, self.certs[name])
plaintext = message.encode("utf-8")
return self.RatchetEncrypt(state, plaintext, b"")
def receiveMessage(self, name, header, ciphertext): def receiveMessage(self, name, header, ciphertext):
raise Exception("not implemented!") state = self.conns.get(name)
return if state is None:
state = SimpleNamespace()
initial_secret = DH(self.dh, self.certs[name])
self.conns[name] = state
self.RatchetInitBob(state, initial_secret, self.dh)
try:
plaintext = self.RatchetDecrypt(state, header, ciphertext, b"")
except InvalidTag:
return None
return plaintext.decode("utf-8")
# this is technically hybrid encryption instead of just el gamal but I blame the instructions instead of myself # this is technically hybrid encryption instead of just el gamal but I blame the instructions instead of myself
# maybe i'll change this if I want to use a late day # maybe i'll change this if I want to use a late day
@@ -204,4 +222,3 @@ class MessengerClient:
"ciphertext": ciphertext "ciphertext": ciphertext
} }
return plaintext.decode("latin1"), pickle.dumps(data) return plaintext.decode("latin1"), pickle.dumps(data)