blob: 851dbb0b3464c8692e4de134b27c8605c1b9f2c1 [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 Kehrer0b181182014-01-29 16:34:47 -06008Key derivation functions derive bytes suitable for cryptographic operations
9from passwords or other data sources using a pseudo-random function (PRF).
10Different KDFs are suitable for different tasks such as:
Paul Kehrer1cab1042014-01-29 14:30:11 -060011
Paul Kehrer0b181182014-01-29 16:34:47 -060012* Cryptographic key derivation
Paul Kehrer1cab1042014-01-29 14:30:11 -060013
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
David Reidc0248b92014-01-30 15:23:33 -080016 such as :class:`~cryptography.hazmat.primitives.kdf.pbkdf2.PBKDF2HMAC` or
17 :class:`~cryptography.hazmat.primitives.kdf.hkdf.HKDF`.
Paul Kehrer1cab1042014-01-29 14:30:11 -060018 This process is typically known as `key stretching`_.
19
Paul Kehrer0b181182014-01-29 16:34:47 -060020* Password storage
Paul Kehrer1cab1042014-01-29 14:30:11 -060021
22 When storing passwords you want to use an algorithm that is computationally
23 intensive. Legitimate users will only need to compute it once (for example,
24 taking the user's password, running it through the KDF, then comparing it
25 to the stored value), while attackers will need to do it billions of times.
26 Ideal password storage KDFs will be demanding on both computational and
27 memory resources.
Paul Kehrerb6d764c2014-01-27 22:32:11 -060028
Paul Kehrer0b181182014-01-29 16:34:47 -060029.. currentmodule:: cryptography.hazmat.primitives.kdf.pbkdf2
30
31.. class:: PBKDF2HMAC(algorithm, length, salt, iterations, backend)
Paul Kehrerb6d764c2014-01-27 22:32:11 -060032
Paul Kehrer5d1af212014-01-28 12:19:32 -060033 .. versionadded:: 0.2
34
Paul Kehrer298e5332014-01-29 11:16:22 -060035 `PBKDF2`_ (Password Based Key Derivation Function 2) is typically used for
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060036 deriving a cryptographic key from a password. It may also be used for
Paul Kehrer0b181182014-01-29 16:34:47 -060037 key storage, but an alternate key storage KDF such as `scrypt`_ is generally
Paul Kehrer1cab1042014-01-29 14:30:11 -060038 considered a better solution.
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060039
Paul Kehrer5d1af212014-01-28 12:19:32 -060040 This class conforms to the
41 :class:`~cryptography.hazmat.primitives.interfaces.KeyDerivationFunction`
42 interface.
43
Paul Kehrerb6d764c2014-01-27 22:32:11 -060044 .. doctest::
45
Paul Kehrer5d1af212014-01-28 12:19:32 -060046 >>> import os
47 >>> from cryptography.hazmat.primitives import hashes
Paul Kehrerb3f763f2014-01-28 16:42:15 -060048 >>> from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
Paul Kehrerb6d764c2014-01-27 22:32:11 -060049 >>> from cryptography.hazmat.backends import default_backend
50 >>> backend = default_backend()
51 >>> salt = os.urandom(16)
52 >>> # derive
Paul Kehrerb3f763f2014-01-28 16:42:15 -060053 >>> kdf = PBKDF2HMAC(
54 ... algorithm=hashes.SHA256(),
55 ... length=32,
56 ... salt=salt,
Paul Kehrer1277bc72014-01-28 17:09:59 -060057 ... iterations=100000,
Paul Kehrerb3f763f2014-01-28 16:42:15 -060058 ... backend=backend
59 ... )
Paul Kehrerb6d764c2014-01-27 22:32:11 -060060 >>> key = kdf.derive(b"my great password")
61 >>> # verify
Paul Kehrerb3f763f2014-01-28 16:42:15 -060062 >>> kdf = PBKDF2HMAC(
63 ... algorithm=hashes.SHA256(),
64 ... length=32,
65 ... salt=salt,
Paul Kehrer1277bc72014-01-28 17:09:59 -060066 ... iterations=100000,
Paul Kehrerb3f763f2014-01-28 16:42:15 -060067 ... backend=backend
68 ... )
Paul Kehrerb6d764c2014-01-27 22:32:11 -060069 >>> kdf.verify(b"my great password", key)
Paul Kehrerb6d764c2014-01-27 22:32:11 -060070
Paul Kehrer5d1af212014-01-28 12:19:32 -060071 :param algorithm: An instance of a
72 :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
73 provider.
74 :param int length: The desired length of the derived key. Maximum is
Paul Kehrerb3f763f2014-01-28 16:42:15 -060075 (2\ :sup:`32` - 1) * ``algorithm.digest_size``.
Paul Kehrer5d1af212014-01-28 12:19:32 -060076 :param bytes salt: A salt. `NIST SP 800-132`_ recommends 128-bits or
77 longer.
78 :param int iterations: The number of iterations to perform of the hash
Paul Kehrerc58b4782014-01-29 13:56:25 -060079 function. This can be used to control the length of time the operation
80 takes. Higher numbers help mitigate brute force attacks against derived
81 keys. See OWASP's `Password Storage Cheat Sheet`_ for more
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060082 detailed recommendations if you intend to use this for password storage.
Paul Kehrer5d1af212014-01-28 12:19:32 -060083 :param backend: A
Paul Kehrer15a86a02014-01-29 17:44:47 -060084 :class:`~cryptography.hazmat.backends.interfaces.PBKDF2HMACBackend`
Paul Kehrer5d1af212014-01-28 12:19:32 -060085 provider.
Paul Kehrerb6d764c2014-01-27 22:32:11 -060086
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060087 .. method:: derive(key_material)
88
Alex Gaynora8e125f2014-01-29 19:21:03 -080089 :param bytes key_material: The input key material. For PBKDF2 this
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060090 should be a password.
Paul Kehrer0b181182014-01-29 16:34:47 -060091 :return bytes: the derived key.
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060092 :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 generates and returns a new key from the supplied password.
99
100 .. method:: verify(key_material, expected_key)
101
Alex Gaynora8e125f2014-01-29 19:21:03 -0800102 :param bytes key_material: The input key material. This is the same as
Paul Kehrer3d8c66f2014-01-28 17:36:50 -0600103 ``key_material`` in :meth:`derive`.
Alex Gaynora8e125f2014-01-29 19:21:03 -0800104 :param bytes expected_key: The expected result of deriving a new key,
Paul Kehrer3d8c66f2014-01-28 17:36:50 -0600105 this is the same as the return value of
106 :meth:`derive`.
107 :raises cryptography.exceptions.InvalidKey: This is raised when the
108 derived key does not match
109 the expected key.
110 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
111 :meth:`derive` or
112 :meth:`verify` is
113 called more than
114 once.
115
116 This checks whether deriving a new key from the supplied
117 ``key_material`` generates the same key as the ``expected_key``, and
118 raises an exception if they do not match. This can be used for
Paul Kehrer99d51902014-01-28 20:16:20 -0600119 checking whether the password a user provides matches the stored derived
Paul Kehrer3d8c66f2014-01-28 17:36:50 -0600120 key.
121
David Reidc0248b92014-01-30 15:23:33 -0800122
123.. currentmodule:: cryptography.hazmat.primitives.kdf.hkdf
124
125.. class:: HKDF(algorithm, length, salt, info, backend)
126
127 .. versionadded:: 0.2
128
David Reid2ad94ab2014-02-03 10:01:15 -0800129 `HKDF`_ (HMAC-based Extract-and-Expand Key Derivation Function) is suitable
David Reidc0248b92014-01-30 15:23:33 -0800130 for deriving keys of a fixed size used for other cryptographic operations.
Alex Gaynorc43bb752014-02-12 16:42:11 -0800131
132 .. warning::
133
134 HKDF should not be used for password storage.
David Reidc0248b92014-01-30 15:23:33 -0800135
David Reid5df929c2014-02-03 13:26:15 -0800136 .. doctest::
137
138 >>> import os
139 >>> from cryptography.hazmat.primitives import hashes
140 >>> from cryptography.hazmat.primitives.kdf.hkdf import HKDF
141 >>> from cryptography.hazmat.backends import default_backend
142 >>> backend = default_backend()
143 >>> salt = os.urandom(16)
144 >>> info = b"hkdf-example"
145 >>> hkdf = HKDF(
146 ... algorithm=hashes.SHA256(),
147 ... length=32,
148 ... salt=salt,
149 ... info=info,
150 ... backend=backend
151 ... )
David Reid134f1f42014-02-03 13:54:30 -0800152 >>> key = hkdf.derive(b"input key")
David Reid5df929c2014-02-03 13:26:15 -0800153 >>> hkdf = HKDF(
154 ... algorithm=hashes.SHA256(),
155 ... length=32,
156 ... salt=salt,
157 ... info=info,
158 ... backend=backend
159 ... )
160 >>> hkdf.verify(b"input key", key)
161
David Reidc0248b92014-01-30 15:23:33 -0800162 :param algorithm: An instance of a
163 :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
164 provider.
165
166 :param int length: The desired length of the derived key. Maximum is
David Reidb89f34c2014-02-03 10:01:42 -0800167 ``255 * (algorithm.digest_size // 8)``.
David Reidc0248b92014-01-30 15:23:33 -0800168
David Reid2ad94ab2014-02-03 10:01:15 -0800169 :param bytes salt: A salt. Randomizes the KDF's output. Optional, but
170 highly recommended. Ideally as many bits of entropy as the security
171 level of the hash: often that means cryptographically random and as
172 long as the hash output. Worse (shorter, less entropy) salt values can
173 still meaningfully contribute to security. May be reused. Does not have
174 to be secret, but may cause stronger security guarantees if secret; see
175 `RFC 5869`_ and the `HKDF paper`_ for more details. If ``None`` is
176 explicitly passed a default salt of ``algorithm.digest_size // 8`` null
177 bytes will be used.
David Reidc0248b92014-01-30 15:23:33 -0800178
179 :param bytes info: Application specific context information. If ``None``
180 is explicitly passed an empty byte string will be used.
181
Wouter Bolsterleed63cbd02014-03-01 00:45:31 +0100182 :param backend: A
David Reidc0248b92014-01-30 15:23:33 -0800183 :class:`~cryptography.hazmat.backends.interfaces.HMACBackend`
184 provider.
185
186 .. method:: derive(key_material)
187
188 :param bytes key_material: The input key material.
189 :retunr bytes: The derived key.
190
191 Derives a new key from the input key material by performing both the
192 extract and expand operations.
193
194 .. method:: verify(key_material, expected_key)
195
196 :param key_material bytes: The input key material. This is the same as
197 ``key_material`` in :meth:`derive`.
198 :param expected_key bytes: The expected result of deriving a new key,
199 this is the same as the return value of
200 :meth:`derive`.
201 :raises cryptography.exceptions.InvalidKey: This is raised when the
202 derived key does not match
203 the expected key.
204 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
205 :meth:`derive` or
206 :meth:`verify` is
207 called more than
208 once.
209
210 This checks whether deriving a new key from the supplied
211 ``key_material`` generates the same key as the ``expected_key``, and
David Reidb9fa7712014-02-03 10:45:11 -0800212 raises an exception if they do not match.
David Reidc0248b92014-01-30 15:23:33 -0800213
Paul Kehrerb6d764c2014-01-27 22:32:11 -0600214.. _`NIST SP 800-132`: http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf
Paul Kehrerb3f763f2014-01-28 16:42:15 -0600215.. _`Password Storage Cheat Sheet`: https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet
David Reidb80deea2014-02-03 10:33:16 -0800216.. _`PBKDF2`: https://en.wikipedia.org/wiki/PBKDF2
217.. _`scrypt`: https://en.wikipedia.org/wiki/Scrypt
218.. _`key stretching`: https://en.wikipedia.org/wiki/Key_stretching
David Reid2ad94ab2014-02-03 10:01:15 -0800219.. _`HKDF`:
David Reidb80deea2014-02-03 10:33:16 -0800220.. _`RFC 5869`: https://tools.ietf.org/html/rfc5869
221.. _`HKDF paper`: https://eprint.iacr.org/2010/264