o
    vhT                     @   s   d dl mZmZmZmZ ddlmZ ddlmZm	Z	 ddl
mZmZmZ ddl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'S )(    )unicode_literalsdivisionabsolute_importprint_function   )pretty_message)newnull   )CoreFoundation	CFHelpershandle_cf_error)Security)
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   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tj| ||tj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)
              c
            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
            )len
ValueErrorr   r   _encryptr   kSecAttrKeyTypeAESkSecPaddingNoneKeykeydataiv r-   U/var/www/html/hyperkenya/venv/lib/python3.10/site-packages/oscrypto/_mac/symmetric.pyr      s$   
r   c                 C   P   t | dvrttdt | t |dkrttdt |ttj| ||tj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   r"   r   r#   )r$   r%   r   _decryptr   r'   r(   r)   r-   r-   r.   r   R   s   r   c                 C   sb   t | dvrttdt | |std}nt |dkr%ttdt ||ttj| ||tj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   r"   r   r#   )r$   r%   r   r   r&   r   r'   kSecPaddingPKCS7Keyr)   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   zo
            key must be either 16, 24 or 32 bytes (128, 192 or 256 bits)
            long - is %s
            r   r#   )r$   r%   r   r0   r   r'   r1   r)   r-   r-   r.   r      s   r   c                 C   <   t | dk st | dkrttdt | ttj| |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
            N)r$   r%   r   r&   r   kSecAttrKeyTypeRC4r*   r+   r-   r-   r.   r         r   c                 C   r2   )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
    r3   r   r4   N)r$   r%   r   r0   r   r5   r6   r-   r-   r.   r      r7   r   c                 C   sn   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tj| ||tj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)
    r3   r   r4      9
            iv must be 8 bytes long - is %s
            )r$   r%   r   r   r&   r   kSecAttrKeyTypeRC2kSecPaddingPKCS5Keyr)   r-   r-   r.   r     s   
r   c                 C   s\   t | dk st | dkrttdt | t |dkr$ttdt |ttj| ||tj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
    r3   r   r4   r8   r9   )r$   r%   r   r0   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 | |std}nt |dkr+ttdt |t | dkr9| | dd  } |ttj| ||tj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    zT
            key must be 16 bytes (2 key) or 24 bytes (3 key) long - %s
            r8   z6
            iv must be 8 bytes long - %s
            r   )r$   r%   r   r   r&   r   kSecAttrKeyType3DESr;   r)   r-   r-   r.   r   d  s   
r   c                 C   sx   t | dkrt | dkrttdt | t |dkr$ttdt |t | dkr2| | dd  } ttj| ||tj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    zW
            key must be 16 bytes (2 key) or 24 bytes (3 key) long - is %s
            r8   r9   r   )r$   r%   r   r0   r   r<   r;   r)   r-   r-   r.   r     s   r   c                 C   sb   t | dkrttdt | |std}nt |dkr%ttdt ||ttj| ||tj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)
    r8   T
            key must be 8 bytes (56 bits + 8 parity bits) long - is %s
            r9   )r$   r%   r   r   r&   r   kSecAttrKeyTypeDESr;   r)   r-   r-   r.   r     s   
r   c                 C   sP   t | dkrttdt | t |dkrttdt |ttj| ||tjS )aC  
    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
    r8   r=   r9   )r$   r%   r   r0   r   r>   r;   r)   r-   r-   r.   r     s   r   c              	   C   <  t |tsttdt|t |tsttdt|| tjkr/t |ts/ttdt|| tjkr:|s:tdd}d}d}d}d}	d}
zt	tj
| fg}t|}t|}ttd}t|||}	t| t|	|}
t| | tjkrt|
tjt | t| t|
tj|| t| t|}t|
tj|| t| t|
tj|| t| t|
|}t| t|W |rt| |rt| |rt| |rt| |	rt|	 |
rt|
 S S |rt| |rt| |rt| |rt| |	rt|	 |
rt|
 w w )a  
    Encrypts plaintext

    :param cipher:
        A kSecAttrKeyType* value that specifies the cipher to use

    :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:
        The padding mode to use, specified as a kSecPadding*Key value - 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
            :
            iv must be a byte string, not %s
            padding must be specifiedNCFErrorRef *)
isinstancer   	TypeErrorr   r   r   r5   r%   r   cf_dictionary_from_pairskSecAttrKeyTypecf_data_from_bytesr   r   SecKeyCreateFromDatar   SecEncryptTransformCreateSecTransformSetAttributekSecModeCBCKeyr	   kSecPaddingKey	kSecIVKeykSecTransformInputAttributeNameSecTransformExecutecf_data_to_bytes	CFRelease)cipherr*   r+   r,   paddingcf_dictcf_keycf_datacf_ivsec_keysec_transformerror_pointer
ciphertextr-   r-   r.   r&        

















r&   c              	   C   r?   )a  
    Decrypts AES/RC4/RC2/3DES/DES ciphertext

    :param cipher:
        A kSecAttrKeyType* value that specifies the cipher to use

    :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:
        The padding mode to use, specified as a kSecPadding*Key value - 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@   rA   rB   rC   NrD   )rE   r   rF   r   r   r   r5   r%   r   rG   rH   rI   r   r   rJ   r   SecDecryptTransformCreaterL   rM   r	   rN   rO   rP   rQ   rR   rS   )rT   r*   r+   r,   rU   rV   rW   rX   rY   rZ   r[   r\   	plaintextr-   r-   r.   r0     r^   r0   N)$
__future__r   r   r   r   _errorsr   _ffir   r	   _core_foundationr   r   r   	_securityr   utilr   _typesr   r   __all__r   r   r   r   r   r   r   r   r   r   r   r   r&   r0   r-   r-   r-   r.   <module>   s,   6*-*,)0-,)q