Impl the basic signal functions that were listed
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import os
|
||||
import pickle
|
||||
import string
|
||||
import hmac
|
||||
from cryptography.hazmat.primitives import hashes, serialization
|
||||
from cryptography.hazmat.primitives.asymmetric import ec
|
||||
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
||||
@@ -8,12 +9,51 @@ from cryptography.hazmat.primitives.kdf.hkdf import HKDF
|
||||
|
||||
CURVE = ec.SECP256R1()
|
||||
|
||||
class MessageHeader:
|
||||
dh: object
|
||||
pn: int
|
||||
n: int
|
||||
|
||||
def GENERATE_DH():
|
||||
return ec.generate_private_key(CURVE)
|
||||
|
||||
def DH(dh_pair, dh_pub):
|
||||
return dh_pair.exchange(ec.ECDH(), dh_pub)
|
||||
|
||||
def KDF_RK(rk, dh_out):
|
||||
output = HKDF(
|
||||
algorithm=hashes.SHA256(),
|
||||
length=64,
|
||||
salt=rk,
|
||||
info=b"",
|
||||
).derive(dh_out)
|
||||
return output[:32], output[32:]
|
||||
|
||||
|
||||
def KDF_CK(ck):
|
||||
return (
|
||||
hmac.new(ck, b"\x01", "sha256").digest(),
|
||||
hmac.new(ck, b"\x02", "sha256").digest(),
|
||||
)
|
||||
|
||||
def ENCRYPT(mk, plaintext, associated_data):
|
||||
nonce = os.urandom(12)
|
||||
return nonce + AESGCM(mk).encrypt(nonce, plaintext, associated_data)
|
||||
|
||||
|
||||
def DECRYPT(mk, ciphertext, associated_data):
|
||||
return AESGCM(mk).decrypt(ciphertext[:12], ciphertext[12:], associated_data)
|
||||
|
||||
def HEADER(dh_pair, pn, n):
|
||||
public_key = dh_pair.public_key()
|
||||
return MessageHeader(public_key, int(pn), int(n))
|
||||
|
||||
def CONCAT(ad, header):
|
||||
header_tup = pickle.dumps(
|
||||
(header.dh.public_bytes(serialization.Encoding.X962,serialization.PublicFormat.UncompressedPoint), header.pn, header.n),
|
||||
)
|
||||
return len(ad).to_bytes(8) + ad + header_tup
|
||||
|
||||
class MessengerServer:
|
||||
def __init__(self, server_signing_key, server_decryption_key):
|
||||
self.server_signing_key = server_signing_key
|
||||
|
||||
Reference in New Issue
Block a user