
    ĞiV                     4   d dl Z d dlmZ ddlmZ ddlmZ ej        rd Zd Z	nd Zd	 Z	d
 Z
dZd Zd Zd Zd1dZd1dZd Zd Zd Zdd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#d& Z$d' Z%d( Z&d) Z'd* Z(d+ Z)d, Z*d- Z+d. Z,d/ Z-g d0Z.dS )2    N)ceil   
compat_ord)
Cryptodomec                     t           j                            |t           j        j        |                              |           S )z/ Decrypt bytes with AES-CBC using pycryptodome )r   AESnewMODE_CBCdecryptdatakeyivs      OC:\Users\betacode\Desktop\descargarYoutube\venv\Lib\site-packages\yt_dlp/aes.pyaes_cbc_decrypt_bytesr      s/    ~!!#z~'>CCKKDQQQ    c                     t           j                            |t           j        j        |                              | |          S )z/ Decrypt bytes with AES-GCM using pycryptodome )r   r	   r
   MODE_GCMdecrypt_and_verifyr   r   tagnonces       r    aes_gcm_decrypt_and_verify_bytesr      s3    ~!!#z~'>FFYYZ^`cdddr   c           
      Z    t          t          t          t          | ||f                     S )zZ Decrypt bytes with AES-CBC using native implementation since pycryptodome is unavailable )bytesaes_cbc_decryptmaplistr   s      r   r   r      s%    _c$sB&@&@ABBBr   c                 \    t          t          t          t          | |||f                     S )zZ Decrypt bytes with AES-GCM using native implementation since pycryptodome is unavailable )r   aes_gcm_decrypt_and_verifyr   r   r   s       r   r   r      s)    /TD#sE;R1S1STUUUr   c           
      `    t          t          t          t          | ||f          i |          S N)r   aes_cbc_encryptr   r   )r   r   r   kwargss       r   aes_cbc_encrypt_bytesr&      s-    #dT3O"<"<GGGHHHr      c                 >    | d t          | d                             S )Nr   r   s    r   unpad_pkcs7r+   !   s"    &*T"X&&&&''r   c                 R    t           t          |           t           z  z
  }| |g|z  z   S )zn
    PKCS#7 padding

    @param {int[]} data        cleartext
    @returns {int[]}           padding data
    )BLOCK_SIZE_BYTESlen)r   remaining_lengths     r   pkcs7_paddingr0   %   s0     (#d))6F*FF#$'7777r   c                     t           t          |           z
  }|dddd}|dk     rt          d          ||vrt          d| d          |dk    r|rg | d} |d	z  }| ||         g|z  z   S )
z
    Pad a block with the given padding mode
    @param {int[]} block        block to pad
    @param padding_mode         padding mode
    r       )pkcs7iso7816
whitespacezerozBlock size exceededzPadding mode z is not implementedr4      r   )r-   r.   
ValueErrorNotImplementedError)blockpadding_modepadding_sizePADDING_BYTEs       r   	pad_blockr>   1   s     $c%jj0L 	 L a.///	\	)	)!"S,"S"S"STTTy  \ %L./,>>>r   c                    t          |          }t          t          |           t          z            }g }t	          |          D ]?}| |t          z  |dz   t          z           }|t          t          |          |          z  }@|S )a   
    Encrypt with aes in ECB mode. Using PKCS#7 padding

    @param {int[]} data        cleartext
    @param {int[]} key         16/24/32-Byte cipher key
    @param {int[]} iv          Unused for this mode
    @returns {int[]}           encrypted data
    r   )key_expansionr   r.   r-   rangeaes_encryptr0   r   r   r   expanded_keyblock_countencrypted_datair:   s           r   aes_ecb_encryptrH   L   s     !%%Ls4yy#3344KN; J JQ))AE5E+EEF+mE&:&:LIIIr   c                    t          |          }t          t          |           t          z            }g }t	          |          D ]2}| |t          z  |dz   t          z           }|t          ||          z  }3|dt          |                    S )z
    Decrypt with aes in ECB mode

    @param {int[]} data        cleartext
    @param {int[]} key         16/24/32-Byte cipher key
    @param {int[]} iv          Unused for this mode
    @returns {int[]}           decrypted data
    r   N)r@   r   r.   r-   rA   aes_decryptrC   s           r   aes_ecb_decryptrK   `   s     !%%Ls4yy#3344KN; ; ;Q))AE5E+EEF+e\:::*3t99*%%r   c                 $    t          | ||          S )z
    Decrypt with aes in counter mode

    @param {int[]} data        cipher
    @param {int[]} key         16/24/32-Byte cipher key
    @param {int[]} iv          16-Byte initialization vector
    @returns {int[]}           decrypted data
    )aes_ctr_encryptr   s      r   aes_ctr_decryptrN   s   s     4b)))r   c                    t          |          }t          t          |           t          z            }t	          |          }g }t          |          D ]o}t          |          }| |t          z  |dz   t          z           }	|	dgt          t          |	          z
  z  z  }	t          ||          }
|t          |	|
          z  }p|dt          |                    S )z
    Encrypt with aes in counter mode

    @param {int[]} data        cleartext
    @param {int[]} key         16/24/32-Byte cipher key
    @param {int[]} iv          16-Byte initialization vector
    @returns {int[]}           encrypted data
    r   r   N)	r@   r   r.   r-   iter_vectorrA   nextrB   xor)r   r   r   rD   rE   counterrF   rG   counter_blockr:   cipher_counter_blocks              r   rM   rM      s     !%%Ls4yy#3344K"ooGN; ; ;WQ))AE5E+EEF!(3u::566*=,GG#e%9:::*3t99*%%r   c                    t          |          }t          t          |           t          z            }g }|}t	          |          D ]b}| |t          z  |dz   t          z           }|dgt          t          |          z
  z  z  }t          ||          }	|t          |	|          z  }|}c|dt          |                    S )z
    Decrypt with aes in CBC mode

    @param {int[]} data        cipher
    @param {int[]} key         16/24/32-Byte cipher key
    @param {int[]} iv          16-Byte IV
    @returns {int[]}           decrypted data
    r   r   N)r@   r   r.   r-   rA   rJ   rR   )
r   r   r   rD   rE   decrypted_dataprevious_cipher_blockrG   r:   decrypted_blocks
             r   r   r      s     !%%Ls4yy#3344KN; & &Q))AE5E+EEF!(3u::566%e\::#o/DEEE %*3t99*%%r   r3   )r;   c                @   t          |          }t          t          |           t          z            }g }|}t	          |          D ]V}| |t          z  |dz   t          z           }	t          |	|          }	t          |	|          }
t          |
|          }||z  }|}W|S )a  
    Encrypt with aes in CBC mode

    @param {int[]} data        cleartext
    @param {int[]} key         16/24/32-Byte cipher key
    @param {int[]} iv          16-Byte IV
    @param padding_mode        Padding mode to use
    @returns {int[]}           encrypted data
    r   )r@   r   r.   r-   rA   r>   rR   rB   )r   r   r   r;   rD   rE   rF   rX   rG   r:   mixed_blockencrypted_blocks               r   r$   r$      s     !%%Ls4yy#3344KN; 	0 	0Q))AE5E+EEF%..%!677%k<@@/) /r   c                 R   t          dgt          z  t          |                    }t          |          dk    rg |dddd}nwt          t          |          t          z  z
  t          z  dz   }|dg|z  z   t	          dt          |          z                      dd                    z   }t          ||          }t          |          }t          | ||dgt          t          |          z
  z  z             }	t          t          |           t          z  z
  t          z  }
t          || dg|
z  z   t	          d                    dd          t          |           dz                      dd          z             z             }|t          |||          k    rt          d          |	S )a9  
    Decrypt with aes in GBM mode and checks authenticity using tag

    @param {int[]} data        cipher
    @param {int[]} key         16-Byte cipher key
    @param {int[]} tag         authentication tag
    @param {int[]} nonce       IV (recommended 12-Byte)
    @returns {int[]}           decrypted data
    r      r      bigzMismatching authentication tag)rB   r-   r@   r.   r   to_bytesghashincrN   rM   r8   )r   r   r   r   hash_subkeyj0fillghash_iniv_ctrrW   pad_lens_tags               r   r!   r!      s    qc$44mC6H6HIIK
5zzR!u!a!!A!q! CJJ1A$ABFVVYZZA3:%a#e**n-F-Fq%-P-P(Q(QQ;))
 WWF$T3!@PSVW]S^S^@^9_0_``N3t99/?#?@DTTG#-	
5))t99q=**1e446 7 7	7 E oeS"----9:::r   c                    t          |          t          z  dz
  }t          | |dt                             } t          d|dz             D ]s}t	          |           } t          |           } ||k    r"t          t          | t                              } t          | ||t          z  |dz   t          z                     } t| S )z
    Encrypt one block with aes

    @param {int[]} data          16-Byte state
    @param {int[]} expanded_key  176/208/240-Byte expanded key
    @returns {int[]}             16-Byte cipher
    r   N)	r.   r-   rR   rA   	sub_bytes
shift_rowsr   iter_mix_columnsMIX_COLUMN_MATRIXr   rD   roundsrG   s       r   rB   rB      s     "22Q6Ft\"3#3"3455D1fqj!! Y Y$;;(/@AABBD4a*:&:QUFV<V&VWXXKr   c                 z   t          |          t          z  dz
  }t          |dd          D ]s}t          | ||t          z  |dz   t          z                     } ||k    r"t	          t          | t                              } t          |           } t          |           } tt          | |dt                             S )z
    Decrypt one block with aes

    @param {int[]} data          16-Byte cipher
    @param {int[]} expanded_key  176/208/240-Byte expanded key
    @returns {int[]}             16-Byte state
    r   r   r)   N)	r.   r-   rA   rR   r   rn   MIX_COLUMN_MATRIX_INVshift_rows_invsub_bytes_invrp   s       r   rJ   rJ     s     "22Q6F61b!! # #4a*:&:QUFV<V&VWXX;;(/DEEFFDd##T""t\"3#3"34555r   c                    d}t          t          j        |                     } t          |                                          }|d|         dg|t	          |          z
  z  z   }t          |dt                   t          |                    |t          z  z  }| d|         }| |d         }t          |||dgt          |z
  z  z             }t          |          S )af  
    Decrypt text
    - The first 8 Bytes of decoded 'data' are the 8 high Bytes of the counter
    - The cipher key is retrieved by encrypting the first 16 Byte of 'password'
      with the first 'key_size_bytes' Bytes from 'password' (if necessary filled with 0's)
    - Mode of operation is 'counter'

    @param {str} data                    Base64 encoded string
    @param {str,unicode} password        Password (will be encoded with utf-8)
    @param {int} key_size_bytes          Possible values: 16 for 128-Bit, 24 for 192-Bit or 32 for 256-Bit
    @returns {str}                       Decrypted data
    r_   Nr   )
r   base64	b64decodeencoder.   rB   r-   r@   rN   r   )r   passwordkey_size_bytesNONCE_LENGTH_BYTESr   r   cipherrW   s           r   aes_decrypt_textr~     s      &&''DHOO%%&&H
?N?
#qc^c(mm-K&L
LC
c+++,mC.@.@
A
A^WgEg
hC$$$%E$%%&F$VS%1#AQTfAf:g2ghhN   r   )   r         r_   r'   r2   @   r7      6   (   c   |   w   {      k   o      0   r   g   +            v            }      Y   G                        r               &   r   ?         4            q      1      r      #                        r7         '      u   	      ,      r   n   Z      R   ;         )      /      S      r      r2         [   j         9   J   L   X                  C   M   3      E      r      P   <         Q      r            8               !   r'               r^         _      D            ~   =   d   ]      s   `      O      "   *         F               ^            2   :   
   I      $   \            b            y         7   m   r      N      l   V         e   z      r_      x   %   .                     t      K            p   >      f   H            a   5   W                              i                           U   (                        B   h   A      -         T         (   r   r   r   r.  r   r   r   r   rl  r   r   rW  r  r   r   r   r   r   r   r   r`  r   r   rb  r   r^  r   r   r  r  rc  r   ru  r   r_  r  r=  r"  r   r  r  r   r'  r  rn  r   r   r/  r_   r;  ri  rK  rf  r]  r   r   r   r   r   r  r-  rF  r   r:  r   rY  rN  r  rT  ro  rZ  rw  r   r   r!  r   r  r5  r   r   r1  rH  rL  r   r   r   rS  r   r  r   r  rR  r  r   r   r   r  r   r   r   rh  r   r#  r  r   r(  r   r   r  r   r   r  r   r   ra  r   r   r   rs  r   rU  r   rE  rM  r   r   rG  r   r  r&  r[  rp  r  r   r  r4  r   r   r   rd  r   r>  rm  r	  r   r$  rB  r  r*  r   rQ  r   r   r   r,  r@  r<  r   rg  r   r   r   r   r   rV  r   r   rj  r   r   r%  rO  r   r   r   r   r   r2  rI  rD  r?  r   r)  r2   r   r  r   r   r9  r   r   r3  rC  rA  r   r   r  r   r   r   r   r   r'   r   r   r7   r   r   r
  r   r   r0  r  rJ  r   rk  rr  r   r6  r   r   r   r   r   r   r  r   r   r7  r  r   rt  r+  r   rv  r   r   r   rq  rP  r  r   r   r  r8  r   r   r   rX  r\  r  r   re  r   r^   r   ))r   rM  r   r   )r   r   rM  r   )r   r   r   rM  )rM  r   r   r   ))rO  r  rk  r   )r   rO  r  rk  )rk  r   rO  r  )r  rk  r   rO  (   r   rM  r   rs  r[  r   re  r   r   r;  r   r   ri  rY  r   rQ  r   rX  r   rL  r   r	  r'  r   r   r   r  r  ra  r  rK  r   r   r   r!  r(  r,  r   r   r   r   r   r]  rH  r  r   rm  r   r   r   r   r^   r  r   r   r   r  r   ro  r  r#  r   r   r   r   r   r   r0  r  r   r   r   r%  r=  r   r_   r   rf  r9  r  r   rW  rS  r   r   rE  r  r   r  rZ  r   rd  r  r  r   r   rJ  r  rR  r   r'   r   r   r   r  rV  r   r\  rv  r   rP  r   r   r  r   r   rb  r   r   r   r   r   r   r7  rc  r2   r
  r   r   rw  r  r/  r   r-  r   r"  r  r*  r  r2  r   r   r   rp  r   r  r   r  r   r   r   r   r   r   r   rB  r   rl  r   r   r   r8  r.  r  r$  r   r  r  r   r   r   rg  r6  r^  rj  r7   r`  r   rU  r   r@  r   r5  r   r4  r:  r   r   r+  r   r   ru  r   rC  r   r   r   r3  r   r   r   rr  r   rq  rt  r   r  r   r   r   r   r  r)  rF  rT  r&  r   r   rI  rn  r?  r   r   rO  r   r   r   r  r   r   r   rh  r   rG  r   r_  r  r   rk  r  r   rD  rA  r   r   r   r   r   r<  r   r1  r>  r   r   rN  r   (   r   r   r  r   r  r   r   r?  rD  r   r   ro  r   r  rg  rM  r  r   r  rO  r   r   r  r   r   r   r_   r+  rY  r\  r<  rU  r   r"  rV  rJ  r   rS  r   r   r   r(  r=  r   r   r   r   r9  r5  r   rG  r   r   rs  rX  r   r   r   r   r   rQ  r   r   r^  r   r   r  rE  r   r   rd  r_  r   r!  r   r   r   r  r   r   rK  rA  r   r   rl  r  rF  r%  r   r:  r   rZ  r  r  r&  r'   r  r   rL  r   r   r   ra  rn  r  r   rf  ru  r   r   r  r8  r   r)  r  r   r`  r   r  r   r/  r   r$  r   r   r	  r  rR  r   r   r   r   r3  r4  r   rB  r  r7  rc  r.  r*  rm  r   r@  r   r   r   r6  r   rw  r  r   r   r   r   rt  r   r0  r   r   r   r^   rN  r   r  r  r  r   r   r   rC  rr  r   r   r   r   r   rv  rI  r   r   r
  r   rT  r   r   ri  r1  r   re  r   r   r   r   rb  r  rP  r   r  r   r   r'  r   r   r,  r   r   r   r   r   r   r   rp  r   r-  r   r  r  rW  r  r2  r   r#  r   r   r[  r   r]  r   r2   r;  rj  r>  r   r  r   r   rq  r   r   r   r   r   r  r   r   r   r   rk  r   rh  r7   r   r   rH  r   c           	         | dd         } d}t          |           }|dz  dz   t          z  }t          |           |k     r| dd         }t          ||          }|dz  }| t          || | d|z
                     z  } t	          d          D ]+}| dd         }| t          || | d|z
                     z  } ,|dk    r8| dd         }t          |          }| t          || | d|z
                     z  } t	          |dk    rdn	|dk    rd	nd
          D ]+}| dd         }| t          || | d|z
                     z  } ,t          |           |k     | d|         S )z
    Generate key schedule

    @param {int[]} data  16/24/32-Byte cipher key
    @returns {int[]}     176/208/240-Byte expanded key
    Nr   r   r   rM  r2   r   r   r   )r.   r-   key_schedule_corerR   rA   rl   )r   rcon_iterationr{   expanded_key_size_bytestemp_s         r   r@   r@     s    7DNYYN-2Q6:JJ
d))-
-
-BCCy ~66!D$N0BBCDDDq 	I 	IA9DCdN?A4F#FGHHHDDR9DT??DCdN?A4F#FGHHHDNb00qq>R;O;OaaUVWW 	I 	IA9DCdN?A4F#FGHHHDD# d))-
-
-$ ((())r   c              #   0   K   	 | V  t          |           } r#   )rc   )r   s    r   rP   rP     s$      WWr   c                     d | D             S )Nc                 (    g | ]}t           |         S  )SBOX.0xs     r   
<listcomp>zsub_bytes.<locals>.<listcomp>  s    """DG"""r   r  r*   s    r   rl   rl     s    ""T""""r   c                     d | D             S )Nc                 (    g | ]}t           |         S r  )SBOX_INVr  s     r   r  z!sub_bytes_inv.<locals>.<listcomp>  s    &&&AHQK&&&r   r  r*   s    r   ru   ru     s    &&&&&&r   c                 *    g | dd          | d         S )Nr   r   r  r*   s    r   rotater    s    T!""XtAwr   c                 t    t          |           } t          |           } | d         t          |         z  | d<   | S )Nr   )r  rl   RCON)r   r{  s     r   rz  rz    s4    $<<DT??D1g^,,DGKr   c                 :    d t          | |d          D             S )Nc                     g | ]
\  }}||z  S r  r  )r  r  ys      r   r  zxor.<locals>.<listcomp>  s     >>>daAE>>>r   F)strict)zip)data1data2s     r   rR   rR     s%    >>c%u===>>>>r   c           	   #     K   dD ]}|D ]z}d}t          d          D ]b}|| ||dz            |         dk    s||         dk    rdn7t          t          | ||z                     t          ||                  z   dz           z  }c|V  {d S )N)r   r   r_   r^   r   r   r   )rA   RIJNDAEL_EXP_TABLERIJNDAEL_LOG_TABLE)r   matrixrG   rowmixedjs         r   rn   rn     s         	 	CE1XX u utAa!eG}Q/144A!!!,.@a!e.MPbcfghciPj.jnr-rsuKKKK	 r   c                 :      fdt          d          D             S )Nc                 \    g | ](}t          d           D ]}||z   dz  d z  |z            )S r   rM  rA   r  columnr  r   s      r   r  zshift_rows.<locals>.<listcomp>  H    ___V[\]V^V^__sD6C<4'1,s23____r   r   r  r*   s   `r   rm   rm     #    ____%((____r   c                 :      fdt          d          D             S )Nc                 \    g | ](}t          d           D ]}||z
  dz  d z  |z            )S r  r  r  s      r   r  z"shift_rows_inv.<locals>.<listcomp>  r  r   r   r  r*   s   `r   rt   rt     r  r   c                 d    g }d}| D ](}|r|dz  }|dz  }|dz  }|                     |           )|S )Nr      r   )append)r   data_shiftedbitns       r   shift_blockr    s[    L
C   	JA!e	aAr   c                     | d d          } t          t          |           dz
  dd          D ]"}| |         dk    rd| |<   | |         dz   | |<    | S )Nr   r)   r   r   )rA   r.   )r   rG   s     r   rc   rc     se    7D3t99q="b))  7c>>DGG1gkDGKr   c                    t          |           t          k    st          |          t          k    rt          dt           d          dgdgt          dz
  z  z   }|d d          }dgt          z  }| D ]Z}t          ddd          D ]F}|d|z  z  rt	          ||          }|d         dz  }t          |          }|rt	          ||          }G[|S )NzLength of blocks need to be  bytesrX  r   r   r   r)   )r.   r-   r8   rA   rR   r  )block_xblock_yblock_rblock_vblock_zrG   r  do_xors           r   block_productr    s     7||'''3w<<;K+K+KP8HPPPQQQfs.233GaaajGc$$G 0 0B## 	0 	0CAH~ 0gw//R[1_F!'**G 0gw//	0 Nr   c                 &   t          |          t          z  rt          dt           d          dgt          z  }t          dt          |          t                    D ]2}|||t          z            }t	          t          ||          |           }3|S )NzLength of data should be r  r   )r.   r-   r8   rA   r  rR   )subkeyr   last_yrG   r:   s        r   rb   rb     s     4yy## OM5EMMMNNNS##F1c$ii!122 ; ;Q,,,-s65116::Mr   )r   r   r$   r&   rN   rM   rJ   r~   rK   rH   rB   r!   r   r@   r>   r0   r+   r#   )/rw   mathr   compatr   dependenciesr   r	   r   r   r&   r-   r+   r0   r>   rH   rK   rN   rM   r   r$   r!   rB   rJ   r~   r  r  r  ro   rs   r  r  r@   rP   rl   ru   r  rz  rR   rn   rm   rt   r  rc   r  rb   __all__r  r   r   <module>r     s                $ $ $ $ $ $> VR R Re e e e
C C CV V V
I I I  ( ( (	8 	8 	8? ? ?6   (& & & &&	* 	* 	*& & &0& & &0 4;     :( ( (V  *6 6 6&! ! !8 Jh l + / v  v $* * *B  # # #' ' '       ? ? ?  ` ` `` ` `      .    r   