Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 1 | .. hazmat:: |
| 2 | |
| 3 | Key Derivation Functions |
| 4 | ======================== |
| 5 | |
| 6 | .. currentmodule:: cryptography.hazmat.primitives.kdf |
| 7 | |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 8 | Key derivation functions derive key material from passwords or other data |
| 9 | sources using a pseudo-random function (PRF). Each KDF is suitable for |
| 10 | different tasks (cryptographic key derivation, password storage, |
| 11 | key stretching) so match your needs to their capabilities. |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 12 | |
Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 13 | .. class:: PBKDF2HMAC(algorithm, length, salt, iterations, backend): |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 14 | |
Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 15 | .. versionadded:: 0.2 |
| 16 | |
Paul Kehrer | 298e533 | 2014-01-29 11:16:22 -0600 | [diff] [blame] | 17 | `PBKDF2`_ (Password Based Key Derivation Function 2) is typically used for |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 18 | deriving a cryptographic key from a password. It may also be used for |
Paul Kehrer | 298e533 | 2014-01-29 11:16:22 -0600 | [diff] [blame] | 19 | 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 Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 21 | |
Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 22 | This class conforms to the |
| 23 | :class:`~cryptography.hazmat.primitives.interfaces.KeyDerivationFunction` |
| 24 | interface. |
| 25 | |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 26 | .. doctest:: |
| 27 | |
Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 28 | >>> import os |
| 29 | >>> from cryptography.hazmat.primitives import hashes |
Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 30 | >>> from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 31 | >>> from cryptography.hazmat.backends import default_backend |
| 32 | >>> backend = default_backend() |
| 33 | >>> salt = os.urandom(16) |
| 34 | >>> # derive |
Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 35 | >>> kdf = PBKDF2HMAC( |
| 36 | ... algorithm=hashes.SHA256(), |
| 37 | ... length=32, |
| 38 | ... salt=salt, |
Paul Kehrer | 1277bc7 | 2014-01-28 17:09:59 -0600 | [diff] [blame] | 39 | ... iterations=100000, |
Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 40 | ... backend=backend |
| 41 | ... ) |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 42 | >>> key = kdf.derive(b"my great password") |
| 43 | >>> # verify |
Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 44 | >>> kdf = PBKDF2HMAC( |
| 45 | ... algorithm=hashes.SHA256(), |
| 46 | ... length=32, |
| 47 | ... salt=salt, |
Paul Kehrer | 1277bc7 | 2014-01-28 17:09:59 -0600 | [diff] [blame] | 48 | ... iterations=100000, |
Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 49 | ... backend=backend |
| 50 | ... ) |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 51 | >>> kdf.verify(b"my great password", key) |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 52 | |
Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 53 | :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 Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 57 | (2\ :sup:`32` - 1) * ``algorithm.digest_size``. |
Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 58 | :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 Kehrer | c58b478 | 2014-01-29 13:56:25 -0600 | [diff] [blame^] | 61 | function. This can be used to control the length of time the operation |
| 62 | takes. Higher numbers help mitigate brute force attacks against derived |
| 63 | keys. See OWASP's `Password Storage Cheat Sheet`_ for more |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 64 | detailed recommendations if you intend to use this for password storage. |
Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 65 | :param backend: A |
| 66 | :class:`~cryptography.hazmat.backends.interfaces.CipherBackend` |
| 67 | provider. |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 68 | |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 69 | .. method:: derive(key_material) |
| 70 | |
| 71 | :param key_material bytes: The input key material. For PBKDF2 this |
| 72 | should be a password. |
| 73 | :return: The new key. |
| 74 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 75 | :meth:`derive` or |
| 76 | :meth:`verify` is |
| 77 | called more than |
| 78 | once. |
| 79 | |
| 80 | This generates and returns a new key from the supplied password. |
| 81 | |
| 82 | .. method:: verify(key_material, expected_key) |
| 83 | |
| 84 | :param key_material bytes: The input key material. This is the same as |
| 85 | ``key_material`` in :meth:`derive`. |
| 86 | :param expected_key bytes: The expected result of deriving a new key, |
| 87 | this is the same as the return value of |
| 88 | :meth:`derive`. |
| 89 | :raises cryptography.exceptions.InvalidKey: This is raised when the |
| 90 | derived key does not match |
| 91 | the expected key. |
| 92 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 93 | :meth:`derive` or |
| 94 | :meth:`verify` is |
| 95 | called more than |
| 96 | once. |
| 97 | |
| 98 | This checks whether deriving a new key from the supplied |
| 99 | ``key_material`` generates the same key as the ``expected_key``, and |
| 100 | raises an exception if they do not match. This can be used for |
Paul Kehrer | 99d5190 | 2014-01-28 20:16:20 -0600 | [diff] [blame] | 101 | checking whether the password a user provides matches the stored derived |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 102 | key. |
| 103 | |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 104 | .. _`NIST SP 800-132`: http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf |
Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 105 | .. _`Password Storage Cheat Sheet`: https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet |
Paul Kehrer | 298e533 | 2014-01-29 11:16:22 -0600 | [diff] [blame] | 106 | .. _`PBKDF2`: http://en.wikipedia.org/wiki/PBKDF2 |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 107 | .. _`scrypt`: http://en.wikipedia.org/wiki/Scrypt |