o
    FDizr                  
   @   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- Z.eed< e/ Z0eed< e1 Z2eed< e3 Z4eed< e5 Z6eed< e6e4 Z7e8 Z9eed< e: Z;eed< e< Z=eed< e> Z?eed< e@ ZAeed< eAe? Z7eB ZCeed< eD ZEeed< eF ZGeed < eH ZIeed!< eJ ZKeed"< eKeI Z7d#eLd$eeL d%eLd&eLd'eLf
d(d)ZMd*eLd$eeL d%eLd&eLd'eLf
d+d,ZNd#eLd$eeL d%eLd&eLd'eLf
d-d.ZOd*eLd$eeL d%eLd&eLd'eLf
d/d0ZPd#eLd$eeL d%eLd&eLd'eLf
d1d2ZQd*eLd$eeL d%eLd&eLd'eLf
d3d4ZRd#eLd$eeL d%eLd&eLd'eLf
d5d6ZSd*eLd$eeL d%eLd&eLd'eLf
d7d8ZTd#eLd$eeL d%eLd&eLd'eLf
d9d:ZUd*eLd$eeL d%eLd&eLd'eLf
d;d<ZVd#eLd$eeL d%eLd&eLd'eLf
d=d>ZWd*eLd$eeL d%eLd&eLd'eLf
d?d@ZXdAS )B    )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crypto_aead_aegis256_KEYBYTEScrypto_aead_aegis256_NSECBYTEScrypto_aead_aegis256_NPUBBYTEScrypto_aead_aegis256_ABYTES%crypto_aead_aegis256_MESSAGEBYTES_MAXcrypto_aead_aegis128l_KEYBYTEScrypto_aead_aegis128l_NSECBYTEScrypto_aead_aegis128l_NPUBBYTEScrypto_aead_aegis128l_ABYTES&crypto_aead_aegis128l_MESSAGEBYTES_MAXcrypto_aead_aes256gcm_KEYBYTEScrypto_aead_aes256gcm_NSECBYTEScrypto_aead_aes256gcm_NPUBBYTEScrypto_aead_aes256gcm_ABYTES&crypto_aead_aes256gcm_MESSAGEBYTES_MAXmessageaadnoncekeyreturnc                 C   (  t t| tdtjd t| }t |tkdttjd t t|tp%|du dtjd t t|to6t|t	kdt	tjd t t|toJt|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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 rI   R/var/www/Datamplify/venv/lib/python3.10/site-packages/nacl/bindings/crypto_aead.pyr>   x   ^   

	

	

r>   rG   c                 C   (  t t| tdtjd t| }t |tkdttjd t t|tp%|du dtjd t t|to6t|t	kdt	tjd t t|toJt|t
kdt
tjd |t }td}td	|}|rk|}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/   r0   r1   r2   r3   r   Decryption failed.)r   r5   r6   r7   r8   r9   *_aead_chacha20poly1305_ietf_CRYPTBYTES_MAXr:   r;   r	   r   r
   r   r=   r<   r   )crypto_aead_chacha20poly1305_ietf_decryptr?   r@   rG   r&   r'   r(   rF   rE   rB   r%   rC   rD   rH   rI   rI   rJ   rQ      ^   

	

	

rQ   c                 C   r*   )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/   r0   r1   r   r2   r3   r4   )r   r5   r6   r7   r8   r9   r   r:   r;   r   r   r   r<   r
   r=   r   $crypto_aead_chacha20poly1305_encryptr?   r@   rA   rI   rI   rJ   rT     rK   rT   c                 C   rL   )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
    rM   r,   rN   Nr/   r0   r1   r2   r3   r   rO   )r   r5   r6   r7   r8   r9   %_aead_chacha20poly1305_CRYPTBYTES_MAXr:   r;   r   r   r   r   r=   r<   r   $crypto_aead_chacha20poly1305_decryptr?   r@   rR   rI   rI   rJ   rV   e  rS   rV   c                 C   r*   )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/   r0   r1   r   r2   r3   r4   )r   r5   r6   r7   r8   r9   r   r:   r;   r   r   r   r<   r   r=   r   *crypto_aead_xchacha20poly1305_ietf_encryptr?   r@   rA   rI   rI   rJ   rW     rK   rW   c                 C   rL   )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
    rM   r,   rN   Nr/   r0   r1   r2   r3   r   rO   )r   r5   r6   r7   r8   r9   +_aead_xchacha20poly1305_ietf_CRYPTBYTES_MAXr:   r;   r   r   r   r   r=   r<   r   *crypto_aead_xchacha20poly1305_ietf_decryptr?   r@   rR   rI   rI   rJ   rY     s^   

	

	

rY   c                 C   r*   )a0  
    Encrypt the given ``message`` using the AEGIS-256
    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/   r0   r1   r   r2   r3   r4   )r   r5   r6   r7   r8   r9   r   r:   r;   r   r   r   r<   r   r=   r   crypto_aead_aegis256_encryptr?   r@   rA   rI   rI   rJ   rZ   Q  \   

	

rZ   c                 C   rL   )aA  
    Decrypt the given ``ciphertext`` using the AEGIS-256
    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
    rM   r,   rN   Nr/   r0   r1   r2   r3   r   rO   )r   r5   r6   r7   r8   r9   _aead_aegis256_CRYPTBYTES_MAXr:   r;   r   r   r   r   r=   r<   r   crypto_aead_aegis256_decryptr?   r@   rR   rI   rI   rJ   r]     \   

	

r]   c                 C   r*   )a1  
    Encrypt the given ``message`` using the AEGIS-128L
    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/   r0   r1   r   r2   r3   r4   )r   r5   r6   r7   r8   r9   r   r:   r;   r   r   r   r<   r   r=   r   crypto_aead_aegis128l_encryptr?   r@   rA   rI   rI   rJ   r_     r[   r_   c                 C   rL   )aB  
    Decrypt the given ``ciphertext`` using the AEGIS-128L
    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
    rM   r,   rN   Nr/   r0   r1   r2   r3   r   rO   )r   r5   r6   r7   r8   r9   r\   r:   r;   r   r   r   r   r=   r<   r   crypto_aead_aegis128l_decryptr?   r@   rR   rI   rI   rJ   r`   :  r^   r`   c                 C   s@  t t dkdtjd t t| tdtjd t| }t |t	kd
t	tjd t t|tp1|du dtjd t t|toBt|tkd
ttjd t t|toVt|tkd	
ttjd |rh|}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 )a{  
    Encrypt the given ``message`` using the AES-256-GCM
    construction.  Requires the Intel AES-NI extensions,
    or the ARM Crypto extensions.

    :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
       +Construction requires hardware accelerationr,   r+   r.   Nr/   r0   r1   r   r2   r3   r4   )r   r   "crypto_aead_aes256gcm_is_availabler7   UnavailableErrorr5   r6   r8   r9   r$   r:   r;   r"   r    r   r<   r#   r=   crypto_aead_aes256gcm_encryptr?   r@   rA   rI   rI   rJ   re     sf   


	

re   c                 C   s@  t t dkdtjd t t| tdtjd t| }t |t	kd
t	tjd t t|tp1|du dtjd t t|toBt|tkd
ttjd t t|toVt|tkd	
ttjd |t }td
}td|}|rw|}t|}	ntj}d}	t||tj| |||	||	}
t |
dkdtjd t||d dd S )a  
    Decrypt the given ``ciphertext`` using the AES-256-GCM
    construction.  Requires the Intel AES-NI extensions,
    or the ARM Crypto extensions.

    :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
    ra   rb   r,   rM   rN   Nr/   r0   r1   r2   r3   r   rO   )r   r   rc   r7   rd   r5   r6   r8   r9   r\   r:   r;   r"   r    r#   r   r=   r<   crypto_aead_aes256gcm_decryptr?   r@   rR   rI   rI   rJ   rf     sf   


	

rf   N)Ytypingr   naclr   r7   nacl._sodiumr   r   nacl.exceptionsr   *crypto_aead_chacha20poly1305_ietf_keybytesr   int__annotations__+crypto_aead_chacha20poly1305_ietf_nsecbytesr   +crypto_aead_chacha20poly1305_ietf_npubbytesr	   (crypto_aead_chacha20poly1305_ietf_abytesr
   2crypto_aead_chacha20poly1305_ietf_messagebytes_maxr   rP   %crypto_aead_chacha20poly1305_keybytesr   &crypto_aead_chacha20poly1305_nsecbytesr   &crypto_aead_chacha20poly1305_npubbytesr   #crypto_aead_chacha20poly1305_abytesr   -crypto_aead_chacha20poly1305_messagebytes_maxr   rU   +crypto_aead_xchacha20poly1305_ietf_keybytesr   ,crypto_aead_xchacha20poly1305_ietf_nsecbytesr   ,crypto_aead_xchacha20poly1305_ietf_npubbytesr   )crypto_aead_xchacha20poly1305_ietf_abytesr   3crypto_aead_xchacha20poly1305_ietf_messagebytes_maxr   rX   crypto_aead_aegis256_keybytesr   crypto_aead_aegis256_nsecbytesr   crypto_aead_aegis256_npubbytesr   crypto_aead_aegis256_abytesr   %crypto_aead_aegis256_messagebytes_maxr   r\   crypto_aead_aegis128l_keybytesr   crypto_aead_aegis128l_nsecbytesr   crypto_aead_aegis128l_npubbytesr   crypto_aead_aegis128l_abytesr   &crypto_aead_aegis128l_messagebytes_maxr   crypto_aead_aes256gcm_keybytesr    crypto_aead_aes256gcm_nsecbytesr!   crypto_aead_aes256gcm_npubbytesr"   crypto_aead_aes256gcm_abytesr#   &crypto_aead_aes256gcm_messagebytes_maxr$   r6   r>   rQ   rT   rV   rW   rY   rZ   r]   r_   r`   re   rf   rI   rI   rI   rJ   <module>   s  


















O
O
O
O
O
N
N
M
N
M
U