o
    bG/                     @   s  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 d dl	m
Z
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j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)UnsupportedAlgorithm_Reasons)_get_backend)Backend
RSABackend)_serializationhashes)AsymmetricPadding)AsymmetricSignatureContextAsymmetricVerificationContextutilsc                	   @   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   l/var/www/secure340b-portal/env/lib/python3.10/site-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   sign1   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_bytesB   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   EncodingZPrivateFormatZ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   r7   r   r   r   r   r   verifierR   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   c   r   zRSAPublicKey.key_sizeRSAPublicNumbersc                 C   r   )z-
        Returns an RSAPublicNumbers
        Nr   r   r   r   r   public_numbersi   r   zRSAPublicKey.public_numbersr#   r$   c                 C   r   r&   r   )r   r#   r$   r   r   r   public_byteso   r   zRSAPublicKey.public_bytesr   Nc                 C   r   )z5
        Verifies the signature of the data.
        Nr   )r   r7   r   r   r   r   r   r   verifyy   r   zRSAPublicKey.verifyc                 C   r   )z@
        Recovers the original data from the signature.
        Nr   r8   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   r9   r;   r/   r0   r   r=   r   r5   ZPublicFormatr>   r1   r2   r3   r4   r?   Optionalr@   r   r   r   r   r   Q   s^    
	
r   public_exponentr   backendr   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)rB   r   rC   r   r   r   generate_private_key   s   

rF   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)rB   r   r   r   r   rE      s   rE   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 )NrG   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.rH   )rJ   rK   rL   rM   rN   rO   rB   rP   r   r   r   _check_private_key_components   s2   
rR   enc                 C   s@   |dk rt d| dk s| |krt d| d@ dkrt dd S )NrG   zn must be >= 3.ze must be >= 3 and < n.rQ   r   ze must be odd.rH   )rS   rT   r   r   r   _check_public_key_components   s   rU   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
    )rQ   r   r   )divmod)	rS   rV   x1Zx2abrK   rZxnr   r   r   _modinv   s   
r\   c                 C   s
   t || S )zF
    Compute the CRT (q ** -1) % p value from RSA primes p and q.
    )r\   )rJ   rK   r   r   r   rsa_crt_iqmp   s   
r]   c                 C      | |d  S )zg
    Compute the CRT private_exponent % (p - 1) value from the RSA
    private_exponent (d) and p.
    rQ   r   )rL   rJ   r   r   r   rsa_crt_dmp1      r_   c                 C   r^   )zg
    Compute the CRT private_exponent % (q - 1) value from the RSA
    private_exponent (d) and q.
    rQ   r   )rL   rK   r   r   r   rsa_crt_dmq1  r`   ra   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.
    rQ      r   FTz2Unable to compute factors p and q from exponent d.)reverse)_MAX_RECOVERY_ATTEMPTSpowr   rI   rW   sorted)rT   rS   rb   ZktottZspottedrY   kcandrJ   rK   r[   r   r   r   rsa_recover_prime_factors  s2   $rk   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d Zedd Zedd Zedd Z	edd Z
edd Zedd Z	ddeje defddZdd Zdd Zdd ZdS )r!   rJ   rK   rb   rM   rN   rO   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.)rD   r0   	TypeErrorr<   _p_q_d_dmp1_dmq1_iqmp_public_numbers)r   rJ   rK   rb   rM   rN   rO   r=   r   r   r   __init__A  s4   

zRSAPrivateNumbers.__init__c                 C      | j S N)rm   r   r   r   r   <lambda>f      zRSAPrivateNumbers.<lambda>c                 C   ru   rv   )rn   r   r   r   r   rw   g  rx   c                 C   ru   rv   )ro   r   r   r   r   rw   h  rx   c                 C   ru   rv   )rp   r   r   r   r   rw   i  rx   c                 C   ru   rv   )rq   r   r   r   r   rw   j  rx   c                 C   ru   rv   )rr   r   r   r   r   rw   k  rx   c                 C   ru   rv   )rs   r   r   r   r   rw   l  rx   NrC   r   c                 C      t |}|| S rv   )r   Zload_rsa_private_numbersr   rC   r   r   r   private_keyn     
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 rv   )
rD   r!   NotImplementedrJ   rK   rb   rM   rN   rO   r=   r   otherr   r   r   __eq__t  s   






zRSAPrivateNumbers.__eq__c                 C   
   | |k S rv   r   r~   r   r   r   __ne__     
zRSAPrivateNumbers.__ne__c                 C   s$   t | j| j| j| j| j| j| jfS rv   )hashrJ   rK   rb   rM   rN   rO   r=   r   r   r   r   __hash__  s   zRSAPrivateNumbers.__hash__rv   )r(   r)   r*   r0   rt   propertyrJ   rK   rb   rM   rN   rO   r=   r1   rA   r   r   r{   r   r   r   r   r   r   r   r!   @  s@    
%
r!   c                   @   sr   e Zd ZdedefddZedd Zedd Z	dd	ej	e
 d
efddZdd Zdd Zdd Zdd ZdS )r<   rS   rT   c                 C   s,   t |tr
t |tstd|| _|| _d S )Nz,RSAPublicNumbers arguments must be integers.)rD   r0   rl   _e_n)r   rS   rT   r   r   r   rt     s   
zRSAPublicNumbers.__init__c                 C   ru   rv   )r   r   r   r   r   rw     rx   zRSAPublicNumbers.<lambda>c                 C   ru   rv   )r   r   r   r   r   rw     rx   NrC   r   c                 C   ry   rv   )r   Zload_rsa_public_numbersrz   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 rv   )rD   r<   r}   rS   rT   r~   r   r   r   r     s   
zRSAPublicNumbers.__eq__c                 C   r   rv   r   r~   r   r   r   r     r   zRSAPublicNumbers.__ne__c                 C   s   t | j| jfS rv   )r   rS   rT   r   r   r   r   r     s   zRSAPublicNumbers.__hash__rv   )r(   r)   r*   r0   rt   r   rS   rT   r1   rA   r   r   r   r   r   r   r   r   r   r   r   r<     s    
r<   rv   )+r+   r1   mathr   Zcryptography.exceptionsr   r   Zcryptography.hazmat.backendsr   Z'cryptography.hazmat.backends.interfacesr   r   Zcryptography.hazmat.primitivesr   r	   Z*cryptography.hazmat.primitives._asymmetricr
   Z)cryptography.hazmat.primitives.asymmetricr   r   r   r3   ABCMetar   ZRSAPrivateKeyWithSerializationr   ZRSAPublicKeyWithSerializationr0   rA   rF   rE   rR   rU   r\   r]   r_   ra   re   Tuplerk   objectr!   r<   r   r   r   r   <module>   sx   8@
	
/
-S