blob: 56c3a2bd37b8bc26a66a78be987ecc46989fdb14 [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
Paul Kehrer1cab1042014-01-29 14:30:11 -06009sources using a pseudo-random function (PRF). Different KDFs are suitable for
10different 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 Kehrerb6d764c2014-01-27 22:32:11 -060027
Paul Kehrerb3f763f2014-01-28 16:42:15 -060028.. class:: PBKDF2HMAC(algorithm, length, salt, iterations, backend):
Paul Kehrerb6d764c2014-01-27 22:32:11 -060029
Paul Kehrer5d1af212014-01-28 12:19:32 -060030 .. versionadded:: 0.2
31
Paul Kehrer298e5332014-01-29 11:16:22 -060032 `PBKDF2`_ (Password Based Key Derivation Function 2) is typically used for
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060033 deriving a cryptographic key from a password. It may also be used for
Paul Kehrer298e5332014-01-29 11:16:22 -060034 key storage, but an alternate key storage KDF such as `scrypt` is generally
Paul Kehrer1cab1042014-01-29 14:30:11 -060035 considered a better solution.
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060036
Paul Kehrer5d1af212014-01-28 12:19:32 -060037 This class conforms to the
38 :class:`~cryptography.hazmat.primitives.interfaces.KeyDerivationFunction`
39 interface.
40
Paul Kehrerb6d764c2014-01-27 22:32:11 -060041 .. doctest::
42
Paul Kehrer5d1af212014-01-28 12:19:32 -060043 >>> import os
44 >>> from cryptography.hazmat.primitives import hashes
Paul Kehrerb3f763f2014-01-28 16:42:15 -060045 >>> from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
Paul Kehrerb6d764c2014-01-27 22:32:11 -060046 >>> from cryptography.hazmat.backends import default_backend
47 >>> backend = default_backend()
48 >>> salt = os.urandom(16)
49 >>> # derive
Paul Kehrerb3f763f2014-01-28 16:42:15 -060050 >>> kdf = PBKDF2HMAC(
51 ... algorithm=hashes.SHA256(),
52 ... length=32,
53 ... salt=salt,
Paul Kehrer1277bc72014-01-28 17:09:59 -060054 ... iterations=100000,
Paul Kehrerb3f763f2014-01-28 16:42:15 -060055 ... backend=backend
56 ... )
Paul Kehrerb6d764c2014-01-27 22:32:11 -060057 >>> key = kdf.derive(b"my great password")
58 >>> # verify
Paul Kehrerb3f763f2014-01-28 16:42:15 -060059 >>> kdf = PBKDF2HMAC(
60 ... algorithm=hashes.SHA256(),
61 ... length=32,
62 ... salt=salt,
Paul Kehrer1277bc72014-01-28 17:09:59 -060063 ... iterations=100000,
Paul Kehrerb3f763f2014-01-28 16:42:15 -060064 ... backend=backend
65 ... )
Paul Kehrerb6d764c2014-01-27 22:32:11 -060066 >>> kdf.verify(b"my great password", key)
Paul Kehrerb6d764c2014-01-27 22:32:11 -060067
Paul Kehrer5d1af212014-01-28 12:19:32 -060068 :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 Kehrerb3f763f2014-01-28 16:42:15 -060072 (2\ :sup:`32` - 1) * ``algorithm.digest_size``.
Paul Kehrer5d1af212014-01-28 12:19:32 -060073 :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 Kehrerc58b4782014-01-29 13:56:25 -060076 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 Kehrer3d8c66f2014-01-28 17:36:50 -060079 detailed recommendations if you intend to use this for password storage.
Paul Kehrer5d1af212014-01-28 12:19:32 -060080 :param backend: A
81 :class:`~cryptography.hazmat.backends.interfaces.CipherBackend`
82 provider.
Paul Kehrerb6d764c2014-01-27 22:32:11 -060083
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060084 .. 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 Kehrer99d51902014-01-28 20:16:20 -0600116 checking whether the password a user provides matches the stored derived
Paul Kehrer3d8c66f2014-01-28 17:36:50 -0600117 key.
118
Paul Kehrerb6d764c2014-01-27 22:32:11 -0600119.. _`NIST SP 800-132`: http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf
Paul Kehrerb3f763f2014-01-28 16:42:15 -0600120.. _`Password Storage Cheat Sheet`: https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet
Paul Kehrer298e5332014-01-29 11:16:22 -0600121.. _`PBKDF2`: http://en.wikipedia.org/wiki/PBKDF2
Paul Kehrer3d8c66f2014-01-28 17:36:50 -0600122.. _`scrypt`: http://en.wikipedia.org/wiki/Scrypt
Paul Kehrer1cab1042014-01-29 14:30:11 -0600123.. _`key stretching`: http://en.wikipedia.org/wiki/Key_stretching