| 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 |
| Alex Gaynor | c3a3ed4 | 2016-10-07 09:34:58 -0500 | [diff] [blame] | 37 | key storage, but an alternate key storage KDF such as |
| 38 | :class:`~cryptography.hazmat.primitives.kdf.scrypt.Scrypt` is generally |
| Paul Kehrer | 1cab104 | 2014-01-29 14:30:11 -0600 | [diff] [blame] | 39 | considered a better solution. |
| Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 40 | |
| Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 41 | This class conforms to the |
| Paul Kehrer | 48402ff | 2015-02-16 15:31:52 -0600 | [diff] [blame] | 42 | :class:`~cryptography.hazmat.primitives.kdf.KeyDerivationFunction` |
| Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 43 | interface. |
| 44 | |
| Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 45 | .. doctest:: |
| 46 | |
| Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 47 | >>> import os |
| 48 | >>> from cryptography.hazmat.primitives import hashes |
| Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 49 | >>> from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC |
| Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 50 | >>> from cryptography.hazmat.backends import default_backend |
| 51 | >>> backend = default_backend() |
| Cory Benfield | 9b22eb9 | 2017-05-22 22:40:40 -0700 | [diff] [blame] | 52 | >>> # Salts should be randomly generated |
| Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 53 | >>> salt = os.urandom(16) |
| 54 | >>> # derive |
| Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 55 | >>> kdf = PBKDF2HMAC( |
| 56 | ... algorithm=hashes.SHA256(), |
| 57 | ... length=32, |
| 58 | ... salt=salt, |
| Paul Kehrer | 1277bc7 | 2014-01-28 17:09:59 -0600 | [diff] [blame] | 59 | ... iterations=100000, |
| Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 60 | ... backend=backend |
| 61 | ... ) |
| Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 62 | >>> key = kdf.derive(b"my great password") |
| 63 | >>> # verify |
| Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 64 | >>> kdf = PBKDF2HMAC( |
| 65 | ... algorithm=hashes.SHA256(), |
| 66 | ... length=32, |
| 67 | ... salt=salt, |
| Paul Kehrer | 1277bc7 | 2014-01-28 17:09:59 -0600 | [diff] [blame] | 68 | ... iterations=100000, |
| Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 69 | ... backend=backend |
| 70 | ... ) |
| Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 71 | >>> kdf.verify(b"my great password", key) |
| Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 72 | |
| Gabriel Orisaka | 617fe4b | 2016-07-31 10:49:59 -0300 | [diff] [blame] | 73 | :param algorithm: An instance of |
| 74 | :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`. |
| Paul Kehrer | a857fe6 | 2017-06-28 23:03:29 -0500 | [diff] [blame] | 75 | :param int length: The desired length of the derived key in bytes. Maximum |
| 76 | is (2\ :sup:`32` - 1) * ``algorithm.digest_size``. |
| Cory Benfield | 9b22eb9 | 2017-05-22 22:40:40 -0700 | [diff] [blame] | 77 | :param bytes salt: A salt. Secure values [#nist]_ are 128-bits (16 bytes) |
| 78 | or longer and randomly generated. |
| Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 79 | :param int iterations: The number of iterations to perform of the hash |
| Paul Kehrer | c58b478 | 2014-01-29 13:56:25 -0600 | [diff] [blame] | 80 | function. This can be used to control the length of time the operation |
| 81 | takes. Higher numbers help mitigate brute force attacks against derived |
| 82 | keys. See OWASP's `Password Storage Cheat Sheet`_ for more |
| Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 83 | detailed recommendations if you intend to use this for password storage. |
| Gabriel Orisaka | 617fe4b | 2016-07-31 10:49:59 -0300 | [diff] [blame] | 84 | :param backend: An instance of |
| 85 | :class:`~cryptography.hazmat.backends.interfaces.PBKDF2HMACBackend`. |
| 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 | |
| Paul Kehrer | 62e22a5 | 2019-01-17 15:53:16 -0600 | [diff] [blame] | 95 | :param 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 | 62e22a5 | 2019-01-17 15:53:16 -0600 | [diff] [blame] | 97 | :type key_material: :term:`bytes-like` |
| Paul Kehrer | 0b18118 | 2014-01-29 16:34:47 -0600 | [diff] [blame] | 98 | :return bytes: the derived key. |
| Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 99 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 100 | :meth:`derive` or |
| 101 | :meth:`verify` is |
| 102 | called more than |
| 103 | once. |
| 104 | |
| Ayrx | 6d69eab | 2014-05-17 16:59:31 +0800 | [diff] [blame] | 105 | :raises TypeError: This exception is raised if ``key_material`` is not |
| Ayrx | 00eff9c | 2014-05-17 19:47:09 +0800 | [diff] [blame] | 106 | ``bytes``. |
| Ayrx | 6d69eab | 2014-05-17 16:59:31 +0800 | [diff] [blame] | 107 | |
| Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 108 | This generates and returns a new key from the supplied password. |
| 109 | |
| 110 | .. method:: verify(key_material, expected_key) |
| 111 | |
| Alex Gaynor | a8e125f | 2014-01-29 19:21:03 -0800 | [diff] [blame] | 112 | :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] | 113 | ``key_material`` in :meth:`derive`. |
| Alex Gaynor | a8e125f | 2014-01-29 19:21:03 -0800 | [diff] [blame] | 114 | :param bytes expected_key: The expected result of deriving a new key, |
| Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 115 | this is the same as the return value of |
| 116 | :meth:`derive`. |
| 117 | :raises cryptography.exceptions.InvalidKey: This is raised when the |
| 118 | derived key does not match |
| 119 | the expected key. |
| 120 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 121 | :meth:`derive` or |
| 122 | :meth:`verify` is |
| 123 | called more than |
| 124 | once. |
| 125 | |
| 126 | This checks whether deriving a new key from the supplied |
| 127 | ``key_material`` generates the same key as the ``expected_key``, and |
| 128 | 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] | 129 | checking whether the password a user provides matches the stored derived |
| Paul Kehrer | 3d8c66f | 2014-01-28 17:36:50 -0600 | [diff] [blame] | 130 | key. |
| 131 | |
| David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 132 | |
| 133 | .. currentmodule:: cryptography.hazmat.primitives.kdf.hkdf |
| 134 | |
| 135 | .. class:: HKDF(algorithm, length, salt, info, backend) |
| 136 | |
| 137 | .. versionadded:: 0.2 |
| 138 | |
| David Reid | 2ad94ab | 2014-02-03 10:01:15 -0800 | [diff] [blame] | 139 | `HKDF`_ (HMAC-based Extract-and-Expand Key Derivation Function) is suitable |
| David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 140 | for deriving keys of a fixed size used for other cryptographic operations. |
| Alex Gaynor | c43bb75 | 2014-02-12 16:42:11 -0800 | [diff] [blame] | 141 | |
| 142 | .. warning:: |
| 143 | |
| 144 | HKDF should not be used for password storage. |
| David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 145 | |
| David Reid | 5df929c | 2014-02-03 13:26:15 -0800 | [diff] [blame] | 146 | .. doctest:: |
| 147 | |
| 148 | >>> import os |
| 149 | >>> from cryptography.hazmat.primitives import hashes |
| 150 | >>> from cryptography.hazmat.primitives.kdf.hkdf import HKDF |
| 151 | >>> from cryptography.hazmat.backends import default_backend |
| 152 | >>> backend = default_backend() |
| 153 | >>> salt = os.urandom(16) |
| 154 | >>> info = b"hkdf-example" |
| 155 | >>> hkdf = HKDF( |
| 156 | ... algorithm=hashes.SHA256(), |
| 157 | ... length=32, |
| 158 | ... salt=salt, |
| 159 | ... info=info, |
| 160 | ... backend=backend |
| 161 | ... ) |
| David Reid | 134f1f4 | 2014-02-03 13:54:30 -0800 | [diff] [blame] | 162 | >>> key = hkdf.derive(b"input key") |
| David Reid | 5df929c | 2014-02-03 13:26:15 -0800 | [diff] [blame] | 163 | >>> hkdf = HKDF( |
| 164 | ... algorithm=hashes.SHA256(), |
| 165 | ... length=32, |
| 166 | ... salt=salt, |
| 167 | ... info=info, |
| 168 | ... backend=backend |
| 169 | ... ) |
| 170 | >>> hkdf.verify(b"input key", key) |
| 171 | |
| Gabriel Orisaka | 617fe4b | 2016-07-31 10:49:59 -0300 | [diff] [blame] | 172 | :param algorithm: An instance of |
| 173 | :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`. |
| David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 174 | |
| Paul Kehrer | a857fe6 | 2017-06-28 23:03:29 -0500 | [diff] [blame] | 175 | :param int length: The desired length of the derived key in bytes. Maximum |
| 176 | is ``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 |
| Alex Gaynor | 2e8725d | 2016-08-29 21:40:19 -0400 | [diff] [blame] | 184 | :rfc:`5869` and the `HKDF paper`_ for more details. If ``None`` is |
| David Reid | 2ad94ab | 2014-02-03 10:01:15 -0800 | [diff] [blame] | 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 | |
| Gabriel Orisaka | 617fe4b | 2016-07-31 10:49:59 -0300 | [diff] [blame] | 191 | :param backend: An instance of |
| 192 | :class:`~cryptography.hazmat.backends.interfaces.HMACBackend`. |
| David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 193 | |
| Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 194 | :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised if the |
| Ayrx | 6870241 | 2014-03-15 23:29:36 +0800 | [diff] [blame] | 195 | provided ``backend`` does not implement |
| 196 | :class:`~cryptography.hazmat.backends.interfaces.HMACBackend` |
| 197 | |
| Ayrx | 6d69eab | 2014-05-17 16:59:31 +0800 | [diff] [blame] | 198 | :raises TypeError: This exception is raised if ``salt`` or ``info`` is not |
| Ayrx | 00eff9c | 2014-05-17 19:47:09 +0800 | [diff] [blame] | 199 | ``bytes``. |
| Ayrx | 6d69eab | 2014-05-17 16:59:31 +0800 | [diff] [blame] | 200 | |
| David Reid | c0248b9 | 2014-01-30 15:23:33 -0800 | [diff] [blame] | 201 | .. method:: derive(key_material) |
| 202 | |
| Paul Kehrer | 62e22a5 | 2019-01-17 15:53:16 -0600 | [diff] [blame] | 203 | :param key_material: The input key material. |
| 204 | :type key_material: :term:`bytes-like` |
| 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 | |
| Gabriel Orisaka | 617fe4b | 2016-07-31 10:49:59 -0300 | [diff] [blame] | 272 | :param algorithm: An instance of |
| 273 | :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`. |
| Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 274 | |
| Paul Kehrer | a857fe6 | 2017-06-28 23:03:29 -0500 | [diff] [blame] | 275 | :param int length: The desired length of the derived key in bytes. Maximum |
| 276 | is ``255 * (algorithm.digest_size // 8)``. |
| Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 277 | |
| 278 | :param bytes info: Application specific context information. If ``None`` |
| 279 | is explicitly passed an empty byte string will be used. |
| 280 | |
| Gabriel Orisaka | 617fe4b | 2016-07-31 10:49:59 -0300 | [diff] [blame] | 281 | :param backend: An instance of |
| 282 | :class:`~cryptography.hazmat.backends.interfaces.HMACBackend`. |
| Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 283 | |
| 284 | :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised if the |
| 285 | provided ``backend`` does not implement |
| 286 | :class:`~cryptography.hazmat.backends.interfaces.HMACBackend` |
| Ayrx | 00eff9c | 2014-05-17 19:47:09 +0800 | [diff] [blame] | 287 | :raises TypeError: This exception is raised if ``info`` is not ``bytes``. |
| Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 288 | |
| 289 | .. method:: derive(key_material) |
| 290 | |
| 291 | :param bytes key_material: The input key material. |
| 292 | :return bytes: The derived key. |
| 293 | |
| Ayrx | 6d69eab | 2014-05-17 16:59:31 +0800 | [diff] [blame] | 294 | :raises TypeError: This exception is raised if ``key_material`` is not |
| Ayrx | 00eff9c | 2014-05-17 19:47:09 +0800 | [diff] [blame] | 295 | ``bytes``. |
| Ayrx | c48100a | 2014-05-10 13:01:46 +0800 | [diff] [blame] | 296 | |
| Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 297 | Derives a new key from the input key material by performing both the |
| 298 | extract and expand operations. |
| 299 | |
| 300 | .. method:: verify(key_material, expected_key) |
| 301 | |
| Alex Gaynor | 1cfc5d5 | 2014-11-23 17:44:28 -0600 | [diff] [blame] | 302 | :param bytes key_material: The input key material. This is the same as |
| Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 303 | ``key_material`` in :meth:`derive`. |
| Alex Gaynor | 1cfc5d5 | 2014-11-23 17:44:28 -0600 | [diff] [blame] | 304 | :param bytes expected_key: The expected result of deriving a new key, |
| Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 305 | this is the same as the return value of |
| 306 | :meth:`derive`. |
| 307 | :raises cryptography.exceptions.InvalidKey: This is raised when the |
| 308 | derived key does not match |
| 309 | the expected key. |
| 310 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 311 | :meth:`derive` or |
| 312 | :meth:`verify` is |
| 313 | called more than |
| 314 | once. |
| Ayrx | c48100a | 2014-05-10 13:01:46 +0800 | [diff] [blame] | 315 | :raises TypeError: This is raised if the provided ``key_material`` is |
| Alex Gaynor | 617825d | 2018-05-12 14:33:20 -0400 | [diff] [blame] | 316 | a ``unicode`` object |
| Ayrx | 9d72f12 | 2014-05-06 20:27:51 +0800 | [diff] [blame] | 317 | |
| 318 | This checks whether deriving a new key from the supplied |
| 319 | ``key_material`` generates the same key as the ``expected_key``, and |
| 320 | raises an exception if they do not match. |
| 321 | |
| Simo Sorce | 8a690fb | 2015-05-01 15:56:30 -0400 | [diff] [blame] | 322 | .. currentmodule:: cryptography.hazmat.primitives.kdf.concatkdf |
| 323 | |
| 324 | .. class:: ConcatKDFHash(algorithm, length, otherinfo, backend) |
| 325 | |
| 326 | .. versionadded:: 1.0 |
| 327 | |
| 328 | ConcatKDFHash (Concatenation Key Derivation Function) is defined by the |
| 329 | NIST Special Publication `NIST SP 800-56Ar2`_ document, to be used to |
| 330 | derive keys for use after a Key Exchange negotiation operation. |
| 331 | |
| 332 | .. warning:: |
| 333 | |
| 334 | ConcatKDFHash should not be used for password storage. |
| 335 | |
| 336 | .. doctest:: |
| 337 | |
| 338 | >>> import os |
| 339 | >>> from cryptography.hazmat.primitives import hashes |
| 340 | >>> from cryptography.hazmat.primitives.kdf.concatkdf import ConcatKDFHash |
| 341 | >>> from cryptography.hazmat.backends import default_backend |
| 342 | >>> backend = default_backend() |
| Simo Sorce | 8a690fb | 2015-05-01 15:56:30 -0400 | [diff] [blame] | 343 | >>> otherinfo = b"concatkdf-example" |
| 344 | >>> ckdf = ConcatKDFHash( |
| 345 | ... algorithm=hashes.SHA256(), |
| Paul Kehrer | a857fe6 | 2017-06-28 23:03:29 -0500 | [diff] [blame] | 346 | ... length=32, |
| Simo Sorce | 8a690fb | 2015-05-01 15:56:30 -0400 | [diff] [blame] | 347 | ... otherinfo=otherinfo, |
| 348 | ... backend=backend |
| 349 | ... ) |
| 350 | >>> key = ckdf.derive(b"input key") |
| 351 | >>> ckdf = ConcatKDFHash( |
| 352 | ... algorithm=hashes.SHA256(), |
| Paul Kehrer | a857fe6 | 2017-06-28 23:03:29 -0500 | [diff] [blame] | 353 | ... length=32, |
| Simo Sorce | 8a690fb | 2015-05-01 15:56:30 -0400 | [diff] [blame] | 354 | ... otherinfo=otherinfo, |
| 355 | ... backend=backend |
| 356 | ... ) |
| 357 | >>> ckdf.verify(b"input key", key) |
| 358 | |
| Gabriel Orisaka | 617fe4b | 2016-07-31 10:49:59 -0300 | [diff] [blame] | 359 | :param algorithm: An instance of |
| 360 | :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`. |
| Simo Sorce | 8a690fb | 2015-05-01 15:56:30 -0400 | [diff] [blame] | 361 | |
| 362 | :param int length: The desired length of the derived key in bytes. |
| 363 | Maximum is ``hashlen * (2^32 -1)``. |
| 364 | |
| 365 | :param bytes otherinfo: Application specific context information. |
| 366 | If ``None`` is explicitly passed an empty byte string will be used. |
| 367 | |
| Gabriel Orisaka | 617fe4b | 2016-07-31 10:49:59 -0300 | [diff] [blame] | 368 | :param backend: An instance of |
| 369 | :class:`~cryptography.hazmat.backends.interfaces.HashBackend`. |
| Simo Sorce | 8a690fb | 2015-05-01 15:56:30 -0400 | [diff] [blame] | 370 | |
| 371 | :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised |
| 372 | if the provided ``backend`` does not implement |
| 373 | :class:`~cryptography.hazmat.backends.interfaces.HashBackend` |
| 374 | |
| 375 | :raises TypeError: This exception is raised if ``otherinfo`` is not |
| 376 | ``bytes``. |
| 377 | |
| 378 | .. method:: derive(key_material) |
| 379 | |
| Paul Kehrer | a779944 | 2019-01-17 15:56:23 -0600 | [diff] [blame^] | 380 | :param key_material: The input key material. |
| 381 | :type key_material: :term:`bytes-like` |
| Simo Sorce | 8a690fb | 2015-05-01 15:56:30 -0400 | [diff] [blame] | 382 | :return bytes: The derived key. |
| 383 | :raises TypeError: This exception is raised if ``key_material`` is |
| 384 | not ``bytes``. |
| 385 | |
| Terry Chia | dae4076 | 2015-06-13 11:26:16 +0800 | [diff] [blame] | 386 | Derives a new key from the input key material. |
| Simo Sorce | 8a690fb | 2015-05-01 15:56:30 -0400 | [diff] [blame] | 387 | |
| 388 | .. method:: verify(key_material, expected_key) |
| 389 | |
| 390 | :param bytes key_material: The input key material. This is the same as |
| 391 | ``key_material`` in :meth:`derive`. |
| 392 | :param bytes expected_key: The expected result of deriving a new key, |
| 393 | this is the same as the return value of |
| 394 | :meth:`derive`. |
| 395 | :raises cryptography.exceptions.InvalidKey: This is raised when the |
| 396 | derived key does not match |
| 397 | the expected key. |
| 398 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 399 | :meth:`derive` or |
| 400 | :meth:`verify` is |
| 401 | called more than |
| 402 | once. |
| 403 | |
| 404 | This checks whether deriving a new key from the supplied |
| 405 | ``key_material`` generates the same key as the ``expected_key``, and |
| 406 | raises an exception if they do not match. |
| 407 | |
| 408 | |
| 409 | .. class:: ConcatKDFHMAC(algorithm, length, salt, otherinfo, backend) |
| 410 | |
| 411 | .. versionadded:: 1.0 |
| 412 | |
| 413 | Similar to ConcatKFDHash but uses an HMAC function instead. |
| 414 | |
| 415 | .. warning:: |
| 416 | |
| 417 | ConcatKDFHMAC should not be used for password storage. |
| 418 | |
| 419 | .. doctest:: |
| 420 | |
| 421 | >>> import os |
| 422 | >>> from cryptography.hazmat.primitives import hashes |
| 423 | >>> from cryptography.hazmat.primitives.kdf.concatkdf import ConcatKDFHMAC |
| 424 | >>> from cryptography.hazmat.backends import default_backend |
| 425 | >>> backend = default_backend() |
| 426 | >>> salt = os.urandom(16) |
| 427 | >>> otherinfo = b"concatkdf-example" |
| 428 | >>> ckdf = ConcatKDFHMAC( |
| 429 | ... algorithm=hashes.SHA256(), |
| Paul Kehrer | a857fe6 | 2017-06-28 23:03:29 -0500 | [diff] [blame] | 430 | ... length=32, |
| Simo Sorce | 8a690fb | 2015-05-01 15:56:30 -0400 | [diff] [blame] | 431 | ... salt=salt, |
| 432 | ... otherinfo=otherinfo, |
| 433 | ... backend=backend |
| 434 | ... ) |
| 435 | >>> key = ckdf.derive(b"input key") |
| 436 | >>> ckdf = ConcatKDFHMAC( |
| 437 | ... algorithm=hashes.SHA256(), |
| Paul Kehrer | a857fe6 | 2017-06-28 23:03:29 -0500 | [diff] [blame] | 438 | ... length=32, |
| Simo Sorce | 8a690fb | 2015-05-01 15:56:30 -0400 | [diff] [blame] | 439 | ... salt=salt, |
| 440 | ... otherinfo=otherinfo, |
| 441 | ... backend=backend |
| 442 | ... ) |
| 443 | >>> ckdf.verify(b"input key", key) |
| 444 | |
| Gabriel Orisaka | 617fe4b | 2016-07-31 10:49:59 -0300 | [diff] [blame] | 445 | :param algorithm: An instance of |
| 446 | :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`. |
| Simo Sorce | 8a690fb | 2015-05-01 15:56:30 -0400 | [diff] [blame] | 447 | |
| 448 | :param int length: The desired length of the derived key in bytes. Maximum |
| 449 | is ``hashlen * (2^32 -1)``. |
| 450 | |
| 451 | :param bytes salt: A salt. Randomizes the KDF's output. Optional, but |
| 452 | highly recommended. Ideally as many bits of entropy as the security |
| 453 | level of the hash: often that means cryptographically random and as |
| 454 | long as the hash output. Does not have to be secret, but may cause |
| 455 | stronger security guarantees if secret; If ``None`` is explicitly |
| 456 | passed a default salt of ``algorithm.block_size`` null bytes will be |
| 457 | used. |
| 458 | |
| 459 | :param bytes otherinfo: Application specific context information. |
| 460 | If ``None`` is explicitly passed an empty byte string will be used. |
| 461 | |
| Gabriel Orisaka | 617fe4b | 2016-07-31 10:49:59 -0300 | [diff] [blame] | 462 | :param backend: An instance of |
| 463 | :class:`~cryptography.hazmat.backends.interfaces.HMACBackend`. |
| Simo Sorce | 8a690fb | 2015-05-01 15:56:30 -0400 | [diff] [blame] | 464 | |
| 465 | :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised if the |
| 466 | provided ``backend`` does not implement |
| 467 | :class:`~cryptography.hazmat.backends.interfaces.HMACBackend` |
| 468 | |
| 469 | :raises TypeError: This exception is raised if ``salt`` or ``otherinfo`` |
| 470 | is not ``bytes``. |
| 471 | |
| 472 | .. method:: derive(key_material) |
| 473 | |
| 474 | :param bytes key_material: The input key material. |
| 475 | :return bytes: The derived key. |
| 476 | :raises TypeError: This exception is raised if ``key_material`` is not |
| 477 | ``bytes``. |
| 478 | |
| Terry Chia | dae4076 | 2015-06-13 11:26:16 +0800 | [diff] [blame] | 479 | Derives a new key from the input key material. |
| Simo Sorce | 8a690fb | 2015-05-01 15:56:30 -0400 | [diff] [blame] | 480 | |
| 481 | .. method:: verify(key_material, expected_key) |
| 482 | |
| 483 | :param bytes key_material: The input key material. This is the same as |
| 484 | ``key_material`` in :meth:`derive`. |
| 485 | :param bytes expected_key: The expected result of deriving a new key, |
| 486 | this is the same as the return value of |
| 487 | :meth:`derive`. |
| 488 | :raises cryptography.exceptions.InvalidKey: This is raised when the |
| 489 | derived key does not match |
| 490 | the expected key. |
| 491 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 492 | :meth:`derive` or |
| 493 | :meth:`verify` is |
| 494 | called more than |
| 495 | once. |
| 496 | |
| 497 | This checks whether deriving a new key from the supplied |
| 498 | ``key_material`` generates the same key as the ``expected_key``, and |
| 499 | raises an exception if they do not match. |
| 500 | |
| Simo Sorce | 53c8f6a | 2015-09-15 11:13:56 -0400 | [diff] [blame] | 501 | .. currentmodule:: cryptography.hazmat.primitives.kdf.x963kdf |
| 502 | |
| 503 | .. class:: X963KDF(algorithm, length, otherinfo, backend) |
| 504 | |
| 505 | .. versionadded:: 1.1 |
| 506 | |
| 507 | X963KDF (ANSI X9.63 Key Derivation Function) is defined by ANSI |
| 508 | in the `ANSI X9.63:2001`_ document, to be used to derive keys for use |
| 509 | after a Key Exchange negotiation operation. |
| 510 | |
| 511 | SECG in `SEC 1 v2.0`_ recommends that |
| 512 | :class:`~cryptography.hazmat.primitives.kdf.concatkdf.ConcatKDFHash` be |
| 513 | used for new projects. This KDF should only be used for backwards |
| Alex Gaynor | ace036d | 2015-09-24 20:23:08 -0400 | [diff] [blame] | 514 | compatibility with pre-existing protocols. |
| Simo Sorce | 53c8f6a | 2015-09-15 11:13:56 -0400 | [diff] [blame] | 515 | |
| 516 | |
| 517 | .. warning:: |
| 518 | |
| 519 | X963KDF should not be used for password storage. |
| 520 | |
| 521 | .. doctest:: |
| 522 | |
| 523 | >>> import os |
| 524 | >>> from cryptography.hazmat.primitives import hashes |
| 525 | >>> from cryptography.hazmat.primitives.kdf.x963kdf import X963KDF |
| 526 | >>> from cryptography.hazmat.backends import default_backend |
| 527 | >>> backend = default_backend() |
| 528 | >>> sharedinfo = b"ANSI X9.63 Example" |
| 529 | >>> xkdf = X963KDF( |
| 530 | ... algorithm=hashes.SHA256(), |
| Paul Kehrer | a857fe6 | 2017-06-28 23:03:29 -0500 | [diff] [blame] | 531 | ... length=32, |
| Simo Sorce | 53c8f6a | 2015-09-15 11:13:56 -0400 | [diff] [blame] | 532 | ... sharedinfo=sharedinfo, |
| 533 | ... backend=backend |
| 534 | ... ) |
| 535 | >>> key = xkdf.derive(b"input key") |
| 536 | >>> xkdf = X963KDF( |
| 537 | ... algorithm=hashes.SHA256(), |
| Paul Kehrer | a857fe6 | 2017-06-28 23:03:29 -0500 | [diff] [blame] | 538 | ... length=32, |
| Simo Sorce | 53c8f6a | 2015-09-15 11:13:56 -0400 | [diff] [blame] | 539 | ... sharedinfo=sharedinfo, |
| 540 | ... backend=backend |
| 541 | ... ) |
| 542 | >>> xkdf.verify(b"input key", key) |
| 543 | |
| Gabriel Orisaka | 617fe4b | 2016-07-31 10:49:59 -0300 | [diff] [blame] | 544 | :param algorithm: An instance of |
| 545 | :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`. |
| Simo Sorce | 53c8f6a | 2015-09-15 11:13:56 -0400 | [diff] [blame] | 546 | |
| 547 | :param int length: The desired length of the derived key in bytes. |
| 548 | Maximum is ``hashlen * (2^32 -1)``. |
| 549 | |
| 550 | :param bytes sharedinfo: Application specific context information. |
| 551 | If ``None`` is explicitly passed an empty byte string will be used. |
| 552 | |
| 553 | :param backend: A cryptography backend |
| 554 | :class:`~cryptography.hazmat.backends.interfaces.HashBackend` |
| Gabriel Orisaka | 617fe4b | 2016-07-31 10:49:59 -0300 | [diff] [blame] | 555 | instance. |
| Simo Sorce | 53c8f6a | 2015-09-15 11:13:56 -0400 | [diff] [blame] | 556 | |
| 557 | :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised |
| 558 | if the provided ``backend`` does not implement |
| 559 | :class:`~cryptography.hazmat.backends.interfaces.HashBackend` |
| 560 | |
| 561 | :raises TypeError: This exception is raised if ``sharedinfo`` is not |
| 562 | ``bytes``. |
| 563 | |
| 564 | .. method:: derive(key_material) |
| 565 | |
| Paul Kehrer | a779944 | 2019-01-17 15:56:23 -0600 | [diff] [blame^] | 566 | :param key_material: The input key material. |
| 567 | :type key_material: :term:`bytes-like` |
| Simo Sorce | 53c8f6a | 2015-09-15 11:13:56 -0400 | [diff] [blame] | 568 | :return bytes: The derived key. |
| 569 | :raises TypeError: This exception is raised if ``key_material`` is |
| 570 | not ``bytes``. |
| 571 | |
| 572 | Derives a new key from the input key material. |
| 573 | |
| 574 | .. method:: verify(key_material, expected_key) |
| 575 | |
| 576 | :param bytes key_material: The input key material. This is the same as |
| 577 | ``key_material`` in :meth:`derive`. |
| 578 | :param bytes expected_key: The expected result of deriving a new key, |
| 579 | this is the same as the return value of |
| 580 | :meth:`derive`. |
| 581 | :raises cryptography.exceptions.InvalidKey: This is raised when the |
| 582 | derived key does not match |
| 583 | the expected key. |
| 584 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 585 | :meth:`derive` or |
| 586 | :meth:`verify` is |
| 587 | called more than |
| 588 | once. |
| 589 | |
| 590 | This checks whether deriving a new key from the supplied |
| 591 | ``key_material`` generates the same key as the ``expected_key``, and |
| 592 | raises an exception if they do not match. |
| 593 | |
| Simo Sorce | 8a690fb | 2015-05-01 15:56:30 -0400 | [diff] [blame] | 594 | |
| Jared | 6d7fe00 | 2016-05-29 17:32:37 -0700 | [diff] [blame] | 595 | .. currentmodule:: cryptography.hazmat.primitives.kdf.kbkdf |
| 596 | |
| 597 | .. class:: KBKDFHMAC(algorithm, mode, length, rlen, llen, location,\ |
| 598 | label, context, fixed, backend) |
| 599 | |
| 600 | .. versionadded:: 1.4 |
| 601 | |
| 602 | KBKDF (Key Based Key Derivation Function) is defined by the |
| 603 | `NIST SP 800-108`_ document, to be used to derive additional |
| 604 | keys from a key that has been established through an automated |
| 605 | key-establishment scheme. |
| 606 | |
| 607 | .. warning:: |
| 608 | |
| 609 | KBKDFHMAC should not be used for password storage. |
| 610 | |
| 611 | .. doctest:: |
| 612 | |
| 613 | >>> import os |
| 614 | >>> from cryptography.hazmat.primitives import hashes |
| 615 | >>> from cryptography.hazmat.primitives.kdf.kbkdf import ( |
| 616 | ... CounterLocation, KBKDFHMAC, Mode |
| 617 | ... ) |
| 618 | >>> from cryptography.hazmat.backends import default_backend |
| 619 | >>> backend = default_backend() |
| 620 | >>> label = b"KBKDF HMAC Label" |
| 621 | >>> context = b"KBKDF HMAC Context" |
| 622 | >>> kdf = KBKDFHMAC( |
| 623 | ... algorithm=hashes.SHA256(), |
| 624 | ... mode=Mode.CounterMode, |
| Paul Kehrer | a857fe6 | 2017-06-28 23:03:29 -0500 | [diff] [blame] | 625 | ... length=32, |
| Jared | 6d7fe00 | 2016-05-29 17:32:37 -0700 | [diff] [blame] | 626 | ... rlen=4, |
| 627 | ... llen=4, |
| 628 | ... location=CounterLocation.BeforeFixed, |
| 629 | ... label=label, |
| 630 | ... context=context, |
| 631 | ... fixed=None, |
| 632 | ... backend=backend |
| 633 | ... ) |
| 634 | >>> key = kdf.derive(b"input key") |
| 635 | >>> kdf = KBKDFHMAC( |
| 636 | ... algorithm=hashes.SHA256(), |
| 637 | ... mode=Mode.CounterMode, |
| Paul Kehrer | a857fe6 | 2017-06-28 23:03:29 -0500 | [diff] [blame] | 638 | ... length=32, |
| Jared | 6d7fe00 | 2016-05-29 17:32:37 -0700 | [diff] [blame] | 639 | ... rlen=4, |
| 640 | ... llen=4, |
| 641 | ... location=CounterLocation.BeforeFixed, |
| 642 | ... label=label, |
| 643 | ... context=context, |
| 644 | ... fixed=None, |
| 645 | ... backend=backend |
| 646 | ... ) |
| 647 | >>> kdf.verify(b"input key", key) |
| 648 | |
| Gabriel Orisaka | 617fe4b | 2016-07-31 10:49:59 -0300 | [diff] [blame] | 649 | :param algorithm: An instance of |
| 650 | :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`. |
| Jared | 6d7fe00 | 2016-05-29 17:32:37 -0700 | [diff] [blame] | 651 | |
| 652 | :param mode: The desired mode of the PRF. A value from the |
| 653 | :class:`~cryptography.hazmat.primitives.kdf.kbkdf.Mode` enum. |
| 654 | |
| 655 | :param int length: The desired length of the derived key in bytes. |
| 656 | |
| 657 | :param int rlen: An integer that indicates the length of the binary |
| 658 | representation of the counter in bytes. |
| 659 | |
| 660 | :param int llen: An integer that indicates the binary |
| 661 | representation of the ``length`` in bytes. |
| 662 | |
| 663 | :param location: The desired location of the counter. A value from the |
| 664 | :class:`~cryptography.hazmat.primitives.kdf.kbkdf.CounterLocation` enum. |
| 665 | |
| 666 | :param bytes label: Application specific label information. If ``None`` |
| 667 | is explicitly passed an empty byte string will be used. |
| 668 | |
| 669 | :param bytes context: Application specific context information. If ``None`` |
| 670 | is explicitly passed an empty byte string will be used. |
| 671 | |
| 672 | :param bytes fixed: Instead of specifying ``label`` and ``context`` you |
| 673 | may supply your own fixed data. If ``fixed`` is specified, ``label`` |
| 674 | and ``context`` is ignored. |
| 675 | |
| 676 | :param backend: A cryptography backend |
| 677 | :class:`~cryptography.hazmat.backends.interfaces.HashBackend` |
| Gabriel Orisaka | 617fe4b | 2016-07-31 10:49:59 -0300 | [diff] [blame] | 678 | instance. |
| Jared | 6d7fe00 | 2016-05-29 17:32:37 -0700 | [diff] [blame] | 679 | |
| 680 | :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised |
| 681 | if the provided ``backend`` does not implement |
| 682 | :class:`~cryptography.hazmat.backends.interfaces.HashBackend` |
| 683 | |
| 684 | :raises TypeError: This exception is raised if ``label`` or ``context`` |
| 685 | is not ``bytes``. Also raised if ``rlen`` or ``llen`` is not ``int``. |
| 686 | |
| 687 | :raises ValueError: This exception is raised if ``rlen`` or ``llen`` |
| 688 | is greater than 4 or less than 1. This exception is also raised if |
| 689 | you specify a ``label`` or ``context`` and ``fixed``. |
| 690 | |
| 691 | .. method:: derive(key_material) |
| 692 | |
| 693 | :param bytes key_material: The input key material. |
| 694 | :return bytes: The derived key. |
| 695 | :raises TypeError: This exception is raised if ``key_material`` is |
| 696 | not ``bytes``. |
| 697 | |
| 698 | Derives a new key from the input key material. |
| 699 | |
| 700 | .. method:: verify(key_material, expected_key) |
| 701 | |
| 702 | :param bytes key_material: The input key material. This is the same as |
| 703 | ``key_material`` in :meth:`derive`. |
| 704 | :param bytes expected_key: The expected result of deriving a new key, |
| 705 | this is the same as the return value of |
| 706 | :meth:`derive`. |
| 707 | :raises cryptography.exceptions.InvalidKey: This is raised when the |
| 708 | derived key does not match |
| 709 | the expected key. |
| 710 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 711 | :meth:`derive` or |
| 712 | :meth:`verify` is |
| 713 | called more than |
| 714 | once. |
| 715 | |
| 716 | This checks whether deriving a new key from the supplied |
| 717 | ``key_material`` generates the same key as the ``expected_key``, and |
| 718 | raises an exception if they do not match. |
| 719 | |
| 720 | .. class:: Mode |
| 721 | |
| 722 | An enumeration for the key based key derivative modes. |
| 723 | |
| 724 | .. attribute:: CounterMode |
| 725 | |
| 726 | The output of the PRF is computed with a counter |
| 727 | as the iteration variable. |
| 728 | |
| 729 | .. class:: CounterLocation |
| 730 | |
| 731 | An enumeration for the key based key derivative counter location. |
| 732 | |
| 733 | .. attribute:: BeforeFixed |
| 734 | |
| 735 | The counter iteration variable will be concatenated before |
| 736 | the fixed input data. |
| 737 | |
| 738 | .. attribute:: AfterFixed |
| 739 | |
| 740 | The counter iteration variable will be concatenated after |
| 741 | the fixed input data. |
| 742 | |
| Terry Chia | d8a27df | 2016-09-01 23:39:57 +0800 | [diff] [blame] | 743 | .. currentmodule:: cryptography.hazmat.primitives.kdf.scrypt |
| 744 | |
| 745 | .. class:: Scrypt(salt, length, n, r, p, backend) |
| 746 | |
| 747 | .. versionadded:: 1.6 |
| 748 | |
| 749 | Scrypt is a KDF designed for password storage by Colin Percival to be |
| 750 | resistant against hardware-assisted attackers by having a tunable memory |
| 751 | cost. It is described in :rfc:`7914`. |
| 752 | |
| 753 | This class conforms to the |
| 754 | :class:`~cryptography.hazmat.primitives.kdf.KeyDerivationFunction` |
| 755 | interface. |
| 756 | |
| Paul Kehrer | f12955c | 2017-06-07 02:20:33 -1000 | [diff] [blame] | 757 | .. doctest:: |
| Terry Chia | d8a27df | 2016-09-01 23:39:57 +0800 | [diff] [blame] | 758 | |
| 759 | >>> import os |
| Terry Chia | d8a27df | 2016-09-01 23:39:57 +0800 | [diff] [blame] | 760 | >>> from cryptography.hazmat.primitives.kdf.scrypt import Scrypt |
| 761 | >>> from cryptography.hazmat.backends import default_backend |
| 762 | >>> backend = default_backend() |
| 763 | >>> salt = os.urandom(16) |
| 764 | >>> # derive |
| 765 | >>> kdf = Scrypt( |
| 766 | ... salt=salt, |
| Paul Kehrer | a857fe6 | 2017-06-28 23:03:29 -0500 | [diff] [blame] | 767 | ... length=32, |
| Nick Badger | acaf89d | 2016-12-10 17:41:50 -0800 | [diff] [blame] | 768 | ... n=2**14, |
| Terry Chia | d8a27df | 2016-09-01 23:39:57 +0800 | [diff] [blame] | 769 | ... r=8, |
| Nick Badger | acaf89d | 2016-12-10 17:41:50 -0800 | [diff] [blame] | 770 | ... p=1, |
| Terry Chia | d8a27df | 2016-09-01 23:39:57 +0800 | [diff] [blame] | 771 | ... backend=backend |
| 772 | ... ) |
| 773 | >>> key = kdf.derive(b"my great password") |
| 774 | >>> # verify |
| 775 | >>> kdf = Scrypt( |
| 776 | ... salt=salt, |
| Paul Kehrer | a857fe6 | 2017-06-28 23:03:29 -0500 | [diff] [blame] | 777 | ... length=32, |
| Nick Badger | acaf89d | 2016-12-10 17:41:50 -0800 | [diff] [blame] | 778 | ... n=2**14, |
| Terry Chia | d8a27df | 2016-09-01 23:39:57 +0800 | [diff] [blame] | 779 | ... r=8, |
| Nick Badger | acaf89d | 2016-12-10 17:41:50 -0800 | [diff] [blame] | 780 | ... p=1, |
| Terry Chia | d8a27df | 2016-09-01 23:39:57 +0800 | [diff] [blame] | 781 | ... backend=backend |
| 782 | ... ) |
| 783 | >>> kdf.verify(b"my great password", key) |
| 784 | |
| 785 | :param bytes salt: A salt. |
| Paul Kehrer | a857fe6 | 2017-06-28 23:03:29 -0500 | [diff] [blame] | 786 | :param int length: The desired length of the derived key in bytes. |
| Terry Chia | d8a27df | 2016-09-01 23:39:57 +0800 | [diff] [blame] | 787 | :param int n: CPU/Memory cost parameter. It must be larger than 1 and be a |
| 788 | power of 2. |
| 789 | :param int r: Block size parameter. |
| 790 | :param int p: Parallelization parameter. |
| 791 | |
| 792 | The computational and memory cost of Scrypt can be adjusted by manipulating |
| Nick Badger | acaf89d | 2016-12-10 17:41:50 -0800 | [diff] [blame] | 793 | the 3 parameters: ``n``, ``r``, and ``p``. In general, the memory cost of |
| 794 | Scrypt is affected by the values of both ``n`` and ``r``, while ``n`` also |
| 795 | determines the number of iterations performed. ``p`` increases the |
| 796 | computational cost without affecting memory usage. A more in-depth |
| 797 | explanation of the 3 parameters can be found `here`_. |
| Terry Chia | d8a27df | 2016-09-01 23:39:57 +0800 | [diff] [blame] | 798 | |
| Nick Badger | acaf89d | 2016-12-10 17:41:50 -0800 | [diff] [blame] | 799 | :rfc:`7914` `recommends`_ values of ``r=8`` and ``p=1`` while scaling ``n`` |
| 800 | to a number appropriate for your system. `The scrypt paper`_ suggests a |
| 801 | minimum value of ``n=2**14`` for interactive logins (t < 100ms), or |
| 802 | ``n=2**20`` for more sensitive files (t < 5s). |
| Terry Chia | d8a27df | 2016-09-01 23:39:57 +0800 | [diff] [blame] | 803 | |
| 804 | :param backend: An instance of |
| 805 | :class:`~cryptography.hazmat.backends.interfaces.ScryptBackend`. |
| 806 | |
| 807 | :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised if the |
| 808 | provided ``backend`` does not implement |
| 809 | :class:`~cryptography.hazmat.backends.interfaces.ScryptBackend` |
| 810 | |
| 811 | :raises TypeError: This exception is raised if ``salt`` is not ``bytes``. |
| Terry Chia | a2d0da9 | 2016-09-03 07:57:45 +0800 | [diff] [blame] | 812 | :raises ValueError: This exception is raised if ``n`` is less than 2, if |
| 813 | ``n`` is not a power of 2, if ``r`` is less than 1 or if ``p`` is less |
| 814 | than 1. |
| Terry Chia | d8a27df | 2016-09-01 23:39:57 +0800 | [diff] [blame] | 815 | |
| 816 | .. method:: derive(key_material) |
| 817 | |
| Paul Kehrer | a779944 | 2019-01-17 15:56:23 -0600 | [diff] [blame^] | 818 | :param key_material: The input key material. |
| 819 | :type key_material: :term:`bytes-like` |
| Terry Chia | d8a27df | 2016-09-01 23:39:57 +0800 | [diff] [blame] | 820 | :return bytes: the derived key. |
| 821 | :raises TypeError: This exception is raised if ``key_material`` is not |
| 822 | ``bytes``. |
| 823 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 824 | :meth:`derive` or |
| 825 | :meth:`verify` is |
| 826 | called more than |
| 827 | once. |
| 828 | |
| 829 | This generates and returns a new key from the supplied password. |
| 830 | |
| 831 | .. method:: verify(key_material, expected_key) |
| 832 | |
| 833 | :param bytes key_material: The input key material. This is the same as |
| 834 | ``key_material`` in :meth:`derive`. |
| 835 | :param bytes expected_key: The expected result of deriving a new key, |
| 836 | this is the same as the return value of |
| 837 | :meth:`derive`. |
| 838 | :raises cryptography.exceptions.InvalidKey: This is raised when the |
| 839 | derived key does not match |
| 840 | the expected key. |
| 841 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 842 | :meth:`derive` or |
| 843 | :meth:`verify` is |
| 844 | called more than |
| 845 | once. |
| 846 | |
| 847 | This checks whether deriving a new key from the supplied |
| 848 | ``key_material`` generates the same key as the ``expected_key``, and |
| 849 | raises an exception if they do not match. This can be used for |
| 850 | checking whether the password a user provides matches the stored derived |
| 851 | key. |
| 852 | |
| Paul Kehrer | 48402ff | 2015-02-16 15:31:52 -0600 | [diff] [blame] | 853 | Interface |
| 854 | ~~~~~~~~~ |
| 855 | |
| 856 | .. currentmodule:: cryptography.hazmat.primitives.kdf |
| 857 | |
| 858 | .. class:: KeyDerivationFunction |
| 859 | |
| 860 | .. versionadded:: 0.2 |
| 861 | |
| 862 | .. method:: derive(key_material) |
| 863 | |
| 864 | :param bytes key_material: The input key material. Depending on what |
| 865 | key derivation function you are using this |
| 866 | could be either random bytes, or a user |
| 867 | supplied password. |
| 868 | :return: The new key. |
| 869 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 870 | :meth:`derive` or |
| 871 | :meth:`verify` is |
| 872 | called more than |
| 873 | once. |
| 874 | |
| 875 | This generates and returns a new key from the supplied key material. |
| 876 | |
| 877 | .. method:: verify(key_material, expected_key) |
| 878 | |
| 879 | :param bytes key_material: The input key material. This is the same as |
| 880 | ``key_material`` in :meth:`derive`. |
| 881 | :param bytes expected_key: The expected result of deriving a new key, |
| 882 | this is the same as the return value of |
| 883 | :meth:`derive`. |
| 884 | :raises cryptography.exceptions.InvalidKey: This is raised when the |
| 885 | derived key does not match |
| 886 | the expected key. |
| 887 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 888 | :meth:`derive` or |
| 889 | :meth:`verify` is |
| 890 | called more than |
| 891 | once. |
| 892 | |
| 893 | This checks whether deriving a new key from the supplied |
| 894 | ``key_material`` generates the same key as the ``expected_key``, and |
| 895 | raises an exception if they do not match. This can be used for |
| 896 | something like checking whether a user's password attempt matches the |
| 897 | stored derived key. |
| 898 | |
| 899 | |
| Cory Benfield | 9b22eb9 | 2017-05-22 22:40:40 -0700 | [diff] [blame] | 900 | .. [#nist] See `NIST SP 800-132`_. |
| 901 | |
| Alex Gaynor | 53e4505 | 2017-09-20 09:57:47 -0400 | [diff] [blame] | 902 | .. _`NIST SP 800-132`: https://csrc.nist.gov/publications/detail/sp/800-132/final |
| 903 | .. _`NIST SP 800-108`: https://csrc.nist.gov/publications/detail/sp/800-108/final |
| 904 | .. _`NIST SP 800-56Ar2`: https://csrc.nist.gov/publications/detail/sp/800-56a/rev-2/final |
| Simo Sorce | 53c8f6a | 2015-09-15 11:13:56 -0400 | [diff] [blame] | 905 | .. _`ANSI X9.63:2001`: https://webstore.ansi.org |
| 906 | .. _`SEC 1 v2.0`: http://www.secg.org/sec1-v2.pdf |
| Paul Kehrer | b3f763f | 2014-01-28 16:42:15 -0600 | [diff] [blame] | 907 | .. _`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] | 908 | .. _`PBKDF2`: https://en.wikipedia.org/wiki/PBKDF2 |
| David Reid | b80deea | 2014-02-03 10:33:16 -0800 | [diff] [blame] | 909 | .. _`key stretching`: https://en.wikipedia.org/wiki/Key_stretching |
| Alex Gaynor | 2e8725d | 2016-08-29 21:40:19 -0400 | [diff] [blame] | 910 | .. _`HKDF`: https://en.wikipedia.org/wiki/HKDF |
| David Reid | b80deea | 2014-02-03 10:33:16 -0800 | [diff] [blame] | 911 | .. _`HKDF paper`: https://eprint.iacr.org/2010/264 |
| Terry Chia | d8a27df | 2016-09-01 23:39:57 +0800 | [diff] [blame] | 912 | .. _`here`: https://stackoverflow.com/a/30308723/1170681 |
| 913 | .. _`recommends`: https://tools.ietf.org/html/rfc7914#section-2 |
| Nick Badger | acaf89d | 2016-12-10 17:41:50 -0800 | [diff] [blame] | 914 | .. _`The scrypt paper`: https://www.tarsnap.com/scrypt/scrypt.pdf |