def test_ecies_gen_ephemeral_key(self): result_data = _internal._ecies_gen_ephemeral_key( self.pubkey_a) self.assertEqual(tuple, type(result_data)) self.assertEqual(2, len(result_data)) eph_privkey, enc_data = result_data self.assertEqual(bytes, type(eph_privkey)) self.assertEqual(32, len(eph_privkey)) self.assertEqual(tuple, type(enc_data)) self.assertEqual(2, len(enc_data)) enc_symm_key, enc_eph_key = enc_data self.assertEqual(umbral.EncryptedKey, type(enc_symm_key)) self.assertEqual(EncryptedMessage, type(enc_eph_key)) self.assertNotEqual(eph_privkey, enc_eph_key) dec_symm_key = api.ecies_decapsulate(self.privkey_a, enc_symm_key) self.assertEqual(bytes, type(dec_symm_key)) self.assertEqual(32, len(dec_symm_key)) dec_eph_key = api.symm_decrypt(dec_symm_key, enc_eph_key) self.assertEqual(bytes, type(dec_eph_key)) self.assertEqual(32, len(dec_eph_key)) self.assertEqual(eph_privkey, dec_eph_key)
def test_symm_decrypt(self): key = api.secure_random(32) plaintext = b'this is a test' ciphertext = api.symm_encrypt(key, plaintext) self.assertEqual(EncryptedMessage, type(ciphertext)) self.assertNotEqual(plaintext, ciphertext) dec_text = api.symm_decrypt(key, ciphertext) self.assertEqual(bytes, type(dec_text)) self.assertNotEqual(ciphertext, dec_text) self.assertEqual(plaintext, dec_text)
def _decrypt_key(self, enc_key: bytes, enc_symm_key: bytes, privkey: bytes = None) -> bytes: """ Decrypts the encapsulated `enc_key` with the `privkey`, if provided. If `privkey` is None, then it uses `self.priv_key`. :param enc_key: ECIES encapsulated key :param enc_symm_key: Symmetrically encrypted key :param privkey: Private key to decrypt with (if provided) :return: Decrypted key """ privkey = privkey or self.priv_key dec_symm_key = API.ecies_decapsulate(privkey) return API.symm_decrypt(dec_symm_key, enc_symm_key)
def decrypt(self, enc_data: Tuple[bytes, bytes], privkey: bytes = None) -> bytes: """ Decrypts data using ECIES PKE. If no `privkey` is provided, it uses `self.priv_key`. :param enc_data: Tuple: (encrypted data, ECIES encapsulated key) :param privkey: Private key to decapsulate with :return: Decrypted data """ privkey = privkey or self.priv_key ciphertext, enc_key = enc_data enc_key = API.elliptic_curve.deserialize(API.PRE.ecgroup, enc_key) enc_key = API.umbral.EncryptedKey(ekey=enc_key, re_id=None) dec_key = API.ecies_decapsulate(privkey, enc_key) return API.symm_decrypt(dec_key, ciphertext)