95
95
class FortunaAccumulator(object):
97
min_pool_size = 64 # TODO: explain why
98
reseed_interval = 0.100 # 100 ms TODO: explain why
97
# An estimate of how many bytes we must append to pool 0 before it will
98
# contain 128 bits of entropy (with respect to an attack). We reseed the
99
# generator only after pool 0 contains `min_pool_size` bytes. Note that
100
# unlike with some other PRNGs, Fortuna's security does not rely on the
101
# accuracy of this estimate---we can accord to be optimistic here.
102
min_pool_size = 64 # size in bytes
104
# If an attacker can predict some (but not all) of our entropy sources, the
105
# `min_pool_size` check may not be sufficient to prevent a successful state
106
# compromise extension attack. To resist this attack, Fortuna spreads the
107
# input across 32 pools, which are then consumed (to reseed the output
108
# generator) with exponentially decreasing frequency.
110
# In order to prevent an attacker from gaining knowledge of all 32 pools
111
# before we have a chance to fill them with enough information that the
112
# attacker cannot predict, we impose a rate limit of 10 reseeds/second (one
113
# per 100 ms). This ensures that a hypothetical 33rd pool would only be
114
# needed after a minimum of 13 years of sustained attack.
115
reseed_interval = 0.100 # time in seconds
100
117
def __init__(self):
101
118
self.reseed_count = 0
109
126
self.pools = [FortunaPool() for i in range(32)] # 32 pools
110
127
assert(self.pools[0] is not self.pools[1])
129
def _forget_last_reseed(self):
130
# This is not part of the standard Fortuna definition, and using this
131
# function frequently can weaken Fortuna's ability to resist a state
132
# compromise extension attack, but we need this in order to properly
133
# implement Crypto.Random.atfork(). Otherwise, forked child processes
134
# might continue to use their parent's PRNG state for up to 100ms in
135
# some cases. (e.g. CVE-2013-1445)
136
self.last_reseed = None
112
138
def random_data(self, bytes):
113
139
current_time = time.time()
114
140
if (self.last_reseed is not None and self.last_reseed > current_time): # Avoid float comparison to None to make Py3k happy