
    BwgT                         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 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#y)    )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                    t        |       dvrt        t        dt        |                   |st        d      }n,t        |      dk7  rt        t        dt        |                  t        |      dz  dk7  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
    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ivs      N/var/www/horilla/myenv/lib/python3.12/site-packages/oscrypto/_mac/symmetric.pyr   r      s    2 3x|# H	
  	 ^	RB G	
  	 4y2~ I	
  	 44c4XE`E`abb    c                    t        |       dvrt        t        dt        |                   t        |      dk7  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+   s      r/   r   r   R   s    . 3x|# H	
  	 2w"} G	
  	 H//dB@[@[\\r0   c                 &   t        |       dvrt        t        dt        |                   |st        d      }n,t        |      dk7  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+   s      r/   r   r   |   s    0 3x|# H	
  	 ^	RB G	
  	 44c4XEaEabccr0   c                    t        |       dvrt        t        dt        |                   t        |      dk7  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   r2   r   r)   r4   r+   s      r/   r   r      s    , 3x|# H
  	 2w"} G	
  	 H//dB@\@\]]r0   c                     t        |       dk  st        |       dkD  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-   s     r/   r   r      V    & 3x!|s3x"} H	
  	 H//dD$GGr0   c                     t        |       dk  st        |       dkD  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
    r7   r!   r8   N)r&   r'   r   r2   r   r9   r:   s     r/   r   r      r;   r0   c                 D   t        |       dk  st        |       dkD  rt        t        dt        |                   |st        d      }n,t        |      dk7  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)
    r7   r!   r8      9
            iv must be 8 bytes long - is %s
            )r&   r'   r   r   r(   r   kSecAttrKeyTypeRC2kSecPaddingPKCS5Keyr+   s      r/   r   r     s    . 3x!|s3x"} H	
  	 ]	RA G	
  	 44c4XEaEabccr0   c                 $   t        |       dk  st        |       dkD  rt        t        dt        |                   t        |      dk7  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
    r7   r!   r8   r>   r?   )r&   r'   r   r2   r   r@   rA   r+   s      r/   r   r   ;  s    , 3x!|s3x"} H	
  	 2w!| G	
  	 H//dB@\@\]]r0   c                 p   t        |       dk7  r,t        |       dk7  rt        t        dt        |                   |st        d      }n,t        |      dk7  rt        t        dt        |                  t        |       dk(  r| | dd z   } |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
            r>   z6
            iv must be 8 bytes long - %s
            r   )r&   r'   r   r   r(   r   kSecAttrKeyType3DESrA   r+   s      r/   r   r   d  s    . 3x2~#c(b. H	
  	 ]	RA G	
  	 3x2~C!Hn55sD"hFbFbcddr0   c                 P   t        |       dk7  r,t        |       dk7  rt        t        dt        |                   t        |      dk7  rt        t        dt        |                  t        |       dk(  r| | dd z   } 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
            r>   r?   r   )r&   r'   r   r2   r   rD   rA   r+   s      r/   r   r     s    , 3x2~#c(b. H	
  	 2w!| G	
  	 3x2~C!HnH00#tRA]A]^^r0   c                 (   t        |       dk7  rt        t        dt        |                   |st        d      }n,t        |      dk7  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)
    r>   T
            key must be 8 bytes (56 bits + 8 parity bits) long - is %s
            r?   )r&   r'   r   r   r(   r   kSecAttrKeyTypeDESrA   r+   s      r/   r   r     s    . 3x1} H	
  	 ]	RA G	
  	 44c4XEaEabccr0   c                    t        |       dk7  rt        t        dt        |                   t        |      dk7  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
    r>   rG   r?   )r&   r'   r   r2   r   rH   rA   r+   s      r/   r   r     s    , 3x1} H	
  	 2w!| G	
  	 H//dB@\@\]]r0   c                 f   t        |t              st        t        dt	        |                  t        |t              st        t        dt	        |                  | t
        j                  k7  r.t        |t              st        t        dt	        |                  | t
        j                  k7  r|st        d      d}d}d}d}d}	d}
	 t        j                  t
        j                  | fg      }t        j                  |      }t        j                  |      }t        t        d      }t        j                  |||      }	t        |       t        j                   |	|      }
t        |       | t
        j                  k7  rt        j"                  |
t
        j$                  t'               |       t        |       t        j"                  |
t
        j(                  ||       t        |       t        j                  |      }t        j"                  |
t
        j*                  ||       t        |       t        j"                  |
t
        j,                  ||       t        |       t        j.                  |
|      }t        |       t        j0                  |      |rt        j2                  |       |rt        j2                  |       |rt        j2                  |       |rt        j2                  |       |	rt        j2                  |	       |
rt        j2                  |
       S S # |rt        j2                  |       |rt        j2                  |       |rt        j2                  |       |rt        j2                  |       |	rt        j2                  |	       |
rt        j2                  |
       w w xY 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   r9   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
ciphertexts                r/   r(   r(     s   8 c8$ cN	
  	 dH% dO	
  	 ,,,ZH5M bM	
  	 ,,,W455GFGEGM0444x7O7OQW6X5YZ--c2..t4NN;//O& ::7MR&X000--mX=T=TVZV\^klM*--mX=T=TV]_lmM*004E--mX=O=OQVXefM*))44		
 	&11-O
&))*5 $$W-$$V,$$W-$$U+$$W-$$]3  $$W-$$V,$$W-$$U+$$W-$$]3    GL# #BN0c                 f   t        |t              st        t        dt	        |                  t        |t              st        t        dt	        |                  | t
        j                  k7  r.t        |t              st        t        dt	        |                  | t
        j                  k7  r|st        d      d}d}d}d}d}	d}
	 t        j                  t
        j                  | fg      }t        j                  |      }t        j                  |      }t        t        d      }t        j                  |||      }	t        |       t        j                   |	|      }
t        |       | t
        j                  k7  rt        j"                  |
t
        j$                  t'               |       t        |       t        j"                  |
t
        j(                  ||       t        |       t        j                  |      }t        j"                  |
t
        j*                  ||       t        |       t        j"                  |
t
        j,                  ||       t        |       t        j.                  |
|      }t        |       t        j0                  |      |rt        j2                  |       |rt        j2                  |       |rt        j2                  |       |rt        j2                  |       |	rt        j2                  |	       |
rt        j2                  |
       S S # |rt        j2                  |       |rt        j2                  |       |rt        j2                  |       |rt        j2                  |       |	rt        j2                  |	       |
rt        j2                  |
       w w xY w)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
    rK   rL   rM   rN   NrO   )rP   r   rQ   r   r   r   r9   r'   r   rR   rS   rT   r	   r   rU   r   SecDecryptTransformCreaterW   rX   r
   rY   rZ   r[   r\   r]   r^   )r_   r,   r-   r.   r`   ra   rb   rc   rd   re   rf   rg   	plaintexts                r/   r2   r2     s   8 c8$ cN	
  	 dH% dO	
  	 ,,,ZH5M bM	
  	 ,,,W455GFGEGM0444x7O7OQW6X5YZ--c2..t4NN;//O& ::7MR&X000--mX=T=TVZV\^klM*--mX=T=TV]_lmM*004E--mX=O=OQVXefM*))44		
 	&00N	&)))4 $$W-$$V,$$W-$$U+$$W-$$]3  $$W-$$V,$$W-$$U+$$W-$$]3 ri   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(   r2    r0   r/   <module>rv      s    R R $  H H   ( 3cl']T*dZ'^TH<H<)dX&^R-e`*_Z)dX&^Rn4bn4r0   