U
    üS‚gô  ã                   @  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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-xwsikbwl/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_bytesN)Ú__name__Ú
__module__Ú__qualname__ÚabcÚabstractmethodr   Úpropertyr   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d S )!r   r
   r   )Ú	plaintextr   r   c                 C  s   dS )z/
        Encrypts the given plaintext.
        Nr   )r   r,   r   r   r   r   ÚencryptH   s    zRSAPublicKey.encryptr   r   c                 C  s   dS r   r   r   r   r   r   r   N   s    zRSAPublicKey.key_sizeÚRSAPublicNumbersc                 C  s   dS )z-
        Returns an RSAPublicNumbers
        Nr   r   r   r   r   Úpublic_numbersU   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_bytes[   s    zRSAPublicKey.public_bytesr   ÚNone)Ú	signaturer   r   r   r   c                 C  s   dS )z5
        Verifies the signature of the data.
        Nr   )r   r2   r   r   r   r   r   r   Úverifye   s    zRSAPublicKey.verifyzhashes.HashAlgorithm | None)r2   r   r   r   c                 C  s   dS )z@
        Recovers the original data from the signature.
        Nr   )r   r2   r   r   r   r   r   Úrecover_data_from_signatureq   s    z(RSAPublicKey.recover_data_from_signatureÚobjectÚbool)Úotherr   c                 C  s   dS )z"
        Checks equality.
        Nr   )r   r7   r   r   r   Ú__eq__|   s    zRSAPublicKey.__eq__N)r%   r&   r'   r(   r)   r-   r*   r   r/   r0   r3   r4   r8   r   r   r   r   r   G   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)r9   r   r:   r   r   r   r>   Š   s    
r>   r1   )r9   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)r9   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)	rA   rB   Úx1Zx2ÚaÚbÚqÚrZxnr   r   r   Ú_modinvž   s    
rJ   )ÚprH   r   c                 C  s
   t || ƒS )zF
    Compute the CRT (q ** -1) % p value from RSA primes p and q.
    )rJ   )rK   rH   r   r   r   Úrsa_crt_iqmp«   s    rL   )Úprivate_exponentrK   r   c                 C  s   | |d  S )zg
    Compute the CRT private_exponent % (p - 1) value from the RSA
    private_exponent (d) and p.
    rC   r   )rM   rK   r   r   r   Úrsa_crt_dmp1²   s    rN   )rM   rH   r   c                 C  s   | |d  S )zg
    Compute the CRT private_exponent % (q - 1) value from the RSA
    private_exponent (d) and q.
    rC   r   )rM   rH   r   r   r   Úrsa_crt_dmq1º   s    rO   )rA   rK   rH   r   c                 C  s,   |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.
    rC   )r   rJ   )rA   rK   rH   Zlambda_nr   r   r   Úrsa_recover_private_exponentÂ   s    "rP   iô  ztuple[int, int])ÚnrA   Údr   c                 C  s  dt d|| | ƒkrtdƒ‚|| d }|}|d dkrB|d }q,d}d}|sÊ|tk rÊt d| d ¡}|d7 }|}||k rJt ||| ƒ}	|	dkr¾|	| d kr¾t |	d| ƒdkr¾t|	d | ƒ}
d}qJ|d9 }qrqJ|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.
    é   zn, d, e don't matchrC   é   r   FTz2Unable to compute factors p and q from exponent d.)Úreverse)	Úpowr@   Ú_MAX_RECOVERY_ATTEMPTSÚrandomÚrandintr   rD   ÚAssertionErrorÚsorted)rQ   rA   rR   ZktotÚtZspottedÚtriesrF   ÚkZcandrK   rH   rI   r   r   r   Úrsa_recover_prime_factorsÜ   s2    
$r_   )N)$Ú
__future__r   r(   rX   Ú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   r.   r>   r;   rJ   rL   rN   rO   rP   rW   r_   r   r   r   r   Ú<module>   s4   1< ý	