]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
hashutil: add constant-time comparison function, to avoid timing attacks when python...
authorBrian Warner <warner@lothar.com>
Mon, 23 Mar 2009 03:20:55 +0000 (20:20 -0700)
committerBrian Warner <warner@lothar.com>
Mon, 23 Mar 2009 03:20:55 +0000 (20:20 -0700)
src/allmydata/test/test_util.py
src/allmydata/util/hashutil.py

index ece54181dddbe14d2186bec23a976edb552ec29f..19ae70f561a698d8d6eadd7ac7093bb90d765acc 100644 (file)
@@ -601,6 +601,12 @@ class HashUtilTests(unittest.TestCase):
         h2.update("foo")
         self.failUnlessEqual(h1, h2.digest())
 
+    def test_constant_time_compare(self):
+        self.failUnless(hashutil.constant_time_compare("a", "a"))
+        self.failUnless(hashutil.constant_time_compare("ab", "ab"))
+        self.failIf(hashutil.constant_time_compare("a", "b"))
+        self.failIf(hashutil.constant_time_compare("a", "aa"))
+
 class Abbreviate(unittest.TestCase):
     def test_time(self):
         a = abbreviate.abbreviate_time
index 0161e6765005c94528daa210a07a9e9423eec543..d5b260ad379598ce79cedb22bdff61a09f33c060 100644 (file)
@@ -189,3 +189,7 @@ def ssk_readkey_data_hash(IV, readkey):
     return tagged_pair_hash(MUTABLE_DATAKEY_TAG, IV, readkey, KEYLEN)
 def ssk_storage_index_hash(readkey):
     return tagged_hash(MUTABLE_STORAGEINDEX_TAG, readkey, KEYLEN)
+
+def constant_time_compare(a, b):
+    n = os.urandom(8)
+    return bool(tagged_hash(n, a) == tagged_hash(n, b))