o
    Ìn~bà?  ã                   @   sô   d dl mZ d dlmZmZmZmZ d dlmZm	Z	 d dl
mZmZmZmZmZmZ d dlmZmZ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(dZ)G dd„ de*ƒZ+G dd„ de,ƒZ-G dd„ de,ƒZ.dS )é    )ÚRandom)ÚDerSequenceÚDerOctetStringÚDerObjectIdÚ
DerInteger)ÚpadÚunpad)ÚMD5ÚSHA1ÚSHA224ÚSHA256ÚSHA384ÚSHA512)ÚDESÚARC2ÚDES3ÚAES)Ú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.11ú1.2.840.113549.2.7z1.2.840.113549.2.8z1.2.840.113549.2.9z1.2.840.113549.2.10z1.2.840.113549.2.11z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.42c                   @   s   e Zd ZdS )Ú	PbesErrorN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú>/usr/local/lib/python3.10/dist-packages/Cryptodome/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 ƒ  | ¡}t ƒ  |d ¡}tƒ  |d ¡j}tƒ  |d ¡j}i }|tkr+t}t}n'|t	kr8t}t
}d|d< n|tkrAt}t}n|tkrNt}t
}d|d< ntdƒ‚t ƒ j|d dd}	tƒ  |	d ¡j}
|	d }t||
d||ƒ}|d	d
… |d
d	… }}|j||j|fi |¤Ž}| |¡}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   é   é@   Zeffective_keylenzUnknown OID for PBES1é   ©Znr_elementsé   Né   )r   Údecoder   Úpayloadr   ÚvalueÚ_OID_PBE_WITH_MD5_AND_DES_CBCr	   r   Ú_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_paramsZhashmodÚ	ciphermodZ
pbe_paramsÚsaltZ
iterationsZkey_ivÚkeyÚivÚcipherÚptr   r   r   r-   y   s8   


zPBES1.decryptN)r   r   r   Ú__doc__Ústaticmethodr-   r   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u ri }|du rt  ¡ j}|dkrd}t}tj}t}n.|dv r+d}t}tj}t}n |dv r9d}t}tj}t}n|dv rGd}t}tj}t	}nt
d	ƒ‚||jƒ}	|| d
d¡ƒ}
| d¡r{| dd¡}t||
||ƒ}tttƒtt|
ƒt|ƒgƒgƒ}n1| dd¡}| dd¡}| dd¡}t||
||||ƒ}tttƒtt|
ƒt|ƒt|ƒt|ƒgƒgƒ}| |||	¡}| t| |jƒ¡}tt|ƒt|	ƒgƒ}ttttƒt||gƒgƒt|ƒgƒ}| ¡ 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 ``Cryptodome.Random``.

        :Returns:
          The encrypted data, as a binary string.
        Nz"PBKDF2WithHMAC-SHA1AndDES-EDE3-CBCé   )z PBKDF2WithHMAC-SHA1AndAES128-CBCzscryptAndAES128-CBCr"   )z PBKDF2WithHMAC-SHA1AndAES192-CBCzscryptAndAES192-CBC)z PBKDF2WithHMAC-SHA1AndAES256-CBCzscryptAndAES256-CBCé    zUnknown PBES2 modeZ	salt_sizer#   r   Úiteration_countiè  i @  r.   Zparallelizationr   )r   r+   Úreadr   r,   Ú_OID_DES_EDE3_CBCr   Ú_OID_AES128_CBCÚ_OID_AES192_CBCÚ_OID_AES256_CBCÚ
ValueErrorr.   ÚgetÚ
startswithr   r   r   Ú_OID_PBKDF2r   r   r   Ú_OID_SCRYPTÚencryptr   Ú
_OID_PBES2Úencode)r/   r0   Z
protectionZprot_paramsZrandfuncÚkey_sizeÚmoduleZcipher_modeÚenc_oidr7   r5   Úcountr6   Úkdf_infoÚscrypt_rÚscrypt_pr8   r2   Úenc_infor1   r   r   r   rJ   µ   sŠ   /


þþ	ÿüþþþþ÷zPBES2.encryptc                    s   t ƒ j| dd}t ƒ  |d ¡}tƒ  |d ¡j}tƒ  |d ¡j}|tkr*tdƒ‚t ƒ j|d dd}t ƒ j|d dd}tƒ  |d ¡j}d}	|tkr¢t ƒ j|d dd}
tƒ  |
d ¡j}|
d }t	|
ƒd }d}|dkrŠz|
| d }	|d8 }|d7 }W n	 t
y‰   Y nw d}|dkr¡t ƒ  |
| ¡}tƒ  |d ¡j}n5|tkrÓt ƒ j|d d	d‰ tƒ  ˆ d ¡j}‡ fd
d„dD ƒ\}}}t	ˆ ƒdkrÐˆ d }	nd}	ntdƒ‚t ƒ  |d ¡}tƒ  |d ¡j}|tkrñt}d}n!|tkrút}d}n|tkrt}d}n|tkrt}d}ntdƒ‚|	r|	|krtdƒ‚tƒ  |d ¡j}|tkrd|tkr4t}n&|tkr<t}n|tkrDt}n|tkrLt}n|tkrTt}ntd| ƒ‚t|||||d}n	t||||||ƒ}| ||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 objectN)r    é   é   r   )rV   é   c                    s   g | ]}ˆ | ‘qS r   r   )Ú.0Úx©Zscrypt_paramsr   r   Ú
<listcomp>u  s    ÿz!PBES2.decrypt.<locals>.<listcomp>)r   r    rU   rV   zUnsupported PBES2 KDFr=   r"   r>   zUnsupported PBES2 cipherz9Mismatch between PBES2 KDF parameters and selected cipherzUnsupported HMAC %s)Úhmac_hash_module)#r   r$   r   r%   r   r&   rK   r   rH   ÚlenÚ	TypeErrorrI   rA   r   rB   r   rC   rD   Ú_OID_HMAC_SHA1r
   Ú_OID_HMAC_SHA224r   Ú_OID_HMAC_SHA256r   Ú_OID_HMAC_SHA384r   Ú_OID_HMAC_SHA512r   r   r   r+   r,   r-   r   r.   )r/   r0   r1   Zenc_algor2   r3   Zpbes2_paramsrQ   Zkdf_oidZkdf_key_lengthZpbkdf2_paramsr5   r?   ÚleftÚidxZpbkdf2_prf_oidZpbkdf2_prf_algo_idrR   rS   rT   rO   r4   rM   ZIVr\   r6   r8   r9   r   rZ   r   r-   :  sœ   ÿ€
ÿ









ÿ
ÿ
zPBES2.decrypt)NN)r   r   r   r:   r;   rJ   r-   r   r   r   r   r<   ¯   s     r<   N)/Z
Cryptodomer   ZCryptodome.Util.asn1r   r   r   r   ZCryptodome.Util.Paddingr   r   ZCryptodome.Hashr	   r
   r   r   r   r   ZCryptodome.Cipherr   r   r   r   ZCryptodome.Protocol.KDFr   r   r   r'   r(   r)   r*   rK   rH   rI   r_   r`   ra   rb   rc   rA   rB   rC   rD   rE   r   Úobjectr   r<   r   r   r   r   Ú<module>   s2   ! /=