blob: 16e99ff79f701e73f9fd6adc3217a591144cfe21 [file] [log] [blame]
David Reid30722b92013-11-07 13:03:39 -08001.. hazmat::
2
3Interfaces
4==========
5
6
7``cryptography`` uses `Abstract Base Classes`_ as interfaces to describe the
David Reidbd18bcd2013-11-07 13:13:30 -08008properties and methods of most primitive constructs. Backends may also use
9this information to influence their operation. Interfaces should also be used
David Reid30722b92013-11-07 13:03:39 -080010to document argument and return types.
11
Alex Gaynore9df2942014-12-12 10:56:26 -080012.. _`Abstract Base Classes`: https://docs.python.org/3/library/abc.html
David Reid30722b92013-11-07 13:03:39 -080013
14
Alex Stapletonc5fffd32014-03-18 15:29:00 +000015Symmetric ciphers
Alex Gaynor645315b2014-06-23 11:55:55 -070016-----------------
David Reid30722b92013-11-07 13:03:39 -080017
18.. currentmodule:: cryptography.hazmat.primitives.interfaces
19
David Reid0a394df2013-11-15 16:19:50 -080020
21.. class:: CipherAlgorithm
22
23 A named symmetric encryption algorithm.
24
25 .. attribute:: name
26
27 :type: str
28
29 The standard name for the mode, for example, "AES", "Camellia", or
30 "Blowfish".
31
32 .. attribute:: key_size
33
34 :type: int
35
36 The number of bits in the key being used.
37
38
David Reid668d4802013-12-17 11:53:43 -080039.. class:: BlockCipherAlgorithm
40
41 A block cipher algorithm.
42
43 .. attribute:: block_size
44
45 :type: int
46
47 The number of bits in a block.
48
49
Alex Stapletonc5fffd32014-03-18 15:29:00 +000050Cipher modes
Alex Gaynor645315b2014-06-23 11:55:55 -070051~~~~~~~~~~~~
David Reid0a394df2013-11-15 16:19:50 -080052
David Reid30722b92013-11-07 13:03:39 -080053Interfaces used by the symmetric cipher modes described in
54:ref:`Symmetric Encryption Modes <symmetric-encryption-modes>`.
55
56.. class:: Mode
57
58 A named cipher mode.
59
60 .. attribute:: name
61
62 :type: str
63
64 This should be the standard shorthand name for the mode, for example
65 Cipher-Block Chaining mode is "CBC".
66
67 The name may be used by a backend to influence the operation of a
68 cipher in conjunction with the algorithm's name.
69
Alex Gaynor9626b5a2013-11-19 16:49:26 -080070 .. method:: validate_for_algorithm(algorithm)
71
72 :param CipherAlgorithm algorithm:
73
74 Checks that the combination of this mode with the provided algorithm
75 meets any necessary invariants. This should raise an exception if they
76 are not met.
77
78 For example, the :class:`~cryptography.hazmat.primitives.modes.CBC`
79 mode uses this method to check that the provided initialization
80 vector's length matches the block size of the algorithm.
81
David Reid30722b92013-11-07 13:03:39 -080082
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 Kehrerac423232014-01-25 14:13:09 -0600105
Alex Stapletonc5fffd32014-03-18 15:29:00 +0000106Asymmetric interfaces
Alex Gaynor645315b2014-06-23 11:55:55 -0700107---------------------
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
142
143RSA
144~~~
Paul Kehrerac423232014-01-25 14:13:09 -0600145
Alex Stapletonf79c2312014-12-30 12:50:14 +0000146In 0.8 the RSA key interfaces were moved to the
147:mod:`cryptography.hazmat.primitives.asymmetric.rsa` module.
Paul Kehrerf0a48c62014-06-07 17:04:13 -0500148
Alex Stapleton085f3782014-04-01 16:18:17 +0100149
Alex Gaynor645315b2014-06-23 11:55:55 -0700150Elliptic Curve
151~~~~~~~~~~~~~~
152
Paul Kehrer3bc87ab2015-02-12 00:01:53 -0600153In 0.8 the EC key interfaces were moved to the
154:mod:`cryptography.hazmat.primitives.asymmetric.ec` module.
Paul Kehrere025be22014-09-24 11:26:48 -0500155
156
Alex Stapletonc5fffd32014-03-18 15:29:00 +0000157Hash algorithms
Alex Gaynor645315b2014-06-23 11:55:55 -0700158---------------
Paul Kehrere51a2db2014-01-29 11:49:35 -0600159
160.. class:: HashAlgorithm
161
Paul Kehrere51a2db2014-01-29 11:49:35 -0600162 .. attribute:: name
163
164 :type: str
165
Paul Kehrer4c75a8c2014-01-29 12:20:37 -0600166 The standard name for the hash algorithm, for example: ``"sha256"`` or
167 ``"whirlpool"``.
Paul Kehrere51a2db2014-01-29 11:49:35 -0600168
169 .. attribute:: digest_size
170
171 :type: int
172
173 The size of the resulting digest in bytes.
174
175 .. attribute:: block_size
176
177 :type: int
178
179 The internal block size of the hash algorithm in bytes.
180
181
Ayrxa0f98502014-04-15 19:17:03 +0800182.. class:: HashContext
183
184 .. attribute:: algorithm
185
186 A :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` that
187 will be used by this context.
188
189 .. method:: update(data)
190
Alex Gaynore85e3562014-11-22 23:26:30 -0800191 :param bytes data: The data you want to hash.
Ayrxa0f98502014-04-15 19:17:03 +0800192
193 .. method:: finalize()
194
195 :return: The final digest as bytes.
196
197 .. method:: copy()
198
199 :return: A :class:`~cryptography.hazmat.primitives.interfaces.HashContext`
200 that is a copy of the current context.
201
202
Alex Stapletonc5fffd32014-03-18 15:29:00 +0000203Key derivation functions
Alex Gaynor645315b2014-06-23 11:55:55 -0700204------------------------
Alex Gaynorb2774f52014-01-27 11:05:29 -0800205
206.. class:: KeyDerivationFunction
207
Alex Gaynor8454c512014-01-28 07:01:54 -0800208 .. versionadded:: 0.2
209
Alex Gaynorb2774f52014-01-27 11:05:29 -0800210 .. method:: derive(key_material)
211
Alex Gaynore85e3562014-11-22 23:26:30 -0800212 :param bytes key_material: The input key material. Depending on what
Alex Gaynor5484f722014-01-28 05:46:15 -0800213 key derivation function you are using this
Alex Gaynore85e3562014-11-22 23:26:30 -0800214 could be either random bytes, or a user
Alex Gaynorb2774f52014-01-27 11:05:29 -0800215 supplied password.
Alex Gaynor5484f722014-01-28 05:46:15 -0800216 :return: The new key.
Alex Gaynore19e89f2014-01-28 06:58:43 -0800217 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
218 :meth:`derive` or
219 :meth:`verify` is
220 called more than
221 once.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800222
Alex Gaynor5484f722014-01-28 05:46:15 -0800223 This generates and returns a new key from the supplied key material.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800224
225 .. method:: verify(key_material, expected_key)
226
Alex Gaynore85e3562014-11-22 23:26:30 -0800227 :param bytes key_material: The input key material. This is the same as
Alex Gaynorb2774f52014-01-27 11:05:29 -0800228 ``key_material`` in :meth:`derive`.
Alex Gaynore85e3562014-11-22 23:26:30 -0800229 :param bytes expected_key: The expected result of deriving a new key,
Alex Gaynor5484f722014-01-28 05:46:15 -0800230 this is the same as the return value of
231 :meth:`derive`.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800232 :raises cryptography.exceptions.InvalidKey: This is raised when the
233 derived key does not match
234 the expected key.
Alex Gaynore19e89f2014-01-28 06:58:43 -0800235 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
236 :meth:`derive` or
237 :meth:`verify` is
238 called more than
239 once.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800240
Alex Gaynor5484f722014-01-28 05:46:15 -0800241 This checks whether deriving a new key from the supplied
242 ``key_material`` generates the same key as the ``expected_key``, and
243 raises an exception if they do not match. This can be used for
244 something like checking whether a user's password attempt matches the
245 stored derived key.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800246
Ayrxc8121702014-04-15 19:02:05 +0800247
Terry Chiacc5e4452014-10-12 15:35:21 +0800248`Message Authentication Code`_
249------------------------------
Ayrxc8121702014-04-15 19:02:05 +0800250
251.. class:: CMACContext
252
Alex Gaynor7d156882014-10-20 10:40:34 -0700253 :class:`CMACContext` has been deprecated in favor of :class:`MACContext`.
Terry Chiac7c82f32014-10-20 12:15:22 +0800254
Ayrxc8121702014-04-15 19:02:05 +0800255 .. versionadded:: 0.4
256
257 .. method:: update(data)
258
Alex Gaynore85e3562014-11-22 23:26:30 -0800259 :param bytes data: The data you want to authenticate.
Ayrxc8121702014-04-15 19:02:05 +0800260
261 .. method:: finalize()
262
Ayrx7964c172014-04-15 21:50:58 +0800263 :return: The message authentication code.
Ayrxc8121702014-04-15 19:02:05 +0800264
265 .. method:: copy()
266
267 :return: A :class:`~cryptography.hazmat.primitives.interfaces.CMACContext`
268 that is a copy of the current context.
269
Terry Chiacc5e4452014-10-12 15:35:21 +0800270.. class:: MACContext
271
272 .. versionadded:: 0.7
273
274 .. method:: update(data)
275
Alex Gaynore85e3562014-11-22 23:26:30 -0800276 :param bytes data: The data you want to authenticate.
Terry Chiacc5e4452014-10-12 15:35:21 +0800277
278 .. method:: finalize()
279
280 :return: The message authentication code.
281
282 .. method:: copy()
283
Alex Gaynor7d156882014-10-20 10:40:34 -0700284 :return: A
285 :class:`~cryptography.hazmat.primitives.interfaces.MACContext` that
286 is a copy of the current context.
Terry Chiacc5e4452014-10-12 15:35:21 +0800287
Alex Gaynor7d156882014-10-20 10:40:34 -0700288 .. method:: verify(signature)
Terry Chiacc5e4452014-10-12 15:35:21 +0800289
Alex Gaynore85e3562014-11-22 23:26:30 -0800290 :param bytes signature: The signature to verify.
Terry Chiacc5e4452014-10-12 15:35:21 +0800291
292 :raises cryptography.exceptions.InvalidSignature: This is raised when
293 the provided signature does not match the expected signature.
Ayrxc8121702014-04-15 19:02:05 +0800294
Paul Kehrer05c122b2014-11-24 08:41:05 -1000295
Ayrx83cd3f82014-04-15 21:56:32 +0800296.. _`CMAC`: https://en.wikipedia.org/wiki/CMAC