U +Mb*@sddlmZddlmZddlmZmZmZmZm Z m Z m Z m Z m Z eddZddZGdd d eZd d Zd d ZdZdZdS))get_random_bytes) _copy_bytes) load_pycryptodome_raw_libcreate_string_bufferget_raw_buffer VoidPointer SmartPointerc_size_t c_uint8_ptrc_ulongis_writeable_bufferzCrypto.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]); cCsVt|dkstt|dks ttd}tt|t|t|}|rRtd||S)N z,Error %d when deriving subkey with HChaCha20)lenAssertionError bytearray_raw_chacha20_libZ hchacha20r ValueError)keynoncesubkeyresultr=/tmp/pip-target-t616c12r/lib/python/Crypto/Cipher/ChaCha20.py _HChaCha20Bs rc@s@eZdZdZdZddZdddZdd Zdd d Zd d Z dS)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 Cstdd||_t|dkrDt||dd}d|dd}d|_n d|_|j}|j|jf|_t|_ t |j t |tt||tt|}|rtd||jft|j t j|_ dS)z\Initialize a ChaCha20/XChaCha20 cipher object See also `new()` at the module level.NrZ XChaCha20ChaCha20z"Error %d instantiating a %s cipher)rrrr_nameencryptdecrypt_nextr_staterZ chacha20_initZ address_ofr r rrgetZchacha20_destroy)selfrrrrrr__init__\s.    zChaCha20Cipher.__init__NcCs*|j|jkrtd|jf|_|||S)aEncrypt 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``. z-Cipher object can only be used for decryption)r!r# TypeError_encrypt)r& plaintextoutputrrrr!|s  zChaCha20Cipher.encryptcCs|dkrtt|}n4|}t|s*tdt|t|krJtdt|t|jt |t |t t|}|rtd||j f|dkrt |SdSdS)zEncrypt 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) rrr r(rrZchacha20_encryptr$r%r r r r)r&r*r+ ciphertextrrrrr)s( zChaCha20Cipher._encryptc Csh|j|jkrtd|jf|_z|||WStk rb}ztt|ddW5d}~XYnXdS)aDecrypt 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``. z-Cipher object can only be used for encryptionencdecN)r"r#r(r)rstrreplace)r&r,r+errrr"s  zChaCha20Cipher.decryptcCsVt|d\}}|d@}|d?}t|jt|t||}|rRtd||jfdS)zSeek to a certain position in the key stream. Args: position (integer): The absolute position within the key stream, in bytes. @lr zError %d while seeking with %sN)divmodrZ chacha20_seekr$r%r rr )r&positionoffsetZ block_lowZ block_highrrrrseekszChaCha20Cipher.seek)N)N) __name__ __module__ __qualname____doc__ block_sizer'r!r)r"r6rrrrrRs  rcCst|dkrtd|dkr*td}}n0t|dkr@d|}nt|dkrR|}ntdt||dd }|dd |d d|fS) 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 rz6Poly1305 with ChaCha20 requires an 8- or 12-byte nonce)rrs r)rrrnewr!)rrZ padded_noncersrrr_derive_Poly1305_key_pairs    r@c Ksz|d}Wn.tk r<}ztd|W5d}~XYnX|dd}|dkrZtd}t|dkrntdt|dkrtd |rtd t|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:`Crypto.Cipher.ChaCha20.ChaCha20Cipher` object rzMissing parameter %sNrr=r z,ChaCha20/XChaCha20 key must be 32 bytes long)r=r<rz:Nonce must be 8/12 bytes(ChaCha20) or 24 bytes (XChaCha20)zUnknown parameters: )popKeyErrorr(rrrr/r)kwargsrr1rrrrr>s   r>rr N)Z Crypto.RandomrZCrypto.Util.py3compatrZCrypto.Util._raw_apirrrrrr r r r rrobjectrr@r>r;key_sizerrrrs  ,)