def raw_encrypt(data, pubkey_x, pubkey_y, curve='sect283r1', ephemcurve=None, ciphername='aes-256-cbc'): if ephemcurve == None: ephemcurve = curve ephem = ecc(curve=ephemcurve) key = ephem.raw_get_ecdh_key(pubkey_x, pubkey_y) pubkey = ephem.get_pubkey() iv = openssl.rand(openssl.get_cipher(ciphername).get_blocksize()) ctx = cipher(key, iv, 1, ciphername) return iv + pubkey + ctx.ciphering(data)
def decrypt(self, data, ciphername='aes-256-cbc'): """ Decrypt data with ECIES method using the local private key """ blocksize = openssl.get_cipher(ciphername).get_blocksize() iv = data[:blocksize] i = blocksize curve, pubkey_x, pubkey_y, i2 = ecc._decode_pubkey(data[i:]) i += i2 data = data[i:] key = self.raw_get_ecdh_key(pubkey_x, pubkey_y) ctx = cipher(key, iv, 0, ciphername) return ctx.ciphering(data)
def __init__(self, key, iv, do, ciphername='aes-256-cbc'): """ do == 1 => Encrypt; do == 0 => Decrypt """ self.cipher = openssl.get_cipher(ciphername) self.ctx = openssl.EVP_CIPHER_CTX_new() self.ciphertext = b"" self.size = 0 if do == 1 or do == 0: k = openssl.malloc(key, len(key)) IV = openssl.malloc(iv, len(iv)) openssl.EVP_CipherInit_ex(self.ctx, self.cipher.get_pointer(), 0, k, IV, do) else: raise Exception("RTFM ...")
def gen_IV(ciphername): cipher = openssl.get_cipher(ciphername) return openssl.rand(cipher.get_blocksize())
def get_blocksize(ciphername): cipher = openssl.get_cipher(ciphername) return cipher.get_blocksize()