o
    ÖèFhý   ã                   @  sF  d dl mZ 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r-d 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 )é    )ÚannotationsN)ÚAlreadyFinalizedÚAlreadyUpdatedÚNotYetFinalized)ÚCipherAlgorithm)Úmodes)Ú_CipherContextc                   @  s<   e Zd Zejddd„ƒZejddd	„ƒZejdd
d„ƒZdS )ÚCipherContextÚdataÚbytesÚreturnc                 C  ó   dS )zk
        Processes the provided bytes through the cipher and returns the results
        as bytes.
        N© ©Úselfr
   r   r   úd/var/www/html/loop/nvenv/lib/python3.10/site-packages/cryptography/hazmat/primitives/ciphers/base.pyÚupdate   ó    zCipherContext.updateÚbufÚintc                 C  r   )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    r   zCipherContext.update_intoc                 C  r   )zM
        Returns the results of processing the final block as bytes.
        Nr   ©r   r   r   r   Úfinalize'   r   zCipherContext.finalizeN©r
   r   r   r   ©r
   r   r   r   r   r   ©r   r   )Ú__name__Ú
__module__Ú__qualname__ÚabcÚabstractmethodr   r   r   r   r   r   r   r	      s    r	   )Ú	metaclassc                   @  s   e Zd Zejddd„ƒZdS )	ÚAEADCipherContextr
   r   r   ÚNonec                 C  r   )z3
        Authenticates the provided bytes.
        Nr   r   r   r   r   Úauthenticate_additional_data/   r   z.AEADCipherContext.authenticate_additional_dataN©r
   r   r   r$   )r   r   r   r    r!   r%   r   r   r   r   r#   .   ó    r#   c                   @  s   e Zd Zejddd„ƒZdS )ÚAEADDecryptionContextÚtagr   r   c                 C  r   )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_tag7   r   z'AEADDecryptionContext.finalize_with_tagN©r)   r   r   r   )r   r   r   r    r!   r*   r   r   r   r   r(   6   r'   r(   c                   @  s    e Zd Zeejddd„ƒƒZdS )ÚAEADEncryptionContextr   r   c                 C  r   )zb
        Returns tag bytes. This is only available after encryption is
        finalized.
        Nr   r   r   r   r   r)   @   r   zAEADEncryptionContext.tagNr   )r   r   r   Úpropertyr    r!   r)   r   r   r   r   r,   ?   s    r,   ÚModeT)ÚboundÚ	covariantc                   @  st   e Zd Z	d!d"d
d„Zejd#dd„ƒZejd$dd„ƒZdd„ Zejd%dd„ƒZejd$dd„ƒZdd„ Zd&dd „ZdS )'ÚCipherNÚ	algorithmr   Úmoder.   Úbackendú
typing.Anyr   r$   c                 C  sD   t |tƒs	tdƒ‚|d urt |tjƒsJ ‚| |¡ || _|| _d S )Nz&Expected interface of CipherAlgorithm.)Ú
isinstancer   Ú	TypeErrorr   r.   Úvalidate_for_algorithmr2   r3   )r   r2   r3   r4   r   r   r   Ú__init__O   s   


zCipher.__init__r   ú'Cipher[modes.ModeWithAuthenticationTag]r,   c                 C  ó   d S ©Nr   r   r   r   r   Ú	encryptora   ó   zCipher.encryptorÚ_CIPHER_TYPEr	   c                 C  r;   r<   r   r   r   r   r   r=   g   r>   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   ©r4   T©Úencrypt)r6   r3   r   ÚModeWithAuthenticationTagr)   Ú
ValueErrorÚ,cryptography.hazmat.backends.openssl.backendr4   Úcreate_symmetric_encryption_ctxr2   Ú	_wrap_ctx©r   r4   Úctxr   r   r   r=   m   s   ÿÿr(   c                 C  r;   r<   r   r   r   r   r   Ú	decryptorz   r>   zCipher.decryptorc                 C  r;   r<   r   r   r   r   r   rJ   €   r>   c                 C  s*   ddl m} | | j| j¡}| j|dd�S )Nr   r@   FrA   )rE   r4   Úcreate_symmetric_decryption_ctxr2   r3   rG   rH   r   r   r   rJ   †   s
   ÿrI   Ú_BackendCipherContextrB   Úboolú=AEADEncryptionContext | AEADDecryptionContext | CipherContextc                 C  s*   t | jtjƒr|rt|ƒS t|ƒS t|ƒS r<   )r6   r3   r   rC   Ú_AEADEncryptionContextÚ_AEADDecryptionContextr   )r   rI   rB   r   r   r   rG   Ž   s
   zCipher._wrap_ctxr<   )r2   r   r3   r.   r4   r5   r   r$   )r   r:   r   r,   )r   r?   r   r	   )r   r:   r   r(   )rI   rL   rB   rM   r   rN   )	r   r   r   r9   ÚtypingÚoverloadr=   rJ   rG   r   r   r   r   r1   N   s    ür1   c                   @  s>   e Zd ZU ded< ddd„Zddd„Zddd„Zddd„ZdS )r   ú_BackendCipherContext | NoneÚ_ctxrI   rL   r   r$   c                 C  s
   || _ d S r<   )rT   ©r   rI   r   r   r   r9   ¨   s   
z_CipherContext.__init__r
   r   c                 C  s   | j d u r	tdƒ‚| j  |¡S ©NúContext was already finalized.)rT   r   r   r   r   r   r   r   «   s   
z_CipherContext.updater   r   c                 C  s    | j d u r	tdƒ‚| j  ||¡S rV   )rT   r   r   r   r   r   r   r   °   s   
z_CipherContext.update_intoc                 C  s&   | j d u r	tdƒ‚| j  ¡ }d | _ |S rV   )rT   r   r   r   r   r   r   r   µ   s
   

z_CipherContext.finalizeN©rI   rL   r   r$   r   r   r   )r   r   r   Ú__annotations__r9   r   r   r   r   r   r   r   r   ¥   s   
 


r   c                   @  sZ   e Zd ZU ded< ded< dd	d
„Zddd„Zddd„Zddd„Zddd„Zd dd„Z	dS )!Ú_AEADCipherContextrS   rT   zbytes | NoneÚ_tagrI   rL   r   r$   c                 C  s"   || _ d| _d| _d | _d| _d S )Nr   F)rT   Ú_bytes_processedÚ_aad_bytes_processedr[   Ú_updatedrU   r   r   r   r9   Á   s
   
z_AEADCipherContext.__init__Ú	data_sizer   c                 C  sV   | j d u r	tdƒ‚d| _|  j|7  _| j| j jjkr)td | j jj| j jj¡ƒ‚d S )NrW   Tz+{} has a maximum encrypted byte limit of {})	rT   r   r^   r\   Ú_modeÚ_MAX_ENCRYPTED_BYTESrD   ÚformatÚname)r   r_   r   r   r   Ú_check_limitÈ   s   
ÿÿÿz_AEADCipherContext._check_limitr
   r   c                 C  s(   |   t|ƒ¡ | jd usJ ‚| j |¡S r<   )rd   ÚlenrT   r   r   r   r   r   r   Ô   s   z_AEADCipherContext.updater   c                 C  s*   |   t|ƒ¡ | jd usJ ‚| j ||¡S r<   )rd   re   rT   r   r   r   r   r   r   Ú   s   z_AEADCipherContext.update_intoc                 C  s0   | j d u r	tdƒ‚| j  ¡ }| j j| _d | _ |S rV   )rT   r   r   r)   r[   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 )NrW   z'Update has been called on this context.z%{} has a maximum AAD byte limit of {})rT   r   r^   r   r]   re   r`   Ú_MAX_AAD_BYTESrD   rb   rc   r%   r   r   r   r   r%   è   s   
ÿÿz/_AEADCipherContext.authenticate_additional_dataNrX   )r_   r   r   r$   r   r   r   r&   )
r   r   r   rY   r9   rd   r   r   r   r%   r   r   r   r   rZ   ½   s   
 




rZ   c                   @  s   e Zd Zddd„ZdS )rP   r)   r   r   c                 C  sF   | j d u r	tdƒ‚| j jd urtdƒ‚| j  |¡}| j j| _d | _ |S )NrW   z^tag provided both in mode and in call with finalize_with_tag: tag should only be provided once)rT   r   r[   rD   r*   r)   )r   r)   r
   r   r   r   r*   ú   s   
ÿ
z(_AEADDecryptionContext.finalize_with_tagNr+   )r   r   r   r*   r   r   r   r   rP   ù   s    rP   c                   @  s   e Zd Zeddd„ƒZdS )rO   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.)rT   r   r[   r   r   r   r   r)   	  s   
ÿz_AEADEncryptionContext.tagNr   )r   r   r   r-   r)   r   r   r   r   rO     s    rO   )#Ú
__future__r   r    rQ   Úcryptography.exceptionsr   r   r   Ú/cryptography.hazmat.primitives._cipheralgorithmr   Ú&cryptography.hazmat.primitives.ciphersr   ÚTYPE_CHECKINGÚ,cryptography.hazmat.backends.openssl.ciphersr   rL   ÚABCMetar	   r#   r(   r,   ÚTypeVarÚOptionalr.   ÚGenericr1   ÚUnionÚModeWithNonceÚModeWithTweakÚECBÚModeWithInitializationVectorr?   rZ   rP   rO   r   r   r   r   Ú<module>   s<   	
ÿLüÿÿ<