o
    ge$                     @   s|   d dl mZ d dlmZmZmZ d dlmZ d dlm	Z	 e
eje
eje
eje
ejG dd deZdS )    )utils)
InvalidTagUnsupportedAlgorithm_Reasons)ciphers)modesc                   @   s   e Zd ZdZdZdZdd Zdedefdd	Zdede	fd
dZ
defddZdedefddZdeddfddZedZdS )_CipherContext   r   i?c                 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|}	|d urx|	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&r=| j j
'|| j j
j(t$|
| j jj}| j "|dk |j)d u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 "|dk | j j
+|d || _,d S )N   r	   z6cipher {} in {} mode is not supported by this backend.zcipher {0.name} zin {0.name} mode z_is not supported by this backend (Your version of OpenSSL may be too old. Current version: {}.)r   )-_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nonce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EVP_CIPHER_CTX_set_padding_ctx)selfbackendciphermode	operationctxregistryadapter
evp_ciphermsgiv_nonceres r@   N/usr/lib/python3/dist-packages/cryptography/hazmat/backends/openssl/ciphers.py__init__   s   
	



z_CipherContext.__init__datareturnc                 C   s2   t t|| j d }| ||}t|d | S )Nr	   )	bytearrayr+   r   update_intobytes)r4   rC   bufnr@   r@   rA   updatey   s   z_CipherContext.updatec                 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rgt| jtjrgt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.)r+   r   
ValueErrorr   r   r   newr"   min_MAX_CHUNK_SIZEr   EVP_CipherUpdater3   r   r   r   XTSr)   )r4   rC   rH   total_data_lendata_processed	total_outoutlen
baseoutbuf	baseinbufoutbufinbufinlenr?   r@   r@   rA   rF   ~   s6   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ro| j }|sDt| jtjrDt| jj|d | jjj| jjjpg| jjjog|d | jjj| jjj|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[]rK   r   )errorszFThe length of the provided data is not a multiple of the block length.r	   )r   _DECRYPTr   r   r   ModeWithAuthenticationTagr0   rM   r   r   rN   r   r   EVP_CipherFinal_exr3   _consume_errorsr-   r   r)   _lib_reason_matchERR_LIB_EVP'EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTHCryptography_HAS_PROVIDERSERR_LIB_PROVPROV_R_WRONG_FINAL_BLOCK_LENGTH_ENCRYPTr.   EVP_CTRL_AEAD_GET_TAGbufferr   EVP_CIPHER_CTX_reset)r4   rH   rV   r?   r\   tag_bufr@   r@   rA   finalize   s\   


z_CipherContext.finalizer0   c                 C   s`   t || jjk rtd| jj| jj| j| jjj	t ||}| j
|dk || _|  S )Nz.Authentication tag must be {} bytes or longer.r   )r+   r   _min_tag_lengthrM   r   r   r   r.   r3   r1   r)   r   rl   )r4   r0   r?   r@   r@   rA   finalize_with_tag   s   z _CipherContext.finalize_with_tagNc                 C   sN   | j jd}| j j| j| j jj|| j j|t|}| j 	|dk d S )NrK   r   )
r   r   rN   r   rQ   r3   r   r"   r+   r)   )r4   rC   rV   r?   r@   r@   rA   authenticate_additional_data   s   z+_CipherContext.authenticate_additional_datar   )__name__
__module____qualname__rg   r]   rP   rB   rG   rJ   intrF   rl   rn   ro   r   read_only_propertyr0   r@   r@   r@   rA   r      s    d":r   N)cryptographyr   cryptography.exceptionsr   r   r   cryptography.hazmat.primitivesr   &cryptography.hazmat.primitives.ciphersr   register_interfaceCipherContextAEADCipherContextAEADEncryptionContextAEADDecryptionContextobjectr   r@   r@   r@   rA   <module>   s   



