summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDwayne Litzenberger <dlitz@dlitz.net>2013-04-21 23:24:23 -0700
committerDwayne Litzenberger <dlitz@dlitz.net>2013-04-21 23:24:23 -0700
commit22d7760ae7346d80a6c54b1549e9a1d560c239bc (patch)
tree91989962330105d327d8fade2cc9a5d6c8864f72
parentcce74edc6c792efbe402eca681a7cead4836f543 (diff)
downloadpycrypto-22d7760ae7346d80a6c54b1549e9a1d560c239bc.tar.gz
FortunaAccumulator: Use time.monotonic if available (i.e. Python 3.3 and later)
-rw-r--r--lib/Crypto/Random/Fortuna/FortunaAccumulator.py7
-rw-r--r--lib/Crypto/Util/_time.py28
2 files changed, 33 insertions, 2 deletions
diff --git a/lib/Crypto/Random/Fortuna/FortunaAccumulator.py b/lib/Crypto/Random/Fortuna/FortunaAccumulator.py
index 5ebbe2b..42e998c 100644
--- a/lib/Crypto/Random/Fortuna/FortunaAccumulator.py
+++ b/lib/Crypto/Random/Fortuna/FortunaAccumulator.py
@@ -36,6 +36,9 @@ import warnings
from Crypto.pct_warnings import ClockRewindWarning
import SHAd256
+# If the system has monotonic time, we'll use it.
+from Crypto.Util._time import maybe_monotonic_time
+
import FortunaGenerator
class FortunaPool(object):
@@ -110,7 +113,7 @@ class FortunaAccumulator(object):
assert(self.pools[0] is not self.pools[1])
def random_data(self, bytes):
- current_time = time.time()
+ current_time = maybe_monotonic_time()
if (self.last_reseed is not None and self.last_reseed > current_time): # Avoid float comparison to None to make Py3k happy
warnings.warn("Clock rewind detected. Resetting last_reseed.", ClockRewindWarning)
self.last_reseed = None
@@ -123,7 +126,7 @@ class FortunaAccumulator(object):
def _reseed(self, current_time=None):
if current_time is None:
- current_time = time.time()
+ current_time = maybe_monotonic_time()
seed = []
self.reseed_count += 1
self.last_reseed = current_time
diff --git a/lib/Crypto/Util/_time.py b/lib/Crypto/Util/_time.py
new file mode 100644
index 0000000..ff4c6a9
--- /dev/null
+++ b/lib/Crypto/Util/_time.py
@@ -0,0 +1,28 @@
+# -*- coding: ascii -*-
+#
+# _time.py : Internal monotonic time module.
+#
+# Written in 2013 by Dwayne C. Litzenberger <dlitz@dlitz.net>
+#
+# ===================================================================
+# The contents of this file are dedicated to the public domain. To
+# the extent that dedication to the public domain is not available,
+# everyone is granted a worldwide, perpetual, royalty-free,
+# non-exclusive license to exercise all rights associated with the
+# contents of this file for any purpose whatsoever.
+# No rights are reserved.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+# ===================================================================
+
+try:
+ from time import monotonic as maybe_monotonic_time
+except ImportError:
+ from time import time as maybe_monotonic_time