summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEli Collins <elic@assurancetechnologies.com>2020-02-17 11:21:46 -0500
committerEli Collins <elic@assurancetechnologies.com>2020-02-17 11:21:46 -0500
commitec62db38788b1dc0c8f78060f6119cb63bbacfbd (patch)
treea381646403b89d70866e31cc57641cdd14553f77
parente53460bc94667aaf29ff0221ba3e44d61b87fd2d (diff)
downloadpasslib-ec62db38788b1dc0c8f78060f6119cb63bbacfbd.tar.gz
passlib.hash.bcrypt: added notes re: final salt character
-rw-r--r--docs/lib/passlib.hash.bcrypt.rst2
-rw-r--r--passlib/handlers/bcrypt.py8
-rw-r--r--passlib/tests/test_handlers_bcrypt.py4
3 files changed, 11 insertions, 3 deletions
diff --git a/docs/lib/passlib.hash.bcrypt.rst b/docs/lib/passlib.hash.bcrypt.rst
index a8c4625..0d7319c 100644
--- a/docs/lib/passlib.hash.bcrypt.rst
+++ b/docs/lib/passlib.hash.bcrypt.rst
@@ -104,6 +104,8 @@ Bcrypt hashes have the format :samp:`$2a${rounds}${salt}{checksum}`, where:
* :samp:`{rounds}` is a cost parameter, encoded as 2 zero-padded decimal digits,
which determines the number of iterations used via :samp:`{iterations}=2**{rounds}` (rounds is 12 in the example).
* :samp:`{salt}` is a 22 character salt string, using the characters in the regexp range ``[./A-Za-z0-9]`` (``GhvMmNVjRW29ulnudl.Lbu`` in the example).
+ Note that due to padding bits within the encoding, the last character should always be one of ``[.Oeu]``:
+ under some bcrypt implementations, other final characters may result in false negatives when verifying.
* :samp:`{checksum}` is a 31 character checksum, using the same characters as the salt (``AnUtN/LRfe1JsBm1Xu6LE3059z5Tr8m`` in the example).
While BCrypt's basic algorithm is described in its design document [#f1]_,
diff --git a/passlib/handlers/bcrypt.py b/passlib/handlers/bcrypt.py
index 3c168e9..817416b 100644
--- a/passlib/handlers/bcrypt.py
+++ b/passlib/handlers/bcrypt.py
@@ -128,7 +128,9 @@ class _BcryptCommon(uh.SubclassBackendMixin, uh.TruncateMixin, uh.HasManyIdents,
#--------------------
min_salt_size = max_salt_size = 22
salt_chars = bcrypt64.charmap
- # NOTE: 22nd salt char must be in bcrypt64._padinfo2[1], not full charmap
+
+ # NOTE: 22nd salt char must be in restricted set of ``final_salt_chars``, not full set above.
+ final_salt_chars = ".Oeu" # bcrypt64._padinfo2[1]
#--------------------
# HasRounds
@@ -195,10 +197,12 @@ class _BcryptCommon(uh.SubclassBackendMixin, uh.TruncateMixin, uh.HasManyIdents,
@classmethod
def needs_update(cls, hash, **kwds):
+ # NOTE: can't convert this to use _calc_needs_update() helper,
+ # since _norm_hash() will correct salt padding before we can read it here.
# check for incorrect padding bits (passlib issue 25)
if isinstance(hash, bytes):
hash = hash.decode("ascii")
- if hash.startswith(IDENT_2A) and hash[28] not in bcrypt64._padinfo2[1]:
+ if hash.startswith(IDENT_2A) and hash[28] not in cls.final_salt_chars:
return True
# TODO: try to detect incorrect 8bit/wraparound hashes using kwds.get("secret")
diff --git a/passlib/tests/test_handlers_bcrypt.py b/passlib/tests/test_handlers_bcrypt.py
index 978b68b..8c88935 100644
--- a/passlib/tests/test_handlers_bcrypt.py
+++ b/passlib/tests/test_handlers_bcrypt.py
@@ -402,7 +402,9 @@ class _bcrypt_test(HandlerCase):
bcrypt = self.handler.using(rounds=4)
# PASS1 = "test"
- BAD1 = "$2a$04$yjDgE74RJkeqC0/1NheSScrvKeu9IbKDpcQf/Ox3qsrRS/Kw42qIS"
+ # bad contains invalid 'c' char at end of salt:
+ # \/
+ BAD1 = "$2a$04$yjDgE74RJkeqC0/1NheSScrvKeu9IbKDpcQf/Ox3qsrRS/Kw42qIS"
GOOD1 = "$2a$04$yjDgE74RJkeqC0/1NheSSOrvKeu9IbKDpcQf/Ox3qsrRS/Kw42qIS"
self.assertTrue(bcrypt.needs_update(BAD1))