3 M(ÌYô-ã @sÔdZdZdZdZy ddlZWnek r8ddlZYnXddlmZmZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlmZddlZejdeƒejd eƒd d „Zd d „Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„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.dd?„Z0d@dA„Z1dBdC„Z2dDdE„Z3dFdG„Z4edHk�rÂddl5Z5e5j6ƒd/dAdEdCdGgZ7dS)Ia_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-05z1.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.cCs0||krt||ƒS|dkr|St|t||ƒƒS)zNReturns the greatest common divisor of p and q >>> gcd(42, 6) 6 r)ÚgcdÚabs)ÚpÚq©r ú6/private/tmp/pip-build-nl73fm5q/rsa/rsa/_version133.pyr?s  rcCs`t|ƒtjkpt|ƒtjks$tdƒ‚d}x2|D]*}|d9}t|ƒtjkrPt|ƒ}||7}q.W|S)z7Converts a list of bytes or a string to an integer z You must pass a string or a listré)ÚtypeÚtypesZListTypeZ StringTypeÚ TypeErrorÚord)ÚbytesÚintegerrr r r Ú bytes2intJs  rcCsVt|ƒtjkpt|ƒtjks$tdƒ‚d}x(|dkrPdt|d@ƒ|f}|d}q*W|S)z+Converts a number to a string of bytes zYou must pass a long or an intÚrz%s%séÿr )r r ÚLongTypeÚIntTyperr)ÚnumberÚstringr r r Ú int2bytesZs  rcCsZ||}g}x"|dkr.|j|d@ƒ|d?}qWx$|rT|jƒ}|||d|}q2W|S)zCalculates r = a^p mod n éé)ÚappendÚpop)ÚarÚnÚresultZ remaindersÚremr r r Úfast_exponentiationis  r"cCst|dƒ}tj|ƒ}t|ƒS)zPReads a random integer of approximately nbits bits rounded up to whole bytesg @)ÚceilÚosÚurandomr)ÚnbitsÚnbytesZ randomdatar r r Úread_random_intvs  r(cCsttj|ƒƒS)zceil(x) -> int(math.ceil(x)))ÚintÚmathr#)Úxr r r r#~sr#cCsNd}||}ttj|dƒdƒ}t|d|dƒ}tj||ƒ}t|ƒ||S)z9Returns a random integer x with minvalue <= x <= maxvalueé rg @é)r#r*ÚlogÚmaxÚrandomÚrandintr()ZminvalueÚmaxvalueZ min_nbitsÚrangeZ rangebytesZ rangebitsr&r r r r1ƒs  r1cCstd|dƒ}t||d|ƒS)zPReturns 1 if p may be prime, and something else if p definitely is not primer)r1r")rrr r r Úfermat_little_theorem˜sr4cCs~||dkrdSd}xd|dkrx|d@rT|d|dd?d@rD| }|||}}q|ddd?d@rn| }|d?}qW|S)z4Calculates the value of the Jacobi symbol (a/b) rrrér )rÚbr r r r ÚjacobiŸs   r7cCs2t||ƒ|}t||dd|ƒ}||kr.dSdS)zUReturns False if n is an Euler pseudo-prime with base x, and True otherwise. rrFT)r7r")r+rÚjÚfr r r Újacobi_witness±s r:cCsRd}t|tjd|dƒƒ}x0t|dƒD] }td|dƒ}t||ƒr*dSq*WdS)zÓCalculates 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à?rrFT)r#r*r.r3r1r:)rÚkrÚtÚir+r r r Úrandomized_primality_testing¼s r>cCst|dƒrdSdS)z>Returns True if the number is prime, and False otherwise. éTF)r>)rr r r Úis_primeÎs r@cCs6ttj|dƒƒ}xt|ƒ}|dO}t|ƒrPqW|S)zxReturns a prime number of max. 'math.ceil(nbits/8)*8' bits. In other words: nbits is rounded up to whole bytes. g @r)r)r*r#r(r@)r&r'rr r r ÚgetprimeàsrAcCst||ƒ}|dkS)zQReturns True if a and b are relatively prime, and False if they are not. r)r)rr6Údr r r Úare_relatively_primeôs rCcCs(t|ƒ}xt|ƒ}||ks Pq W||fS)z5Returns a tuple of two different primes of nbits bits)rA)r&rrr r r Úfind_p_qüs rDcCsL|dkr|ddfSt||ƒ}t||ƒ}t||ƒ\}}}|||||fS)z@Returns a tuple (d, i, j) such that d = gcd(a, b) = ia + jb rr)rÚlongÚextended_euclid_gcd)rr6rÚrrBr;Úlr r r rFs    rFc Cs˜||}|d|d}x,ttd|dƒƒ}t||ƒrt||ƒrPqWt||ƒ\}}}|dksntd||fƒ‚|||dks�td|||fƒ‚||fS)zaCalculates an encryption and a decryption key for p and q, and returns them as a tuple (e, d)rr-rz.e (%d) and phi_n (%d) are not relatively primez6e (%d) and i (%d) are not mult. inv. modulo phi_n (%d))rAr/rCrFÚ Exception) rrr&rZphi_nÚerBr=r8r r r Úcalculate_keyssrKcCs8x*t|ƒ\}}t|||ƒ\}}|dkrPqW||||fS)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)rDrK)r&rrrJrBr r r Úgen_keys,s  rLcCs*t|ƒ\}}}}|||dœ|||dœfS)zÌGenerates 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: ....). )rJr)rBrr)rL)r&rrrJrBr r r Úgen_pubpriv_keys<srMcCstt|ƒtjkrtt|ƒ||ƒSt|ƒtjk r4tdƒ‚|dkrhtjtj |dƒƒtjtj |dƒƒkrht dƒ‚t |||ƒS)zDEncrypts a message using encryption key 'ekey', working modulo nzYou must pass a long or an intrrzThe message is too long) r r rÚ encrypt_intrErrr*Úfloorr.Ú OverflowErrorr")ÚmessageÚekeyrr r r rNHs$rNcCs t|||ƒS)zLDecrypts a cypher text using the decryption key 'dkey', working modulo n)rN)Z cyphertextÚdkeyrr r r Ú decrypt_intXsrTcCs t|||ƒS)z2Signs 'message' using key 'dkey', working modulo n)rT)rQrSrr r r Úsign_int^srUcCs t|||ƒS)z4verifies 'signed' using key 'ekey', working modulo n)rN)ÚsignedrRrr r r Ú verify_intcsrWcCs tjt|ƒƒ}tj|ƒ}|jƒS)z-Pickles and base64encodes it's argument chops)ÚzlibÚcompressrÚbase64Ú encodestringÚstrip)ÚchopsÚvalueÚencodedr r r Ú picklechopshs r`cCsttjtj|ƒƒƒS)z:base64decodes and unpickes it's argument string into chops)rrXÚ decompressrZÚ decodestring)rr r r Ú unpicklechopsosrccCsšt|ƒ}|d}ttjtj|dƒƒƒ}|d}||}||dkrJ|d7}g} xBt|ƒD]6} | |} || | |…} t| ƒ} | j|| ||ƒƒqXWt| ƒS)zºSplits '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-rrr) Úlenr)r*rOr.r3rrr`)rQÚkeyrÚfuncrefZmsglenZmbitsr&r'ÚblocksÚcypherZbindexÚoffsetÚblockr^r r r Ú chopstringts rkcCs6d}t|ƒ}x$|D]}||||ƒ}|t|ƒ7}qW|S)z…Glues chops back together into a string. calls funcref(integer, key, n) for each chop. Used by 'decrypt' and 'verify'. r)rcr)r]rerrfrQZcpartZmpartr r r Ú gluechops�s   rlcCst||d|dtƒS)z5Encrypts a string 'message' with the public key 'key'rJr)rkrN)rQrer r r ÚencryptŸsrmcCst||d|d|dtƒS)z3Signs a string 'message' with the private key 'key'rBrr)rkrT)rQrer r r Úsign¤srncCst||d|d|dtƒS)z,Decrypts a cypher with the private key 'key'rBrr)rlrT)rhrer r r Údecrypt©srocCst||d|dtƒS)z+Verifies a cypher with the public key 'key'rJr)rlrN)rhrer r r Úverify®srpÚ__main__)8Ú__doc__Ú __author__Ú__date__Ú __version__ÚcPickleÚpickleÚ ImportErrorrrrZr*r$r0Úsysr rXZ rsa._compatrÚwarningsÚwarnÚ__name__ÚDeprecationWarningrrrr"r(r#r1r4r7r:r>r@rArCrDrFrKrLrMrNrTrUrWr`rcrkrlrmrnrorpÚdoctestÚtestmodÚ__all__r r r r Úsp