Example #1
0
 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()
     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 ...")
Example #2
0
 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
     _, pubkey_x, pubkey_y, i2 = ECC._decode_pubkey(data[i:])
     i += i2
     ciphertext = data[i:len(data) - 32]
     i += len(ciphertext)
     mac = data[i:]
     key = sha512(self.raw_get_ecdh_key(pubkey_x, pubkey_y)).digest()
     key_e, key_m = key[:32], key[32:]
     if not equals(hmac_sha256(key_m, data[:len(data) - 32]), mac):
         raise RuntimeError("Fail to verify data")
     ctx = Cipher(key_e, iv, 0, ciphername)
     return ctx.ciphering(ciphertext)
Example #3
0
    def raw_encrypt(
        data,
        pubkey_x,
        pubkey_y,
        curve='sect283r1',
        ephemcurve=None,
        ciphername='aes-256-cbc',
    ):  # pylint: disable=too-many-arguments
        """ECHD encryption, keys supplied in binary data format"""

        if ephemcurve is None:
            ephemcurve = curve
        ephem = ECC(curve=ephemcurve)
        key = sha512(ephem.raw_get_ecdh_key(pubkey_x, pubkey_y)).digest()
        key_e, key_m = key[:32], key[32:]
        pubkey = ephem.get_pubkey()
        iv = OpenSSL.rand(OpenSSL.get_cipher(ciphername).get_blocksize())
        ctx = Cipher(key_e, iv, 1, ciphername)
        ciphertext = iv + pubkey + ctx.ciphering(data)
        mac = hmac_sha256(key_m, ciphertext)
        return ciphertext + mac
Example #4
0
 def gen_IV(ciphername):
     """Generate random initialization vector"""
     cipher = OpenSSL.get_cipher(ciphername)
     return OpenSSL.rand(cipher.get_blocksize())
Example #5
0
 def get_blocksize(ciphername):
     """This Method returns cipher blocksize"""
     cipher = OpenSSL.get_cipher(ciphername)
     return cipher.get_blocksize()