
     hT                         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#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                    t          |           dvr*t          t          dt          |                               |st          d          }n=t          |          dk    r*t          t          dt          |                              t          |          dz  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
    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      S/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/oscrypto/_mac/symmetric.pyr   r      s    2 3xx|## HH	
 
   	  ^^	RB GG	
 
   	 4yy2~ II	
 
   	 4c4XE`aabb    c                 @   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+   s      r/   r   r   R   s    . 3xx|## HH	
 
   	 2ww"}} GG	
 
   	 H/dB@[\\\r0   c                 h   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+   s      r/   r   r   |   s    0 3xx|## HH	
 
   	  ^^	RB GG	
 
   	 4c4XEabbccr0   c                 @   t          |           dv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   r2   r   r)   r4   r+   s      r/   r   r      s    , 3xx|## HH
 
   	 2ww"}} GG	
 
   	 H/dB@\]]]r0   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-   s     r/   r   r      j    & 3xx!||s3xx"}} HH	
 
   	 H/dD$GGGr0   c                     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
    r7   r!   r8   N)r&   r'   r   r2   r   r9   r:   s     r/   r   r      r;   r0   c                    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)
    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    . 3xx!||s3xx"}} HH	
 
   	  ]]	RA GG	
 
   	 4c4XEabbccr0   c                 j   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
    r7   r!   r8   r>   r?   )r&   r'   r   r2   r   r@   rA   r+   s      r/   r   r   ;  s    , 3xx!||s3xx"}} HH	
 
   	 2ww!|| GG	
 
   	 H/dB@\]]]r0   c                    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    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    . 3xx2~~#c((b.. HH	
 
   	  ]]	RA GG	
 
   	 3xx2~~C!Hn5sD"hFbccddr0   c                    t          |           dk    r=t          |           dk    r*t          t          dt          |                               t          |          dk    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    , 3xx2~~#c((b.. HH	
 
   	 2ww!|| GG	
 
   	 3xx2~~C!HnH0#tRA]^^^r0   c                 l   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)
    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    . 3xx1}} HH	
 
   	  ]]	RA GG	
 
   	 4c4XEabbccr0   c                 D   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
    r>   rG   r?   )r&   r'   r   r2   r   rH   rA   r+   s      r/   r   r     s    , 3xx1}} HH	
 
   	 2ww!|| GG	
 
   	 H/dB@\]]]r0   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}
	 t          j	        t
          j
        | fg          }t          j        |          }t          j        |          }t          t          d          }t          j        |||          }	t          |           t          j        |	|          }
t          |           | t
          j        k    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          j        |          |rt          j        |           |rt          j        |           |rt          j        |           |rt          j        |           |	rt          j        |	           |
rt          j        |
           S S # |rt          j        |           |rt          j        |           |rt          j        |           |rt          j        |           |	rt          j        |	           |
rt          j        |
           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$$  cNN	
 
   	 dH%%  dOO	
 
   	 ,,,ZH5M5M, bMM	
 
   	 ,,,W,4555GFGEGM044x7OQW6X5YZZ-c22.t44NN;;/OO&&& :7MRR&&&X000-mX=TVZV\V\^klllM***-mX=TV]_lmmmM***044E-mX=OQVXefffM***)4		
 	
 	
 	&&&1-OO
&&&)*55  	.$W--- 	-$V,,, 	.$W--- 	,$U+++ 	.$W--- 	4$]3333	4  	.$W--- 	-$V,,, 	.$W--- 	,$U+++ 	.$W--- 	4$]3333	4   <GM BO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}
	 t          j	        t
          j
        | fg          }t          j        |          }t          j        |          }t          t          d          }t          j        |||          }	t          |           t          j        |	|          }
t          |           | t
          j        k    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          j        |          |rt          j        |           |rt          j        |           |rt          j        |           |rt          j        |           |	rt          j        |	           |
rt          j        |
           S S # |rt          j        |           |rt          j        |           |rt          j        |           |rt          j        |           |	rt          j        |	           |
rt          j        |
           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$$  cNN	
 
   	 dH%%  dOO	
 
   	 ,,,ZH5M5M, bMM	
 
   	 ,,,W,4555GFGEGM044x7OQW6X5YZZ-c22.t44NN;;/OO&&& :7MRR&&&X000-mX=TVZV\V\^klllM***-mX=TV]_lmmmM***044E-mX=OQVXefffM***)4		
 	
 	
 	&&&0NN	&&&))44  	.$W--- 	-$V,,, 	.$W--- 	,$U+++ 	.$W--- 	4$]3333	4  	.$W--- 	-$V,,, 	.$W--- 	,$U+++ 	.$W--- 	4$]3333	4ri   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 R R R R R R R R R R $ $ $ $ $ $         H H H H H H H H H H             ( ( ( ( ( ( ( (   3c 3c 3cl'] '] ']T*d *d *dZ'^ '^ '^TH H H<H H H<)d )d )dX&^ &^ &^R-e -e -e`*_ *_ *_Z)d )d )dX&^ &^ &^Rn4 n4 n4bn4 n4 n4 n4 n4r0   