def test_private_key_from_to_bytes(self): KmsChain = KMSChain() sk_1 = KmsChain.generate() sk_data_1 = sk_1.to_bytes() sk_2 = KmsChain.private_key_from_bytes(sk_data_1) sk_data_2 = sk_2.to_bytes() self.assertEqual(sk_data_1, sk_data_2)
def test_public_key_from_to_bytes(self): KmsChain = KMSChain() sk = KmsChain.generate() pk_1 = KmsChain.public_key(sk) pk_data_1 = pk_1.to_bytes() print("\nPK1 {}\n".format(pk_data_1)) pk_2 = KmsChain.public_key_from_bytes(pk_data_1) pk_data_2 = pk_2.to_bytes() print("\nPK2 {}\n".format(pk_data_2)) self.assertEqual(pk_data_1, pk_data_2)
def test_capsule_from_to_bytes(self): KmsChain = KMSChain() sk = KmsChain.generate() pk = KmsChain.public_key(sk) capsule_1, _ = KmsChain.encapsulate(pk) cData_1 = capsule_1.to_bytes() capsule_2 = KmsChain.capsule_from_bytes(cData_1) cData_2 = capsule_2.to_bytes() self.assertEqual(cData_1, cData_2)
def test_AES_encrypt_decrypt(self): KmsChain = KMSChain() alice_sk = KmsChain.generate() alice_pk = KmsChain.public_key(alice_sk) bob_sk = KmsChain.generate() bob_pk = KmsChain.public_key(bob_sk) capsule, symmetric_key_1 = KmsChain.encapsulate(bob_pk) #print("\n{}\n".format(binascii.hexlify(symmetric_key_1))) alice_cipher = AESCipher(binascii.hexlify(symmetric_key_1)[:16]) plaintext = 'abkcdclnlfwenhcbehkslaKmsChainnosbo' encrypted = alice_cipher.encrypt(plaintext) symmetric_key_2 = KmsChain.decapsulate(bob_sk, capsule) bob_cipher = AESCipher(binascii.hexlify(symmetric_key_2)[:16]) decrypted = bob_cipher.decrypt(encrypted) self.assertEqual(decrypted, plaintext)
def test_re_key_from_to_bytes(self): KmsChain = KMSChain() skA = KmsChain.generate() skB = KmsChain.generate() pkB = KmsChain.public_key(skB) rkAB_1 = KmsChain.generate_re_key(skA, pkB) rkAB_1_data = rkAB_1.to_bytes() rkAB_2 = KmsChain.re_encryption_key_from_bytes(rkAB_1_data) rkAB_2_data = rkAB_2.to_bytes() self.assertEqual(rkAB_1_data, rkAB_2_data)
def test_AES_encrypt_decrypt_re_encrypt(self): KmsChain = KMSChain() # create private, public keys alice_sk = KmsChain.generate() alice_pk = KmsChain.public_key(alice_sk) bob_sk = KmsChain.generate() bob_pk = KmsChain.public_key(bob_sk) # re-encryption key from Alice to Bob rk_AB = KmsChain.generate_re_key(alice_sk, bob_pk) # alice encrypt plaintext alice_capsule, alice_symmetric_key = KmsChain.encapsulate(alice_pk) print("\n{}\n".format(binascii.hexlify(alice_symmetric_key))) alice_cipher = AESCipher(binascii.hexlify(alice_symmetric_key)[:16]) plaintext = 'abkcdclnlfwenhcbehkslascnosbo' ciphertext = alice_cipher.encrypt(plaintext) # Bob decrypt ciphertext # first re-encrypt capsule recapsule = KmsChain.re_encrypt(rk_AB, alice_capsule) bob_symmetric_key = KmsChain.decapsulate(bob_sk, recapsule) recapsule = KmsChain.re_encrypt(rk_AB, alice_capsule) bob_symmetric_key = KmsChain.decapsulate(bob_sk, recapsule) bob_cipher = AESCipher(binascii.hexlify(bob_symmetric_key)[:16]) decrypted = bob_cipher.decrypt(ciphertext) self.assertEqual(binascii.hexlify(alice_symmetric_key), binascii.hexlify(bob_symmetric_key))