argon2id.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Copyright 2013 Donald Stufft and individual contributors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import nacl.bindings
  15. import nacl.encoding
  16. from . import _argon2
  17. ALG = _argon2.ALG_ARGON2ID13
  18. STRPREFIX = nacl.bindings.crypto_pwhash_argon2id_STRPREFIX
  19. SALTBYTES = _argon2.SALTBYTES
  20. PASSWD_MIN = _argon2.PASSWD_MIN
  21. PASSWD_MAX = _argon2.PASSWD_MAX
  22. PWHASH_SIZE = _argon2.PWHASH_SIZE
  23. BYTES_MIN = _argon2.BYTES_MIN
  24. BYTES_MAX = _argon2.BYTES_MAX
  25. verify = _argon2.verify
  26. MEMLIMIT_MIN = nacl.bindings.crypto_pwhash_argon2id_MEMLIMIT_MIN
  27. MEMLIMIT_MAX = nacl.bindings.crypto_pwhash_argon2id_MEMLIMIT_MAX
  28. OPSLIMIT_MIN = nacl.bindings.crypto_pwhash_argon2id_OPSLIMIT_MIN
  29. OPSLIMIT_MAX = nacl.bindings.crypto_pwhash_argon2id_OPSLIMIT_MAX
  30. OPSLIMIT_INTERACTIVE = (
  31. nacl.bindings.crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE
  32. )
  33. MEMLIMIT_INTERACTIVE = (
  34. nacl.bindings.crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE
  35. )
  36. OPSLIMIT_SENSITIVE = nacl.bindings.crypto_pwhash_argon2id_OPSLIMIT_SENSITIVE
  37. MEMLIMIT_SENSITIVE = nacl.bindings.crypto_pwhash_argon2id_MEMLIMIT_SENSITIVE
  38. OPSLIMIT_MODERATE = nacl.bindings.crypto_pwhash_argon2id_OPSLIMIT_MODERATE
  39. MEMLIMIT_MODERATE = nacl.bindings.crypto_pwhash_argon2id_MEMLIMIT_MODERATE
  40. def kdf(
  41. size: int,
  42. password: bytes,
  43. salt: bytes,
  44. opslimit: int = OPSLIMIT_SENSITIVE,
  45. memlimit: int = MEMLIMIT_SENSITIVE,
  46. encoder: nacl.encoding.Encoder = nacl.encoding.RawEncoder,
  47. ) -> bytes:
  48. """
  49. Derive a ``size`` bytes long key from a caller-supplied
  50. ``password`` and ``salt`` pair using the argon2i
  51. memory-hard construct.
  52. the enclosing module provides the constants
  53. - :py:const:`.OPSLIMIT_INTERACTIVE`
  54. - :py:const:`.MEMLIMIT_INTERACTIVE`
  55. - :py:const:`.OPSLIMIT_MODERATE`
  56. - :py:const:`.MEMLIMIT_MODERATE`
  57. - :py:const:`.OPSLIMIT_SENSITIVE`
  58. - :py:const:`.MEMLIMIT_SENSITIVE`
  59. as a guidance for correct settings.
  60. :param size: derived key size, must be between
  61. :py:const:`.BYTES_MIN` and
  62. :py:const:`.BYTES_MAX`
  63. :type size: int
  64. :param password: password used to seed the key derivation procedure;
  65. it length must be between
  66. :py:const:`.PASSWD_MIN` and
  67. :py:const:`.PASSWD_MAX`
  68. :type password: bytes
  69. :param salt: **RANDOM** salt used in the key derivation procedure;
  70. its length must be exactly :py:const:`.SALTBYTES`
  71. :type salt: bytes
  72. :param opslimit: the time component (operation count)
  73. of the key derivation procedure's computational cost;
  74. it must be between
  75. :py:const:`.OPSLIMIT_MIN` and
  76. :py:const:`.OPSLIMIT_MAX`
  77. :type opslimit: int
  78. :param memlimit: the memory occupation component
  79. of the key derivation procedure's computational cost;
  80. it must be between
  81. :py:const:`.MEMLIMIT_MIN` and
  82. :py:const:`.MEMLIMIT_MAX`
  83. :type memlimit: int
  84. :rtype: bytes
  85. .. versionadded:: 1.2
  86. """
  87. return encoder.encode(
  88. nacl.bindings.crypto_pwhash_alg(
  89. size, password, salt, opslimit, memlimit, ALG
  90. )
  91. )
  92. def str(
  93. password: bytes,
  94. opslimit: int = OPSLIMIT_INTERACTIVE,
  95. memlimit: int = MEMLIMIT_INTERACTIVE,
  96. ) -> bytes:
  97. """
  98. Hashes a password with a random salt, using the memory-hard
  99. argon2id construct and returning an ascii string that has all
  100. the needed info to check against a future password
  101. The default settings for opslimit and memlimit are those deemed
  102. correct for the interactive user login case.
  103. :param bytes password:
  104. :param int opslimit:
  105. :param int memlimit:
  106. :rtype: bytes
  107. .. versionadded:: 1.2
  108. """
  109. return nacl.bindings.crypto_pwhash_str_alg(
  110. password, opslimit, memlimit, ALG
  111. )