Ñò 6š@c @ sZ d Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k l Z y d d k i i Z Wn e Z n Xd Z d f d „ ƒ YZ d e f d „ ƒ YZ d Z e o7 y) d d k Z d f d „ ƒ YZ d Z Wn n Xn e o7 y) d d k Z d f d „ ƒ YZ d Z Wn n Xn d e f d „ ƒ YZ e d j oe ƒ Z d Ge i Gd GHe i d ƒ e i d ƒ GHd d k Z d d k Z e i ƒ Z e d e ƒ Z d Ge i Gd GHe i ƒ d Ge i Gd GHe i d ƒ e i ƒ e e d ƒ i ƒ Z! d Ge! GHd Ge i" i# ƒ GHe e ƒ Z$ d Ge i Gd GHe i% e ƒ n d S( s; $Id: randpool.py,v 1.14 2004/05/06 12:56:54 akuchling Exp $iÿÿÿÿN( t long_to_bytesi t RandomPoolc B s¡ e Z d Z d d d d „ Z d „ Z d d d „ Z d d „ Z e d „ Z d d „ Z d „ Z d d „ Z d „ Z d „ Z d „ Z d „ Z d d „ Z RS( s1 randpool.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 c C s | d j o d d k l } n | d j o t i d ƒ n t | t i ƒ o- t d | d d d g ƒ } t i d ƒ n | | _ | i d | _ d | _ | | _ t i d d g | i ƒ | _ d | _ | _ d | _ | i | _ t i ƒ | _ d | _ | i ƒ | i ƒ d S( Niÿÿÿÿ( t SHAs$ 'cipher' parameter is no longer useds Crypto.Hash.t news/ 'hash' parameter should now be a hashing modulei i t B( t Nonet Crypto.HashR t warningst warnt isinstancet typest StringTypet __import__t bytest bitst entropyt _hasht arrayt _randpoolt _event1t _event2t _addPost digest_sizet _getPost timet _lastcountert _RandomPool__countert _measureTickSizet _randomize( t selft numbytest ciphert hash( ( s: /usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyt __init__7 s( c C sS | i | 7_ | i d j o d | _ n$ | i | i j o | i | _ n d S( Ni ( R R ( R t nbits( ( s: /usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyt _updateEntropyEstimateY s i s /dev/urandomc C s$ d } | d j o" t | i | i d d ƒ } n t | d d ƒ } t o t i ƒ i | ƒ } n t i i | ƒ ok y) t | ƒ } | i | ƒ } | i ƒ Wqç t j o/ \ } } | d j o t | | f ‚ qã qç Xn | o( | i | ƒ | i d t | ƒ ƒ n | i ƒ d S( 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. t i i g à?i N( t intR R t winrandomR t get_bytest ost patht existst opent readt closet IOErrort _addBytesR# t lent stir_n( R t Nt devnamet datat nbytest ft numt msg( ( s: /usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyR ` s$ " c C s | i | ƒ S( s‹ randomize(N:int) use the class entropy source to get some entropy data. This is overridden by KeyboardRandomize(). ( R ( R R2 ( ( s: /usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyt randomize s c C s% x t | ƒ D] } | i ƒ q Wd S( s8 stir_n(N) stirs the random pool N times N( t xranget stir( R R2 t i( ( s: /usr/lib64/python2.6/site-packages/Crypto/Util/randpool.pyR1 ˆ s R$ c C sÔ | i } | i ƒ xŽ t | i | i i ƒ D]s } | i i | i ƒ } | i t | i ƒ t | ƒ t | i ƒ | ƒ | i | i ƒ ƒ | i d d @| _ q- Wd | i i | _ | _ | i ƒ | | _ d S( 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. i l ÿÿ i N( R t add_eventt rangeR R R R R t updatet strR R R/ t digestR ( R t sR R<