o
    vhx                     @   sp  d dl mZmZmZmZ ddlmZ ddlmZm	Z	m
Z
mZmZmZmZmZmZmZ ddlmZ ddlmZ ddlmZmZ e Zed	krTdd
lmZmZmZmZmZ nddl m!Z!m"Z"mZ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" Z0d#d$ Z1d%d& Z2d'd( Z3d)d* Z4d+d, Z5d-d. Z6d/d0 Z7d1d2 Z8d3d4 Z9d5S )6    )unicode_literalsdivisionabsolute_importprint_function   )pretty_message)
buffer_from_bytesbytes_from_bufferderefnewnullpointer_setstructstruct_bytesunwrapwrite_to_buffer   )
rand_bytes)backend)	type_namebyte_cls	winlegacy)advapi32Advapi32Consthandle_erroropen_context_handleclose_context_handle)bcryptBcryptConstr   open_alg_handleclose_alg_handle)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   s   t | dvrttdt | |std}nt |dkr%ttdt |t |d dkr6ttdt ||td| ||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 the OS crypto library

    :return:
        A tuple of two byte strings (iv, ciphertext)
              o
            key must be either 16, 24 or 32 bytes (128, 192 or 256 bits)
            long - is %s
            r.   :
            iv must be 16 bytes long - is %s
            r   zJ
            data must be a multiple of 16 bytes long - is %s
            aesFlen
ValueErrorr   r   _encryptkeydataiv r<   U/var/www/html/hyperkenya/venv/lib/python3.10/site-packages/oscrypto/_win/symmetric.pyr"   -   s$   
r"   c                 C   L   t | dvrttdt | t |dkrttdt |td| ||dS )a[  
    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 the OS crypto library

    :return:
        A byte string of the plaintext
    r-   r1   r.   r2   r3   Fr5   r6   r   _decryptr8   r<   r<   r=   r!   d   s   r!   c                 C   s^   t | dv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 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 the OS crypto library

    :return:
        A tuple of two byte strings (iv, ciphertext)
    r-   r1   r.   r2   r3   Tr4   r8   r<   r<   r=   r$      s   
r$   c                 C   r>   )aG  
    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 the OS crypto library

    :return:
        A byte string of the plaintext
    r-   r1   r.   r2   r3   Tr?   r8   r<   r<   r=   r#      s   r#   c                 C   :   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 the OS crypto library

    :return:
        A byte string of the ciphertext
       r.   Q
            key must be 5 to 16 bytes (40 to 128 bits) long - is %s
            rc4N)r5   r6   r   r7   r9   r:   r<   r<   r=   r*         r*   c                 C   rA   )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 the OS crypto library

    :return:
        A byte string of the plaintext
    rB   r.   rC   rD   Nr?   rE   r<   r<   r=   r)     rF   r)   c                 C   sj   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 )ab  
    Encrypts plaintext using RC2 with a 64 bit key

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

    :param data:
        The plaintext - a byte string

    :param iv:
        The 8-byte initialization vector to use - a byte string - set as None
        to generate an appropriate one

    :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 the OS crypto library

    :return:
        A tuple of two byte strings (iv, ciphertext)
    rB   r.   rC      9
            iv must be 8 bytes long - is %s
            rc2Tr4   r8   r<   r<   r=   r(   #  s   
r(   c                 C   sX   t | dk st | dkrttdt | t |dkr$ttdt |td| ||dS )a"  
    Decrypts RC2 ciphertext using a 64 bit key

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

    :param data:
        The ciphertext - a byte string

    :param iv:
        The initialization vector used for encryption - 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 the OS crypto library

    :return:
        A byte string of the plaintext
    rB   r.   rC   rG   rH   rI   Tr?   r8   r<   r<   r=   r'   O  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r5d}|t|| ||dfS )	a  
    Encrypts plaintext using 3DES in either 2 or 3 key mode

    :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 8-byte initialization vector to use - a byte string - set as None
        to generate an appropriate one

    :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 the OS crypto library

    :return:
        A tuple of two byte strings (iv, ciphertext)
    r.   r/   W
            key must be 16 bytes (2 key) or 24 bytes (3 key) long - is %s
            rG   rH   tripledes_3keytripledes_2keyTr4   r9   r:   r;   cipherr<   r<   r=   r,   x  s    
r,   c                 C   sl   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}t|| ||dS )	aC  
    Decrypts 3DES ciphertext in either 2 or 3 key mode

    :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 used for encryption - 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 the OS crypto library

    :return:
        A byte string of the plaintext
    r.   r/   rJ   rG   rH   rK   rL   Tr?   rM   r<   r<   r=   r+     s   r+   c                 C   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 )a  
    Encrypts plaintext using DES with a 56 bit key

    :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 8-byte initialization vector to use - a byte string - set as None
        to generate an appropriate one

    :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 the OS crypto library

    :return:
        A tuple of two byte strings (iv, ciphertext)
    rG   T
            key must be 8 bytes (56 bits + 8 parity bits) long - is %s
            rH   desTr4   r8   r<   r<   r=   r&     s   
r&   c                 C   sL   t | dkrttdt | t |dkrttdt |td| ||dS )aK  
    Decrypts DES ciphertext using a 56 bit key

    :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 used for encryption - 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 the OS crypto library

    :return:
        A byte string of the plaintext
    rG   rO   rH   rP   Tr?   r8   r<   r<   r=   r%     s   r%   c                 C   s  d}| dkrt jt jt jdt| }nt jt jt jt jt j	d|  }t j
}t|dd}ttd}t|}t j|_t j|_d|_||_ttd	}t|}	||	_t||	_t|| }
d}| td
dgv rmt|dkrmt j}ttd}t||
t|
t ||}t| t|}| d
krttdt|d }t|t j|d}t| | dkrt|t j |d}t| ttdt j!}t|t j"|d}t| ttdt j#}t|t j$|d}t| ||fS )a  
    Creates an HCRYPTPROV and HCRYPTKEY for symmetric encryption/decryption. The
    HCRYPTPROV must be released by close_context_handle() and the
    HCRYPTKEY must be released by advapi32.CryptDestroyKey() when done.

    :param cipher:
        A unicode string of "aes", "des", "tripledes_2key", "tripledes_3key",
        "rc2", "rc4"

    :param key:
        A byte string of the symmetric key

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

    :return:
        A tuple of (HCRYPTPROV, HCRYPTKEY)
    Nr3   r-   )rP   rL   rK   rI   rD   F)verify_only
BLOBHEADERr   PLAINTEXTKEYBLOBrI   rD   rB   zHCRYPTKEY *DWORD *rG   )%r   CALG_AES_128CALG_AES_192CALG_AES_256r5   CALG_DESCALG_3DES_112	CALG_3DESCALG_RC2CALG_RC4MS_ENH_RSA_AES_PROVr   r   r   r   rS   bTypeCUR_BLOB_VERSIONbVersionreservedaiKeyAlghdr	dwKeySizer   setCRYPT_NO_SALTr   CryptImportKeyr   r   CryptSetKeyParamKP_EFFECTIVE_KEYLENKP_IVCRYPT_MODE_CBCKP_MODEPKCS5_PADDING
KP_PADDING)rN   r9   r;   context_handlealgorithm_idproviderblob_header_pointerblob_headerblob_struct_pointerblob_structblobflagskey_handle_pointerres
key_handlebufr<   r<   r=   _advapi32_create_handles,  s   



r|   c                 C   s  d}t jt jt jt jt jt jd|  }zet|}t j}t	t
d}t|}t j|_t j|_t||_t|| }| dkrStt
dt|d }t
|t j|dd}	t|	 tt
d	}
t
|t ||
t d|t|d	}	t|	 t|
W |rxt| S S |rt| w w )
ao  
    Creates a BCRYPT_KEY_HANDLE for symmetric encryption/decryption. The
    handle must be released by bcrypt.BCryptDestroyKey() when done.

    :param cipher:
        A unicode string of "aes", "des", "tripledes_2key", "tripledes_3key",
        "rc2", "rc4"

    :param key:
        A byte string of the symmetric key

    :return:
        A BCRYPT_KEY_HANDLE
    N)r3   rP   rL   rK   rI   rD   BCRYPT_KEY_DATA_BLOB_HEADERrI   rT   rG      r   zBCRYPT_KEY_HANDLE *)r   BCRYPT_AES_ALGORITHMBCRYPT_DES_ALGORITHMBCRYPT_3DES_112_ALGORITHMBCRYPT_3DES_ALGORITHMBCRYPT_RC2_ALGORITHMBCRYPT_RC4_ALGORITHMr   BCRYPT_KEY_DATA_BLOBr   r   r   BCRYPT_KEY_DATA_BLOB_MAGICdwMagicBCRYPT_KEY_DATA_BLOB_VERSION1	dwVersionr5   	cbKeyDatar   r   BCryptSetPropertyBCRYPT_EFFECTIVE_KEY_LENGTHr   BCryptImportKeyr   r    )rN   r9   
alg_handlealg_constant	blob_typert   ru   rv   r{   ry   rx   r<   r<   r=   _bcrypt_create_key_handle  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rD|sD| dkr@t|d dksDtdtd	krPt| ||||S t	| ||||S )
a  
    Encrypts plaintext

    :param cipher:
        A unicode string of "aes", "des", "tripledes_2key", "tripledes_3key",
        "rc2", "rc4"

    :param key:
        The encryption key - a byte string 5-16 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 the OS crypto library

    :return:
        A byte string of the ciphertext
    ;
            key must be a byte string, not %s
            <
            data must be a byte string, not %s
            rD   :
            iv must be a byte string, not %s
            r3   r.   r   padding must be specifiedr   )

isinstancer   	TypeErrorr   r   r5   r6   _backend_advapi32_encrypt_bcrypt_encryptrN   r9   r:   r;   paddingr<   r<   r=   r7     s*   

r7   c              	   C   s  d}d}zst | ||\}}ttdt|}t|t ddt |d}t| t|}	t|	}
t	|
| t
|t| t|t dd|
||	}t| t|
t|}| dkrg|sgt|t|d krg|dd }|W |rpt| |rwt| S S |rt| |rt| w w )a  
    Encrypts plaintext via CryptoAPI

    :param cipher:
        A unicode string of "aes", "des", "tripledes_2key", "tripledes_3key",
        "rc2", "rc4"

    :param key:
        The encryption key - a byte string 5-16 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 the OS crypto library

    :return:
        A byte string of the ciphertext
    NrT   Tr   r3   r.   i)r|   r   r   r5   CryptEncryptr   r   r
   r   r   r   r	   CryptDestroyKeyr   )rN   r9   r:   r;   r   ro   rz   out_lenry   
buffer_lenbufferoutputr<   r<   r=   r   "  sV   	
	 



r   c                 C      d}zkt | |}|du rd}nt|}d}|du rtj}ttd}t||t|t t dt d||
}	t|	 t	|}
t
|
}|rFt
|nt }t||t|t ||||
||
}	t|	 t|t	|W |rmt| S S |rvt| w w )a  
    Encrypts plaintext via CNG

    :param cipher:
        A unicode string of "aes", "des", "tripledes_2key", "tripledes_3key",
        "rc2", "rc4"

    :param key:
        The encryption key - a byte string 5-16 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 the OS crypto library

    :return:
        A byte string of the ciphertext
    Nr   TULONG *)r   r5   r   BCRYPT_BLOCK_PADDINGr   r   BCryptEncryptr   r   r
   r   r	   BCryptDestroyKeyrN   r9   r:   r;   r   rz   iv_lenrw   r   ry   r   r   	iv_bufferr<   r<   r=   r   q  Z   

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ddgvr<|s<tdtdkrHt| ||||S t	| ||||S )a  
    Decrypts AES/RC4/RC2/3DES/DES ciphertext

    :param cipher:
        A unicode string of "aes", "des", "tripledes_2key", "tripledes_3key",
        "rc2", "rc4"

    :param key:
        The encryption key - a byte string 5-16 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 the OS crypto library

    :return:
        A byte string of the plaintext
    r   r   rD   r   r3   r   r   )
r   r   r   r   r   re   r6   r   _advapi32_decrypt_bcrypt_decryptr   r<   r<   r=   r@     s(   

r@   c           
   	   C   s   d}d}zSt | ||\}}| dkr|st|d dkrtdt|}ttdt|}t|t | dkr8|s8dndd||}	t|	 t	|t
|W |rPt| |rWt| S S |r_t| |rft| w w )	a  
    Decrypts AES/RC4/RC2/3DES/DES ciphertext via CryptoAPI

    :param cipher:
        A unicode string of "aes", "des", "tripledes_2key", "tripledes_3key",
        "rc2", "rc4"

    :param key:
        The encryption key - a byte string 5-16 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 the OS crypto library

    :return:
        A byte string of the plaintext
    Nr3   r.   r   z9Invalid data - ciphertext length must be a multiple of 16rT   FT)r|   r5   r6   r   r   r   CryptDecryptr   r   r	   r
   r   r   )
rN   r9   r:   r;   r   ro   rz   r   r   ry   r<   r<   r=   r     s8   




r   c                 C   r   )a  
    Decrypts AES/RC4/RC2/3DES/DES ciphertext via CNG

    :param cipher:
        A unicode string of "aes", "des", "tripledes_2key", "tripledes_3key",
        "rc2", "rc4"

    :param key:
        The encryption key - a byte string 5-16 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 the OS crypto library

    :return:
        A byte string of the plaintext
    Nr   Tr   )r   r5   r   r   r   r   BCryptDecryptr   r   r
   r   r	   r   r   r<   r<   r=   r   >  r   r   N):
__future__r   r   r   r   _errorsr   _ffir   r	   r
   r   r   r   r   r   r   r   utilr    r   _typesr   r   r   	_advapi32r   r   r   r   r   _cngr   r   r   r    __all__r"   r!   r$   r#   r*   r)   r(   r'   r,   r+   r&   r%   r|   r   r7   r   r   r@   r   r   r<   r<   r<   r=   <module>   s>   07+.*,)0--*oG@OS==