<s> __revision__ = "<STR_LIT>" <EOL> from Crypto . PublicKey import pubkey <EOL> from Crypto . Util import number <EOL> try : <EOL> from Crypto . PublicKey import _fastmath <EOL> except ImportError : <EOL> _fastmath = None <EOL> class error ( Exception ) : <EOL> pass <EOL> def generate ( bits , randfunc , progress_func = None ) : <EOL> """<STR_LIT>""" <EOL> obj = RSAobj ( ) <EOL> if progress_func : <EOL> progress_func ( '<STR_LIT>' ) <EOL> p = q = <NUM_LIT:1> L <EOL> while number . size ( p * q ) < bits : <EOL> p = pubkey . getPrime ( bits / <NUM_LIT:2> , randfunc ) <EOL> q = pubkey . getPrime ( bits / <NUM_LIT:2> , randfunc ) <EOL> if p > q : <EOL> ( p , q ) = ( q , p ) <EOL> obj . p = p <EOL> obj . q = q <EOL> if progress_func : <EOL> progress_func ( '<STR_LIT>' ) <EOL> obj . u = pubkey . inverse ( obj . p , obj . q ) <EOL> obj . n = obj . p * obj . q <EOL> obj . e = <NUM_LIT> L <EOL> if progress_func : <EOL> progress_func ( '<STR_LIT>' ) <EOL> obj . d = pubkey . inverse ( obj . e , ( obj . p - <NUM_LIT:1> ) * ( obj . q - <NUM_LIT:1> ) ) <EOL> assert bits <= <NUM_LIT:1> + obj . size ( ) , "<STR_LIT>" <EOL> return obj <EOL> def construct ( tuple ) : <EOL> """<STR_LIT>""" <EOL> obj = RSAobj ( ) <EOL> if len ( tuple ) not in [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:6> ] : <EOL> raise error , '<STR_LIT>' <EOL> for i in range ( len ( tuple ) ) : <EOL> field = obj . keydata [ i ] <EOL> setattr ( obj , field , tuple [ i ] ) <EOL> if len ( tuple ) >= <NUM_LIT:5> : <EOL> if obj . p > obj . q : <EOL> ( obj . p , obj . q ) = ( obj . q , obj . p ) <EOL> if len ( tuple ) == <NUM_LIT:5> : <EOL> obj . u = pubkey . inverse ( obj . p , obj . q ) <EOL> return obj <EOL> class RSAobj ( pubkey . pubkey ) : <EOL> keydata = [ '<STR_LIT:n>' , '<STR_LIT:e>' , '<STR_LIT:d>' , '<STR_LIT:p>' , '<STR_LIT:q>' , '<STR_LIT:u>' ] <EOL> def _encrypt ( self , plaintext , K = '<STR_LIT>' ) : <EOL> if self . n <= plaintext : <EOL> raise error , '<STR_LIT>' <EOL> return ( pow ( plaintext , self . e , self . n ) , ) <EOL> def _decrypt ( self , ciphertext ) : <EOL> if ( not hasattr ( self , '<STR_LIT:d>' ) ) : <EOL> raise error , '<STR_LIT>' <EOL> if self . n <= ciphertext [ <NUM_LIT:0> ] : <EOL> raise error , '<STR_LIT>' <EOL> return pow ( ciphertext [ <NUM_LIT:0> ] , self . d , self . n ) <EOL> def _sign ( self , M , K = '<STR_LIT>' ) : <EOL> return ( self . _decrypt ( ( M , ) ) , ) <EOL> def _verify ( self , M , sig ) : <EOL> m2 = self . _encrypt ( sig [ <NUM_LIT:0> ] ) <EOL> if m2 [ <NUM_LIT:0> ] == M : <EOL> return <NUM_LIT:1> <EOL> else : return <NUM_LIT:0> <EOL> def _blind ( self , M , B ) : <EOL> tmp = pow ( B , self . e , self . n ) <EOL> return ( M * tmp ) % self . n <EOL> def _unblind ( self , M , B ) : <EOL> tmp = pubkey . inverse ( B , self . n ) <EOL> return ( M * tmp ) % self . n <EOL> def can_blind ( self ) : <EOL> """<STR_LIT>""" <EOL> return <NUM_LIT:1> <EOL> def size ( self ) : <EOL> """<STR_LIT>""" <EOL> return number . size ( self . n ) - <NUM_LIT:1> <EOL> def has_private ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , '<STR_LIT:d>' ) : <EOL> return <NUM_LIT:1> <EOL> else : return <NUM_LIT:0> <EOL> def publickey ( self ) : <EOL> """<STR_LIT>""" <EOL> return construct ( ( self . n , self . e ) ) <EOL> class RSAobj_c ( pubkey . pubkey ) : <EOL> keydata = [ '<STR_LIT:n>' , '<STR_LIT:e>' , '<STR_LIT:d>' , '<STR_LIT:p>' , '<STR_LIT:q>' , '<STR_LIT:u>' ] <EOL> def __init__ ( self , key ) : <EOL> self . key = key <EOL> def __getattr__ ( self , attr ) : <EOL> if attr in self . keydata : <EOL> return getattr ( self . key , attr ) <EOL> else : <EOL> if self . __dict__ . has_key ( attr ) : <EOL> self . __dict__ [ attr ] <EOL> else : <EOL> raise AttributeError , '<STR_LIT>' % ( self . __class__ , attr ) <EOL> def __getstate__ ( self ) : <EOL> d = { } <EOL> for k in self . keydata : <EOL> if hasattr ( self . key , k ) : <EOL> d [ k ] = getattr ( self . key , k ) <EOL> return d <EOL> def __setstate__ ( self , state ) : <EOL> n , e = state [ '<STR_LIT:n>' ] , state [ '<STR_LIT:e>' ] <EOL> if not state . has_key ( '<STR_LIT:d>' ) : <EOL> self . key = _fastmath . rsa_construct ( n , e ) <EOL> else : <EOL> d = state [ '<STR_LIT:d>' ] <EOL> if not state . has_key ( '<STR_LIT:q>' ) : <EOL> self . key = _fastmath . rsa_construct ( n , e , d ) <EOL> else : <EOL> p , q , u = state [ '<STR_LIT:p>' ] , state [ '<STR_LIT:q>' ] , state [ '<STR_LIT:u>' ] <EOL> self . key = _fastmath . rsa_construct ( n , e , d , p , q , u ) <EOL> def _encrypt ( self , plain , K ) : <EOL> return ( self . key . _encrypt ( plain ) , ) <EOL> def _decrypt ( self , cipher ) : <EOL> return self . key . _decrypt ( cipher [ <NUM_LIT:0> ] ) <EOL> def _sign ( self , M , K ) : <EOL> return ( self . key . _sign ( M ) , ) <EOL> def _verify ( self , M , sig ) : <EOL> return self . key . _verify ( M , sig [ <NUM_LIT:0> ] ) <EOL> def _blind ( self , M , B ) : <EOL> return self . key . _blind ( M , B ) <EOL> def _unblind ( self , M , B ) : <EOL> return self . key . _unblind ( M , B ) <EOL> def can_blind ( self ) : <EOL> return <NUM_LIT:1> <EOL> def size ( self ) : <EOL> return self . key . size ( ) <EOL> def has_private ( self ) : <EOL> return self . key . has_private ( ) <EOL> def publickey ( self ) : <EOL> return construct_c ( ( self . key . n , self . key . e ) ) <EOL> def generate_c ( bits , randfunc , progress_func = None ) : <EOL> if progress_func : <EOL> progress_func ( '<STR_LIT>' ) <EOL> p = q = <NUM_LIT:1> L <EOL> while number . size ( p * q ) < bits : <EOL> p = pubkey . getPrime ( bits / <NUM_LIT:2> , randfunc ) <EOL> q = pubkey . getPrime ( bits / <NUM_LIT:2> , randfunc ) <EOL> if p > q : <EOL> ( p , q ) = ( q , p ) <EOL> if progress_func : <EOL> progress_func ( '<STR_LIT>' ) <EOL> u = pubkey . inverse ( p , q ) <EOL> n = p * q <EOL> e = <NUM_LIT> L <EOL> if progress_func : <EOL> progress_func ( '<STR_LIT>' ) <EOL> d = pubkey . inverse ( e , ( p - <NUM_LIT:1> ) * ( q - <NUM_LIT:1> ) ) <EOL> key = _fastmath . rsa_construct ( n , e , d , p , q , u ) <EOL> obj = RSAobj_c ( key ) <EOL> assert bits <= <NUM_LIT:1> + obj . size ( ) , "<STR_LIT>" <EOL> return obj <EOL> def construct_c ( tuple ) : <EOL> key = apply ( _fastmath . rsa_construct , tuple ) <EOL> return RSAobj_c ( key ) <EOL> object = RSAobj <EOL> generate_py = generate <EOL> construct_py = construct <EOL> if _fastmath : <EOL> generate = generate_c <EOL> construct = construct_c <EOL> error = _fastmath . error </s>
