__init__.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # Author:: Donald Stufft (<donald@stufft.io>)
  2. # Copyright:: Copyright (c) 2013 Donald Stufft
  3. # License:: Apache License, Version 2.0
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from __future__ import absolute_import
  17. from __future__ import division
  18. import os
  19. import re
  20. import warnings
  21. import six
  22. from bcrypt import _bcrypt
  23. from .__about__ import (
  24. __author__, __copyright__, __email__, __license__, __summary__, __title__,
  25. __uri__, __version__,
  26. )
  27. __all__ = [
  28. "__title__", "__summary__", "__uri__", "__version__", "__author__",
  29. "__email__", "__license__", "__copyright__",
  30. "gensalt", "hashpw", "kdf",
  31. ]
  32. _normalize_re = re.compile(br"^\$2y\$")
  33. def gensalt(rounds=12, prefix=b"2b"):
  34. if prefix not in (b"2a", b"2b"):
  35. raise ValueError("Supported prefixes are b'2a' or b'2b'")
  36. if rounds < 4 or rounds > 31:
  37. raise ValueError("Invalid rounds")
  38. salt = os.urandom(16)
  39. output = _bcrypt.ffi.new("char[]", 30)
  40. _bcrypt.lib.encode_base64(output, salt, len(salt))
  41. return (
  42. b"$" + prefix + b"$" + ("%2.2u" % rounds).encode("ascii") + b"$" +
  43. _bcrypt.ffi.string(output)
  44. )
  45. def hashpw(password, salt):
  46. if isinstance(password, six.text_type) or isinstance(salt, six.text_type):
  47. raise TypeError("Unicode-objects must be encoded before hashing")
  48. if b"\x00" in password:
  49. raise ValueError("password may not contain NUL bytes")
  50. # bcrypt originally suffered from a wraparound bug:
  51. # http://www.openwall.com/lists/oss-security/2012/01/02/4
  52. # This bug was corrected in the OpenBSD source by truncating inputs to 72
  53. # bytes on the updated prefix $2b$, but leaving $2a$ unchanged for
  54. # compatibility. However, pyca/bcrypt 2.0.0 *did* correctly truncate inputs
  55. # on $2a$, so we do it here to preserve compatibility with 2.0.0
  56. password = password[:72]
  57. # When the original 8bit bug was found the original library we supported
  58. # added a new prefix, $2y$, that fixes it. This prefix is exactly the same
  59. # as the $2b$ prefix added by OpenBSD other than the name. Since the
  60. # OpenBSD library does not support the $2y$ prefix, if the salt given to us
  61. # is for the $2y$ prefix, we'll just mugne it so that it's a $2b$ prior to
  62. # passing it into the C library.
  63. original_salt, salt = salt, _normalize_re.sub(b"$2b$", salt)
  64. hashed = _bcrypt.ffi.new("char[]", 128)
  65. retval = _bcrypt.lib.bcrypt_hashpass(password, salt, hashed, len(hashed))
  66. if retval != 0:
  67. raise ValueError("Invalid salt")
  68. # Now that we've gotten our hashed password, we want to ensure that the
  69. # prefix we return is the one that was passed in, so we'll use the prefix
  70. # from the original salt and concatenate that with the return value (minus
  71. # the return value's prefix). This will ensure that if someone passed in a
  72. # salt with a $2y$ prefix, that they get back a hash with a $2y$ prefix
  73. # even though we munged it to $2b$.
  74. return original_salt[:4] + _bcrypt.ffi.string(hashed)[4:]
  75. def checkpw(password, hashed_password):
  76. if (isinstance(password, six.text_type) or
  77. isinstance(hashed_password, six.text_type)):
  78. raise TypeError("Unicode-objects must be encoded before checking")
  79. if b"\x00" in password or b"\x00" in hashed_password:
  80. raise ValueError(
  81. "password and hashed_password may not contain NUL bytes"
  82. )
  83. ret = hashpw(password, hashed_password)
  84. if len(ret) != len(hashed_password):
  85. return False
  86. return _bcrypt.lib.timingsafe_bcmp(ret, hashed_password, len(ret)) == 0
  87. def kdf(password, salt, desired_key_bytes, rounds, ignore_few_rounds=False):
  88. if isinstance(password, six.text_type) or isinstance(salt, six.text_type):
  89. raise TypeError("Unicode-objects must be encoded before hashing")
  90. if len(password) == 0 or len(salt) == 0:
  91. raise ValueError("password and salt must not be empty")
  92. if desired_key_bytes <= 0 or desired_key_bytes > 512:
  93. raise ValueError("desired_key_bytes must be 1-512")
  94. if rounds < 1:
  95. raise ValueError("rounds must be 1 or more")
  96. if rounds < 50 and not ignore_few_rounds:
  97. # They probably think bcrypt.kdf()'s rounds parameter is logarithmic,
  98. # expecting this value to be slow enough (it probably would be if this
  99. # were bcrypt). Emit a warning.
  100. warnings.warn((
  101. "Warning: bcrypt.kdf() called with only {0} round(s). "
  102. "This few is not secure: the parameter is linear, like PBKDF2.")
  103. .format(rounds),
  104. UserWarning)
  105. key = _bcrypt.ffi.new("uint8_t[]", desired_key_bytes)
  106. res = _bcrypt.lib.bcrypt_pbkdf(
  107. password, len(password), salt, len(salt), key, len(key), rounds
  108. )
  109. _bcrypt_assert(res == 0)
  110. return _bcrypt.ffi.buffer(key, desired_key_bytes)[:]
  111. def _bcrypt_assert(ok):
  112. if not ok:
  113. raise SystemError("bcrypt assertion failed")