Ñò 6š@c@sZdZddkZddkZddkZddkZddkZddklZyddk i i Z Wn e Z nXdZ dfd„ƒYZdefd„ƒYZd Ze o7y)ddkZd fd „ƒYZd ZWnnXne o7y)ddkZd fd „ƒYZd ZWnnXndefd„ƒYZedjoeƒZdGeiGdGHeidƒeidƒ GHddkZddkZeiƒZedeƒZdGeiGdGHeiƒdGeiGdGHeidƒeiƒeedƒi ƒZ!dGe! GHdGei"i#ƒ GHeeƒZ$dGeiGdGHei%eƒndS(s;$Id: randpool.py,v 1.14 2004/05/06 12:56:54 akuchling Exp $iÿÿÿÿN(t long_to_bytesit RandomPoolcBs¡eZdZdddd„Zd„Zddd„Zdd„Zed„Z d d „Z d „Z d d „Z d „Z d„Zd„Zd„Zd d„ZRS(s1randpool.py : Cryptographically strong random number generation. The implementation here is similar to the one in PGP. To be cryptographically strong, it must be difficult to determine the RNG's output, whether in the future or the past. This is done by using a cryptographic hash function to "stir" the random data. Entropy is gathered in the same fashion as PGP; the highest-resolution clock around is read and the data is added to the random number pool. A conservative estimate of the entropy is then kept. If a cryptographically secure random source is available (/dev/urandom on many Unixes, Windows CryptGenRandom on most Windows), then use it. Instance Attributes: bits : int Maximum size of pool in bits bytes : int Maximum size of pool in bytes entropy : int Number of bits of entropy in this pool. Methods: add_event([s]) : add some entropy to the pool get_bytes(int) : get N bytes of random data randomize([N]) : get N bytes of randomness from external source i cCs|djoddkl}n|dj otidƒnt|tiƒo-td|dddgƒ}tidƒn||_ |i d|_ d|_ ||_ t i d dg|i ƒ|_d|_|_d|_|i|_tiƒ|_d|_|iƒ|iƒdS( Niÿÿÿÿ(tSHAs$'cipher' parameter is no longer useds Crypto.Hash.tnews/'hash' parameter should now be a hashing moduleiitB(tNonet Crypto.HashRtwarningstwarnt isinstancettypest StringTypet __import__tbytestbitstentropyt_hashtarrayt _randpoolt_event1t_event2t_addPost digest_sizet_getPosttimet _lastcountert_RandomPool__countert_measureTickSizet _randomize(tselftnumbytestcipherthash((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyt__init__7s(          cCsS|i|7_|idjo d|_n$|i|ijo|i|_ndS(Ni(RR(Rtnbits((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyt_updateEntropyEstimateYs  is /dev/urandomcCs$d}|djo"t|i|iddƒ}nt|ddƒ}totiƒi|ƒ}ntii|ƒoky)t |ƒ}|i |ƒ}|i ƒWqçt j o/\}}|djot ||f‚qãqçXn|o(|i |ƒ|idt|ƒƒn|iƒdS(sl_randomize(N, DEVNAME:device-filepath) collects N bits of randomness from some entropy source (e.g., /dev/urandom on Unixes that have it, Windows CryptoAPI CryptGenRandom, etc) DEVNAME is optional, defaults to /dev/urandom. You can change it to /dev/random if you want to block till you get enough entropy. tiigà?iN(tintRRt winrandomRt get_bytestostpathtexiststopentreadtclosetIOErrort _addBytesR#tlentstir_n(RtNtdevnametdatatnbytestftnumtmsg((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyR`s$  "   cCs |i|ƒS(s‹randomize(N:int) use the class entropy source to get some entropy data. This is overridden by KeyboardRandomize(). (R(RR2((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyt randomizescCs%xt|ƒD]}|iƒq WdS(s8stir_n(N) stirs the random pool N times N(txrangetstir(RR2ti((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyR1ˆs R$cCsÔ|i}|iƒxŽt|i|iiƒD]s}|ii|iƒ}|it |i ƒt |ƒt |i ƒ|ƒ|i |i ƒƒ|i dd@|_ q-Wd|ii|_ |_|iƒ||_dS(s stir(s:string) Mix up the randomness pool. This will call add_event() twice, but out of paranoia the entropy attribute will not be increased. The optional 's' parameter is a string that will be hashed with the randomness pool. ilÿÿiN(Rt add_eventtrangeR RRRRtupdatetstrRRR/tdigestR(RtsRR<th((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyR;s  1 cCsÛd}|i|i}}|iiƒ}|ii}|}x||djon|i|i|||!ƒ||iƒ}||}|||i}||jo|iƒ|i}q=q=W||_|i d|ƒ|| S(sIget_bytes(N:int) : string Return N bytes of random data. R$iiøÿÿÿ( RRRRRR?RAR R;R#(RR2RBR<tpoolRCtdsizeR7((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyR'ªs"      cCsãtiƒd}|iƒ}|t|ƒdtdƒt|ƒ}|i|ƒ||ijo||ijo d}nBd}x!|o|d?|d}}q„W|djo d}n||i|_|_|i|ƒ|S(s9add_event(s:string) Add an event to the random pool. The current time is stored between calls and used to estimate the entropy. The optional 's' parameter is a string that will also be XORed into the pool. Returns the estimated number of additional bits of entropy gain. ièiiªiii(Rt_noiseRtchrR/RRR#(RRBteventtdeltaR((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyR=Âs (     cCsÃtiƒ}||i|id}||_|ittdtiƒƒƒƒ|ittdtiƒƒƒƒ|ittdtiƒƒƒƒ|itt|ƒƒƒ|d}t|ƒS(Ng€„.Aièiÿ(RRt _ticksizeR/RtlongtclockR%(RttRI((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyRFßs  ### cCs"dgd}|iit|ƒt|ƒf ƒ}tiƒ}|i| ƒd}d}xx|djojtiƒ}|i|||f ƒ|d7}t||dƒ}|o|||<|d7}|}q[q[W|iƒ|t|ƒd|_ |i||i f ƒ|i |i ƒƒdS(Nidiig€„.Ai( RRRtidRR?R%tsortR0RJR;RA(RtintervalRCRMR<tjtt2RI((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyRòs( %       cCsm|i|i}}xJtdt|ƒƒD]3}||t||ƒA||<|d|i}q)W||_dS(s5XOR the contents of the string S into the random pooliiN(RRR>R0tordR (RRBR<RDRQ((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyR/s cCstidtƒ|i|ƒS(Ns)getBytes() method replaced by get_bytes()(RRtDeprecationWarningR'(RR2((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pytgetBytess cCs'tidtƒ|i|t|ƒƒS(Ns)addEvent() method replaced by add_event()(RRRTR=R@(RRHRB((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pytaddEvents N(t__name__t __module__t__doc__RR!R#RR9tSTIRNUMR1R;R'R=RFRR/RURV(((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyRs" !        tPersistentRandomPoolcBseZdd„Zd„ZRS(cOs‹ti|||Ž||_|odyJt|dƒ}|iƒ|iƒ}|iƒ|i|ƒ|iƒWq‡tj oq‡XndS(Ntrb( RR!tfilenameR+R=R,R;R-R.(RR]targstkwargsR6R4((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyR!%s     cCs{|idjo td‚n|iƒt|idƒ}|iƒ|i|iiƒƒ|iƒ|iƒ|i ƒdS(NR$sNo filename set for this objecttwb( R]t ValueErrorR1R+R=twriteRttostringR-R;(RR6((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pytsave8s     N(RWRXRR!Rd(((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyR[$s it KeyboardEntrycBseZd„Zdd„ZRS(cCs1tiƒ}|djo|tiƒ7}n|S(Ntsà(Rfsà(tmsvcrttgetch(Rtc((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyRhLs  icCs;|o0ti|ƒx tiƒotiƒqWndS(N(RtsleepRgtkbhitRh(Rtdelay((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyR-Rs   (RWRXRhR-(((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyReKs icBs)eZdd„Zd„Zdd„ZRS(icCsb||_ti|ƒ|_ti|ƒ}|dti@ti@|dbits of entropy are now required. Please type on the keyboards-until enough randomness has been accumulated.R$iis"  Enough. Please wait a moment. i(R(tstringRRRReRR@trjustRbRhR=RGRRAR-R1( RR2R(RzRRtkbRBR tettemp((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyR9vs2$       $  (RWRXR!R9(((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyRyrs t__main__srandom pool entropyRt somethingidR]skeyboard random pool entropyi€R\tsavedspool spersistent random pool entropy(&t __revision__RRR Rtos.pathR(tCrypto.Util.numberRtCrypto.Util.winrandomtUtilR&RRZRR[t_kbRgReRnRyRWRDRR=R'ttempfiletmktemptfnameR9RdR+R,RRRctnewpooltremove(((s:/usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyt sZ< ÿ #    !