-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Scrypt Implementation #3117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Scrypt Implementation #3117
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
06288ab
Scrypt implementation.
Ayrx ba96e7e
Docs stuff.
Ayrx d5d0c83
Make example just an example and not a doctest.
Ayrx 4e85946
Add changelog entry.
Ayrx 70a7ed0
Docs cleanup.
Ayrx ca40cf9
Add more tests.
Ayrx 5133441
Add multibackend tests.
Ayrx 79ffdac
PEP8.
Ayrx 271c4fa
Add docs about Scrypt parameters.
Ayrx 5f6a21b
Merge branch 'master' into scrypt-interface
Ayrx 9e19cd8
Docs cleanup.
Ayrx 39c46f2
Add AlreadyFinalized.
Ayrx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -737,6 +737,111 @@ Different KDFs are suitable for different tasks such as: | |
The counter iteration variable will be concatenated after | ||
the fixed input data. | ||
|
||
.. currentmodule:: cryptography.hazmat.primitives.kdf.scrypt | ||
|
||
.. class:: Scrypt(salt, length, n, r, p, backend) | ||
|
||
.. versionadded:: 1.6 | ||
|
||
Scrypt is a KDF designed for password storage by Colin Percival to be | ||
resistant against hardware-assisted attackers by having a tunable memory | ||
cost. It is described in :rfc:`7914`. | ||
|
||
This class conforms to the | ||
:class:`~cryptography.hazmat.primitives.kdf.KeyDerivationFunction` | ||
interface. | ||
|
||
.. code-block:: python | ||
|
||
>>> import os | ||
>>> from cryptography.hazmat.primitives import hashes | ||
>>> from cryptography.hazmat.primitives.kdf.scrypt import Scrypt | ||
>>> from cryptography.hazmat.backends import default_backend | ||
>>> backend = default_backend() | ||
>>> salt = os.urandom(16) | ||
>>> # derive | ||
>>> kdf = Scrypt( | ||
... salt=salt, | ||
... length=64, | ||
... n=1024, | ||
... r=8, | ||
... p=16, | ||
... backend=backend | ||
... ) | ||
>>> key = kdf.derive(b"my great password") | ||
>>> # verify | ||
>>> kdf = Scrypt( | ||
... salt=salt, | ||
... length=64, | ||
... n=1024, | ||
... r=8, | ||
... p=16, | ||
... backend=backend | ||
... ) | ||
>>> kdf.verify(b"my great password", key) | ||
|
||
:param bytes salt: A salt. | ||
:param int length: The desired length of the derived key. | ||
:param int n: CPU/Memory cost parameter. It must be larger than 1 and be a | ||
power of 2. | ||
:param int r: Block size parameter. | ||
:param int p: Parallelization parameter. | ||
|
||
The computational and memory cost of Scrypt can be adjusted by manipulating | ||
the 3 parameters: n, r and p. In general, the memory cost of Scrypt is | ||
affected by the values of both n and r while n also determines the number | ||
of iterations performed. p increases the computational cost without | ||
affecting memory usage. A more in-depth explanation of the 3 parameters can | ||
be found `here`_. | ||
|
||
:rfc:`7914` `recommends`_ values of r=8 and p=1 while scaling n to the | ||
number appropriate for your system. | ||
|
||
:param backend: An instance of | ||
:class:`~cryptography.hazmat.backends.interfaces.ScryptBackend`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't documented, that's why the tests are failing. |
||
|
||
:raises cryptography.exceptions.UnsupportedAlgorithm: This is raised if the | ||
provided ``backend`` does not implement | ||
:class:`~cryptography.hazmat.backends.interfaces.ScryptBackend` | ||
|
||
:raises TypeError: This exception is raised if ``salt`` is not ``bytes``. | ||
|
||
.. method:: derive(key_material) | ||
|
||
:param bytes key_material: The input key material. | ||
:return bytes: the derived key. | ||
:raises TypeError: This exception is raised if ``key_material`` is not | ||
``bytes``. | ||
:raises cryptography.exceptions.AlreadyFinalized: This is raised when | ||
:meth:`derive` or | ||
:meth:`verify` is | ||
called more than | ||
once. | ||
|
||
This generates and returns a new key from the supplied password. | ||
|
||
.. method:: verify(key_material, expected_key) | ||
|
||
:param bytes key_material: The input key material. This is the same as | ||
``key_material`` in :meth:`derive`. | ||
:param bytes expected_key: The expected result of deriving a new key, | ||
this is the same as the return value of | ||
:meth:`derive`. | ||
:raises cryptography.exceptions.InvalidKey: This is raised when the | ||
derived key does not match | ||
the expected key. | ||
:raises cryptography.exceptions.AlreadyFinalized: This is raised when | ||
:meth:`derive` or | ||
:meth:`verify` is | ||
called more than | ||
once. | ||
|
||
This checks whether deriving a new key from the supplied | ||
``key_material`` generates the same key as the ``expected_key``, and | ||
raises an exception if they do not match. This can be used for | ||
checking whether the password a user provides matches the stored derived | ||
key. | ||
|
||
Interface | ||
~~~~~~~~~ | ||
|
||
|
@@ -795,3 +900,5 @@ Interface | |
.. _`key stretching`: https://en.wikipedia.org/wiki/Key_stretching | ||
.. _`HKDF`: https://en.wikipedia.org/wiki/HKDF | ||
.. _`HKDF paper`: https://eprint.iacr.org/2010/264 | ||
.. _`here`: https://stackoverflow.com/a/30308723/1170681 | ||
.. _`recommends`: https://tools.ietf.org/html/rfc7914#section-2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# This file is dual licensed under the terms of the Apache License, Version | ||
# 2.0, and the BSD License. See the LICENSE file in the root of this repository | ||
# for complete details. | ||
|
||
from __future__ import absolute_import, division, print_function | ||
|
||
from cryptography import utils | ||
from cryptography.exceptions import ( | ||
AlreadyFinalized, InvalidKey, UnsupportedAlgorithm, _Reasons | ||
) | ||
from cryptography.hazmat.backends.interfaces import ScryptBackend | ||
from cryptography.hazmat.primitives import constant_time | ||
from cryptography.hazmat.primitives.kdf import KeyDerivationFunction | ||
|
||
|
||
@utils.register_interface(KeyDerivationFunction) | ||
class Scrypt(object): | ||
def __init__(self, salt, length, n, r, p, backend): | ||
if not isinstance(backend, ScryptBackend): | ||
raise UnsupportedAlgorithm( | ||
"Backend object does not implement ScryptBackend.", | ||
_Reasons.BACKEND_MISSING_INTERFACE | ||
) | ||
|
||
self._length = length | ||
if not isinstance(salt, bytes): | ||
raise TypeError("salt must be bytes.") | ||
self._used = False | ||
self._salt = salt | ||
self._n = n | ||
self._r = r | ||
self._p = p | ||
self._backend = backend | ||
|
||
def derive(self, key_material): | ||
if self._used: | ||
raise AlreadyFinalized("Scrypt instances can only be used once.") | ||
self._used = True | ||
|
||
if not isinstance(key_material, bytes): | ||
raise TypeError("key_material must be bytes.") | ||
return self._backend.derive_scrypt( | ||
key_material, self._salt, self._length, self._n, self._r, self._p | ||
) | ||
|
||
def verify(self, key_material, expected_key): | ||
derived_key = self.derive(key_material) | ||
if not constant_time.bytes_eq(derived_key, expected_key): | ||
raise InvalidKey("Keys do not match.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These params need better descriptions, there isn't enough info here for someone to intelligently use them