HEX
Server: Apache
System: Linux 185.122.168.184.host.secureserver.net 5.14.0-570.60.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Nov 5 05:00:59 EST 2025 x86_64
User: barbeatleanalyti (1024)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //var/opt/nydus/ops/oscrypto/_openssl/__pycache__/symmetric.cpython-39.pyc
a

�,�h�[�@sddlmZmZmZmZddlZddlmZddlm	Z	m
Z
mZmZm
Z
mZddlmZmZmZmZddlmZdd	lmZmZgd
�Zdd�Zd
d�Zdd�Zdd�Zdd�Zdd�Zdd�Z dd�Z!dd�Z"dd�Z#dd �Z$d!d"�Z%d#d$�Z&d%d&�Z'd'd(�Z(d)d*�Z)dS)+�)�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_encryptcCsjt|�}|std�}nt|�dkr4ttdt|����t|�ddkrVttdt|����|t||||d�fS)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
            rzJ
            data must be a multiple of 16 bytes long - is %s
            F��_calculate_aes_cipherr�len�
ValueErrorr�_encrypt��key�data�iv�cipher�r.�L/opt/nydus/tmp/pip-target-wkfpz8uv/lib/python/oscrypto/_openssl/symmetric.pyrs
��rcCs6t|�}t|�dkr&ttdt|����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/rMs�rcCsHt|�}|std�}nt|�dkr4ttdt|����|t||||d�fS)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/rqs
�rcCs6t|�}t|�dkr&ttdt|����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#Tr0r)r.r.r/r�s�rcCsVt|�dvrttdt|����t|�dkr0d}n"t|�dkrBd}nt|�dkrRd}|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"�aes128r2�aes192r3�aes256)r&r'r)r*r-r.r.r/r%�s�r%cCsFtstd��t|�dks$t|�dkr6ttdt|����td||dd�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'rr(�r*r+r.r.r/r�s�rcCsFtstd��t|�dks$t|�dkr6ttdt|����td||dd�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
    r7r8r"r9r:N�rr;r&r'rr1r<r.r.r/r�s�rcCsvtstd��t|�dks$t|�dkr6ttdt|����|sDtd�}nt|�dkrbttdt|����|td|||d�fS)	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 supportr8r"r9��9
            iv must be 8 bytes long - is %s
            �rc2T�rr;r&r'rrr(�r*r+r,r.r.r/rs�
�rcCsdtstd��t|�dks$t|�dkr6ttdt|����t|�dkrTttdt|����td|||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
    r>r8r"r9r?r@rATr=rCr.r.r/rNs��rcCs�t|�dkr*t|�dkr*ttdt|����|s8td�}nt|�dkrVttdt|����d}t|�dkrz||dd�}d}|t||||d	�fS)
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"r2zT
            key must be 16 bytes (2 key) or 24 bytes (3 key) long - %s
            r?z6
            iv must be 8 bytes long - %s
            �tripledes_3keyr�tripledes_2keyT)r&r'rrr(r)r.r.r/r!{s"�
�r!cCs|t|�dkr*t|�dkr*ttdt|����t|�dkrHttdt|����d}t|�dkrl||dd�}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"r2zW
            key must be 16 bytes (2 key) or 24 bytes (3 key) long - is %s
            r?r@rDrrET)r&r'rr1r)r.r.r/r �s��r cCsjtstd��t|�dkr*ttdt|����|s8td�}nt|�dkrVttdt|����|td|||d�fS)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 supportr?�T
            key must be 8 bytes (56 bits + 8 parity bits) long - is %s
            r@�desTrBrCr.r.r/r�s�
�rcCsXtstd��t|�dkr*ttdt|����t|�dkrHttdt|����td|||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
    rFr?rGr@rHTr=rCr.r.r/rs��rc
	Cs$t|t�sttdt|����t|t�s8ttdt|����|dkr\t|t�s\ttdt|����|dkr�|s�|tgd��v}|r�|r�t|�ddkr�td��d	}�znt�	�}t
|�r�td�t||�\}}|d	ur�t
�}|td
dg�v�rRt�||t
�t
�t
��}	t|	�t�|t|��}	t|	�|d
k�rLt�|tjt|�dt
��}	t|	�t
�}t�||t
�||�}	t|	�|d	u�r�t�|t|��}	t|	�t|�}
ttd�}t�||
||t|��}	t|	�t|
t|��}t�||
|�}	t|	�|t|
t|��7}|W|�r
t�|�Sn|�rt�|�0d	S)
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
            )r4r5r6r"r�padding must be specifiedNrAr?�int *)�
isinstancer�	TypeErrorrr�setr&r'r�EVP_CIPHER_CTX_newr
r�_setup_evp_encrypt_decryptr	ZEVP_EncryptInit_ex�EVP_CIPHER_CTX_set_key_length�EVP_CIPHER_CTX_ctrlr�EVP_CTRL_SET_RC2_KEY_BITS�EVP_CIPHER_CTX_set_padding�intrrZEVP_EncryptUpdaterr
ZEVP_EncryptFinal_ex�EVP_CIPHER_CTX_free)
r-r*r+r,�paddingZis_aes�evp_cipher_ctx�
evp_cipher�buffer_size�res�buffer�
output_length�outputr.r.r/r(:s|
�
��

�


��r(c	Cst|t�sttdt|����t|t�s8ttdt|����|dkr\t|t�s\ttdt|����|tgd��vrx|sxtd��d}�znt��}t	|�r�t
d�t||�\}}|dur�t�}|td	dg�v�r2t�
||t�t�t��}t
|�t�|t|��}t
|�|d	k�r,t�|tjt|�d
t��}t
|�t�}t�
||t�||�}t
|�|du�rpt�|t|��}t
|�t|�}	ttd�}
t�||	|
|t|��}t
|�t|	t|
��}t�||	|
�}t
|�|t|	t|
��7}|W|�r�t�|�Sn|�r�t�|�0dS)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
    rIrJr:rK)r:r4r5r6rLNrrAr?rM)rNrrOrrrPr'rrQr
rrRr	ZEVP_DecryptInit_exrSr&rTrrUrVrWrrZEVP_DecryptUpdaterr
ZEVP_DecryptFinal_exrX)r-r*r+r,rYrZr[r\r]r^r_r`r.r.r/r1�sx
�
��

�


��r1c	Csxtjtjtjtjtjtjtjtjd�|�}|dkr>t	|�}n2dddddddd�|}|t
t�t	|�|��}||fS)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
    )r4r5r6rAr:rHrErDr:r"r?)r4r5r6rArHrErD)
rZEVP_aes_128_cbcZEVP_aes_192_cbcZEVP_aes_256_cbcZEVP_rc2_cbcZEVP_rc4ZEVP_des_cbcZEVP_des_ede_cbcZEVP_des_ede3_cbcr&rW�math�ceil)r-r+r[r\�
block_sizer.r.r/rRs2�	�
��	rR)*�
__future__rrrrra�_errorsrZ_ffirr	r
rrr
Z
_libcryptorrrr�utilr�_typesrr�__all__rrrrr%rrrrr!r rrr(r1rRr.r.r.r/�<module>s. 0$'#!!!0-300,rn