o
    Œõ±j,'  ã                   @   s  d dl mZmZmZmZ d dlZddlmZ ddlm	Z	m
Z
mZmZ 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 g d¢ZdZddgZdd„ Zdd„ Zdd„ Zde_dd„ Z zddl!m"Z" dd„ Z#dd„ Z$W dS  ey€   ddl%m$Z$ Y dS w )é    )Úunicode_literalsÚdivisionÚabsolute_importÚprint_functionNé   )Úpretty_message)Úbuffer_from_bytesÚbytes_from_bufferÚerrnoÚbyte_string_from_buffer)Ú	type_nameÚstr_clsÚbyte_clsÚ	int_types)ÚLibraryNotFoundErroré   )ÚCommonCryptoÚCommonCryptoConst)ÚSecurity)Úpbkdf2Ú
pkcs12_kdfÚ
rand_bytesúutf-8Úcp1252c                 C   s`   zt | tƒW S  ty)   tD ]}zt | |dd�W    Y S  ty&   Y qw Y nw t | dd�S )NÚstrict)ÚerrorsÚreplace)r   Ú	_encodingÚUnicodeDecodeErrorÚ_fallback_encodings)ÚvalueÚencoding© r"   ú…/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/oscrypto/_mac/util.pyÚ_try_decode   s   ÿýÿr$   c                  C   sF   t ƒ } zt | ¡}W n ty   t| ƒ Y S w t|tƒr|S t|ƒS )z~
    Extracts the last OS error message into a python unicode string

    :return:
        A unicode string error message
    )r
   ÚosÚstrerrorÚ
ValueErrorr   Ú
isinstancer$   )Ú	error_numÚerror_stringr"   r"   r#   Ú_extract_error*   s   ÿ
r+   c                 C   s  t |tƒsttdt|ƒƒƒ‚t |tƒsttdt|ƒƒƒ‚t |tƒs*ttdt|ƒƒƒ‚|dk r2tdƒ‚t |tƒs@ttdt|ƒƒƒ‚|dk rHtdƒ‚| tg d¢ƒvrYttd	t| ƒƒƒ‚t	j
t	jt	jt	jt	jdœ|  }t|ƒ}t t	j|t|ƒ|t|ƒ||||¡	}|d
kr‡ttƒ ƒ‚t|ƒS )aç  
    PBKDF2 from PKCS#5

    :param hash_algorithm:
        The string name of the hash algorithm to use: "sha1", "sha224", "sha256", "sha384", "sha512"

    :param password:
        A byte string of the password to use an input to the KDF

    :param salt:
        A cryptographic random byte string

    :param iterations:
        The numbers of iterations to use when deriving the key

    :param key_length:
        The length of the desired key in bytes

    :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:
        The derived key as a byte string
    z@
            password must be a byte string, not %s
            z<
            salt must be a byte string, not %s
            z?
            iterations must be an integer, not %s
            r   z!iterations must be greater than 0z?
            key_length must be an integer, not %s
            z!key_length must be greater than 0)Úsha1Úsha224Úsha256Úsha384Úsha512zz
            hash_algorithm must be one of "sha1", "sha224", "sha256", "sha384",
            "sha512", not %s
            r   )r(   r   Ú	TypeErrorr   r   r   r'   ÚsetÚreprr   ÚkCCPRFHmacAlgSHA1ÚkCCPRFHmacAlgSHA224ÚkCCPRFHmacAlgSHA256ÚkCCPRFHmacAlgSHA384ÚkCCPRFHmacAlgSHA512r   r   ÚCCKeyDerivationPBKDFÚ	kCCPBKDF2ÚlenÚOSErrorr+   r	   )Úhash_algorithmÚpasswordÚsaltÚ
iterationsÚ
key_lengthÚalgoÚoutput_bufferÚresultr"   r"   r#   r   ?   sh   
ü
ü
ü
üû	ûú÷
r   Fc                 C   sn   t | tƒsttdt| ƒƒƒ‚| dk rtdƒ‚| dkrtdƒ‚t| ƒ}t tj	| |¡}|dkr3t
tƒ ƒ‚t|ƒS )a‹  
    Returns a number of random bytes suitable for cryptographic purposes

    :param length:
        The desired number of bytes

    :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
    z;
            length must be an integer, not %s
            r   zlength must be greater than 0i   z$length must not be greater than 1024r   )r(   r   r1   r   r   r'   r   r   ÚSecRandomCopyBytesÚkSecRandomDefaultr<   r+   r	   )ÚlengthÚbufferrD   r"   r"   r#   r   §   s   
ü
r   )Ú	libcryptoc                  C   s,   t  ¡ } tdƒ}t  | |¡ t|ƒ}t|ƒS )z“
        Extracts the last OpenSSL error message into a python unicode string

        :return:
            A unicode string error message
        éx   )rI   ÚERR_get_errorr   ÚERR_error_stringr   r$   )r)   rH   r*   r"   r"   r#   Ú_extract_openssl_errorÒ   s
   rM   c           
      C   sd  t |tƒsttdt|ƒƒƒ‚t |tƒsttdt|ƒƒƒ‚t |tƒs*ttdt|ƒƒƒ‚|dk r7ttdt|ƒƒƒ‚t |tƒsEttdt|ƒƒƒ‚|dk rRttdt|ƒƒƒ‚| tg d¢ƒvrcttd	t| ƒƒƒ‚|tg d
¢ƒvrtttdt|ƒƒƒ‚| 	d¡ 
d¡d }tjtjtjtjtjtjdœ|  ƒ }t|ƒ}t |t|ƒ|t|ƒ|||||¡	}	|	dkr®ttƒ ƒ‚t|ƒS )aÅ  
        KDF from RFC7292 appendix B.2 - https://tools.ietf.org/html/rfc7292#page-19

        :param hash_algorithm:
            The string name of the hash algorithm to use: "md5", "sha1", "sha224", "sha256", "sha384", "sha512"

        :param password:
            A byte string of the password to use an input to the KDF

        :param salt:
            A cryptographic random byte string

        :param iterations:
            The numbers of iterations to use when deriving the key

        :param key_length:
            The length of the desired key in bytes

        :param id_:
            The ID of the usage - 1 for key, 2 for iv, 3 for mac

        :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:
            The derived key as a byte string
        zH
                password must be a byte string, not %s
                zD
                salt must be a byte string, not %s
                zG
                iterations must be an integer, not %s
                r   zK
                iterations must be greater than 0 - is %s
                zG
                key_length must be an integer, not %s
                zK
                key_length must be greater than 0 - is %s
                )Úmd5r,   r-   r.   r/   r0   z�
                hash_algorithm must be one of "md5", "sha1", "sha224", "sha256",
                "sha384", "sha512", not %s
                )r   r   é   zD
                id_ must be one of 1, 2, 3, not %s
                r   zutf-16bes     )r(   r   r1   r   r   r   r'   r3   r2   ÚdecodeÚencoderI   ÚEVP_md5ÚEVP_sha1Ú
EVP_sha224Ú
EVP_sha256Ú
EVP_sha384Ú
EVP_sha512r   ÚPKCS12_key_gen_unir;   r<   rM   r	   )
r=   r>   r?   r@   rA   Úid_Úutf16_passwordÚdigest_typerC   rD   r"   r"   r#   r   ã   s‚   
ü
ü
üü
üüûüúù	÷
r   )r   )&Ú
__future__r   r   r   r   r%   Ú_errorsr   Ú_ffir   r	   r
   r   Ú_typesr   r   r   r   r   r   Ú_common_cryptor   r   Ú	_securityr   Ú__all__r   r   r$   r+   r   Úpure_pythonr   Ú_openssl._libcryptorI   rM   r   Ú_pkcs12r"   r"   r"   r#   Ú<module>   s.   e(|þ