U q`C@sddlmZmZmZddlmZddlmZm Z ddl m Z e Z e Ze Ze Ze ZeeZe Ze Ze Ze Ze Z e eZ!e "Z#e $Z%e &Z'e (Z)e *Z+e+e)Z,ddZ-ddZ.d d Z/d d Z0d dZ1ddZ2dS))absolute_importdivisionprint_function) exceptions)ffilib)ensurec Cs(tt|tdtjdt|}t|tkdttjdtt|tpJ|dkdtjdtt|tolt|t kdt tjdtt|tot|t kdt tjd|r|}t|}n t j }d}|t }t d }t d |} t| |||||t j || } t| dkd tjdt | |dddS) aP Encrypt the given ``message`` using the IETF ratified chacha20poly1305 construction described in RFC7539. :param message: :type message: bytes :param aad: :type aad: bytes :param nonce: :type nonce: bytes :param key: :type key: bytes :return: authenticated ciphertext :rtype: bytes Input message type must be bytesZraising&Message must be at most {0} bytes longN%Additional data must be bytes or None-Nonce must be a {0} bytes long bytes sequence+Key must be a {0} bytes long bytes sequencerunsigned long long *unsigned char[]Encryption failed.)r isinstancebytesexc TypeErrorlen2crypto_aead_chacha20poly1305_ietf_MESSAGEBYTES_MAXformat ValueError+crypto_aead_chacha20poly1305_ietf_NPUBBYTES*crypto_aead_chacha20poly1305_ietf_KEYBYTESrNULL(crypto_aead_chacha20poly1305_ietf_ABYTESnewr)crypto_aead_chacha20poly1305_ietf_encrypt CryptoErrorbuffer messageaadnoncekeymlen_aadaalenmxoutclen ciphertextresr.@/tmp/pip-target-nv4zd3e_/lib/python/nacl/bindings/crypto_aead.pyrFs`         rc Cs(tt|tdtjdt|}t|tkdttjdtt|tpJ|dkdtjdtt|tolt|t kdt tjdtt|tot|t kdt tjd|t }t d}t d |}|r|}t|} n t j}d } t||t j|||| || } t| d kd tjdt ||d ddS) aH Decrypt the given ``ciphertext`` using the IETF ratified chacha20poly1305 construction described in RFC7539. :param ciphertext: :type ciphertext: bytes :param aad: :type aad: bytes :param nonce: :type nonce: bytes :param key: :type key: bytes :return: message :rtype: bytes #Input ciphertext type must be bytesr )Ciphertext must be at most {0} bytes longNr r rrrrDecryption failed.)rrrrrr*_aead_chacha20poly1305_ietf_CRYPTBYTES_MAXrrrrrrrrr)crypto_aead_chacha20poly1305_ietf_decryptr r! r,r$r%r&r+r*r'r#r(r)r-r.r.r/r4sb         r4c Cs0tt|tdtjdt|}t|tkdttjdtt|tpJ|dkdtjdtt|tolt|t kdt tjdtt|tot|t kdt tjd|r|}t|}n t j }d}t|}|t }t d }t d |} t| |||||t j || } t| dkd tjdt | |dddS) aQ Encrypt the given ``message`` using the "legacy" construction described in draft-agl-tls-chacha20poly1305. :param message: :type message: bytes :param aad: :type aad: bytes :param nonce: :type nonce: bytes :param key: :type key: bytes :return: authenticated ciphertext :rtype: bytes r r r Nr r rrrrr)rrrrrr-crypto_aead_chacha20poly1305_MESSAGEBYTES_MAXrr&crypto_aead_chacha20poly1305_NPUBBYTES%crypto_aead_chacha20poly1305_KEYBYTESrrrrr$crypto_aead_chacha20poly1305_encryptr r!r"r.r.r/r9sb         r9c Cs(tt|tdtjdt|}t|tkdttjdtt|tpJ|dkdtjdtt|tolt|t kdt tjdtt|tot|t kdt tjd|t }t d}t d |}|r|}t|} n t j}d } t||t j|||| || } t| d kd tjdt ||d ddS) ab Decrypt the given ``ciphertext`` using the "legacy" construction described in draft-agl-tls-chacha20poly1305. :param ciphertext: authenticated ciphertext :type ciphertext: bytes :param aad: :type aad: bytes :param nonce: :type nonce: bytes :param key: :type key: bytes :return: message :rtype: bytes r0r r1Nr r rrrrr2)rrrrrr%_aead_chacha20poly1305_CRYPTBYTES_MAXrrr7r8#crypto_aead_chacha20poly1305_ABYTESrrrr$crypto_aead_chacha20poly1305_decryptr r!r5r.r.r/r<sb         r<c Cs0tt|tdtjdt|}t|tkdttjdtt|tpJ|dkdtjdtt|tolt|t kdt tjdtt|tot|t kdt tjd|r|}t|}n t j }d}t|}|t }t d }t d |} t| |||||t j || } t| dkd tjdt | |dddS) a: Encrypt the given ``message`` using the long-nonces xchacha20poly1305 construction. :param message: :type message: bytes :param aad: :type aad: bytes :param nonce: :type nonce: bytes :param key: :type key: bytes :return: authenticated ciphertext :rtype: bytes r r r Nr r rrrrr)rrrrrr3crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAXrr,crypto_aead_xchacha20poly1305_ietf_NPUBBYTES+crypto_aead_xchacha20poly1305_ietf_KEYBYTESrr)crypto_aead_xchacha20poly1305_ietf_ABYTESrr*crypto_aead_xchacha20poly1305_ietf_encryptr r!r"r.r.r/rA]sb         rAc Cs(tt|tdtjdt|}t|tkdttjdtt|tpJ|dkdtjdtt|tolt|t kdt tjdtt|tot|t kdt tjd|t }t d}t d |}|r|}t|} n t j}d } t||t j|||| || } t| d kd tjdt ||d ddS) aK Decrypt the given ``ciphertext`` using the long-nonces xchacha20poly1305 construction. :param ciphertext: authenticated ciphertext :type ciphertext: bytes :param aad: :type aad: bytes :param nonce: :type nonce: bytes :param key: :type key: bytes :return: message :rtype: bytes r0r r1Nr r rrrrr2)rrrrrr+_aead_xchacha20poly1305_ietf_CRYPTBYTES_MAXrrr>r?r@rrrr*crypto_aead_xchacha20poly1305_ietf_decryptr r!r5r.r.r/rCsb         rCN)3 __future__rrrZnaclrrZ nacl._sodiumrrZnacl.exceptionsrZ*crypto_aead_chacha20poly1305_ietf_keybytesrZ+crypto_aead_chacha20poly1305_ietf_nsecbytesZ+crypto_aead_chacha20poly1305_ietf_NSECBYTESZ+crypto_aead_chacha20poly1305_ietf_npubbytesrZ(crypto_aead_chacha20poly1305_ietf_abytesrZ2crypto_aead_chacha20poly1305_ietf_messagebytes_maxrr3Z%crypto_aead_chacha20poly1305_keybytesr8Z&crypto_aead_chacha20poly1305_nsecbytesZ&crypto_aead_chacha20poly1305_NSECBYTESZ&crypto_aead_chacha20poly1305_npubbytesr7Z#crypto_aead_chacha20poly1305_abytesr;Z-crypto_aead_chacha20poly1305_messagebytes_maxr6r:Z+crypto_aead_xchacha20poly1305_ietf_keybytesr?Z,crypto_aead_xchacha20poly1305_ietf_nsecbytesZ,crypto_aead_xchacha20poly1305_ietf_NSECBYTESZ,crypto_aead_xchacha20poly1305_ietf_npubbytesr>Z)crypto_aead_xchacha20poly1305_ietf_abytesr@Z3crypto_aead_xchacha20poly1305_ietf_messagebytes_maxr=rBrr4r9r<rArCr.r.r.r/sf  EFFFF