a
    db-                     @   s  d Z dZdZdZzddlZW n ey6   ddlZY n0 ddlmZmZ ddl	Z	ddl
Z
ddlZddlZddlZddlZddlZddlmZ ddlZede  ed	e d
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Z d d! Z!d"d# Z"d$d% Z#d&d' Z$d(d) Z%d*d+ Z&d,d- Z'd.d/ Z(d0d1 Z)d2d3 Z*d4d5 Z+d6d7 Z,d8d9 Z-d:d; Z.d<d= Z/d>d? Z0d@dA Z1dBdC Z2dDdE Z3dFdG Z4edHkrddl5Z5e56  g dIZ7dS )Ja_  Deprecated version of the RSA module

.. deprecated:: 2.0

    This submodule is deprecated and will be completely removed as of version 4.0.

Module for calculating large primes, and RSA encryption, decryption,
signing and verification. Includes generating public and private keys.

WARNING: this code implements the mathematics of RSA. It is not suitable for
real-world secure cryptography purposes. It has not been reviewed by a security
expert. It does not include padding of data. There are many ways in which the
output of this module, when used without any modification, can be sucessfully
attacked.
z.Sybren Stuvel, Marloes de Boer and Ivo Tamboerz
2010-02-05z1.3.3    N)dumpsloads)bytez@Insecure version of the RSA module is imported as %s, be carefulzNThis submodule is deprecated and will be completely removed as of version 4.0.c                 C   s0   | |k rt || S |dkr| S t |t| | S )zNReturns the greatest common divisor of p and q


    >>> gcd(42, 6)
    6
    r   )gcdabs)pq r	   `/home/tom/ab/renpy-build/tmp/install.linux-x86_64/lib/python3.9/site-packages/rsa/_version133.pyr   ?   s    r   c                 C   s\   t | tju s$t | tju s$tdd}| D ]*}|d9 }t |tju rNt|}||7 }q,|S )z7Converts a list of bytes or a string to an integer
    z You must pass a string or a listr      )typetypesZListTypeZ
StringType	TypeErrorord)bytesintegerr   r	   r	   r
   	bytes2intJ   s    
r   c                 C   sR   t | tju s$t | tju s$tdd}| dkrNdt| d@ |f }| d } q(|S )z+Converts a number to a string of bytes
    You must pass a long or an int r   z%s%s   r   )r   r   LongTypeIntTyper   r   )numberstringr	   r	   r
   	int2bytesZ   s    
r   c                 C   sR   | | }g }|dkr,| |d@  |d? }q|rN| }| | |d  | }q,|S )zCalculates r = a^p mod n
          )appendpop)ar   nresultZ
remaindersremr	   r	   r
   fast_exponentiationi   s    
r#   c                 C   s   t | d }t|}t|S )zPReads a random integer of approximately nbits bits rounded up
    to whole bytes       @)ceilosurandomr   )nbitsnbytesZ
randomdatar	   r	   r
   read_random_intv   s    
r*   c                 C   s   t t| S )zceil(x) -> int(math.ceil(x)))intmathr%   )xr	   r	   r
   r%   ~   s    r%   c                 C   sN   d}||  }t t|dd }t|d |d }t||}t|| |  S )z9Returns a random integer x with minvalue <= x <= maxvalue    r   r$      )r%   r,   logmaxrandomrandintr*   )ZminvalueZmaxvalueZ	min_nbitsrangeZ
rangebytesZ	rangebitsr(   r	   r	   r
   r3      s    r3   c                 C   s   t d| d }t|| d | S )zPReturns 1 if p may be prime, and something else if p definitely
    is not primer   )r3   r#   )r   r   r	   r	   r
   fermat_little_theorem   s    r5   c                 C   sz   | | dkrdS d}| dkrv| d@ rR| d |d  d? d@ rB| }| ||   }} q|d d d? d@ rl| }| d? } q|S )z4Calculates the value of the Jacobi symbol (a/b)
    r   r   r      r	   )r   br!   r	   r	   r
   jacobi   s    
r8   c                 C   s2   t | || }t| |d d |}||kr.dS dS )zUReturns False if n is an Euler pseudo-prime with base x, and
    True otherwise.
    r   r   FT)r8   r#   )r-   r    jfr	   r	   r
   jacobi_witness   s    r;   c                 C   sP   d}t |td| d }t|d D ]"}td| d }t|| r( dS q(dS )zCalculates whether n is composite (which is always correct) or
    prime (which is incorrect with error probability 2**-k)

    Returns False if the number if composite, and True if it's
    probably prime.
    g      ?r   r   FT)r%   r,   r0   r4   r3   r;   )r    kr   tir-   r	   r	   r
   randomized_primality_testing   s    r?   c                 C   s   t | drdS dS )z>Returns True if the number is prime, and False otherwise.
       TF)r?   )r   r	   r	   r
   is_prime   s    

rA   c                 C   s2   t t| d }t| }|dO }t|rq.q|S )zxReturns a prime number of max. 'math.ceil(nbits/8)*8' bits. In
    other words: nbits is rounded up to whole bytes.
    r$   r   )r+   r,   r%   r*   rA   )r(   r)   r   r	   r	   r
   getprime   s
    rB   c                 C   s   t | |}|dkS )zQReturns True if a and b are relatively prime, and False if they
    are not.
    r   )r   )r   r7   dr	   r	   r
   are_relatively_prime   s    
rD   c                 C   s$   t | }t | }||ksqq||fS )z5Returns a tuple of two different primes of nbits bits)rB   )r(   r   r   r	   r	   r
   find_p_q   s    rE   c                 C   sL   |dkr| ddfS t | | }t| | }t||\}}}|||||  fS )z@Returns a tuple (d, i, j) such that d = gcd(a, b) = ia + jb
    r   r   )r   longextended_euclid_gcd)r   r7   r   rrC   r<   lr	   r	   r
   rG     s    
rG   c           	      C   s   | | }| d |d  }t td|d }t||rt||rqBqt||\}}}|dksjtd||f || | dkstd|||f ||fS )zaCalculates an encryption and a decryption key for p and q, and
    returns them as a tuple (e, d)r   r/   r   z.e (%d) and phi_n (%d) are not relatively primez6e (%d) and i (%d) are not mult. inv. modulo phi_n (%d))rB   r1   rD   rG   	Exception)	r   r   r(   r    Zphi_nerC   r>   r9   r	   r	   r
   calculate_keys  s    rL   c                 C   s4   t | \}}t||| \}}|dkr q(q ||||fS )z|Generate RSA keys of nbits bits. Returns (p, q, e, d).

    Note: this can take a long time, depending on the key size.
    r   )rE   rL   r(   r   r   rK   rC   r	   r	   r
   gen_keys,  s    rN   c                 C   s*   t | \}}}}||| d|||dfS )zGenerates public and private keys, and returns them as (pub,
    priv).

    The public key consists of a dict {e: ..., , n: ....). The private
    key consists of a dict {d: ...., p: ...., q: ....).
    )rK   r    )rC   r   r   )rN   rM   r	   r	   r
   gen_pubpriv_keys<  s    rO   c                 C   st   t | tju rtt| ||S t | tjur4td| dkrhtt	| dtt	|dkrht
dt| ||S )zDEncrypts a message using encryption key 'ekey', working modulo
    nr   r   r   zThe message is too long)r   r   r   encrypt_intrF   r   r   r,   floorr0   OverflowErrorr#   )messageekeyr    r	   r	   r
   rP   H  s    "rP   c                 C   s   t | ||S )zLDecrypts a cypher text using the decryption key 'dkey', working
    modulo nrP   )Z
cyphertextdkeyr    r	   r	   r
   decrypt_intX  s    rW   c                 C   s   t | ||S )z2Signs 'message' using key 'dkey', working modulo n)rW   )rS   rV   r    r	   r	   r
   sign_int^  s    rX   c                 C   s   t | ||S )z4verifies 'signed' using key 'ekey', working modulo nrU   )signedrT   r    r	   r	   r
   
verify_intc  s    rZ   c                 C   s    t t| }t|}| S )z-Pickles and base64encodes it's argument chops)zlibcompressr   base64encodestringstrip)chopsvalueencodedr	   r	   r
   picklechopsh  s    
rc   c                 C   s   t tt| S )z:base64decodes and unpickes it's argument string into chops)r   r[   
decompressr]   decodestring)r   r	   r	   r
   unpicklechopso  s    rf   c                 C   s   t | }|d }ttt|d}|d }|| }|| dkrJ|d7 }g }	t|D ]6}
|
| }| |||  }t|}|	|||| qVt|	S )zSplits 'message' into chops that are at most as long as n,
    converts these into integers, and calls funcref(integer, key, n)
    for each chop.

    Used by 'encrypt' and 'sign'.
    r/   r   r   r   )	lenr+   r,   rQ   r0   r4   r   r   rc   )rS   keyr    funcrefZmsglenZmbitsr(   r)   blockscypherZbindexoffsetblockra   r	   r	   r
   
chopstringt  s    rn   c                 C   s2   d}t | } | D ]}||||}|t|7 }q|S )zGlues chops back together into a string.  calls
    funcref(integer, key, n) for each chop.

    Used by 'decrypt' and 'verify'.
    r   )rf   r   )r`   rh   r    ri   rS   ZcpartZmpartr	   r	   r
   	gluechops  s    ro   c                 C   s   t | |d |d tS )z5Encrypts a string 'message' with the public key 'key'rK   r    )rn   rP   rS   rh   r	   r	   r
   encrypt  s    rq   c                 C   s   t | |d |d |d  tS )z3Signs a string 'message' with the private key 'key'rC   r   r   )rn   rW   rp   r	   r	   r
   sign  s    rr   c                 C   s   t | |d |d |d  tS )z,Decrypts a cypher with the private key 'key'rC   r   r   )ro   rW   rk   rh   r	   r	   r
   decrypt  s    rt   c                 C   s   t | |d |d tS )z+Verifies a cypher with the public key 'key'rK   r    )ro   rP   rs   r	   r	   r
   verify  s    ru   __main__)rO   rq   rt   rr   ru   )8__doc__
__author____date____version__cPicklepickleImportErrorr   r   r]   r,   r&   r2   sysr   r[   Zrsa._compatr   warningswarn__name__DeprecationWarningr   r   r   r#   r*   r%   r3   r5   r8   r;   r?   rA   rB   rD   rE   rG   rL   rN   rO   rP   rW   rX   rZ   rc   rf   rn   ro   rq   rr   rt   ru   doctesttestmod__all__r	   r	   r	   r
   <module>   st   

