
    9iM                         S SK r S SKJr  S SKJr  S SKJrJrJrJr  S SK	J
r
  S SKJrJr  S SKJrJrJr  SrS	rS
rSrSrSrSrSrSrSrSrSrSrSrSr  " S S\!5      r" " S S\#5      r$ " S S\#5      r%g)    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                       \ rS rSrSrg)	PbesErrorC    N)__name__
__module____qualname____firstlineno____static_attributes__r       O/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/Crypto/IO/_PBES.pyr   r   C   s    r   r   c                   (    \ rS rSrSr\S 5       rSrg)PBES1}   zDeprecated 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                 P   [        5       R                  U 5      n[        5       R                  US   5      n[        5       R                  US   5      R                  n[	        5       R                  US   5      R
                  n0 nU[        :X  a  SSKJn  SSK	J
n  Un	Un
OfU[        :X  a  SSKJn  SSK	Jn  Un	Un
SUS'   OFU[        :X  a  SSKJn  SSK	J
n  Un	Un
O+U[        :X  a  SSKJn  SSK	Jn  Un	Un
SUS'   O[!        S	5      e[        5       R                  US   S
S9n[        5       R                  US   5      R                  nUS   n[#        XSX5      nUSS USS nnU
R$                  " UU
R&                  U40 UD6nUR)                  U5      n[+        UUR,                  5      $ )a0  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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_infoencrypted_algorithmencrypted_datapbe_oidcipher_paramsr   r    hashmodmoduler!   r$   
pbe_paramssalt
iterationskey_ivkeyivcipherpts                        r   r5   PBES1.decrypt   s     +}33D9)m223G3JK')001Ea1HIQQ-&&':1'=>DD33')GF55'*GF02M,-66()GF66(*GF02M,-344 ]))*=a*@a)P
&&z!}5==]

"jB!*fQRjRC"FF^^N+R**++r   r   N)r   r   r   r   __doc__staticmethodr5   r   r   r   r   r   r   }   s     :, :,r   r   c                   <    \ rS rSrSr\SS j5       r\S 5       rSrg)PBES2   zEncryption scheme with password-based key derivation
(defined in `PKCS#5 v2.0`__).

.. __: http://www.ietf.org/rfc/rfc2898.txt.Nc                 0	   Uc  0 nUc  [         R                  " 5       R                  n[        R                  " S5      nUR                  U5      nUc  [        SU-  5      eUR                  S5      (       a%  SnUR                  S5      nUR                  S5      n	OSnUR                  S5      n	Sn
U	S	:X  a(  S
SK	J
n  SnUnUR                  n[        nSU" S5      0nGO.U	S:X  a)  Sn[        n[        R                  n[        nSU" S5      0nOU	S:X  a)  Sn[        n[        R                  n[        nSU" S5      0nOU	S:X  a)  Sn[        n[        R                  n[         nSU" S5      0nOU	S:X  a+  Sn[        n[        R"                  n[$        nSU" S5      0nSn
OpU	S:X  a+  Sn[        n[        R"                  n[&        nSU" S5      0nSn
O?U	S:X  a+  Sn[        n[        R"                  n[(        nSU" S5      0nSn
O[        SU	-  5      e[+        UR-                  5       5      S
   nU" UR/                  SS5      5      nUS:X  a  UR/                  SS5      n[0        R                  " W5      n[3        UUUUUS9n[5        [7        U5      [9        U5      /5      nUS:w  aM   [0        R:                  R                  S US!9R<                  nURA                  [5        [C        U5      /5      5        [5        [C        [D        5      U/5      nOUS:X  a  UR/                  SS#5      nUR/                  S$S5      nUR/                  S%S&5      n[G        UUUUUU5      n[5        [C        [H        5      [5        [7        U5      [9        U5      [9        U5      [9        U5      /5      /5      nO[        S'UR                  S&5      -   5      eUR                  " UU40 UD6nU
(       a  URK                  U 5      u  nnUU-   nO%URM                  [O        U URP                  5      5      n[5        [C        U5      [7        U5      /5      n[5        [5        [C        [R        5      [5        UU/5      /5      [7        U5      /5      n U RU                  5       $ ! [>         a    [        S"5      ef = f)(av  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.
z5^(PBKDF2WithHMAC-([0-9A-Z-]+)|scrypt)And([0-9A-Z-]+)$zUnknown protection %sPBKDFpbkdf2r%      r   FzDES-EDE3-CBCr   DES3   rE   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 @  r6   parallelizationr   zUnknown KDF )+r   r3   readrecompilematch
ValueError
startswithgroupr/   rS   r4   _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
   r6   
_OID_PBES2encode)!r7   r8   
protectionprot_paramsrandfuncpatternrespbkdfpbkdf2_hmac_algoenc_algoaeadrS   key_sizer?   cipher_modeenc_oid	enc_paramiv_noncerA   countr\   rD   pbkdf2_paramshmac_oidkdf_infoscrypt_rscrypt_prF   cttagr;   enc_infor9   s!                                    r   rw   PBES2.encrypt   s   ^ Kzz|((H**UVmmJ';4zABB  ))E"yy|yy|HEyy|H~%*HF--K'Gx{+I%HF,,K%Gx|,I%HF,,K%Gx|,I%HF,,K%Gx|,I%HF,,K%G (2,/ID%HF,,K%G (2,/ID%HF,,K%G (2,/ID;hFGG	((*+A.Q78 HOO$5t<E!12I!*3	5C ( .t 4 *5 1) M
  6)G#yy}}SI}FJJH $$[+h2G1H%IJ",!$ H
 hOO$5u=E"|Q7H"'8!<HT8(4C",&t,"5)"8,"8,	! $ H ^ciil:;; C:	://5GB#XN#^^Cf6G6G,HING$x( 
   +J'   >*
,
 
  $**,,k   G$%EFFGs   !(Q? ?Rc                 	   [        5       R                  U SS9n[        5       R                  US   5      n[        5       R                  US   5      R                  n[	        5       R                  US   5      R
                  nU[        :w  a  [        S5      e[        5       R                  US   SS9n[        5       R                  US   SS9n[	        5       R                  US   5      R
                  nSn	U[        :X  a  [        5       R                  US   SS9n
[        5       R                  U
S   5      R                  nU
S   n[        U
5      S-
  nSnUS:  a   X   S-
  n	US-  nUS-  n[        nUS:  aA  [        5       R                  X   5      n[	        5       R                  US   5      R
                  nOU[        :X  as  [        5       R                  US   SS9n[        5       R                  US   5      R                  nS	 Vs/ s H  nUU   PM
     snu  nnn[        U5      S
:  a  US
   n	OSn	O[        S5      e[        5       R                  US   5      n[	        5       R                  US   5      R
                  nSnU[        :X  a  SSKJn  UnUR                   nSnSnOU["        :X  a  [$        n[$        R                   nSnSnOU[&        :X  a  [$        n[$        R                   nSnSnOU[(        :X  a  [$        n[$        R                   nSnSnOU[*        :X  a  [$        n[$        R,                  nSnSnSnO\U[.        :X  a  [$        n[$        R,                  nSnSnSnO5U[0        :X  a  [$        n[$        R,                  nSnSnSnO[        SU-   5      eU	(       a  U	U:w  a  [        S5      e[        5       R                  US   5      R                  nU[        :X  aA   [2        R4                  R6                  W   n[2        R:                  " U5      n[=        XUUUS9n O[?        XUUWW5      n UR:                  " U U40 UU0D6n![        U5      U!R@                  :  a  [C        S5      eU(       a(  U!R@                  n"U!RE                  USU"*  UU"* S 5      n#U#$ U!RG                  U5      n$[I        U$U!R@                  5      n#U#$ ! [         a     GNff = fs  snf ! [8         a    [        SW-  5      ef = f)a0  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%   rQ      )r      )r   r%   rQ   r   zUnsupported PBES2 KDFFrR   rT   rE   r(   rU   rV   TzUnsupported PBES2 cipher z9Mismatch between PBES2 KDF parameters and selected cipherzUnsupported HMAC %srZ   zToo little data to decrypt)%r   r*   r   r+   r   r,   rx   r   rt   len	TypeError_OID_HMAC_SHA1ru   re   r/   rS   r4   rf   r	   rg   rh   rj   ri   rk   rl   r   rp   _hmac2hash_oidrr   r3   r   r   r6   rb   decrypt_and_verifyr5   r   )%r7   r8   r9   r   r;   r<   pbes2_paramsr   kdf_oidkdf_key_lengthr   rA   rY   leftidxpbkdf2_prf_oidpbkdf2_prf_algo_idscrypt_paramsxr   r   r   r   r   rS   r?   r   r   cipher_paramr   hmac_hash_module_oidr[   rD   rF   tag_lenrG   	pt_paddeds%                                        r   r5   PBES2.decrypt  s     +}33Da3H=''(<Q(?@')001Ea1HIQQ-&&x{399j 011"}++HQKQ+G =''QQ'G-&&x{399 k!'M00!)0TM!#**=+;<DDD+A.O}%)DCax%2%7!%;NAID1HC ,Nax%0]%9%9-:L%M"!,!5!56H6K!L!R!R#'M00!&0QM!#**=+;<DDD<E3G<Eq 4A3C<E3G/OXx=!A%!.q!1!%344 =''Q8-&&x{399''*F--KHL'F,,KHL'F,,KHL'F,,KHL'F,,KH"LD'F,,KH"LD'F,,KH"LD7(BCCn8 3 4 4 "#**8A;7?? k!H'+yy'?'?'O$  $xx(<=8_*:<C 8_!8-CCHh0GH ~!2!229::''G**>)G8+D+97()+DFB 	 ~6Iy&"3"34B	S ! 3GH  H 5 FGGHs$   S 	S$S 
SSS4r   )NN)	r   r   r   r   rI   rJ   rw   r5   r   r   r   r   rL   rL      s4    3
 - -B W Wr   rL   )&r_   Cryptor   r   Crypto.Util.asn1r   r   r   r   r/   r	   Crypto.Util.Paddingr
   r   Crypto.Protocol.KDFr   r   r   r-   r0   r1   r2   rx   rt   ru   r   re   rf   rg   rh   rj   rk   rl   rb   r   objectr   rL   r   r   r   <module>r      s   D 
   
  * 6 6 6  6 !8 !8 $
%&%( *++*++	
 	tB,F B,J`F `r   