def create_ephemeral_channel(msg_id: bytes, ttl: int, ttr: int, addr_from: bytes, kyber_pk: bytes, kyber_sk: bytes, receiver_kyber_pk: bytes, dilithium_pk: bytes, dilithium_sk: bytes, prf512_seed: bytes, data: bytes, nonce: int): sender_kyber = Kyber(kyber_pk, kyber_sk) sender_kyber.kem_encode(receiver_kyber_pk) enc_aes256_symkey = bytes(sender_kyber.getCypherText()) aes256_symkey = sender_kyber.getMyKey() aes = AES(bytes(aes256_symkey)) sender_dilithium = Dilithium(dilithium_pk, dilithium_sk) ephemeral_data = EphemeralChannelPayload.create(addr_from, prf512_seed, data) ephemeral_data.dilithium_sign(msg_id, ttl, ttr, enc_aes256_symkey, nonce, sender_dilithium) encrypted_ephemeral_message = EncryptedEphemeralMessage() encrypted_ephemeral_message._data.msg_id = msg_id encrypted_ephemeral_message._data.ttl = ttl encrypted_ephemeral_message._data.ttr = ttr encrypted_ephemeral_message._data.channel.enc_aes256_symkey = enc_aes256_symkey encrypted_ephemeral_message._data.nonce = nonce encrypted_ephemeral_message._data.payload = aes.encrypt( ephemeral_data.to_json().encode()) return encrypted_ephemeral_message
def test_exchange_keys(self): alice = Kyber() bob = Kyber() # Alice sends her public key to Bob alice_public_key = alice.getPK() # Bob receives the public key, derives a secret and a response bob.kem_encode(alice_public_key) cypherText = bob.getCypherText() # Bob sends the cyphertext to alice valid = alice.kem_decode(cypherText) # Now Alice and Bob share the same key alice_key = alice.getMyKey() bob_key = bob.getMyKey() self.assertTrue(valid) self.assertEqual(alice_key, bob_key)
class Kyb(): def __init__(self, PK=None, SK=None): if not PK or not SK: self.k = Kyber() else: self.k = Kyber(PK, SK) self.PK = self.k.getPK() self.SK = self.k.getSK() def kem_encode(self, kyber_pk): self.k.kem_encode(kyber_pk) self.ciphertext = self.k.getCypherText() self.shared_secret = self.k.getMyKey() return self.ciphertext, self.shared_secret def kem_decode(self, ciphertext): if self.k.kem_decode(ciphertext) == True: self.shared_secret = self.k.getMyKey() return self.shared_secret else: return False