o
    Œõ±j˜[  ã                   @   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.   úŽ/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/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	}z´t 	¡ }t
|ƒr[tdƒ t| |ƒ\}}|d	u ritƒ }| td
dgƒv r¦t ||tƒ tƒ tƒ ¡}	t|	ƒ t |t|ƒ¡}	t|	ƒ | d
kr£t |tjt|ƒd tƒ ¡}	t|	ƒ tƒ }t ||tƒ ||¡}	t|	ƒ |d	urÄt |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}z³t ¡ }t	|ƒrKt
dƒ t| |ƒ\}}|du rYtƒ }| td	dgƒv r–t ||tƒ tƒ tƒ ¡}t
|ƒ t |t|ƒ¡}t
|ƒ | d	kr“t |tjt|ƒd
 tƒ ¡}t
|ƒ tƒ }t ||tƒ ||¡}t
|ƒ |dur´t |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ñ  
    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