blob: 7def2a225442c400bbfa7de15509ee5dc4add10e [file] [log] [blame]
Paul Kehrerb6d764c2014-01-27 22:32:11 -06001.. hazmat::
2
Alex Stapletonc5fffd32014-03-18 15:29:00 +00003Key derivation functions
Paul Kehrerb6d764c2014-01-27 22:32:11 -06004========================
5
Paul Kehrer48402ff2015-02-16 15:31:52 -06006.. module:: cryptography.hazmat.primitives.kdf
Paul Kehrerb6d764c2014-01-27 22:32:11 -06007
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
Paul Kehrer48402ff2015-02-16 15:31:52 -060041 :class:`~cryptography.hazmat.primitives.kdf.KeyDerivationFunction`
Paul Kehrer5d1af212014-01-28 12:19:32 -060042 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
Paul Kehrer601278a2015-02-12 12:51:00 -060072 :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`
Paul Kehrer5d1af212014-01-28 12:19:32 -060073 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
Alex Gaynor7a489db2014-03-22 15:09:34 -070087 :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised if the
Ayrx68702412014-03-15 23:29:36 +080088 provided ``backend`` does not implement
89 :class:`~cryptography.hazmat.backends.interfaces.PBKDF2HMACBackend`
90
Ayrx00eff9c2014-05-17 19:47:09 +080091 :raises TypeError: This exception is raised if ``salt`` is not ``bytes``.
Ayrx6d69eab2014-05-17 16:59:31 +080092
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060093 .. method:: derive(key_material)
94
Alex Gaynora8e125f2014-01-29 19:21:03 -080095 :param bytes key_material: The input key material. For PBKDF2 this
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060096 should be a password.
Paul Kehrer0b181182014-01-29 16:34:47 -060097 :return bytes: the derived key.
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060098 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
99 :meth:`derive` or
100 :meth:`verify` is
101 called more than
102 once.
103
Ayrx6d69eab2014-05-17 16:59:31 +0800104 :raises TypeError: This exception is raised if ``key_material`` is not
Ayrx00eff9c2014-05-17 19:47:09 +0800105 ``bytes``.
Ayrx6d69eab2014-05-17 16:59:31 +0800106
Paul Kehrer3d8c66f2014-01-28 17:36:50 -0600107 This generates and returns a new key from the supplied password.
108
109 .. method:: verify(key_material, expected_key)
110
Alex Gaynora8e125f2014-01-29 19:21:03 -0800111 :param bytes key_material: The input key material. This is the same as
Paul Kehrer3d8c66f2014-01-28 17:36:50 -0600112 ``key_material`` in :meth:`derive`.
Alex Gaynora8e125f2014-01-29 19:21:03 -0800113 :param bytes expected_key: The expected result of deriving a new key,
Paul Kehrer3d8c66f2014-01-28 17:36:50 -0600114 this is the same as the return value of
115 :meth:`derive`.
116 :raises cryptography.exceptions.InvalidKey: This is raised when the
117 derived key does not match
118 the expected key.
119 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
120 :meth:`derive` or
121 :meth:`verify` is
122 called more than
123 once.
124
125 This checks whether deriving a new key from the supplied
126 ``key_material`` generates the same key as the ``expected_key``, and
127 raises an exception if they do not match. This can be used for
Paul Kehrer99d51902014-01-28 20:16:20 -0600128 checking whether the password a user provides matches the stored derived
Paul Kehrer3d8c66f2014-01-28 17:36:50 -0600129 key.
130
David Reidc0248b92014-01-30 15:23:33 -0800131
132.. currentmodule:: cryptography.hazmat.primitives.kdf.hkdf
133
134.. class:: HKDF(algorithm, length, salt, info, backend)
135
136 .. versionadded:: 0.2
137
David Reid2ad94ab2014-02-03 10:01:15 -0800138 `HKDF`_ (HMAC-based Extract-and-Expand Key Derivation Function) is suitable
David Reidc0248b92014-01-30 15:23:33 -0800139 for deriving keys of a fixed size used for other cryptographic operations.
Alex Gaynorc43bb752014-02-12 16:42:11 -0800140
141 .. warning::
142
143 HKDF should not be used for password storage.
David Reidc0248b92014-01-30 15:23:33 -0800144
David Reid5df929c2014-02-03 13:26:15 -0800145 .. doctest::
146
147 >>> import os
148 >>> from cryptography.hazmat.primitives import hashes
149 >>> from cryptography.hazmat.primitives.kdf.hkdf import HKDF
150 >>> from cryptography.hazmat.backends import default_backend
151 >>> backend = default_backend()
152 >>> salt = os.urandom(16)
153 >>> info = b"hkdf-example"
154 >>> hkdf = HKDF(
155 ... algorithm=hashes.SHA256(),
156 ... length=32,
157 ... salt=salt,
158 ... info=info,
159 ... backend=backend
160 ... )
David Reid134f1f42014-02-03 13:54:30 -0800161 >>> key = hkdf.derive(b"input key")
David Reid5df929c2014-02-03 13:26:15 -0800162 >>> hkdf = HKDF(
163 ... algorithm=hashes.SHA256(),
164 ... length=32,
165 ... salt=salt,
166 ... info=info,
167 ... backend=backend
168 ... )
169 >>> hkdf.verify(b"input key", key)
170
David Reidc0248b92014-01-30 15:23:33 -0800171 :param algorithm: An instance of a
Paul Kehrer601278a2015-02-12 12:51:00 -0600172 :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`
David Reidc0248b92014-01-30 15:23:33 -0800173 provider.
174
175 :param int length: The desired length of the derived key. Maximum is
David Reidb89f34c2014-02-03 10:01:42 -0800176 ``255 * (algorithm.digest_size // 8)``.
David Reidc0248b92014-01-30 15:23:33 -0800177
David Reid2ad94ab2014-02-03 10:01:15 -0800178 :param bytes salt: A salt. Randomizes the KDF's output. Optional, but
179 highly recommended. Ideally as many bits of entropy as the security
180 level of the hash: often that means cryptographically random and as
181 long as the hash output. Worse (shorter, less entropy) salt values can
182 still meaningfully contribute to security. May be reused. Does not have
183 to be secret, but may cause stronger security guarantees if secret; see
184 `RFC 5869`_ and the `HKDF paper`_ for more details. If ``None`` is
185 explicitly passed a default salt of ``algorithm.digest_size // 8`` null
186 bytes will be used.
David Reidc0248b92014-01-30 15:23:33 -0800187
188 :param bytes info: Application specific context information. If ``None``
189 is explicitly passed an empty byte string will be used.
190
Wouter Bolsterleed63cbd02014-03-01 00:45:31 +0100191 :param backend: A
David Reidc0248b92014-01-30 15:23:33 -0800192 :class:`~cryptography.hazmat.backends.interfaces.HMACBackend`
193 provider.
194
Alex Gaynor7a489db2014-03-22 15:09:34 -0700195 :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised if the
Ayrx68702412014-03-15 23:29:36 +0800196 provided ``backend`` does not implement
197 :class:`~cryptography.hazmat.backends.interfaces.HMACBackend`
198
Ayrx6d69eab2014-05-17 16:59:31 +0800199 :raises TypeError: This exception is raised if ``salt`` or ``info`` is not
Ayrx00eff9c2014-05-17 19:47:09 +0800200 ``bytes``.
Ayrx6d69eab2014-05-17 16:59:31 +0800201
David Reidc0248b92014-01-30 15:23:33 -0800202 .. method:: derive(key_material)
203
204 :param bytes key_material: The input key material.
Ayrx1534e092014-05-06 14:19:46 +0800205 :return bytes: The derived key.
Ayrx6d69eab2014-05-17 16:59:31 +0800206 :raises TypeError: This exception is raised if ``key_material`` is not
Ayrx00eff9c2014-05-17 19:47:09 +0800207 ``bytes``.
David Reidc0248b92014-01-30 15:23:33 -0800208
209 Derives a new key from the input key material by performing both the
210 extract and expand operations.
211
212 .. method:: verify(key_material, expected_key)
213
Alex Gaynor1cfc5d52014-11-23 17:44:28 -0600214 :param bytes key_material: The input key material. This is the same as
David Reidc0248b92014-01-30 15:23:33 -0800215 ``key_material`` in :meth:`derive`.
Alex Gaynor1cfc5d52014-11-23 17:44:28 -0600216 :param bytes expected_key: The expected result of deriving a new key,
David Reidc0248b92014-01-30 15:23:33 -0800217 this is the same as the return value of
218 :meth:`derive`.
219 :raises cryptography.exceptions.InvalidKey: This is raised when the
220 derived key does not match
221 the expected key.
222 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
223 :meth:`derive` or
224 :meth:`verify` is
225 called more than
226 once.
227
228 This checks whether deriving a new key from the supplied
229 ``key_material`` generates the same key as the ``expected_key``, and
David Reidb9fa7712014-02-03 10:45:11 -0800230 raises an exception if they do not match.
David Reidc0248b92014-01-30 15:23:33 -0800231
Ayrx9d72f122014-05-06 20:27:51 +0800232
Ayrxc0ce9112014-05-07 16:22:09 +0800233.. class:: HKDFExpand(algorithm, length, info, backend)
Ayrx9d72f122014-05-06 20:27:51 +0800234
235 .. versionadded:: 0.5
236
237 HKDF consists of two stages, extract and expand. This class exposes an
238 expand only version of HKDF that is suitable when the key material is
239 already cryptographically strong.
240
241 .. warning::
242
Ayrxc0ce9112014-05-07 16:22:09 +0800243 HKDFExpand should only be used if the key material is
Ayrx9d72f122014-05-06 20:27:51 +0800244 cryptographically strong. You should use
245 :class:`~cryptography.hazmat.primitives.kdf.hkdf.HKDF` if
246 you are unsure.
247
248 .. doctest::
249
250 >>> import os
251 >>> from cryptography.hazmat.primitives import hashes
Ayrxc0ce9112014-05-07 16:22:09 +0800252 >>> from cryptography.hazmat.primitives.kdf.hkdf import HKDFExpand
Ayrx9d72f122014-05-06 20:27:51 +0800253 >>> from cryptography.hazmat.backends import default_backend
254 >>> backend = default_backend()
255 >>> info = b"hkdf-example"
256 >>> key_material = os.urandom(16)
Ayrxc0ce9112014-05-07 16:22:09 +0800257 >>> hkdf = HKDFExpand(
Ayrx9d72f122014-05-06 20:27:51 +0800258 ... algorithm=hashes.SHA256(),
259 ... length=32,
260 ... info=info,
261 ... backend=backend
262 ... )
263 >>> key = hkdf.derive(key_material)
Ayrxc0ce9112014-05-07 16:22:09 +0800264 >>> hkdf = HKDFExpand(
Ayrx9d72f122014-05-06 20:27:51 +0800265 ... algorithm=hashes.SHA256(),
266 ... length=32,
267 ... info=info,
268 ... backend=backend
269 ... )
270 >>> hkdf.verify(key_material, key)
271
272 :param algorithm: An instance of a
Paul Kehrer601278a2015-02-12 12:51:00 -0600273 :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`
Ayrx9d72f122014-05-06 20:27:51 +0800274 provider.
275
276 :param int length: The desired length of the derived key. Maximum is
277 ``255 * (algorithm.digest_size // 8)``.
278
279 :param bytes info: Application specific context information. If ``None``
280 is explicitly passed an empty byte string will be used.
281
282 :param backend: A
283 :class:`~cryptography.hazmat.backends.interfaces.HMACBackend`
284 provider.
285
286 :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised if the
287 provided ``backend`` does not implement
288 :class:`~cryptography.hazmat.backends.interfaces.HMACBackend`
Ayrxc48100a2014-05-10 13:01:46 +0800289 :raises TypeError: This is raised if the provided ``info`` is a unicode object
Ayrx00eff9c2014-05-17 19:47:09 +0800290 :raises TypeError: This exception is raised if ``info`` is not ``bytes``.
Ayrx9d72f122014-05-06 20:27:51 +0800291
292 .. method:: derive(key_material)
293
294 :param bytes key_material: The input key material.
295 :return bytes: The derived key.
296
Ayrxc48100a2014-05-10 13:01:46 +0800297 :raises TypeError: This is raised if the provided ``key_material`` is
298 a unicode object
Ayrx6d69eab2014-05-17 16:59:31 +0800299 :raises TypeError: This exception is raised if ``key_material`` is not
Ayrx00eff9c2014-05-17 19:47:09 +0800300 ``bytes``.
Ayrxc48100a2014-05-10 13:01:46 +0800301
Ayrx9d72f122014-05-06 20:27:51 +0800302 Derives a new key from the input key material by performing both the
303 extract and expand operations.
304
305 .. method:: verify(key_material, expected_key)
306
Alex Gaynor1cfc5d52014-11-23 17:44:28 -0600307 :param bytes key_material: The input key material. This is the same as
Ayrx9d72f122014-05-06 20:27:51 +0800308 ``key_material`` in :meth:`derive`.
Alex Gaynor1cfc5d52014-11-23 17:44:28 -0600309 :param bytes expected_key: The expected result of deriving a new key,
Ayrx9d72f122014-05-06 20:27:51 +0800310 this is the same as the return value of
311 :meth:`derive`.
312 :raises cryptography.exceptions.InvalidKey: This is raised when the
313 derived key does not match
314 the expected key.
315 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
316 :meth:`derive` or
317 :meth:`verify` is
318 called more than
319 once.
Ayrxc48100a2014-05-10 13:01:46 +0800320 :raises TypeError: This is raised if the provided ``key_material`` is
321 a unicode object
Ayrx9d72f122014-05-06 20:27:51 +0800322
323 This checks whether deriving a new key from the supplied
324 ``key_material`` generates the same key as the ``expected_key``, and
325 raises an exception if they do not match.
326
Simo Sorce8a690fb2015-05-01 15:56:30 -0400327.. currentmodule:: cryptography.hazmat.primitives.kdf.concatkdf
328
329.. class:: ConcatKDFHash(algorithm, length, otherinfo, backend)
330
331 .. versionadded:: 1.0
332
333 ConcatKDFHash (Concatenation Key Derivation Function) is defined by the
334 NIST Special Publication `NIST SP 800-56Ar2`_ document, to be used to
335 derive keys for use after a Key Exchange negotiation operation.
336
337 .. warning::
338
339 ConcatKDFHash should not be used for password storage.
340
341 .. doctest::
342
343 >>> import os
344 >>> from cryptography.hazmat.primitives import hashes
345 >>> from cryptography.hazmat.primitives.kdf.concatkdf import ConcatKDFHash
346 >>> from cryptography.hazmat.backends import default_backend
347 >>> backend = default_backend()
348 >>> salt = os.urandom(16)
349 >>> otherinfo = b"concatkdf-example"
350 >>> ckdf = ConcatKDFHash(
351 ... algorithm=hashes.SHA256(),
352 ... length=256,
353 ... otherinfo=otherinfo,
354 ... backend=backend
355 ... )
356 >>> key = ckdf.derive(b"input key")
357 >>> ckdf = ConcatKDFHash(
358 ... algorithm=hashes.SHA256(),
359 ... length=256,
360 ... otherinfo=otherinfo,
361 ... backend=backend
362 ... )
363 >>> ckdf.verify(b"input key", key)
364
365 :param algorithm: An instance of a
366 :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`
367 provider
368
369 :param int length: The desired length of the derived key in bytes.
370 Maximum is ``hashlen * (2^32 -1)``.
371
372 :param bytes otherinfo: Application specific context information.
373 If ``None`` is explicitly passed an empty byte string will be used.
374
375 :param backend: A
376 :class:`~cryptography.hazmat.backends.interfaces.HashBackend`
377 provider.
378
379 :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised
380 if the provided ``backend`` does not implement
381 :class:`~cryptography.hazmat.backends.interfaces.HashBackend`
382
383 :raises TypeError: This exception is raised if ``otherinfo`` is not
384 ``bytes``.
385
386 .. method:: derive(key_material)
387
388 :param bytes key_material: The input key material.
389 :return bytes: The derived key.
390 :raises TypeError: This exception is raised if ``key_material`` is
391 not ``bytes``.
392
393 Derives a new key from the input key material by performing both the
394 extract and expand operations.
395
396 .. method:: verify(key_material, expected_key)
397
398 :param bytes key_material: The input key material. This is the same as
399 ``key_material`` in :meth:`derive`.
400 :param bytes expected_key: The expected result of deriving a new key,
401 this is the same as the return value of
402 :meth:`derive`.
403 :raises cryptography.exceptions.InvalidKey: This is raised when the
404 derived key does not match
405 the expected key.
406 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
407 :meth:`derive` or
408 :meth:`verify` is
409 called more than
410 once.
411
412 This checks whether deriving a new key from the supplied
413 ``key_material`` generates the same key as the ``expected_key``, and
414 raises an exception if they do not match.
415
416
417.. class:: ConcatKDFHMAC(algorithm, length, salt, otherinfo, backend)
418
419 .. versionadded:: 1.0
420
421 Similar to ConcatKFDHash but uses an HMAC function instead.
422
423 .. warning::
424
425 ConcatKDFHMAC should not be used for password storage.
426
427 .. doctest::
428
429 >>> import os
430 >>> from cryptography.hazmat.primitives import hashes
431 >>> from cryptography.hazmat.primitives.kdf.concatkdf import ConcatKDFHMAC
432 >>> from cryptography.hazmat.backends import default_backend
433 >>> backend = default_backend()
434 >>> salt = os.urandom(16)
435 >>> otherinfo = b"concatkdf-example"
436 >>> ckdf = ConcatKDFHMAC(
437 ... algorithm=hashes.SHA256(),
438 ... length=256,
439 ... salt=salt,
440 ... otherinfo=otherinfo,
441 ... backend=backend
442 ... )
443 >>> key = ckdf.derive(b"input key")
444 >>> ckdf = ConcatKDFHMAC(
445 ... algorithm=hashes.SHA256(),
446 ... length=256,
447 ... salt=salt,
448 ... otherinfo=otherinfo,
449 ... backend=backend
450 ... )
451 >>> ckdf.verify(b"input key", key)
452
453 :param algorithm: An instance of a
454 :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`
455 provider
456
457 :param int length: The desired length of the derived key in bytes. Maximum
458 is ``hashlen * (2^32 -1)``.
459
460 :param bytes salt: A salt. Randomizes the KDF's output. Optional, but
461 highly recommended. Ideally as many bits of entropy as the security
462 level of the hash: often that means cryptographically random and as
463 long as the hash output. Does not have to be secret, but may cause
464 stronger security guarantees if secret; If ``None`` is explicitly
465 passed a default salt of ``algorithm.block_size`` null bytes will be
466 used.
467
468 :param bytes otherinfo: Application specific context information.
469 If ``None`` is explicitly passed an empty byte string will be used.
470
471 :param backend: A
472 :class:`~cryptography.hazmat.backends.interfaces.HMACBackend`
473 provider.
474
475 :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised if the
476 provided ``backend`` does not implement
477 :class:`~cryptography.hazmat.backends.interfaces.HMACBackend`
478
479 :raises TypeError: This exception is raised if ``salt`` or ``otherinfo``
480 is not ``bytes``.
481
482 .. method:: derive(key_material)
483
484 :param bytes key_material: The input key material.
485 :return bytes: The derived key.
486 :raises TypeError: This exception is raised if ``key_material`` is not
487 ``bytes``.
488
489 Derives a new key from the input key material by performing both the
490 extract and expand operations.
491
492 .. method:: verify(key_material, expected_key)
493
494 :param bytes key_material: The input key material. This is the same as
495 ``key_material`` in :meth:`derive`.
496 :param bytes expected_key: The expected result of deriving a new key,
497 this is the same as the return value of
498 :meth:`derive`.
499 :raises cryptography.exceptions.InvalidKey: This is raised when the
500 derived key does not match
501 the expected key.
502 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
503 :meth:`derive` or
504 :meth:`verify` is
505 called more than
506 once.
507
508 This checks whether deriving a new key from the supplied
509 ``key_material`` generates the same key as the ``expected_key``, and
510 raises an exception if they do not match.
511
512
Paul Kehrer48402ff2015-02-16 15:31:52 -0600513Interface
514~~~~~~~~~
515
516.. currentmodule:: cryptography.hazmat.primitives.kdf
517
518.. class:: KeyDerivationFunction
519
520 .. versionadded:: 0.2
521
522 .. method:: derive(key_material)
523
524 :param bytes key_material: The input key material. Depending on what
525 key derivation function you are using this
526 could be either random bytes, or a user
527 supplied password.
528 :return: The new key.
529 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
530 :meth:`derive` or
531 :meth:`verify` is
532 called more than
533 once.
534
535 This generates and returns a new key from the supplied key material.
536
537 .. method:: verify(key_material, expected_key)
538
539 :param bytes key_material: The input key material. This is the same as
540 ``key_material`` in :meth:`derive`.
541 :param bytes expected_key: The expected result of deriving a new key,
542 this is the same as the return value of
543 :meth:`derive`.
544 :raises cryptography.exceptions.InvalidKey: This is raised when the
545 derived key does not match
546 the expected key.
547 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
548 :meth:`derive` or
549 :meth:`verify` is
550 called more than
551 once.
552
553 This checks whether deriving a new key from the supplied
554 ``key_material`` generates the same key as the ``expected_key``, and
555 raises an exception if they do not match. This can be used for
556 something like checking whether a user's password attempt matches the
557 stored derived key.
558
559
Paul Kehrerb6d764c2014-01-27 22:32:11 -0600560.. _`NIST SP 800-132`: http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf
Simo Sorce8a690fb2015-05-01 15:56:30 -0400561.. _`NIST SP 800-56Ar2`: http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar2.pdf
Paul Kehrerb3f763f2014-01-28 16:42:15 -0600562.. _`Password Storage Cheat Sheet`: https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet
David Reidb80deea2014-02-03 10:33:16 -0800563.. _`PBKDF2`: https://en.wikipedia.org/wiki/PBKDF2
564.. _`scrypt`: https://en.wikipedia.org/wiki/Scrypt
565.. _`key stretching`: https://en.wikipedia.org/wiki/Key_stretching
David Reid2ad94ab2014-02-03 10:01:15 -0800566.. _`HKDF`:
David Reidb80deea2014-02-03 10:33:16 -0800567.. _`RFC 5869`: https://tools.ietf.org/html/rfc5869
568.. _`HKDF paper`: https://eprint.iacr.org/2010/264