
    ,hW*                         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y)    )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Crypto.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        j                  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       X/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/Crypto/Cipher/ChaCha20.py
_HChaCha20r   C   sk    s8r>u:r]F((C E"F#%F G&PQQM    c                   6    e Zd ZdZdZd Zd	dZd Zd	dZd Z	y)
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                 6   t        dd|      | _        t        |      dk(  rt        ||dd       }d|dd z   }d| _        nd| _        | j                  }d| _        t               | _        t        j                  | j                  j                         t        |      t        t        |            |t        t        |                  }|rt        d|| j                  fz        t        | j                  j                         t        j                         | _        y)	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__]   s    
 !tU3
 u:S%*-C%*,E$DJ#DJJJE+
!m"00..0#C( S* U,. AVEIZZEQ Q R R"4;;??#4#4#E#EGr   Nc                 f    d| j                   vrt        d      d| _         | j                  ||      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}   s4     DJJ&KLL!
}}Y//r   c           
         |t        t        |            }nF|}t        |      st        d      t        |      t        |      k7  rt	        dt        |      z        t
        j                  | j                  j                         t        |      t        |      t        t        |                  }|rt	        d|| j                  fz        |t        |      S y)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)n=JJ&v. VWW9~V,  "025i."A B B #33)-):)4Y)?)4Z)@)1#i.)A	C
 @FDJJCWWXX>!*--r   c                     d| j                   vrt        d      d| _         	 | j                  ||      S # t        $ r)}t        t	        |      j                  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   sc     DJJ&KLL!
	;==V44 	;SV^^E59::	;s   4 	A&$A!!A&c                     t        |d      \  }}|dz  }|dz	  }t        j                  | j                  j	                         t        |      t        |      |      }|rt        d|| j                  fz        y)a   Seek to a certain position in the key stream.

        If you want to seek to a certain block,
        use ``seek(block_number * 64)``.

        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block_numberoffset	block_low
block_highr   s          r   seekzChaCha20Cipher.seek   s~      &h3f :-	!R'
"00151B181D181C17	3 =@TTUU r   )N)
__name__
__module____qualname____doc__
block_sizer/   r%   r2   r&   rG    r   r   r   r   S   s*     JG@0$8;,Vr   r   c                    t        |       dk7  rt        d      |t        d      x}}n0t        |      dk(  rd|z   }nt        |      dk(  r|}nt        d      t        | |      j	                  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_pairrT      s     3x2~HII}/33u	Uq +U2	Ur	QRR	L	)	1	1,	?Bcr7BrsGU""r   c                  N   	 | j                  d      }| j                  dd      }|t        d      }t	        |      dk7  rt        d      t	        |      dvrt        d	      | rt        d
t        |       z         t        ||      S # t        $ r}t        d|z        d}~ww xY w)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:`Crypto.Cipher.ChaCha20.ChaCha20Cipher` object
    r   zMissing parameter %sNr   rP   r   z,ChaCha20/XChaCha20 key must be 32 bytes long)rP   rO   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   rQ   rQ      s    (4jj JJw%E} #
3x2~GHH
5z$UVV.V<==#u%%!  4.2334s   B 	B$BB$r   r   N)Crypto.Randomr   Crypto.Util.py3compatr   Crypto.Util._raw_apir   r   r   r   r	   r
   r   r   r   r   r   objectr   rT   rQ   rL   key_sizerM   r   r   <module>r^      s`   > + -7 7 7 ..G 4 DVV DVN#:&&R 
 r   