o
    �)%aä.  ã                   @   sÈ  d dl Z d dlZd dlmZ d dlmZ d dlmZmZ d dl	m
Z
 d dlmZ d dlmZmZ d dlmZ d d	lmZmZmZ G d
d„ de jd�ZeZG dd„ de jd�ZeZ	d4dededefdd„Zdededdfdd„Zdededededededededdfdd„Zded eddfd!d"„Zded#edefd$d%„Z dededefd&d'„Z!dededefd(d)„Z"dededefd*d+„Z#d,Z$d eded-edej%eef fd.d/„Z&G d0d1„ d1e'ƒZ(G d2d3„ d3e'ƒZ)dS )5é    N)Úgcd)Úutils)ÚUnsupportedAlgorithmÚ_Reasons)Ú_get_backend)Ú
RSABackend)Ú_serializationÚhashes)ÚAsymmetricPadding)ÚAsymmetricSignatureContextÚAsymmetricVerificationContextr   c                	   @   sÎ   e Zd Zejdedejdefdd„ƒZ	ejde
dede
fdd„ƒZejdefd	d
„ƒZejddd„ƒZejde
dedejejejf de
fdd„ƒZejddd„ƒZejdejdejdejde
fdd„ƒZdS )ÚRSAPrivateKeyÚpaddingÚ	algorithmÚreturnc                 C   ó   dS )zN
        Returns an AsymmetricSignatureContext used for signing data.
        N© )Úselfr   r   r   r   úO/usr/lib/python3/dist-packages/cryptography/hazmat/primitives/asymmetric/rsa.pyÚsigner   ó    zRSAPrivateKey.signerÚ
ciphertextc                 C   r   )z3
        Decrypts the provided ciphertext.
        Nr   )r   r   r   r   r   r   Údecrypt    r   zRSAPrivateKey.decryptc                 C   r   ©z7
        The bit length of the public modulus.
        Nr   ©r   r   r   r   Úkey_size&   r   zRSAPrivateKey.key_sizeÚRSAPublicKeyc                 C   r   )zD
        The RSAPublicKey associated with this private key.
        Nr   r   r   r   r   Ú
public_key,   r   zRSAPrivateKey.public_keyÚdatac                 C   r   )z!
        Signs the data.
        Nr   )r   r   r   r   r   r   r   Úsign2   r   zRSAPrivateKey.signÚRSAPrivateNumbersc                 C   r   )z/
        Returns an RSAPrivateNumbers.
        Nr   r   r   r   r   Úprivate_numbers=   r   zRSAPrivateKey.private_numbersÚencodingÚformatÚencryption_algorithmc                 C   r   ©z6
        Returns the key serialized as bytes.
        Nr   )r   r"   r#   r$   r   r   r   Úprivate_bytesC   r   zRSAPrivateKey.private_bytesN)r   r   )r   r    )Ú__name__Ú
__module__Ú__qualname__ÚabcÚabstractmethodr
   r	   ÚHashAlgorithmr   r   Úbytesr   ÚabstractpropertyÚintr   r   ÚtypingÚUnionÚ
asym_utilsÚ	Prehashedr   r!   r   ÚEncodingÚPrivateFormatÚKeySerializationEncryptionr&   r   r   r   r   r      sJ    ÿÿþþýüû
þýüûr   )Ú	metaclassc                   @   sè   e Zd Zejdededejde	fdd„ƒZ
ejdededefdd	„ƒZejdefd
d„ƒZejddd„ƒZejdejdejdefdd„ƒZejdedededejejejf ddf
dd„ƒZejdededejej defdd„ƒZdS )r   Ú	signaturer   r   r   c                 C   r   )zY
        Returns an AsymmetricVerificationContext used for verifying signatures.
        Nr   ©r   r8   r   r   r   r   r   ÚverifierS   r   zRSAPublicKey.verifierÚ	plaintextc                 C   r   )z/
        Encrypts the given plaintext.
        Nr   )r   r;   r   r   r   r   Úencrypt^   r   zRSAPublicKey.encryptc                 C   r   r   r   r   r   r   r   r   d   r   zRSAPublicKey.key_sizeÚRSAPublicNumbersc                 C   r   )z-
        Returns an RSAPublicNumbers
        Nr   r   r   r   r   Úpublic_numbersj   r   zRSAPublicKey.public_numbersr"   r#   c                 C   r   r%   r   )r   r"   r#   r   r   r   Úpublic_bytesp   r   zRSAPublicKey.public_bytesr   Nc                 C   r   )z5
        Verifies the signature of the data.
        Nr   )r   r8   r   r   r   r   r   r   Úverifyz   r   zRSAPublicKey.verifyc                 C   r   )z@
        Recovers the original data from the signature.
        Nr   r9   r   r   r   Úrecover_data_from_signature†   r   z(RSAPublicKey.recover_data_from_signature)r   r=   )r'   r(   r)   r*   r+   r-   r
   r	   r,   r   r:   r<   r.   r/   r   r>   r   r4   ÚPublicFormatr?   r0   r1   r2   r3   r@   ÚOptionalrA   r   r   r   r   r   R   s^    þýüû
þýü	þýüûúþý
üûr   Úpublic_exponentr   r   c                 C   s4   t |ƒ}t|tƒstdtjƒ‚t| |ƒ | | |¡S )Nz-Backend object does not implement RSABackend.)r   Ú
isinstancer   r   r   ÚBACKEND_MISSING_INTERFACEÚ_verify_rsa_parametersÚgenerate_rsa_private_key)rD   r   Úbackendr   r   r   Úgenerate_private_key•   s   
þ
rJ   c                 C   s$   | dvrt dƒ‚|dk rt dƒ‚d S )N)é   i  zopublic_exponent must be either 3 (for legacy compatibility) or 65537. Almost everyone should choose 65537 here!i   z#key_size must be at least 512-bits.©Ú
ValueError)rD   r   r   r   r   rG   £   s   ÿÿrG   ÚpÚqÚprivate_exponentÚdmp1Údmq1ÚiqmpÚmodulusc                 C   sÜ   |dk rt dƒ‚| |krt dƒ‚||krt dƒ‚||kr t dƒ‚||kr(t dƒ‚||kr0t dƒ‚||kr8t dƒ‚|dk s@||krDt d	ƒ‚|d
@ dkrNt dƒ‚|d
@ dkrXt dƒ‚|d
@ dkrbt dƒ‚| | |krlt dƒ‚d S )NrK   zmodulus must be >= 3.zp must be < modulus.zq must be < modulus.zdmp1 must be < modulus.zdmq1 must be < modulus.ziqmp must be < modulus.z#private_exponent must be < modulus.z+public_exponent must be >= 3 and < modulus.é   r   zpublic_exponent must be odd.zdmp1 must be odd.zdmq1 must be odd.zp*q must equal modulus.rL   )rN   rO   rP   rQ   rR   rS   rD   rT   r   r   r   Ú_check_private_key_components®   s2   
ÿrV   ÚeÚnc                 C   s@   |dk rt dƒ‚| dk s| |krt dƒ‚| d@ dkrt dƒ‚d S )NrK   zn must be >= 3.ze must be >= 3 and < n.rU   r   ze must be odd.rL   )rW   rX   r   r   r   Ú_check_public_key_componentsÝ   s   ÿrY   Úmc           	      C   sX   d\}}| |}}|dkr(t ||ƒ\}}|||  }||||f\}}}}|dks|| S )zO
    Modular Multiplicative Inverse. Returns x such that: (x*e) mod m == 1
    )rU   r   r   )Údivmod)	rW   rZ   Úx1Úx2ÚaÚbrO   ÚrÚxnr   r   r   Ú_modinvè   s   
ýrb   c                 C   s
   t || ƒS )zF
    Compute the CRT (q ** -1) % p value from RSA primes p and q.
    )rb   )rN   rO   r   r   r   Úrsa_crt_iqmpõ   s   
rc   c                 C   ó   | |d  S )zg
    Compute the CRT private_exponent % (p - 1) value from the RSA
    private_exponent (d) and p.
    rU   r   )rP   rN   r   r   r   Úrsa_crt_dmp1ü   ó   re   c                 C   rd   )zg
    Compute the CRT private_exponent % (q - 1) value from the RSA
    private_exponent (d) and q.
    rU   r   )rP   rO   r   r   r   Úrsa_crt_dmq1  rf   rg   iè  Údc                 C   sú   || d }|}|d dkr|d }|d dksd}d}|s\|t k r\|}||k rRt||| ƒ}|dkrJ|| d krJt|d| ƒdkrJt|d | ƒ}	d}n|d9 }||k s(|d7 }|s\|t k s"|sbtdƒ‚t| |	ƒ\}
}|dksoJ ‚t|	|
fdd�\}	}
|	|
fS )z¡
    Compute factors p and q from the private exponent d. We assume that n has
    no more than two factors. This function is adapted from code in PyCrypto.
    rU   é   r   FTz2Unable to compute factors p and q from exponent d.)Úreverse)Ú_MAX_RECOVERY_ATTEMPTSÚpowr   rM   r[   Úsorted)rX   rW   rh   ÚktotÚtÚspottedr^   ÚkÚcandrN   rO   r`   r   r   r   Úrsa_recover_prime_factors  s2   ÿ$÷òrs   c                   @   s    e Zd Zdededededededdfd	d
„Ze d¡Ze d¡Ze d¡Z	e d¡Z
e d¡Ze d¡Ze d¡Zddefdd„Zdd„ Zdd„ Zdd„ ZdS )r    rN   rO   rh   rQ   rR   rS   r>   r=   c                 C   s„   t |tƒrt |tƒrt |tƒrt |tƒrt |tƒrt |tƒs"tdƒ‚t |tƒs+tdƒ‚|| _|| _|| _|| _|| _|| _	|| _
d S )NzNRSAPrivateNumbers p, q, d, dmp1, dmq1, iqmp arguments must all be an integers.zFRSAPrivateNumbers public_numbers must be an RSAPublicNumbers instance.)rE   r/   Ú	TypeErrorr=   Ú_pÚ_qÚ_dÚ_dmp1Ú_dmq1Ú_iqmpÚ_public_numbers)r   rN   rO   rh   rQ   rR   rS   r>   r   r   r   Ú__init__@  s4   ÿþýüûúÿ
ÿ
zRSAPrivateNumbers.__init__ru   rv   rw   rx   ry   rz   r{   Nr   c                 C   ó   t |ƒ}| | ¡S ©N)r   Úload_rsa_private_numbers©r   rI   r   r   r   Úprivate_keym  ó   
zRSAPrivateNumbers.private_keyc                 C   sb   t |tƒstS | j|jko0| j|jko0| j|jko0| j|jko0| j|jko0| j|jko0| j	|j	kS r~   )
rE   r    ÚNotImplementedrN   rO   rh   rQ   rR   rS   r>   ©r   Úotherr   r   r   Ú__eq__q  s   

ÿ
þ
ý
ü
û
ùzRSAPrivateNumbers.__eq__c                 C   ó
   | |k S r~   r   r„   r   r   r   Ú__ne__  ó   
zRSAPrivateNumbers.__ne__c                 C   s$   t | j| j| j| j| j| j| jfƒS r~   )ÚhashrN   rO   rh   rQ   rR   rS   r>   r   r   r   r   Ú__hash__‚  s   ùÿzRSAPrivateNumbers.__hash__r~   )r'   r(   r)   r/   r|   r   Úread_only_propertyrN   rO   rh   rQ   rR   rS   r>   r   r�   r†   rˆ   r‹   r   r   r   r   r    ?  s6    þýüûúù
ø
%





r    c                   @   sb   e Zd Zdedefdd„Ze d¡Ze d¡Zdde	fd	d
„Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )r=   rW   rX   c                 C   s,   t |tƒr
t |tƒstdƒ‚|| _|| _d S )Nz,RSAPublicNumbers arguments must be integers.)rE   r/   rt   Ú_eÚ_n)r   rW   rX   r   r   r   r|   ‘  s   
zRSAPublicNumbers.__init__r�   rŽ   Nr   c                 C   r}   r~   )r   Úload_rsa_public_numbersr€   r   r   r   r   ›  r‚   zRSAPublicNumbers.public_keyc                 C   s
   d  | ¡S )Nz$<RSAPublicNumbers(e={0.e}, n={0.n})>)r#   r   r   r   r   Ú__repr__Ÿ  r‰   zRSAPublicNumbers.__repr__c                 C   s&   t |tƒstS | j|jko| j|jkS r~   )rE   r=   rƒ   rW   rX   r„   r   r   r   r†   ¢  s   
zRSAPublicNumbers.__eq__c                 C   r‡   r~   r   r„   r   r   r   rˆ   ¨  r‰   zRSAPublicNumbers.__ne__c                 C   s   t | j| jfƒS r~   )rŠ   rW   rX   r   r   r   r   r‹   «  s   zRSAPublicNumbers.__hash__r~   )r'   r(   r)   r/   r|   r   rŒ   rW   rX   r   r   r�   r†   rˆ   r‹   r   r   r   r   r=   �  s    

r=   r~   )*r*   r0   Úmathr   Úcryptographyr   Úcryptography.exceptionsr   r   Úcryptography.hazmat.backendsr   Ú'cryptography.hazmat.backends.interfacesr   Úcryptography.hazmat.primitivesr   r	   Ú*cryptography.hazmat.primitives._asymmetricr
   Ú)cryptography.hazmat.primitives.asymmetricr   r   r2   ÚABCMetar   ÚRSAPrivateKeyWithSerializationr   ÚRSAPublicKeyWithSerializationr/   rJ   rG   rV   rY   rb   rc   re   rg   rk   ÚTuplers   Úobjectr    r=   r   r   r   r   Ú<module>   sv   8@ÿÿÿ
þÿþýüûúùø	
÷/ÿÿÿ
þ-Q