U
    Ųcc I  ć                
   @   s>  U d dl Z d dlmZ d dlmZ d dlmZmZ d dlm	Z	 e
ejZdZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zere e de ” ””dd Ze ” Ze  ” Ze !” Ze "” Ze #” Ze $” Ze %” Ze &” Ze '” Ze (” Ze )” Ze *” Ze +” Ze ,” Ze -” Z.e/e0d< e 1” Z2e/e0d< e 3” Z4e/e0d	< e 5” Z6e/e0d
< e 7” Z8e/e0d< e 9” Z:e/e0d< e ;” Z<e/e0d< e =” Z>e/e0d< e ?” Z@e/e0d< e e de A” ””dd ZBeCe0d< e D” ZEe/e0d< e F” ZGe/e0d< e H” ZIe/e0d< e J” ZKe/e0d< e L” ZMe/e0d< e N” ZOe/e0d< e P” ZQe/e0d< e R” ZSe/e0d< e T” ZUe/e0d< e V” ZWe/e0d< e e de X” ””dd ZYeCe0d< e Z” Z[e/e0d< e \” Z]e/e0d< e ^” Z_e/e0d< e `” Zae/e0d< e b” Zce/e0d < e d” Zee/e0d!< e f” Zge/e0d"< e h” Zie/e0d#< e j” Zke/e0d$< e l” Zme/e0d%< eZneZoeZpeZqeZreZsd&Ztd'Zud(Zvd)Zwewfe/e/e/e/dd*d+d,Zxe/e/ee/e/e/f d-d.d/Zyd0ewfeCeCe/e/e/e/e/eCd1d2d3ZzeneofeCe/e/eCd4d5d6Z{eCeCe
d7d8d9Z|e/e/e/dd:d;d<Z}e/eCeCe/e/e/eCd=d>d?Z~eCe/e/e/eCd@dAdBZeCeCe
d7dCdDZeZdS )Eé    N)ŚTuple)ŚffiŚlib)Śensureó    śchar *Ścrypto_pwhash_ALG_ARGON2I13Ścrypto_pwhash_ALG_ARGON2ID13Ścrypto_pwhash_ALG_DEFAULTŚcrypto_pwhash_SALTBYTESŚcrypto_pwhash_STRBYTESŚcrypto_pwhash_PASSWD_MINŚcrypto_pwhash_PASSWD_MAXŚcrypto_pwhash_BYTES_MINŚcrypto_pwhash_BYTES_MAXŚcrypto_pwhash_argon2i_STRPREFIXŚ"crypto_pwhash_argon2i_MEMLIMIT_MINŚ"crypto_pwhash_argon2i_MEMLIMIT_MAXŚ"crypto_pwhash_argon2i_OPSLIMIT_MINŚ"crypto_pwhash_argon2i_OPSLIMIT_MAXŚ*crypto_pwhash_argon2i_OPSLIMIT_INTERACTIVEŚ*crypto_pwhash_argon2i_MEMLIMIT_INTERACTIVEŚ'crypto_pwhash_argon2i_OPSLIMIT_MODERATEŚ'crypto_pwhash_argon2i_MEMLIMIT_MODERATEŚ(crypto_pwhash_argon2i_OPSLIMIT_SENSITIVEŚ(crypto_pwhash_argon2i_MEMLIMIT_SENSITIVEŚ crypto_pwhash_argon2id_STRPREFIXŚ#crypto_pwhash_argon2id_MEMLIMIT_MINŚ#crypto_pwhash_argon2id_MEMLIMIT_MAXŚ#crypto_pwhash_argon2id_OPSLIMIT_MINŚ#crypto_pwhash_argon2id_OPSLIMIT_MAXŚ+crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVEŚ+crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVEŚ(crypto_pwhash_argon2id_OPSLIMIT_MODERATEŚ(crypto_pwhash_argon2id_MEMLIMIT_MODERATEŚ)crypto_pwhash_argon2id_OPSLIMIT_SENSITIVEŚ)crypto_pwhash_argon2id_MEMLIMIT_SENSITIVEé’’’?é?   l   ’’’’ i   )ŚnŚrŚpŚmaxmemŚreturnc                 C   s  t |dkdtjd t |dkdtjd t | | d @ dkdtjd t | dkdtjd t |t| kd t”tjd t | dd	| > k tjd |d
 | }td
 }t | d || ktjd d| | d  d }t |t| ktjd t |tj| ktjd t || |kdtjd d S )Nr   zInvalid block size©ZraisingzInvalid parallelization factoré   z Cost factor must be a power of 2zCost factor must be at least 2zp*r is greater than {}é   é   é   é    é   z7Memory limit would be exceeded with the choosen n, r, p)r   ŚexcŚ
ValueErrorŚSCRYPT_PR_MAXŚformatŚ
UINT64_MAXŚsysŚmaxsize)r)   r*   r+   r,   ZBlenŚiZVlen© r=   ś?/tmp/pip-unpacked-wheel-e6sdgn5m/nacl/bindings/crypto_pwhash.pyŚ_check_memory_occupationæ   s2    ż
ż
żr?   )ŚopslimitŚmemlimitr-   c                 C   s°   | dk rd} d}| |d k rRd}| d|  }t ddD ]}d| |d kr6 q¦q6nT||d  }t ddD ]}d| |d krh qqh| d d|  }|d	krd	}|| }|||fS )
z/Python implementation of libsodium's pickparamsi   é   r3   r/   r4   r(   r2   r1   r'   )Śrange)r@   rA   r*   r+   ZmaxnZn_log2Zmaxrpr=   r=   r>   Ś nacl_bindings_pick_scrypt_paramsé   s$    rD   é@   )ŚpasswdŚsaltr)   r*   r+   Śdklenr,   r-   c           	      C   sÖ   t tdtjd t t|ttd t t|ttd t t|ttd t t| ttd t t|ttd t|||| t	 
d|”}t | t| |t||||||”	}t |dkdtjd t	 t	 d|”|”dd S )a  
    Derive a cryptographic key using the ``passwd`` and ``salt``
    given as input.

    The work factor can be tuned by by picking different
    values for the parameters

    :param bytes passwd:
    :param bytes salt:
    :param bytes salt: *must* be *exactly* :py:const:`.SALTBYTES` long
    :param int dklen:
    :param int opslimit:
    :param int n:
    :param int r: block size,
    :param int p: the parallelism factor
    :param int maxmem: the maximum available memory available for scrypt's
                       operations
    :rtype: bytes
    :raises nacl.exceptions.UnavailableError: If called when using a
        minimal build of libsodium.
    śNot available in minimal buildr.   z	uint8_t[]r   ś$Unexpected failure in key derivationr   N)r   Ś&has_crypto_pwhash_scryptsalsa208sha256r5   ŚUnavailableErrorŚ
isinstanceŚintŚ	TypeErrorŚbytesr?   r   Śnewr   Ś%crypto_pwhash_scryptsalsa208sha256_llŚlenŚRuntimeErrorŚbufferŚcast)	rF   rG   r)   r*   r+   rH   r,   ŚbufŚretr=   r=   r>   rR   	  s:    ż        ’żrR   )rF   r@   rA   r-   c                 C   sP   t tdtjd t dt”}t || t	| ||”}t |dkdtj
d t |”S )a  
    Derive a cryptographic key using the ``passwd`` and ``salt``
    given as input, returning a string representation which includes
    the salt and the tuning parameters.

    The returned string can be directly stored as a password hash.

    See :py:func:`.crypto_pwhash_scryptsalsa208sha256` for a short
    discussion about ``opslimit`` and ``memlimit`` values.

    :param bytes passwd:
    :param int opslimit:
    :param int memlimit:
    :return: serialized key hash, including salt and tuning parameters
    :rtype: bytes
    :raises nacl.exceptions.UnavailableError: If called when using a
        minimal build of libsodium.
    rI   r.   śchar[]r   z&Unexpected failure in password hashing)r   rK   r5   rL   r   rQ   ŚSCRYPT_STRBYTESr   Ś&crypto_pwhash_scryptsalsa208sha256_strrS   rT   Śstring)rF   r@   rA   rW   rX   r=   r=   r>   r[   E  s&    ż    ’żr[   )Śpasswd_hashrF   r-   c                 C   sV   t tdtjd t t| td kdtjd t | |t|”}t |dkdtj	d dS )a9  
    Verifies the ``passwd`` against the ``passwd_hash`` that was generated.
    Returns True or False depending on the success

    :param passwd_hash: bytes
    :param passwd: bytes
    :rtype: boolean
    :raises nacl.exceptions.UnavailableError: If called when using a
        minimal build of libsodium.
    rI   r.   r/   zInvalid password hashr   śWrong passwordT)
r   rK   r5   rL   rS   rZ   r6   r   Ś-crypto_pwhash_scryptsalsa208sha256_str_verifyŚInvalidkeyError©r]   rF   rX   r=   r=   r>   r_   q  s"    żż  ’r_   )r@   rA   Śalgr-   c                 C   sź   |t krn|tk r"t d t””n|tkr:t d t””| tk rTt d t””qę| tkręt d t””nx|tkrÜ|t	k rt d t	””n|t
krØt d t
””| tk rĀt d t””qę| tkręt d t””n
t d”d S )Nz"memlimit must be at least {} bytesz!memlimit must be at most {} byteszopslimit must be at least {}zopslimit must be at most {}zUnsupported algorithm)r   r   r5   r6   r8   r   r   r   r	   r   r   r   r    rO   )r@   rA   rb   r=   r=   r>   Ś_check_argon2_limits_alg  sf    ’’’’’’’’’’’’’’’’rc   )ŚoutlenrF   rG   r@   rA   rb   r-   c              
   C   s  t t| ttjd t t|ttjd t t|ttjd t t|ttjd t t|ttjd t|tkrt d 	t””| t
k rt d 	t
””n| tkr²t d 	t””t||| t d| ”}t || |t|||||”}t |dkdtjd t || ”dd S )	a  
    Derive a raw cryptographic key using the ``passwd`` and the ``salt``
    given as input to the ``alg`` algorithm.

    :param outlen: the length of the derived key
    :type outlen: int
    :param passwd: The input password
    :type passwd: bytes
    :param salt:
    :type salt: bytes
    :param opslimit: computational cost
    :type opslimit: int
    :param memlimit: memory cost
    :type memlimit: int
    :param alg: algorithm identifier
    :type alg: int
    :return: derived key
    :rtype: bytes
    r.   z"salt must be exactly {} bytes longz*derived key must be at least {} bytes longz)derived key must be at most {} bytes longzunsigned char[]r   rJ   N)r   rM   rN   r5   rO   rP   rS   r   r6   r8   r   r   rc   r   rQ   r   Zcrypto_pwhashrT   rU   )rd   rF   rG   r@   rA   rb   ŚoutbufrX   r=   r=   r>   Ścrypto_pwhash_algĖ  sR    ’’’’’’       ’żrf   )rF   r@   rA   rb   r-   c                 C   s   t t|ttd t t|ttd t t| ttd t||| t dd”}t 	|| t
| |||”}t |dkdtjd t |”S )a  
    Derive a cryptographic key using the ``passwd`` given as input
    and a random salt, returning a string representation which
    includes the salt, the tuning parameters and the used algorithm.

    :param passwd: The input password
    :type passwd: bytes
    :param opslimit: computational cost
    :type opslimit: int
    :param memlimit: memory cost
    :type memlimit: int
    :param alg: The algorithm to use
    :type alg: int
    :return: serialized derived key and parameters
    :rtype: bytes
    r.   rY   r1   r   rJ   )r   rM   rN   rO   rP   rc   r   rQ   r   Ścrypto_pwhash_str_algrS   r5   rT   r\   )rF   r@   rA   rb   re   rX   r=   r=   r>   rg     s&         ’żrg   c                 C   sf   t t| ttd t t|ttd t t| dkdtjd t | |t|”}t |dkdtj	d dS )a4  
    Verifies the ``passwd`` against a given password hash.

    Returns True on success, raises InvalidkeyError on failure
    :param passwd_hash: saved password hash
    :type passwd_hash: bytes
    :param passwd: password to be checked
    :type passwd: bytes
    :return: success
    :rtype: boolean
    r.   é   z#Hash must be at most 127 bytes longr   r^   T)
r   rM   rP   rO   rS   r5   r6   r   Ścrypto_pwhash_str_verifyr`   ra   r=   r=   r>   ri   =  s    
żri   )r:   Śtypingr   Znacl.exceptionsŚ
exceptionsr5   Znacl._sodiumr   r   r   ŚboolZ-PYNACL_HAS_CRYPTO_PWHASH_SCRYPTSALSA208SHA256rK   Z,crypto_pwhash_scryptsalsa208sha256_STRPREFIXZ,crypto_pwhash_scryptsalsa208sha256_SALTBYTESZ+crypto_pwhash_scryptsalsa208sha256_STRBYTESZ-crypto_pwhash_scryptsalsa208sha256_PASSWD_MINZ-crypto_pwhash_scryptsalsa208sha256_PASSWD_MAXZ,crypto_pwhash_scryptsalsa208sha256_BYTES_MINZ,crypto_pwhash_scryptsalsa208sha256_BYTES_MAXZ/crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MINZ/crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAXZ/crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MINZ/crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAXZ7crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVEZ7crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVEZ5crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVEZ5crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVEr\   rV   Z,crypto_pwhash_scryptsalsa208sha256_strprefixZ,crypto_pwhash_scryptsalsa208sha256_saltbytesZ+crypto_pwhash_scryptsalsa208sha256_strbytesZ-crypto_pwhash_scryptsalsa208sha256_passwd_minZ-crypto_pwhash_scryptsalsa208sha256_passwd_maxZ,crypto_pwhash_scryptsalsa208sha256_bytes_minZ,crypto_pwhash_scryptsalsa208sha256_bytes_maxZ/crypto_pwhash_scryptsalsa208sha256_memlimit_minZ/crypto_pwhash_scryptsalsa208sha256_memlimit_maxZ/crypto_pwhash_scryptsalsa208sha256_opslimit_minZ/crypto_pwhash_scryptsalsa208sha256_opslimit_maxZ7crypto_pwhash_scryptsalsa208sha256_opslimit_interactiveZ7crypto_pwhash_scryptsalsa208sha256_memlimit_interactiveZ5crypto_pwhash_scryptsalsa208sha256_opslimit_sensitiveZ5crypto_pwhash_scryptsalsa208sha256_memlimit_sensitiveZcrypto_pwhash_alg_argon2i13r   rN   Ś__annotations__Zcrypto_pwhash_alg_argon2id13r	   Zcrypto_pwhash_alg_defaultr
   Zcrypto_pwhash_saltbytesr   Zcrypto_pwhash_strbytesr   Zcrypto_pwhash_passwd_minr   Zcrypto_pwhash_passwd_maxr   Zcrypto_pwhash_bytes_minr   Zcrypto_pwhash_bytes_maxr   Zcrypto_pwhash_argon2i_strprefixr   rP   Z"crypto_pwhash_argon2i_memlimit_minr   Z"crypto_pwhash_argon2i_memlimit_maxr   Z"crypto_pwhash_argon2i_opslimit_minr   Z"crypto_pwhash_argon2i_opslimit_maxr   Z*crypto_pwhash_argon2i_opslimit_interactiver   Z*crypto_pwhash_argon2i_memlimit_interactiver   Z'crypto_pwhash_argon2i_opslimit_moderater   Z'crypto_pwhash_argon2i_memlimit_moderater   Z(crypto_pwhash_argon2i_opslimit_sensitiver   Z(crypto_pwhash_argon2i_memlimit_sensitiver   Z crypto_pwhash_argon2id_strprefixr   Z#crypto_pwhash_argon2id_memlimit_minr   Z#crypto_pwhash_argon2id_memlimit_maxr   Z#crypto_pwhash_argon2id_opslimit_minr   Z#crypto_pwhash_argon2id_opslimit_maxr    Z+crypto_pwhash_argon2id_opslimit_interactiver!   Z+crypto_pwhash_argon2id_memlimit_interactiver"   Z(crypto_pwhash_argon2id_opslimit_moderater#   Z(crypto_pwhash_argon2id_memlimit_moderater$   Z)crypto_pwhash_argon2id_opslimit_sensitiver%   Z)crypto_pwhash_argon2id_memlimit_sensitiver&   ZSCRYPT_OPSLIMIT_INTERACTIVEZSCRYPT_MEMLIMIT_INTERACTIVEZSCRYPT_OPSLIMIT_SENSITIVEZSCRYPT_MEMLIMIT_SENSITIVEZSCRYPT_SALTBYTESrZ   r7   ZLOG2_UINT64_MAXr9   ZSCRYPT_MAX_MEMr?   rD   rR   r[   r_   rc   rf   rg   ri   Z crypto_pwhash_argon2i_str_verifyr=   r=   r=   r>   Ś<module>   sb   ’’’’’’’’’’’’’’’’’’
’
’
’
’
’
’
’
’
’
’’
’
’
’
’
’
’
’
’
’
’’’’’   ž+ ž&łų>żü- ž!:łHū+