o
    ev(                     @  sh   d dl mZ 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r+d dlmZ G dd dZdS )	    )annotationsN)
InvalidTagUnsupportedAlgorithm_Reasons)ciphers)
algorithmsmodes)Backendc                   @  sb   e Zd ZdZdZdZdd
dZdddZd ddZd!ddZ	d"ddZ
d#ddZed$ddZdS )%_CipherContext   r   i?backendr	   	operationintreturnNonec                 C  s@  || _ || _|| _|| _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 n tyY   td|j|rS|jn|tjw || j ||}|| j jjkrd|j d}	|d ur||	d|j d7 }	|	d| j  7 }	t|	tjt|tjr| j j|j}
n2t|tjr| j j|j}
n#t|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(rA| j j
)|| j j
j*t&|
| j jj}| j $|d	k |j+d urA| 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.sv|d	 /|j0|j1s|j2r|d	 /|j3|j4rt5d
| 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_backend_cipher_mode
_operation_tag
isinstancer   BlockCipherAlgorithm
block_size_block_size_bytes_libEVP_CIPHER_CTX_new_ffigcEVP_CIPHER_CTX_free_cipher_registrytypeKeyErrorr   formatnamer   UNSUPPORTED_CIPHERNULLopenssl_version_textr   ModeWithInitializationVectorfrom_bufferinitialization_vectorModeWithTweaktweakModeWithNoncenoncer   ChaCha20EVP_CipherInit_exopenssl_assertEVP_CIPHER_CTX_set_key_lengthlenkeyGCMEVP_CIPHER_CTX_ctrlEVP_CTRL_AEAD_SET_IVLENtagEVP_CTRL_AEAD_SET_TAG_consume_errorsCRYPTOGRAPHY_IS_LIBRESSL_lib_reason_matchERR_LIB_EVPEVP_R_XTS_DUPLICATED_KEYSCryptography_HAS_PROVIDERSERR_LIB_PROVPROV_R_XTS_DUPLICATED_KEYS
ValueErrorEVP_CIPHER_CTX_set_padding_ctx)selfr   ciphermoder   ctxregistryadapter
evp_ciphermsgiv_nonceresr   lib rS   _/var/www/html/venv/lib/python3.10/site-packages/cryptography/hazmat/backends/openssl/ciphers.py__init__   s   
	




	
z_CipherContext.__init__databytesc                 C  s2   t t|| j d }| ||}t|d | S )Nr   )	bytearrayr6   r   update_intorW   )rH   rV   bufnrS   rS   rT   update   s   z_CipherContext.updaterZ   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rlt| jtjrl| j  td| j|dk ||7 }||d 7 }||ks<|S )Nr   z1buffer must be at least {} bytes for this payloadr   int *T)require_writablezeIn XTS mode you must supply at least a full block in the first update call. For AES this is 16 bytes.)r6   r   rE   r&   r   r    newr,   min_MAX_CHUNK_SIZEr   EVP_CipherUpdaterG   r   r   r   XTSr=   r4   )rH   rV   rZ   total_data_lendata_processed	total_outoutlen
baseoutbuf	baseinbufoutbufinbufinlenrQ   rS   rS   rT   rY      s8   
z_CipherContext.update_intoc                 C  s  | j | jkrt| jtjr| jd u rtd| jj	
d| j}| jj	
d}| jj| j||}|dkrt| j }|sDt| jtjrDt| jj}| jj|d |j|jpl|joa|d |j|jpl|jol|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[]r]   r   r   zFThe length of the provided data is not a multiple of the block length.r   )"r   _DECRYPTr   r   r   ModeWithAuthenticationTagr;   rE   r   r    r_   r   r   EVP_CipherFinal_exrG   r=   r8   r   r4   r?   r@   'EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTHrB   rC   PROV_R_WRONG_FINAL_BLOCK_LENGTHCRYPTOGRAPHY_IS_BORINGSSLreason*CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH_ENCRYPTr9   EVP_CTRL_AEAD_GET_TAGbufferr   EVP_CIPHER_CTX_reset)rH   rZ   rg   rQ   r   rR   tag_bufrS   rS   rT   finalize   sh   

z_CipherContext.finalizer;   c                 C  s~   t |}|| jjk rtd| jj|| jkr t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   )r6   r   _min_tag_lengthrE   r&   r   r   r   r9   rG   r<   r4   r   rz   )rH   r;   tag_lenrQ   rS   rS   rT   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 )Nr]   r   )
r   r    r_   r   rb   rG   r)   r,   r6   r4   )rH   rV   rg   rQ   rS   rS   rT   authenticate_additional_data  s   z+_CipherContext.authenticate_additional_datatyping.Optional[bytes]c                 C  s   | j S )N)r   )rH   rS   rS   rT   r;     s   z_CipherContext.tagN)r   r	   r   r   r   r   )rV   rW   r   rW   )rV   rW   rZ   rW   r   r   )r   rW   )r;   rW   r   rW   )rV   rW   r   r   )r   r   )__name__
__module____qualname__ru   rm   ra   rU   r\   rY   rz   r}   r~   propertyr;   rS   rS   rS   rT   r
      s    

y

#
@
r
   )
__future__r   typingcryptography.exceptionsr   r   r   cryptography.hazmat.primitivesr   &cryptography.hazmat.primitives.ciphersr   r   TYPE_CHECKING,cryptography.hazmat.backends.openssl.backendr	   r
   rS   rS   rS   rT   <module>   s   