ff)-ddlmZddlmZddlmZddlmZmZddl m Z ddl m Z ddl mZmZddlmZd Zed d d ZGddeZdZdZdS)) unhexlify)ChaCha20) _HChaCha20)Poly1305BLAKE2s)get_random_bytes) long_to_bytes) _copy_bytesbord) is_bufferc $tdd|S)NEnum)type)enumss /builddir/build/BUILD/imunify360-venv-2.3.5/opt/imunify360/venv/lib64/python3.11/site-packages/Crypto/Cipher/ChaCha20_Poly1305.py_enumr,s E " "")PROCESSING_AUTH_DATAPROCESSING_CIPHERTEXTPROCESSING_DONEc^eZdZdZdZdZdZddZddZdZ d Z d Z d Z d Z d ZdZdS)ChaCha20Poly1305CipherzChaCha20-Poly1305 and XChaCha20-Poly1305 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: byte string c d|_tj||t|_tj|||_|jdd|_d|_d|_ tj |_ dS)z`Initialize a ChaCha20-Poly1305 AEAD cipher object See also `new()` at the module level.)updateencryptdecryptdigestverify)keynoncecipher)r#r$@rN) _nextrnewr_authenticator_cipherseek_len_aad_len_ct_mac_tag _CipherStatusr_status)selfr#r$s r__init__zChaCha20Poly1305Cipher.__init__=su   'ls%QQQ|5999  "   $9 rcd|jvrtd|xjt|z c_|j|dS)aProtect the associated data. Associated data (also known as *additional authenticated data* - AAD) is the piece of the message that must stay in the clear, while still allowing the receiver to verify its integrity. An example is packet headers. The associated data (possibly split into multiple segments) is fed into :meth:`update` before any call to :meth:`decrypt` or :meth:`encrypt`. If there is no associated data, :meth:`update` is not called. :param bytes/bytearray/memoryview assoc_data: A piece of associated data. There are no restrictions on its size. rz update() method cannot be calledN)r' TypeErrorr,lenr)r)r1datas rrzChaCha20Poly1305Cipher.updateOsS 4: % %>?? ? T"  ""4(((((rc|jtjksJ|jdzr(|jdd|jdzz ztj|_dS)N)r0r/rr,r)rrr1s r_pad_aadzChaCha20Poly1305Cipher._pad_aades^|}AAAAA =4  P   & &w" 8L2M'N O O O$: rNcvd|jvrtd|jtjkr|d|_|j||}|xjt|z c_||j |n|j ||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``. rz!encrypt() method cannot be calledrr!output) r'r4r0r/rr<r*rr-r5r)r)r1 plaintextr@results rrzChaCha20Poly1305Cipher.encryptls DJ & &?@@ @ <== = = MMOOO* %%i%?? I& >   & &v . . . .   & &v . . . rc8d|jvrtd|jtjkr|d|_|xjt|z c_|j ||j ||S)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``. r z!decrypt() method cannot be calledr r"r?) r'r4r0r/rr<r-r5r)rr*r )r1 ciphertextr@s rr zChaCha20Poly1305Cipher.decrypts DJ & &?@@ @ <== = = MMOOO*  J'  "":...|##Jv#>>>rct|jr|jtjksJ|jS|jtjksJ|jtjkr||jdzr(|jdd|jdzz ztj|_|jt|j dddd|jt|jdddd|j |_|jS)z=Finalize the cipher (if not done already) and return the MAC.r8r9r:N) r.r0r/rrr<r-r)rr r,r!r;s r _compute_macz#ChaCha20Poly1305Cipher._compute_macs = !<=#@@@@@= |}<<<<< <== = = MMOOO <$  O   & &w" t8K2L'M N N N$4  ""=#B#B44R4#HIII ""=q#A#A$$B$#GHHH+2244 }rchd|jvrtdd|_|S)zgCompute the *binary* authentication tag (MAC). :Return: the MAC tag, as 16 ``bytes``. r!z digest() method cannot be called)r!)r'r4rIr;s rr!zChaCha20Poly1305Cipher.digests9 4: % %>?? ?    """rcddd|DS)zCompute the *printable* authentication tag (MAC). This method is like :meth:`digest`. :Return: the MAC tag, as a hexadecimal string. c2g|]}dt|zS)z%02x)r ).0xs r z4ChaCha20Poly1305Cipher.hexdigest..s#@@@Qa(@@@r)joinr!r;s r hexdigestz ChaCha20Poly1305Cipher.hexdigests-ww@@$++--@@@AAArcbd|jvrtdd|_td}|t jd||j}t jd||}||krtddS) aValidate the *binary* authentication tag (MAC). The receiver invokes this method at the very end, to check if the associated data (if any) and the decrypted messages are valid. :param bytes/bytearray/memoryview received_mac_tag: This is the 16-byte *binary* MAC, as received from the sender. :Raises ValueError: if the MAC does not match. The message has been tampered with or the key is incorrect. r"z3verify() cannot be called when encrypting a message)r"r:) digest_bitsr#r6zMAC check failedN) r'r4rrIrr(r.r! ValueError)r1received_mac_tagsecretmac1mac2s rr"zChaCha20Poly1305Cipher.verifys 4: % %9:: :  !"%% {s $ ///{s 0222 ;;==DKKMM ) )/00 0 * )rcJ|t|dS)a@Validate the *printable* authentication tag (MAC). This method is like :meth:`verify`. :param string hex_mac_tag: This is the *printable* MAC. :Raises ValueError: if the MAC does not match. The message has been tampered with or the key is incorrect. N)r"r)r1 hex_mac_tags r hexverifyz ChaCha20Poly1305Cipher.hexverifys$ Ik**+++++rcT|||fS)aHPerform :meth:`encrypt` and :meth:`digest` in one step. :param plaintext: The data to encrypt, of any size. :type plaintext: bytes/bytearray/memoryview :return: a tuple with two ``bytes`` objects: - the ciphertext, of equal length as the plaintext - the 16-byte MAC tag r>)r1rAs rencrypt_and_digestz)ChaCha20Poly1305Cipher.encrypt_and_digests#||I&& 55rcZ||}|||S)aPerform :meth:`decrypt` and :meth:`verify` in one step. :param ciphertext: The piece of data to decrypt. :type ciphertext: bytes/bytearray/memoryview :param bytes received_mac_tag: This is the 16-byte *binary* MAC, as received from the sender. :return: the decrypted data (as ``bytes``) :raises ValueError: if the MAC does not match. The message has been tampered with or the key is incorrect. rD)r1rErWrAs rdecrypt_and_verifyz)ChaCha20Poly1305Cipher.decrypt_and_verifys.LL,,  $%%%r)N)__name__ __module__ __qualname____doc__r2rr<rr rIr!rRr"r]r_rarrrrr5s:::$))),;;;:????2, # # #BBB111> , , , 6 6 6rrc  |d}n$#t$r}td|zd}~wwxYwt |dkrtd|dd}|td}t |dvr|}nHt |d kr&t||dd }d |d dz}ntd t|std |rtdt|zt||}tdd||_|S)aCreate a new ChaCha20-Poly1305 or XChaCha20-Poly1305 AEAD cipher. :keyword key: The secret key to use. It must be 32 bytes long. :type key: byte string :keyword nonce: A value that must never be reused for any other encryption done with this key. For ChaCha20-Poly1305, it must be 8 or 12 bytes long. For XChaCha20-Poly1305, it must be 24 bytes long. If not provided, 12 ``bytes`` will be generated randomly (you can find them back in the ``nonce`` attribute). :type nonce: bytes, bytearray, memoryview :Return: a :class:`Crypto.Cipher.ChaCha20.ChaCha20Poly1305Cipher` object r#zMissing parameter %sN zKey must be 32 bytes longr$ )rGrhr:sz$Nonce must be 8, 12 or 24 bytes longz,nonce must be bytes, bytearray or memoryviewzUnknown parameters: )popKeyErrorr4r1r-r5rArVrrr strrr r$)kwargsr#er$chacha20_poly1305_noncer%s rr(r(sc*'jj '''.2333'  3xx2~~4555 JJw % %E } $$ 5zzW"' Ur  eCRCj))"5bcc "B?@@@ U  HFGGG >.V<=== #C)@ A AFtT511FL Ms 949rgN)binasciir Crypto.CipherrCrypto.Cipher.ChaCha20r Crypto.Hashrr Crypto.RandomrCrypto.Util.numberr Crypto.Util.py3compatr r Crypto.Util._raw_apir rr/objectrr(key_sizerrrrzs >""""""------))))))))******,,,,,,33333333******###1,-&'))) aaaaaVaaaH333n r