o
    vh[                     @   s  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d Zdd Zdd Zdd Zdd Zdd Zdd Z dd Z!dd Z"dd Z#dd  Z$d!d" Z%d#d$ Z&d%d& Z'd'd( Z(d)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                 C   sj   t | }|std}nt|dkrttdt|t|d 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cipher r.   Y/var/www/html/hyperkenya/venv/lib/python3.10/site-packages/oscrypto/_openssl/symmetric.pyr      s   
r   c                 C   6   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)   r.   r.   r/   r   M   s   r   c                 C   sH   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)   r.   r.   r/   r   q   s   
r   c                 C   r0   )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#   Tr1   r)   r.   r.   r/   r      s   r   c                 C   sZ   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"   aes128r3   aes192r4   aes256)r&   r'   r   )r*   r-   r.   r.   r/   r%      s   r%   c                 C   F   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+   r.   r.   r/   r         r   c                 C   r8   )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
    r9   r:   r"   r;   r<   Nr   r=   r&   r'   r   r2   r>   r.   r.   r/   r      r?   r   c                 C   sv   t stdt| dk st| dkrttdt| |s"td}nt|dkr1t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   r=   r&   r'   r   r   r(   r*   r+   r,   r.   r.   r/   r     s   
r   c                 C   sd   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
    rA   r:   r"   r;   rB   rC   rD   Tr@   rF   r.   r.   r/   r   N  s   r   c                 C   s   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  } 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"   r3   zT
            key must be 16 bytes (2 key) or 24 bytes (3 key) long - %s
            rB   z6
            iv must be 8 bytes long - %s
            tripledes_3keyr   tripledes_2keyT)r&   r'   r   r   r(   r)   r.   r.   r/   r!   {  s"   
r!   c                 C   s|   t | dkrt | dkrttdt | t |dkr$ttdt |d}t | dkr6| | dd  } 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"   r3   zW
            key must be 16 bytes (2 key) or 24 bytes (3 key) long - is %s
            rB   rC   rG   r   rH   T)r&   r'   r   r2   r)   r.   r.   r/   r      s   r    c                 C   sj   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 supportrB   T
            key must be 8 bytes (56 bits + 8 parity bits) long - is %s
            rC   desTrE   rF   r.   r.   r/   r     s   
r   c                 C   sX   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
    rI   rB   rJ   rC   rK   Tr@   rF   r.   r.   r/   r     s   r   c              	   C   s  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rL|sL| tg dv }|rH|rLt|d dkrLtdd	}zt	 }t
|r[td t| |\}}|d	u rit }| td
dgv rt||t t t }	t|	 t|t|}	t|	 | d
krt|tjt|d t }	t|	 t }t||t ||}	t|	 |d	urt|t|}	t|	 t|}
ttd}t||
||t|}	t|	 t|
t|}t||
|}	t|	 |t|
t|7 }|W |rt| S S |rt| w 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
            r<   :
            iv must be a byte string, not %s
            )r5   r6   r7   r"   r   padding must be specifiedNrD   rB   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outputr.   r.   r/   r(   :  sx   



r(   c              	   C   s  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}zt }t	|rKt
d t| |\}}|du rYt }| td	dgv rt||t t t }t
| t|t|}t
| | d	krt|tjt|d
 t }t
| t }t||t ||}t
| |durt|t|}t
| t|}	ttd}
t||	|
|t|}t
| t|	t|
}t||	|
}t
| |t|	t|
7 }|W |rt| S S |rt| w 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
    rL   rM   r<   rN   )r<   r5   r6   r7   rO   Nr   rD   rB   rP   )rQ   r   rR   r   r   rS   r'   r   rT   r
   r   rU   r	   EVP_DecryptInit_exrW   r&   rX   r   rY   rZ   r[   r   r   EVP_DecryptUpdater   r   EVP_DecryptFinal_exr^   )r-   r*   r+   r,   r_   ra   rb   rc   rd   re   rf   rg   r.   r.   r/   r2     st   



r2   c              	   C   s~   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t	||  }||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
    )r5   r6   r7   rD   r<   rK   rH   rG   r<   r"   rB   )r5   r6   r7   rD   rK   rH   rG   )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&   r[   mathceil)r-   r+   rb   rc   
block_sizer.   r.   r/   rU     s4   		rU   )*
__future__r   r   r   r   rs   _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(   r2   rU   r.   r.   r.   r/   <module>   s0    0$'#!!!0-300,rn