File: //lib/python3/dist-packages/cryptography/hazmat/backends/openssl/__pycache__/ciphers.cpython-38.pyc
U
�\�ey$ � @ s� d dl mZmZmZ d dlmZ d dlmZmZm Z d dl
mZ d dlm
Z
e�ej�e�ej�e�ej�e�ej�G dd� de�����ZdS ) � )�absolute_import�division�print_function)�utils)�
InvalidTag�UnsupportedAlgorithm�_Reasons)�ciphers)�modesc @ sN e Zd ZdZdZdd� Zdd� Zdd� Zd d
� Zdd� Z d
d� Z
e�d�Z
dS )�_CipherContext� r c C s || _ || _|| _|| _d | _t| jtj�r<| jjd | _ nd| _ | j j
�� }| j j�
|| j j
j�}| j j}z|t|�t|�f }W n4 tk
r� td�|j|r�|jn|�tj��Y nX || j ||�}|| j jjk�rd�|�} |d k r�| d�|�7 } | d�| j �� �7 } t| tj��t|tj��r8| j j�|j�}
njt|tj��rX| j j�|j�}
nJt|tj��rx| j j�|j �}
n*t|tj��r�| j j�|j �}
n
| j jj}
| j j
�!||| j jj| j jj| j jj|�}| j �"|dk� | j j
�#|t$|j%��}| j �"|dk� t|tj&��r�| j j
�'|| j j
j(t$|
�| j jj�}| j �"|dk� |j)d k �r�| j j
�'|| j j
j*t$|j)�|j)�}| j �"|dk� |j)| _n.| j| j+k�r�| j j
j,�r�| j j
j-�s�t.d��| j j
�!|| j jj| j jj| j j�|j%�|
|�}| j �"|dk� | j j
�/|d� || _0d S ) N� r z6cipher {} in {} mode is not supported by this backend.zcipher {0.name} zin {0.name} mode z_is not supported by this backend (Your version of OpenSSL may be too old. Current version: {}.)r z_delayed passing of GCM tag requires OpenSSL >= 1.0.2. To use this feature please update OpenSSL)1�_backendZ_cipher�_mode�
_operation�_tag�
isinstancer ZBlockCipherAlgorithmZ
block_size�_block_size_bytes�_libZEVP_CIPHER_CTX_new�_ffi�gcZEVP_CIPHER_CTX_freeZ_cipher_registry�type�KeyErrorr �format�namer ZUNSUPPORTED_CIPHER�NULLZopenssl_version_textr
ZModeWithInitializationVector�from_bufferZinitialization_vectorZ
ModeWithTweakZtweakZ
ModeWithNonceZnonceZEVP_CipherInit_ex�openssl_assertZEVP_CIPHER_CTX_set_key_length�len�key�GCM�EVP_CIPHER_CTX_ctrlZEVP_CTRL_AEAD_SET_IVLEN�tag�EVP_CTRL_AEAD_SET_TAG�_DECRYPT�"CRYPTOGRAPHY_OPENSSL_LESS_THAN_102�CRYPTOGRAPHY_IS_LIBRESSL�NotImplementedErrorZEVP_CIPHER_CTX_set_padding�_ctx)�selfZbackendZcipher�modeZ operationZctx�registryZadapterZ
evp_cipher�msgZiv_nonce�res� r. �N/usr/lib/python3/dist-packages/cryptography/hazmat/backends/openssl/ciphers.py�__init__ s� � ��
���
� � � �
�����z_CipherContext.__init__c C s2 t t|�| j d �}| �||�}t|d |� �S )Nr )� bytearrayr r �update_into�bytes)r) �data�buf�nr. r. r/ �updatez s z_CipherContext.updatec C s� t |�t |�| j d k r6td�t |�| j d ���| jj�d| jjj|dd��}| jj�d�}| jj �
| j||| jj�|�t |��}| j�|dk� |d S )Nr z1buffer must be at least {} bytes for this payloadzunsigned char *T)Zrequire_writable�int *r )
r r �
ValueErrorr r r �castr �newr �EVP_CipherUpdater( r )r) r4 r5 �outlenr- r. r. r/ r2 s( �� � �z_CipherContext.update_intoc C s| t | jtj�r| �d� | j| jkrDt | jtj�rD| jd krDt d��| j
j�d| j
�}| j
j�d�}| j
j�| j||�}|dkr�| j
�� }|s�t | jtj�r�t�| j
�|d �| j
jj| j
jj�� t d��t | jtj��rB| j| jk�rB| j
j�d| j
�}| j
j�| j| j
jj| j
|�}| j
�|dk� | j
j�|�d d � | _| j
j�| j�}| j
�|dk� | j
j�|�d |d � S )N� z4Authentication tag must be provided when decrypting.zunsigned char[]r8 r zFThe length of the provided data is not a multiple of the block length.r )r r r
r r7 r r$ ZModeWithAuthenticationTagr"