3
¬ �eÛM  ã               @   sØ   d dl Z d dlmZ d dlmZ d dlmZmZmZmZ d dl	m
Z
 d dlmZmZ d dlmZmZm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 G dd„ de!ƒZ"G dd„ de#ƒZ$G dd„ de#ƒZ%dS )é    N)ÚHash)ÚRandom)ÚDerSequenceÚDerOctetStringÚDerObjectIdÚ
DerInteger)ÚAES)ÚpadÚunpad)ÚPBKDF1ÚPBKDF2Úscryptz1.2.840.113549.1.5.3z1.2.840.113549.1.5.6z1.2.840.113549.1.5.10z1.2.840.113549.1.5.11z1.2.840.113549.1.5.13z1.2.840.113549.1.5.12z1.3.6.1.4.1.11591.4.11z1.2.840.113549.2.7z1.2.840.113549.3.7z2.16.840.1.101.3.4.1.2z2.16.840.1.101.3.4.1.22z2.16.840.1.101.3.4.1.42z2.16.840.1.101.3.4.1.6z2.16.840.1.101.3.4.1.26z2.16.840.1.101.3.4.1.46c               @   s   e Zd ZdS )Ú	PbesErrorN)Ú__name__Ú
__module__Ú__qualname__© r   r   úV/var/www/html/wholesaler-api-python-env/lib/python3.6/site-packages/Crypto/IO/_PBES.pyr   C   s   r   c               @   s   e Zd ZdZedd„ ƒZdS )ÚPBES1z¼Deprecated encryption scheme with password-based key derivation
    (originally defined in PKCS#5 v1.5, but still present in `v2.0`__).

    .. __: http://www.ietf.org/rfc/rfc2898.txt
    c             C   s†  t ƒ j| ƒ}t ƒ j|d ƒ}tƒ j|d ƒj}tƒ j|d ƒj}i }|tkrnddlm} ddl	m
} |}	|}
n–|tkr ddlm} ddl	m} |}	|}
d|d< nd|tkrÊddlm} ddl	m
} |}	|}
n:|tkrüddlm} ddl	m} |}	|}
d|d< ntd	ƒ‚t ƒ j|d d
d�}tƒ j|d ƒj}|d }t||d||	ƒ}|dd… |dd…  }}|
j||
j|f|Ž}|j|ƒ}t||jƒS )ax  Decrypt a piece of data using a passphrase and *PBES1*.

        The algorithm to use is automatically detected.

        :Parameters:
          data : byte string
            The piece of data to decrypt.
          passphrase : byte string
            The passphrase to use for decrypting the data.
        :Returns:
          The decrypted data, as a binary string.
        r   é   )ÚMD5)ÚDES)ÚARC2é@   Zeffective_keylen)ÚSHA1zUnknown OID for PBES1é   )Únr_elementsé   Né   )r   Údecoder   Úpayloadr   ÚvalueÚ_OID_PBE_WITH_MD5_AND_DES_CBCZCrypto.Hashr   ÚCrypto.Cipherr   Ú_OID_PBE_WITH_MD5_AND_RC2_CBCr   Ú_OID_PBE_WITH_SHA1_AND_DES_CBCr   Ú_OID_PBE_WITH_SHA1_AND_RC2_CBCr   r   ÚnewÚMODE_CBCÚdecryptr
   Ú
block_size)ÚdataÚ
passphraseÚenc_private_key_infoZencrypted_algorithmÚencrypted_dataÚpbe_oidZcipher_paramsr   r   ZhashmodÚmoduler   r   Z
pbe_paramsÚsaltZ
iterationsZkey_ivÚkeyÚivÚcipherÚptr   r   r   r)   „   sH    


zPBES1.decryptN)r   r   r   Ú__doc__Ústaticmethodr)   r   r   r   r   r   }   s   r   c               @   s*   e Zd ZdZeddd„ƒZedd„ ƒZdS )ÚPBES2z‡Encryption scheme with password-based key derivation
    (defined in `PKCS#5 v2.0`__).

    .. __: http://www.ietf.org/rfc/rfc2898.txt.Nc       !      C   s�  |dkri }|dkrt jƒ j}tjdƒ}|j|ƒ}|dkrFtd| ƒ‚|jdƒrjd}|jdƒ}|jdƒ}	nd}|jdƒ}	d	}
|	d
kr²ddl	m
} d}|}|j}t}d|dƒi}�n|	dkrÚd}t}tj}t}d|dƒi}nê|	dk�rd}t}tj}t}d|dƒi}nÀ|	dk�r.d}t}tj}t}d|dƒi}n–|	dk�r\d}t}tj}t}d|dƒi}d}
nh|	dk�rŠd}t}tj}t}d|dƒi}d}
n:|	dk�r¸d}t}tj}t}d|dƒi}d}
ntd|	 ƒ‚t|jƒ ƒd }||jddƒƒ}|dk�r�|jddƒ}tj|ƒ}t|||||d�}tt|ƒt|ƒgƒ}|d k�r~ytjjd!|d"�j}W n tk
�rh   td#ƒ‚Y nX |j tt!|ƒgƒƒ tt!t"ƒ|gƒ}n€|dk�rþ|jdd$ƒ}|jd%dƒ}|jd&d'ƒ}t#||||||ƒ}tt!t$ƒtt|ƒt|ƒt|ƒt|ƒgƒgƒ}ntd(|jd'ƒ ƒ‚|j||f|Ž}|
�r>|j%| ƒ\}}|| }n|j&t'| |j(ƒƒ}tt!|ƒt|ƒgƒ}ttt!t)ƒt||gƒgƒt|ƒgƒ} | j*ƒ S ))a¦	  Encrypt a piece of data using a passphrase and *PBES2*.

        :Parameters:
          data : byte string
            The piece of data to encrypt.
          passphrase : byte string
            The passphrase to use for encrypting the data.
          protection : string
            The identifier of the encryption algorithm to use.
            The default value is '``PBKDF2WithHMAC-SHA1AndDES-EDE3-CBC``'.
          prot_params : dictionary
            Parameters of the protection algorithm.

            +------------------+-----------------------------------------------+
            | Key              | Description                                   |
            +==================+===============================================+
            | iteration_count  | The KDF algorithm is repeated several times to|
            |                  | slow down brute force attacks on passwords    |
            |                  | (called *N* or CPU/memory cost in scrypt).    |
            |                  |                                               |
            |                  | The default value for PBKDF2 is 1 000.        |
            |                  | The default value for scrypt is 16 384.       |
            +------------------+-----------------------------------------------+
            | salt_size        | Salt is used to thwart dictionary and rainbow |
            |                  | attacks on passwords. The default value is 8  |
            |                  | bytes.                                        |
            +------------------+-----------------------------------------------+
            | block_size       | *(scrypt only)* Memory-cost (r). The default  |
            |                  | value is 8.                                   |
            +------------------+-----------------------------------------------+
            | parallelization  | *(scrypt only)* CPU-cost (p). The default     |
            |                  | value is 1.                                   |
            +------------------+-----------------------------------------------+


          randfunc : callable
            Random number generation function; it should accept
            a single integer N and return a string of random data,
            N bytes long. If not specified, a new RNG will be
            instantiated from ``Crypto.Random``.

        :Returns:
          The encrypted data, as a binary string.
        Nz5^(PBKDF2WithHMAC-([0-9A-Z-]+)|scrypt)And([0-9A-Z-]+)$zUnknown protection %sZPBKDFZpbkdf2r   é   r   FzDES-EDE3-CBCr   )ÚDES3é   r3   r   z
AES128-CBCr   z
AES192-CBCz
AES256-CBCé    z
AES128-GCMÚnonceé   Tz
AES192-GCMz
AES256-GCMzUnknown encryption mode '%s'Z	salt_sizeÚiteration_countiè  )Úhmac_hash_moduler   ó    )Ú	digestmodzNo OID for HMAC hash algorithmi @  r*   Zparallelizationr   zUnknown KDF )+r   r'   ÚreadÚreÚcompileÚmatchÚ
ValueErrorÚ
startswithÚgroupr#   r:   r(   Ú_OID_DES_EDE3_CBCr   Ú_OID_AES128_CBCÚ_OID_AES192_CBCÚ_OID_AES256_CBCÚMODE_GCMÚ_OID_AES128_GCMÚ_OID_AES192_GCMÚ_OID_AES256_GCMÚlistÚvaluesÚgetr   r   r   r   r   ÚHMACZoidÚKeyErrorÚappendr   Ú_OID_PBKDF2r   Ú_OID_SCRYPTZencrypt_and_digestÚencryptr	   r*   Ú
_OID_PBES2Úencode)!r+   r,   Z
protectionZprot_paramsZrandfuncÚpatternÚresZpbkdfZpbkdf2_hmac_algoÚenc_algoÚaeadr:   Úkey_sizer0   Úcipher_modeÚenc_oidZ	enc_paramÚiv_noncer1   ÚcountrB   r2   Úpbkdf2_paramsZhmac_oidÚkdf_infoÚscrypt_rÚscrypt_pr4   ÚctÚtagr.   Úenc_infor-   r   r   r   rZ   È   sä    /


















zPBES2.encryptc       #         sä  t ƒ j| dd�}t ƒ j|d ƒ}tƒ j|d ƒj}tƒ j|d ƒj}|tkrTtdƒ‚t ƒ j|d dd�}t ƒ j|d dd�}tƒ j|d ƒj}d}	|tk�rNt ƒ j|d dd�}
tƒ j|
d ƒj}|
d }t	|
ƒd }d}|dk�ry |
| d }	|d8 }|d7 }W n t
k
�r   Y nX t}|dk�r¼t ƒ j|
| ƒ}tƒ j|d ƒj}nn|tk�r´t ƒ j|d dd�‰ tƒ jˆ d ƒj}‡ fd
d„dD ƒ\}}}t	ˆ ƒdk�r®ˆ d }	nd}	ntdƒ‚t ƒ j|d ƒ}tƒ j|d ƒj}d}|tk�rddlm} |}|j}d}d}nÌ|tk�r*t}tj}d}d}n®|tk�rHt}tj}d}d}n�|tk�rft}tj}d}d}nr|tk�rˆt}tj}d}d}d}nP|tk�rªt}tj}d}d}d}n.|tk�rÌt}tj}d}d}d}ntd| ƒ‚|	�rð|	|k�rðtdƒ‚tƒ j|d ƒj}|tk�r^ytjj| }W n" tk
�r>   td| ƒ‚Y nX tj|ƒ}t|||||d�}nt||||||ƒ}|j||f||iŽ}t	|ƒ|j k �rœt!dƒ‚|�rÊ|j } |j"|d|  … ||  d… ƒ}!n|j#|ƒ}"t$|"|j ƒ}!|!S )ax  Decrypt a piece of data using a passphrase and *PBES2*.

        The algorithm to use is automatically detected.

        :Parameters:
          data : byte string
            The piece of data to decrypt.
          passphrase : byte string
            The passphrase to use for decrypting the data.
        :Returns:
          The decrypted data, as a binary string.
        r   )r   r   r   zNot a PBES2 objectNr9   é   é   c                s   g | ]}ˆ | ‘qS r   r   )Ú.0Úx)Úscrypt_paramsr   r   ú
<listcomp>Ç  s   z!PBES2.decrypt.<locals>.<listcomp>zUnsupported PBES2 KDFF)r:   r;   r3   r   r<   r=   TzUnsupported PBES2 cipher z9Mismatch between PBES2 KDF parameters and selected cipherzUnsupported HMAC %s)r@   zToo little data to decrypt)r   r9   rm   )rm   rn   )r   r   r9   )%r   r   r   r    r   r!   r[   r   rX   ÚlenÚ	TypeErrorÚ_OID_HMAC_SHA1rY   rJ   r#   r:   r(   rK   r   rL   rM   rO   rN   rP   rQ   r   rU   Z_hmac2hash_oidrV   r'   r   r   r*   rG   Zdecrypt_and_verifyr)   r
   )#r+   r,   r-   r_   r.   r/   Zpbes2_paramsrg   Zkdf_oidZkdf_key_lengthrf   r1   r?   ÚleftÚidxZpbkdf2_prf_oidZpbkdf2_prf_algo_idrh   ri   rl   rc   r`   r:   r0   rb   ra   Zcipher_paramrd   Zhmac_hash_module_oidr@   r2   r4   Ztag_lenr5   Z	pt_paddedr   )rq   r   r)   Š  sÌ    


















zPBES2.decrypt)NN)r   r   r   r6   r7   rZ   r)   r   r   r   r   r8   Â   s
    Br8   )&rD   ÚCryptor   r   ZCrypto.Util.asn1r   r   r   r   r#   r   ZCrypto.Util.Paddingr	   r
   ZCrypto.Protocol.KDFr   r   r   r"   r$   r%   r&   r[   rX   rY   ru   rJ   rK   rL   rM   rO   rP   rQ   rG   r   Úobjectr   r8   r   r   r   r   Ú<module>"   s0   :E