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 /passlib/utils/__init__.py | |
| parent | c228ae93a81348e890d4dcb9adde8e95750e149e (diff) | |
| download | passlib-8fc2cc7de320b253cc2ad39d33a0c9d4e99d68aa.tar.gz | |
passlib.utils: consteq() now an alias for hmac.compare_digest() on python 3.3 and up
Diffstat (limited to 'passlib/utils/__init__.py')
| -rw-r--r-- | passlib/utils/__init__.py | 46 |
1 files changed, 22 insertions, 24 deletions
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. |
