blob: e652ecbf12be61dc3ea1cc841581692b6e57c878 [file] [log] [blame]
Paul Kehrerb6d764c2014-01-27 22:32:11 -06001.. hazmat::
2
3Key Derivation Functions
4========================
5
6.. currentmodule:: cryptography.hazmat.primitives.kdf
7
Paul Kehrer3d8c66f2014-01-28 17:36:50 -06008Key derivation functions derive key material from passwords or other data
9sources using a pseudo-random function (PRF). Each KDF is suitable for
10different tasks (cryptographic key derivation, password storage,
11key stretching) so match your needs to their capabilities.
Paul Kehrerb6d764c2014-01-27 22:32:11 -060012
Paul Kehrerb3f763f2014-01-28 16:42:15 -060013.. class:: PBKDF2HMAC(algorithm, length, salt, iterations, backend):
Paul Kehrerb6d764c2014-01-27 22:32:11 -060014
Paul Kehrer5d1af212014-01-28 12:19:32 -060015 .. versionadded:: 0.2
16
Paul Kehrer298e5332014-01-29 11:16:22 -060017 `PBKDF2`_ (Password Based Key Derivation Function 2) is typically used for
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060018 deriving a cryptographic key from a password. It may also be used for
Paul Kehrer298e5332014-01-29 11:16:22 -060019 key storage, but an alternate key storage KDF such as `scrypt` is generally
20 considered a better solution since it is designed to be slow.
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060021
Paul Kehrer5d1af212014-01-28 12:19:32 -060022 This class conforms to the
23 :class:`~cryptography.hazmat.primitives.interfaces.KeyDerivationFunction`
24 interface.
25
Paul Kehrerb6d764c2014-01-27 22:32:11 -060026 .. doctest::
27
Paul Kehrer5d1af212014-01-28 12:19:32 -060028 >>> import os
29 >>> from cryptography.hazmat.primitives import hashes
Paul Kehrerb3f763f2014-01-28 16:42:15 -060030 >>> from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
Paul Kehrerb6d764c2014-01-27 22:32:11 -060031 >>> from cryptography.hazmat.backends import default_backend
32 >>> backend = default_backend()
33 >>> salt = os.urandom(16)
34 >>> # derive
Paul Kehrerb3f763f2014-01-28 16:42:15 -060035 >>> kdf = PBKDF2HMAC(
36 ... algorithm=hashes.SHA256(),
37 ... length=32,
38 ... salt=salt,
Paul Kehrer1277bc72014-01-28 17:09:59 -060039 ... iterations=100000,
Paul Kehrerb3f763f2014-01-28 16:42:15 -060040 ... backend=backend
41 ... )
Paul Kehrerb6d764c2014-01-27 22:32:11 -060042 >>> key = kdf.derive(b"my great password")
43 >>> # verify
Paul Kehrerb3f763f2014-01-28 16:42:15 -060044 >>> kdf = PBKDF2HMAC(
45 ... algorithm=hashes.SHA256(),
46 ... length=32,
47 ... salt=salt,
Paul Kehrer1277bc72014-01-28 17:09:59 -060048 ... iterations=100000,
Paul Kehrerb3f763f2014-01-28 16:42:15 -060049 ... backend=backend
50 ... )
Paul Kehrerb6d764c2014-01-27 22:32:11 -060051 >>> kdf.verify(b"my great password", key)
Paul Kehrerb6d764c2014-01-27 22:32:11 -060052
Paul Kehrer5d1af212014-01-28 12:19:32 -060053 :param algorithm: An instance of a
54 :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
55 provider.
56 :param int length: The desired length of the derived key. Maximum is
Paul Kehrerb3f763f2014-01-28 16:42:15 -060057 (2\ :sup:`32` - 1) * ``algorithm.digest_size``.
Paul Kehrer5d1af212014-01-28 12:19:32 -060058 :param bytes salt: A salt. `NIST SP 800-132`_ recommends 128-bits or
59 longer.
60 :param int iterations: The number of iterations to perform of the hash
Paul Kehrerb3f763f2014-01-28 16:42:15 -060061 function. See OWASP's `Password Storage Cheat Sheet`_ for more
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060062 detailed recommendations if you intend to use this for password storage.
Paul Kehrer5d1af212014-01-28 12:19:32 -060063 :param backend: A
64 :class:`~cryptography.hazmat.backends.interfaces.CipherBackend`
65 provider.
Paul Kehrerb6d764c2014-01-27 22:32:11 -060066
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060067 .. method:: derive(key_material)
68
69 :param key_material bytes: The input key material. For PBKDF2 this
70 should be a password.
71 :return: The new key.
72 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
73 :meth:`derive` or
74 :meth:`verify` is
75 called more than
76 once.
77
78 This generates and returns a new key from the supplied password.
79
80 .. method:: verify(key_material, expected_key)
81
82 :param key_material bytes: The input key material. This is the same as
83 ``key_material`` in :meth:`derive`.
84 :param expected_key bytes: The expected result of deriving a new key,
85 this is the same as the return value of
86 :meth:`derive`.
87 :raises cryptography.exceptions.InvalidKey: This is raised when the
88 derived key does not match
89 the expected key.
90 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
91 :meth:`derive` or
92 :meth:`verify` is
93 called more than
94 once.
95
96 This checks whether deriving a new key from the supplied
97 ``key_material`` generates the same key as the ``expected_key``, and
98 raises an exception if they do not match. This can be used for
Paul Kehrer99d51902014-01-28 20:16:20 -060099 checking whether the password a user provides matches the stored derived
Paul Kehrer3d8c66f2014-01-28 17:36:50 -0600100 key.
101
Paul Kehrerb6d764c2014-01-27 22:32:11 -0600102.. _`NIST SP 800-132`: http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf
Paul Kehrerb3f763f2014-01-28 16:42:15 -0600103.. _`Password Storage Cheat Sheet`: https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet
Paul Kehrer298e5332014-01-29 11:16:22 -0600104.. _`PBKDF2`: http://en.wikipedia.org/wiki/PBKDF2
Paul Kehrer3d8c66f2014-01-28 17:36:50 -0600105.. _`scrypt`: http://en.wikipedia.org/wiki/Scrypt