U
    [e.                  
   @   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   o/var/www/html/services/stratfitenv/lib/python3.8/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      s2    
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   r5   r   r   r   r   r   verifierS   s    zRSAPublicKey.verifier)	plaintextr   r   c                 C   s   dS )z/
        Encrypts the given plaintext.
        Nr   )r   r8   r   r   r   r   encrypt^   s    zRSAPublicKey.encryptr   c                 C   s   dS r   r   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 r$   r   )r   r!   r"   r   r   r   public_bytesp   s    zRSAPublicKey.public_bytesN)r5   r   r   r   r   c                 C   s   dS )z5
        Verifies the signature of the data.
        Nr   )r   r5   r   r   r   r   r   r   verifyz   s    zRSAPublicKey.verifyc                 C   s   dS )z@
        Recovers the original data from the signature.
        Nr   r6   r   r   r   recover_data_from_signature   s    z(RSAPublicKey.recover_data_from_signature)r&   r'   r(   r)   r*   r,   r
   r	   r+   r   r7   r9   r-   r.   r   r;   r   r3   ZPublicFormatr<   r/   r0   r1   r2   r=   Optionalr>   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   ZBACKEND_MISSING_INTERFACE_verify_rsa_parametersZgenerate_rsa_private_key)r@   r   backendr   r   r   generate_private_key   s    

rD   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   rB      s    rB   )	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 )NrE   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.rF   )rH   rI   rJ   rK   rL   rM   r@   rN   r   r   r   _check_private_key_components   s0    
rP   )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 )NrE   zn must be >= 3.ze must be >= 3 and < n.rO   r   ze must be odd.rF   rQ   rR   r   r   r   _check_public_key_components   s    rT   )rQ   mr   c           	      C   sR   d\}}| | }}|dkrJt ||\}}|||  }||||f\}}}}q|| S )zO
    Modular Multiplicative Inverse. Returns x such that: (x*e) mod m == 1
    )rO   r   r   )divmod)	rQ   rU   x1Zx2abrI   rZxnr   r   r   _modinv   s    
r[   )rH   rI   r   c                 C   s
   t || S )zF
    Compute the CRT (q ** -1) % p value from RSA primes p and q.
    )r[   )rH   rI   r   r   r   rsa_crt_iqmp   s    r\   )rJ   rH   r   c                 C   s   | |d  S )zg
    Compute the CRT private_exponent % (p - 1) value from the RSA
    private_exponent (d) and p.
    rO   r   )rJ   rH   r   r   r   rsa_crt_dmp1   s    r]   )rJ   rI   r   c                 C   s   | |d  S )zg
    Compute the CRT private_exponent % (q - 1) value from the RSA
    private_exponent (d) and q.
    rO   r   )rJ   rI   r   r   r   rsa_crt_dmq1  s    r^   i  )rR   rQ   dr   c                 C   s   || d }|}|d dkr&|d }qd}d}|s|t k r|}||k rt||| }|dkr|| d krt|d| dkrt|d | }	d}q|d9 }q>|d7 }q.|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.
    rO      r   FTz2Unable to compute factors p and q from exponent d.)reverse)_MAX_RECOVERY_ATTEMPTSpowr   rG   rV   AssertionErrorsorted)rR   rQ   r_   ZktottZspottedrX   kcandrH   rI   rZ   r   r   r   rsa_recover_prime_factors  s,    
$

ri   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   r:   )rH   rI   r_   rK   rL   rM   r;   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.)rA   r.   	TypeErrorr:   _p_q_d_dmp1_dmq1_iqmp_public_numbers)r   rH   rI   r_   rK   rL   rM   r;   r   r   r   __init__@  s4    
zRSAPrivateNumbers.__init__rk   rl   rm   rn   ro   rp   rq   Nr   c                 C   s   t |}|| S N)r   Zload_rsa_private_numbersr   rC   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 rs   )
rA   r   NotImplementedrH   rI   r_   rK   rL   rM   r;   r   otherr   r   r   __eq__q  s    






zRSAPrivateNumbers.__eq__c                 C   s
   | |k S rs   r   rw   r   r   r   __ne__  s    zRSAPrivateNumbers.__ne__c                 C   s$   t | j| j| j| j| j| j| jfS rs   )hashrH   rI   r_   rK   rL   rM   r;   r   r   r   r   __hash__  s    zRSAPrivateNumbers.__hash__)N)r&   r'   r(   r.   rr   r   read_only_propertyrH   rI   r_   rK   rL   rM   r;   r   ru   ry   rz   r|   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 )r:   rS   c                 C   s,   t |trt |tstd|| _|| _d S )Nz,RSAPublicNumbers arguments must be integers.)rA   r.   rj   _e_n)r   rQ   rR   r   r   r   rr     s    zRSAPublicNumbers.__init__r~   r   Nr   c                 C   s   t |}|| S rs   )r   Zload_rsa_public_numbersrt   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 rs   )rA   r:   rv   rQ   rR   rw   r   r   r   ry     s    
zRSAPublicNumbers.__eq__c                 C   s
   | |k S rs   r   rw   r   r   r   rz     s    zRSAPublicNumbers.__ne__c                 C   s   t | j| jfS rs   )r{   rQ   rR   r   r   r   r   r|     s    zRSAPublicNumbers.__hash__)N)r&   r'   r(   r.   rr   r   r}   rQ   rR   r   r   r   ry   rz   r|   r   r   r   r   r:     s   

r:   )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   r1   ABCMetar   ZRSAPrivateKeyWithSerializationr   ZRSAPublicKeyWithSerializationr.   rD   rB   rP   rT   r[   r\   r]   r^   rb   Tupleri   objectr   r:   r   r   r   r   <module>   sV   8@  /  -Q