
    ¿igN                         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e!          Z" G d de#          Z$ G 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                       e Zd ZdS )	PbesErrorN)__name__
__module____qualname__     O/var/www/html/afkarena/venv/lib/python3.11/site-packages/Cryptodome/IO/_PBES.pyr   r   C   s        Dr   r   c                   (    e Zd ZdZed             ZdS )PBES1zDeprecated 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                    t                                          |           }t                                          |d                   }t                                          |d                   j        }t	                                          |d                   j        }i }|t          k    rddlm} ddl	m
} |}	|}
nm|t          k    rddlm} ddl	m} |}	|}
d|d<   nL|t          k    rddlm} ddl	m
} |}	|}
n0|t          k    rddlm} ddl	m} |}	|}
d|d<   nt!          d	          t                                          |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      )MD5)DES)ARC2@   effective_keylen)SHA1zUnknown OID for PBES1   nr_elements   N   )r   decoder   payloadr   value_OID_PBE_WITH_MD5_AND_DES_CBCCryptodome.Hashr   Cryptodome.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_infoencrypted_algorithmencrypted_datapbe_oidcipher_paramsr   r   hashmodmoduler   r    
pbe_paramssalt
iterationskey_ivkeyivcipherpts                        r   r1   zPBES1.decrypt   sT     +}}33D99)mm223G3JKK'))001Ea1HIIQ--&&':1'=>>D333++++++------GFF555++++++......GF02M,--666,,,,,,------GFF666,,,,,,......GF02M,--3444 ]]))*=a*@a)PP
&&z!}55=]

D"j'BB!*fQRRjRC"FFFF^^N++R*+++r   N)r   r   r   __doc__staticmethodr1   r   r   r   r   r   }   s9          :, :, \:, :, :,r   r   c                   @    e Zd ZdZedd            Zed             ZdS )PBES2zEncryption scheme with password-based key derivation
    (defined in `PKCS#5 v2.0`__).

    .. __: http://www.ietf.org/rfc/rfc2898.txt.Nc                 
   |i }|t          j                    j        }t          j        d          }|                    |          }|t          d|z            |                    d          r-d}|                    d          }|                    d          }	nd}|                    d          }	d	}
|	d
k    r'ddl	m
} d}|}|j        }t          }d |d          i}n9|	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}
nv|	dk    r,d}t          }t          j        }t&          }d |d          i}d}
nD|	dk    r,d}t          }t          j        }t(          }d |d          i}d}
nt          d|	z            t+          |                                          d         } ||                    dd                    }|dk    r|                    dd          }t1          j        |          }t3          |||||          }t5          t7          |          t9          |          g          }|d k    ru	 t0          j                            d!|"          j        }n# t>          $ r t          d#          w xY w|                     t5          tC          |          g                     t5          tC          tD                    |g          }n|dk    r|                    dd$          }|                    d%d          }|                    d&d'          }tG          ||||||          }t5          tC          tH                    t5          t7          |          t9          |          t9          |          t9          |          g          g          }n%t          d(|                    d'          z              |j        ||fi |}|
r|%                    |           \  }}||z   }n(|&                    tO          | |j(                            }t5          tC          |          t7          |          g          }t5          t5          tC          tR                    t5          ||g          g          t7          |          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.
        Nz5^(PBKDF2WithHMAC-([0-9A-Z-]+)|scrypt)And([0-9A-Z-]+)$zUnknown protection %sPBKDFpbkdf2r!      r   FzDES-EDE3-CBCr   DES3   rA   r%   z
AES128-CBCr$   z
AES192-CBCz
AES256-CBC    z
AES128-GCMnonce   Tz
AES192-GCMz
AES256-GCMzUnknown encryption mode '%s'	salt_sizeiteration_counti  hmac_hash_moduler    r   )	digestmodzNo OID for HMAC hash algorithmi @  r2   parallelizationr   zUnknown KDF )+r   r/   readrecompilematch
ValueError
startswithgroupr+   rM   r0   _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   HMACoidKeyErrorappendr   _OID_PBKDF2r   _OID_SCRYPTencrypt_and_digestencryptr
   r2   
_OID_PBES2encode)!r3   r4   
protectionprot_paramsrandfuncpatternrespbkdfpbkdf2_hmac_algoenc_algoaeadrM   key_sizer;   cipher_modeenc_oid	enc_paramiv_noncer=   countrV   r@   pbkdf2_paramshmac_oidkdf_infoscrypt_rscrypt_prB   cttagr7   enc_infor5   s!                                    r   rq   zPBES2.encrypt   s   ^ Kz||(H*UVVmmJ'';4zABBB  )) 	$E"yy||yy||HHEyy||H~%%......HF-K'Gxx{{+II%%HF,K%Gxx||,II%%HF,K%Gxx||,II%%HF,K%Gxx||,II%%HF,K%G ((2,,/IDD%%HF,K%G ((2,,/IDD%%HF,K%G ((2,,/IDD;hFGGG	((**++A.xQ7788 HOO$5t<<E!122I!*3	5 5 5C ( .t 4 4 *5 1 1)  M
  6))G#y}}SI}FFJHH G G G$%EFFFG$$[+h2G2G1H%I%IJJJ",,!$  HH
 hOO$5u==E"|Q77H"'8!<<HT8(4 4C",,&t,,"5))"8,,"8,,	!  $  HH ^ciill:;;; C::	:: 	J//55GB#XNN#^^Cf6G,H,HIING$$x(( 
    +J''     >**
,
 
  
  $**,,,s   &L Lc                 @  # t                                          | d          }t                                          |d                   }t                                          |d                   j        }t	                                          |d                   j        }|t          k    rt          d          t                                          |d         d          }t                                          |d         d          }t	                                          |d                   j        }d}	|t          k    rt                                          |d         d          }
t                                          |
d                   j        }|
d         }t          |
          dz
  }d}|dk    r'	 |
|         dz
  }	|dz  }|dz  }n# t          $ r Y nw xY wt          }|dk    rSt                                          |
|                   }t	                                          |d                   j        }n|t          k    rt                                          |d         d          #t                                          #d                   j        }#fd	d
D             \  }}}t          #          dk    r	#d         }	nd}	nt          d          t                                          |d                   }t	                                          |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    rt$          }t$          j        }d}d}n|t(          k    rt$          }t$          j        }d}d}n|t*          k    rt$          }t$          j        }d}d}d}n\|t.          k    rt$          }t$          j        }d}d}d}n7|t0          k    rt$          }t$          j        }d}d}d}nt          d|z             |	r|	|k    rt          d          t                                          |d                   j        }|t          k    rb	 t2          j        j        |         }n # t8          $ r t          d|z            w xY wt3          j        |          }t=          |||||          }nt?          ||||||          } |j        ||fi ||i}t          |          |j         k     rtC          d          |r0|j         } |"                    |d|           ||  d                   }!n*|#                    |          }"tI          |"|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!   rK      )r      c                      g | ]
}|         S r   r   ).0xscrypt_paramss     r   
<listcomp>z!PBES2.decrypt.<locals>.<listcomp>  s5     3G 3G 3G78 4A3C 3G 3G 3Gr   )r   r!   rK   r   zUnsupported PBES2 KDFFrL   rN   rA   r$   rO   rP   TzUnsupported PBES2 cipher z9Mismatch between PBES2 KDF parameters and selected cipherzUnsupported HMAC %srT   zToo little data to decrypt)%r   r&   r   r'   r   r(   rr   r   rn   len	TypeError_OID_HMAC_SHA1ro   r_   r+   rM   r0   r`   r	   ra   rb   rd   rc   re   rf   r   rj   _hmac2hash_oidrl   r/   r   r   r2   r\   decrypt_and_verifyr1   r   )$r3   r4   r5   r{   r7   r8   pbes2_paramsr   kdf_oidkdf_key_lengthr   r=   rS   leftidxpbkdf2_prf_oidpbkdf2_prf_algo_idr   r   r   r   r|   rM   r;   r~   r}   cipher_paramr   hmac_hash_module_oidrU   r@   rB   tag_lenrC   	pt_paddedr   s$                                      @r   r1   zPBES2.decrypt  s     +}}33Da3HH==''(<Q(?@@'))001Ea1HIIQ--&&x{339j  0111"}}++HQKQ+GG ==''QQ'GG--&&x{339 k!!'MM00!)0TTM!##**=+;<<DD+A.O}%%)DCaxx%23%7!%;NAID1HCC    D
 ,Naxx%0]]%9%9-:L%M%M"!,!5!56H6K!L!L!R##'MM00!&0QQM!##**=+;<<DD3G 3G 3G 3G<E3G 3G 3G/OXx=!!A%%!.q!1!%3444 ==''Q88--&&x{339'''......F-KHLL''F,KHLL''F,KHLL''F,KHLL''F,KH"LDD''F,KH"LDD''F,KH"LDD7(BCCC 	4n88 3 4 4 4 "##**8A;77? k!!H'+y'?'O$$ H H H 5 FGGGH#x(<==T8_*:< < <CC T8_!8- -CCHHh0GHH ~!2229::: 	5'G**>)G8)+D+97())+DF FBB ~66Iy&"344B	s   G 
G$#G$R- -S
)NN)r   r   r   rD   rE   rq   r1   r   r   r   rG   rG      sc        3 3
 - - - \-B W W \W W Wr   rG   )&rY   
Cryptodomer   r   Cryptodome.Util.asn1r   r   r   r   r+   r	   Cryptodome.Util.Paddingr
   r   Cryptodome.Protocol.KDFr   r   r   r)   r,   r-   r.   rr   rn   ro   r   r_   r`   ra   rb   rd   re   rf   r\   r   objectr   rG   r   r   r   <module>r      s  D 
			                       
 " ! ! ! ! ! . . . . . . . . : : : : : : : : : : 6  6 !8 !8 $
%&%( *++*++	 	 	 	 	
 	 	 	tB, B, B, B, B,F B, B, B,J` ` ` ` `F ` ` ` ` `r   