__init__.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 hmac
  19. import os
  20. import re
  21. import warnings
  22. from .__about__ import (
  23. __author__,
  24. __copyright__,
  25. __email__,
  26. __license__,
  27. __summary__,
  28. __title__,
  29. __uri__,
  30. __version__,
  31. )
  32. from . import _bcrypt # noqa: I100
  33. __all__ = [
  34. "__title__",
  35. "__summary__",
  36. "__uri__",
  37. "__version__",
  38. "__author__",
  39. "__email__",
  40. "__license__",
  41. "__copyright__",
  42. "gensalt",
  43. "hashpw",
  44. "kdf",
  45. "checkpw",
  46. ]
  47. _normalize_re = re.compile(rb"^\$2y\$")
  48. def gensalt(rounds: int = 12, prefix: bytes = b"2b") -> bytes:
  49. if prefix not in (b"2a", b"2b"):
  50. raise ValueError("Supported prefixes are b'2a' or b'2b'")
  51. if rounds < 4 or rounds > 31:
  52. raise ValueError("Invalid rounds")
  53. salt = os.urandom(16)
  54. output = _bcrypt.ffi.new("char[]", 30)
  55. _bcrypt.lib.encode_base64(output, salt, len(salt))
  56. return (
  57. b"$"
  58. + prefix
  59. + b"$"
  60. + ("%2.2u" % rounds).encode("ascii")
  61. + b"$"
  62. + _bcrypt.ffi.string(output)
  63. )
  64. def hashpw(password: bytes, salt: bytes) -> bytes:
  65. if isinstance(password, str) or isinstance(salt, str):
  66. raise TypeError("Strings must be encoded before hashing")
  67. if b"\x00" in password:
  68. raise ValueError("password may not contain NUL bytes")
  69. # bcrypt originally suffered from a wraparound bug:
  70. # http://www.openwall.com/lists/oss-security/2012/01/02/4
  71. # This bug was corrected in the OpenBSD source by truncating inputs to 72
  72. # bytes on the updated prefix $2b$, but leaving $2a$ unchanged for
  73. # compatibility. However, pyca/bcrypt 2.0.0 *did* correctly truncate inputs
  74. # on $2a$, so we do it here to preserve compatibility with 2.0.0
  75. password = password[:72]
  76. # When the original 8bit bug was found the original library we supported
  77. # added a new prefix, $2y$, that fixes it. This prefix is exactly the same
  78. # as the $2b$ prefix added by OpenBSD other than the name. Since the
  79. # OpenBSD library does not support the $2y$ prefix, if the salt given to us
  80. # is for the $2y$ prefix, we'll just mugne it so that it's a $2b$ prior to
  81. # passing it into the C library.
  82. original_salt, salt = salt, _normalize_re.sub(b"$2b$", salt)
  83. hashed = _bcrypt.ffi.new("char[]", 128)
  84. retval = _bcrypt.lib.bcrypt_hashpass(password, salt, hashed, len(hashed))
  85. if retval != 0:
  86. raise ValueError("Invalid salt")
  87. # Now that we've gotten our hashed password, we want to ensure that the
  88. # prefix we return is the one that was passed in, so we'll use the prefix
  89. # from the original salt and concatenate that with the return value (minus
  90. # the return value's prefix). This will ensure that if someone passed in a
  91. # salt with a $2y$ prefix, that they get back a hash with a $2y$ prefix
  92. # even though we munged it to $2b$.
  93. return original_salt[:4] + _bcrypt.ffi.string(hashed)[4:]
  94. def checkpw(password: bytes, hashed_password: bytes) -> bool:
  95. if isinstance(password, str) or isinstance(hashed_password, str):
  96. raise TypeError("Strings must be encoded before checking")
  97. if b"\x00" in password or b"\x00" in hashed_password:
  98. raise ValueError(
  99. "password and hashed_password may not contain NUL bytes"
  100. )
  101. ret = hashpw(password, hashed_password)
  102. return hmac.compare_digest(ret, hashed_password)
  103. def kdf(
  104. password: bytes,
  105. salt: bytes,
  106. desired_key_bytes: int,
  107. rounds: int,
  108. ignore_few_rounds: bool = False,
  109. ) -> bytes:
  110. if isinstance(password, str) or isinstance(salt, str):
  111. raise TypeError("Strings must be encoded before hashing")
  112. if len(password) == 0 or len(salt) == 0:
  113. raise ValueError("password and salt must not be empty")
  114. if desired_key_bytes <= 0 or desired_key_bytes > 512:
  115. raise ValueError("desired_key_bytes must be 1-512")
  116. if rounds < 1:
  117. raise ValueError("rounds must be 1 or more")
  118. if rounds < 50 and not ignore_few_rounds:
  119. # They probably think bcrypt.kdf()'s rounds parameter is logarithmic,
  120. # expecting this value to be slow enough (it probably would be if this
  121. # were bcrypt). Emit a warning.
  122. warnings.warn(
  123. (
  124. "Warning: bcrypt.kdf() called with only {0} round(s). "
  125. "This few is not secure: the parameter is linear, like PBKDF2."
  126. ).format(rounds),
  127. UserWarning,
  128. stacklevel=2,
  129. )
  130. key = _bcrypt.ffi.new("uint8_t[]", desired_key_bytes)
  131. res = _bcrypt.lib.bcrypt_pbkdf(
  132. password, len(password), salt, len(salt), key, len(key), rounds
  133. )
  134. _bcrypt_assert(res == 0)
  135. return _bcrypt.ffi.buffer(key, desired_key_bytes)[:]
  136. def _bcrypt_assert(ok: bool) -> None:
  137. if not ok:
  138. raise SystemError("bcrypt assertion failed")