def __init__(self, drbg_spec, drbg_source, drbg_reseed_rate, raw_source): self.drbg = None self.raw = None if drbg_spec is not None and drbg_source is not None: self.drbg = sp800_90a.new(drbg_spec, drbg_source, drbg_reseed_rate) if raw_source is not None: self.raw = entropysource.new(raw_source) self.total_bytes = 0 self.start_time = time.time() if not self.drbg and not self.raw: raise ValueError( 'Cannot construct engine with neither DRBG nor raw source.')
def simple_test(): 'This is just a simple test, not a unit test suite.' source = entropysource.new('FILE_dev_urandom') print 'Simple test of HashDRBG' drbg = HashDRBG('SHA256', source, 256) rand = drbg.generate(56) if rand[0] == 'SUCCESS': print 'Successfully generated value [%s]' % rand[1].encode('hex') else: print 'Failed with error [%s]' % rand[0] print 'Simple test of CTR_DRBG' drbg = CTR_DRBG(False, 'AES', source, 256) rand = drbg.generate(56) if rand[0] == 'SUCCESS': print 'Successfully generated value [%s]' % rand[1].encode('hex') else: print 'Failed with error [%s]' % rand[0]
def __init__(self, entropy_source, supported_security_strengths, nonce_required, max_length, max_personalization, max_additional_input, max_n_bits, max_interval): if type(self) == DRBG: raise NotImplementedError( 'This class should not be instantiated directly.') # Check that we can process the entropy source provided if isinstance(entropy_source, entropysource.EntropySource): self.entropy_source = entropy_source else: self.entropy_source = entropysource.new(entropy_source) if not self.entropy_source: raise ValueError( 'Unrecognized entropy source [%s]' % entropy_source) self.supported_security_strengths = supported_security_strengths self.nonce_required = nonce_required self.max_length = max_length self.max_personalization = max_personalization self.max_additional_input = max_additional_input self.max_n_bits = max_n_bits self.max_interval = max_interval self.counter = None self.instantiated = False self.security_strength = 0 self.min_length = 0 self.prediction_resistance = False self.reseed_required = True self.reseed_failed = False self.max_security_strength = max(self.supported_security_strengths)