3 ꅭh�!�@s�ddlmZmZmZddlmZddlmZmZm Z ddl m Z ddl m Z eje j�eje j�eje j�eje j�Gdd�de�����ZdS) �)�absolute_import�division�print_function)�utils)� InvalidTag�UnsupportedAlgorithm�_Reasons)�ciphers)�modesc@sReZdZdZdZdZdd�Zdd�Zd d �Zd d �Z d d�Z dd�Z e j d�ZdS)�_CipherContext�r��c Cs�||_||_||_||_d|_t|jtj�r<|jjd|_ nd|_ |jj j �}|jj j ||jj j�}|jj}y|t|�t|�f}Wn4tk r�tdj|j|r�|jn|�tj��YnX||j||�}||jj jk�rdj|�} |dk r�| dj|�7} | dj|jj��7} t| tj��t|tj��r8|jj j|j�} njt|tj��rX|jj j|j�} nJt|tj��rx|jj j|j �} n*t|tj��r�|jj j|j �} n |jj j} |jj j!|||jj j|jj j|jj j|�} |jj"| dk�|jj j#|t$|j%��} |jj"| dk�t|tj&��r�|jj j'||jj j(t$| �|jj j�} |jj"| dk�|j)dk �r�|jj j'||jj j*t$|j)�|j)�} |jj"| dk�|j)|_|jj j!||jj j|jj j|jj j|j%�| |�} |jj"| dk�|jj j+|d�||_,dS)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)-�_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�namerZUNSUPPORTED_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_TAGZEVP_CIPHER_CTX_set_padding�_ctx) �selfZbackendZcipher�modeZ operationZctx�registryZadapterZ evp_cipher�msgZiv_nonce�res�r,�/usr/lib64/python3.6/ciphers.py�__init__s�       z_CipherContext.__init__cCs2tt|�|jd�}|j||�}t|d|��S)Nr )� bytearrayr r� update_into�bytes)r'�data�buf�nr,r,r-�updatezs z_CipherContext.updatec Cs�t|�}t|�||jdkr:tdjt|�|jd���d}d}|jjjd�}|jjj|dd�}|jjj|�}xh||kr�||} ||} t|j ||�} |jj j |j | || | �} |jj | dk�|| 7}||d7}qrW|S)Nr z1buffer must be at least {} bytes for this payloadrzint *T)Zrequire_writable)r r� ValueErrorrrr�newr�min�_MAX_CHUNK_SIZEr�EVP_CipherUpdater&r) r'r2r3Ztotal_data_lenZdata_processedZ total_out�outlenZ baseoutbufZ baseinbufZoutbufZinbufZinlenr+r,r,r-r0s( z_CipherContext.update_intocCsh|j|jkr,t|jtj�r,|jdkr,td��|jj j d|j �}|jj j d�}|jj j |j||�}|dkr�|jj�}| r�t|jtj�r�t�|jj|dj|jj j|jj j�|d�td��t|jtj�o�|j|jk�r.|jj j d|j �}|jj j|j|jj j|j |�}|jj|dk�|jj j|�dd�|_|jj j|j�}|jj|dk�|jj j|�d|d�S)Nz4Authentication tag must be provided when decrypting.zunsigned char[]zint *r)�errorszFThe length of the provided data is not a multiple of the block length.r )r�_DECRYPTrrr ZModeWithAuthenticationTagr$r6rrr7rrZEVP_CipherFinal_exr&Z_consume_errorsr"rrZ_lib_reason_matchZ ERR_LIB_EVPZ'EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH�_ENCRYPTr#ZEVP_CTRL_AEAD_GET_TAG�bufferrZEVP_CIPHER_CTX_cleanup)r'r3r;r+r<Ztag_bufr,r,r-�finalize�sB     z_CipherContext.finalizecCs`t|�|jjkr"tdj|jj���|jjj|j|jjj t|�|�}|jj |dk�||_ |j �S)Nz.Authentication tag must be {} bytes or longer.r) r rZ_min_tag_lengthr6rrrr#r&r%rrr@)r'r$r+r,r,r-�finalize_with_tag�s z _CipherContext.finalize_with_tagcCsN|jjjd�}|jjj|j|jjj||jjj|�t|��}|jj |dk�dS)Nzint *r) rrr7rr:r&rrr r)r'r2r;r+r,r,r-�authenticate_additional_data�s  z+_CipherContext.authenticate_additional_datarNi@i���?)�__name__� __module__� __qualname__r>r=r9r.r5r0r@rArBrZread_only_propertyr$r,r,r,r-r sd3 r N)Z __future__rrrZ cryptographyrZcryptography.exceptionsrrrZcryptography.hazmat.primitivesr Z&cryptography.hazmat.primitives.ciphersr Zregister_interfaceZ CipherContextZAEADCipherContextZAEADEncryptionContextZAEADDecryptionContext�objectr r,r,r,r-�s