U +¥Mb*ã@s‚ddlmZddlmZddlmZmZmZmZm Z m Z m Z m Z m Z eddƒZdd„ZGdd „d eƒZd d „Zd d „ZdZdZdS)é)Ú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_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|ƒdkst‚t|ƒdks t‚tdƒ}t t|ƒt|ƒt|ƒ¡}|rRtd|ƒ‚|S)Né éz,Error %d when deriving subkey with HChaCha20)ÚlenÚAssertionErrorÚ bytearrayÚ_raw_chacha20_libZ hchacha20r Ú ValueError)ÚkeyÚnonceÚsubkeyÚresult©rú=/tmp/pip-target-t616c12r/lib/python/Crypto/Cipher/ChaCha20.pyÚ _HChaCha20Bsý rc@s@eZdZdZdZdd„Zddd„Zdd „Zdd d „Zd d „Z dS)ÚChaCha20Cipherz¹ChaCha20 (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 CsÀtdd|ƒ|_t|ƒdkrDt||dd…ƒ}d|dd…}d|_n d|_|j}|j|jf|_tƒ|_ t   |j   ¡t |ƒtt|ƒƒ|tt|ƒƒ¡}|r¨td||jfƒ‚t|j  ¡t jƒ|_ dS)z\Initialize a ChaCha20/XChaCha20 cipher object See also `new()` at the module level.NéróZ XChaCha20ÚChaCha20z"Error %d instantiating a %s cipher)rrrrÚ_nameÚencryptÚdecryptÚ_nextrÚ_staterZ chacha20_initZ address_ofr r rrÚgetZchacha20_destroy)ÚselfrrrrrrÚ__init__\s.   ûÿ ÿzChaCha20Cipher.__init__NcCs*|j|jkrtdƒ‚|jf|_| ||¡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``. z-Cipher object can only be used for decryption)r!r#Ú TypeErrorÚ_encrypt)r&Ú plaintextÚoutputrrrr!|s  zChaCha20Cipher.encryptcCsž|dkrtt|ƒƒ}n4|}t|ƒs*tdƒ‚t|ƒt|ƒkrJtdt|ƒƒ‚t |j ¡t |ƒt |ƒt t|ƒƒ¡}|r†td||j fƒ‚|dkr–t |ƒ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|ƒ dd¡ƒ‚W5d}~XYnXdS)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``. z-Cipher object can only be used for encryptionÚencÚdecN)r"r#r(r)rÚstrÚreplace)r&r,r+Úerrrr"ªs  zChaCha20Cipher.decryptcCsVt|dƒ\}}|d@}|d?}t |j ¡t|ƒt|ƒ|¡}|rRtd||jfƒ‚dS)z¦Seek to a certain position in the key stream. Args: position (integer): The absolute position within the key stream, in bytes. é@lÿÿr zError %d while seeking with %sN)ÚdivmodrZ chacha20_seekr$r%r rr )r&ÚpositionÚoffsetZ block_lowZ block_highrrrrÚseekÀsüzChaCha20Cipher.seek)N)N) Ú__name__Ú __module__Ú __qualname__Ú__doc__Ú block_sizer'r!r)r"r6rrrrrRs  rcCs†t|ƒdkrtdƒ‚|dkr*tdƒ}}n0t|ƒdkr@d|}nt|ƒdkrR|}ntdƒ‚t||d� d ¡}|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)rrrÚnewr!)rrZ padded_nonceÚrsrrrÚ_derive_Poly1305_key_pairÖs    r@c Ks z| d¡}Wn.tk r<}ztd|ƒ‚W5d}~XYnX| dd¡}|dkrZtdƒ}t|ƒdkrntdƒ‚t|ƒdkr‚td ƒ‚|r–td 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: )ÚpopÚKeyErrorr(rrrr/r)Úkwargsrr1rrrrr>ós   r>rr N)Z Crypto.RandomrZCrypto.Util.py3compatrZCrypto.Util._raw_apirrrrrr r r r rrÚobjectrr@r>r;Úkey_sizerrrrÚs  ,ÿ)