o
    Œõ±j�T  ã                   @   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-   úŠ/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/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}
z§t 	tj
| fg¡}t |¡}t |¡}ttdƒ}t |||¡}	t|ƒ t |	|¡}
t|ƒ | tjkr¦t |
tjtƒ |¡ t|ƒ t |
tj||¡ t|ƒ t |¡}t |
tj||¡ t|ƒ t |
tj||¡ t|ƒ t |
|¡}t|ƒ t |¡W |rÉt |¡ |rÐt |¡ |r×t |¡ |rÞt |¡ |	råt |	¡ |
rít |
¡ S S |rõt |¡ |rüt |¡ |�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