def __init__(self, cipher_name, key, iv, op, crypto_path=None): if not loaded: load_libsodium(crypto_path) AeadCryptoBase.__init__(self, cipher_name, key, iv, op, crypto_path) if cipher_name == 'chacha20-poly1305': self.encryptor = libsodium.crypto_aead_chacha20poly1305_encrypt self.decryptor = libsodium.crypto_aead_chacha20poly1305_decrypt elif cipher_name == 'chacha20-ietf-poly1305': self.encryptor = libsodium. \ crypto_aead_chacha20poly1305_ietf_encrypt self.decryptor = libsodium. \ crypto_aead_chacha20poly1305_ietf_decrypt elif cipher_name == 'xchacha20-ietf-poly1305': if hasattr(libsodium, 'crypto_aead_xchacha20poly1305_ietf_encrypt'): self.encryptor = libsodium. \ crypto_aead_xchacha20poly1305_ietf_encrypt self.decryptor = libsodium. \ crypto_aead_xchacha20poly1305_ietf_decrypt else: raise Exception('Unsupported cipher') elif cipher_name == 'sodium:aes-256-gcm': if hasattr(libsodium, 'crypto_aead_aes256gcm_encrypt'): self.encryptor = libsodium.crypto_aead_aes256gcm_encrypt self.decryptor = libsodium.crypto_aead_aes256gcm_decrypt else: raise Exception('Unsupported cipher') else: raise Exception('Unknown cipher')
def __init__(self, cipher_name, key, iv, op, crypto_path=None): OpenSSLCryptoBase.__init__(self, cipher_name, crypto_path) AeadCryptoBase.__init__(self, cipher_name, key, iv, op, crypto_path) key_ptr = c_char_p(self._skey) r = libcrypto.EVP_CipherInit_ex( self._ctx, self._cipher, None, key_ptr, None, c_int(op) ) if not r: self.clean() raise Exception('can not initialize cipher context') r = libcrypto.EVP_CIPHER_CTX_ctrl( self._ctx, c_int(EVP_CTRL_AEAD_SET_IVLEN), c_int(self._nlen), None ) if not r: self.clean() raise Exception('Set ivlen failed') self.cipher_ctx_init()
def __init__(self, cipher_name, key, iv, op, crypto_path=None): global tag_buf_size, tag_buf if cipher_name[:len('cng:')] == 'cng:': cipher_name = cipher_name[len('cng:'):] AeadCryptoBase.__init__(self, cipher_name, key, iv, op, crypto_path) CNGCryptBase.__init__(self, cipher_name, self._skey, iv, op, crypto_path) # reserve buffer for tag if tag_buf_size < self._tlen: tag_buf_size = self._tlen * 2 tag_buf = create_string_buffer(tag_buf_size) # in place of macro BCRYPT_INIT_AUTH_MODE_INFO self._auth_cipher_mode_info = BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO( sizeof(BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO), BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO_VERSION, ) # further setup _auth_cipher_mode_info self._auth_cipher_mode_info.pbNonce = cast(byref(self._nonce), PUCHAR) self._auth_cipher_mode_info.cbNonce = self._nlen self._auth_cipher_mode_info.cbTag = self._tlen self.encrypt_once = self.aead_encrypt self.decrypt_once = self.aead_decrypt
def cipher_ctx_init(self): """ Need init cipher context after EVP_CipherFinal_ex to reuse context :return: None """ iv_ptr = c_char_p(self._nonce.raw) r = libcrypto.EVP_CipherInit_ex(self._ctx, None, None, None, iv_ptr, c_int(CIPHER_ENC_UNCHANGED)) if not r: self.clean() raise Exception('can not initialize cipher context') AeadCryptoBase.nonce_increment(self)
def __init__(self, cipher_name, key, iv, op): super(OpenSSLAeadCrypto, self).__init__(cipher_name) AeadCryptoBase.__init__(self, cipher_name, key, iv, op) r = libcrypto.EVP_CipherInit_ex(self._ctx, self._cipher, None, None, None, c_int(op)) if not r: self.clean() raise Exception('can not initialize cipher context') r = libcrypto.EVP_CIPHER_CTX_ctrl(self._ctx, c_int(EVP_CTRL_AEAD_SET_IVLEN), c_int(self._nlen), None) if not r: raise Exception('Set ivlen failed') self.cipher_ctx_init()
def cipher_ctx_init(self): """ Need init cipher context after EVP_CipherFinal_ex to reuse context :return: None """ iv_ptr = c_char_p(self._nonce.raw) r = libcrypto.EVP_CipherInit_ex( self._ctx, None, None, None, iv_ptr, c_int(CIPHER_ENC_UNCHANGED) ) if not r: self.clean() raise Exception('can not initialize cipher context') AeadCryptoBase.nonce_increment(self)
def __init__(self, cipher_name, key, iv, op, crypto_path=None): if cipher_name[:len('mbedtls:')] == 'mbedtls:': cipher_name = cipher_name[len('mbedtls:'):] MbedTLSCryptoBase.__init__(self, cipher_name, crypto_path) AeadCryptoBase.__init__(self, cipher_name, key, iv, op, crypto_path) key_ptr = c_char_p(self._skey) r = libmbedtls.mbedtls_cipher_setkey(byref(self._ctx), key_ptr, c_int(len(key) * 8), c_int(op)) if r: self.clean() raise Exception('can not initialize cipher context') r = libmbedtls.mbedtls_cipher_reset(byref(self._ctx)) if r: self.clean() raise Exception('can not finish preparation of mbed TLS ' 'cipher context')
def __init__(self, cipher_name, key, iv, op, crypto_path=None): if cipher_name[:len('mbedtls:')] == 'mbedtls:': cipher_name = cipher_name[len('mbedtls:'):] MbedTLSCryptoBase.__init__(self, cipher_name, crypto_path) AeadCryptoBase.__init__(self, cipher_name, key, iv, op, crypto_path) key_ptr = c_char_p(self._skey) r = libmbedtls.mbedtls_cipher_setkey( byref(self._ctx), key_ptr, c_int(len(key) * 8), c_int(op) ) if r: self.clean() raise Exception('can not initialize cipher context') r = libmbedtls.mbedtls_cipher_reset(byref(self._ctx)) if r: self.clean() raise Exception('can not finish preparation of mbed TLS ' 'cipher context')
def __init__(self, cipher_name, key, iv, op): if not loaded: load_libsodium() AeadCryptoBase.__init__(self, cipher_name, key, iv, op) if cipher_name == 'chacha20-poly1305': self.encryptor = libsodium.crypto_aead_chacha20poly1305_encrypt self.decryptor = libsodium.crypto_aead_chacha20poly1305_decrypt elif cipher_name == 'chacha20-ietf-poly1305': self.encryptor = libsodium.\ crypto_aead_chacha20poly1305_ietf_encrypt self.decryptor = libsodium.\ crypto_aead_chacha20poly1305_ietf_decrypt elif cipher_name == 'xchacha20-ietf-poly1305': if hasattr(libsodium, 'crypto_aead_xchacha20poly1305_ietf_encrypt'): self.encryptor = libsodium.\ crypto_aead_xchacha20poly1305_ietf_encrypt self.decryptor = libsodium.\ crypto_aead_xchacha20poly1305_ietf_decrypt else: raise Exception('Unknown cipher') else: raise Exception('Unknown cipher')
def cipher_ctx_init(self): """ Nonce + 1 :return: None """ AeadCryptoBase.nonce_increment(self)