ó |~úVc@sDdZdZdZdZyddlZWnek rGddlZnXddlmZmZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlmZddlZejdeƒejd eƒd „Zd „Zd „Zd „Zd„Zd„Zd„Zd„Zd„Zd„Zd„Z d„Z!d„Z"d„Z#d„Z$d„Z%d„Z&d„Z'd„Z(d„Z)d„Z*d„Z+d „Z,d!„Z-d"„Z.d#„Z/d$„Z0d%„Z1d&„Z2d'„Z3d(„Z4ed)kr+ddl5Z5e5j6ƒnd*d+d,d-d.gZ7dS(/s_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. s.Sybren Stuvel, Marloes de Boer and Ivo Tamboers 2010-02-05s1.3.3iÿÿÿÿN(tdumpstloads(tbytes@Insecure version of the RSA module is imported as %s, be carefulsNThis submodule is deprecated and will be completely removed as of version 4.0.cCs@||krt||ƒS|dkr)|St|t||ƒƒS(sNReturns the greatest common divisor of p and q >>> gcd(42, 6) 6 i(tgcdtabs(tptq((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pyR?s   cCsŒt|ƒtjkp't|ƒtjks9tdƒ‚nd}xF|D]>}|d9}t|ƒtjkrzt|ƒ}n||7}qFW|S(s7Converts a list of bytes or a string to an integer s You must pass a string or a listii(ttypettypestListTypet StringTypet TypeErrortord(tbytestintegerR((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pyt bytes2intJs*  cCszt|ƒtjkp't|ƒtjks9tdƒ‚nd}x4|dkrudt|d@ƒ|f}|d:}qBW|S(s+Converts a number to a string of bytes sYou must pass a long or an inttis%s%siÿi(RRtLongTypetIntTypeR R(tnumbertstring((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pyt int2bytesZs*cCsq||}g}x+|dkr=|j|d@ƒ|d?}qWx,|rl|jƒ}|||d|}qAW|S(sCalculates r = a^p mod n ii(tappendtpop(taRtntresultt remainderstrem((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pytfast_exponentiationis   cCs)t|dƒ}tj|ƒ}t|ƒS(sPReads a random integer of approximately nbits bits rounded up to whole bytesg @(tceiltosturandomR(tnbitstnbytest randomdata((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pytread_random_intvscCsttj|ƒƒS(sceil(x) -> int(math.ceil(x))(tinttmathR(tx((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pyR~scCsgd}||}ttj|dƒdƒ}t|d|dƒ}tj||ƒ}t|ƒ||S(s9Returns a random integer x with minvalue <= x <= maxvaluei ig @i(RR&tlogtmaxtrandomtrandintR$(tminvaluetmaxvaluet min_nbitstranget rangebytest rangebitsR!((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pyR+ƒs  cCs'td|dƒ}t||d|ƒS(sPReturns 1 if p may be prime, and something else if p definitely is not primei(R+R(RR((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pytfermat_little_theorem˜scCs||dkrdSd}x||dkr˜|d@rk|d|dd?d@rW| }n|||}}q|ddd?d@r‹| }n|d?}qW|S(s4Calculates the value of the Jacobi symbol (a/b) iiii((RtbR((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pytjacobiŸs   cCsAt||ƒ|}t||dd|ƒ}||kr=tStS(sUReturns False if n is an Euler pseudo-prime with base x, and True otherwise. ii(R4RtFalsetTrue(R'Rtjtf((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pytjacobi_witness±s  cCskd}t|tjd|dƒƒ}x>t|dƒD],}td|dƒ}t||ƒr7tSq7WtS(sÓ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à?ii(RR&R(R/R+R9R5R6(RtkRtttiR'((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pytrandomized_primality_testing¼s cCst|dƒrtStS(s>Returns True if the number is prime, and False otherwise. i(R=R6R5(R((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pytis_primeÎs cCsPttj|dƒƒ}x0trKt|ƒ}|dO}t|ƒrPqqW|S(sxReturns a prime number of max. 'math.ceil(nbits/8)*8' bits. In other words: nbits is rounded up to whole bytes. g @i(R%R&RR6R$R>(R!R"R((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pytgetprimeàs    cCst||ƒ}|dkS(sQReturns True if a and b are relatively prime, and False if they are not. i(R(RR3td((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pytare_relatively_primeôscCs?t|ƒ}x&tr4t|ƒ}||ksPqqW||fS(s5Returns a tuple of two different primes of nbits bits(R?R6(R!RR((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pytfind_p_qüs     cCsf|dkr|ddfSt||ƒ}t||ƒ}t||ƒ\}}}|||||fS(s@Returns a tuple (d, i, j) such that d = gcd(a, b) = ia + jb ii(Rtlongtextended_euclid_gcd(RR3RtrR@R:tl((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pyRDs   c CsÛ||}|d|d}xEtrcttd|dƒƒ}t||ƒrt||ƒrPqqWt||ƒ\}}}|dks¡td||fƒ‚n|||dksÑtd|||fƒ‚n||fS(saCalculates an encryption and a decryption key for p and q, and returns them as a tuple (e, d)iiis.e (%d) and phi_n (%d) are not relatively primes6e (%d) and i (%d) are not mult. inv. modulo phi_n (%d)(R6R?R)RARDt Exception( RRR!Rtphi_nteR@R<R7((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pytcalculate_keyss   cCsWxDtrFt|ƒ\}}t|||ƒ\}}|dkrPqqW||||fS(s|Generate RSA keys of nbits bits. Returns (p, q, e, d). Note: this can take a long time, depending on the key size. i(R6RBRJ(R!RRRIR@((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pytgen_keys,s   cCsIt|ƒ\}}}}i|d6||d6i|d6|d6|d6fS(sÌ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: ....). RIRR@RR(RK(R!RRRIR@((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pytgen_pubpriv_keys<scCs°t|ƒtjkr+tt|ƒ||ƒSt|ƒtjk rOtdƒ‚n|dkr tjtj |dƒƒtjtj |dƒƒkr t dƒ‚nt |||ƒS(sDEncrypts a message using encryption key 'ekey', working modulo nsYou must pass a long or an intiisThe message is too long( RRRt encrypt_intRCRR R&tfloorR(t OverflowErrorR(tmessagetekeyR((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pyRMHs 6cCst|||ƒS(sLDecrypts a cypher text using the decryption key 'dkey', working modulo n(RM(t cyphertexttdkeyR((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pyt decrypt_intXscCst|||ƒS(s2Signs 'message' using key 'dkey', working modulo n(RT(RPRSR((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pytsign_int^scCst|||ƒS(s4verifies 'signed' using key 'ekey', working modulo n(RM(tsignedRQR((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pyt verify_intcscCs.tjt|ƒƒ}tj|ƒ}|jƒS(s-Pickles and base64encodes it's argument chops(tzlibtcompressRtbase64t encodestringtstrip(tchopstvaluetencoded((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pyt picklechopshscCsttjtj|ƒƒƒS(s:base64decodes and unpickes it's argument string into chops(RRXt decompressRZt decodestring(R((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pyt unpicklechopsoscCsÏt|ƒ}|d}ttjtj|dƒƒƒ}|d}||}||dkrh|d7}ng} xTt|ƒD]F} | |} || | |!} t| ƒ} | j|| ||ƒƒq{Wt| ƒS(sº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'. iiii( tlenR%R&RNR(R/RRR`(RPtkeyRtfuncreftmsglentmbitsR!R"tblockstcyphertbindextoffsettblockR^((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pyt chopstringts  !     cCsId}t|ƒ}x0|D](}||||ƒ}|t|ƒ7}qW|S(s…Glues chops back together into a string. calls funcref(integer, key, n) for each chop. Used by 'decrypt' and 'verify'. R(RcR(R]ReRRfRPtcparttmpart((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pyt gluechopss   cCst||d|dtƒS(s5Encrypts a string 'message' with the public key 'key'RIR(RnRM(RPRe((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pytencryptŸscCs#t||d|d|dtƒS(s3Signs a string 'message' with the private key 'key'R@RR(RnRT(RPRe((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pytsign¤scCs#t||d|d|dtƒS(s,Decrypts a cypher with the private key 'key'R@RR(RqRT(RjRe((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pytdecrypt©scCst||d|dtƒS(s+Verifies a cypher with the public key 'key'RIR(RqRM(RjRe((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pytverify®st__main__RLRrRtRsRu(8t__doc__t __author__t__date__t __version__tcPickletpicklet ImportErrorRRRZR&RR*tsysRRXt rsa._compatRtwarningstwarnt__name__tDeprecationWarningRRRRR$RR+R2R4R9R=R>R?RARBRDRJRKRLRMRTRURWR`RcRnRqRrRsRtRutdoctestttestmodt__all__(((s:/opt/awscli/lib/python2.7/site-packages/rsa/_version133.pytsp