diff options
author | Brett Cannon <brett@python.org> | 2012-06-15 19:04:29 -0400 |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2012-06-15 19:04:29 -0400 |
commit | 24aa693c7ef8f217fbd238eb7af7d828e13a07eb (patch) | |
tree | 7d01ab630c2e8eef1e168b1aa5d84131b60cfd50 /Lib/hmac.py | |
parent | 99d776fdf4aa5a66266ebcec2263fab501f03088 (diff) | |
parent | 016ef551a793f72f582d707ce5bb55bf4940cf27 (diff) | |
download | cpython-git-24aa693c7ef8f217fbd238eb7af7d828e13a07eb.tar.gz |
Merge
Diffstat (limited to 'Lib/hmac.py')
-rw-r--r-- | Lib/hmac.py | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/Lib/hmac.py b/Lib/hmac.py index 13ffdbe14b..e47965b1cf 100644 --- a/Lib/hmac.py +++ b/Lib/hmac.py @@ -13,24 +13,24 @@ trans_36 = bytes((x ^ 0x36) for x in range(256)) digest_size = None -def secure_compare(a, b): - """Returns the equivalent of 'a == b', but using a time-independent - comparison method to prevent timing attacks.""" - if not ((isinstance(a, str) and isinstance(b, str)) or - (isinstance(a, bytes) and isinstance(b, bytes))): - raise TypeError("inputs must be strings or bytes") - +def compare_digest(a, b): + """Returns the equivalent of 'a == b', but avoids content based short + circuiting to reduce the vulnerability to timing attacks.""" + # Consistent timing matters more here than data type flexibility + if not (isinstance(a, bytes) and isinstance(b, bytes)): + raise TypeError("inputs must be bytes instances") + + # We assume the length of the expected digest is public knowledge, + # thus this early return isn't leaking anything an attacker wouldn't + # already know if len(a) != len(b): return False + # We assume that integers in the bytes range are all cached, + # thus timing shouldn't vary much due to integer object creation result = 0 - if isinstance(a, bytes): - for x, y in zip(a, b): - result |= x ^ y - else: - for x, y in zip(a, b): - result |= ord(x) ^ ord(y) - + for x, y in zip(a, b): + result |= x ^ y return result == 0 |