a b<@sUddlmZddlmZddlmZmZddlm Z e Z e e d<eZe e d<eZe e d<eZe e d<eZe e d <eeZeZe e d <eZe e d <eZe e d <eZe e d <eZ e e d<e eZ!e"Z#e e d<e$Z%e e d<e&Z'e e d<e(Z)e e d<e*Z+e e d<e+e)Z,e-ee-e-e-e-dddZ.e-ee-e-e-e-dddZ/e-ee-e-e-e-dddZ0e-ee-e-e-e-dddZ1e-ee-e-e-e-dddZ2e-ee-e-e-e-dd d!Z3d"S)#)Optional) exceptions)ffilib)ensure*crypto_aead_chacha20poly1305_ietf_KEYBYTES+crypto_aead_chacha20poly1305_ietf_NSECBYTES+crypto_aead_chacha20poly1305_ietf_NPUBBYTES(crypto_aead_chacha20poly1305_ietf_ABYTES2crypto_aead_chacha20poly1305_ietf_MESSAGEBYTES_MAX%crypto_aead_chacha20poly1305_KEYBYTES&crypto_aead_chacha20poly1305_NSECBYTES&crypto_aead_chacha20poly1305_NPUBBYTES#crypto_aead_chacha20poly1305_ABYTES-crypto_aead_chacha20poly1305_MESSAGEBYTES_MAX+crypto_aead_xchacha20poly1305_ietf_KEYBYTES,crypto_aead_xchacha20poly1305_ietf_NSECBYTES,crypto_aead_xchacha20poly1305_ietf_NPUBBYTES)crypto_aead_xchacha20poly1305_ietf_ABYTES3crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX)messageaadnoncekeyreturnc Cs(tt|tdtjdt|}t|tkdttjdtt|tpJ|dudtjdtt|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) aZ Encrypt the given ``message`` using the IETF ratified chacha20poly1305 construction described in RFC7539. :param message: :type message: bytes :param aad: :type aad: Optional[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 {} bytes longN%Additional data must be bytes or None,Nonce must be a {} bytes long bytes sequence*Key must be a {} bytes long bytes sequencerunsigned long long *unsigned char[]Encryption failed.)r isinstancebytesexc TypeErrorlenr format ValueErrorr rrNULLr newr)crypto_aead_chacha20poly1305_ietf_encrypt CryptoErrorbuffer rrrrmlen_aadaalenmxoutclen ciphertextresr8@/tmp/pip-target-98j97qn4/lib/python/nacl/bindings/crypto_aead.pyr-Ws^         r-)r6rrrrc Cs(tt|tdtjdt|}t|tkdttjdtt|tpJ|dudtjdtt|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) aR Decrypt the given ``ciphertext`` using the IETF ratified chacha20poly1305 construction described in RFC7539. :param ciphertext: :type ciphertext: bytes :param aad: :type aad: Optional[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 {} bytes longNrrr r!r"rDecryption failed.)rr$r%r&r'r(*_aead_chacha20poly1305_ietf_CRYPTBYTES_MAXr)r*r rr rr,r+r)crypto_aead_chacha20poly1305_ietf_decryptr.r/ r6rrrr5r4r1rr2r3r7r8r8r9r>s^         r>c Cs0tt|tdtjdt|}t|tkdttjdtt|tpJ|dudtjdtt|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 "legacy" construction described in draft-agl-tls-chacha20poly1305. :param message: :type message: bytes :param aad: :type aad: Optional[bytes] :param nonce: :type nonce: bytes :param key: :type key: bytes :return: authenticated ciphertext :rtype: bytes rrrNrrr rr!r"r#)rr$r%r&r'r(rr)r*rr rr+r r,r$crypto_aead_chacha20poly1305_encryptr.r/r0r8r8r9r@s`         r@c Cs(tt|tdtjdt|}t|tkdttjdtt|tpJ|dudtjdtt|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) al 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: Optional[bytes] :param nonce: :type nonce: bytes :param key: :type key: bytes :return: message :rtype: bytes r:rr;Nrrr r!r"rr<)rr$r%r&r'r(%_aead_chacha20poly1305_CRYPTBYTES_MAXr)r*rr rrr,r+r$crypto_aead_chacha20poly1305_decryptr.r/r?r8r8r9rBEs^         rBc Cs0tt|tdtjdt|}t|tkdttjdtt|tpJ|dudtjdtt|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) aD Encrypt the given ``message`` using the long-nonces xchacha20poly1305 construction. :param message: :type message: bytes :param aad: :type aad: Optional[bytes] :param nonce: :type nonce: bytes :param key: :type key: bytes :return: authenticated ciphertext :rtype: bytes rrrNrrr rr!r"r#)rr$r%r&r'r(rr)r*rrrr+rr,r*crypto_aead_xchacha20poly1305_ietf_encryptr.r/r0r8r8r9rCs`         rCc Cs(tt|tdtjdt|}t|tkdttjdtt|tpJ|dudtjdtt|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) aU Decrypt the given ``ciphertext`` using the long-nonces xchacha20poly1305 construction. :param ciphertext: authenticated ciphertext :type ciphertext: bytes :param aad: :type aad: Optional[bytes] :param nonce: :type nonce: bytes :param key: :type key: bytes :return: message :rtype: bytes r:rr;Nrrr r!r"rr<)rr$r%r&r'r(+_aead_xchacha20poly1305_ietf_CRYPTBYTES_MAXr)r*rrrrr,r+r*crypto_aead_xchacha20poly1305_ietf_decryptr.r/r?r8r8r9rEs^         rEN)4typingrZnaclrr&Z nacl._sodiumrrZnacl.exceptionsrZ*crypto_aead_chacha20poly1305_ietf_keybytesrint__annotations__Z+crypto_aead_chacha20poly1305_ietf_nsecbytesrZ+crypto_aead_chacha20poly1305_ietf_npubbytesr Z(crypto_aead_chacha20poly1305_ietf_abytesr Z2crypto_aead_chacha20poly1305_ietf_messagebytes_maxr r=Z%crypto_aead_chacha20poly1305_keybytesr Z&crypto_aead_chacha20poly1305_nsecbytesr Z&crypto_aead_chacha20poly1305_npubbytesrZ#crypto_aead_chacha20poly1305_abytesrZ-crypto_aead_chacha20poly1305_messagebytes_maxrrAZ+crypto_aead_xchacha20poly1305_ietf_keybytesrZ,crypto_aead_xchacha20poly1305_ietf_nsecbytesrZ,crypto_aead_xchacha20poly1305_ietf_npubbytesrZ)crypto_aead_xchacha20poly1305_ietf_abytesrZ3crypto_aead_xchacha20poly1305_ietf_messagebytes_maxrrDr%r-r>r@rBrCrEr8r8r8r9s~                   P  P  Q  P  Q