a
    ݌xdM                      @   s:  d dl Z d dlZd dlmZmZmZ d dlmZ d dlm	Z	 ej
rNd dlmZ G dd de jdZG d	d
 d
ee jdZG dd dee jdZG dd dee jdZejdeje	j ddZG dd deje Zeeje	je	jde	je	jf  ZG dd deZG dd deZG dd deeZG dd deeZ dS )    N)AlreadyFinalizedAlreadyUpdatedNotYetFinalized)CipherAlgorithm)modes)_CipherContextc                   @   sN   e Zd ZejeedddZejeeedddZejeddd	Z	d
S )CipherContextdatareturnc                 C   s   dS )zk
        Processes the provided bytes through the cipher and returns the results
        as bytes.
        N selfr
   r   r   c/var/www/html/Ranjet/env/lib/python3.9/site-packages/cryptography/hazmat/primitives/ciphers/base.pyupdate   s    zCipherContext.updater
   bufr   c                 C   s   dS )z
        Processes the provided bytes and writes the resulting data into the
        provided buffer. Returns the number of bytes written.
        Nr   r   r
   r   r   r   r   update_into   s    zCipherContext.update_intor   c                 C   s   dS )zM
        Returns the results of processing the final block as bytes.
        Nr   r   r   r   r   finalize&   s    zCipherContext.finalizeN)
__name__
__module____qualname__abcabstractmethodbytesr   intr   r   r   r   r   r   r      s   r   )	metaclassc                   @   s"   e Zd ZejeddddZdS )AEADCipherContextNr	   c                 C   s   dS )z3
        Authenticates the provided bytes.
        Nr   r   r   r   r   authenticate_additional_data.   s    z.AEADCipherContext.authenticate_additional_data)r   r   r   r   r   r   r!   r   r   r   r   r    -   s   r    c                   @   s"   e Zd ZejeedddZdS )AEADDecryptionContexttagr   c                 C   s   dS )z
        Returns the results of processing the final block as bytes and allows
        delayed passing of the authentication tag.
        Nr   )r   r$   r   r   r   finalize_with_tag6   s    z'AEADDecryptionContext.finalize_with_tagN)r   r   r   r   r   r   r%   r   r   r   r   r"   5   s   r"   c                   @   s$   e Zd ZeejedddZdS )AEADEncryptionContextr   c                 C   s   dS )zb
        Returns tag bytes. This is only available after encryption is
        finalized.
        Nr   r   r   r   r   r$   ?   s    zAEADEncryptionContext.tagN)r   r   r   propertyr   r   r   r$   r   r   r   r   r&   >   s   r&   ModeT)bound	covariantc                   @   s   e Zd ZdeeejddddZejde	dddZ
ejd	edd
dZ
dd Z
ejdedddZejd	edddZdd Zdeeje	eef dddZdS )CipherN)	algorithmmodebackendr   c                 C   sD   t |tstd|d ur4t |tjs*J || || _|| _d S )Nz&Expected interface of CipherAlgorithm.)
isinstancer   	TypeErrorr   r(   Zvalidate_for_algorithmr,   r-   )r   r,   r-   r.   r   r   r   __init__N   s    

zCipher.__init__z'Cipher[modes.ModeWithAuthenticationTag])r   r   c                 C   s   d S Nr   r   r   r   r   	encryptor`   s    zCipher.encryptor_CIPHER_TYPEc                 C   s   d S r2   r   r   r   r   r   r3   f   s    c                 C   sL   t | jtjr"| jjd ur"tdddlm} || j	| j}| j
|ddS )Nz0Authentication tag must be None when encrypting.r   r.   Tencrypt)r/   r-   r   ModeWithAuthenticationTagr$   
ValueError,cryptography.hazmat.backends.openssl.backendr.   Zcreate_symmetric_encryption_ctxr,   	_wrap_ctxr   r.   ctxr   r   r   r3   l   s    c                 C   s   d S r2   r   r   r   r   r   	decryptory   s    zCipher.decryptorc                 C   s   d S r2   r   r   r   r   r   r>      s    c                 C   s*   ddl m} || j| j}| j|ddS )Nr   r5   Fr6   )r:   r.   Zcreate_symmetric_decryption_ctxr,   r-   r;   r<   r   r   r   r>      s
    _BackendCipherContext)r=   r7   r   c                 C   s0   t | jtjr$|rt|S t|S nt|S d S r2   )r/   r-   r   r8   _AEADEncryptionContext_AEADDecryptionContextr   )r   r=   r7   r   r   r   r;      s
    
zCipher._wrap_ctx)N)r   r   r   r   r(   typingAnyr1   overloadr&   r3   r   r"   r>   boolUnionr;   r   r   r   r   r+   M   s<    	r+   c                   @   s\   e Zd ZU ejd ed< dddddZeeddd	Zeee	d
ddZ
edddZdS )r   r?   _ctxNr=   r   c                 C   s
   || _ d S r2   )rG   r   r=   r   r   r   r1      s    z_CipherContext.__init__r	   c                 C   s   | j d u rtd| j |S NContext was already finalized.)rG   r   r   r   r   r   r   r      s    
z_CipherContext.updater   c                 C   s    | j d u rtd| j ||S rJ   )rG   r   r   r   r   r   r   r      s    
z_CipherContext.update_intor   c                 C   s&   | j d u rtd| j  }d | _ |S rJ   )rG   r   r   r   r   r   r   r      s
    

z_CipherContext.finalize)r   r   r   rB   Optional__annotations__r1   r   r   r   r   r   r   r   r   r   r      s
   
r   c                   @   s   e Zd ZU ejd ed< eje ed< dddddZ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ddddZdS )_AEADCipherContextr?   rG   _tagNrH   c                 C   s"   || _ d| _d| _d | _d| _d S )Nr   F)rG   _bytes_processed_aad_bytes_processedrO   _updatedrI   r   r   r   r1      s
    z_AEADCipherContext.__init__)	data_sizer   c                 C   sV   | j d u rtdd| _|  j|7  _| j| j jjkrRtd| j jj| j jjd S )NrK   Tz+{} has a maximum encrypted byte limit of {})	rG   r   rR   rP   _modeZ_MAX_ENCRYPTED_BYTESr9   formatname)r   rS   r   r   r   _check_limit   s    
z_AEADCipherContext._check_limitr	   c                 C   s(   |  t| | jd usJ | j|S r2   )rW   lenrG   r   r   r   r   r   r      s    z_AEADCipherContext.updater   c                 C   s*   |  t| | jd usJ | j||S r2   )rW   rX   rG   r   r   r   r   r   r      s    z_AEADCipherContext.update_intor   c                 C   s0   | j d u rtd| j  }| j j| _d | _ |S rJ   )rG   r   r   r$   rO   r   r   r   r   r      s    


z_AEADCipherContext.finalizec                 C   sn   | j d u rtd| jr td|  jt|7  _| j| j jjkr^td	| j jj
| j jj| j | d S )NrK   z'Update has been called on this context.z%{} has a maximum AAD byte limit of {})rG   r   rR   r   rQ   rX   rT   Z_MAX_AAD_BYTESr9   rU   rV   r!   r   r   r   r   r!      s    
z/_AEADCipherContext.authenticate_additional_data)r   r   r   rB   rL   rM   r   r1   r   rW   r   r   r   r!   r   r   r   r   rN      s   
rN   c                   @   s   e Zd ZeedddZdS )rA   r#   c                 C   s2   | j d u rtd| j |}| j j| _d | _ |S rJ   )rG   r   r%   r$   rO   )r   r$   r
   r   r   r   r%      s    

z(_AEADDecryptionContext.finalize_with_tagN)r   r   r   r   r%   r   r   r   r   rA      s   rA   c                   @   s   e Zd ZeedddZdS )r@   r   c                 C   s&   | j d urtd| jd us J | jS )Nz4You must finalize encryption before getting the tag.)rG   r   rO   r   r   r   r   r$     s    
z_AEADEncryptionContext.tagN)r   r   r   r'   r   r$   r   r   r   r   r@     s   r@   )!r   rB   Zcryptography.exceptionsr   r   r   Z/cryptography.hazmat.primitives._cipheralgorithmr   Z&cryptography.hazmat.primitives.ciphersr   TYPE_CHECKINGZ,cryptography.hazmat.backends.openssl.ciphersr   r?   ABCMetar   r    r"   r&   TypeVarrL   r(   Genericr+   rF   ZModeWithNonceZModeWithTweakZECBZModeWithInitializationVectorr4   rN   rA   r@   r   r   r   r   <module>   s8   	
N<
