U
    ;gT                     @   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 d
dddddddddddg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krttdt | |s,td}nt |dkrJttdt |t |d dkrlt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-   ;/tmp/pip-unpacked-wheel-x1gypflw/oscrypto/_mac/symmetric.pyr      s$    
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 )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    c                 C   sb   t | dkrttdt | |s,td}nt |dkrJt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    
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 )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   r/   r   r'   r0   r)   r-   r-   r.   r      s    c                 C   s<   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      s    c                 C   s<   t | dk st | dkr*ttdt | ttj| |dd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 the OS crypto library

    :return:
        A byte string of the plaintext
    r1   r   r2   N)r$   r%   r   r/   r   r3   r4   r-   r-   r.   r      s    c                 C   sn   t | dk st | dkr*ttdt | |s8td}nt |dkrVt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)
    r1   r   r2      9
            iv must be 8 bytes long - is %s
            )r$   r%   r   r   r&   r   kSecAttrKeyTypeRC2kSecPaddingPKCS5Keyr)   r-   r-   r.   r     s    
c                 C   s\   t | dk st | dkr*ttdt | t |dkrHt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
    r1   r   r2   r5   r6   )r$   r%   r   r/   r   r7   r8   r)   r-   r-   r.   r   ;  s    c                 C   s   t | dkr*t | dkr*ttdt | |s8td}nt |dkrVttdt |t | dkrr| | 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
            r5   z6
            iv must be 8 bytes long - %s
            r   )r$   r%   r   r   r&   r   kSecAttrKeyType3DESr8   r)   r-   r-   r.   r   d  s    
c                 C   sx   t | dkr*t | dkr*ttdt | t |dkrHttdt |t | dkrd| | 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
            r5   r6   r   )r$   r%   r   r/   r   r9   r8   r)   r-   r-   r.   r     s    c                 C   sb   t | dkrttdt | |s,td}nt |dkrJt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)
    r5   T
            key must be 8 bytes (56 bits + 8 parity bits) long - is %s
            r6   )r$   r%   r   r   r&   r   kSecAttrKeyTypeDESr8   r)   r-   r-   r.   r     s    
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
    r5   r:   r6   )r$   r%   r   r/   r   r;   r8   r)   r-   r-   r.   r     s    c              	   C   s  t |tsttdt|t |ts8ttdt|| tjkr^t |ts^ttdt|| tjkrt|sttdd}d}d}d}d}	d}
 zt
tj| fg}t
|}t
|}ttd}t|||}	t| t|	|}
t| | tjkrPt|
tjt | t| t|
tj|| t| t
|}t|
tj|| t| t|
tj|| t| t|
|}t| t
|W S |rt	| |rt	| |rt	| |rt	| |	rt	|	 |
rt	|
 X dS )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   r3   r%   r   	CFReleaser   cf_dictionary_from_pairskSecAttrKeyTypecf_data_from_bytesr   SecKeyCreateFromDatar   ZSecEncryptTransformCreateSecTransformSetAttributekSecModeCBCKeyr	   kSecPaddingKey	kSecIVKeykSecTransformInputAttributeNameSecTransformExecutecf_data_to_bytes)cipherr*   r+   r,   paddingcf_dictcf_keycf_datacf_ivsec_keysec_transformerror_pointer
ciphertextr-   r-   r.   r&     s|    










r&   c              	   C   s  t |tsttdt|t |ts8ttdt|| tjkr^t |ts^ttdt|| tjkrt|sttdd}d}d}d}d}	d}
 zt
tj| fg}t
|}t
|}ttd}t|||}	t| t|	|}
t| | tjkrPt|
tjt | t| t|
tj|| t| t
|}t|
tj|| t| t|
tj|| t| t|
|}t| t
|W S |rt	| |rt	| |rt	| |rt	| |	rt	|	 |
rt	|
 X dS )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<   r=   r>   r?   Nr@   )rA   r   rB   r   r   r   r3   r%   r   rC   r   rD   rE   rF   r   rG   r   ZSecDecryptTransformCreaterH   rI   r	   rJ   rK   rL   rM   rN   )rO   r*   r+   r,   rP   rQ   rR   rS   rT   rU   rV   rW   	plaintextr-   r-   r.   r/     s|    










r/   N)$
__future__r   r   r   r   _errorsr   _ffir   r	   Z_core_foundationr   r   r   Z	_securityr   utilr   _typesr   r   __all__r   r   r   r   r   r   r   r   r   r   r   r   r&   r/   r-   r-   r-   r.   <module>   sB   6*-*,)0-,)q