blob: 7fef1c1322da617b939970db7014b1c049f54c02 [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
David Reid9ed25e42013-11-07 13:15:27 -080012.. _`Abstract Base Classes`: http://docs.python.org/3.2/library/abc.html
David Reid30722b92013-11-07 13:03:39 -080013
14
David Reid0a394df2013-11-15 16:19:50 -080015Symmetric Ciphers
16~~~~~~~~~~~~~~~~~
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
David Reid0a394df2013-11-15 16:19:50 -080050Cipher Modes
51------------
52
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
106Asymmetric Interfaces
107~~~~~~~~~~~~~~~~~~~~~
108
109.. class:: RSAPrivateKey
110
Paul Kehrer46688b12014-01-26 13:23:13 -0600111 .. versionadded:: 0.2
Paul Kehrer82629f42014-01-26 12:25:02 -0600112
Paul Kehrerac423232014-01-25 14:13:09 -0600113 An `RSA`_ private key.
114
Paul Kehrer0e94fbe2014-01-26 11:47:21 -0600115 .. method:: public_key()
Paul Kehrerac423232014-01-25 14:13:09 -0600116
Paul Kehrer359b9462014-01-26 12:03:05 -0600117 :return: :class:`~cryptography.hazmat.primitives.interfaces.RSAPublicKey`
Paul Kehrerac423232014-01-25 14:13:09 -0600118
119 An RSA public key object corresponding to the values of the private key.
120
121 .. attribute:: modulus
122
Paul Kehrerd527b302014-01-26 11:41:38 -0600123 :type: int
Paul Kehrerac423232014-01-25 14:13:09 -0600124
Paul Kehrer0e94fbe2014-01-26 11:47:21 -0600125 The public modulus.
Paul Kehrerac423232014-01-25 14:13:09 -0600126
127 .. attribute:: public_exponent
128
129 :type: int
130
Paul Kehrer0e94fbe2014-01-26 11:47:21 -0600131 The public exponent.
Paul Kehrerac423232014-01-25 14:13:09 -0600132
Alex Stapletonee3e6bf2014-02-02 21:13:48 +0000133 .. attribute:: key_size
Paul Kehrerac423232014-01-25 14:13:09 -0600134
135 :type: int
136
137 The bit length of the modulus.
138
139 .. attribute:: p
140
Paul Kehrerd527b302014-01-26 11:41:38 -0600141 :type: int
Paul Kehrerac423232014-01-25 14:13:09 -0600142
Alex Gaynor2a918742014-01-26 16:53:44 -0600143 ``p``, one of the two primes composing the :attr:`modulus`.
Paul Kehrerac423232014-01-25 14:13:09 -0600144
145 .. attribute:: q
146
Paul Kehrerd527b302014-01-26 11:41:38 -0600147 :type: int
Paul Kehrerac423232014-01-25 14:13:09 -0600148
Alex Gaynor2a918742014-01-26 16:53:44 -0600149 ``q``, one of the two primes composing the :attr:`modulus`.
Paul Kehrerac423232014-01-25 14:13:09 -0600150
151 .. attribute:: d
152
Paul Kehrerd527b302014-01-26 11:41:38 -0600153 :type: int
Paul Kehrerac423232014-01-25 14:13:09 -0600154
Paul Kehrerd527b302014-01-26 11:41:38 -0600155 The private exponent.
Paul Kehrerac423232014-01-25 14:13:09 -0600156
157 .. attribute:: n
158
Paul Kehrerd527b302014-01-26 11:41:38 -0600159 :type: int
Paul Kehrerac423232014-01-25 14:13:09 -0600160
Alex Gaynor2a918742014-01-26 16:53:44 -0600161 The public modulus. Alias for :attr:`modulus`.
Paul Kehrerac423232014-01-25 14:13:09 -0600162
163 .. attribute:: e
164
165 :type: int
166
Alex Gaynor2a918742014-01-26 16:53:44 -0600167 The public exponent. Alias for :attr:`public_exponent`.
Paul Kehrerac423232014-01-25 14:13:09 -0600168
169
170.. class:: RSAPublicKey
171
Paul Kehrer46688b12014-01-26 13:23:13 -0600172 .. versionadded:: 0.2
Paul Kehrer82629f42014-01-26 12:25:02 -0600173
Paul Kehrerac423232014-01-25 14:13:09 -0600174 An `RSA`_ public key.
175
176 .. attribute:: modulus
177
Paul Kehrerd527b302014-01-26 11:41:38 -0600178 :type: int
Paul Kehrerac423232014-01-25 14:13:09 -0600179
Paul Kehrer0e94fbe2014-01-26 11:47:21 -0600180 The public modulus.
Paul Kehrerac423232014-01-25 14:13:09 -0600181
Alex Stapletonee3e6bf2014-02-02 21:13:48 +0000182 .. attribute:: key_size
Paul Kehrerac423232014-01-25 14:13:09 -0600183
184 :type: int
185
186 The bit length of the modulus.
187
188 .. attribute:: public_exponent
189
190 :type: int
191
Paul Kehrer0e94fbe2014-01-26 11:47:21 -0600192 The public exponent.
Paul Kehrerac423232014-01-25 14:13:09 -0600193
194 .. attribute:: n
195
Paul Kehrerd527b302014-01-26 11:41:38 -0600196 :type: int
Paul Kehrerac423232014-01-25 14:13:09 -0600197
Alex Gaynor2a918742014-01-26 16:53:44 -0600198 The public modulus. Alias for :attr:`modulus`.
Paul Kehrerac423232014-01-25 14:13:09 -0600199
200 .. attribute:: e
201
202 :type: int
203
Alex Gaynor2a918742014-01-26 16:53:44 -0600204 The public exponent. Alias for :attr:`public_exponent`.
205
Paul Kehrerac423232014-01-25 14:13:09 -0600206
Paul Kehrere51a2db2014-01-29 11:49:35 -0600207Hash Algorithms
208~~~~~~~~~~~~~~~
209
210.. class:: HashAlgorithm
211
Paul Kehrere51a2db2014-01-29 11:49:35 -0600212 .. attribute:: name
213
214 :type: str
215
Paul Kehrer4c75a8c2014-01-29 12:20:37 -0600216 The standard name for the hash algorithm, for example: ``"sha256"`` or
217 ``"whirlpool"``.
Paul Kehrere51a2db2014-01-29 11:49:35 -0600218
219 .. attribute:: digest_size
220
221 :type: int
222
223 The size of the resulting digest in bytes.
224
225 .. attribute:: block_size
226
227 :type: int
228
229 The internal block size of the hash algorithm in bytes.
230
231
Alex Gaynorb2774f52014-01-27 11:05:29 -0800232Key Derivation Functions
233~~~~~~~~~~~~~~~~~~~~~~~~
234
235.. class:: KeyDerivationFunction
236
Alex Gaynor8454c512014-01-28 07:01:54 -0800237 .. versionadded:: 0.2
238
Alex Gaynorb2774f52014-01-27 11:05:29 -0800239 .. method:: derive(key_material)
240
Alex Gaynor5484f722014-01-28 05:46:15 -0800241 :param key_material bytes: The input key material. Depending on what
242 key derivation function you are using this
243 could be either random material, or a user
Alex Gaynorb2774f52014-01-27 11:05:29 -0800244 supplied password.
Alex Gaynor5484f722014-01-28 05:46:15 -0800245 :return: The new key.
Alex Gaynore19e89f2014-01-28 06:58:43 -0800246 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
247 :meth:`derive` or
248 :meth:`verify` is
249 called more than
250 once.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800251
Alex Gaynor5484f722014-01-28 05:46:15 -0800252 This generates and returns a new key from the supplied key material.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800253
254 .. method:: verify(key_material, expected_key)
255
Alex Gaynor5484f722014-01-28 05:46:15 -0800256 :param key_material bytes: The input key material. This is the same as
Alex Gaynorb2774f52014-01-27 11:05:29 -0800257 ``key_material`` in :meth:`derive`.
Alex Gaynor5484f722014-01-28 05:46:15 -0800258 :param expected_key bytes: The expected result of deriving a new key,
259 this is the same as the return value of
260 :meth:`derive`.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800261 :raises cryptography.exceptions.InvalidKey: This is raised when the
262 derived key does not match
263 the expected key.
Alex Gaynore19e89f2014-01-28 06:58:43 -0800264 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
265 :meth:`derive` or
266 :meth:`verify` is
267 called more than
268 once.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800269
Alex Gaynor5484f722014-01-28 05:46:15 -0800270 This checks whether deriving a new key from the supplied
271 ``key_material`` generates the same key as the ``expected_key``, and
272 raises an exception if they do not match. This can be used for
273 something like checking whether a user's password attempt matches the
274 stored derived key.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800275
Paul Kehrerac423232014-01-25 14:13:09 -0600276.. _`RSA`: http://en.wikipedia.org/wiki/RSA_(cryptosystem)