blob: 86a3a7e4be4d79eed2f4dc6ed30e06b0c2e79a77 [file] [log] [blame]
David Reid30722b92013-11-07 13:03:39 -08001.. hazmat::
2
Paul Kehrer45efdbc2015-02-12 10:58:22 -06003.. module:: cryptography.hazmat.primitives.interfaces
4
David Reid30722b92013-11-07 13:03:39 -08005Interfaces
6==========
7
8
9``cryptography`` uses `Abstract Base Classes`_ as interfaces to describe the
David Reidbd18bcd2013-11-07 13:13:30 -080010properties and methods of most primitive constructs. Backends may also use
11this information to influence their operation. Interfaces should also be used
David Reid30722b92013-11-07 13:03:39 -080012to document argument and return types.
13
Alex Gaynore9df2942014-12-12 10:56:26 -080014.. _`Abstract Base Classes`: https://docs.python.org/3/library/abc.html
David Reid30722b92013-11-07 13:03:39 -080015
16
Alex Stapletonc5fffd32014-03-18 15:29:00 +000017Symmetric ciphers
Alex Gaynor645315b2014-06-23 11:55:55 -070018-----------------
David Reid30722b92013-11-07 13:03:39 -080019
David Reid0a394df2013-11-15 16:19:50 -080020.. 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 Reid668d4802013-12-17 11:53:43 -080038.. 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 Stapletonc5fffd32014-03-18 15:29:00 +000049Cipher modes
Alex Gaynor645315b2014-06-23 11:55:55 -070050~~~~~~~~~~~~
David Reid0a394df2013-11-15 16:19:50 -080051
David Reid30722b92013-11-07 13:03:39 -080052Interfaces 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 Gaynor9626b5a2013-11-19 16:49:26 -080069 .. 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 Kehrer45efdbc2015-02-12 10:58:22 -060077 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 Gaynor9626b5a2013-11-19 16:49:26 -080081
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
Paul Kehrerd2fa7d22015-02-12 00:15:02 -0600142DSA
143~~~
144
145In 0.8 the DSA key interfaces were moved to the
146:mod:`cryptography.hazmat.primitives.asymmetric.dsa` module.
147
Alex Gaynor645315b2014-06-23 11:55:55 -0700148
149RSA
150~~~
Paul Kehrerac423232014-01-25 14:13:09 -0600151
Alex Stapletonf79c2312014-12-30 12:50:14 +0000152In 0.8 the RSA key interfaces were moved to the
153:mod:`cryptography.hazmat.primitives.asymmetric.rsa` module.
Paul Kehrerf0a48c62014-06-07 17:04:13 -0500154
Alex Stapleton085f3782014-04-01 16:18:17 +0100155
Alex Gaynor645315b2014-06-23 11:55:55 -0700156Elliptic Curve
157~~~~~~~~~~~~~~
158
Paul Kehrer3bc87ab2015-02-12 00:01:53 -0600159In 0.8 the EC key interfaces were moved to the
160:mod:`cryptography.hazmat.primitives.asymmetric.ec` module.
Paul Kehrere025be22014-09-24 11:26:48 -0500161
162
Alex Stapletonc5fffd32014-03-18 15:29:00 +0000163Key derivation functions
Alex Gaynor645315b2014-06-23 11:55:55 -0700164------------------------
Alex Gaynorb2774f52014-01-27 11:05:29 -0800165
166.. class:: KeyDerivationFunction
167
Alex Gaynor8454c512014-01-28 07:01:54 -0800168 .. versionadded:: 0.2
169
Alex Gaynorb2774f52014-01-27 11:05:29 -0800170 .. method:: derive(key_material)
171
Alex Gaynore85e3562014-11-22 23:26:30 -0800172 :param bytes key_material: The input key material. Depending on what
Alex Gaynor5484f722014-01-28 05:46:15 -0800173 key derivation function you are using this
Alex Gaynore85e3562014-11-22 23:26:30 -0800174 could be either random bytes, or a user
Alex Gaynorb2774f52014-01-27 11:05:29 -0800175 supplied password.
Alex Gaynor5484f722014-01-28 05:46:15 -0800176 :return: The new key.
Alex Gaynore19e89f2014-01-28 06:58:43 -0800177 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
178 :meth:`derive` or
179 :meth:`verify` is
180 called more than
181 once.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800182
Alex Gaynor5484f722014-01-28 05:46:15 -0800183 This generates and returns a new key from the supplied key material.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800184
185 .. method:: verify(key_material, expected_key)
186
Alex Gaynore85e3562014-11-22 23:26:30 -0800187 :param bytes key_material: The input key material. This is the same as
Alex Gaynorb2774f52014-01-27 11:05:29 -0800188 ``key_material`` in :meth:`derive`.
Alex Gaynore85e3562014-11-22 23:26:30 -0800189 :param bytes expected_key: The expected result of deriving a new key,
Alex Gaynor5484f722014-01-28 05:46:15 -0800190 this is the same as the return value of
191 :meth:`derive`.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800192 :raises cryptography.exceptions.InvalidKey: This is raised when the
193 derived key does not match
194 the expected key.
Alex Gaynore19e89f2014-01-28 06:58:43 -0800195 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
196 :meth:`derive` or
197 :meth:`verify` is
198 called more than
199 once.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800200
Alex Gaynor5484f722014-01-28 05:46:15 -0800201 This checks whether deriving a new key from the supplied
202 ``key_material`` generates the same key as the ``expected_key``, and
203 raises an exception if they do not match. This can be used for
204 something like checking whether a user's password attempt matches the
205 stored derived key.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800206
Ayrxc8121702014-04-15 19:02:05 +0800207
Terry Chiacc5e4452014-10-12 15:35:21 +0800208`Message Authentication Code`_
209------------------------------
Ayrxc8121702014-04-15 19:02:05 +0800210
211.. class:: CMACContext
212
Alex Gaynor7d156882014-10-20 10:40:34 -0700213 :class:`CMACContext` has been deprecated in favor of :class:`MACContext`.
Terry Chiac7c82f32014-10-20 12:15:22 +0800214
Ayrxc8121702014-04-15 19:02:05 +0800215 .. versionadded:: 0.4
216
217 .. method:: update(data)
218
Alex Gaynore85e3562014-11-22 23:26:30 -0800219 :param bytes data: The data you want to authenticate.
Ayrxc8121702014-04-15 19:02:05 +0800220
221 .. method:: finalize()
222
Ayrx7964c172014-04-15 21:50:58 +0800223 :return: The message authentication code.
Ayrxc8121702014-04-15 19:02:05 +0800224
225 .. method:: copy()
226
227 :return: A :class:`~cryptography.hazmat.primitives.interfaces.CMACContext`
228 that is a copy of the current context.
229
Terry Chiacc5e4452014-10-12 15:35:21 +0800230.. class:: MACContext
231
232 .. versionadded:: 0.7
233
234 .. method:: update(data)
235
Alex Gaynore85e3562014-11-22 23:26:30 -0800236 :param bytes data: The data you want to authenticate.
Terry Chiacc5e4452014-10-12 15:35:21 +0800237
238 .. method:: finalize()
239
240 :return: The message authentication code.
241
242 .. method:: copy()
243
Alex Gaynor7d156882014-10-20 10:40:34 -0700244 :return: A
245 :class:`~cryptography.hazmat.primitives.interfaces.MACContext` that
246 is a copy of the current context.
Terry Chiacc5e4452014-10-12 15:35:21 +0800247
Alex Gaynor7d156882014-10-20 10:40:34 -0700248 .. method:: verify(signature)
Terry Chiacc5e4452014-10-12 15:35:21 +0800249
Alex Gaynore85e3562014-11-22 23:26:30 -0800250 :param bytes signature: The signature to verify.
Terry Chiacc5e4452014-10-12 15:35:21 +0800251
252 :raises cryptography.exceptions.InvalidSignature: This is raised when
253 the provided signature does not match the expected signature.
Ayrxc8121702014-04-15 19:02:05 +0800254
Paul Kehrer05c122b2014-11-24 08:41:05 -1000255
Ayrx83cd3f82014-04-15 21:56:32 +0800256.. _`CMAC`: https://en.wikipedia.org/wiki/CMAC