Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 1 | .. hazmat:: |
| 2 | |
Alex Stapleton | c5fffd3 | 2014-03-18 15:29:00 +0000 | [diff] [blame] | 3 | Key derivation functions |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 4 | ======================== |
| 5 | |
| 6 | .. currentmodule:: cryptography.hazmat.primitives.kdf |
| 7 | |
Paul Kehrer | 0b18118 | 2014-01-29 16:34:47 -0600 | [diff] [blame] | 8 | Key derivation functions derive bytes suitable for cryptographic operations |
| 9 | from passwords or other data sources using a pseudo-random function (PRF). |
| 10 | Different KDFs are suitable for different tasks such as: |
Paul Kehrer | 1cab104 | 2014-01-29 14:30:11 -0600 | [diff] [blame] | 11 | |
Paul Kehrer | 0b18118 | 2014-01-29 16:34:47 -0600 | [diff] [blame] | 12 | * Cryptographic key derivation |
Paul Kehrer | 1cab104 | 2014-01-29 14:30:11 -0600 | [diff] [blame] | 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 |
David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 16 | such as :class:`~cryptography.hazmat.primitives.kdf.pbkdf2.PBKDF2HMAC` or |
| 17 | :class:`~cryptography.hazmat.primitives.kdf.hkdf.HKDF`. |
Paul Kehrer | 1cab104 | 2014-01-29 14:30:11 -0600 | [diff] [blame] | 18 | This process is typically known as `key stretching`_. |
| 19 | |
Paul Kehrer | 0b18118 | 2014-01-29 16:34:47 -0600 | [diff] [blame] | 20 | * Password storage |
Paul Kehrer | 1cab104 | 2014-01-29 14:30:11 -0600 | [diff] [blame] | 21 | |
| 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 Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 28 | |
Paul Kehrer | 0b18118 | 2014-01-29 16:34:47 -0600 | [diff] [blame] | 29 | .. currentmodule:: cryptography.hazmat.primitives.kdf.pbkdf2 |
| 30 | |
| 31 | .. class:: PBKDF2HMAC(algorithm, length, salt, iterations, backend) |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 32 | |
Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 33 | .. versionadded:: 0.2 |
| 34 | |
Paul Kehrer | 298e533 | 2014-01-29 11:16:22 -0600 | [diff] [blame] | 35 | `PBKDF2`_ (Password Based Key Derivation Function 2) is typically used for |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 36 | deriving a cryptographic key from a password. It may also be used for |
Paul Kehrer | 0b18118 | 2014-01-29 16:34:47 -0600 | [diff] [blame] | 37 | key storage, but an alternate key storage KDF such as `scrypt`_ is generally |
Paul Kehrer | 1cab104 | 2014-01-29 14:30:11 -0600 | [diff] [blame] | 38 | considered a better solution. |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 39 | |
Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 40 | This class conforms to the |
| 41 | :class:`~cryptography.hazmat.primitives.interfaces.KeyDerivationFunction` |
| 42 | interface. |
| 43 | |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 44 | .. doctest:: |
| 45 | |
Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 46 | >>> import os |
| 47 | >>> from cryptography.hazmat.primitives import hashes |
Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 48 | >>> from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 49 | >>> from cryptography.hazmat.backends import default_backend |
| 50 | >>> backend = default_backend() |
| 51 | >>> salt = os.urandom(16) |
| 52 | >>> # derive |
Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 53 | >>> kdf = PBKDF2HMAC( |
| 54 | ... algorithm=hashes.SHA256(), |
| 55 | ... length=32, |
| 56 | ... salt=salt, |
Paul Kehrer | 1277bc7 | 2014-01-28 17:09:59 -0600 | [diff] [blame] | 57 | ... iterations=100000, |
Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 58 | ... backend=backend |
| 59 | ... ) |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 60 | >>> key = kdf.derive(b"my great password") |
| 61 | >>> # verify |
Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 62 | >>> kdf = PBKDF2HMAC( |
| 63 | ... algorithm=hashes.SHA256(), |
| 64 | ... length=32, |
| 65 | ... salt=salt, |
Paul Kehrer | 1277bc7 | 2014-01-28 17:09:59 -0600 | [diff] [blame] | 66 | ... iterations=100000, |
Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 67 | ... backend=backend |
| 68 | ... ) |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 69 | >>> kdf.verify(b"my great password", key) |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 70 | |
Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 71 | :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 Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 75 | (2\ :sup:`32` - 1) * ``algorithm.digest_size``. |
Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 76 | :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 Kehrer | c58b478 | 2014-01-29 13:56:25 -0600 | [diff] [blame] | 79 | 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 Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 82 | detailed recommendations if you intend to use this for password storage. |
Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 83 | :param backend: A |
Paul Kehrer | 15a86a0 | 2014-01-29 17:44:47 -0600 | [diff] [blame] | 84 | :class:`~cryptography.hazmat.backends.interfaces.PBKDF2HMACBackend` |
Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 85 | provider. |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 86 | |
Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 87 | :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised if the |
Ayrx | 6870241 | 2014-03-15 23:29:36 +0800 | [diff] [blame] | 88 | provided ``backend`` does not implement |
| 89 | :class:`~cryptography.hazmat.backends.interfaces.PBKDF2HMACBackend` |
| 90 | |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 91 | .. method:: derive(key_material) |
| 92 | |
Alex Gaynor | a8e125f | 2014-01-29 19:21:03 -0800 | [diff] [blame] | 93 | :param bytes key_material: The input key material. For PBKDF2 this |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 94 | should be a password. |
Paul Kehrer | 0b18118 | 2014-01-29 16:34:47 -0600 | [diff] [blame] | 95 | :return bytes: the derived key. |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 96 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 97 | :meth:`derive` or |
| 98 | :meth:`verify` is |
| 99 | called more than |
| 100 | once. |
| 101 | |
| 102 | This generates and returns a new key from the supplied password. |
| 103 | |
| 104 | .. method:: verify(key_material, expected_key) |
| 105 | |
Alex Gaynor | a8e125f | 2014-01-29 19:21:03 -0800 | [diff] [blame] | 106 | :param bytes key_material: The input key material. This is the same as |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 107 | ``key_material`` in :meth:`derive`. |
Alex Gaynor | a8e125f | 2014-01-29 19:21:03 -0800 | [diff] [blame] | 108 | :param bytes expected_key: The expected result of deriving a new key, |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 109 | this is the same as the return value of |
| 110 | :meth:`derive`. |
| 111 | :raises cryptography.exceptions.InvalidKey: This is raised when the |
| 112 | derived key does not match |
| 113 | the expected key. |
| 114 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 115 | :meth:`derive` or |
| 116 | :meth:`verify` is |
| 117 | called more than |
| 118 | once. |
| 119 | |
| 120 | This checks whether deriving a new key from the supplied |
| 121 | ``key_material`` generates the same key as the ``expected_key``, and |
| 122 | raises an exception if they do not match. This can be used for |
Paul Kehrer | 99d5190 | 2014-01-28 20:16:20 -0600 | [diff] [blame] | 123 | checking whether the password a user provides matches the stored derived |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 124 | key. |
| 125 | |
David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 126 | |
| 127 | .. currentmodule:: cryptography.hazmat.primitives.kdf.hkdf |
| 128 | |
| 129 | .. class:: HKDF(algorithm, length, salt, info, backend) |
| 130 | |
| 131 | .. versionadded:: 0.2 |
| 132 | |
David Reid | 2ad94ab | 2014-02-03 10:01:15 -0800 | [diff] [blame] | 133 | `HKDF`_ (HMAC-based Extract-and-Expand Key Derivation Function) is suitable |
David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 134 | for deriving keys of a fixed size used for other cryptographic operations. |
Alex Gaynor | c43bb75 | 2014-02-12 16:42:11 -0800 | [diff] [blame] | 135 | |
| 136 | .. warning:: |
| 137 | |
| 138 | HKDF should not be used for password storage. |
David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 139 | |
David Reid | 5df929c | 2014-02-03 13:26:15 -0800 | [diff] [blame] | 140 | .. doctest:: |
| 141 | |
| 142 | >>> import os |
| 143 | >>> from cryptography.hazmat.primitives import hashes |
| 144 | >>> from cryptography.hazmat.primitives.kdf.hkdf import HKDF |
| 145 | >>> from cryptography.hazmat.backends import default_backend |
| 146 | >>> backend = default_backend() |
| 147 | >>> salt = os.urandom(16) |
| 148 | >>> info = b"hkdf-example" |
| 149 | >>> hkdf = HKDF( |
| 150 | ... algorithm=hashes.SHA256(), |
| 151 | ... length=32, |
| 152 | ... salt=salt, |
| 153 | ... info=info, |
| 154 | ... backend=backend |
| 155 | ... ) |
David Reid | 134f1f4 | 2014-02-03 13:54:30 -0800 | [diff] [blame] | 156 | >>> key = hkdf.derive(b"input key") |
David Reid | 5df929c | 2014-02-03 13:26:15 -0800 | [diff] [blame] | 157 | >>> hkdf = HKDF( |
| 158 | ... algorithm=hashes.SHA256(), |
| 159 | ... length=32, |
| 160 | ... salt=salt, |
| 161 | ... info=info, |
| 162 | ... backend=backend |
| 163 | ... ) |
| 164 | >>> hkdf.verify(b"input key", key) |
| 165 | |
David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 166 | :param algorithm: An instance of a |
| 167 | :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` |
| 168 | provider. |
| 169 | |
| 170 | :param int length: The desired length of the derived key. Maximum is |
David Reid | b89f34c | 2014-02-03 10:01:42 -0800 | [diff] [blame] | 171 | ``255 * (algorithm.digest_size // 8)``. |
David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 172 | |
David Reid | 2ad94ab | 2014-02-03 10:01:15 -0800 | [diff] [blame] | 173 | :param bytes salt: A salt. Randomizes the KDF's output. Optional, but |
| 174 | highly recommended. Ideally as many bits of entropy as the security |
| 175 | level of the hash: often that means cryptographically random and as |
| 176 | long as the hash output. Worse (shorter, less entropy) salt values can |
| 177 | still meaningfully contribute to security. May be reused. Does not have |
| 178 | to be secret, but may cause stronger security guarantees if secret; see |
| 179 | `RFC 5869`_ and the `HKDF paper`_ for more details. If ``None`` is |
| 180 | explicitly passed a default salt of ``algorithm.digest_size // 8`` null |
| 181 | bytes will be used. |
David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 182 | |
| 183 | :param bytes info: Application specific context information. If ``None`` |
| 184 | is explicitly passed an empty byte string will be used. |
| 185 | |
Wouter Bolsterlee | d63cbd0 | 2014-03-01 00:45:31 +0100 | [diff] [blame] | 186 | :param backend: A |
David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 187 | :class:`~cryptography.hazmat.backends.interfaces.HMACBackend` |
| 188 | provider. |
| 189 | |
Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 190 | :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised if the |
Ayrx | 6870241 | 2014-03-15 23:29:36 +0800 | [diff] [blame] | 191 | provided ``backend`` does not implement |
| 192 | :class:`~cryptography.hazmat.backends.interfaces.HMACBackend` |
| 193 | |
David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 194 | .. method:: derive(key_material) |
| 195 | |
| 196 | :param bytes key_material: The input key material. |
| 197 | :retunr bytes: The derived key. |
| 198 | |
| 199 | Derives a new key from the input key material by performing both the |
| 200 | extract and expand operations. |
| 201 | |
| 202 | .. method:: verify(key_material, expected_key) |
| 203 | |
| 204 | :param key_material bytes: The input key material. This is the same as |
| 205 | ``key_material`` in :meth:`derive`. |
| 206 | :param expected_key bytes: The expected result of deriving a new key, |
| 207 | this is the same as the return value of |
| 208 | :meth:`derive`. |
| 209 | :raises cryptography.exceptions.InvalidKey: This is raised when the |
| 210 | derived key does not match |
| 211 | the expected key. |
| 212 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 213 | :meth:`derive` or |
| 214 | :meth:`verify` is |
| 215 | called more than |
| 216 | once. |
| 217 | |
| 218 | This checks whether deriving a new key from the supplied |
| 219 | ``key_material`` generates the same key as the ``expected_key``, and |
David Reid | b9fa771 | 2014-02-03 10:45:11 -0800 | [diff] [blame] | 220 | raises an exception if they do not match. |
David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 221 | |
Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 222 | |
Ayrx | c0ce911 | 2014-05-07 16:22:09 +0800 | [diff] [blame^] | 223 | .. class:: HKDFExpand(algorithm, length, info, backend) |
Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 224 | |
| 225 | .. versionadded:: 0.5 |
| 226 | |
| 227 | HKDF consists of two stages, extract and expand. This class exposes an |
| 228 | expand only version of HKDF that is suitable when the key material is |
| 229 | already cryptographically strong. |
| 230 | |
| 231 | .. warning:: |
| 232 | |
Ayrx | c0ce911 | 2014-05-07 16:22:09 +0800 | [diff] [blame^] | 233 | HKDFExpand should only be used if the key material is |
Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 234 | cryptographically strong. You should use |
| 235 | :class:`~cryptography.hazmat.primitives.kdf.hkdf.HKDF` if |
| 236 | you are unsure. |
| 237 | |
| 238 | .. doctest:: |
| 239 | |
| 240 | >>> import os |
| 241 | >>> from cryptography.hazmat.primitives import hashes |
Ayrx | c0ce911 | 2014-05-07 16:22:09 +0800 | [diff] [blame^] | 242 | >>> from cryptography.hazmat.primitives.kdf.hkdf import HKDFExpand |
Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 243 | >>> from cryptography.hazmat.backends import default_backend |
| 244 | >>> backend = default_backend() |
| 245 | >>> info = b"hkdf-example" |
| 246 | >>> key_material = os.urandom(16) |
Ayrx | c0ce911 | 2014-05-07 16:22:09 +0800 | [diff] [blame^] | 247 | >>> hkdf = HKDFExpand( |
Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 248 | ... algorithm=hashes.SHA256(), |
| 249 | ... length=32, |
| 250 | ... info=info, |
| 251 | ... backend=backend |
| 252 | ... ) |
| 253 | >>> key = hkdf.derive(key_material) |
Ayrx | c0ce911 | 2014-05-07 16:22:09 +0800 | [diff] [blame^] | 254 | >>> hkdf = HKDFExpand( |
Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 255 | ... algorithm=hashes.SHA256(), |
| 256 | ... length=32, |
| 257 | ... info=info, |
| 258 | ... backend=backend |
| 259 | ... ) |
| 260 | >>> hkdf.verify(key_material, key) |
| 261 | |
| 262 | :param algorithm: An instance of a |
| 263 | :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` |
| 264 | provider. |
| 265 | |
| 266 | :param int length: The desired length of the derived key. Maximum is |
| 267 | ``255 * (algorithm.digest_size // 8)``. |
| 268 | |
| 269 | :param bytes info: Application specific context information. If ``None`` |
| 270 | is explicitly passed an empty byte string will be used. |
| 271 | |
| 272 | :param backend: A |
| 273 | :class:`~cryptography.hazmat.backends.interfaces.HMACBackend` |
| 274 | provider. |
| 275 | |
| 276 | :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised if the |
| 277 | provided ``backend`` does not implement |
| 278 | :class:`~cryptography.hazmat.backends.interfaces.HMACBackend` |
| 279 | |
| 280 | .. method:: derive(key_material) |
| 281 | |
| 282 | :param bytes key_material: The input key material. |
| 283 | :return bytes: The derived key. |
| 284 | |
| 285 | Derives a new key from the input key material by performing both the |
| 286 | extract and expand operations. |
| 287 | |
| 288 | .. method:: verify(key_material, expected_key) |
| 289 | |
| 290 | :param key_material bytes: The input key material. This is the same as |
| 291 | ``key_material`` in :meth:`derive`. |
| 292 | :param expected_key bytes: The expected result of deriving a new key, |
| 293 | this is the same as the return value of |
| 294 | :meth:`derive`. |
| 295 | :raises cryptography.exceptions.InvalidKey: This is raised when the |
| 296 | derived key does not match |
| 297 | the expected key. |
| 298 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 299 | :meth:`derive` or |
| 300 | :meth:`verify` is |
| 301 | called more than |
| 302 | once. |
| 303 | |
| 304 | This checks whether deriving a new key from the supplied |
| 305 | ``key_material`` generates the same key as the ``expected_key``, and |
| 306 | raises an exception if they do not match. |
| 307 | |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 308 | .. _`NIST SP 800-132`: http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf |
Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 309 | .. _`Password Storage Cheat Sheet`: https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet |
David Reid | b80deea | 2014-02-03 10:33:16 -0800 | [diff] [blame] | 310 | .. _`PBKDF2`: https://en.wikipedia.org/wiki/PBKDF2 |
| 311 | .. _`scrypt`: https://en.wikipedia.org/wiki/Scrypt |
| 312 | .. _`key stretching`: https://en.wikipedia.org/wiki/Key_stretching |
David Reid | 2ad94ab | 2014-02-03 10:01:15 -0800 | [diff] [blame] | 313 | .. _`HKDF`: |
David Reid | b80deea | 2014-02-03 10:33:16 -0800 | [diff] [blame] | 314 | .. _`RFC 5869`: https://tools.ietf.org/html/rfc5869 |
| 315 | .. _`HKDF paper`: https://eprint.iacr.org/2010/264 |