Skip to content

Commit 1088881

Browse files
reaperhulkdstufft
authored andcommitted
Restore compatibility with 2.0.0's fix for wraparound bug (#81)
1 parent c9a9ec1 commit 1088881

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/bcrypt/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ def hashpw(password, salt):
6767
if b"\x00" in password:
6868
raise ValueError("password may not contain NUL bytes")
6969

70+
# bcrypt originally suffered from a wraparound bug:
71+
# http://www.openwall.com/lists/oss-security/2012/01/02/4
72+
# This bug was corrected in the OpenBSD source by truncating inputs to 72
73+
# bytes on the updated prefix $2b$, but leaving $2a$ unchanged for
74+
# compatibility. However, pyca/bcrypt 2.0.0 *did* correctly truncate inputs
75+
# on $2a$, so we do it here to preserve compatibility with 2.0.0
76+
password = password[:72]
77+
7078
salt = _normalize_prefix(salt)
7179

7280
hashed = _bcrypt.ffi.new("unsigned char[]", 128)

tests/test_bcrypt.py

+6
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,9 @@ def test_invalid_params(password, salt, desired_key_bytes, rounds, error):
430430
def test_bcrypt_assert():
431431
with pytest.raises(SystemError):
432432
bcrypt._bcrypt_assert(False)
433+
434+
435+
def test_2a_wraparound_bug():
436+
assert bcrypt.hashpw(
437+
(b"0123456789" * 26)[:255], b"$2a$04$R1lJ2gkNaoPGdafE.H.16."
438+
) == b"$2a$04$R1lJ2gkNaoPGdafE.H.16.1MKHPvmKwryeulRe225LKProWYwt9Oi"

0 commit comments

Comments
 (0)