U
    cc<                     @   s  U d dl mZ d dlmZ d dlmZmZ d dlm	Z	 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 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" 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-ee- e-e-e-dddZ.e-ee- e-e-e-dddZ/e-ee- e-e-e-dddZ0e-ee- e-e-e-dddZ1e-ee- e-e-e-dddZ2e-ee- e-e-e-dd d!Z3d"S )#    )Optional)
exceptions)ffilib)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)messageaadnoncekeyreturnc                 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 )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 bytesZraising%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 sequencer   unsigned long long *unsigned char[]Encryption failed.)r   
isinstancebytesexc	TypeErrorlenr   format
ValueErrorr	   r   r   NULLr
   newr   )crypto_aead_chacha20poly1305_ietf_encryptCryptoErrorbufferr   r   r   r   mlen_aadaalenmxoutclen
ciphertextres r8   =/tmp/pip-unpacked-wheel-e6sdgn5m/nacl/bindings/crypto_aead.pyr-   W   sn    

	

	

        r-   )r6   r   r   r   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 )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 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/   r6   r   r   r   r5   r4   r1   r   r2   r3   r7   r8   r8   r9   r>      sn    

	

	

        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 "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
    r   r   r   Nr   r   r    r   r!   r"   r#   )r   r$   r%   r&   r'   r(   r   r)   r*   r   r   r   r+   r
   r,   r   $crypto_aead_chacha20poly1305_encryptr.   r/   r0   r8   r8   r9   r@      sp    

	

	

        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 )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:   r   r;   Nr   r   r    r!   r"   r   r<   )r   r$   r%   r&   r'   r(   %_aead_chacha20poly1305_CRYPTBYTES_MAXr)   r*   r   r   r   r   r,   r+   r   $crypto_aead_chacha20poly1305_decryptr.   r/   r?   r8   r8   r9   rB   E  sn    

	

	

        rB   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 )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
    r   r   r   Nr   r   r    r   r!   r"   r#   )r   r$   r%   r&   r'   r(   r   r)   r*   r   r   r   r+   r   r,   r   *crypto_aead_xchacha20poly1305_ietf_encryptr.   r/   r0   r8   r8   r9   rC     sp    

	

	

        rC   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 )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:   r   r;   Nr   r   r    r!   r"   r   r<   )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/   r?   r8   r8   r9   rE     sn    

	

	

        rE   N)4typingr   Znaclr   r&   Znacl._sodiumr   r   Znacl.exceptionsr   Z*crypto_aead_chacha20poly1305_ietf_keybytesr   int__annotations__Z+crypto_aead_chacha20poly1305_ietf_nsecbytesr   Z+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_npubbytesr   Z#crypto_aead_chacha20poly1305_abytesr   Z-crypto_aead_chacha20poly1305_messagebytes_maxr   rA   Z+crypto_aead_xchacha20poly1305_ietf_keybytesr   Z,crypto_aead_xchacha20poly1305_ietf_nsecbytesr   Z,crypto_aead_xchacha20poly1305_ietf_npubbytesr   Z)crypto_aead_xchacha20poly1305_ietf_abytesr   Z3crypto_aead_xchacha20poly1305_ietf_messagebytes_maxr   rD   r%   r-   r>   r@   rB   rC   rE   r8   r8   r8   r9   <module>   s    	














   P   P   Q   P   Q   