U
    c6{io   ã                   @  s`  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	„ d	ejd
�ZeZe e	jj¡ G dd„ dejd
�ZeZe e	jj¡ e	jjZe	jjZd*dddd	dœdd„Zddddœdd„Zddddœdd„Zddddœdd„Zddddœdd„Zddddœd d!„Z ddddd"œd#d$„Z!d%Z"dddd&d'œd(d)„Z#dS )+é    )ÚannotationsN)Úgcd)Úopenssl)Ú_serializationÚhashes)ÚAsymmetricPadding)Úutilsc                   @  s¬   e Zd Zejddddœdd„ƒZeejddœdd	„ƒƒZejd
dœdd„ƒZejdddddœdd„ƒZ	ejddœdd„ƒZ
ejdddddœdd„ƒZejd dœdd„ƒZdS )ÚRSAPrivateKeyÚbytesr   )Ú
ciphertextÚpaddingÚreturnc                 C  s   dS )z3
        Decrypts the provided ciphertext.
        N© )Úselfr   r   r   r   úQ/tmp/pip-unpacked-wheel-6eg_dtv9/cryptography/hazmat/primitives/asymmetric/rsa.pyÚdecrypt   s    zRSAPrivateKey.decryptÚint©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ú+asym_utils.Prehashed | hashes.HashAlgorithm)Údatar   Ú	algorithmr   c                 C  s   dS )z!
        Signs the data.
        Nr   )r   r   r   r   r   r   r   Úsign&   s    zRSAPrivateKey.signÚRSAPrivateNumbersc                 C  s   dS )z/
        Returns an RSAPrivateNumbers.
        Nr   r   r   r   r   Úprivate_numbers1   s    zRSAPrivateKey.private_numbersú_serialization.Encodingz_serialization.PrivateFormatz)_serialization.KeySerializationEncryption)Ú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_bytes7   s    zRSAPrivateKey.private_bytesc                 C  s   dS ©z!
        Returns a copy.
        Nr   r   r   r   r   Ú__copy__B   s    zRSAPrivateKey.__copy__N)Ú__name__Ú
__module__Ú__qualname__ÚabcÚabstractmethodr   Úpropertyr   r   r   r   r$   r&   r   r   r   r   r	      s   

r	   )Ú	metaclassc                   @  sÈ   e Zd Zejddddœdd„ƒZeejddœdd	„ƒƒZejd
dœdd„ƒZejddddœdd„ƒZ	ejddddddœdd„ƒZ
ejdddddœdd„ƒZejdddœdd„ƒZejd dœd d!„ƒZd"S )#r   r
   r   )Ú	plaintextr   r   c                 C  s   dS )z/
        Encrypts the given plaintext.
        Nr   )r   r.   r   r   r   r   ÚencryptN   s    zRSAPublicKey.encryptr   r   c                 C  s   dS r   r   r   r   r   r   r   T   s    zRSAPublicKey.key_sizeÚRSAPublicNumbersc                 C  s   dS )z-
        Returns an RSAPublicNumbers
        Nr   r   r   r   r   Úpublic_numbers[   s    zRSAPublicKey.public_numbersr   z_serialization.PublicFormat)r    r!   r   c                 C  s   dS r#   r   )r   r    r!   r   r   r   Úpublic_bytesa   s    zRSAPublicKey.public_bytesr   ÚNone)Ú	signaturer   r   r   r   c                 C  s   dS )z5
        Verifies the signature of the data.
        Nr   )r   r4   r   r   r   r   r   r   Úverifyk   s    zRSAPublicKey.verifyzhashes.HashAlgorithm | None)r4   r   r   r   c                 C  s   dS )z@
        Recovers the original data from the signature.
        Nr   )r   r4   r   r   r   r   r   Úrecover_data_from_signaturew   s    z(RSAPublicKey.recover_data_from_signatureÚobjectÚbool)Úotherr   c                 C  s   dS )z"
        Checks equality.
        Nr   )r   r9   r   r   r   Ú__eq__‚   s    zRSAPublicKey.__eq__c                 C  s   dS r%   r   r   r   r   r   r&   ˆ   s    zRSAPublicKey.__copy__N)r'   r(   r)   r*   r+   r/   r,   r   r1   r2   r5   r6   r:   r&   r   r   r   r   r   M   s"   	
r   r   z
typing.Any)Úpublic_exponentr   Úbackendr   c                 C  s   t | |ƒ tj | |¡S )N)Ú_verify_rsa_parametersÚrust_opensslÚrsaÚgenerate_private_key)r;   r   r<   r   r   r   r@   –   s    
r@   r3   )r;   r   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 1024-bits.©Ú
ValueError)r;   r   r   r   r   r=   Ÿ   s    ÿr=   )ÚeÚmr   c           	      C  sR   d\}}| | }}|dkrJt ||ƒ\}}|||  }||||f\}}}}q|| S )zO
    Modular Multiplicative Inverse. Returns x such that: (x*e) mod m == 1
    )é   r   r   )Údivmod)	rD   rE   Úx1Zx2ÚaÚbÚqÚrZxnr   r   r   Ú_modinvª   s    
rM   )ÚprK   r   c                 C  s"   | dks|dkrt dƒ‚t|| ƒS )zF
    Compute the CRT (q ** -1) % p value from RSA primes p and q.
    rF   úValues can't be <= 1)rC   rM   )rN   rK   r   r   r   Úrsa_crt_iqmp·   s    rP   )Úprivate_exponentrN   r   c                 C  s$   | dks|dkrt dƒ‚| |d  S )zg
    Compute the CRT private_exponent % (p - 1) value from the RSA
    private_exponent (d) and p.
    rF   rO   rB   )rQ   rN   r   r   r   Úrsa_crt_dmp1À   s    rR   )rQ   rK   r   c                 C  s$   | dks|dkrt dƒ‚| |d  S )zg
    Compute the CRT private_exponent % (q - 1) value from the RSA
    private_exponent (d) and q.
    rF   rO   rB   )rQ   rK   r   r   r   Úrsa_crt_dmq1Ê   s    rS   )rD   rN   rK   r   c                 C  sL   | dks|dks|dkr t dƒ‚|d |d  t|d |d ƒ }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.
    rF   rO   )rC   r   rM   )rD   rN   rK   Zlambda_nr   r   r   Úrsa_recover_private_exponentÔ   s    "rT   iô  ztuple[int, int])ÚnrD   Údr   c                 C  s&  |dks|dkrt dƒ‚dtd|| | ƒkr4t dƒ‚|| d }|}|d dkrZ|d }qDd}d}|sâ|tk rât d| d ¡}|d7 }|}||k rbt||| ƒ}	|	dkrÖ|	| d krÖt|	d| ƒdkrÖt|	d | ƒ}
d}qb|d9 }qŠqb|sît d	ƒ‚t| |
ƒ\}}|dk�s
t‚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.
    rF   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)	rC   ÚpowÚ_MAX_RECOVERY_ATTEMPTSÚrandomÚrandintr   rG   ÚAssertionErrorÚsorted)rU   rD   rV   ZktotÚtZspottedÚtriesrI   ÚkZcandrN   rK   rL   r   r   r   Úrsa_recover_prime_factorsð   s6    
$rc   )N)$Ú
__future__r   r*   r\   ÚtypingÚmathr   Z"cryptography.hazmat.bindings._rustr   r>   Zcryptography.hazmat.primitivesr   r   Z*cryptography.hazmat.primitives._asymmetricr   Z)cryptography.hazmat.primitives.asymmetricr   Z
asym_utilsÚABCMetar	   ZRSAPrivateKeyWithSerializationÚregisterr?   r   ZRSAPublicKeyWithSerializationr   r0   r@   r=   rM   rP   rR   rS   rT   r[   rc   r   r   r   r   Ú<module>   s4   7B ý		

