David Reid | 30722b9 | 2013-11-07 13:03:39 -0800 | [diff] [blame] | 1 | .. hazmat:: |
| 2 | |
Paul Kehrer | 45efdbc | 2015-02-12 10:58:22 -0600 | [diff] [blame^] | 3 | .. module:: cryptography.hazmat.primitives.interfaces |
| 4 | |
David Reid | 30722b9 | 2013-11-07 13:03:39 -0800 | [diff] [blame] | 5 | Interfaces |
| 6 | ========== |
| 7 | |
| 8 | |
| 9 | ``cryptography`` uses `Abstract Base Classes`_ as interfaces to describe the |
David Reid | bd18bcd | 2013-11-07 13:13:30 -0800 | [diff] [blame] | 10 | properties and methods of most primitive constructs. Backends may also use |
| 11 | this information to influence their operation. Interfaces should also be used |
David Reid | 30722b9 | 2013-11-07 13:03:39 -0800 | [diff] [blame] | 12 | to document argument and return types. |
| 13 | |
Alex Gaynor | e9df294 | 2014-12-12 10:56:26 -0800 | [diff] [blame] | 14 | .. _`Abstract Base Classes`: https://docs.python.org/3/library/abc.html |
David Reid | 30722b9 | 2013-11-07 13:03:39 -0800 | [diff] [blame] | 15 | |
| 16 | |
Alex Stapleton | c5fffd3 | 2014-03-18 15:29:00 +0000 | [diff] [blame] | 17 | Symmetric ciphers |
Alex Gaynor | 645315b | 2014-06-23 11:55:55 -0700 | [diff] [blame] | 18 | ----------------- |
David Reid | 30722b9 | 2013-11-07 13:03:39 -0800 | [diff] [blame] | 19 | |
David Reid | 0a394df | 2013-11-15 16:19:50 -0800 | [diff] [blame] | 20 | .. class:: CipherAlgorithm |
| 21 | |
| 22 | A named symmetric encryption algorithm. |
| 23 | |
| 24 | .. attribute:: name |
| 25 | |
| 26 | :type: str |
| 27 | |
| 28 | The standard name for the mode, for example, "AES", "Camellia", or |
| 29 | "Blowfish". |
| 30 | |
| 31 | .. attribute:: key_size |
| 32 | |
| 33 | :type: int |
| 34 | |
| 35 | The number of bits in the key being used. |
| 36 | |
| 37 | |
David Reid | 668d480 | 2013-12-17 11:53:43 -0800 | [diff] [blame] | 38 | .. class:: BlockCipherAlgorithm |
| 39 | |
| 40 | A block cipher algorithm. |
| 41 | |
| 42 | .. attribute:: block_size |
| 43 | |
| 44 | :type: int |
| 45 | |
| 46 | The number of bits in a block. |
| 47 | |
| 48 | |
Alex Stapleton | c5fffd3 | 2014-03-18 15:29:00 +0000 | [diff] [blame] | 49 | Cipher modes |
Alex Gaynor | 645315b | 2014-06-23 11:55:55 -0700 | [diff] [blame] | 50 | ~~~~~~~~~~~~ |
David Reid | 0a394df | 2013-11-15 16:19:50 -0800 | [diff] [blame] | 51 | |
David Reid | 30722b9 | 2013-11-07 13:03:39 -0800 | [diff] [blame] | 52 | Interfaces used by the symmetric cipher modes described in |
| 53 | :ref:`Symmetric Encryption Modes <symmetric-encryption-modes>`. |
| 54 | |
| 55 | .. class:: Mode |
| 56 | |
| 57 | A named cipher mode. |
| 58 | |
| 59 | .. attribute:: name |
| 60 | |
| 61 | :type: str |
| 62 | |
| 63 | This should be the standard shorthand name for the mode, for example |
| 64 | Cipher-Block Chaining mode is "CBC". |
| 65 | |
| 66 | The name may be used by a backend to influence the operation of a |
| 67 | cipher in conjunction with the algorithm's name. |
| 68 | |
Alex Gaynor | 9626b5a | 2013-11-19 16:49:26 -0800 | [diff] [blame] | 69 | .. method:: validate_for_algorithm(algorithm) |
| 70 | |
| 71 | :param CipherAlgorithm algorithm: |
| 72 | |
| 73 | Checks that the combination of this mode with the provided algorithm |
| 74 | meets any necessary invariants. This should raise an exception if they |
| 75 | are not met. |
| 76 | |
Paul Kehrer | 45efdbc | 2015-02-12 10:58:22 -0600 | [diff] [blame^] | 77 | For example, the |
| 78 | :class:`~cryptography.hazmat.primitives.ciphers.modes.CBC` mode uses |
| 79 | this method to check that the provided initialization vector's length |
| 80 | matches the block size of the algorithm. |
Alex Gaynor | 9626b5a | 2013-11-19 16:49:26 -0800 | [diff] [blame] | 81 | |
David Reid | 30722b9 | 2013-11-07 13:03:39 -0800 | [diff] [blame] | 82 | |
| 83 | .. class:: ModeWithInitializationVector |
| 84 | |
| 85 | A cipher mode with an initialization vector. |
| 86 | |
| 87 | .. attribute:: initialization_vector |
| 88 | |
| 89 | :type: bytes |
| 90 | |
| 91 | Exact requirements of the initialization are described by the |
| 92 | documentation of individual modes. |
| 93 | |
| 94 | |
| 95 | .. class:: ModeWithNonce |
| 96 | |
| 97 | A cipher mode with a nonce. |
| 98 | |
| 99 | .. attribute:: nonce |
| 100 | |
| 101 | :type: bytes |
| 102 | |
| 103 | Exact requirements of the nonce are described by the documentation of |
| 104 | individual modes. |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 105 | |
Alex Stapleton | c5fffd3 | 2014-03-18 15:29:00 +0000 | [diff] [blame] | 106 | Asymmetric interfaces |
Alex Gaynor | 645315b | 2014-06-23 11:55:55 -0700 | [diff] [blame] | 107 | --------------------- |
| 108 | |
| 109 | .. class:: AsymmetricSignatureContext |
| 110 | |
| 111 | .. versionadded:: 0.2 |
| 112 | |
| 113 | .. method:: update(data) |
| 114 | |
| 115 | :param bytes data: The data you want to sign. |
| 116 | |
| 117 | .. method:: finalize() |
| 118 | |
| 119 | :return bytes signature: The signature. |
| 120 | |
| 121 | |
| 122 | .. class:: AsymmetricVerificationContext |
| 123 | |
| 124 | .. versionadded:: 0.2 |
| 125 | |
| 126 | .. method:: update(data) |
| 127 | |
| 128 | :param bytes data: The data you wish to verify using the signature. |
| 129 | |
| 130 | .. method:: verify() |
| 131 | |
| 132 | :raises cryptography.exceptions.InvalidSignature: If the signature does |
| 133 | not validate. |
| 134 | |
| 135 | |
| 136 | .. class:: AsymmetricPadding |
| 137 | |
| 138 | .. versionadded:: 0.2 |
| 139 | |
| 140 | .. attribute:: name |
| 141 | |
Paul Kehrer | d2fa7d2 | 2015-02-12 00:15:02 -0600 | [diff] [blame] | 142 | DSA |
| 143 | ~~~ |
| 144 | |
| 145 | In 0.8 the DSA key interfaces were moved to the |
| 146 | :mod:`cryptography.hazmat.primitives.asymmetric.dsa` module. |
| 147 | |
Alex Gaynor | 645315b | 2014-06-23 11:55:55 -0700 | [diff] [blame] | 148 | |
| 149 | RSA |
| 150 | ~~~ |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 151 | |
Alex Stapleton | f79c231 | 2014-12-30 12:50:14 +0000 | [diff] [blame] | 152 | In 0.8 the RSA key interfaces were moved to the |
| 153 | :mod:`cryptography.hazmat.primitives.asymmetric.rsa` module. |
Paul Kehrer | f0a48c6 | 2014-06-07 17:04:13 -0500 | [diff] [blame] | 154 | |
Alex Stapleton | 085f378 | 2014-04-01 16:18:17 +0100 | [diff] [blame] | 155 | |
Alex Gaynor | 645315b | 2014-06-23 11:55:55 -0700 | [diff] [blame] | 156 | Elliptic Curve |
| 157 | ~~~~~~~~~~~~~~ |
| 158 | |
Paul Kehrer | 3bc87ab | 2015-02-12 00:01:53 -0600 | [diff] [blame] | 159 | In 0.8 the EC key interfaces were moved to the |
| 160 | :mod:`cryptography.hazmat.primitives.asymmetric.ec` module. |
Paul Kehrer | e025be2 | 2014-09-24 11:26:48 -0500 | [diff] [blame] | 161 | |
| 162 | |
Alex Stapleton | c5fffd3 | 2014-03-18 15:29:00 +0000 | [diff] [blame] | 163 | Hash algorithms |
Alex Gaynor | 645315b | 2014-06-23 11:55:55 -0700 | [diff] [blame] | 164 | --------------- |
Paul Kehrer | e51a2db | 2014-01-29 11:49:35 -0600 | [diff] [blame] | 165 | |
| 166 | .. class:: HashAlgorithm |
| 167 | |
Paul Kehrer | e51a2db | 2014-01-29 11:49:35 -0600 | [diff] [blame] | 168 | .. attribute:: name |
| 169 | |
| 170 | :type: str |
| 171 | |
Paul Kehrer | 4c75a8c | 2014-01-29 12:20:37 -0600 | [diff] [blame] | 172 | The standard name for the hash algorithm, for example: ``"sha256"`` or |
| 173 | ``"whirlpool"``. |
Paul Kehrer | e51a2db | 2014-01-29 11:49:35 -0600 | [diff] [blame] | 174 | |
| 175 | .. attribute:: digest_size |
| 176 | |
| 177 | :type: int |
| 178 | |
| 179 | The size of the resulting digest in bytes. |
| 180 | |
| 181 | .. attribute:: block_size |
| 182 | |
| 183 | :type: int |
| 184 | |
| 185 | The internal block size of the hash algorithm in bytes. |
| 186 | |
| 187 | |
Ayrx | a0f9850 | 2014-04-15 19:17:03 +0800 | [diff] [blame] | 188 | .. class:: HashContext |
| 189 | |
| 190 | .. attribute:: algorithm |
| 191 | |
| 192 | A :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` that |
| 193 | will be used by this context. |
| 194 | |
| 195 | .. method:: update(data) |
| 196 | |
Alex Gaynor | e85e356 | 2014-11-22 23:26:30 -0800 | [diff] [blame] | 197 | :param bytes data: The data you want to hash. |
Ayrx | a0f9850 | 2014-04-15 19:17:03 +0800 | [diff] [blame] | 198 | |
| 199 | .. method:: finalize() |
| 200 | |
| 201 | :return: The final digest as bytes. |
| 202 | |
| 203 | .. method:: copy() |
| 204 | |
| 205 | :return: A :class:`~cryptography.hazmat.primitives.interfaces.HashContext` |
| 206 | that is a copy of the current context. |
| 207 | |
| 208 | |
Alex Stapleton | c5fffd3 | 2014-03-18 15:29:00 +0000 | [diff] [blame] | 209 | Key derivation functions |
Alex Gaynor | 645315b | 2014-06-23 11:55:55 -0700 | [diff] [blame] | 210 | ------------------------ |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 211 | |
| 212 | .. class:: KeyDerivationFunction |
| 213 | |
Alex Gaynor | 8454c51 | 2014-01-28 07:01:54 -0800 | [diff] [blame] | 214 | .. versionadded:: 0.2 |
| 215 | |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 216 | .. method:: derive(key_material) |
| 217 | |
Alex Gaynor | e85e356 | 2014-11-22 23:26:30 -0800 | [diff] [blame] | 218 | :param bytes key_material: The input key material. Depending on what |
Alex Gaynor | 5484f72 | 2014-01-28 05:46:15 -0800 | [diff] [blame] | 219 | key derivation function you are using this |
Alex Gaynor | e85e356 | 2014-11-22 23:26:30 -0800 | [diff] [blame] | 220 | could be either random bytes, or a user |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 221 | supplied password. |
Alex Gaynor | 5484f72 | 2014-01-28 05:46:15 -0800 | [diff] [blame] | 222 | :return: The new key. |
Alex Gaynor | e19e89f | 2014-01-28 06:58:43 -0800 | [diff] [blame] | 223 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 224 | :meth:`derive` or |
| 225 | :meth:`verify` is |
| 226 | called more than |
| 227 | once. |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 228 | |
Alex Gaynor | 5484f72 | 2014-01-28 05:46:15 -0800 | [diff] [blame] | 229 | This generates and returns a new key from the supplied key material. |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 230 | |
| 231 | .. method:: verify(key_material, expected_key) |
| 232 | |
Alex Gaynor | e85e356 | 2014-11-22 23:26:30 -0800 | [diff] [blame] | 233 | :param bytes key_material: The input key material. This is the same as |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 234 | ``key_material`` in :meth:`derive`. |
Alex Gaynor | e85e356 | 2014-11-22 23:26:30 -0800 | [diff] [blame] | 235 | :param bytes expected_key: The expected result of deriving a new key, |
Alex Gaynor | 5484f72 | 2014-01-28 05:46:15 -0800 | [diff] [blame] | 236 | this is the same as the return value of |
| 237 | :meth:`derive`. |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 238 | :raises cryptography.exceptions.InvalidKey: This is raised when the |
| 239 | derived key does not match |
| 240 | the expected key. |
Alex Gaynor | e19e89f | 2014-01-28 06:58:43 -0800 | [diff] [blame] | 241 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 242 | :meth:`derive` or |
| 243 | :meth:`verify` is |
| 244 | called more than |
| 245 | once. |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 246 | |
Alex Gaynor | 5484f72 | 2014-01-28 05:46:15 -0800 | [diff] [blame] | 247 | This checks whether deriving a new key from the supplied |
| 248 | ``key_material`` generates the same key as the ``expected_key``, and |
| 249 | raises an exception if they do not match. This can be used for |
| 250 | something like checking whether a user's password attempt matches the |
| 251 | stored derived key. |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 252 | |
Ayrx | c812170 | 2014-04-15 19:02:05 +0800 | [diff] [blame] | 253 | |
Terry Chia | cc5e445 | 2014-10-12 15:35:21 +0800 | [diff] [blame] | 254 | `Message Authentication Code`_ |
| 255 | ------------------------------ |
Ayrx | c812170 | 2014-04-15 19:02:05 +0800 | [diff] [blame] | 256 | |
| 257 | .. class:: CMACContext |
| 258 | |
Alex Gaynor | 7d15688 | 2014-10-20 10:40:34 -0700 | [diff] [blame] | 259 | :class:`CMACContext` has been deprecated in favor of :class:`MACContext`. |
Terry Chia | c7c82f3 | 2014-10-20 12:15:22 +0800 | [diff] [blame] | 260 | |
Ayrx | c812170 | 2014-04-15 19:02:05 +0800 | [diff] [blame] | 261 | .. versionadded:: 0.4 |
| 262 | |
| 263 | .. method:: update(data) |
| 264 | |
Alex Gaynor | e85e356 | 2014-11-22 23:26:30 -0800 | [diff] [blame] | 265 | :param bytes data: The data you want to authenticate. |
Ayrx | c812170 | 2014-04-15 19:02:05 +0800 | [diff] [blame] | 266 | |
| 267 | .. method:: finalize() |
| 268 | |
Ayrx | 7964c17 | 2014-04-15 21:50:58 +0800 | [diff] [blame] | 269 | :return: The message authentication code. |
Ayrx | c812170 | 2014-04-15 19:02:05 +0800 | [diff] [blame] | 270 | |
| 271 | .. method:: copy() |
| 272 | |
| 273 | :return: A :class:`~cryptography.hazmat.primitives.interfaces.CMACContext` |
| 274 | that is a copy of the current context. |
| 275 | |
Terry Chia | cc5e445 | 2014-10-12 15:35:21 +0800 | [diff] [blame] | 276 | .. class:: MACContext |
| 277 | |
| 278 | .. versionadded:: 0.7 |
| 279 | |
| 280 | .. method:: update(data) |
| 281 | |
Alex Gaynor | e85e356 | 2014-11-22 23:26:30 -0800 | [diff] [blame] | 282 | :param bytes data: The data you want to authenticate. |
Terry Chia | cc5e445 | 2014-10-12 15:35:21 +0800 | [diff] [blame] | 283 | |
| 284 | .. method:: finalize() |
| 285 | |
| 286 | :return: The message authentication code. |
| 287 | |
| 288 | .. method:: copy() |
| 289 | |
Alex Gaynor | 7d15688 | 2014-10-20 10:40:34 -0700 | [diff] [blame] | 290 | :return: A |
| 291 | :class:`~cryptography.hazmat.primitives.interfaces.MACContext` that |
| 292 | is a copy of the current context. |
Terry Chia | cc5e445 | 2014-10-12 15:35:21 +0800 | [diff] [blame] | 293 | |
Alex Gaynor | 7d15688 | 2014-10-20 10:40:34 -0700 | [diff] [blame] | 294 | .. method:: verify(signature) |
Terry Chia | cc5e445 | 2014-10-12 15:35:21 +0800 | [diff] [blame] | 295 | |
Alex Gaynor | e85e356 | 2014-11-22 23:26:30 -0800 | [diff] [blame] | 296 | :param bytes signature: The signature to verify. |
Terry Chia | cc5e445 | 2014-10-12 15:35:21 +0800 | [diff] [blame] | 297 | |
| 298 | :raises cryptography.exceptions.InvalidSignature: This is raised when |
| 299 | the provided signature does not match the expected signature. |
Ayrx | c812170 | 2014-04-15 19:02:05 +0800 | [diff] [blame] | 300 | |
Paul Kehrer | 05c122b | 2014-11-24 08:41:05 -1000 | [diff] [blame] | 301 | |
Ayrx | 83cd3f8 | 2014-04-15 21:56:32 +0800 | [diff] [blame] | 302 | .. _`CMAC`: https://en.wikipedia.org/wiki/CMAC |