blob: bf069faa7db7eaf2a9cdc9fc745b98e52077a50d [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
9sources using a pseudo-random function (PRF). Each KDF is suitable for
10different tasks (cryptographic key derivation, password storage,
11key stretching) so match your needs to their capabilities.
Paul Kehrerb6d764c2014-01-27 22:32:11 -060012
Paul Kehrerb3f763f2014-01-28 16:42:15 -060013.. class:: PBKDF2HMAC(algorithm, length, salt, iterations, backend):
Paul Kehrerb6d764c2014-01-27 22:32:11 -060014
Paul Kehrer5d1af212014-01-28 12:19:32 -060015 .. versionadded:: 0.2
16
Paul Kehrer298e5332014-01-29 11:16:22 -060017 `PBKDF2`_ (Password Based Key Derivation Function 2) is typically used for
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060018 deriving a cryptographic key from a password. It may also be used for
Paul Kehrer298e5332014-01-29 11:16:22 -060019 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 Kehrer3d8c66f2014-01-28 17:36:50 -060021
Paul Kehrer5d1af212014-01-28 12:19:32 -060022 This class conforms to the
23 :class:`~cryptography.hazmat.primitives.interfaces.KeyDerivationFunction`
24 interface.
25
Paul Kehrerb6d764c2014-01-27 22:32:11 -060026 .. doctest::
27
Paul Kehrer5d1af212014-01-28 12:19:32 -060028 >>> import os
29 >>> from cryptography.hazmat.primitives import hashes
Paul Kehrerb3f763f2014-01-28 16:42:15 -060030 >>> from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
Paul Kehrerb6d764c2014-01-27 22:32:11 -060031 >>> from cryptography.hazmat.backends import default_backend
32 >>> backend = default_backend()
33 >>> salt = os.urandom(16)
34 >>> # derive
Paul Kehrerb3f763f2014-01-28 16:42:15 -060035 >>> kdf = PBKDF2HMAC(
36 ... algorithm=hashes.SHA256(),
37 ... length=32,
38 ... salt=salt,
Paul Kehrer1277bc72014-01-28 17:09:59 -060039 ... iterations=100000,
Paul Kehrerb3f763f2014-01-28 16:42:15 -060040 ... backend=backend
41 ... )
Paul Kehrerb6d764c2014-01-27 22:32:11 -060042 >>> key = kdf.derive(b"my great password")
43 >>> # verify
Paul Kehrerb3f763f2014-01-28 16:42:15 -060044 >>> kdf = PBKDF2HMAC(
45 ... algorithm=hashes.SHA256(),
46 ... length=32,
47 ... salt=salt,
Paul Kehrer1277bc72014-01-28 17:09:59 -060048 ... iterations=100000,
Paul Kehrerb3f763f2014-01-28 16:42:15 -060049 ... backend=backend
50 ... )
Paul Kehrerb6d764c2014-01-27 22:32:11 -060051 >>> kdf.verify(b"my great password", key)
Paul Kehrerb6d764c2014-01-27 22:32:11 -060052
Paul Kehrer5d1af212014-01-28 12:19:32 -060053 :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 Kehrerb3f763f2014-01-28 16:42:15 -060057 (2\ :sup:`32` - 1) * ``algorithm.digest_size``.
Paul Kehrer5d1af212014-01-28 12:19:32 -060058 :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 Kehrerc58b4782014-01-29 13:56:25 -060061 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 Kehrer3d8c66f2014-01-28 17:36:50 -060064 detailed recommendations if you intend to use this for password storage.
Paul Kehrer5d1af212014-01-28 12:19:32 -060065 :param backend: A
66 :class:`~cryptography.hazmat.backends.interfaces.CipherBackend`
67 provider.
Paul Kehrerb6d764c2014-01-27 22:32:11 -060068
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060069 .. 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 Kehrer99d51902014-01-28 20:16:20 -0600101 checking whether the password a user provides matches the stored derived
Paul Kehrer3d8c66f2014-01-28 17:36:50 -0600102 key.
103
Paul Kehrerb6d764c2014-01-27 22:32:11 -0600104.. _`NIST SP 800-132`: http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf
Paul Kehrerb3f763f2014-01-28 16:42:15 -0600105.. _`Password Storage Cheat Sheet`: https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet
Paul Kehrer298e5332014-01-29 11:16:22 -0600106.. _`PBKDF2`: http://en.wikipedia.org/wiki/PBKDF2
Paul Kehrer3d8c66f2014-01-28 17:36:50 -0600107.. _`scrypt`: http://en.wikipedia.org/wiki/Scrypt