mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
Issue #14532: Add a secure_compare() helper to the hmac module, to mitigate
timing attacks. Patch by Jon Oberheide.
This commit is contained in:
parent
d200bf534b
commit
7feb9f4225
5 changed files with 94 additions and 1 deletions
|
@ -302,12 +302,48 @@ class CopyTestCase(unittest.TestCase):
|
|||
self.assertEqual(h1.hexdigest(), h2.hexdigest(),
|
||||
"Hexdigest of copy doesn't match original hexdigest.")
|
||||
|
||||
class SecureCompareTestCase(unittest.TestCase):
|
||||
|
||||
def test_compare(self):
|
||||
# Testing input type exception handling
|
||||
a, b = 100, 200
|
||||
self.assertRaises(TypeError, hmac.secure_compare, a, b)
|
||||
a, b = 100, "foobar"
|
||||
self.assertRaises(TypeError, hmac.secure_compare, a, b)
|
||||
a, b = "foobar", b"foobar"
|
||||
self.assertRaises(TypeError, hmac.secure_compare, a, b)
|
||||
|
||||
# Testing str/bytes of different lengths
|
||||
a, b = "foobar", "foo"
|
||||
self.assertFalse(hmac.secure_compare(a, b))
|
||||
a, b = b"foobar", b"foo"
|
||||
self.assertFalse(hmac.secure_compare(a, b))
|
||||
a, b = b"\xde\xad\xbe\xef", b"\xde\xad"
|
||||
self.assertFalse(hmac.secure_compare(a, b))
|
||||
|
||||
# Testing str/bytes of same lengths, different values
|
||||
a, b = "foobar", "foobaz"
|
||||
self.assertFalse(hmac.secure_compare(a, b))
|
||||
a, b = b"foobar", b"foobaz"
|
||||
self.assertFalse(hmac.secure_compare(a, b))
|
||||
a, b = b"\xde\xad\xbe\xef", b"\xab\xad\x1d\xea"
|
||||
self.assertFalse(hmac.secure_compare(a, b))
|
||||
|
||||
# Testing str/bytes of same lengths, same values
|
||||
a, b = "foobar", "foobar"
|
||||
self.assertTrue(hmac.secure_compare(a, b))
|
||||
a, b = b"foobar", b"foobar"
|
||||
self.assertTrue(hmac.secure_compare(a, b))
|
||||
a, b = b"\xde\xad\xbe\xef", b"\xde\xad\xbe\xef"
|
||||
self.assertTrue(hmac.secure_compare(a, b))
|
||||
|
||||
def test_main():
|
||||
support.run_unittest(
|
||||
TestVectorsTestCase,
|
||||
ConstructorTestCase,
|
||||
SanityTestCase,
|
||||
CopyTestCase
|
||||
CopyTestCase,
|
||||
SecureCompareTestCase
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue