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 |
Paul Kehrer | 1cab104 | 2014-01-29 14:30:11 -0600 | [diff] [blame^] | 9 | sources using a pseudo-random function (PRF). Different KDFs are suitable for |
| 10 | different tasks such as: |
| 11 | |
| 12 | - Cryptographic key derivation |
| 13 | |
| 14 | Deriving a key suitable for use as input to an encryption algorithm. |
| 15 | Typically this means taking a password and running it through an algorithm |
| 16 | such as :class:`~cryptography.hazmat.primitives.kdf.PBKDF2HMAC` or HKDF. |
| 17 | This process is typically known as `key stretching`_. |
| 18 | |
| 19 | - Password storage |
| 20 | |
| 21 | When storing passwords you want to use an algorithm that is computationally |
| 22 | intensive. Legitimate users will only need to compute it once (for example, |
| 23 | taking the user's password, running it through the KDF, then comparing it |
| 24 | to the stored value), while attackers will need to do it billions of times. |
| 25 | Ideal password storage KDFs will be demanding on both computational and |
| 26 | memory resources. |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 27 | |
Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 28 | .. class:: PBKDF2HMAC(algorithm, length, salt, iterations, backend): |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 29 | |
Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 30 | .. versionadded:: 0.2 |
| 31 | |
Paul Kehrer | 298e533 | 2014-01-29 11:16:22 -0600 | [diff] [blame] | 32 | `PBKDF2`_ (Password Based Key Derivation Function 2) is typically used for |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 33 | 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] | 34 | key storage, but an alternate key storage KDF such as `scrypt` is generally |
Paul Kehrer | 1cab104 | 2014-01-29 14:30:11 -0600 | [diff] [blame^] | 35 | considered a better solution. |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 36 | |
Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 37 | This class conforms to the |
| 38 | :class:`~cryptography.hazmat.primitives.interfaces.KeyDerivationFunction` |
| 39 | interface. |
| 40 | |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 41 | .. doctest:: |
| 42 | |
Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 43 | >>> import os |
| 44 | >>> from cryptography.hazmat.primitives import hashes |
Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 45 | >>> from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 46 | >>> from cryptography.hazmat.backends import default_backend |
| 47 | >>> backend = default_backend() |
| 48 | >>> salt = os.urandom(16) |
| 49 | >>> # derive |
Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 50 | >>> kdf = PBKDF2HMAC( |
| 51 | ... algorithm=hashes.SHA256(), |
| 52 | ... length=32, |
| 53 | ... salt=salt, |
Paul Kehrer | 1277bc7 | 2014-01-28 17:09:59 -0600 | [diff] [blame] | 54 | ... iterations=100000, |
Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 55 | ... backend=backend |
| 56 | ... ) |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 57 | >>> key = kdf.derive(b"my great password") |
| 58 | >>> # verify |
Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 59 | >>> kdf = PBKDF2HMAC( |
| 60 | ... algorithm=hashes.SHA256(), |
| 61 | ... length=32, |
| 62 | ... salt=salt, |
Paul Kehrer | 1277bc7 | 2014-01-28 17:09:59 -0600 | [diff] [blame] | 63 | ... iterations=100000, |
Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 64 | ... backend=backend |
| 65 | ... ) |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 66 | >>> kdf.verify(b"my great password", key) |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 67 | |
Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 68 | :param algorithm: An instance of a |
| 69 | :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` |
| 70 | provider. |
| 71 | :param int length: The desired length of the derived key. Maximum is |
Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 72 | (2\ :sup:`32` - 1) * ``algorithm.digest_size``. |
Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 73 | :param bytes salt: A salt. `NIST SP 800-132`_ recommends 128-bits or |
| 74 | longer. |
| 75 | :param int iterations: The number of iterations to perform of the hash |
Paul Kehrer | c58b478 | 2014-01-29 13:56:25 -0600 | [diff] [blame] | 76 | function. This can be used to control the length of time the operation |
| 77 | takes. Higher numbers help mitigate brute force attacks against derived |
| 78 | keys. See OWASP's `Password Storage Cheat Sheet`_ for more |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 79 | detailed recommendations if you intend to use this for password storage. |
Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 80 | :param backend: A |
| 81 | :class:`~cryptography.hazmat.backends.interfaces.CipherBackend` |
| 82 | provider. |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 83 | |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 84 | .. method:: derive(key_material) |
| 85 | |
| 86 | :param key_material bytes: The input key material. For PBKDF2 this |
| 87 | should be a password. |
| 88 | :return: The new key. |
| 89 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 90 | :meth:`derive` or |
| 91 | :meth:`verify` is |
| 92 | called more than |
| 93 | once. |
| 94 | |
| 95 | This generates and returns a new key from the supplied password. |
| 96 | |
| 97 | .. method:: verify(key_material, expected_key) |
| 98 | |
| 99 | :param key_material bytes: The input key material. This is the same as |
| 100 | ``key_material`` in :meth:`derive`. |
| 101 | :param expected_key bytes: The expected result of deriving a new key, |
| 102 | this is the same as the return value of |
| 103 | :meth:`derive`. |
| 104 | :raises cryptography.exceptions.InvalidKey: This is raised when the |
| 105 | derived key does not match |
| 106 | the expected key. |
| 107 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 108 | :meth:`derive` or |
| 109 | :meth:`verify` is |
| 110 | called more than |
| 111 | once. |
| 112 | |
| 113 | This checks whether deriving a new key from the supplied |
| 114 | ``key_material`` generates the same key as the ``expected_key``, and |
| 115 | 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] | 116 | checking whether the password a user provides matches the stored derived |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 117 | key. |
| 118 | |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 119 | .. _`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] | 120 | .. _`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] | 121 | .. _`PBKDF2`: http://en.wikipedia.org/wiki/PBKDF2 |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 122 | .. _`scrypt`: http://en.wikipedia.org/wiki/Scrypt |
Paul Kehrer | 1cab104 | 2014-01-29 14:30:11 -0600 | [diff] [blame^] | 123 | .. _`key stretching`: http://en.wikipedia.org/wiki/Key_stretching |