U
    ­Ö«[B  ć                   @   s  d dl mZmZmZ 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 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 0” Z1e 2” Z3e 4” Z5e 6” Z7e 8” Z9e :” Z;e <” Z=e >” Z?e e de @” ””dd ZAe B” ZCe D” ZEe F” ZGe H” ZIe J” ZKe L” ZMe N” ZOe P” ZQe R” ZSe T” ZUe e de V” ””dd ZWe X” ZYe Z” Z[e \” Z]e ^” Z_e `” Zae b” Zce d” Zee f” Zge h” Zie j” Zke'Zle)Zme+Zne-ZoeZpeZqdZrdZsd	Ztd
ZueufddZvdd ZwdeufddZxelemfddZydd Zzdd Z{dd Z|dd Z}dd Z~e~ZdS )é    )Śabsolute_importŚdivisionŚprint_functionN)Śinteger_types)ŚffiŚlib)Śensureśchar *é’’’?é?   l   ’’’’ i   c                 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 {0}é   é   é   é    é   z7Memory limit would be exceeded with the choosen n, r, p)r   ŚexcŚ
ValueErrorŚSCRYPT_PR_MAXŚformatŚ
UINT64_MAXŚsysŚmaxsize)ŚnŚrŚpŚmaxmemZBlenŚiZVlen© r   ś=/usr/lib/python3/dist-packages/nacl/bindings/crypto_pwhash.pyŚ_check_memory_occupation   sF    
’
’’
’ž’’’’žr!   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   é   r   r   r   r   r   r   r
   )Śrange)ŚopslimitŚmemlimitr   r   ZmaxnZn_log2Zmaxrpr   r   r    Ś nacl_bindings_pick_scrypt_params¶   s$    r&   é@   c           	      C   sĘ   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 )a1  
    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
    r   z	uint8_t[]r   ś$Unexpected failure in key derivationr	   N)r   Ś
isinstancer   Ś	TypeErrorŚbytesr!   r   Śnewr   Ś%crypto_pwhash_scryptsalsa208sha256_llŚlenr   ŚRuntimeErrorŚbufferŚcast)	ŚpasswdŚsaltr   r   r   Zdklenr   ŚbufŚretr   r   r    r-   Ō   s<    
’
’
’
’
’    ż
’r-   c                 C   s@   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
    śchar[]r   z&Unexpected failure in password hashingr   )
r   r,   ŚSCRYPT_STRBYTESr   Ś&crypto_pwhash_scryptsalsa208sha256_strr.   r   r   r/   Śstring)r2   r$   r%   r4   r5   r   r   r    r8     s    ż
’r8   c                 C   sF   t t| td kdtjd t | |t|”}t |dkdtjd dS )zŠ
    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
    r   zInvalid password hashr   r   śWrong passwordT)r   r.   r7   r   r   r   Ś-crypto_pwhash_scryptsalsa208sha256_str_verifyŚInvalidkeyError©Zpasswd_hashr2   r5   r   r   r    r;   $  s    
’žžr;   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 {0} bytesz"memlimit must be at most {0} byteszopslimit must be at least {0}zopslimit must be at most {0}zUnsupported algorithm)Ścrypto_pwhash_ALG_ARGON2I13Ś"crypto_pwhash_argon2i_MEMLIMIT_MINr   r   r   Ś"crypto_pwhash_argon2i_MEMLIMIT_MAXŚ"crypto_pwhash_argon2i_OPSLIMIT_MINŚ"crypto_pwhash_argon2i_OPSLIMIT_MAXŚcrypto_pwhash_ALG_ARGON2ID13Ś#crypto_pwhash_argon2id_MEMLIMIT_MINŚ#crypto_pwhash_argon2id_MEMLIMIT_MAXŚ#crypto_pwhash_argon2id_OPSLIMIT_MINŚ#crypto_pwhash_argon2id_OPSLIMIT_MAXr*   )r$   r%   Śalgr   r   r    Ś_check_argon2_limits_alg;  sF    ’’’’’’’’rI   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 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 {0} bytes longz+derived key must be at least {0} bytes longz*derived key must be at most {0} bytes longzunsigned char[]r   r(   N)r   r)   r   r   r*   r+   r.   Ścrypto_pwhash_SALTBYTESr   r   Ścrypto_pwhash_BYTES_MINŚcrypto_pwhash_BYTES_MAXrI   r   r,   r   Zcrypto_pwhashr/   r0   )Zoutlenr2   r3   r$   r%   rH   Śoutbufr5   r   r   r    Ścrypto_pwhash_alg\  sV    
’
’
’
’
’’’’’’   ’
’rN   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   r6   r   r   r(   )r   r)   r   r*   r+   rI   r   r,   r   Ścrypto_pwhash_str_algr.   r   r/   r9   )r2   r$   r%   rH   rM   r5   r   r   r    rO     s(    
’
’
’  ’
’rO   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   r)   r+   r*   r.   r   r   r   Ścrypto_pwhash_str_verifyr<   r=   r   r   r    rQ   ¹  s     
’
’žžrQ   )Z
__future__r   r   r   r   Zsixr   Znacl.exceptionsŚ
exceptionsr   Znacl._sodiumr   r   r   r9   r1   Z,crypto_pwhash_scryptsalsa208sha256_strprefixZ,crypto_pwhash_scryptsalsa208sha256_STRPREFIXZ,crypto_pwhash_scryptsalsa208sha256_saltbytesZ,crypto_pwhash_scryptsalsa208sha256_SALTBYTESZ+crypto_pwhash_scryptsalsa208sha256_strbytesZ+crypto_pwhash_scryptsalsa208sha256_STRBYTESZ-crypto_pwhash_scryptsalsa208sha256_passwd_minZ-crypto_pwhash_scryptsalsa208sha256_PASSWD_MINZ-crypto_pwhash_scryptsalsa208sha256_passwd_maxZ-crypto_pwhash_scryptsalsa208sha256_PASSWD_MAXZ,crypto_pwhash_scryptsalsa208sha256_bytes_minZ,crypto_pwhash_scryptsalsa208sha256_BYTES_MINZ,crypto_pwhash_scryptsalsa208sha256_bytes_maxZ,crypto_pwhash_scryptsalsa208sha256_BYTES_MAXZ/crypto_pwhash_scryptsalsa208sha256_memlimit_minZ/crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MINZ/crypto_pwhash_scryptsalsa208sha256_memlimit_maxZ/crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAXZ/crypto_pwhash_scryptsalsa208sha256_opslimit_minZ/crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MINZ/crypto_pwhash_scryptsalsa208sha256_opslimit_maxZ/crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAXZ7crypto_pwhash_scryptsalsa208sha256_opslimit_interactiveZ7crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVEZ7crypto_pwhash_scryptsalsa208sha256_memlimit_interactiveZ7crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVEZ5crypto_pwhash_scryptsalsa208sha256_opslimit_sensitiveZ5crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVEZ5crypto_pwhash_scryptsalsa208sha256_memlimit_sensitiveZ5crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVEZcrypto_pwhash_alg_argon2i13r>   Zcrypto_pwhash_alg_argon2id13rC   Zcrypto_pwhash_alg_defaultZcrypto_pwhash_ALG_DEFAULTZcrypto_pwhash_saltbytesrJ   Zcrypto_pwhash_strbytesZcrypto_pwhash_STRBYTESZcrypto_pwhash_passwd_minZcrypto_pwhash_PASSWD_MINZcrypto_pwhash_passwd_maxZcrypto_pwhash_PASSWD_MAXZcrypto_pwhash_bytes_minrK   Zcrypto_pwhash_bytes_maxrL   Zcrypto_pwhash_argon2i_strprefixZcrypto_pwhash_argon2i_STRPREFIXZ"crypto_pwhash_argon2i_memlimit_minr?   Z"crypto_pwhash_argon2i_memlimit_maxr@   Z"crypto_pwhash_argon2i_opslimit_minrA   Z"crypto_pwhash_argon2i_opslimit_maxrB   Z*crypto_pwhash_argon2i_opslimit_interactiveZ*crypto_pwhash_argon2i_OPSLIMIT_INTERACTIVEZ*crypto_pwhash_argon2i_memlimit_interactiveZ*crypto_pwhash_argon2i_MEMLIMIT_INTERACTIVEZ'crypto_pwhash_argon2i_opslimit_moderateZ'crypto_pwhash_argon2i_OPSLIMIT_MODERATEZ'crypto_pwhash_argon2i_memlimit_moderateZ'crypto_pwhash_argon2i_MEMLIMIT_MODERATEZ(crypto_pwhash_argon2i_opslimit_sensitiveZ(crypto_pwhash_argon2i_OPSLIMIT_SENSITIVEZ(crypto_pwhash_argon2i_memlimit_sensitiveZ(crypto_pwhash_argon2i_MEMLIMIT_SENSITIVEZ crypto_pwhash_argon2id_strprefixZ crypto_pwhash_argon2id_STRPREFIXZ#crypto_pwhash_argon2id_memlimit_minrD   Z#crypto_pwhash_argon2id_memlimit_maxrE   Z#crypto_pwhash_argon2id_opslimit_minrF   Z#crypto_pwhash_argon2id_opslimit_maxrG   Z+crypto_pwhash_argon2id_opslimit_interactiveZ+crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVEZ+crypto_pwhash_argon2id_memlimit_interactiveZ+crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVEZ(crypto_pwhash_argon2id_opslimit_moderateZ(crypto_pwhash_argon2id_OPSLIMIT_MODERATEZ(crypto_pwhash_argon2id_memlimit_moderateZ(crypto_pwhash_argon2id_MEMLIMIT_MODERATEZ)crypto_pwhash_argon2id_opslimit_sensitiveZ)crypto_pwhash_argon2id_OPSLIMIT_SENSITIVEZ)crypto_pwhash_argon2id_memlimit_sensitiveZ)crypto_pwhash_argon2id_MEMLIMIT_SENSITIVEZSCRYPT_OPSLIMIT_INTERACTIVEZSCRYPT_MEMLIMIT_INTERACTIVEZSCRYPT_OPSLIMIT_SENSITIVEZSCRYPT_MEMLIMIT_SENSITIVEZSCRYPT_SALTBYTESr7   r   ZLOG2_UINT64_MAXr   ZSCRYPT_MAX_MEMr!   r&   r-   r8   r;   rI   rN   rO   rQ   Z crypto_pwhash_argon2i_str_verifyr   r   r   r    Ś<module>   sų   
’’’’’’’’’’’’’’’’
’’’’’’’’’’’’
’’’’’’’’’’’’’’’’’’(’
1ž
 !8%