diff options
| author | Eli Collins <elic@assurancetechnologies.com> | 2016-06-11 12:50:40 -0400 |
|---|---|---|
| committer | Eli Collins <elic@assurancetechnologies.com> | 2016-06-11 12:50:40 -0400 |
| commit | 8fc2cc7de320b253cc2ad39d33a0c9d4e99d68aa (patch) | |
| tree | 1647807aa967579e284a5d9e54f288fcee87bb52 | |
| parent | c228ae93a81348e890d4dcb9adde8e95750e149e (diff) | |
| download | passlib-8fc2cc7de320b253cc2ad39d33a0c9d4e99d68aa.tar.gz | |
passlib.utils: consteq() now an alias for hmac.compare_digest() on python 3.3 and up
| -rw-r--r-- | CHANGES | 3 | ||||
| -rw-r--r-- | docs/lib/passlib.utils.rst | 35 | ||||
| -rw-r--r-- | passlib/handlers/misc.py | 4 | ||||
| -rw-r--r-- | passlib/tests/test_utils.py | 21 | ||||
| -rw-r--r-- | passlib/utils/__init__.py | 46 |
5 files changed, 78 insertions, 31 deletions
@@ -86,6 +86,9 @@ Minor Internal Changes when using a hash directly, rather than via CryptContext, as well making it easier for CryptContext to support hash-specific parameters. + * :func:`~passlib.utils.consteq` is now an alias for stdlib's :func:`hmac.compare_digest` + under python 3.3 and up. + Deprecations ------------ * The :class:`~passlib.ifc.PasswordHash` API (used by all hashes in passlib), diff --git a/docs/lib/passlib.utils.rst b/docs/lib/passlib.utils.rst index 5f26dea..ef6432d 100644 --- a/docs/lib/passlib.utils.rst +++ b/docs/lib/passlib.utils.rst @@ -56,7 +56,40 @@ Constants Unicode Helpers =============== -.. autofunction:: consteq +.. function:: consteq(left, right) + + Check two strings/bytes for equality. + + This is functionally equivalent to ``left == right``, + but attempts to take constant time relative to the size of the righthand input. + + The purpose of this function is to help prevent timing attacks + during digest comparisons: the standard ``==`` operator aborts + after the first mismatched character, causing its runtime to be + proportional to the longest prefix shared by the two inputs. + If an attacker is able to predict and control one of the two + inputs, repeated queries can be leveraged to reveal information about + the content of the second argument. To minimize this risk, :func:`!consteq` + is designed to take ``THETA(len(right))`` time, regardless + of the contents of the two strings. + It is recommended that the attacker-controlled input + be passed in as the left-hand value. + + .. warning:: + + This function is *not* perfect. Various VM-dependant issues + (e.g. the VM's integer object instantiation algorithm, internal unicode representation, etc), + may still cause the function's run time to be affected by the inputs, + though in a less predictable manner. + *To minimize such risks, this function should not be passed* :class:`unicode` + *inputs that might contain non-* ``ASCII`` *characters*. + + .. versionadded:: 1.6 + + .. versionchanged:: 1.7 + + This is an alias for stdlib's :func:`hmac.compare_digest` under Python 3.3 and up. + .. autofunction:: saslprep Bytes Helpers diff --git a/passlib/handlers/misc.py b/passlib/handlers/misc.py index af75017..b342905 100644 --- a/passlib/handlers/misc.py +++ b/passlib/handlers/misc.py @@ -9,7 +9,7 @@ import logging; log = logging.getLogger(__name__) from warnings import warn # site # pkg -from passlib.utils import to_native_str, consteq +from passlib.utils import to_native_str, str_consteq from passlib.utils.compat import unicode, u, unicode_or_bytes_types import passlib.utils.handlers as uh # local @@ -211,7 +211,7 @@ class plaintext(uh.MinimalHandler): hash = to_native_str(hash, encoding, "hash") if not cls.identify(hash): raise uh.exc.InvalidHashError(cls) - return consteq(cls.hash(secret, encoding), hash) + return str_consteq(cls.hash(secret, encoding), hash) #============================================================================= # eof diff --git a/passlib/tests/test_utils.py b/passlib/tests/test_utils.py index 1d0d0ea..3dcdb9d 100644 --- a/passlib/tests/test_utils.py +++ b/passlib/tests/test_utils.py @@ -9,6 +9,7 @@ import warnings # site # pkg # module +from passlib.utils import is_ascii_safe from passlib.utils.compat import irange, PY3, u, unicode, join_bytes from passlib.tests.utils import TestCase @@ -217,7 +218,7 @@ class MiscTest(TestCase): """test consteq()""" # NOTE: this test is kind of over the top, but that's only because # this is used for the critical task of comparing hashes for equality. - from passlib.utils import consteq + from passlib.utils import consteq, str_consteq # ensure error raises for wrong types self.assertRaises(TypeError, consteq, u(''), b'') @@ -239,7 +240,12 @@ class MiscTest(TestCase): u("abc"), u("\xff\xa2\x12\x00")*10, ]: - self.assertTrue(consteq(value, value), "value %r:" % (value,)) + if consteq is str_consteq or is_ascii_safe(value): + self.assertTrue(consteq(value, value), "value %r:" % (value,)) + else: + self.assertRaises(TypeError, consteq, value, value) + self.assertTrue(str_consteq(value, value), "value %r:" % (value,)) + value = value.encode("latin-1") self.assertTrue(consteq(value, value), "value %r:" % (value,)) @@ -259,8 +265,15 @@ class MiscTest(TestCase): (u("abc"), u("defabc")), (u("qwertyuiopasdfghjklzxcvbnm"), u("abc")), ]: - self.assertFalse(consteq(l, r), "values %r %r:" % (l,r)) - self.assertFalse(consteq(r, l), "values %r %r:" % (r,l)) + if consteq is str_consteq or is_ascii_safe(l): + self.assertFalse(consteq(l, r), "values %r %r:" % (l,r)) + self.assertFalse(consteq(r, l), "values %r %r:" % (r,l)) + else: + self.assertRaises(TypeError, consteq, l, r) + self.assertRaises(TypeError, consteq, r, l) + self.assertFalse(str_consteq(l, r), "values %r %r:" % (l,r)) + self.assertFalse(str_consteq(r, l), "values %r %r:" % (r,l)) + l = l.encode("latin-1") r = r.encode("latin-1") self.assertFalse(consteq(l, r), "values %r %r:" % (l,r)) diff --git a/passlib/utils/__init__.py b/passlib/utils/__init__.py index 70d7ea9..4176abe 100644 --- a/passlib/utils/__init__.py +++ b/passlib/utils/__init__.py @@ -279,33 +279,19 @@ class SequenceMixin(object): # unicode helpers #============================================================================= +# XXX: should this be moved to passlib.crypto, or compat backports? + def consteq(left, right): """Check two strings/bytes for equality. - This is functionally equivalent to ``left == right``, - but attempts to take constant time relative to the size of the righthand input. - - The purpose of this function is to help prevent timing attacks - during digest comparisons: the standard ``==`` operator aborts - after the first mismatched character, causing its runtime to be - proportional to the longest prefix shared by the two inputs. - If an attacker is able to predict and control one of the two - inputs, repeated queries can be leveraged to reveal information about - the content of the second argument. To minimize this risk, :func:`!consteq` - is designed to take ``THETA(len(right))`` time, regardless - of the contents of the two strings. - It is recommended that the attacker-controlled input - be passed in as the left-hand value. - - .. warning:: - - This function is *not* perfect. Various VM-dependant issues - (e.g. the VM's integer object instantiation algorithm, internal unicode representation, etc), - may still cause the function's run time to be affected by the inputs, - though in a less predictable manner. - *To minimize such risks, this function should not be passed* :class:`unicode` - *inputs that might contain non-* ``ASCII`` *characters*. - .. versionadded:: 1.6 + This function uses an approach designed to prevent + timing analysis, making it appropriate for cryptography. + a and b must both be of the same type: either str (ASCII only), + or any type that supports the buffer protocol (e.g. bytes). + + Note: If a and b are of different lengths, or if an error occurs, + a timing attack could theoretically reveal information about the + types and lengths of a and b--but not their values. """ # NOTE: # resources & discussions considered in the design of this function: @@ -353,6 +339,18 @@ def consteq(left, right): result |= ord(l) ^ ord(r) return result == 0 +# keep copy of this around since stdlib's version throws error on non-ascii chars in unicode strings. +# our version does, but suffers from some underlying VM issues. but something is better than +# nothing for plaintext hashes, which need this. everything else should use consteq(), +# since the stdlib one is going to be as good / better in the general case. +str_consteq = consteq + +try: + # for py3.3 and up, use the stdlib version + from hmac import compare_digest as consteq +except ImportError: + pass + def splitcomma(source, sep=","): """split comma-separated string into list of elements, stripping whitespace. |
