o
    ™¨Êhì  ã                   @   s‚  d dl mZmZmZ d dlZd dlZd dlmZ d dlm	Z	m
Z
mZmZmZ d dlmZ d dlmZ e ej¡G dd„ deƒƒZe ej¡G d	d
„ d
eƒƒZe ej¡G dd„ deƒƒZe ej¡G dd„ deƒƒZe ej¡G dd„ deƒƒZe ej¡G dd„ deƒƒZG dd„ deƒZe e¡G dd„ deƒƒZe e¡e e¡e e¡G dd„ deƒƒƒƒZe e¡G dd„ deƒƒZdS )é    )Úabsolute_importÚdivisionÚprint_functionN)Úutils)ÚAlreadyFinalizedÚAlreadyUpdatedÚNotYetFinalizedÚUnsupportedAlgorithmÚ_Reasons)ÚCipherBackend)Úmodesc                   @   s(   e Zd Zejdd„ ƒZejdd„ ƒZdS )ÚCipherAlgorithmc                 C   ó   dS )zE
        A string naming this mode (e.g. "AES", "Camellia").
        N© ©Úselfr   r   ú]/var/www/html/env/lib/python3.10/site-packages/cryptography/hazmat/primitives/ciphers/base.pyÚname   ó    zCipherAlgorithm.namec                 C   r   )zW
        The size of the key being used as an integer in bits (e.g. 128, 256).
        Nr   r   r   r   r   Úkey_size   r   zCipherAlgorithm.key_sizeN)Ú__name__Ú
__module__Ú__qualname__ÚabcÚabstractpropertyr   r   r   r   r   r   r      s
    
r   c                   @   ó   e Zd Zejdd„ ƒZdS )ÚBlockCipherAlgorithmc                 C   r   )zK
        The size of a block as an integer in bits (e.g. 64, 128).
        Nr   r   r   r   r   Ú
block_size%   r   zBlockCipherAlgorithm.block_sizeN)r   r   r   r   r   r   r   r   r   r   r   #   ó    r   c                   @   s6   e Zd Zejdd„ ƒZejdd„ ƒZejdd„ ƒZdS )ÚCipherContextc                 C   r   )zk
        Processes the provided bytes through the cipher and returns the results
        as bytes.
        Nr   ©r   Údatar   r   r   Úupdate.   r   zCipherContext.updatec                 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!   Úbufr   r   r   Úupdate_into5   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   Úabstractmethodr"   r%   r&   r   r   r   r   r   ,   s    

r   c                   @   r   )ÚAEADCipherContextc                 C   r   )z3
        Authenticates the provided bytes.
        Nr   r    r   r   r   Úauthenticate_additional_dataE   r   z.AEADCipherContext.authenticate_additional_dataN)r   r   r   r   r'   r)   r   r   r   r   r(   C   r   r(   c                   @   r   )ÚAEADDecryptionContextc                 C   r   )zŠ
        Returns the results of processing the final block as bytes and allows
        delayed passing of the authentication tag.
        Nr   )r   Útagr   r   r   Úfinalize_with_tagN   r   z'AEADDecryptionContext.finalize_with_tagN)r   r   r   r   r'   r,   r   r   r   r   r*   L   r   r*   c                   @   r   )ÚAEADEncryptionContextc                 C   r   )zb
        Returns tag bytes. This is only available after encryption is
        finalized.
        Nr   r   r   r   r   r+   X   r   zAEADEncryptionContext.tagN)r   r   r   r   r   r+   r   r   r   r   r-   V   r   r-   c                   @   ó,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚCipherc                 C   sP   t |tƒstdtjƒ‚t |tƒstdƒ‚|d ur| |¡ || _|| _	|| _
d S )Nz0Backend object does not implement CipherBackend.z&Expected interface of CipherAlgorithm.)Ú
isinstancer   r	   r
   ÚBACKEND_MISSING_INTERFACEr   Ú	TypeErrorÚvalidate_for_algorithmÚ	algorithmÚmodeÚ_backend)r   r4   r5   Úbackendr   r   r   Ú__init__a   s   
þ


zCipher.__init__c                 C   sB   t | jtjƒr| jjd urtdƒ‚| j | j| j¡}| j	|dd�S )Nz0Authentication tag must be None when encrypting.T©Úencrypt)
r0   r5   r   ÚModeWithAuthenticationTagr+   Ú
ValueErrorr6   Úcreate_symmetric_encryption_ctxr4   Ú	_wrap_ctx©r   Úctxr   r   r   Ú	encryptorr   s   ÿÿzCipher.encryptorc                 C   s    | j  | j| j¡}| j|dd�S )NFr9   )r6   Úcreate_symmetric_decryption_ctxr4   r5   r>   r?   r   r   r   Ú	decryptor}   s   ÿzCipher.decryptorc                 C   s*   t | jtjƒr|rt|ƒS t|ƒS t|ƒS ©N)r0   r5   r   r;   Ú_AEADEncryptionContextÚ_AEADCipherContextÚ_CipherContext)r   r@   r:   r   r   r   r>   ƒ   s
   zCipher._wrap_ctxN)r   r   r   r8   rA   rC   r>   r   r   r   r   r/   `   s
    r/   c                   @   r.   )
rG   c                 C   s
   || _ d S rD   )Ú_ctxr?   r   r   r   r8   �   s   
z_CipherContext.__init__c                 C   s   | j d u r	tdƒ‚| j  |¡S ©NúContext was already finalized.)rH   r   r"   r    r   r   r   r"   ’   s   
z_CipherContext.updatec                 C   s    | j d u r	tdƒ‚| j  ||¡S rI   )rH   r   r%   r#   r   r   r   r%   —   s   
z_CipherContext.update_intoc                 C   s&   | j d u r	tdƒ‚| j  ¡ }d | _ |S rI   )rH   r   r&   r    r   r   r   r&   œ   s
   

z_CipherContext.finalizeN)r   r   r   r8   r"   r%   r&   r   r   r   r   rG   �   s
    rG   c                   @   sD   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )rF   c                 C   s"   || _ d| _d| _d | _d| _d S )Nr   F)rH   Ú_bytes_processedÚ_aad_bytes_processedÚ_tagÚ_updatedr?   r   r   r   r8   ¨   s
   
z_AEADCipherContext.__init__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 )NrJ   Tz-{0} has a maximum encrypted byte limit of {1})	rH   r   rN   rK   Ú_modeÚ_MAX_ENCRYPTED_BYTESr<   Úformatr   )r   Ú	data_sizer   r   r   Ú_check_limit¯   s   
ÿÿÿz_AEADCipherContext._check_limitc                 C   s   |   t|ƒ¡ | j |¡S rD   )rS   ÚlenrH   r"   r    r   r   r   r"   »   s   z_AEADCipherContext.updatec                 C   s   |   t|ƒ¡ | j ||¡S rD   )rS   rT   rH   r%   r#   r   r   r   r%   ¿   s   z_AEADCipherContext.update_intoc                 C   s0   | j d u r	tdƒ‚| j  ¡ }| j j| _d | _ |S rI   )rH   r   r&   r+   rM   r    r   r   r   r&   Ã   s   


z_AEADCipherContext.finalizec                 C   s2   | j d u r	tdƒ‚| j  |¡}| j j| _d | _ |S rI   )rH   r   r,   r+   rM   )r   r+   r!   r   r   r   r,   Ë   s   

z$_AEADCipherContext.finalize_with_tagc                 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 )NrJ   z'Update has been called on this context.z'{0} has a maximum AAD byte limit of {1})rH   r   rN   r   rL   rT   rO   Ú_MAX_AAD_BYTESr<   rQ   r   r)   r    r   r   r   r)   Ó   s   
ÿÿz/_AEADCipherContext.authenticate_additional_dataN)
r   r   r   r8   rS   r"   r%   r&   r,   r)   r   r   r   r   rF   ¤   s    rF   c                   @   s   e Zd Zedd„ ƒZdS )rE   c                 C   s   | j d ur	tdƒ‚| jS )Nz4You must finalize encryption before getting the tag.)rH   r   rM   r   r   r   r   r+   æ   s   
z_AEADEncryptionContext.tagN)r   r   r   Úpropertyr+   r   r   r   r   rE   ä   s    rE   ) Ú
__future__r   r   r   r   ÚsixÚcryptographyr   Úcryptography.exceptionsr   r   r   r	   r
   Ú'cryptography.hazmat.backends.interfacesr   Ú&cryptography.hazmat.primitives.ciphersr   Úadd_metaclassÚABCMetaÚobjectr   r   r   r(   r*   r-   r/   Úregister_interfacerG   rF   rE   r   r   r   r   Ú<module>   s8   





		-=