U
    gV                     @   s  d dl Z d dlmZ ddlmZ ddlmZ ddlmZm	Z	 ej
rTdd Zd	d
 Zndd Zdd
 Zdd ZdZdd Zdd Zdd ZdOddZdPddZdd Zdd Zdd Zd d!d"d#Zd$d% Zd&d' Zd(d) Zd*d+ Zd,Zd-Zd.Zd/Zd0Z d1Z!d2Z"d3d4 Z#d5d6 Z$d7d8 Z%d9d: Z&d;d< Z'd=d> Z(d?d@ Z)dAdB Z*dCdD Z+dEdF Z,dGdH Z-dIdJ Z.dKdL Z/dMdN Z0dddd+d)dd%d
d#dddd'd4dddgZ1dS )Q    N)ceil   
compat_ord)
Cryptodome)bytes_to_intlistintlist_to_bytesc                 C   s   t j|t jj|| S )z/ Decrypt bytes with AES-CBC using pycryptodome )r   AESnewZMODE_CBCZdecryptdatakeyiv r   ./tmp/pip-unpacked-wheel-q5ljy6pj/yt_dlp/aes.pyaes_cbc_decrypt_bytes	   s    r   c                 C   s   t j|t jj|| |S )z/ Decrypt bytes with AES-GCM using pycryptodome )r   r	   r
   ZMODE_GCMZdecrypt_and_verifyr   r   tagnoncer   r   r    aes_gcm_decrypt_and_verify_bytes   s    r   c                 C   s   t ttt| ||f S )zZ Decrypt bytes with AES-CBC using native implementation since pycryptodome is unavailable )r   aes_cbc_decryptmapr   r   r   r   r   r      s    c                 C   s   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   r   r   r   r      s    c                 K   s   t ttt| ||f|S N)r   aes_cbc_encryptr   r   )r   r   r   kwargsr   r   r   aes_cbc_encrypt_bytes   s    r      c                 C   s   | d t | d   S )Nr   r   r   r   r   unpad_pkcs7"   s    r    c                 C   s   t t| t   }| |g|  S )zn
    PKCS#7 padding

    @param {int[]} data        cleartext
    @returns {int[]}           padding data
    )BLOCK_SIZE_BYTESlen)r   Zremaining_lengthr   r   r   pkcs7_padding&   s    r#   c                 C   sr   t t|  }|dddd}|dk r,tdn||krDtd| d|dkr`|r`| d
} |d	8 }| || g|  S )z
    Pad a block with the given padding mode
    @param {int[]} block        block to pad
    @param padding_mode         padding mode
    r       )pkcs7iso7816
whitespaceZzerozBlock size exceededzPadding mode z is not implementedr&      r   )r(   )r!   r"   
ValueErrorNotImplementedError)blockpadding_modeZpadding_sizeZPADDING_BYTEr   r   r   	pad_block2   s    
r-   c                 C   s`   t |}tttt| t }g }t|D ].}| |t |d t  }|tt||7 }q,|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_expansionintr   floatr"   r!   rangeaes_encryptr#   r   r   r   expanded_keyblock_countencrypted_datair+   r   r   r   aes_ecb_encryptM   s    	r8   c                 C   sh   t |}tttt| t }g }t|D ]*}| |t |d t  }|t||7 }q,|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   r0   r"   r!   r1   aes_decryptr3   r   r   r   aes_ecb_decrypta   s    	r:   c                 C   s   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   r   r   r   aes_ctr_decryptt   s    	r<   c                 C   s   t |}tttt| t }t|}g }t|D ]R}t|}| |t |d t  }	|	dgtt|	  7 }	t	||}
|t
|	|
7 }q4|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   r0   r"   r!   iter_vectorr1   nextr2   xor)r   r   r   r4   r5   counterr6   r7   Zcounter_blockr+   Zcipher_counter_blockr   r   r   r;      s    	
r;   c           
      C   s   t |}tttt| t }g }|}t|D ]N}| |t |d t  }|dgtt|  7 }t||}	|t|	|7 }|}q0|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   r0   r"   r!   r1   r9   r?   )
r   r   r   r4   r5   decrypted_dataprevious_cipher_blockr7   r+   Zdecrypted_blockr   r   r   r      s    	
r   r%   )r,   c                C   s|   t |}tttt| t }g }|}t|D ]F}| |t |d t  }	t|	|}	t|	|}
t	|
|}||7 }|}q0|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   r0   r"   r!   r1   r-   r?   r2   )r   r   r   r,   r4   r5   r6   rB   r7   r+   Zmixed_blockZencrypted_blockr   r   r   r      s    



r   c              	   C   s
  t dgt t|}t|dkr*|d}nHtt|t  t d }|dg|  tdt| dd }t||}t|}t| ||dgtt|   }	t| d d }
t|| dgtt|  |
   tdddt| d dd  }|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      bigr   zMismatching authentication tag)r   r   r   r   )r2   r!   r.   r"   r   to_bytesghashincr<   r;   r)   )r   r   r   r   Zhash_subkeyZj0fillZghash_inZiv_ctrrA   Zpad_lenZs_tagr   r   r   r      s,    
&
r   c                 C   s~   t |t d }t| |dt } td|d D ]H}t| } t| } ||krZtt| t} t| ||t |d t  } q0| 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!   r?   r1   	sub_bytes
shift_rowslistiter_mix_columnsMIX_COLUMN_MATRIXr   r4   roundsr7   r   r   r   r2      s     r2   c                 C   sx   t |t d }t|ddD ]H}t| ||t |d t  } ||krTtt| t} t| } t| } qt| |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!   r1   r?   rL   rM   MIX_COLUMN_MATRIX_INVshift_rows_invsub_bytes_invrO   r   r   r   r9     s    
r9   c                 C   s   d}t t| } t | }|d| dg|t|   }t|dt t||t  }| d| }| |d }t|||dgt|   }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
    rD   Nr   )
r   base64	b64decodeencoder"   r2   r!   r.   r<   r   )r   passwordkey_size_bytesZNONCE_LENGTH_BYTESr   r   cipherrA   r   r   r   aes_decrypt_text   s    rZ   )   r         rD   r   r$   @   r(      6   (   c   |   w   {      k   o      0   r   g   +            v            }      Y   G                        r               &   r`   ?         4            q      1      r]      #                        r(         '      u   	      ,      r_   n   Z      R   ;         )      /      S      r      r$         [   j         9   J   L   X                  C   M   3      E      r\      P   <         Q      r^            8               !   r               rC         _      D            ~   =   d   ]      s   `      O      "   *         F               ^            2   :   
   I      $   \            b            y         7   m   r[      N      l   V         e   z      rD      x   %   .                     t      K            p   >      f   H            a   5   W                              i                           U   (                        B   h   A      -         T         (   r   r   r   r
  ri   r`   r   r   rH  r^   r   r3  r   r   rm   r   rb   r   r   rq   r<  r   r   r>  r   r:  r   r   r   r   r?  r   rQ  rd   r;  r   r  r   r   r   r   r   r  r   rJ  rt   r   r  rD   r  rE  r'  rB  r9  r   r   ro   r   rz   r   r	  r"  r   r  r~   r5  r*  r   r0  rK  r6  rS  ry   r}   r   r   r   r  r   r   r  r$  r(  r   r   r   r/  r   r   r   r   r.  r   r[   r   r   r   r   rn   r   rD  r   r   r   r   r  r   r   r   r   r   r   r   r   r=  r   rp   r   rO  r\   r1  r{   r!  r)  r   r   r#  rf   r   r  r7  rL  r   rj   r   r  r   re   r   r@  rw   r  rI  r   r   r   r  r   r  rx   r-  r   r   r   r  r  r  r   rC  r   rv   r   r   r   r2  r   rh   rF  rg   r   r  r+  r   r   r   r_   r   r  r%  r   r  r   r  r$   r   r   r   rl   r  r   r   r  r  r  r   r   r   r   r   r   r   r   r   ru   r   r(   r   r   r   r   r   r  r   r&  r   rG  rN  r   r  r   r   rr   r|   r   r   r   r   r   r  r   r   rP  r  r   rR  r   r   r   rM  r,  r   rk   r]   r   r  rc   r   r   r4  r8  r   ra   rA  r   rC   rs   ))r\   r)  r   r   )r   r\   r)  r   )r   r   r\   r)  )r)  r   r   r\   ))r+  r   rG  r   )r   r+  r   rG  )rG  r   r+  r   )r   rG  r   r+  (   r   r)  r   rO  r7  r   rA  r   r   r  r~   r   rE  r5  r   r-  r   r4  r   r(  r   r   r  r}   r   r\   r   r   r=  r   r'  r   r   r   r   r  r  ru   r   r   r   r   r9  r$  r   rn   rI  r   r   r   r]   rC   r   r   r   r   r   r   rK  r   r   r   r   r   r   ry   rj   r  r   r   r   rm   r  r  r   rD   r   rB  r  r   r   r3  r/  r   rf   r!  r   r   r   r6  r   r@  r   r   ro   r   r&  r   r.  r   r   ri   r   rw   r   r2  r   r8  rR  r   r,  r   rl   r   rk   rs   r>  r   rx   r   r   r   r   r  r?  r$   r   r   r   rS  r   r  r   r	  r   r   r   r  r   r  rt   r   r   rL  r   r   r   r   rv   rr   r^   r   r   r   r   r  r|   rH  r   r   r   r  r
  r   r   r   r   r   rq   r   r   rC  r  r:  rF  r(   r<  r   r1  r   r  r   r  r{   r  r  rg   r   r  r   rh   rQ  r   r  r   ra   r   r  r   r   r_   rN  rc   rM  rP  r   r   rp   r   r   r   r   r  r"  r0  r  r   r   r%  rJ  r  r   r   r+  r   r`   r   r   r   rd   r[   rD  r   r#  r   r;  r   re   rG  r   r   r   r  rb   r   r   rz   r   r  r   r  r  r   r   r*  r   (   r   r   r   r   r   r\   r   r  r   r   r_   rK  r   r   rC  r)  r   r]   r   r+  r   r[   r   r   r   r   rD   r  r5  r8  r  r1  rs   r   r2  r&  r   r/  r   r   r   r  r  r~   r   rr   r   r  r  r   r#  r   r   rO  r4  r   r   rw   rq   r   r-  r   r   r:  r   r   r   r!  r`   r   r@  r;  r   r   r   r   r^   r   r   r   r'  r  r   ri   rH  r   r"  r  r   r  r   r6  r   r   r  r   r   r   r(  r   r   r   r=  rJ  r   rf   rB  rQ  rt   r   r   r  rk   r  r   r   r<  r   r   rp   r  ry   r   r   r   r   r   r.  r{   r   r   r   r  r  r   r  r   r  r?  r
  r  rI  rx   r  r   rm   r   r  r   rS  r   r   ru   r   r   rP  r|   r  r   r   r   rC   r*  rg   r   r   r   r   r   r   r  rN  r}   ro   rd   r   r   rR  r%  r   r   r   r   r0  r   r   rE  r  r   rA  r   r   r   r   r>  r   r,  r   r   r   r   r  r   r   r  r   r   r   r   r   r   r   rL  rz   r	  rv   r   r   r3  r   r  re   r   rn   r   r7  r   r9  r   r$   r  rF  r  rb   r   r   rc   rM  r   r   rj   r   r   r   rh   r   rl   r   rG  ra   rD  r(   r   r   r$  r   c                 C   s:  | dd } d}t | }|d d t }t | |k r.| dd }t||}|d7 }| t|| | d|  7 } tdD ],}| dd }| t|| | d|  7 } qx|dkr| dd }t|}| t|| | d|  7 } t|dkrdn|dkrd	nd
D ],}| dd }| t|| | d|  7 } qq(| 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   r)  r$   r   r\   r   )r"   r!   key_schedule_corer?   r1   rJ   )r   rcon_iterationrX   Zexpanded_key_size_bytestemp_r   r   r   r.     s(    
$ r.   c                 c   s   | V  t | } q d S r   )rH   )r   r   r   r   r=     s    r=   c                 C   s   dd | D S )Nc                 S   s   g | ]}t | qS r   )SBOX.0xr   r   r   
<listcomp>  s     zsub_bytes.<locals>.<listcomp>r   r   r   r   r   rJ     s    rJ   c                 C   s   dd | D S )Nc                 S   s   g | ]}t | qS r   )SBOX_INVrZ  r   r   r   r]    s     z!sub_bytes_inv.<locals>.<listcomp>r   r   r   r   r   rS     s    rS   c                 C   s   | dd  | d g S )Nr   r   r   r   r   r   r   rotate  s    r_  c                 C   s(   t | } t| } | d t| A | d< | S )Nr   )r_  rJ   RCON)r   rV  r   r   r   rU    s    rU  c                 C   s   dd t | |D S )Nc                 S   s   g | ]\}}||A qS r   r   )r[  r\  yr   r   r   r]    s     zxor.<locals>.<listcomp>)zip)Zdata1Zdata2r   r   r   r?     s    r?   c              	   c   s   dD ]v}|D ]l}d}t dD ]T}|| ||d  | dksF|| dkrJdn"tt| ||   t||   d  N }q|V  qqd S )N)r   r]   rD   rC   r   r]   r   )r1   RIJNDAEL_EXP_TABLERIJNDAEL_LOG_TABLE)r   Zmatrixr7   rowmixedjr   r   r   rM     s    *"rM   c                    s    fddt dD S )Nc                    s2   g | ]*}t d D ]} || d@ d  |  qqS r]   r)  r1   r[  columnre  r   r   r   r]    s     
  zshift_rows.<locals>.<listcomp>r]   ri  r   r   r   r   rK     s    rK   c                    s    fddt dD S )Nc                    s2   g | ]*}t d D ]} || d@ d  |  qqS rh  ri  rj  r   r   r   r]    s     
  z"shift_rows_inv.<locals>.<listcomp>r]   ri  r   r   r   r   rR     s    rR   c                 C   s<   g }d}| D ]*}|r|dO }|d@ }|dL }| | q|S )Nr      r   )append)r   Zdata_shiftedbitnr   r   r   shift_block  s    rp  c                 C   sT   | d d  } t t| d ddD ].}| | dkr:d| |< q | | d | |<  qPq | S )Nr   r   r   r   )r1   r"   )r   r7   r   r   r   rH     s    
rH   c                 C   s   t | tkst |tkr(tdt ddgdgtd   }|d d  }dgt }| D ]N}tdddD ]<}|d|> @ rt||}|d d@ }t|}|rft||}qfqV|S )NzLength of blocks need to be  bytesr4  r   r   r   r   )r"   r!   r)   r1   r?   rp  )Zblock_xZblock_yZblock_rZblock_vZblock_zr7   rn  Zdo_xorr   r   r   block_product  s    

rr  c                 C   s`   t |t rtdt ddgt }tdt |tD ]$}|||t  }tt||| }q6|S )NzLength of data should be rq  r   )r"   r!   r)   r1   rr  r?   )subkeyr   Zlast_yr7   r+   r   r   r   rG   	  s    
rG   )N)N)2rT   mathr   compatr   Zdependenciesr   utilsr   r   r	   r   r   r   r!   r    r#   r-   r8   r:   r<   r;   r   r   r   r2   r9   rZ   r`  rY  r^  rN   rQ   rc  rd  r.   r=   rJ   rS   r_  rU  r?   rM   rK   rR   rp  rH   rr  rG   __all__r   r   r   r   <module>   s~   


+!