o
    e^                      @  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   ^/var/www/html/venv/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   boolItyping.Union[AEADEncryptionContext, AEADDecryptionContext, CipherContext]c                 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   &typing.Optional[_BackendCipherContext]_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typing.Optional[bytes]_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  s2   | j d u r	td| j |}| j j| _d | _ |S rV   )rT   r   r*   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<   	
N<
