U
    EZhj(                     @   s\   d dl Z d dlmZmZmZ d dlmZ d dlmZm	Z	 e j
rJd dlmZ G dd dZdS )    N)
InvalidTagUnsupportedAlgorithm_Reasons)ciphers)
algorithmsmodes)Backendc                   @   s   e Zd ZdZdZdZdeddddZeed	d
dZ	eeedddZ
edddZeedddZedd	ddZeeje dddZdS )_CipherContext   r   i?r   N)backend	operationreturnc                 C   sR  || _ || _|| _|| _d | _t| jtjr<| jjd | _	nd| _	| j j
 }| j j|| j j
j}| j j}z|t|t|f }W n4 tk
r   td|j|r|jn|tjY nX || j ||}|| j jjkr"d|j d}	|d k	r|	d|j d7 }	|	d| j  7 }	t|	tjt|tjrB| j j|j}
njt|tjrb| j j|j}
nJt|tjr| j j|j }
n*t|t!j"r| j j|j }
n
| j jj}
| j j
#||| j jj| j jj| j jj|}| j $|d	k | j j
%|t&|j'}| j $|d	k t|tj(r| j j
)|| j j
j*t&|
| j jj}| j $|d	k |j+d k	r| j j
)|| j j
j,t&|j+|j+}| j $|d	k |j+| _| j j
#|| j jj| j jj| j j|j'|
|}| j - }| j j
}|d	kr$|j.r|d	 /|j0|j1s|j2r$|d	 /|j3|j4r$t5d
| j j$|d	k|d | j j
6|d	 || _7d S )N   r
   z6cipher {} in {} mode is not supported by this backend.zcipher  zin z mode z_is not supported by this backend (Your version of OpenSSL may be too old. Current version: {}.)r   z+In XTS mode duplicated keys are not allowederrors)8_backendZ_cipher_mode
_operation_tag
isinstancer   ZBlockCipherAlgorithm
block_size_block_size_bytes_libZEVP_CIPHER_CTX_new_ffigcZEVP_CIPHER_CTX_freeZ_cipher_registrytypeKeyErrorr   formatnamer   ZUNSUPPORTED_CIPHERNULLZopenssl_version_textr   ZModeWithInitializationVectorfrom_bufferZinitialization_vectorZModeWithTweakZtweakZModeWithNoncenoncer   ZChaCha20ZEVP_CipherInit_exopenssl_assertZEVP_CIPHER_CTX_set_key_lengthlenkeyGCMEVP_CIPHER_CTX_ctrlZEVP_CTRL_AEAD_SET_IVLENtagEVP_CTRL_AEAD_SET_TAG_consume_errorsZ$CRYPTOGRAPHY_OPENSSL_111D_OR_GREATER_lib_reason_matchERR_LIB_EVPZEVP_R_XTS_DUPLICATED_KEYSCryptography_HAS_PROVIDERSERR_LIB_PROVZPROV_R_XTS_DUPLICATED_KEYS
ValueErrorZEVP_CIPHER_CTX_set_padding_ctx)selfr   ciphermoder   ctxregistryadapterZ
evp_ciphermsgZiv_nonceresr   lib r:   s/home/aprabhat/apps/x.techxrdev.in/venv/lib/python3.8/site-packages/cryptography/hazmat/backends/openssl/ciphers.py__init__   s      


 


 	 z_CipherContext.__init__)datar   c                 C   s2   t t|| j d }| ||}t|d | S )Nr
   )	bytearrayr$   r   update_intobytes)r1   r=   bufnr:   r:   r;   update   s    z_CipherContext.update)r=   rA   r   c                 C   s  t |}t ||| j d k r:tdt || j d d}d}| jjd}| jjj|dd}| jj|}||kr|| }	|| }
t| j	|| }| jj
| j|	||
|}|dkrt| jtjr| j  tdn| j|dk ||7 }||d 7 }qp|S )Nr
   z1buffer must be at least {} bytes for this payloadr   int *T)Zrequire_writablezeIn XTS mode you must supply at least a full block in the first update call. For AES this is 16 bytes.)r$   r   r/   r   r   r   newr!   min_MAX_CHUNK_SIZEr   EVP_CipherUpdater0   r   r   r   ZXTSr*   r#   )r1   r=   rA   Ztotal_data_lenZdata_processedZ	total_outoutlenZ
baseoutbufZ	baseinbufZoutbufZinbufZinlenr8   r:   r:   r;   r?      s@    
    
z_CipherContext.update_into)r   c                 C   s  | j | jkr,t| jtjr,| jd kr,td| jj	
d| j}| jj	
d}| jj| j||}|dkr| j }|st| jtjrt| jj}| jj|d |j|jp|jr|d |j|jp|jo|d j|jk|d tdt| jtjr^| j | jkr^| jj	
d| j}| jj| j| jjj| j|}| j|dk | jj	|d d  | _ | jj!| j}| j|dk | jj	|d |d  S )Nz4Authentication tag must be provided when decrypting.zunsigned char[]rD   r   r   zFThe length of the provided data is not a multiple of the block length.r
   )"r   _DECRYPTr   r   r   ZModeWithAuthenticationTagr(   r/   r   r   rE   r   r   ZEVP_CipherFinal_exr0   r*   r&   r   r#   r+   r,   Z'EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTHr-   r.   ZPROV_R_WRONG_FINAL_BLOCK_LENGTHZCRYPTOGRAPHY_IS_BORINGSSLreasonZ*CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH_ENCRYPTr'   ZEVP_CTRL_AEAD_GET_TAGbufferr   ZEVP_CIPHER_CTX_reset)r1   rA   rI   r8   r   r9   Ztag_bufr:   r:   r;   finalize   sp    


 z_CipherContext.finalize)r(   r   c                 C   s   t |}|| jjk r(td| jjn|| jkrBtd| j| jj| j	| jjj
t ||}| j|dk || _|  S )Nz.Authentication tag must be {} bytes or longer.z0Authentication tag cannot be more than {} bytes.r   )r$   r   Z_min_tag_lengthr/   r   r   r   r   r'   r0   r)   r#   r   rN   )r1   r(   Ztag_lenr8   r:   r:   r;   finalize_with_tag   s,    
   z _CipherContext.finalize_with_tagc                 C   sN   | j jd}| j j| j| j jj|| j j|t|}| j 	|dk d S )NrD   r   )
r   r   rE   r   rH   r0   r    r!   r$   r#   )r1   r=   rI   r8   r:   r:   r;   authenticate_additional_data  s    z+_CipherContext.authenticate_additional_datac                 C   s   | j S )N)r   )r1   r:   r:   r;   r(     s    z_CipherContext.tag)__name__
__module____qualname__rL   rJ   rG   intr<   r@   rC   r?   rN   rO   rP   propertytypingOptionalr(   r:   r:   r:   r;   r	      s    {#@r	   )rV   Zcryptography.exceptionsr   r   r   Zcryptography.hazmat.primitivesr   Z&cryptography.hazmat.primitives.ciphersr   r   TYPE_CHECKINGZ,cryptography.hazmat.backends.openssl.backendr   r	   r:   r:   r:   r;   <module>   s   