
    Bwg[                         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)y)    )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                    t        |       }|st        d      }n,t        |      dk7  rt        t	        dt        |                  t        |      dz  dk7  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       R/var/www/horilla/myenv/lib/python3.12/site-packages/oscrypto/_openssl/symmetric.pyr   r      s    2 #3'F^	RB G	
  	 4y2~ I	
  	 dB677    c                     t        |       }t        |      dk7  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   sP    . #3'F
2w"} G	
  	 FCr511r1   c                     t        |       }|st        d      }n,t        |      dk7  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   sa    0 #3'F^	RB G	
  	 dB566r1   c                     t        |       }t        |      dk7  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      sP    , #3'F
2w"} G	
  	 FCr400r1   c                     t        |       dvrt        t        dt        |                   t        |       dk(  rd}|S t        |       dk(  rd}|S 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{     3x|# H
  	 3x2~ M 
SR M 
SRMr1   c                     t         st        d      t        |       dk  st        |       dkD  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       _    & $NOO
3x!|s3x"} H	
  	 E3dD11r1   c                     t         st        d      t        |       dk  st        |       dkD  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kD  rt        t	        dt        |                   |st        d      }n,t        |      dk7  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 $NOO
3x!|s3x"} H	
  	 ]	RA G	
  	 T2t455r1   c                    t         st        d      t        |       dk  st        |       dkD  rt        t	        dt        |                   t        |      dk7  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    . $NOO
3x!|s3x"} H	
  	 2w!| G	
  	 E3b$//r1   c                 @   t        |       dk7  r,t        |       dk7  rt        t        dt        |                   |st        d      }n,t        |      dk7  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 3x2~#c(b. H	
  	 ]	RA G	
  	 F
3x2~C!Hn!dB566r1   c                     t        |       dk7  r,t        |       dk7  rt        t        dt        |                   t        |      dk7  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    . 3x2~#c(b. H	
  	 2w!| G	
  	 F
3x2~C!Hn!FCr400r1   c                    t         st        d      t        |       dk7  rt        t	        dt        |                   |st        d      }n,t        |      dk7  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 $NOO
3x1} H	
  	 ]	RA G	
  	 T2t455r1   c                     t         st        d      t        |       dk7  rt        t	        dt        |                   t        |      dk7  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    , $NOO
3x1} H	
  	 2w!| G	
  	 E3b$//r1   c           	         t        |t              st        t        dt	        |                  t        |t              st        t        dt	        |                  | dk7  r.t        |t              st        t        dt	        |                  | dk7  r1|s/| t        g d      v }|r|rt        |      dz  dk7  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(  rEt        j                   |t"        j$                  t        |      dz  t                     }	t        |	       t               }t        j                  ||t               ||      }	t        |	       |*t        j&                  |t)        |            }	t        |	       t+        |      }
t-        t        d      }t        j.                  ||
||t        |            }	t        |	       t1        |
t3        |            }t        j4                  ||
|      }	t        |	       |t1        |
t3        |            z  }||rt        j6                  |       S S # |rt        j6                  |       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$ cN	
  	 dH% dO	
  	 z"h7 bM	
  	 w 3=>>&c$i"n%:899N1:"557>" #"<VT"J
K:BS%((..~z46SWSY[_[abC %99.#c(SC %33""<<HqLF	 %S)J**>:tvsTVWS!66~s7|TC %";/Iw/)).&-QUWZ[_W`aS!"65+?@++NFMRS!#FE-,@AA )).9 >)).9 s   	G>K   K:c           	         t        |t              st        t        dt	        |                  t        |t              st        t        dt	        |                  | dk7  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(  rEt        j                   |t"        j$                  t        |      d
z  t                     }t        |       t               }t        j                  ||t               ||      }t        |       |*t        j&                  |t)        |            }t        |       t+        |      }	t-        t        d      }
t        j.                  ||	|
|t        |            }t        |       t1        |	t3        |
            }t        j4                  ||	|
      }t        |       |t1        |	t3        |
            z  }||rt        j6                  |       S S # |rt        j6                  |       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$ cN	
  	 dH% dO	
  	 z"h7 bM	
  	 S>??455N1:"557>" #"<VT"J
K:BS%((..~z46SWSY[_[abC %99.#c(SC %33""<<HqLF	 %S)J**>:tvsTVWS!66~s7|TC %";/Iw/)).&-QUWZ[_W`aS!"65+?@++NFMRS!#FE-,@AA )).9 >)).9 s   /G>K K c           
         	 t        j                  t         j                  t         j                  t         j                  t         j
                  t         j                  t         j                  t         j                  d|           }| dk(  rt        |      }||fS d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3#44	 	 	J $i $$ 
 
 !3tyyTZ1G'H#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 a a  ( -8`!2H$7N 1FB2B2B-6`*0Z07f-1`-6`)0Xo:dk:\3%r1   