U
    ֫[C                     @   s   d dl mZmZmZ d dlmZ d dlmZm	Z	 d dl
mZ e	 Ze	 Ze	 Ze	 Ze	 Zee Ze	 Ze	 Ze	 Ze	 Ze	 Z e e Z!e	" Z#e	$ Z%e	& Z'e	( Z)e	* Z+e+e) Z,dd Z-dd Z.d	d
 Z/dd Z0dd Z1dd Z2dS )    )absolute_importdivisionprint_function)
exceptions)ffilib)ensurec                 C   s(  t t| tdtjd t| }t |tkdttjd t t|tpJ|dkdtjd t t|tolt|t	kdt	tjd t t|tot|t
kdt
tjd |r|}t|}n
tj}d}|t }td	}td
|}	t|	|| |||tj||	}
t |
dkdtjd t|	|d dd S )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 bytesZraising&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 sequencer   unsigned long long *unsigned char[]Encryption failed.)r   
isinstancebytesexc	TypeErrorlen2crypto_aead_chacha20poly1305_ietf_MESSAGEBYTES_MAXformat
ValueError+crypto_aead_chacha20poly1305_ietf_NPUBBYTES*crypto_aead_chacha20poly1305_ietf_KEYBYTESr   NULL(crypto_aead_chacha20poly1305_ietf_ABYTESnewr   )crypto_aead_chacha20poly1305_ietf_encryptCryptoErrorbuffermessageaadnoncekeymlen_aadaalenmxoutclen
ciphertextres r.   ;/usr/lib/python3/dist-packages/nacl/bindings/crypto_aead.pyr   F   s`    




r   c                 C   s(  t t| tdtjd t| }t |tkdttjd t t|tpJ|dkdtjd t t|tolt|t	kdt	tjd t t|tot|t
kdt
tjd |t }td}td	|}|r|}t|}	n
tj}d
}	t||tj| |||	||	}
t |
d
kdtjd t||d
 dd S )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   r   r   r   r   Decryption failed.)r   r   r   r   r   r   *_aead_chacha20poly1305_ietf_CRYPTBYTES_MAXr   r   r   r   r   r   r   r   r   )crypto_aead_chacha20poly1305_ietf_decryptr    r!   r,   r$   r%   r&   r+   r*   r'   r#   r(   r)   r-   r.   r.   r/   r4      sb    





r4   c                 C   s0  t t| tdtjd t| }t |tkdttjd t t|tpJ|dkdtjd t t|tolt|t	kdt	tjd t t|tot|t
kdt
tjd |r|}t|}n
tj}d}t| }|t }td	}td
|}	t|	|| |||tj||	}
t |
dkdtjd t|	|d dd S )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   r   r   r   r   r   )r   r   r   r   r   r   -crypto_aead_chacha20poly1305_MESSAGEBYTES_MAXr   r   &crypto_aead_chacha20poly1305_NPUBBYTES%crypto_aead_chacha20poly1305_KEYBYTESr   r   r   r   r   $crypto_aead_chacha20poly1305_encryptr    r!   r"   r.   r.   r/   r9      sb    




r9   c                 C   s(  t t| tdtjd t| }t |tkdttjd t t|tpJ|dkdtjd t t|tolt|t	kdt	tjd t t|tot|t
kdt
tjd |t }td}td	|}|r|}t|}	n
tj}d
}	t||tj| |||	||	}
t |
d
kdtjd t||d
 dd S )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
    r0   r
   r1   Nr   r   r   r   r   r   r2   )r   r   r   r   r   r   %_aead_chacha20poly1305_CRYPTBYTES_MAXr   r   r7   r8   #crypto_aead_chacha20poly1305_ABYTESr   r   r   r   $crypto_aead_chacha20poly1305_decryptr    r!   r5   r.   r.   r/   r<     sb    





r<   c                 C   s0  t t| tdtjd t| }t |tkdttjd t t|tpJ|dkdtjd t t|tolt|t	kdt	tjd t t|tot|t
kdt
tjd |r|}t|}n
tj}d}t| }|t }td	}td
|}	t|	|| |||tj||	}
t |
dkdtjd t|	|d dd S )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   r   r   r   r   r   )r   r   r   r   r   r   3crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAXr   r   ,crypto_aead_xchacha20poly1305_ietf_NPUBBYTES+crypto_aead_xchacha20poly1305_ietf_KEYBYTESr   r   )crypto_aead_xchacha20poly1305_ietf_ABYTESr   r   *crypto_aead_xchacha20poly1305_ietf_encryptr    r!   r"   r.   r.   r/   rA   ]  sb    




rA   c                 C   s(  t t| tdtjd t| }t |tkdttjd t t|tpJ|dkdtjd t t|tolt|t	kdt	tjd t t|tot|t
kdt
tjd |t }td}td	|}|r|}t|}	n
tj}d
}	t||tj| |||	||	}
t |
d
kdtjd t||d
 dd S )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
    r0   r
   r1   Nr   r   r   r   r   r   r2   )r   r   r   r   r   r   +_aead_xchacha20poly1305_ietf_CRYPTBYTES_MAXr   r   r>   r?   r@   r   r   r   r   *crypto_aead_xchacha20poly1305_ietf_decryptr    r!   r5   r.   r.   r/   rC     sb    





rC   N)3Z
__future__r   r   r   Znaclr   r   Znacl._sodiumr   r   Znacl.exceptionsr   Z*crypto_aead_chacha20poly1305_ietf_keybytesr   Z+crypto_aead_chacha20poly1305_ietf_nsecbytesZ+crypto_aead_chacha20poly1305_ietf_NSECBYTESZ+crypto_aead_chacha20poly1305_ietf_npubbytesr   Z(crypto_aead_chacha20poly1305_ietf_abytesr   Z2crypto_aead_chacha20poly1305_ietf_messagebytes_maxr   r3   Z%crypto_aead_chacha20poly1305_keybytesr8   Z&crypto_aead_chacha20poly1305_nsecbytesZ&crypto_aead_chacha20poly1305_NSECBYTESZ&crypto_aead_chacha20poly1305_npubbytesr7   Z#crypto_aead_chacha20poly1305_abytesr;   Z-crypto_aead_chacha20poly1305_messagebytes_maxr6   r:   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=   rB   r   r4   r9   r<   rA   rC   r.   r.   r.   r/   <module>   sf   	EFFFF