You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.8 KiB

7 years ago
  1. # Copyright 2016 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. from __future__ import absolute_import, division, print_function
  15. import binascii
  16. import nacl.bindings
  17. from nacl.utils import bytes_as_string
  18. BYTES = nacl.bindings.crypto_generichash_BYTES
  19. BYTES_MIN = nacl.bindings.crypto_generichash_BYTES_MIN
  20. BYTES_MAX = nacl.bindings.crypto_generichash_BYTES_MAX
  21. KEYBYTES = nacl.bindings.crypto_generichash_KEYBYTES
  22. KEYBYTES_MIN = nacl.bindings.crypto_generichash_KEYBYTES_MIN
  23. KEYBYTES_MAX = nacl.bindings.crypto_generichash_KEYBYTES_MAX
  24. SALTBYTES = nacl.bindings.crypto_generichash_SALTBYTES
  25. PERSONALBYTES = nacl.bindings.crypto_generichash_PERSONALBYTES
  26. _b2b_init = nacl.bindings.crypto_generichash_blake2b_init
  27. _b2b_final = nacl.bindings.crypto_generichash_blake2b_final
  28. _b2b_copy = nacl.bindings.crypto_generichash_blake2b_state_copy
  29. _b2b_update = nacl.bindings.crypto_generichash_blake2b_update
  30. class blake2b(object):
  31. """
  32. :py:mod:`hashlib` API compatible blake2b algorithm implementation
  33. """
  34. MAX_DIGEST_SIZE = BYTES
  35. MAX_KEY_SIZE = KEYBYTES_MAX
  36. PERSON_SIZE = PERSONALBYTES
  37. SALT_SIZE = SALTBYTES
  38. def __init__(self, data=b'', digest_size=BYTES, key=b'',
  39. salt=b'', person=b''):
  40. """
  41. :py:class:`.blake2b` algorithm initializer
  42. :param data:
  43. :type data: bytes
  44. :param int digest_size: the requested digest size; must be
  45. at most :py:attr:`.MAX_DIGEST_SIZE`;
  46. the default digest size is :py:data:`.BYTES`
  47. :param key: the key to be set for keyed MAC/PRF usage; if set,
  48. the key must be at most :py:data:`.KEYBYTES_MAX` long
  49. :type key: bytes
  50. :param salt: an initialization salt at most
  51. :py:attr:`.SALT_SIZE` long; it will be zero-padded
  52. if needed
  53. :type salt: bytes
  54. :param person: a personalization string at most
  55. :py:attr:`.PERSONAL_SIZE` long; it will be zero-padded
  56. if needed
  57. :type person: bytes
  58. """
  59. self._state = _b2b_init(key=key, salt=salt, person=person,
  60. digest_size=digest_size)
  61. self._digest_size = digest_size
  62. if data:
  63. self.update(data)
  64. @property
  65. def digest_size(self):
  66. return self._digest_size
  67. @property
  68. def block_size(self):
  69. return 128
  70. @property
  71. def name(self):
  72. return 'blake2b'
  73. def update(self, data):
  74. _b2b_update(self._state, data)
  75. def digest(self):
  76. _st = nacl.bindings.crypto_generichash_blake2b_state_copy(self._state)
  77. return _b2b_final(_st, self.digest_size)
  78. def hexdigest(self):
  79. return bytes_as_string(binascii.hexlify(self.digest()))
  80. def copy(self):
  81. _cp = type(self)(digest_size=self.digest_size)
  82. _st = _b2b_copy(self._state)
  83. _cp._state = _st
  84. return _cp
  85. def scrypt(password, salt='', n=2**20, r=8, p=1,
  86. maxmem=2**25, dklen=64):
  87. """
  88. Derive a cryptographic key using the scrypt KDF.
  89. Implements the same signature as the ``hashlib.scrypt`` implemented
  90. in cpython version 3.6
  91. """
  92. return nacl.bindings.crypto_pwhash_scryptsalsa208sha256_ll(
  93. password, salt, n, r, p, maxmem=maxmem, dklen=dklen)

Powered by TurnKey Linux.