o
    b'                     @   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   s8  || _ || _|| _|| _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 + }| j j
}|dkr|j,rr|d -|j.|j/s|j0r|d -|j1|j2rt3d| j j"|dk|d	 | j j
4|d || _5d 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   z+In XTS mode duplicated keys are not allowederrors)6_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nonceZ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)selfbackendciphermodeZ	operationctxregistryadapterZ
evp_ciphermsgZiv_nonceresr   lib r6   k/var/www/secure340b-portal/env/lib/python3.10/site-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)r,   r9   bufnr6   r6   r7   update   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j|}||kr|| }	|| }
t| j	|| }| jj
| j|	||
|}|dkrjt| jtjrj| j  td| j|dk ||7 }||d 7 }||ks:|S )Nr	   z1buffer must be at least {} bytes for this payloadr   int *zeIn 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_CipherUpdater+   r   r   r   ZXTSr%   r   )r,   r9   r>   Ztotal_data_lenZdata_processedZ	total_outoutlenZ
baseoutbufZ	baseinbufZoutbufZinbufZinlenr4   r6   r6   r7   r<      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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[]rA   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   rB   r   r   ZEVP_CipherFinal_exr+   r%   r!   r   r   r&   r'   Z'EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTHr(   r)   ZPROV_R_WRONG_FINAL_BLOCK_LENGTH_ENCRYPTr"   ZEVP_CTRL_AEAD_GET_TAGbufferr   ZEVP_CIPHER_CTX_reset)r,   r>   rF   r4   r   Ztag_bufr6   r6   r7   finalize   sb   



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   )r   r   Z_min_tag_lengthr*   r   r   r   r   r"   r+   r$   r   r   rJ   )r,   r#   Ztag_lenr4   r6   r6   r7   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 )NrA   r   )
r   r   rB   r   rE   r+   r   r   r   r   )r,   r9   rF   r4   r6   r6   r7   authenticate_additional_data  s   z+_CipherContext.authenticate_additional_datar   )__name__
__module____qualname__rH   rG   rD   r8   r=   r@   intr<   rJ   rK   rL   r   Zread_only_propertyr#   r6   r6   r6   r7   r      s    y#:r   N)Zcryptographyr   Zcryptography.exceptionsr   r   r   Zcryptography.hazmat.primitivesr   Z&cryptography.hazmat.primitives.ciphersr   Zregister_interfaceZCipherContextZAEADCipherContextZAEADEncryptionContextZAEADDecryptionContextobjectr   r6   r6   r6   r7   <module>   s   



