argon2i.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_ARGON2I13
  18. STRPREFIX = nacl.bindings.crypto_pwhash_argon2i_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_MAX = nacl.bindings.crypto_pwhash_argon2i_MEMLIMIT_MAX
  27. MEMLIMIT_MIN = nacl.bindings.crypto_pwhash_argon2i_MEMLIMIT_MIN
  28. OPSLIMIT_MAX = nacl.bindings.crypto_pwhash_argon2i_OPSLIMIT_MAX
  29. OPSLIMIT_MIN = nacl.bindings.crypto_pwhash_argon2i_OPSLIMIT_MIN
  30. OPSLIMIT_INTERACTIVE = nacl.bindings.crypto_pwhash_argon2i_OPSLIMIT_INTERACTIVE
  31. MEMLIMIT_INTERACTIVE = nacl.bindings.crypto_pwhash_argon2i_MEMLIMIT_INTERACTIVE
  32. OPSLIMIT_SENSITIVE = nacl.bindings.crypto_pwhash_argon2i_OPSLIMIT_SENSITIVE
  33. MEMLIMIT_SENSITIVE = nacl.bindings.crypto_pwhash_argon2i_MEMLIMIT_SENSITIVE
  34. OPSLIMIT_MODERATE = nacl.bindings.crypto_pwhash_argon2i_OPSLIMIT_MODERATE
  35. MEMLIMIT_MODERATE = nacl.bindings.crypto_pwhash_argon2i_MEMLIMIT_MODERATE
  36. def kdf(
  37. size: int,
  38. password: bytes,
  39. salt: bytes,
  40. opslimit: int = OPSLIMIT_SENSITIVE,
  41. memlimit: int = MEMLIMIT_SENSITIVE,
  42. encoder: nacl.encoding.Encoder = nacl.encoding.RawEncoder,
  43. ) -> bytes:
  44. """
  45. Derive a ``size`` bytes long key from a caller-supplied
  46. ``password`` and ``salt`` pair using the argon2i
  47. memory-hard construct.
  48. the enclosing module provides the constants
  49. - :py:const:`.OPSLIMIT_INTERACTIVE`
  50. - :py:const:`.MEMLIMIT_INTERACTIVE`
  51. - :py:const:`.OPSLIMIT_MODERATE`
  52. - :py:const:`.MEMLIMIT_MODERATE`
  53. - :py:const:`.OPSLIMIT_SENSITIVE`
  54. - :py:const:`.MEMLIMIT_SENSITIVE`
  55. as a guidance for correct settings.
  56. :param size: derived key size, must be between
  57. :py:const:`.BYTES_MIN` and
  58. :py:const:`.BYTES_MAX`
  59. :type size: int
  60. :param password: password used to seed the key derivation procedure;
  61. it length must be between
  62. :py:const:`.PASSWD_MIN` and
  63. :py:const:`.PASSWD_MAX`
  64. :type password: bytes
  65. :param salt: **RANDOM** salt used in the key derivation procedure;
  66. its length must be exactly :py:const:`.SALTBYTES`
  67. :type salt: bytes
  68. :param opslimit: the time component (operation count)
  69. of the key derivation procedure's computational cost;
  70. it must be between
  71. :py:const:`.OPSLIMIT_MIN` and
  72. :py:const:`.OPSLIMIT_MAX`
  73. :type opslimit: int
  74. :param memlimit: the memory occupation component
  75. of the key derivation procedure's computational cost;
  76. it must be between
  77. :py:const:`.MEMLIMIT_MIN` and
  78. :py:const:`.MEMLIMIT_MAX`
  79. :type memlimit: int
  80. :rtype: bytes
  81. .. versionadded:: 1.2
  82. """
  83. return encoder.encode(
  84. nacl.bindings.crypto_pwhash_alg(
  85. size, password, salt, opslimit, memlimit, ALG
  86. )
  87. )
  88. def str(
  89. password: bytes,
  90. opslimit: int = OPSLIMIT_INTERACTIVE,
  91. memlimit: int = MEMLIMIT_INTERACTIVE,
  92. ) -> bytes:
  93. """
  94. Hashes a password with a random salt, using the memory-hard
  95. argon2i construct and returning an ascii string that has all
  96. the needed info to check against a future password
  97. The default settings for opslimit and memlimit are those deemed
  98. correct for the interactive user login case.
  99. :param bytes password:
  100. :param int opslimit:
  101. :param int memlimit:
  102. :rtype: bytes
  103. .. versionadded:: 1.2
  104. """
  105. return nacl.bindings.crypto_pwhash_str_alg(
  106. password, opslimit, memlimit, ALG
  107. )