o
    b_                     @   s2  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mZ d dlmZ d dlmZ G dd	 d	e jd
ZG dd de jd
ZG dd de jd
ZG dd de jd
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 )    N)utils)AlreadyFinalizedAlreadyUpdatedNotYetFinalizedUnsupportedAlgorithm_Reasons)_get_backend)BackendCipherBackend)CipherAlgorithm)modesc                   @   sP   e Zd ZejdedefddZejdedefddZejdefddZ	d	S )
CipherContextdatareturnc                 C      dS )zk
        Processes the provided bytes through the cipher and returns the results
        as bytes.
        N selfr   r   r   j/var/www/secure340b-portal/env/lib/python3.10/site-packages/cryptography/hazmat/primitives/ciphers/base.pyupdate       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_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)
__name__
__module____qualname__abcabstractmethodbytesr   intr   r   r   r   r   r   r      s    r   )	metaclassc                   @   s$   e Zd ZejdeddfddZdS )AEADCipherContextr   r   Nc                 C   r   )z3
        Authenticates the provided bytes.
        Nr   r   r   r   r   authenticate_additional_data.   r   z.AEADCipherContext.authenticate_additional_data)r   r   r   r   r    r!   r%   r   r   r   r   r$   -       r$   c                   @   s$   e Zd ZejdedefddZdS )AEADDecryptionContexttagr   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_tag6   r   z'AEADDecryptionContext.finalize_with_tagN)r   r   r   r   r    r!   r)   r   r   r   r   r'   5   r&   r'   c                   @   s    e Zd ZejdefddZdS )AEADEncryptionContextr   c                 C   r   )zb
        Returns tag bytes. This is only available after encryption is
        finalized.
        Nr   r   r   r   r   r(   ?   r   zAEADEncryptionContext.tagN)r   r   r   r   abstractpropertyr!   r(   r   r   r   r   r*   >   s    r*   c                   @   sL   e Zd Z	ddedejej deje fddZ	dd Z
d	d
 Zdd ZdS )CipherN	algorithmmodebackendc                 C   sX   t |}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.)r   
isinstancer
   r   r   ZBACKEND_MISSING_INTERFACEr   	TypeErrorZvalidate_for_algorithmr-   r.   _backend)r   r-   r.   r/   r   r   r   __init__H   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   r.   r   ModeWithAuthenticationTagr(   
ValueErrorr2   Zcreate_symmetric_encryption_ctxr-   	_wrap_ctxr   ctxr   r   r   	encryptor_   s   zCipher.encryptorc                 C   s    | j | j| j}| j|ddS )NFr4   )r2   Zcreate_symmetric_decryption_ctxr-   r.   r8   r9   r   r   r   	decryptorj   s   zCipher.decryptorc                 C   s*   t | jtjr|rt|S t|S t|S N)r0   r.   r   r6   _AEADEncryptionContext_AEADCipherContext_CipherContext)r   r:   r5   r   r   r   r8   p   s
   zCipher._wrap_ctxr=   )r   r   r   r   typingOptionalr   ZModer	   r3   r;   r<   r8   r   r   r   r   r,   G   s    

r,   c                   @   sF   e 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S )r@   c                 C   s
   || _ d S r=   )_ctxr9   r   r   r   r3   |   s   
z_CipherContext.__init__r   r   c                 C   s   | j d u r	td| j |S NContext was already finalized.)rC   r   r   r   r   r   r   r      s   
z_CipherContext.updatec                 C   s    | j d u r	td| j ||S rD   )rC   r   r   r   r   r   r   r      s   
z_CipherContext.update_intoc                 C   s&   | j d u r	td| j  }d | _ |S rD   )rC   r   r   r   r   r   r   r      s
   

z_CipherContext.finalizeN)	r   r   r   r3   r!   r   r"   r   r   r   r   r   r   r@   z   s
    r@   c                   @   s|   e Zd Zdd Zdeddf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dS )r?   c                 C   s"   || _ d| _d| _d | _d| _d S )Nr   F)rC   _bytes_processed_aad_bytes_processed_tag_updatedr9   r   r   r   r3      s
   
z_AEADCipherContext.__init__	data_sizer   Nc                 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 )NrE   Tz+{} has a maximum encrypted byte limit of {})	rC   r   rI   rF   _modeZ_MAX_ENCRYPTED_BYTESr7   formatname)r   rJ   r   r   r   _check_limit   s   
z_AEADCipherContext._check_limitr   c                 C   s   |  t| | j|S r=   )rN   lenrC   r   r   r   r   r   r      s   z_AEADCipherContext.updatec                 C   s   |  t| | j||S r=   )rN   rO   rC   r   r   r   r   r   r      s   z_AEADCipherContext.update_intoc                 C   s0   | j d u r	td| j  }| j j| _d | _ |S rD   )rC   r   r   r(   rH   r   r   r   r   r      s   


z_AEADCipherContext.finalizer(   c                 C   s2   | j d u r	td| j |}| j j| _d | _ |S rD   )rC   r   r)   r(   rH   )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 )NrE   z'Update has been called on this context.z%{} has a maximum AAD byte limit of {})rC   r   rI   r   rG   rO   rK   Z_MAX_AAD_BYTESr7   rL   rM   r%   r   r   r   r   r%      s   
z/_AEADCipherContext.authenticate_additional_data)r   r   r   r3   r"   rN   r!   r   r   r   r)   r%   r   r   r   r   r?      s    r?   c                   @   s   e Zd Zedef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.)rC   r   rH   r   r   r   r   r(      s   
z_AEADEncryptionContext.tagN)r   r   r   propertyr!   r(   r   r   r   r   r>      s    r>   )r   rA   Zcryptographyr   Zcryptography.exceptionsr   r   r   r   r   Zcryptography.hazmat.backendsr   Z'cryptography.hazmat.backends.interfacesr	   r
   Z/cryptography.hazmat.primitives._cipheralgorithmr   Z&cryptography.hazmat.primitives.ciphersr   ABCMetar   r$   r'   r*   objectr,   Zregister_interfacer@   r?   r>   r   r   r   r   <module>   s*   		3=