3 L]C@sddlmZmZmZddlmZddlmZm Z ddl m Z e j Z e jZe jZe jZe jZeeZe jZe jZe jZe jZe jZ e eZ!e j"Z#e j$Z%e j&Z'e j(Z)e j*Z+e+e)Z,ddZ-ddZ.d d Z/d d Z0d dZ1ddZ2dS))absolute_importdivisionprint_function) exceptions)ffilib)ensurec Cs(tt|tdtjdt|}t|tkdjttjdtt|tpJ|dkdtjdtt|tolt|t kdjt tjdtt|tot|t kdjt tjd|r|}t|}n t j }d}|t }t jd }t jd |} tj| |||||t j || } t| dkd tjdt j| |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 z Input message type must be bytes)raisingz&Message must be at most {0} bytes longNz%Additional data must be bytes or Nonez-Nonce must be a {0} bytes long bytes sequencez+Key must be a {0} bytes long bytes sequencerzunsigned long long *zunsigned char[]zEncryption 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-install-wfra5znf/pynacl/nacl/bindings/crypto_aead.pyrFsN             rc Cs(tt|tdtjdt|}t|tkdjttjdtt|tpJ|dkdtjdtt|tolt|t kdjt tjdtt|tot|t kdjt tjd|t }t j d}t j d |}|r|}t|} n t j}d } tj||t j|||| || } t| d kd tjdt j||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 z#Input ciphertext type must be bytes)r z)Ciphertext must be at most {0} bytes longNz%Additional data must be bytes or Nonez-Nonce must be a {0} bytes long bytes sequencez+Key must be a {0} bytes long bytes sequencezunsigned long long *zunsigned char[]rzDecryption failed.)rr r r r r*_aead_chacha20poly1305_ietf_CRYPTBYTES_MAXrrrrrrrrr)crypto_aead_chacha20poly1305_ietf_decryptrr) r#rrrr"r!rrrr r$r%r%r&r(sP             r(c Cs0tt|tdtjdt|}t|tkdjttjdtt|tpJ|dkdtjdtt|tolt|t kdjt tjdtt|tot|t kdjt tjd|r|}t|}n t j }d}t|}|t }t jd }t jd |} tj| |||||t j || } t| dkd tjdt j| |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 z Input message type must be bytes)r z&Message must be at most {0} bytes longNz%Additional data must be bytes or Nonez-Nonce must be a {0} bytes long bytes sequencez+Key must be a {0} bytes long bytes sequencerzunsigned long long *zunsigned char[]zEncryption failed.)rr r r r r-crypto_aead_chacha20poly1305_MESSAGEBYTES_MAXrr&crypto_aead_chacha20poly1305_NPUBBYTES%crypto_aead_chacha20poly1305_KEYBYTESrrrrr$crypto_aead_chacha20poly1305_encryptrr) rrrrrrr r!r"r#r$r%r%r&r,sP             r,c Cs(tt|tdtjdt|}t|tkdjttjdtt|tpJ|dkdtjdtt|tolt|t kdjt tjdtt|tot|t kdjt tjd|t }t j d}t j d |}|r|}t|} n t j}d } tj||t j|||| || } t| d kd tjdt j||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 z#Input ciphertext type must be bytes)r z)Ciphertext must be at most {0} bytes longNz%Additional data must be bytes or Nonez-Nonce must be a {0} bytes long bytes sequencez+Key must be a {0} bytes long bytes sequencezunsigned long long *zunsigned char[]rzDecryption failed.)rr r r r r%_aead_chacha20poly1305_CRYPTBYTES_MAXrrr*r+#crypto_aead_chacha20poly1305_ABYTESrrrr$crypto_aead_chacha20poly1305_decryptrr) r#rrrr"r!rrrr r$r%r%r&r/sP             r/c Cs0tt|tdtjdt|}t|tkdjttjdtt|tpJ|dkdtjdtt|tolt|t kdjt tjdtt|tot|t kdjt tjd|r|}t|}n t j }d}t|}|t }t jd }t jd |} tj| |||||t j || } t| dkd tjdt j| |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 z Input message type must be bytes)r z&Message must be at most {0} bytes longNz%Additional data must be bytes or Nonez-Nonce must be a {0} bytes long bytes sequencez+Key must be a {0} bytes long bytes sequencerzunsigned long long *zunsigned char[]zEncryption failed.)rr r r r r3crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAXrr,crypto_aead_xchacha20poly1305_ietf_NPUBBYTES+crypto_aead_xchacha20poly1305_ietf_KEYBYTESrr)crypto_aead_xchacha20poly1305_ietf_ABYTESrr*crypto_aead_xchacha20poly1305_ietf_encryptrr) rrrrrrr r!r"r#r$r%r%r&r4]sP             r4c Cs(tt|tdtjdt|}t|tkdjttjdtt|tpJ|dkdtjdtt|tolt|t kdjt tjdtt|tot|t kdjt tjd|t }t j d}t j d |}|r|}t|} n t j}d } tj||t j|||| || } t| d kd tjdt j||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 z#Input ciphertext type must be bytes)r z)Ciphertext must be at most {0} bytes longNz%Additional data must be bytes or Nonez-Nonce must be a {0} bytes long bytes sequencez+Key must be a {0} bytes long bytes sequencezunsigned long long *zunsigned char[]rzDecryption failed.)rr r r r r+_aead_xchacha20poly1305_ietf_CRYPTBYTES_MAXrrr1r2r3rrrr*crypto_aead_xchacha20poly1305_ietf_decryptrr) r#rrrr"r!rrrr r$r%r%r&r6sP             r6N)3 __future__rrrZnaclrr Z 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_maxrr'Z%crypto_aead_chacha20poly1305_keybytesr+Z&crypto_aead_chacha20poly1305_nsecbytesZ&crypto_aead_chacha20poly1305_NSECBYTESZ&crypto_aead_chacha20poly1305_npubbytesr*Z#crypto_aead_chacha20poly1305_abytesr.Z-crypto_aead_chacha20poly1305_messagebytes_maxr)r-Z+crypto_aead_xchacha20poly1305_ietf_keybytesr2Z,crypto_aead_xchacha20poly1305_ietf_nsecbytesZ,crypto_aead_xchacha20poly1305_ietf_NSECBYTESZ,crypto_aead_xchacha20poly1305_ietf_npubbytesr1Z)crypto_aead_xchacha20poly1305_ietf_abytesr3Z3crypto_aead_xchacha20poly1305_ietf_messagebytes_maxr0r5rr(r,r/r4r6r%r%r%r&s<  EFFFF