
     h[                         d dl mZmZmZmZ d dlZddl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 dd	lmZmZ g 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S )    )unicode_literalsdivisionabsolute_importprint_functionN   )pretty_message)newnullis_nullbuffer_from_bytesbytes_from_bufferderef   )	libcryptolibcrypto_legacy_supportLibcryptoConsthandle_openssl_error)
rand_bytes)	type_namebyte_cls)aes_cbc_no_padding_decryptaes_cbc_no_padding_encryptaes_cbc_pkcs7_decryptaes_cbc_pkcs7_encryptdes_cbc_pkcs5_decryptdes_cbc_pkcs5_encryptrc2_cbc_pkcs5_decryptrc2_cbc_pkcs5_encryptrc4_decryptrc4_encrypttripledes_cbc_pkcs5_decrypttripledes_cbc_pkcs5_encryptc                 h   t          |           }|st          d          }n=t          |          dk    r*t          t	          dt          |                              t          |          dz  dk    r*t          t	          dt          |                              |t          || ||d          fS )a  
    Encrypts plaintext using AES in CBC mode with a 128, 192 or 256 bit key and
    no padding. This means the ciphertext must be an exact multiple of 16 bytes
    long.

    :param key:
        The encryption key - a byte string either 16, 24 or 32 bytes long

    :param data:
        The plaintext - a byte string

    :param iv:
        The initialization vector - either a byte string 16-bytes long or None
        to generate an IV

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by OpenSSL

    :return:
        A tuple of two byte strings (iv, ciphertext)
       :
            iv must be 16 bytes long - is %s
            r   zJ
            data must be a multiple of 16 bytes long - is %s
            F_calculate_aes_cipherr   len
ValueErrorr   _encryptkeydataivciphers       W/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/oscrypto/_openssl/symmetric.pyr   r      s    2 #3''F ^^	RB GG	
 
   	 4yy2~ II	
 
   	 dB6677    c                     t          |           }t          |          dk    r*t          t          dt          |                              t	          || ||d          S )aM  
    Decrypts AES ciphertext in CBC mode using a 128, 192 or 256 bit key and no
    padding.

    :param key:
        The encryption key - a byte string either 16, 24 or 32 bytes long

    :param data:
        The ciphertext - a byte string

    :param iv:
        The initialization vector - a byte string 16-bytes long

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by OpenSSL

    :return:
        A byte string of the plaintext
    r$   r%   Fr'   r(   r)   r   _decryptr+   s       r0   r   r   M   sf    . #3''F
2ww"}} GG	
 
   	 FCr5111r1   c                     t          |           }|st          d          }n=t          |          dk    r*t          t	          dt          |                              |t          || ||d          fS )a  
    Encrypts plaintext using AES in CBC mode with a 128, 192 or 256 bit key and
    PKCS#7 padding.

    :param key:
        The encryption key - a byte string either 16, 24 or 32 bytes long

    :param data:
        The plaintext - a byte string

    :param iv:
        The initialization vector - either a byte string 16-bytes long or None
        to generate an IV

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by OpenSSL

    :return:
        A tuple of two byte strings (iv, ciphertext)
    r$   r%   Tr&   r+   s       r0   r   r   q   s    0 #3''F ^^	RB GG	
 
   	 dB5566r1   c                     t          |           }t          |          dk    r*t          t          dt          |                              t	          || ||d          S )a9  
    Decrypts AES ciphertext in CBC mode using a 128, 192 or 256 bit key

    :param key:
        The encryption key - a byte string either 16, 24 or 32 bytes long

    :param data:
        The ciphertext - a byte string

    :param iv:
        The initialization vector - a byte string 16-bytes long

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by OpenSSL

    :return:
        A byte string of the plaintext
    r$   r%   Tr3   r+   s       r0   r   r      sf    , #3''F
2ww"}} GG	
 
   	 FCr4000r1   c                     t          |           dvr*t          t          dt          |                               t          |           dk    rd}n+t          |           dk    rd}nt          |           dk    rd}|S )	a  
    Determines if the key is a valid AES 128, 192 or 256 key

    :param key:
        A byte string of the key to use

    :raises:
        ValueError - when an invalid key is provided

    :return:
        A unicode string of the AES variation - "aes128", "aes192" or "aes256"
    )r$          zo
            key must be either 16, 24 or 32 bytes (128, 192 or 256 bits)
            long - is %s
            r$   aes128r8   aes192r9   aes256)r(   r)   r   )r,   r/   s     r0   r'   r'      s     3xx|## HH
 
   	 3xx2~~	SR	SRMr1   c                     t           st          d          t          |           dk     st          |           dk    r*t          t	          dt          |                               t          d| |dd          S )a  
    Encrypts plaintext using RC4 with a 40-128 bit key

    :param key:
        The encryption key - a byte string 5-16 bytes long

    :param data:
        The plaintext - a byte string

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by OpenSSL

    :return:
        A byte string of the ciphertext
    -OpenSSL has been compiled without RC4 support   r$   Q
            key must be 5 to 16 bytes (40 to 128 bits) long - is %s
            rc4N)r   EnvironmentErrorr(   r)   r   r*   r,   r-   s     r0   r    r           & $ PNOOO
3xx!||s3xx"}} HH	
 
   	 E3dD111r1   c                     t           st          d          t          |           dk     st          |           dk    r*t          t	          dt          |                               t          d| |dd          S )a  
    Decrypts RC4 ciphertext using a 40-128 bit key

    :param key:
        The encryption key - a byte string 5-16 bytes long

    :param data:
        The ciphertext - a byte string

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by OpenSSL

    :return:
        A byte string of the plaintext
    r>   r?   r$   r@   rA   Nr   rB   r(   r)   r   r4   rC   s     r0   r   r      rD   r1   c                    t           st          d          t          |           dk     st          |           dk    r*t          t	          dt          |                               |st          d          }n=t          |          dk    r*t          t	          dt          |                              |t          d| ||d          fS )	ah  
    Encrypts plaintext using RC2 in CBC mode with a 40-128 bit key and PKCS#5
    padding.

    :param key:
        The encryption key - a byte string 8 bytes long

    :param data:
        The plaintext - a byte string

    :param iv:
        The initialization vector - a byte string 8-bytes long or None
        to generate an IV

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by OpenSSL

    :return:
        A tuple of two byte strings (iv, ciphertext)
    -OpenSSL has been compiled without RC2 supportr?   r$   r@      9
            iv must be 8 bytes long - is %s
            rc2Tr   rB   r(   r)   r   r   r*   r,   r-   r.   s      r0   r   r     s    0 $ PNOOO
3xx!||s3xx"}} HH	
 
   	  ]]	RA GG	
 
   	 T2t4455r1   c                 n   t           st          d          t          |           dk     st          |           dk    r*t          t	          dt          |                               t          |          dk    r*t          t	          dt          |                              t          d| ||d          S )	a5  
    Decrypts RC2 ciphertext ib CBC mode using a 40-128 bit key and PKCS#5
    padding.

    :param key:
        The encryption key - a byte string 8 bytes long

    :param data:
        The ciphertext - a byte string

    :param iv:
        The initialization vector - a byte string 8 bytes long

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by OpenSSL

    :return:
        A byte string of the plaintext
    rH   r?   r$   r@   rI   rJ   rK   TrF   rM   s      r0   r   r   N  s    . $ PNOOO
3xx!||s3xx"}} HH	
 
   	 2ww!|| GG	
 
   	 E3b$///r1   c                    t          |           dk    r=t          |           dk    r*t          t          dt          |                               |st          d          }n=t          |          dk    r*t          t          dt          |                              d}t          |           dk    r| | dd         z   } d}|t	          || ||d	          fS )
a  
    Encrypts plaintext using 3DES in CBC mode using either the 2 or 3 key
    variant (16 or 24 byte long key) and PKCS#5 padding.

    :param key:
        The encryption key - a byte string 16 or 24 bytes long (2 or 3 key mode)

    :param data:
        The plaintext - a byte string

    :param iv:
        The initialization vector - a byte string 8-bytes long or None
        to generate an IV

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by OpenSSL

    :return:
        A tuple of two byte strings (iv, ciphertext)
    r$   r8   zT
            key must be 16 bytes (2 key) or 24 bytes (3 key) long - %s
            rI   z6
            iv must be 8 bytes long - %s
            tripledes_3keyr   tripledes_2keyT)r(   r)   r   r   r*   r+   s       r0   r"   r"   {  s    0 3xx2~~#c((b.. HH	
 
   	  ]]	RA GG	
 
   	 F
3xx2~~C!Hn!dB5566r1   c                    t          |           dk    r=t          |           dk    r*t          t          dt          |                               t          |          dk    r*t          t          dt          |                              d}t          |           dk    r| | dd         z   } d}t          || ||d	          S )
au  
    Decrypts 3DES ciphertext in CBC mode using either the 2 or 3 key variant
    (16 or 24 byte long key) and PKCS#5 padding.

    :param key:
        The encryption key - a byte string 16 or 24 bytes long (2 or 3 key mode)

    :param data:
        The ciphertext - a byte string

    :param iv:
        The initialization vector - a byte string 8-bytes long

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by OpenSSL

    :return:
        A byte string of the plaintext
    r$   r8   zW
            key must be 16 bytes (2 key) or 24 bytes (3 key) long - is %s
            rI   rJ   rP   r   rQ   T)r(   r)   r   r4   r+   s       r0   r!   r!     s    . 3xx2~~#c((b.. HH	
 
   	 2ww!|| GG	
 
   	 F
3xx2~~C!Hn!FCr4000r1   c                 p   t           st          d          t          |           dk    r*t          t	          dt          |                               |st          d          }n=t          |          dk    r*t          t	          dt          |                              |t          d| ||d          fS )a  
    Encrypts plaintext using DES in CBC mode with a 56 bit key and PKCS#5
    padding.

    :param key:
        The encryption key - a byte string 8 bytes long (includes error correction bits)

    :param data:
        The plaintext - a byte string

    :param iv:
        The initialization vector - a byte string 8-bytes long or None
        to generate an IV

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by OpenSSL

    :return:
        A tuple of two byte strings (iv, ciphertext)
    -OpenSSL has been compiled without DES supportrI   T
            key must be 8 bytes (56 bits + 8 parity bits) long - is %s
            rJ   desTrL   rM   s      r0   r   r     s    0 $ PNOOO
3xx1}} HH	
 
   	  ]]	RA GG	
 
   	 T2t4455r1   c                 H   t           st          d          t          |           dk    r*t          t	          dt          |                               t          |          dk    r*t          t	          dt          |                              t          d| ||d          S )aN  
    Decrypts DES ciphertext in CBC mode using a 56 bit key and PKCS#5 padding.

    :param key:
        The encryption key - a byte string 8 bytes long (includes error correction bits)

    :param data:
        The ciphertext - a byte string

    :param iv:
        The initialization vector - a byte string 8-bytes long

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by OpenSSL

    :return:
        A byte string of the plaintext
    rT   rI   rU   rJ   rV   TrF   rM   s      r0   r   r     s    , $ PNOOO
3xx1}} HH	
 
   	 2ww!|| GG	
 
   	 E3b$///r1   c           	      F   t          |t                    s*t          t          dt	          |                              t          |t                    s*t          t          dt	          |                              | dk    r?t          |t                    s*t          t          dt	          |                              | dk    r>|s<| t          g d          v }|r|r%t          |          dz  dk    rt          d          d	}	 t          j	                    }t          |          rt          d           t          | |          \  }}|t                      }| t          d
dg          v rt          j        ||t                      t                      t                                }	t          |	           t          j        |t          |                    }	t          |	           | d
k    rLt          j        |t"          j        t          |          dz  t                                }	t          |	           t                      }t          j        ||t                      ||          }	t          |	           |1t          j        |t)          |                    }	t          |	           t+          |          }
t-          t          d          }t          j        ||
||t          |                    }	t          |	           t1          |
t3          |                    }t          j        ||
|          }	t          |	           |t1          |
t3          |                    z  }||rt          j        |           S S # |rt          j        |           w w xY w)a  
    Encrypts plaintext

    :param cipher:
        A unicode string of "aes128", "aes192", "aes256", "des",
        "tripledes_2key", "tripledes_3key", "rc2", "rc4"

    :param key:
        The encryption key - a byte string 5-32 bytes long

    :param data:
        The plaintext - a byte string

    :param iv:
        The initialization vector - a byte string - unused for RC4

    :param padding:
        Boolean, if padding should be used - unused for RC4

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by OpenSSL

    :return:
        A byte string of the ciphertext
    ;
            key must be a byte string, not %s
            <
            data must be a byte string, not %s
            rA   :
            iv must be a byte string, not %s
            )r:   r;   r<   r$   r   padding must be specifiedNrK   rI   int *)
isinstancer   	TypeErrorr   r   setr(   r)   r   EVP_CIPHER_CTX_newr   r   _setup_evp_encrypt_decryptr
   EVP_EncryptInit_exEVP_CIPHER_CTX_set_key_lengthEVP_CIPHER_CTX_ctrlr   EVP_CTRL_SET_RC2_KEY_BITSEVP_CIPHER_CTX_set_paddingintr   r	   EVP_EncryptUpdater   r   EVP_EncryptFinal_exEVP_CIPHER_CTX_free)r/   r,   r-   r.   paddingis_aesevp_cipher_ctx
evp_cipherbuffer_sizeresbufferoutput_lengthoutputs                r0   r*   r*   :  s   : c8$$  cNN	
 
   	 dH%%  dOO	
 
   	 z"h77 bMM	
 
   	 w 3===>>> 	:& 	:c$ii"n%:%:8999N1:"577>"" 	$ ###"<VT"J"J
K:BS%((((.~z466SWSYSY[_[a[abbC %%%9.#c((SSC %%%3""<HHqLFF	  %S)))J*>:tvvsTVWWS!!!6~s7||TTC %%%";//Iw//).&-QUWZ[_W`W`aaS!!!"65+?+?@@+NFMRRS!!!#FE-,@,@AAA  	:).9999	:> 	:).9999	:s   I$N N c           	         t          |t                    s*t          t          dt	          |                              t          |t                    s*t          t          dt	          |                              | dk    r?t          |t                    s*t          t          dt	          |                              | t          g d          vr|st          d          d}	 t          j                    }t          |          rt          d           t          | |          \  }}|t                      }| t          d	dg          v rt          j        ||t                      t                      t                                }t          |           t          j        |t          |                    }t          |           | d	k    rLt          j        |t"          j        t          |          d
z  t                                }t          |           t                      }t          j        ||t                      ||          }t          |           |1t          j        |t)          |                    }t          |           t+          |          }	t-          t          d          }
t          j        ||	|
|t          |                    }t          |           t1          |	t3          |
                    }t          j        ||	|
          }t          |           |t1          |	t3          |
                    z  }||rt          j        |           S S # |rt          j        |           w w xY w)a  
    Decrypts AES/RC4/RC2/3DES/DES ciphertext

    :param cipher:
        A unicode string of "aes128", "aes192", "aes256", "des",
        "tripledes_2key", "tripledes_3key", "rc2", "rc4"

    :param key:
        The encryption key - a byte string 5-32 bytes long

    :param data:
        The ciphertext - a byte string

    :param iv:
        The initialization vector - a byte string - unused for RC4

    :param padding:
        Boolean, if padding should be used - unused for RC4

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by OpenSSL

    :return:
        A byte string of the plaintext
    rY   rZ   rA   r[   )rA   r:   r;   r<   r\   Nr   rK   rI   r]   )r^   r   r_   r   r   r`   r)   r   ra   r   r   rb   r
   EVP_DecryptInit_exrd   r(   re   r   rf   rg   rh   r   r	   EVP_DecryptUpdater   r   EVP_DecryptFinal_exrk   )r/   r,   r-   r.   rl   rn   ro   rp   rq   rr   rs   rt   s               r0   r4   r4     s_   : c8$$  cNN	
 
   	 dH%%  dOO	
 
   	 z"h77 bMM	
 
   	 S>>>?????4555N1:"577>"" 	$ ###"<VT"J"J
K:BS%((((.~z466SWSYSY[_[a[abbC %%%9.#c((SSC %%%3""<HHqLFF	  %S)))J*>:tvvsTVWWS!!!6~s7||TTC %%%";//Iw//).&-QUWZ[_W`W`aaS!!!"65+?+?@@+NFMRRS!!!#FE-,@,@AAA  	:).9999	:> 	:).9999	:s   +I$M' 'N c           
         	 t          j        t           j        t           j        t           j        t           j        t           j        t           j        t           j        d|                      }| dk    rt          |          }nDdddddddd|          }|t          t          j        t          |          |z                      z  }||fS )a  
    Creates an EVP_CIPHER pointer object and determines the buffer size
    necessary for the parameter specified.

    :param evp_cipher_ctx:
        An EVP_CIPHER_CTX pointer

    :param cipher:
        A unicode string of "aes128", "aes192", "aes256", "des",
        "tripledes_2key", "tripledes_3key", "rc2", "rc4"

    :param key:
        The key byte string

    :param data:
        The plaintext or ciphertext as a byte string

    :param padding:
        If padding is to be used

    :return:
        A 2-element tuple with the first element being an EVP_CIPHER pointer
        and the second being an integer that is the required buffer size
    )r:   r;   r<   rK   rA   rV   rQ   rP   rA   r$   rI   )r:   r;   r<   rK   rV   rQ   rP   )r   EVP_aes_128_cbcEVP_aes_192_cbcEVP_aes_256_cbcEVP_rc2_cbcEVP_rc4EVP_des_cbcEVP_des_ede_cbcEVP_des_ede3_cbcr(   rh   mathceil)r/   r-   ro   rp   
block_sizes        r0   rb   rb     s    4	+++$ $#3#4	 	 	 	 	J $ii 
 
 
 !3tyTZ1G'H'H#I#II$$r1   )*
__future__r   r   r   r   r   _errorsr   _ffir	   r
   r   r   r   r   
_libcryptor   r   r   r   utilr   _typesr   r   __all__r   r   r   r   r'   r    r   r   r   r"   r!   r   r   r*   r4   rb    r1   r0   <module>r      s   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 R R R a a a a a a a a a a a a       ( ( ( ( ( ( ( (   -8 -8 -8`!2 !2 !2H$7 $7 $7N 1  1  1F  B2 2 2B2 2 2B-6 -6 -6`*0 *0 *0Z07 07 07f-1 -1 -1`-6 -6 -6`)0 )0 )0Xo: o: o:dk: k: k:\3% 3% 3% 3% 3%r1   