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 | |
Paul Kehrer | 48402ff | 2015-02-16 15:31:52 -0600 | [diff] [blame] | 6 | .. module:: cryptography.hazmat.primitives.kdf |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 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 |
Paul Kehrer | 48402ff | 2015-02-16 15:31:52 -0600 | [diff] [blame] | 41 | :class:`~cryptography.hazmat.primitives.kdf.KeyDerivationFunction` |
Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 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 |
Paul Kehrer | 601278a | 2015-02-12 12:51:00 -0600 | [diff] [blame] | 72 | :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm` |
Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 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 | |
Ayrx | 00eff9c | 2014-05-17 19:47:09 +0800 | [diff] [blame] | 91 | :raises TypeError: This exception is raised if ``salt`` is not ``bytes``. |
Ayrx | 6d69eab | 2014-05-17 16:59:31 +0800 | [diff] [blame] | 92 | |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 93 | .. method:: derive(key_material) |
| 94 | |
Alex Gaynor | a8e125f | 2014-01-29 19:21:03 -0800 | [diff] [blame] | 95 | :param bytes key_material: The input key material. For PBKDF2 this |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 96 | should be a password. |
Paul Kehrer | 0b18118 | 2014-01-29 16:34:47 -0600 | [diff] [blame] | 97 | :return bytes: the derived key. |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 98 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 99 | :meth:`derive` or |
| 100 | :meth:`verify` is |
| 101 | called more than |
| 102 | once. |
| 103 | |
Ayrx | 6d69eab | 2014-05-17 16:59:31 +0800 | [diff] [blame] | 104 | :raises TypeError: This exception is raised if ``key_material`` is not |
Ayrx | 00eff9c | 2014-05-17 19:47:09 +0800 | [diff] [blame] | 105 | ``bytes``. |
Ayrx | 6d69eab | 2014-05-17 16:59:31 +0800 | [diff] [blame] | 106 | |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 107 | This generates and returns a new key from the supplied password. |
| 108 | |
| 109 | .. method:: verify(key_material, expected_key) |
| 110 | |
Alex Gaynor | a8e125f | 2014-01-29 19:21:03 -0800 | [diff] [blame] | 111 | :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] | 112 | ``key_material`` in :meth:`derive`. |
Alex Gaynor | a8e125f | 2014-01-29 19:21:03 -0800 | [diff] [blame] | 113 | :param bytes expected_key: The expected result of deriving a new key, |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 114 | 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 Kehrer | 99d5190 | 2014-01-28 20:16:20 -0600 | [diff] [blame] | 128 | checking whether the password a user provides matches the stored derived |
Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 129 | key. |
| 130 | |
David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 131 | |
| 132 | .. currentmodule:: cryptography.hazmat.primitives.kdf.hkdf |
| 133 | |
| 134 | .. class:: HKDF(algorithm, length, salt, info, backend) |
| 135 | |
| 136 | .. versionadded:: 0.2 |
| 137 | |
David Reid | 2ad94ab | 2014-02-03 10:01:15 -0800 | [diff] [blame] | 138 | `HKDF`_ (HMAC-based Extract-and-Expand Key Derivation Function) is suitable |
David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 139 | for deriving keys of a fixed size used for other cryptographic operations. |
Alex Gaynor | c43bb75 | 2014-02-12 16:42:11 -0800 | [diff] [blame] | 140 | |
| 141 | .. warning:: |
| 142 | |
| 143 | HKDF should not be used for password storage. |
David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 144 | |
David Reid | 5df929c | 2014-02-03 13:26:15 -0800 | [diff] [blame] | 145 | .. 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 Reid | 134f1f4 | 2014-02-03 13:54:30 -0800 | [diff] [blame] | 161 | >>> key = hkdf.derive(b"input key") |
David Reid | 5df929c | 2014-02-03 13:26:15 -0800 | [diff] [blame] | 162 | >>> 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 Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 171 | :param algorithm: An instance of a |
Paul Kehrer | 601278a | 2015-02-12 12:51:00 -0600 | [diff] [blame] | 172 | :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm` |
David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 173 | provider. |
| 174 | |
| 175 | :param int length: The desired length of the derived key. Maximum is |
David Reid | b89f34c | 2014-02-03 10:01:42 -0800 | [diff] [blame] | 176 | ``255 * (algorithm.digest_size // 8)``. |
David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 177 | |
David Reid | 2ad94ab | 2014-02-03 10:01:15 -0800 | [diff] [blame] | 178 | :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 Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 187 | |
| 188 | :param bytes info: Application specific context information. If ``None`` |
| 189 | is explicitly passed an empty byte string will be used. |
| 190 | |
Wouter Bolsterlee | d63cbd0 | 2014-03-01 00:45:31 +0100 | [diff] [blame] | 191 | :param backend: A |
David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 192 | :class:`~cryptography.hazmat.backends.interfaces.HMACBackend` |
| 193 | provider. |
| 194 | |
Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 195 | :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised if the |
Ayrx | 6870241 | 2014-03-15 23:29:36 +0800 | [diff] [blame] | 196 | provided ``backend`` does not implement |
| 197 | :class:`~cryptography.hazmat.backends.interfaces.HMACBackend` |
| 198 | |
Ayrx | 6d69eab | 2014-05-17 16:59:31 +0800 | [diff] [blame] | 199 | :raises TypeError: This exception is raised if ``salt`` or ``info`` is not |
Ayrx | 00eff9c | 2014-05-17 19:47:09 +0800 | [diff] [blame] | 200 | ``bytes``. |
Ayrx | 6d69eab | 2014-05-17 16:59:31 +0800 | [diff] [blame] | 201 | |
David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 202 | .. method:: derive(key_material) |
| 203 | |
| 204 | :param bytes key_material: The input key material. |
Ayrx | 1534e09 | 2014-05-06 14:19:46 +0800 | [diff] [blame] | 205 | :return bytes: The derived key. |
Ayrx | 6d69eab | 2014-05-17 16:59:31 +0800 | [diff] [blame] | 206 | :raises TypeError: This exception is raised if ``key_material`` is not |
Ayrx | 00eff9c | 2014-05-17 19:47:09 +0800 | [diff] [blame] | 207 | ``bytes``. |
David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 208 | |
| 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 Gaynor | 1cfc5d5 | 2014-11-23 17:44:28 -0600 | [diff] [blame] | 214 | :param bytes key_material: The input key material. This is the same as |
David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 215 | ``key_material`` in :meth:`derive`. |
Alex Gaynor | 1cfc5d5 | 2014-11-23 17:44:28 -0600 | [diff] [blame] | 216 | :param bytes expected_key: The expected result of deriving a new key, |
David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 217 | 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 Reid | b9fa771 | 2014-02-03 10:45:11 -0800 | [diff] [blame] | 230 | raises an exception if they do not match. |
David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 231 | |
Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 232 | |
Ayrx | c0ce911 | 2014-05-07 16:22:09 +0800 | [diff] [blame] | 233 | .. class:: HKDFExpand(algorithm, length, info, backend) |
Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 234 | |
| 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 | |
Ayrx | c0ce911 | 2014-05-07 16:22:09 +0800 | [diff] [blame] | 243 | HKDFExpand should only be used if the key material is |
Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 244 | 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 |
Ayrx | c0ce911 | 2014-05-07 16:22:09 +0800 | [diff] [blame] | 252 | >>> from cryptography.hazmat.primitives.kdf.hkdf import HKDFExpand |
Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 253 | >>> from cryptography.hazmat.backends import default_backend |
| 254 | >>> backend = default_backend() |
| 255 | >>> info = b"hkdf-example" |
| 256 | >>> key_material = os.urandom(16) |
Ayrx | c0ce911 | 2014-05-07 16:22:09 +0800 | [diff] [blame] | 257 | >>> hkdf = HKDFExpand( |
Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 258 | ... algorithm=hashes.SHA256(), |
| 259 | ... length=32, |
| 260 | ... info=info, |
| 261 | ... backend=backend |
| 262 | ... ) |
| 263 | >>> key = hkdf.derive(key_material) |
Ayrx | c0ce911 | 2014-05-07 16:22:09 +0800 | [diff] [blame] | 264 | >>> hkdf = HKDFExpand( |
Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 265 | ... 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 Kehrer | 601278a | 2015-02-12 12:51:00 -0600 | [diff] [blame] | 273 | :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm` |
Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 274 | 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` |
Ayrx | c48100a | 2014-05-10 13:01:46 +0800 | [diff] [blame] | 289 | :raises TypeError: This is raised if the provided ``info`` is a unicode object |
Ayrx | 00eff9c | 2014-05-17 19:47:09 +0800 | [diff] [blame] | 290 | :raises TypeError: This exception is raised if ``info`` is not ``bytes``. |
Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 291 | |
| 292 | .. method:: derive(key_material) |
| 293 | |
| 294 | :param bytes key_material: The input key material. |
| 295 | :return bytes: The derived key. |
| 296 | |
Ayrx | c48100a | 2014-05-10 13:01:46 +0800 | [diff] [blame] | 297 | :raises TypeError: This is raised if the provided ``key_material`` is |
| 298 | a unicode object |
Ayrx | 6d69eab | 2014-05-17 16:59:31 +0800 | [diff] [blame] | 299 | :raises TypeError: This exception is raised if ``key_material`` is not |
Ayrx | 00eff9c | 2014-05-17 19:47:09 +0800 | [diff] [blame] | 300 | ``bytes``. |
Ayrx | c48100a | 2014-05-10 13:01:46 +0800 | [diff] [blame] | 301 | |
Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 302 | 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 Gaynor | 1cfc5d5 | 2014-11-23 17:44:28 -0600 | [diff] [blame] | 307 | :param bytes key_material: The input key material. This is the same as |
Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 308 | ``key_material`` in :meth:`derive`. |
Alex Gaynor | 1cfc5d5 | 2014-11-23 17:44:28 -0600 | [diff] [blame] | 309 | :param bytes expected_key: The expected result of deriving a new key, |
Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 310 | 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. |
Ayrx | c48100a | 2014-05-10 13:01:46 +0800 | [diff] [blame] | 320 | :raises TypeError: This is raised if the provided ``key_material`` is |
| 321 | a unicode object |
Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 322 | |
| 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 Sorce | 8a690fb | 2015-05-01 15:56:30 -0400 | [diff] [blame] | 327 | .. 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() |
Simo Sorce | 8a690fb | 2015-05-01 15:56:30 -0400 | [diff] [blame] | 348 | >>> otherinfo = b"concatkdf-example" |
| 349 | >>> ckdf = ConcatKDFHash( |
| 350 | ... algorithm=hashes.SHA256(), |
| 351 | ... length=256, |
| 352 | ... otherinfo=otherinfo, |
| 353 | ... backend=backend |
| 354 | ... ) |
| 355 | >>> key = ckdf.derive(b"input key") |
| 356 | >>> ckdf = ConcatKDFHash( |
| 357 | ... algorithm=hashes.SHA256(), |
| 358 | ... length=256, |
| 359 | ... otherinfo=otherinfo, |
| 360 | ... backend=backend |
| 361 | ... ) |
| 362 | >>> ckdf.verify(b"input key", key) |
| 363 | |
| 364 | :param algorithm: An instance of a |
| 365 | :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm` |
| 366 | provider |
| 367 | |
| 368 | :param int length: The desired length of the derived key in bytes. |
| 369 | Maximum is ``hashlen * (2^32 -1)``. |
| 370 | |
| 371 | :param bytes otherinfo: Application specific context information. |
| 372 | If ``None`` is explicitly passed an empty byte string will be used. |
| 373 | |
| 374 | :param backend: A |
| 375 | :class:`~cryptography.hazmat.backends.interfaces.HashBackend` |
| 376 | provider. |
| 377 | |
| 378 | :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised |
| 379 | if the provided ``backend`` does not implement |
| 380 | :class:`~cryptography.hazmat.backends.interfaces.HashBackend` |
| 381 | |
| 382 | :raises TypeError: This exception is raised if ``otherinfo`` is not |
| 383 | ``bytes``. |
| 384 | |
| 385 | .. method:: derive(key_material) |
| 386 | |
| 387 | :param bytes key_material: The input key material. |
| 388 | :return bytes: The derived key. |
| 389 | :raises TypeError: This exception is raised if ``key_material`` is |
| 390 | not ``bytes``. |
| 391 | |
Terry Chia | dae4076 | 2015-06-13 11:26:16 +0800 | [diff] [blame] | 392 | Derives a new key from the input key material. |
Simo Sorce | 8a690fb | 2015-05-01 15:56:30 -0400 | [diff] [blame] | 393 | |
| 394 | .. method:: verify(key_material, expected_key) |
| 395 | |
| 396 | :param bytes key_material: The input key material. This is the same as |
| 397 | ``key_material`` in :meth:`derive`. |
| 398 | :param bytes expected_key: The expected result of deriving a new key, |
| 399 | this is the same as the return value of |
| 400 | :meth:`derive`. |
| 401 | :raises cryptography.exceptions.InvalidKey: This is raised when the |
| 402 | derived key does not match |
| 403 | the expected key. |
| 404 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 405 | :meth:`derive` or |
| 406 | :meth:`verify` is |
| 407 | called more than |
| 408 | once. |
| 409 | |
| 410 | This checks whether deriving a new key from the supplied |
| 411 | ``key_material`` generates the same key as the ``expected_key``, and |
| 412 | raises an exception if they do not match. |
| 413 | |
| 414 | |
| 415 | .. class:: ConcatKDFHMAC(algorithm, length, salt, otherinfo, backend) |
| 416 | |
| 417 | .. versionadded:: 1.0 |
| 418 | |
| 419 | Similar to ConcatKFDHash but uses an HMAC function instead. |
| 420 | |
| 421 | .. warning:: |
| 422 | |
| 423 | ConcatKDFHMAC should not be used for password storage. |
| 424 | |
| 425 | .. doctest:: |
| 426 | |
| 427 | >>> import os |
| 428 | >>> from cryptography.hazmat.primitives import hashes |
| 429 | >>> from cryptography.hazmat.primitives.kdf.concatkdf import ConcatKDFHMAC |
| 430 | >>> from cryptography.hazmat.backends import default_backend |
| 431 | >>> backend = default_backend() |
| 432 | >>> salt = os.urandom(16) |
| 433 | >>> otherinfo = b"concatkdf-example" |
| 434 | >>> ckdf = ConcatKDFHMAC( |
| 435 | ... algorithm=hashes.SHA256(), |
| 436 | ... length=256, |
| 437 | ... salt=salt, |
| 438 | ... otherinfo=otherinfo, |
| 439 | ... backend=backend |
| 440 | ... ) |
| 441 | >>> key = ckdf.derive(b"input key") |
| 442 | >>> ckdf = ConcatKDFHMAC( |
| 443 | ... algorithm=hashes.SHA256(), |
| 444 | ... length=256, |
| 445 | ... salt=salt, |
| 446 | ... otherinfo=otherinfo, |
| 447 | ... backend=backend |
| 448 | ... ) |
| 449 | >>> ckdf.verify(b"input key", key) |
| 450 | |
| 451 | :param algorithm: An instance of a |
| 452 | :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm` |
| 453 | provider |
| 454 | |
| 455 | :param int length: The desired length of the derived key in bytes. Maximum |
| 456 | is ``hashlen * (2^32 -1)``. |
| 457 | |
| 458 | :param bytes salt: A salt. Randomizes the KDF's output. Optional, but |
| 459 | highly recommended. Ideally as many bits of entropy as the security |
| 460 | level of the hash: often that means cryptographically random and as |
| 461 | long as the hash output. Does not have to be secret, but may cause |
| 462 | stronger security guarantees if secret; If ``None`` is explicitly |
| 463 | passed a default salt of ``algorithm.block_size`` null bytes will be |
| 464 | used. |
| 465 | |
| 466 | :param bytes otherinfo: Application specific context information. |
| 467 | If ``None`` is explicitly passed an empty byte string will be used. |
| 468 | |
| 469 | :param backend: A |
| 470 | :class:`~cryptography.hazmat.backends.interfaces.HMACBackend` |
| 471 | provider. |
| 472 | |
| 473 | :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised if the |
| 474 | provided ``backend`` does not implement |
| 475 | :class:`~cryptography.hazmat.backends.interfaces.HMACBackend` |
| 476 | |
| 477 | :raises TypeError: This exception is raised if ``salt`` or ``otherinfo`` |
| 478 | is not ``bytes``. |
| 479 | |
| 480 | .. method:: derive(key_material) |
| 481 | |
| 482 | :param bytes key_material: The input key material. |
| 483 | :return bytes: The derived key. |
| 484 | :raises TypeError: This exception is raised if ``key_material`` is not |
| 485 | ``bytes``. |
| 486 | |
Terry Chia | dae4076 | 2015-06-13 11:26:16 +0800 | [diff] [blame] | 487 | Derives a new key from the input key material. |
Simo Sorce | 8a690fb | 2015-05-01 15:56:30 -0400 | [diff] [blame] | 488 | |
| 489 | .. method:: verify(key_material, expected_key) |
| 490 | |
| 491 | :param bytes key_material: The input key material. This is the same as |
| 492 | ``key_material`` in :meth:`derive`. |
| 493 | :param bytes expected_key: The expected result of deriving a new key, |
| 494 | this is the same as the return value of |
| 495 | :meth:`derive`. |
| 496 | :raises cryptography.exceptions.InvalidKey: This is raised when the |
| 497 | derived key does not match |
| 498 | the expected key. |
| 499 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 500 | :meth:`derive` or |
| 501 | :meth:`verify` is |
| 502 | called more than |
| 503 | once. |
| 504 | |
| 505 | This checks whether deriving a new key from the supplied |
| 506 | ``key_material`` generates the same key as the ``expected_key``, and |
| 507 | raises an exception if they do not match. |
| 508 | |
Simo Sorce | 53c8f6a | 2015-09-15 11:13:56 -0400 | [diff] [blame^] | 509 | .. currentmodule:: cryptography.hazmat.primitives.kdf.x963kdf |
| 510 | |
| 511 | .. class:: X963KDF(algorithm, length, otherinfo, backend) |
| 512 | |
| 513 | .. versionadded:: 1.1 |
| 514 | |
| 515 | X963KDF (ANSI X9.63 Key Derivation Function) is defined by ANSI |
| 516 | in the `ANSI X9.63:2001`_ document, to be used to derive keys for use |
| 517 | after a Key Exchange negotiation operation. |
| 518 | |
| 519 | SECG in `SEC 1 v2.0`_ recommends that |
| 520 | :class:`~cryptography.hazmat.primitives.kdf.concatkdf.ConcatKDFHash` be |
| 521 | used for new projects. This KDF should only be used for backwards |
| 522 | compatibility with pre-existing implementations. |
| 523 | |
| 524 | |
| 525 | .. warning:: |
| 526 | |
| 527 | X963KDF should not be used for password storage. |
| 528 | |
| 529 | .. doctest:: |
| 530 | |
| 531 | >>> import os |
| 532 | >>> from cryptography.hazmat.primitives import hashes |
| 533 | >>> from cryptography.hazmat.primitives.kdf.x963kdf import X963KDF |
| 534 | >>> from cryptography.hazmat.backends import default_backend |
| 535 | >>> backend = default_backend() |
| 536 | >>> sharedinfo = b"ANSI X9.63 Example" |
| 537 | >>> xkdf = X963KDF( |
| 538 | ... algorithm=hashes.SHA256(), |
| 539 | ... length=256, |
| 540 | ... sharedinfo=sharedinfo, |
| 541 | ... backend=backend |
| 542 | ... ) |
| 543 | >>> key = xkdf.derive(b"input key") |
| 544 | >>> xkdf = X963KDF( |
| 545 | ... algorithm=hashes.SHA256(), |
| 546 | ... length=256, |
| 547 | ... sharedinfo=sharedinfo, |
| 548 | ... backend=backend |
| 549 | ... ) |
| 550 | >>> xkdf.verify(b"input key", key) |
| 551 | |
| 552 | :param algorithm: An instance of a |
| 553 | :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm` |
| 554 | provider |
| 555 | |
| 556 | :param int length: The desired length of the derived key in bytes. |
| 557 | Maximum is ``hashlen * (2^32 -1)``. |
| 558 | |
| 559 | :param bytes sharedinfo: Application specific context information. |
| 560 | If ``None`` is explicitly passed an empty byte string will be used. |
| 561 | |
| 562 | :param backend: A cryptography backend |
| 563 | :class:`~cryptography.hazmat.backends.interfaces.HashBackend` |
| 564 | provider. |
| 565 | |
| 566 | :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised |
| 567 | if the provided ``backend`` does not implement |
| 568 | :class:`~cryptography.hazmat.backends.interfaces.HashBackend` |
| 569 | |
| 570 | :raises TypeError: This exception is raised if ``sharedinfo`` is not |
| 571 | ``bytes``. |
| 572 | |
| 573 | .. method:: derive(key_material) |
| 574 | |
| 575 | :param bytes key_material: The input key material. |
| 576 | :return bytes: The derived key. |
| 577 | :raises TypeError: This exception is raised if ``key_material`` is |
| 578 | not ``bytes``. |
| 579 | |
| 580 | Derives a new key from the input key material. |
| 581 | |
| 582 | .. method:: verify(key_material, expected_key) |
| 583 | |
| 584 | :param bytes key_material: The input key material. This is the same as |
| 585 | ``key_material`` in :meth:`derive`. |
| 586 | :param bytes expected_key: The expected result of deriving a new key, |
| 587 | this is the same as the return value of |
| 588 | :meth:`derive`. |
| 589 | :raises cryptography.exceptions.InvalidKey: This is raised when the |
| 590 | derived key does not match |
| 591 | the expected key. |
| 592 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 593 | :meth:`derive` or |
| 594 | :meth:`verify` is |
| 595 | called more than |
| 596 | once. |
| 597 | |
| 598 | This checks whether deriving a new key from the supplied |
| 599 | ``key_material`` generates the same key as the ``expected_key``, and |
| 600 | raises an exception if they do not match. |
| 601 | |
Simo Sorce | 8a690fb | 2015-05-01 15:56:30 -0400 | [diff] [blame] | 602 | |
Paul Kehrer | 48402ff | 2015-02-16 15:31:52 -0600 | [diff] [blame] | 603 | Interface |
| 604 | ~~~~~~~~~ |
| 605 | |
| 606 | .. currentmodule:: cryptography.hazmat.primitives.kdf |
| 607 | |
| 608 | .. class:: KeyDerivationFunction |
| 609 | |
| 610 | .. versionadded:: 0.2 |
| 611 | |
| 612 | .. method:: derive(key_material) |
| 613 | |
| 614 | :param bytes key_material: The input key material. Depending on what |
| 615 | key derivation function you are using this |
| 616 | could be either random bytes, or a user |
| 617 | supplied password. |
| 618 | :return: The new key. |
| 619 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 620 | :meth:`derive` or |
| 621 | :meth:`verify` is |
| 622 | called more than |
| 623 | once. |
| 624 | |
| 625 | This generates and returns a new key from the supplied key material. |
| 626 | |
| 627 | .. method:: verify(key_material, expected_key) |
| 628 | |
| 629 | :param bytes key_material: The input key material. This is the same as |
| 630 | ``key_material`` in :meth:`derive`. |
| 631 | :param bytes expected_key: The expected result of deriving a new key, |
| 632 | this is the same as the return value of |
| 633 | :meth:`derive`. |
| 634 | :raises cryptography.exceptions.InvalidKey: This is raised when the |
| 635 | derived key does not match |
| 636 | the expected key. |
| 637 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 638 | :meth:`derive` or |
| 639 | :meth:`verify` is |
| 640 | called more than |
| 641 | once. |
| 642 | |
| 643 | This checks whether deriving a new key from the supplied |
| 644 | ``key_material`` generates the same key as the ``expected_key``, and |
| 645 | raises an exception if they do not match. This can be used for |
| 646 | something like checking whether a user's password attempt matches the |
| 647 | stored derived key. |
| 648 | |
| 649 | |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 650 | .. _`NIST SP 800-132`: http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf |
Simo Sorce | 8a690fb | 2015-05-01 15:56:30 -0400 | [diff] [blame] | 651 | .. _`NIST SP 800-56Ar2`: http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar2.pdf |
Simo Sorce | 53c8f6a | 2015-09-15 11:13:56 -0400 | [diff] [blame^] | 652 | .. _`ANSI X9.63:2001`: https://webstore.ansi.org |
| 653 | .. _`SEC 1 v2.0`: http://www.secg.org/sec1-v2.pdf |
Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 654 | .. _`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] | 655 | .. _`PBKDF2`: https://en.wikipedia.org/wiki/PBKDF2 |
| 656 | .. _`scrypt`: https://en.wikipedia.org/wiki/Scrypt |
| 657 | .. _`key stretching`: https://en.wikipedia.org/wiki/Key_stretching |
David Reid | 2ad94ab | 2014-02-03 10:01:15 -0800 | [diff] [blame] | 658 | .. _`HKDF`: |
David Reid | b80deea | 2014-02-03 10:33:16 -0800 | [diff] [blame] | 659 | .. _`RFC 5869`: https://tools.ietf.org/html/rfc5869 |
| 660 | .. _`HKDF paper`: https://eprint.iacr.org/2010/264 |