
    ]j,!                       d dl mZ d dlZd dlZd dl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 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.                  j0                         e
j.                  j4                  Ze
j.                  j6                  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lcm)openssl)_serializationhashes)AsymmetricPadding)utilsc                  f   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	)RSAPrivateKeyc                     y)z3
        Decrypts the provided ciphertext.
        N )self
ciphertextpaddings      W/root/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_numbers3   r   r   c                     yz6
        Returns the key serialized as bytes.
        Nr   )r   encodingformatencryption_algorithms       r   private_byteszRSAPrivateKey.private_bytes9   r   r   c                     yz!
        Returns a copy.
        Nr   r   s    r   __copy__zRSAPrivateKey.__copy__D   r   r   c                     yz&
        Returns a deep copy.
        Nr   r   memos     r   __deepcopy__zRSAPrivateKey.__deepcopy__J   r   r   N)r   bytesr   r	   returnr0   r1   intr1   RSAPublicKey)r   r0   r   r	   r   zEasym_utils.Prehashed | hashes.HashAlgorithm | asym_utils.NoDigestInfor1   r0   )r1   RSAPrivateNumbers)r$   _serialization.Encodingr%   z_serialization.PrivateFormatr&   z)_serialization.KeySerializationEncryptionr1   r0   )r1   r   )r.   dictr1   r   )__name__
__module____qualname__abcabstractmethodr   propertyr   r   r   r!   r'   r*   r/   r   r   r   r   r      s%    
   
 	 
 	

 #
"	
 

 
 	 
 	) - H	
 
  	 
 	 r   r   )	metaclassc                     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ej                  dd	       Zy
)r5   c                     y)z/
        Encrypts the given plaintext.
        Nr   )r   	plaintextr   s      r   encryptzRSAPublicKey.encryptV   r   r   c                     yr   r   r   s    r   r   zRSAPublicKey.key_size\   r   r   c                     y)z-
        Returns an RSAPublicNumbers
        Nr   r   s    r   public_numberszRSAPublicKey.public_numbersc   r   r   c                     yr#   r   )r   r$   r%   s      r   public_byteszRSAPublicKey.public_bytesi   r   r   c                     y)z5
        Verifies the signature of the data.
        Nr   )r   	signaturer   r   r   s        r   verifyzRSAPublicKey.verifys   r   r   c                     y)z@
        Recovers the original data from the signature.
        Nr   )r   rJ   r   r   s       r   recover_data_from_signaturez(RSAPublicKey.recover_data_from_signature   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   c                     yr,   r   r-   s     r   r/   zRSAPublicKey.__deepcopy__   r   r   N)rB   r0   r   r	   r1   r0   r2   )r1   RSAPublicNumbers)r$   r7   r%   z_serialization.PublicFormatr1   r0   )
rJ   r0   r   r0   r   r	   r   z+asym_utils.Prehashed | hashes.HashAlgorithmr1   None)rJ   r0   r   r	   r   z5hashes.HashAlgorithm | asym_utils.NoDigestInfo | Noner1   r0   )rO   objectr1   boolr4   )r.   r8   r1   r5   )r9   r:   r;   r<   r=   rC   r>   r   rF   rH   rK   rM   rP   r*   r/   r   r   r   r5   r5   U   se    
   
 	 
 	) , 
	  			 	 #		
 ?	 
	 	 	 # I	
 
  	 
 	 
 	 r   r5   c                Z    t        | |       t        j                  j                  | |      S N)_verify_rsa_parametersrust_opensslrsagenerate_private_key)public_exponentr   backends      r   r\   r\      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)r]   r   s     r   rY   rY      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   _modinvro      sb     FBaqA
a%a|1!b&[!R|1b" a% 6Mr   c                D    | dk  s|dk  rt        d      t        ||       S )zF
    Compute the CRT (q ** -1) % p value from RSA primes p and q.
    rd   Values can't be <= 1)rb   ro   )prl   s     r   rsa_crt_iqmprs      s)     	Ava/001a=r   c                <    | dk  s|dk  rt        d      | |dz
  z  S )zg
    Compute the CRT private_exponent % (p - 1) value from the RSA
    private_exponent (d) and p.
    rd   rq   ra   )private_exponentrr   s     r   rsa_crt_dmp1rv      -    
 1Q/00q1u%%r   c                <    | dk  s|dk  rt        d      | |dz
  z  S )zg
    Compute the CRT private_exponent % (q - 1) value from the RSA
    private_exponent (d) and q.
    rd   rq   ra   )ru   rl   s     r   rsa_crt_dmq1ry      rw   r   c                n    | dk  s
|dk  s|dk  rt        d      t        | t        |dz
  |dz
              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.
    rd   rq   )rb   ro   r   )rf   rr   rl   s      r   rsa_recover_private_exponentr{      s?     	Ava16/001c!a%Q'((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.
    rd   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)rb   pow_MAX_RECOVERY_ATTEMPTSrandomrandintr   re   sorted)nrf   dktottspottedtriesrj   kcandrr   rl   rm   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   rX   )r]   r3   r   r3   r^   z
typing.Anyr1   r   )r]   r3   r   r3   r1   rT   )rf   r3   rg   r3   r1   r3   )rr   r3   rl   r3   r1   r3   )ru   r3   rr   r3   r1   r3   )ru   r3   rl   r3   r1   r3   )rf   r3   rr   r3   rl   r3   r1   r3   )r   r3   rf   r3   r   r3   r1   ztuple[int, int])%
__future__r   r<   r   typingmathr   r   "cryptography.hazmat.bindings._rustr   rZ   cryptography.hazmat.primitivesr   r   *cryptography.hazmat.primitives._asymmetricr	   )cryptography.hazmat.primitives.asymmetricr
   
asym_utilsABCMetar   RSAPrivateKeyWithSerializationregisterr[   r5   RSAPublicKeyWithSerializationr6   rS   r\   rY   ro   rs   rv   ry   r{   r   r   r   r   r   <module>r      s  
 # 
    F A H I<ckk <~ "/    |''55 6ES[[ EP !-    l&&33 4 $$66 ##44  LLL L 	LA
&&)*  -r   