
    ӝgh                    
   d dl mZ d dlZ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  G d d	ej$                  
      ZeZej+                  e	j,                  j&                          G d dej$                  
      ZeZej+                  e	j,                  j.                         e	j,                  j2                  Ze	j,                  j4                  Z	 d	 	 	 	 	 	 	 ddZddZddZddZddZddZ ddZ!dZ"ddZ#y)    )annotationsN)gcd)openssl)_serializationhashes)AsymmetricPadding)utilsc                  @   e Zd Zej                  d	d       Zeej                  d
d              Zej                  dd       Zej                  	 	 	 	 	 	 	 	 dd       Z	ej                  dd       Z
ej                  	 	 	 	 	 	 	 	 dd       Zej                  dd       Zy)RSAPrivateKeyc                     y)z3
        Decrypts the provided ciphertext.
        N )self
ciphertextpaddings      q/var/www/enzed_healthcare/enzed_env/lib/python3.12/site-packages/cryptography/hazmat/primitives/asymmetric/rsa.pydecryptzRSAPrivateKey.decrypt           c                     yz7
        The bit length of the public modulus.
        Nr   r   s    r   key_sizezRSAPrivateKey.key_size   r   r   c                     y)zD
        The RSAPublicKey associated with this private key.
        Nr   r   s    r   
public_keyzRSAPrivateKey.public_key    r   r   c                     y)z!
        Signs the data.
        Nr   )r   datar   	algorithms       r   signzRSAPrivateKey.sign&   r   r   c                     y)z/
        Returns an RSAPrivateNumbers.
        Nr   r   s    r   private_numberszRSAPrivateKey.private_numbers1   r   r   c                     yz6
        Returns the key serialized as bytes.
        Nr   )r   encodingformatencryption_algorithms       r   private_byteszRSAPrivateKey.private_bytes7   r   r   c                     yz!
        Returns a copy.
        Nr   r   s    r   __copy__zRSAPrivateKey.__copy__B   r   r   N)r   bytesr   r   returnr*   r+   intr+   RSAPublicKey)r   r*   r   r   r   +asym_utils.Prehashed | hashes.HashAlgorithmr+   r*   )r+   RSAPrivateNumbers)r#   _serialization.Encodingr$   z_serialization.PrivateFormatr%   z)_serialization.KeySerializationEncryptionr+   r*   )r+   r   )__name__
__module____qualname__abcabstractmethodr   propertyr   r   r   r    r&   r)   r   r   r   r   r      s    
   
 	 
 	 # ?	
 
  	 
 	) - H	
 
  	 r   r   )	metaclassc                  v   e Zd Zej                  d
d       Zeej                  dd              Zej                  dd       Zej                  	 	 	 	 	 	 dd       Z	ej                  	 	 	 	 	 	 	 	 	 	 dd       Z
ej                  	 	 	 	 	 	 	 	 dd       Zej                  dd       Zej                  dd       Zy	)r/   c                     y)z/
        Encrypts the given plaintext.
        Nr   )r   	plaintextr   s      r   encryptzRSAPublicKey.encryptN   r   r   c                     yr   r   r   s    r   r   zRSAPublicKey.key_sizeT   r   r   c                     y)z-
        Returns an RSAPublicNumbers
        Nr   r   s    r   public_numberszRSAPublicKey.public_numbers[   r   r   c                     yr"   r   )r   r#   r$   s      r   public_byteszRSAPublicKey.public_bytesa   r   r   c                     y)z5
        Verifies the signature of the data.
        Nr   )r   	signaturer   r   r   s        r   verifyzRSAPublicKey.verifyk   r   r   c                     y)z@
        Recovers the original data from the signature.
        Nr   )r   rD   r   r   s       r   recover_data_from_signaturez(RSAPublicKey.recover_data_from_signaturew   r   r   c                     y)z"
        Checks equality.
        Nr   )r   others     r   __eq__zRSAPublicKey.__eq__   r   r   c                     yr(   r   r   s    r   r)   zRSAPublicKey.__copy__   r   r   N)r<   r*   r   r   r+   r*   r,   )r+   RSAPublicNumbers)r#   r2   r$   z_serialization.PublicFormatr+   r*   )
rD   r*   r   r*   r   r   r   r0   r+   None)rD   r*   r   r   r   zhashes.HashAlgorithm | Noner+   r*   )rI   objectr+   boolr.   )r3   r4   r5   r6   r7   r=   r8   r   r@   rB   rE   rG   rJ   r)   r   r   r   r/   r/   M   sJ    
   
 	 
 	) , 
	  			 	 #		
 ?	 
	 	 	 # /	
 
  	 
 	 r   r/   c                Z    t        | |       t        j                  j                  | |      S N)_verify_rsa_parametersrust_opensslrsagenerate_private_key)public_exponentr   backends      r   rU   rU      s'    
 ?H500(KKr   c                B    | dvrt        d      |dk  rt        d      y )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 1024-bits.)
ValueError)rV   r   s     r   rR   rR      s6    j(?
 	

 $?@@ r   c                x    d\  }}| |}}|dkD  r(t        ||      \  }}|||z  z
  }||||f\  }}}}|dkD  r(||z  S )zO
    Modular Multiplicative Inverse. Returns x such that: (x*e) mod m == 1
    )   r   r   )divmod)	emx1x2abqrxns	            r   _modinvrg      sb     FBaqA
a%a|1!b&[!R|1b" a% 6Mr   c                    t        ||       S )zF
    Compute the CRT (q ** -1) % p value from RSA primes p and q.
    )rg   )prd   s     r   rsa_crt_iqmprj      s     1a=r   c                    | |dz
  z  S )zg
    Compute the CRT private_exponent % (p - 1) value from the RSA
    private_exponent (d) and p.
    r\   r   )private_exponentri   s     r   rsa_crt_dmp1rm          
 q1u%%r   c                    | |dz
  z  S )zg
    Compute the CRT private_exponent % (q - 1) value from the RSA
    private_exponent (d) and q.
    r\   r   )rl   rd   s     r   rsa_crt_dmq1rp      rn   r   c                V    |dz
  |dz
  z  t        |dz
  |dz
        z  }t        | |      S )z
    Compute the RSA private_exponent (d) given the public exponent (e)
    and the RSA primes p and q.

    This uses the Carmichael totient function to generate the
    smallest possible working value of the private exponent.
    r\   )r   rg   )r^   ri   rd   lambda_ns       r   rsa_recover_private_exponentrs      s7    " A!a% CAq1u$55H1hr   i  c                (   |dk  s|dk  rt        d      dt        d||z  |       k7  rt        d      ||z  dz
  }|}|dz  dk(  r|dz  }|dz  dk(  rd}d}|s|t        k  rxt        j                  d| dz
        }|dz  }|}||k  rGt        |||       }	|	dk7  r*|	| dz
  k7  r"t        |	d|       dk(  rt        |	dz   |       }
d}n|dz  }||k  rG|s
|t        k  rx|st        d	      t        | 
      \  }}|dk(  sJ t        |
|f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.
    r\   zd, e can't be <= 1   zn, d, e don't match   r   FTz2Unable to compute factors p and q from exponent d.)reverse)rZ   pow_MAX_RECOVERY_ATTEMPTSrandomrandintr   r]   sorted)nr^   dktottspottedtriesrb   kcandri   rd   re   s                r   rsa_recover_prime_factorsr      sa    	Ava-..	SQUA.//q519D 	A
a%1*F a%1* GE%"88NN1a!e$
$hq!Q<DqyTa!e_T1aA1E q!$FA $h %"88 MNN!Q<DAq6M61a&$'DAqq6Mr   rQ   )rV   r-   r   r-   rW   z
typing.Anyr+   r   )rV   r-   r   r-   r+   rM   )r^   r-   r_   r-   r+   r-   )ri   r-   rd   r-   r+   r-   )rl   r-   ri   r-   r+   r-   )rl   r-   rd   r-   r+   r-   )r^   r-   ri   r-   rd   r-   r+   r-   )r}   r-   r^   r-   r~   r-   r+   ztuple[int, int])$
__future__r   r6   rz   typingmathr   "cryptography.hazmat.bindings._rustr   rS   cryptography.hazmat.primitivesr   r   *cryptography.hazmat.primitives._asymmetricr   )cryptography.hazmat.primitives.asymmetricr	   
asym_utilsABCMetar   RSAPrivateKeyWithSerializationregisterrT   r/   RSAPublicKeyWithSerializationr1   rL   rU   rR   rg   rj   rm   rp   rs   ry   r   r   r   r   <module>r      s  
 # 
    F A H I4ckk 4n "/    |''55 6?S[[ ?D !-    l&&33 4 $$66 ##44  LLL L 	LA
&& .  -r   