
    +h^                        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ej                   	      Z G d
 deej                   	      Z G d deej                   	      Z G d deej                   	      Z ej*                  dej,                  ej.                     d      Z G d dej0                  e         Zeej4                  ej6                  ej8                  dej:                  ej<                  f      Z G d de      Z G d de      Z  G d de e      Z! G d de e      Z"y)    )annotationsN)AlreadyFinalizedAlreadyUpdatedNotYetFinalized)CipherAlgorithm)modes)_CipherContextc                  ~    e Zd Zej                  dd       Zej                  dd       Zej                  dd       Zy)CipherContextc                     y)zk
        Processes the provided bytes through the cipher and returns the results
        as bytes.
        N selfdatas     ^/var/www/html/venv/lib/python3.12/site-packages/cryptography/hazmat/primitives/ciphers/base.pyupdatezCipherContext.update           c                     y)z
        Processes the provided bytes and writes the resulting data into the
        provided buffer. Returns the number of bytes written.
        Nr   r   r   bufs      r   update_intozCipherContext.update_into    r   r   c                     y)zM
        Returns the results of processing the final block as bytes.
        Nr   r   s    r   finalizezCipherContext.finalize'   r   r   Nr   bytesreturnr   r   r   r   r   r   intr   r   )__name__
__module____qualname__abcabstractmethodr   r   r   r   r   r   r   r      sN      	  	 r   r   )	metaclassc                  2    e Zd Zej                  dd       Zy)AEADCipherContextc                     y)z3
        Authenticates the provided bytes.
        Nr   r   s     r   authenticate_additional_dataz.AEADCipherContext.authenticate_additional_data/   r   r   Nr   r   r   None)r"   r#   r$   r%   r&   r+   r   r   r   r)   r)   .   s     r   r)   c                  2    e Zd Zej                  dd       Zy)AEADDecryptionContextc                     y)z
        Returns the results of processing the final block as bytes and allows
        delayed passing of the authentication tag.
        Nr   )r   tags     r   finalize_with_tagz'AEADDecryptionContext.finalize_with_tag7   r   r   Nr1   r   r   r   )r"   r#   r$   r%   r&   r2   r   r   r   r/   r/   6   s     r   r/   c                  <    e Zd Zeej
                  dd              Zy)AEADEncryptionContextc                     y)zb
        Returns tag bytes. This is only available after encryption is
        finalized.
        Nr   r   s    r   r1   zAEADEncryptionContext.tag@   r   r   Nr!   )r"   r#   r$   propertyr%   r&   r1   r   r   r   r5   r5   ?   s"      r   r5   ModeT)bound	covariantc                      e Zd Z	 d
	 	 	 	 	 	 	 ddZej
                  	 	 	 	 dd       Zej
                  	 	 	 	 dd       Zd Zej
                  	 	 	 	 dd       Zej
                  	 	 	 	 dd       Zd Z	 	 	 	 	 	 dd	Zy)CipherNc                    t        |t              st        d      |-t        |t        j                        sJ |j                  |       || _        || _        y )Nz&Expected interface of CipherAlgorithm.)
isinstancer   	TypeErrorr   r8   validate_for_algorithm	algorithmmode)r   rA   rB   backends       r   __init__zCipher.__init__O   sR     )_5DEE dEJJ///''	2"	r   c                     y Nr   r   s    r   	encryptorzCipher.encryptora        	r   c                     y rF   r   r   s    r   rG   zCipher.encryptorg   rH   r   c                
   t        | j                  t        j                        r!| j                  j                  t        d      ddlm} |j                  | j                  | j                        }| j                  |d      S )Nz0Authentication tag must be None when encrypting.r   rC   Tencrypt)r>   rB   r   ModeWithAuthenticationTagr1   
ValueError,cryptography.hazmat.backends.openssl.backendrC   create_symmetric_encryption_ctxrA   	_wrap_ctxr   rC   ctxs      r   rG   zCipher.encryptorm   sl    dii!@!@Ayy}}( F  	I55NNDII
 ~~c4~00r   c                     y rF   r   r   s    r   	decryptorzCipher.decryptorz   rH   r   c                     y rF   r   r   s    r   rV   zCipher.decryptor   rH   r   c                    ddl m} |j                  | j                  | j                        }| j                  |d      S )Nr   rK   FrL   )rP   rC   create_symmetric_decryption_ctxrA   rB   rR   rS   s      r   rV   zCipher.decryptor   s7    H55NNDII
 ~~c5~11r   c                    t        | j                  t        j                        r|rt	        |      S t        |      S t        |      S rF   )r>   rB   r   rN   _AEADEncryptionContext_AEADDecryptionContextr	   )r   rT   rM   s      r   rR   zCipher._wrap_ctx   s<    
 dii!@!@A-c22-c22!#&&r   rF   )rA   r   rB   r8   rC   z
typing.Anyr   r-   )r   'Cipher[modes.ModeWithAuthenticationTag]r   r5   )r   _CIPHER_TYPEr   r   )r   r]   r   r/   )rT   _BackendCipherContextrM   boolr   zItyping.Union[AEADEncryptionContext, AEADDecryptionContext, CipherContext])	r"   r#   r$   rD   typingoverloadrG   rV   rR   r   r   r   r<   r<   N   s    
 #	"  	
 
$ __5	 
 __	 
1 __5	 
 __	 
2'('37'
'r   r<   c                  8    e Zd ZU ded<   ddZd	dZd
dZddZy)r	   &typing.Optional[_BackendCipherContext]_ctxc                    || _         y rF   )re   r   rT   s     r   rD   z_CipherContext.__init__   s	    	r   c                f    | j                   t        d      | j                   j                  |      S NContext was already finalized.)re   r   r   r   s     r   r   z_CipherContext.update   s-    99"#CDDyy%%r   c                h    | j                   t        d      | j                   j                  ||      S ri   )re   r   r   r   s      r   r   z_CipherContext.update_into   s/    99"#CDDyy$$T3//r   c                v    | j                   t        d      | j                   j                         }d | _         |S ri   )re   r   r   r   s     r   r   z_CipherContext.finalize   s6    99"#CDDyy!!#	r   NrT   r_   r   r-   r   r   r!   )r"   r#   r$   __annotations__rD   r   r   r   r   r   r   r	   r	      s    
00&
0
r   r	   c                  R    e Zd ZU ded<   ded<   ddZddZddZddZdd	Zdd
Z	y)_AEADCipherContextrd   re   ztyping.Optional[bytes]_tagc                J    || _         d| _        d| _        d | _        d| _        y )Nr   F)re   _bytes_processed_aad_bytes_processedrq   _updatedrg   s     r   rD   z_AEADCipherContext.__init__   s'    	 !$%!	r   c                r   | j                   t        d      d| _        | xj                  |z  c_        | j                  | j                   j                  j
                  kD  rWt        dj                  | j                   j                  j                  | j                   j                  j
                              y )Nrj   Tz+{} has a maximum encrypted byte limit of {})	re   r   ru   rs   _mode_MAX_ENCRYPTED_BYTESrO   formatname)r   	data_sizes     r   _check_limitz_AEADCipherContext._check_limit   s    99"#CDD*  499??#G#GG=DDIIOO(($))//*N*N  Hr   c                    | j                  t        |             | j                  J | j                  j                  |      S rF   )r|   lenre   r   r   s     r   r   z_AEADCipherContext.update   s9    #d)$yy$$$yy%%r   c                    | j                  t        |             | j                  J | j                  j                  ||      S rF   )r|   r~   re   r   r   s      r   r   z_AEADCipherContext.update_into   s;    #d)$yy$$$yy$$T3//r   c                    | j                   t        d      | j                   j                         }| j                   j                  | _        d | _         |S ri   )re   r   r   r1   rq   r   s     r   r   z_AEADCipherContext.finalize   sE    99"#CDDyy!!#IIMM		r   c                   | j                   t        d      | j                  rt        d      | xj                  t        |      z  c_        | j                  | j                   j                  j                  kD  rWt        dj                  | j                   j                  j                  | j                   j                  j                              | j                   j                  |       y )Nrj   z'Update has been called on this context.z%{} has a maximum AAD byte limit of {})re   r   ru   r   rt   r~   rw   _MAX_AAD_BYTESrO   ry   rz   r+   r   s     r   r+   z/_AEADCipherContext.authenticate_additional_data   s    99"#CDD== !JKK!!SY.!$$tyy'E'EE7>>IIOO(($))//*H*H  			..t4r   Nrm   )r{   r    r   r-   r   r   r!   r,   )
r"   r#   r$   rn   rD   r|   r   r   r   r+   r   r   r   rp   rp      s,    
00
  
&05r   rp   c                      e Zd ZddZy)r\   c                    | j                   t        d      | j                   j                  |      }| j                   j                  | _        d | _         |S ri   )re   r   r2   r1   rq   )r   r1   r   s      r   r2   z(_AEADDecryptionContext.finalize_with_tag   sG    99"#CDDyy**3/IIMM		r   Nr3   )r"   r#   r$   r2   r   r   r   r\   r\      s    r   r\   c                      e Zd Zedd       Zy)r[   c                d    | j                   t        d      | j                  J | j                  S )Nz4You must finalize encryption before getting the tag.)re   r   rq   r   s    r   r1   z_AEADEncryptionContext.tag  s8    99 !I  yy$$$yyr   Nr!   )r"   r#   r$   r7   r1   r   r   r   r[   r[     s     r   r[   )#
__future__r   r%   ra   cryptography.exceptionsr   r   r   /cryptography.hazmat.primitives._cipheralgorithmr   &cryptography.hazmat.primitives.ciphersr   TYPE_CHECKING,cryptography.hazmat.backends.openssl.ciphersr	   r_   ABCMetar   r)   r/   r5   TypeVarOptionalr8   Genericr<   UnionModeWithNonceModeWithTweakECBModeWithInitializationVectorr^   rp   r\   r[   r   r   r   <module>r      s3  
 # 
  
 L 8	
ckk , - -  v~~
&//%**-
K'V^^D! K'\ 
LL		**		,] 095* 95x/1F /1F r   