
    ¿ig*                         d dl mZ d dlmZ d dlmZmZmZmZm	Z	m
Z
mZmZmZ  edd          Zd Z G d de          Zd	 Zd
 ZdZdZdS )    )get_random_bytes)_copy_bytes)	load_pycryptodome_raw_libcreate_string_bufferget_raw_bufferVoidPointerSmartPointerc_size_tc_uint8_ptrc_ulongis_writeable_bufferzCryptodome.Cipher._chacha20a  
                    int chacha20_init(void **pState,
                                      const uint8_t *key,
                                      size_t keySize,
                                      const uint8_t *nonce,
                                      size_t nonceSize);

                    int chacha20_destroy(void *state);

                    int chacha20_encrypt(void *state,
                                         const uint8_t in[],
                                         uint8_t out[],
                                         size_t len);

                    int chacha20_seek(void *state,
                                      unsigned long block_high,
                                      unsigned long block_low,
                                      unsigned offset);
                    int hchacha20(  const uint8_t key[32],
                                    const uint8_t nonce16[16],
                                    uint8_t subkey[32]);
                    c                 &   t          |           dk    sJ t          |          dk    sJ t          d          }t                              t	          |           t	          |          t	          |                    }|rt          d|z            |S )N       z,Error %d when deriving subkey with HChaCha20)len	bytearray_raw_chacha20_lib	hchacha20r   
ValueError)keynoncesubkeyresults       V/var/www/html/afkarena/venv/lib/python3.11/site-packages/Cryptodome/Cipher/ChaCha20.py
_HChaCha20r   B   s    s88r>>>>u::r]]F((C  E""F##% %F  RG&PQQQM    c                   8    e Zd ZdZdZd Zd	dZd Zd	dZd Z	dS )
ChaCha20CipherzChaCha20 (or XChaCha20) cipher object.
    Do not create it directly. Use :py:func:`new` instead.

    :var nonce: The nonce with length 8, 12 or 24 bytes
    :vartype nonce: bytes
       c                    t          dd|          | _        t          |          dk    r-t          ||dd                   }d|dd         z   }d| _        nd| _        | j        }d| _        t                      | _        t          	                    | j        
                                t          |          t          t          |                    |t          t          |                              }|rt          d|| j        fz            t          | j                                        t          j                  | _        dS )	z\Initialize a ChaCha20/XChaCha20 cipher object

        See also `new()` at the module level.N   r          	XChaCha20ChaCha20)encryptdecryptz"Error %d instantiating a %s cipher)r   r   r   r   _name_nextr   _stater   chacha20_init
address_ofr   r
   r   r	   getchacha20_destroy)selfr   r   r   s       r   __init__zChaCha20Cipher.__init__\   s4   
 !tU33
 u::S%*--C%*,E$DJJ#DJJE+
!mm"00..00#C(( S** U,,. .  	RAVEIZEQ Q R R R"4;??#4#4#4#EG Gr   Nc                 l    d| j         vrt          d          d| _         |                     ||          S )a  Encrypt a piece of data.

        Args:
          plaintext(bytes/bytearray/memoryview): The data to encrypt, of any size.
        Keyword Args:
          output(bytes/bytearray/memoryview): The location where the ciphertext
            is written to. If ``None``, the ciphertext is returned.
        Returns:
          If ``output`` is ``None``, the ciphertext is returned as ``bytes``.
          Otherwise, ``None``.
        r%   z-Cipher object can only be used for decryption)r%   )r(   	TypeError_encrypt)r.   	plaintextoutputs      r   r%   zChaCha20Cipher.encrypt|   s;     DJ&&KLLL!
}}Y///r   c           
      *   |t          t          |                    }n_|}t          |          st          d          t          |          t          |          k    rt	          dt          |          z            t
                              | j                                        t          |          t          |          t          t          |                              }|rt	          d|| j        fz            |t          |          S dS )zEncrypt without FSM checksNz4output must be a bytearray or a writeable memoryviewz9output must have the same length as the input  (%d bytes)z!Error %d while encrypting with %s)r   r   r   r1   r   r   chacha20_encryptr)   r,   r   r
   r'   r   )r.   r3   r4   
ciphertextr   s        r   r2   zChaCha20Cipher._encrypt   s    >-c)nn==JJJ&v.. X VWWW9~~V,,  "025i.."A B B B #33)-):):)4Y)?)?)4Z)@)@)1#i..)A)A	C C
  	Y@FDJCWWXXX>!*---4r   c                     d| j         vrt          d          d| _         	 |                     ||          S # t          $ r5}t          t	          |                              dd                    d}~ww xY w)a  Decrypt a piece of data.

        Args:
          ciphertext(bytes/bytearray/memoryview): The data to decrypt, of any size.
        Keyword Args:
          output(bytes/bytearray/memoryview): The location where the plaintext
            is written to. If ``None``, the plaintext is returned.
        Returns:
          If ``output`` is ``None``, the plaintext is returned as ``bytes``.
          Otherwise, ``None``.
        r&   z-Cipher object can only be used for encryption)r&   encdecN)r(   r1   r2   r   strreplace)r.   r7   r4   es       r   r&   zChaCha20Cipher.decrypt   s     DJ&&KLLL!
	;==V444 	; 	; 	;SVV^^E599:::	;s   7 
A60A11A6c                    t          |d          \  }}|dz  }|dz	  }t                              | j                                        t          |          t          |          |          }|rt          d|| j        fz            dS )zSeek to a certain position in the key stream.

        Args:
          position (integer):
            The absolute position within the key stream, in bytes.
        @   l    r   zError %d while seeking with %sN)divmodr   chacha20_seekr)   r,   r   r   r'   )r.   positionoffset	block_low
block_highr   s         r   seekzChaCha20Cipher.seek   s     "(B//&z)	^
"00151B1B181D1D181C1C17	3 3  	V=@TTUUU	V 	Vr   )N)
__name__
__module____qualname____doc__
block_sizer/   r%   r2   r&   rF    r   r   r   r   R   s          JG G G@0 0 0 0$  8; ; ; ;,V V V V Vr   r   c                 \   t          |           dk    rt          d          |t          d          x}}n>t          |          dk    rd|z   }n%t          |          dk    r|}nt          d          t          | |                              d	          }|dd
         |d
d         |fS )zrDerive a tuple (r, s, nonce) for a Poly1305 MAC.

    If nonce is ``None``, a new 12-byte nonce is generated.
    r   z-Poly1305 with ChaCha20 requires a 32-byte keyN      r"   z6Poly1305 with ChaCha20 requires an 8- or 12-byte nonce)r   r   s                                    r   )r   r   r   newr%   )r   r   padded_noncerss       r   _derive_Poly1305_key_pairrS      s     3xx2~~HIII}/333uu	Uq +U2	Ur		QRRR	L	)	)	)	1	1,	?	?Bcrc7BrssGU""r   c                     	 |                      d          }n$# t          $ r}t          d|z            d}~ww xY w|                      dd          }|t          d          }t	          |          dk    rt          d          t	          |          dvrt          d	          | rt          d
t          |           z             t          ||          S )a  Create a new ChaCha20 or XChaCha20 cipher

    Keyword Args:
        key (bytes/bytearray/memoryview): The secret key to use.
            It must be 32 bytes long.
        nonce (bytes/bytearray/memoryview): A mandatory value that
            must never be reused for any other encryption
            done with this key.

            For ChaCha20, it must be 8 or 12 bytes long.

            For XChaCha20, it must be 24 bytes long.

            If not provided, 8 bytes will be randomly generated
            (you can find them back in the ``nonce`` attribute).

    :Return: a :class:`Cryptodome.Cipher.ChaCha20.ChaCha20Cipher` object
    r   zMissing parameter %sNr   rO   r   z,ChaCha20/XChaCha20 key must be 32 bytes long)rO   rN   r!   z:Nonce must be 8/12 bytes(ChaCha20) or 24 bytes (XChaCha20)zUnknown parameters: )popKeyErrorr1   r   r   r   r;   r   )kwargsr   r=   r   s       r   rP   rP      s    (4jj 4 4 4.23334 JJw%%E} ##
3xx2~~GHHH
5zz$$UVVV >.V<===#u%%%s    
949r   r   N)Cryptodome.Randomr   Cryptodome.Util.py3compatr   Cryptodome.Util._raw_apir   r   r   r   r	   r
   r   r   r   r   r   objectr   rS   rP   rK   key_sizerL   r   r   <module>r]      s1  > / . . . . . 1 1 1 1 1 17 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 .-.K  2   AV AV AV AV AVV AV AV AVH# # #:&& && &&R 
 r   