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