
    iW*                         S SK Jr  S SKJr  S SKJrJrJrJrJ	r	J
r
JrJrJr  \" SS5      rS r " S S\5      rS	 rS
 rSrSrg)    )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                     [        U 5      S:X  d   e[        U5      S:X  d   e[        S5      n[        R                  [	        U 5      [	        U5      [	        U5      5      nU(       a  [        SU-  5      eU$ )N       z,Error %d when deriving subkey with HChaCha20)len	bytearray_raw_chacha20_lib	hchacha20r   
ValueError)keynoncesubkeyresults       Q/var/www/html/trading/venv/lib/python3.13/site-packages/Crypto/Cipher/ChaCha20.py
_HChaCha20r   C   sp    s8r>>u:r]F((C E"F#%F G&PQQM    c                   B    \ rS rSrSrSrS rSS jrS rSS jr	S	 r
S
rg)ChaCha20CipherS   zChaCha20 (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                 @   [        SSU5      U l        [        U5      S:X  a  [        XSS 5      nSUSS -   nSU l        OSU l        U R                  nSU l        [        5       U l        [        R                  U R                  R                  5       [        U5      [        [        U5      5      U[        [        U5      5      5      nU(       a  [        SUU R                  4-  5      e[        U R                  R                  5       [        R                   5      U l        g)	zTInitialize 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__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                 h    SU R                   ;  a  [        S5      eSU l         U R                  X5      $ )ac  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&   ChaCha20Cipher.encrypt}   s2     DJJ&KLL!
}}Y//r   c           
         Uc  [        [        U5      5      nOLUn[        U5      (       d  [        S5      e[        U5      [        U5      :w  a  [	        S[        U5      -  5      e[
        R                  U R                  R                  5       [        U5      [        U5      [        [        U5      5      5      nU(       a  [	        SX@R                  4-  5      eUc  [        U5      $ g)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   r3   r   r   chacha20_encryptr*   r-   r   r
   r(   r   )r/   r5   r6   
ciphertextr   s        r   r4   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
 @FJJCWWXX>!*--r   c                     SU R                   ;  a  [        S5      eSU l          U R                  X5      $ ! [         a)  n[        [	        U5      R                  SS5      5      eSnAff = f)aa  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)   r3   r4   r   strreplace)r/   r:   r6   es       r   r'   ChaCha20Cipher.decrypt   sa     DJJ&KLL!
	;==44 	;SV^^E59::	;s   5 
A($A##A(c                     [        US5      u  p#US-  nUS-	  n[        R                  U R                  R	                  5       [        U5      [        U5      U5      nU(       a  [        SX`R                  4-  5      eg)zSeek 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   seekChaCha20Cipher.seek   sz      &h3 :-	!R'
"00151B181D181C17	3 =@TTUU r   )r(   r)   r*   r   )N)__name__
__module____qualname____firstlineno____doc__
block_sizer0   r&   r4   r'   rL   __static_attributes__ r   r   r   r   S   s*     JG@0$8;,Vr   r   c                     [        U 5      S:w  a  [        S5      eUc  [        S5      =p!O2[        U5      S:X  a  SU-   nO[        U5      S:X  a  UnO[        S5      e[        XS9R	                  S	5      nUSS
 US
S U4$ )zjDerive 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_pairr\      s     3x2~HII}/33u	Uq +U2	Ur	QRR		)	1	1,	?Bcr7BrsGU""r   c                  `    U R                  S5      nU R                  SS5      nUc  [        S5      n[	        U5      S:w  a  [        S5      e[	        U5      S;  a  [        S	5      eU (       a  [        S
[        U 5      -   5      e[        X5      $ ! [         a  n[        SU-  5      eSnAff = f)aM  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   rX   r   z,ChaCha20/XChaCha20 key must be 32 bytes long)rX   rW   r"   z:Nonce must be 8/12 bytes(ChaCha20) or 24 bytes (XChaCha20)zUnknown parameters: )popKeyErrorr3   r   r   r   r?   r   )kwargsr   rA   r   s       r   rY   rY      s    (4jj JJw%E} #
3x2~GHH
5z$UVV.V<==#%%!  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   r\   rY   rS   key_sizerU   r   r   <module>rf      s`   > + -7 7 7 ..G 4 DVV DVN#:&&R 
 r   