o
    Œõ±j]O  ã                   @   s2  d dl mZmZmZmZ d dlZd dlZd dlZd dlZd dl	Z	d dl
Z
ddlmZ ddlmZmZ ddlmZmZmZmZmZ ddlmZ ddlmZ dd	lmZmZmZ ejd
k r`eZ e!Z"ndd„ Z eƒ 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S )$é    )Úunicode_literalsÚdivisionÚabsolute_importÚprint_functionNé   )Úbackend)Úconstant_compareÚ
rand_bytes)ÚCertificateÚint_from_bytesÚint_to_bytesÚPrivateKeyInfoÚPublicKeyInfo)Úpretty_message)Ú
fill_width)Ú	type_nameÚbyte_clsÚ	int_types)é   c                 C   s
   t | gƒS )N)Úbytes)Únum© r   ú‚/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/oscrypto/_pkcs1.pyÚchr_cls   s   
r   )Úadd_pss_paddingÚadd_pkcs1v15_signature_paddingÚraw_rsa_private_cryptÚraw_rsa_public_cryptÚ"remove_pkcs1v15_encryption_paddingÚ!remove_pkcs1v15_signature_paddingÚverify_pss_paddingc                  C   s:   t jdkrdS t ¡ d } ttt|  d¡ƒƒdd… dkS )zM
    :return:
        A bool if the current machine is running OS X 10.7
    ÚdarwinFr   Ú.é   )é
   é   )ÚsysÚplatformÚmac_verÚtupleÚmapÚintÚsplit)Úversionr   r   r   Ú_is_osx_107/   s   
 r.   c                 C   s  t dkrtjdkrttdƒƒ‚t|tƒsttdt|ƒƒƒ‚t|t	ƒs+ttdt|ƒƒƒ‚|dk r8t
tdt|ƒƒƒ‚t|t	ƒsFttdt|ƒƒƒ‚|d	k rSt
td
t|ƒƒƒ‚| tg d¢ƒvrdt
tdt| ƒƒƒ‚tt| ƒ}|d }tt |d ¡ƒ}||ƒ ¡ }t|ƒ}||| d k rŽt
tdƒƒ‚|dkr˜t |¡}	nd}	d| |	 }
||
ƒ ¡ }d|| | d  }|d |	 }t| ||| d ƒ}tt|ƒt|ƒA ƒ}t|t|ƒƒ}d| | }d| dd|   }t|dƒ}|dkrüt|t|dd… ƒ@ ƒ|dd…  }|| d S )a5  
    Pads a byte string using the EMSA-PSS-Encode operation described in PKCS#1
    v2.2.

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

    :param salt_length:
        The length of the salt as an integer - typically the same as the length
        of the output from the hash_algorithm

    :param key_length:
        The length of the RSA key, in bits

    :param message:
        A byte string of the message to pad

    :return:
        The encoded (passed) message
    Ú	winlegacyr!   z~
            Pure-python RSA PSS signature padding addition code is only for
            Windows XP/2003 and OS X
            ú?
            message must be a byte string, not %s
            ú@
            salt_length must be an integer, not %s
            r   ú?
            salt_length must be 0 or more - is %s
            ú?
            key_length must be an integer, not %s
            i   z@
            key_length must be 512 or more - is %s
            ©Úsha1Úsha224Úsha256Úsha384Úsha512úz
            hash_algorithm must be one of "sha1", "sha224", "sha256", "sha384",
            "sha512", not %s
            r   é   r#   zq
            Key is not long enough to use with specified hash_algorithm and
            salt_length
            ó    ó           ó    ó   Ú0Ú1éÿ   Nó   ¼)Ú_backendr&   r'   ÚSystemErrorr   Ú
isinstancer   Ú	TypeErrorr   r   Ú
ValueErrorÚreprÚsetÚgetattrÚhashlibr+   ÚmathÚceilÚdigestÚlenÚosÚurandomÚ_mgf1r   r   r   r   Úord)Úhash_algorithmÚsalt_lengthÚ
key_lengthÚmessageÚ	hash_funcÚem_bitsÚem_lenÚmessage_digestÚhash_lengthÚsaltÚm_primeÚm_prime_digestÚpaddingÚdbÚdb_maskÚ	masked_dbÚ	zero_bitsÚleft_bit_maskÚleft_int_maskr   r   r   r   ;   sv   ÿ
ü
üü
üüû
ÿ
$r   c                 C   s‚  t dkrtjdkrttdƒƒ‚t|tƒsttdt|ƒƒƒ‚t|tƒs+ttdt|ƒƒƒ‚t|t	ƒs9ttdt|ƒƒƒ‚|dk rFt
tdt|ƒƒƒ‚| tg d	¢ƒvrWt
td
t| ƒƒƒ‚tt| ƒ}|d }tt |d ¡ƒ}||ƒ ¡ }t|ƒ}	||	| d k r}dS |dd… dkr‡dS d| | }
||	 d }|d|… }t|dd… ƒ}|d|
 ? }|dkr­dS ||||	 … }t| |||	 d ƒ}d|
 dd|
   }t|dƒ}|dkrät|t|dd… ƒ@ ƒ|dd…  }tt|ƒt|ƒA ƒ}t|ƒt|ƒk �rdt|ƒt|ƒ  | }||	 | d }d| }t|d|… |ƒ�sdS |||d … dk�r(dS |d| d… }d| | }||ƒ ¡ }t||ƒS )aZ  
    Verifies the PSS padding on an encoded message

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

    :param salt_length:
        The length of the salt as an integer - typically the same as the length
        of the output from the hash_algorithm

    :param key_length:
        The length of the RSA key, in bits

    :param message:
        A byte string of the message to pad

    :param signature:
        The signature to verify

    :return:
        A boolean indicating if the signature is invalid
    r/   r!   z‚
            Pure-python RSA PSS signature padding verification code is only for
            Windows XP/2003 and OS X
            r0   zA
            signature must be a byte string, not %s
            r1   r   r2   r4   r:   r   r;   r#   FéÿÿÿÿNrC   r@   rA   rB   r>   r?   r=   )rD   r&   r'   rE   r   rF   r   rG   r   r   rH   rI   rJ   rK   rL   r+   rM   rN   rO   rP   rT   rS   r   r   r   r   )rU   rV   rW   rX   Ú	signaturerY   rZ   r[   r\   r]   re   Úmasked_db_lengthrd   Ú
first_byteÚbits_that_should_be_zeror`   rc   rf   rg   rb   Úzero_lengthÚzero_stringr^   r_   Úh_primer   r   r   r    ¹   s€   ÿ
ü
ü
üüû

$
r    c           
      C   sì   t |tƒsttdt|ƒƒƒ‚t |tƒsttdt|ƒƒƒ‚|dk r)ttdt|ƒƒƒ‚| tg d¢ƒvr:ttdt| ƒƒƒ‚d}dd	d
dddœ|  }t	t
 || ¡ƒ}t d¡j}tt| ƒ}td|ƒD ]}||ƒ}	||||	 ƒ ¡ 7 }q_|d|… S )a|  
    The PKCS#1 MGF1 mask generation algorithm

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

    :param seed:
        A byte string to use as the seed for the mask

    :param mask_length:
        The desired mask length, as an integer

    :return:
        A byte string of the mask
    z<
            seed must be a byte string, not %s
            z@
            mask_length must be an integer, not %s
            r   zD
            mask_length must be greater than 0 - is %s
            r4   r:   r<   é   é   é    é0   é@   s   >Ir   )rF   r   rG   r   r   r   rH   rI   rJ   r+   rM   rN   ÚstructÚStructÚpackrK   rL   ÚrangerO   )
rU   ÚseedÚmask_lengthÚoutputr]   Ú
iterationsrw   rY   ÚcounterÚbr   r   r   rS   :  sH   
ü
üüûûú
rS   c                 C   ó    t dkr
ttdƒƒ‚t| |dƒS )zï
    Adds PKCS#1 v1.5 padding to a message to be signed

    :param key_length:
        An integer of the number of bytes in the key

    :param data:
        A byte string to pad

    :return:
        The padded data as a byte string
    r/   zz
            Pure-python RSA PKCSv1.5 signature padding addition code is only
            for Windows XP/2003
            Úsigning)rD   rE   r   Ú_add_pkcs1v15_padding©rW   Údatar   r   r   r   ƒ  s
   ÿr   c                 C   r   )a  
    Removes PKCS#1 v1.5 padding from a signed message using constant time
    operations

    :param key_length:
        An integer of the number of bytes in the key

    :param data:
        A byte string to unpad

    :return:
        The unpadded data as a byte string
    r/   zy
            Pure-python RSA PKCSv1.5 signature padding removal code is only for
            Windows XP/2003
            Ú	verifying)rD   rE   r   Ú_remove_pkcs1v15_paddingr‚   r   r   r   r   œ  s
   ÿr   c                 C   s   t ƒ s	ttdƒƒ‚t| |dƒS )a  
    Removes PKCS#1 v1.5 padding from a decrypted message using constant time
    operations

    :param key_length:
        An integer of the number of bytes in the key

    :param data:
        A byte string to unpad

    :return:
        The unpadded data as a byte string
    zt
            Pure-python RSA PKCSv1.5 encryption padding removal code is only
            for OS X 10.7
            Ú
decrypting)r.   rE   r   r…   r‚   r   r   r   r   ¶  s
   ÿr   c                 C   sð   |dkrd}nd}t |tƒsttdt|ƒƒƒ‚t | tƒs%ttdt| ƒƒƒ‚| dk r2ttdt| ƒƒƒ‚t|ƒ| d krFttd	| d t|ƒƒƒ‚| d
 t|ƒ }d}|dkrnt	|ƒ}d 
| d¡¡}||7 }|t|ƒ8 }|dksTd| | d | S )a1  
    Adds PKCS#1 v1.5 padding to a message

    :param key_length:
        An integer of the number of bytes in the key

    :param data:
        A byte string to unpad

    :param operation:
        A unicode string of "encrypting" or "signing"

    :return:
        The padded data as a byte string
    Ú
encryptingó   r?   ú<
            data must be a byte string, not %s
            r3   rt   ú?
            key_length must be 64 or more - is %s
            é   zJ
            data must be between 1 and %s bytes long - is %s
            r   r<   r   r>   )rF   r   rG   r   r   r   rH   rI   rP   r	   Újoinr,   )rW   rƒ   Ú	operationÚsecond_byteÚrequired_bytesra   Útemp_paddingr   r   r   r�   Ð  sB   
ü
üüûûr�   c                 C   sd  |dkrd}nd}t |tƒsttdt|ƒƒƒ‚t | tƒs%ttdt| ƒƒƒ‚| dk r2ttdt| ƒƒƒ‚t|ƒ| kr>td| ƒ‚d	}d	}d	}t	d	t|ƒƒD ]T}|||d … }t
|ƒ}	|d	krb||	O }qK|dkrq|t|	|B |kƒO }qK|d
k r€|t|	d	A d	kƒO }qK|	d	B }
|d	kr”|
r�||O }qK||O }qK|
r›||O }qK||O }qK|d	krªtd| ƒ‚||d d… S )aY  
    Removes PKCS#1 v1.5 padding from a message using constant time operations

    :param key_length:
        An integer of the number of bytes in the key

    :param data:
        A byte string to unpad

    :param operation:
        A unicode string of "decrypting" or "verifying"

    :return:
        The unpadded data as a byte string
    r†   r#   r   r‰   r3   rt   rŠ   zError %sr   r$   N)rF   r   rG   r   r   r   rH   rI   rP   rx   rT   r+   )rW   rƒ   r�   rŽ   ÚerrorÚtrashÚpadding_endÚiÚbyteÚbyte_numÚnon_zeror   r   r   r…     sV   
ü
üü




r…   c                 C   s¾   t dkrtdƒ‚t| dƒrt| jtƒsttdt| ƒƒƒ‚| jd d j	}|dkr5|dkr5t
td	| ¡ ƒƒ‚t|tƒsCttd
t|ƒƒƒ‚| jd j}tt|ƒ|d j	|d j	ƒ}t|| jjd�S )aa  
    Performs a raw RSA algorithm in a byte string using a private key.
    This is a low-level primitive and is prone to disastrous results if used
    incorrectly.

    :param private_key:
        An oscrypto.asymmetric.PrivateKey object

    :param data:
        A byte string of the plaintext to be signed or ciphertext to be
        decrypted. Must be less than or equal to the length of the private key.
        In the case of signing, padding must already be applied. In the case of
        decryption, padding must be removed afterward.

    :return:
        A byte string of the transformed data
    r/   ú1Pure-python RSA crypt is only for Windows XP/2003Úasn1zy
            private_key must be an instance of the
            oscrypto.asymmetric.PrivateKey class, not %s
            Úprivate_key_algorithmÚ	algorithmÚrsaÚ
rsassa_pssz@
            private_key must be an RSA key, not %s
            r‰   Úprivate_keyÚprivate_exponentÚmodulus©Úwidth)rD   rE   ÚhasattrrF   r™   r   rG   r   r   ÚnativerH   Úupperr   ÚparsedÚpowr   r   Ú	byte_size)rž   rƒ   ÚalgoÚrsa_private_keyÚtransformed_intr   r   r   r   n  s2   ûü
üýr   c                 C   sÊ   t dkrtdƒ‚t| dƒ}ttf}|rt| j|ƒs"ttdt	| ƒƒƒ‚| jd d j
}|dkr;|dkr;ttd| ¡ ƒƒ‚t|tƒsIttd	t	|ƒƒƒ‚| jd
 j}tt|ƒ|d j
|d j
ƒ}t|| jjd�S )a¥  
    Performs a raw RSA algorithm in a byte string using a certificate or
    public key. This is a low-level primitive and is prone to disastrous results
    if used incorrectly.

    :param certificate_or_public_key:
        An oscrypto.asymmetric.PublicKey or oscrypto.asymmetric.Certificate
        object

    :param data:
        A byte string of the signature when verifying, or padded plaintext when
        encrypting. Must be less than or equal to the length of the public key.
        When verifying, padding will need to be removed afterwards. When
        encrypting, padding must be applied before.

    :return:
        A byte string of the transformed data
    r/   r˜   r™   z·
            certificate_or_public_key must be an instance of the
            oscrypto.asymmetric.PublicKey or oscrypto.asymmetric.Certificate
            classes, not %s
            r›   rœ   r�   zN
            certificate_or_public_key must be an RSA key, not %s
            r‰   Ú
public_keyÚpublic_exponentr    r¡   )rD   rE   r£   r   r
   rF   r™   rG   r   r   r¤   rH   r¥   r   r¦   r§   r   r   r¨   )Úcertificate_or_public_keyrƒ   Úhas_asn1Úvalid_typesr©   Úrsa_public_keyr«   r   r   r   r   §  s<   
ú	ü
üýþr   )0Ú
__future__r   r   r   r   r&   rL   rM   r'   ru   rQ   Ú r   Úutilr   r	   Ú_asn1r
   r   r   r   r   Ú_errorsr   Ú_intr   Ú_typesr   r   r   Úversion_infoÚchrr   Úxrangerx   rD   Ú__all__r.   r   r    rS   r   r   r   r�   r…   r   r   r   r   r   r   Ú<module>   s>   
~ IC[9