blob: 551dbd6d25c55baeecd6ad12a6535c5f03b6cf72 [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
Paul Kehrer0b181182014-01-29 16:34:47 -060016 such as :class:`~cryptography.hazmat.primitives.kdf.pbkdf2.PBKDF2HMAC` or HKDF.
Paul Kehrer1cab1042014-01-29 14:30:11 -060017 This process is typically known as `key stretching`_.
18
Paul Kehrer0b181182014-01-29 16:34:47 -060019* Password storage
Paul Kehrer1cab1042014-01-29 14:30:11 -060020
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 Kehrer0b181182014-01-29 16:34:47 -060028.. currentmodule:: cryptography.hazmat.primitives.kdf.pbkdf2
29
30.. class:: PBKDF2HMAC(algorithm, length, salt, iterations, backend)
Paul Kehrerb6d764c2014-01-27 22:32:11 -060031
Paul Kehrer5d1af212014-01-28 12:19:32 -060032 .. versionadded:: 0.2
33
Paul Kehrer298e5332014-01-29 11:16:22 -060034 `PBKDF2`_ (Password Based Key Derivation Function 2) is typically used for
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060035 deriving a cryptographic key from a password. It may also be used for
Paul Kehrer0b181182014-01-29 16:34:47 -060036 key storage, but an alternate key storage KDF such as `scrypt`_ is generally
Paul Kehrer1cab1042014-01-29 14:30:11 -060037 considered a better solution.
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060038
Paul Kehrer5d1af212014-01-28 12:19:32 -060039 This class conforms to the
40 :class:`~cryptography.hazmat.primitives.interfaces.KeyDerivationFunction`
41 interface.
42
Paul Kehrerb6d764c2014-01-27 22:32:11 -060043 .. doctest::
44
Paul Kehrer5d1af212014-01-28 12:19:32 -060045 >>> import os
46 >>> from cryptography.hazmat.primitives import hashes
Paul Kehrerb3f763f2014-01-28 16:42:15 -060047 >>> from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
Paul Kehrerb6d764c2014-01-27 22:32:11 -060048 >>> from cryptography.hazmat.backends import default_backend
49 >>> backend = default_backend()
50 >>> salt = os.urandom(16)
51 >>> # derive
Paul Kehrerb3f763f2014-01-28 16:42:15 -060052 >>> kdf = PBKDF2HMAC(
53 ... algorithm=hashes.SHA256(),
54 ... length=32,
55 ... salt=salt,
Paul Kehrer1277bc72014-01-28 17:09:59 -060056 ... iterations=100000,
Paul Kehrerb3f763f2014-01-28 16:42:15 -060057 ... backend=backend
58 ... )
Paul Kehrerb6d764c2014-01-27 22:32:11 -060059 >>> key = kdf.derive(b"my great password")
60 >>> # verify
Paul Kehrerb3f763f2014-01-28 16:42:15 -060061 >>> kdf = PBKDF2HMAC(
62 ... algorithm=hashes.SHA256(),
63 ... length=32,
64 ... salt=salt,
Paul Kehrer1277bc72014-01-28 17:09:59 -060065 ... iterations=100000,
Paul Kehrerb3f763f2014-01-28 16:42:15 -060066 ... backend=backend
67 ... )
Paul Kehrerb6d764c2014-01-27 22:32:11 -060068 >>> kdf.verify(b"my great password", key)
Paul Kehrerb6d764c2014-01-27 22:32:11 -060069
Paul Kehrer5d1af212014-01-28 12:19:32 -060070 :param algorithm: An instance of a
71 :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
72 provider.
73 :param int length: The desired length of the derived key. Maximum is
Paul Kehrerb3f763f2014-01-28 16:42:15 -060074 (2\ :sup:`32` - 1) * ``algorithm.digest_size``.
Paul Kehrer5d1af212014-01-28 12:19:32 -060075 :param bytes salt: A salt. `NIST SP 800-132`_ recommends 128-bits or
76 longer.
77 :param int iterations: The number of iterations to perform of the hash
Paul Kehrerc58b4782014-01-29 13:56:25 -060078 function. This can be used to control the length of time the operation
79 takes. Higher numbers help mitigate brute force attacks against derived
80 keys. See OWASP's `Password Storage Cheat Sheet`_ for more
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060081 detailed recommendations if you intend to use this for password storage.
Paul Kehrer5d1af212014-01-28 12:19:32 -060082 :param backend: A
Paul Kehrer15a86a02014-01-29 17:44:47 -060083 :class:`~cryptography.hazmat.backends.interfaces.PBKDF2HMACBackend`
Paul Kehrer5d1af212014-01-28 12:19:32 -060084 provider.
Paul Kehrerb6d764c2014-01-27 22:32:11 -060085
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060086 .. method:: derive(key_material)
87
88 :param key_material bytes: The input key material. For PBKDF2 this
89 should be a password.
Paul Kehrer0b181182014-01-29 16:34:47 -060090 :return bytes: the derived key.
Paul Kehrer3d8c66f2014-01-28 17:36:50 -060091 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
92 :meth:`derive` or
93 :meth:`verify` is
94 called more than
95 once.
96
97 This generates and returns a new key from the supplied password.
98
99 .. method:: verify(key_material, expected_key)
100
101 :param key_material bytes: The input key material. This is the same as
102 ``key_material`` in :meth:`derive`.
103 :param expected_key bytes: The expected result of deriving a new key,
104 this is the same as the return value of
105 :meth:`derive`.
106 :raises cryptography.exceptions.InvalidKey: This is raised when the
107 derived key does not match
108 the expected key.
109 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
110 :meth:`derive` or
111 :meth:`verify` is
112 called more than
113 once.
114
115 This checks whether deriving a new key from the supplied
116 ``key_material`` generates the same key as the ``expected_key``, and
117 raises an exception if they do not match. This can be used for
Paul Kehrer99d51902014-01-28 20:16:20 -0600118 checking whether the password a user provides matches the stored derived
Paul Kehrer3d8c66f2014-01-28 17:36:50 -0600119 key.
120
Paul Kehrerb6d764c2014-01-27 22:32:11 -0600121.. _`NIST SP 800-132`: http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf
Paul Kehrerb3f763f2014-01-28 16:42:15 -0600122.. _`Password Storage Cheat Sheet`: https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet
Paul Kehrer298e5332014-01-29 11:16:22 -0600123.. _`PBKDF2`: http://en.wikipedia.org/wiki/PBKDF2
Paul Kehrer3d8c66f2014-01-28 17:36:50 -0600124.. _`scrypt`: http://en.wikipedia.org/wiki/Scrypt
Paul Kehrer1cab1042014-01-29 14:30:11 -0600125.. _`key stretching`: http://en.wikipedia.org/wiki/Key_stretching