B
    `.              
   @   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d.eeedddZeeddddZeeeeeeeedd	ddZeeddddZeeedddZ eeedddZ!eeed d!d"Z"eeed#d$d%Z#d&Z$eeeej%eef d'd(d)Z&G d*d+ d+e'Z(G d,d- d-e'Z)dS )/    N)gcd)utils)UnsupportedAlgorithm_Reasons)_get_backend)
RSABackend)_serializationhashes)AsymmetricPadding)AsymmetricSignatureContextAsymmetricVerificationContextr   c               @   s   e Zd ZejeejedddZ	eje
ee
dddZejeddd	Zejd
dddZeje
eejejejf e
dddZejddddZejejejeje
dddZdS )RSAPrivateKey)padding	algorithmreturnc             C   s   dS )zN
        Returns an AsymmetricSignatureContext used for signing data.
        N )selfr   r   r   r   \/home/dcms/DCMS/lib/python3.7/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pysigner   s    zRSAPrivateKey.signer)
ciphertextr   r   c             C   s   dS )z3
        Decrypts the provided ciphertext.
        Nr   )r   r   r   r   r   r   decrypt    s    zRSAPrivateKey.decrypt)r   c             C   s   dS )z7
        The bit length of the public modulus.
        Nr   )r   r   r   r   key_size&   s    zRSAPrivateKey.key_sizeRSAPublicKeyc             C   s   dS )zD
        The RSAPublicKey associated with this private key.
        Nr   )r   r   r   r   
public_key,   s    zRSAPrivateKey.public_key)datar   r   r   c             C   s   dS )z!
        Signs the data.
        Nr   )r   r   r   r   r   r   r   sign2   s    	zRSAPrivateKey.signRSAPrivateNumbersc             C   s   dS )z/
        Returns an RSAPrivateNumbers.
        Nr   )r   r   r   r   private_numbers=   s    zRSAPrivateKey.private_numbers)encodingformatencryption_algorithmr   c             C   s   dS )z6
        Returns the key serialized as bytes.
        Nr   )r   r   r   r    r   r   r   private_bytesC   s    	zRSAPrivateKey.private_bytesN)__name__
__module____qualname__abcabstractmethodr
   r	   HashAlgorithmr   r   bytesr   abstractpropertyintr   r   typingUnion
asym_utils	Prehashedr   r   r   EncodingZPrivateFormatZKeySerializationEncryptionr!   r   r   r   r   r      s*   r   )	metaclassc               @   s   e Zd Zejeeeje	dddZ
ejeeedddZejeddd	Zejd
dddZejejejedddZejeeeejejejf ddddZejeeejej edddZdS )r   )	signaturer   r   r   c             C   s   dS )zY
        Returns an AsymmetricVerificationContext used for verifying signatures.
        Nr   )r   r1   r   r   r   r   r   verifierS   s    	zRSAPublicKey.verifier)	plaintextr   r   c             C   s   dS )z/
        Encrypts the given plaintext.
        Nr   )r   r3   r   r   r   r   encrypt^   s    zRSAPublicKey.encrypt)r   c             C   s   dS )z7
        The bit length of the public modulus.
        Nr   )r   r   r   r   r   d   s    zRSAPublicKey.key_sizeRSAPublicNumbersc             C   s   dS )z-
        Returns an RSAPublicNumbers
        Nr   )r   r   r   r   public_numbersj   s    zRSAPublicKey.public_numbers)r   r   r   c             C   s   dS )z6
        Returns the key serialized as bytes.
        Nr   )r   r   r   r   r   r   public_bytesp   s    zRSAPublicKey.public_bytesN)r1   r   r   r   r   c             C   s   dS )z5
        Verifies the signature of the data.
        Nr   )r   r1   r   r   r   r   r   r   verifyz   s    
zRSAPublicKey.verifyc             C   s   dS )z@
        Recovers the original data from the signature.
        Nr   )r   r1   r   r   r   r   r   recover_data_from_signature   s    	z(RSAPublicKey.recover_data_from_signature)r"   r#   r$   r%   r&   r(   r
   r	   r'   r   r2   r4   r)   r*   r   r6   r   r/   ZPublicFormatr7   r+   r,   r-   r.   r8   Optionalr9   r   r   r   r   r   R   s4   
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   ZBACKEND_MISSING_INTERFACE_verify_rsa_parametersZgenerate_rsa_private_key)r;   r   backendr   r   r   generate_private_key   s    

r?   c             C   s$   | dk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)r;   r   r   r   r   r=      s
    r=   )	pqprivate_exponentdmp1dmq1iqmpr;   modulusr   c             C   s   |dk rt d| |kr t d||kr0t d||kr@t d||krPt d||kr`t d||krpt d|dk s||krt d	|d
@ dkrt d|d
@ dkrt d|d
@ dkrt d| | |krt dd S )Nr@   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.)rA   )rB   rC   rD   rE   rF   rG   r;   rH   r   r   r   _check_private_key_components   s0    
rJ   )enr   c             C   s@   |dk rt d| dk s | |kr(t d| d@ dkr<t dd S )Nr@   zn must be >= 3.ze must be >= 3 and < n.rI   r   ze must be odd.)rA   )rK   rL   r   r   r   _check_public_key_components   s    rM   )rK   mr   c       	      C   sV   d\}}| | }}x:|dkrLt ||\}}|||  }||||f\}}}}qW || S )zO
    Modular Multiplicative Inverse. Returns x such that: (x*e) mod m == 1
    )rI   r   r   )divmod)	rK   rN   x1Zx2abrC   rZxnr   r   r   _modinv   s    

rT   )rB   rC   r   c             C   s
   t || S )zF
    Compute the CRT (q ** -1) % p value from RSA primes p and q.
    )rT   )rB   rC   r   r   r   rsa_crt_iqmp   s    rU   )rD   rB   r   c             C   s   | |d  S )zg
    Compute the CRT private_exponent % (p - 1) value from the RSA
    private_exponent (d) and p.
    rI   r   )rD   rB   r   r   r   rsa_crt_dmp1   s    rV   )rD   rC   r   c             C   s   | |d  S )zg
    Compute the CRT private_exponent % (q - 1) value from the RSA
    private_exponent (d) and q.
    rI   r   )rD   rC   r   r   r   rsa_crt_dmq1  s    rW   i  )rL   rK   dr   c             C   s   || d }|}x|d dkr(|d }qW d}d}xv|s|t k r|}xX||k rt||| }|dkr|| d krt|d| dkrt|d | }	d}P |d9 }qFW |d7 }q4W |stdt| |	\}
}|dkst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.
    rI      r   FTz2Unable to compute factors p and q from exponent d.)reverse)_MAX_RECOVERY_ATTEMPTSpowr   rA   rO   AssertionErrorsorted)rL   rK   rX   ZktottZspottedrQ   kZcandrB   rC   rS   r   r   r   rsa_recover_prime_factors  s,    
$ra   c               @   s   e Zd Zeeeeeedd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edddZdd Zdd Zdd ZdS )r   r5   )rB   rC   rX   rE   rF   rG   r6   c             C   s   t |tr<t |tr<t |tr<t |tr<t |tr<t |tsDtdt |tsVtd|| _|| _|| _|| _|| _|| _	|| _
d S )NzNRSAPrivateNumbers p, q, d, dmp1, dmq1, iqmp arguments must all be an integers.zFRSAPrivateNumbers public_numbers must be an RSAPublicNumbers instance.)r<   r*   	TypeErrorr5   _p_q_d_dmp1_dmq1_iqmp_public_numbers)r   rB   rC   rX   rE   rF   rG   r6   r   r   r   __init__@  s$    






zRSAPrivateNumbers.__init__rc   rd   re   rf   rg   rh   ri   N)r   c             C   s   t |}|| S )N)r   Zload_rsa_private_numbers)r   r>   r   r   r   private_keym  s    zRSAPrivateNumbers.private_keyc             C   sb   t |tstS | j|jko`| j|jko`| j|jko`| j|jko`| j|jko`| j|jko`| j	|j	kS )N)
r<   r   NotImplementedrB   rC   rX   rE   rF   rG   r6   )r   otherr   r   r   __eq__q  s    
zRSAPrivateNumbers.__eq__c             C   s
   | |k S )Nr   )r   rm   r   r   r   __ne__  s    zRSAPrivateNumbers.__ne__c             C   s$   t | j| j| j| j| j| j| jfS )N)hashrB   rC   rX   rE   rF   rG   r6   )r   r   r   r   __hash__  s    zRSAPrivateNumbers.__hash__)N)r"   r#   r$   r*   rj   r   read_only_propertyrB   rC   rX   rE   rF   rG   r6   r   rk   rn   ro   rq   r   r   r   r   r   ?  s$   






r   c               @   s`   e Zd ZeedddZedZedZde	ddd	Z
d
d Zdd Zdd Zdd ZdS )r5   )rK   rL   c             C   s,   t |trt |tstd|| _|| _d S )Nz,RSAPublicNumbers arguments must be integers.)r<   r*   rb   _e_n)r   rK   rL   r   r   r   rj     s    zRSAPublicNumbers.__init__rs   rt   N)r   c             C   s   t |}|| S )N)r   Zload_rsa_public_numbers)r   r>   r   r   r   r     s    zRSAPublicNumbers.public_keyc             C   s
   d | S )Nz$<RSAPublicNumbers(e={0.e}, n={0.n})>)r   )r   r   r   r   __repr__  s    zRSAPublicNumbers.__repr__c             C   s&   t |tstS | j|jko$| j|jkS )N)r<   r5   rl   rK   rL   )r   rm   r   r   r   rn     s    
zRSAPublicNumbers.__eq__c             C   s
   | |k S )Nr   )r   rm   r   r   r   ro     s    zRSAPublicNumbers.__ne__c             C   s   t | j| jfS )N)rp   rK   rL   )r   r   r   r   rq     s    zRSAPublicNumbers.__hash__)N)r"   r#   r$   r*   rj   r   rr   rK   rL   r   r   ru   rn   ro   rq   r   r   r   r   r5     s   

r5   )N)*r%   r+   mathr   Zcryptographyr   Zcryptography.exceptionsr   r   Zcryptography.hazmat.backendsr   Z'cryptography.hazmat.backends.interfacesr   Zcryptography.hazmat.primitivesr   r	   Z*cryptography.hazmat.primitives._asymmetricr
   Z)cryptography.hazmat.primitives.asymmetricr   r   r-   ABCMetar   ZRSAPrivateKeyWithSerializationr   ZRSAPublicKeyWithSerializationr*   r?   r=   rJ   rM   rT   rU   rV   rW   r[   Tuplera   objectr   r5   r   r   r   r   <module>   sF   8@&+Q