3 L]B@sddlmZmZmZddlZddlmZddljZ ddl m Z m Z ddlm Z e je jde jddZe jZe jZe jZe jZe jZe jZe jZe j Z!e j"Z#e j$Z%e j&Z'e j(Z)e j*Z+e j,Z-e j.Z/e j0Z1e j2Z3e j4Z5e j6Z7e j8Z9e j:Z;e j<Z=e j>Z?e je jde j@ddZAe jBZCe jDZEe jFZGe jHZIe jJZKe jLZMe jNZOe jPZQe jRZSe jTZUe je jde jVddZWe jXZYe jZZ[e j\Z]e j^Z_e j`Zae jbZce jdZee jfZge jhZie jjZke'Zle)Zme+Zne-ZoeZpeZqd Zrd Zsd"Ztd$Zueufd dZvddZwd eufddZxelemfddZyddZzddZ{ddZ|ddZ}ddZ~e~ZdS)%)absolute_importdivisionprint_functionN) integer_types)ffilib)ensurezchar *?@ icCst|dkdtjdt|dkdtjdt||d@dkdtjdt|dkdtjdt|t|kdjttjdt|dd |>ktjd|d |}td }t|d ||ktjdd ||d d }t|t|ktjdt|tj|ktjdt|||kdtjddS)NrzInvalid block size)raisingzInvalid parallelization factorr z Cost factor must be a power of 2zCost factor must be at least 2zp*r is greater than {0}r z7Memory limit would be exceeded with the choosen n, r, p)rexc ValueError SCRYPT_PR_MAXformat UINT64_MAXsysmaxsize)nrpmaxmemZBleniZVlenr?/tmp/pip-install-wfra5znf/pynacl/nacl/bindings/crypto_pwhash.py_check_memory_occupations2                r!cCs|dkr d}d}||dkrTd}|d|}x|tddD]}d||dkr8Pq8WnV||d}x$tddD]}d||dkrlPqlW|dd|}|d krd }||}|||fS) z/Python implementation of libsodium's pickparamsir r rr rri?)range)opslimitmemlimitrrZmaxnZn_log2Zmaxrprrr nacl_bindings_pick_scrypt_paramss$   r&c Cstt|ttdtt|ttdtt|ttdtt|ttdtt|ttdt||||tjd|}tj |t ||t |||||| }t|dkdt j dtj tjd||ddS)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 )rz uint8_t[]rz$Unexpected failure in key derivationzchar *N)r isinstancer TypeErrorbytesr!rnewr%crypto_pwhash_scryptsalsa208sha256_lllenr RuntimeErrorbuffercast) passwdsaltrrrZdklenrbufretrrr r+s&         r+cCs@tjdt}tj||t|||}t|dkdtjdtj |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 zchar[]rz&Unexpected failure in password hashing)r) rr*SCRYPT_STRBYTESr&crypto_pwhash_scryptsalsa208sha256_strr,rrr-string)r0r$r%r2r3rrr r5s   r5cCsFtt|tdkdtjdtj||t|}t|dkdtjddS)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 zInvalid password hash)rrzWrong passwordT)rr,r4rrr-crypto_pwhash_scryptsalsa208sha256_str_verifyInvalidkeyError) passwd_hashr0r3rrr r7$s    r7cCs|tkrn|tkr"tjdjtn|tkr:tjdjt|tkrTtjdjtq|tkrtjdjtnx|tkr|t krtjdjt n|t krtjdjt |t krtjdjt q|t krtjdjt n tj ddS)Nz#memlimit must be at least {0} bytesz"memlimit must be at most {0} byteszopslimit must be at least {0}zopslimit must be at most {0}zUnsupported algorithm)crypto_pwhash_ALG_ARGON2I13"crypto_pwhash_argon2i_MEMLIMIT_MINrrr"crypto_pwhash_argon2i_MEMLIMIT_MAX"crypto_pwhash_argon2i_OPSLIMIT_MIN"crypto_pwhash_argon2i_OPSLIMIT_MAXcrypto_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%algrrr _check_argon2_limits_alg;s6      rEc Cstt|ttjdtt|ttjdtt|ttjdtt|ttjdtt|ttjdt|tkrtjdj t|t krtjdj t n|t krtjdj t t |||t jd|}tj|||t|||||}t|dkdtjdt j||ddS) 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 )rz#salt must be exactly {0} bytes longz+derived key must be at least {0} bytes longz*derived key must be at most {0} bytes longzunsigned char[]rz$Unexpected failure in key derivationN)rr'rrr(r)r,crypto_pwhash_SALTBYTESrrcrypto_pwhash_BYTES_MINcrypto_pwhash_BYTES_MAXrErr*rZ crypto_pwhashr-r.)Zoutlenr0r1r$r%rDoutbufr3rrr crypto_pwhash_alg\s8                 rJcCstt|ttdtt|ttdtt|ttdt|||tjdd}tj ||t ||||}t|dkdt j dtj |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 )rzchar[]rrz$Unexpected failure in key derivation)rr'rr(r)rErr*rcrypto_pwhash_str_algr,rr-r6)r0r$r%rDrIr3rrr rKs        rKcCsftt|ttdtt|ttdtt|dkdtjdtj||t|}t|dkdtj ddS)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 )rz#Hash must be at most 127 bytes longrzWrong passwordT) rr'r)r(r,rrrcrypto_pwhash_str_verifyr8)r9r0r3rrr rMs     rMi@i?llii) __future__rrrrsixrZnacl.exceptions exceptionsrZ nacl._sodiumrrrr6r/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_SENSITIVEZcrypto_pwhash_alg_argon2i13r:Zcrypto_pwhash_alg_argon2id13r?Zcrypto_pwhash_alg_defaultZcrypto_pwhash_ALG_DEFAULTZcrypto_pwhash_saltbytesrFZcrypto_pwhash_strbytesZcrypto_pwhash_STRBYTESZcrypto_pwhash_passwd_minZcrypto_pwhash_PASSWD_MINZcrypto_pwhash_passwd_maxZcrypto_pwhash_PASSWD_MAXZcrypto_pwhash_bytes_minrGZcrypto_pwhash_bytes_maxrHZcrypto_pwhash_argon2i_strprefixZcrypto_pwhash_argon2i_STRPREFIXZ"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_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_minr@Z#crypto_pwhash_argon2id_memlimit_maxrAZ#crypto_pwhash_argon2id_opslimit_minrBZ#crypto_pwhash_argon2id_opslimit_maxrCZ+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_SENSITIVEZSCRYPT_OPSLIMIT_INTERACTIVEZSCRYPT_MEMLIMIT_INTERACTIVEZSCRYPT_OPSLIMIT_SENSITIVEZSCRYPT_MEMLIMIT_SENSITIVEZSCRYPT_SALTBYTESr4rZLOG2_UINT64_MAXrZSCRYPT_MAX_MEMr!r&r+r5r7rErJrKrMZ crypto_pwhash_argon2i_str_verifyrrrr s       ( 0 !8%