blob: 95fd6f9faf42d2a82ce9d6767fcc24510d8904da [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
Alex Stapletonc5fffd32014-03-18 15:29:00 +000015Symmetric ciphers
David Reid0a394df2013-11-15 16:19:50 -080016~~~~~~~~~~~~~~~~~
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
David Reid0a394df2013-11-15 16:19:50 -080051------------
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
Alex Stapletonc5fffd32014-03-18 15:29:00 +0000106Asymmetric interfaces
Paul Kehrerac423232014-01-25 14:13:09 -0600107~~~~~~~~~~~~~~~~~~~~~
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 Kehrer01cdfb22014-04-15 11:27:03 -0400115 .. method:: signer(padding, algorithm, backend)
116
117 .. versionadded:: 0.3
118
119 Sign data which can be verified later by others using the public key.
120
121 :param padding: An instance of a
122 :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricPadding`
123 provider.
124
125 :param algorithm: An instance of a
126 :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
127 provider.
128
129 :param backend: A
130 :class:`~cryptography.hazmat.backends.interfaces.RSABackend`
131 provider.
132
133 :returns:
134 :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricSignatureContext`
135
Paul Kehrer0e94fbe2014-01-26 11:47:21 -0600136 .. method:: public_key()
Paul Kehrerac423232014-01-25 14:13:09 -0600137
Paul Kehrer359b9462014-01-26 12:03:05 -0600138 :return: :class:`~cryptography.hazmat.primitives.interfaces.RSAPublicKey`
Paul Kehrerac423232014-01-25 14:13:09 -0600139
140 An RSA public key object corresponding to the values of the private key.
141
142 .. attribute:: modulus
143
Paul Kehrerd527b302014-01-26 11:41:38 -0600144 :type: int
Paul Kehrerac423232014-01-25 14:13:09 -0600145
Paul Kehrer0e94fbe2014-01-26 11:47:21 -0600146 The public modulus.
Paul Kehrerac423232014-01-25 14:13:09 -0600147
148 .. attribute:: public_exponent
149
150 :type: int
151
Paul Kehrer0e94fbe2014-01-26 11:47:21 -0600152 The public exponent.
Paul Kehrerac423232014-01-25 14:13:09 -0600153
Alex Gaynor2649a692014-02-03 07:14:16 -0800154 .. attribute:: private_exponent
155
156 :type: int
157
158 The private exponent.
159
Alex Stapletonee3e6bf2014-02-02 21:13:48 +0000160 .. attribute:: key_size
Paul Kehrerac423232014-01-25 14:13:09 -0600161
162 :type: int
163
164 The bit length of the modulus.
165
166 .. attribute:: p
167
Paul Kehrerd527b302014-01-26 11:41:38 -0600168 :type: int
Paul Kehrerac423232014-01-25 14:13:09 -0600169
Alex Gaynor2a918742014-01-26 16:53:44 -0600170 ``p``, one of the two primes composing the :attr:`modulus`.
Paul Kehrerac423232014-01-25 14:13:09 -0600171
172 .. attribute:: q
173
Paul Kehrerd527b302014-01-26 11:41:38 -0600174 :type: int
Paul Kehrerac423232014-01-25 14:13:09 -0600175
Alex Gaynor2a918742014-01-26 16:53:44 -0600176 ``q``, one of the two primes composing the :attr:`modulus`.
Paul Kehrerac423232014-01-25 14:13:09 -0600177
178 .. attribute:: d
179
Paul Kehrerd527b302014-01-26 11:41:38 -0600180 :type: int
Paul Kehrerac423232014-01-25 14:13:09 -0600181
Alex Gaynor2649a692014-02-03 07:14:16 -0800182 The private exponent. Alias for :attr:`private_exponent`.
Paul Kehrerac423232014-01-25 14:13:09 -0600183
Paul Kehrer8e9c9842014-02-13 12:23:27 -0600184 .. attribute:: dmp1
185
186 :type: int
187
188 A `Chinese remainder theorem`_ coefficient used to speed up RSA
189 operations. Calculated as: d mod (p-1)
190
191 .. attribute:: dmq1
192
193 :type: int
194
195 A `Chinese remainder theorem`_ coefficient used to speed up RSA
196 operations. Calculated as: d mod (q-1)
197
198 .. attribute:: iqmp
199
200 :type: int
201
202 A `Chinese remainder theorem`_ coefficient used to speed up RSA
203 operations. Calculated as: q\ :sup:`-1` mod p
204
Paul Kehrerac423232014-01-25 14:13:09 -0600205 .. attribute:: n
206
Paul Kehrerd527b302014-01-26 11:41:38 -0600207 :type: int
Paul Kehrerac423232014-01-25 14:13:09 -0600208
Alex Gaynor2a918742014-01-26 16:53:44 -0600209 The public modulus. Alias for :attr:`modulus`.
Paul Kehrerac423232014-01-25 14:13:09 -0600210
211 .. attribute:: e
212
213 :type: int
214
Alex Gaynor2a918742014-01-26 16:53:44 -0600215 The public exponent. Alias for :attr:`public_exponent`.
Paul Kehrerac423232014-01-25 14:13:09 -0600216
217
218.. class:: RSAPublicKey
219
Paul Kehrer46688b12014-01-26 13:23:13 -0600220 .. versionadded:: 0.2
Paul Kehrer82629f42014-01-26 12:25:02 -0600221
Paul Kehrerac423232014-01-25 14:13:09 -0600222 An `RSA`_ public key.
223
Paul Kehrer01cdfb22014-04-15 11:27:03 -0400224 .. method:: verifier(signature, padding, algorithm, backend)
225
226 .. versionadded:: 0.3
227
228 Verify data was signed by the private key associated with this public
229 key.
230
231 :param bytes signature: The signature to verify.
232
233 :param padding: An instance of a
234 :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricPadding`
235 provider.
236
237 :param algorithm: An instance of a
238 :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
239 provider.
240
241 :param backend: A
242 :class:`~cryptography.hazmat.backends.interfaces.RSABackend`
243 provider.
244
245 :returns:
246 :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricVerificationContext`
247
248
Paul Kehrerac423232014-01-25 14:13:09 -0600249 .. attribute:: modulus
250
Paul Kehrerd527b302014-01-26 11:41:38 -0600251 :type: int
Paul Kehrerac423232014-01-25 14:13:09 -0600252
Paul Kehrer0e94fbe2014-01-26 11:47:21 -0600253 The public modulus.
Paul Kehrerac423232014-01-25 14:13:09 -0600254
Alex Stapletonee3e6bf2014-02-02 21:13:48 +0000255 .. attribute:: key_size
Paul Kehrerac423232014-01-25 14:13:09 -0600256
257 :type: int
258
259 The bit length of the modulus.
260
261 .. attribute:: public_exponent
262
263 :type: int
264
Paul Kehrer0e94fbe2014-01-26 11:47:21 -0600265 The public exponent.
Paul Kehrerac423232014-01-25 14:13:09 -0600266
267 .. attribute:: n
268
Paul Kehrerd527b302014-01-26 11:41:38 -0600269 :type: int
Paul Kehrerac423232014-01-25 14:13:09 -0600270
Alex Gaynor2a918742014-01-26 16:53:44 -0600271 The public modulus. Alias for :attr:`modulus`.
Paul Kehrerac423232014-01-25 14:13:09 -0600272
273 .. attribute:: e
274
275 :type: int
276
Alex Gaynor2a918742014-01-26 16:53:44 -0600277 The public exponent. Alias for :attr:`public_exponent`.
278
Paul Kehrerac423232014-01-25 14:13:09 -0600279
Mohammed Attia71acc672014-03-04 19:20:45 +0200280.. class:: DSAParameters
Mohammed Attiab4167152014-03-04 03:29:56 +0200281
282 .. versionadded:: 0.3
283
284 `DSA`_ parameters.
285
286 .. attribute:: modulus
287
288 :type: int
289
Mohammed Attia7a1738a2014-03-04 19:17:24 +0200290 The prime modulus that is used in generating the DSA key pair and used
Mohammed Attiab4167152014-03-04 03:29:56 +0200291 in the DSA signing and verification processes.
292
293 .. attribute:: subgroup_order
294
295 :type: int
296
Mohammed Attia7a1738a2014-03-04 19:17:24 +0200297 The subgroup order that is used in generating the DSA key pair
Mohammed Attiab4167152014-03-04 03:29:56 +0200298 by the generator and used in the DSA signing and verification
299 processes.
300
301 .. attribute:: generator
302
303 :type: int
304
Mohammed Attiacb9a6c22014-03-04 04:16:35 +0200305 The generator that is used in generating the DSA key pair and used
Mohammed Attia7a1738a2014-03-04 19:17:24 +0200306 in the DSA signing and verification processes.
Mohammed Attiab4167152014-03-04 03:29:56 +0200307
308 .. attribute:: p
309
310 :type: int
311
Mohammed Attia7a1738a2014-03-04 19:17:24 +0200312 The prime modulus that is used in generating the DSA key pair and used
Mohammed Attia70324512014-03-04 03:34:39 +0200313 in the DSA signing and verification processes. Alias for :attr:`modulus`.
Mohammed Attiab4167152014-03-04 03:29:56 +0200314
315 .. attribute:: q
316
317 :type: int
318
Mohammed Attia7a1738a2014-03-04 19:17:24 +0200319 The subgroup order that is used in generating the DSA key pair
Mohammed Attiab4167152014-03-04 03:29:56 +0200320 by the generator and used in the DSA signing and verification
Mohammed Attia70324512014-03-04 03:34:39 +0200321 processes. Alias for :attr:`subgroup_order`.
Mohammed Attiab4167152014-03-04 03:29:56 +0200322
323 .. attribute:: g
324
325 :type: int
326
Mohammed Attiacb9a6c22014-03-04 04:16:35 +0200327 The generator that is used in generating the DSA key pair and used
Mohammed Attia70324512014-03-04 03:34:39 +0200328 in the DSA signing and verification processes. Alias for :attr:`generator`.
Mohammed Attiab4167152014-03-04 03:29:56 +0200329
330
331.. class:: DSAPrivateKey
332
333 .. versionadded:: 0.3
334
Mohammed Attia7a1738a2014-03-04 19:17:24 +0200335 A `DSA`_ private key.
Mohammed Attiab4167152014-03-04 03:29:56 +0200336
337 .. method:: public_key()
338
339 :return: :class:`~cryptography.hazmat.primitives.interfaces.DSAPublicKey`
340
341 An DSA public key object corresponding to the values of the private key.
342
343 .. method:: parameters()
344
Mohammed Attia71acc672014-03-04 19:20:45 +0200345 :return: :class:`~cryptography.hazmat.primitives.interfaces.DSAParameters`
Mohammed Attiab4167152014-03-04 03:29:56 +0200346
Mohammed Attia71acc672014-03-04 19:20:45 +0200347 The DSAParameters object associated with this private key.
Mohammed Attiab4167152014-03-04 03:29:56 +0200348
349 .. attribute:: key_size
350
351 :type: int
352
353 The bit length of the modulus.
354
355 .. attribute:: x
356
357 :type: int
358
359 The private key.
360
361 .. attribute:: y
362
363 :type: int
364
365 The public key.
366
367
368.. class:: DSAPublicKey
369
370 .. versionadded:: 0.3
371
Mohammed Attiaedacb142014-03-17 12:28:23 +0200372 A `DSA`_ public key.
373
374 .. attribute:: key_size
375
376 :type: int
377
378 The bit length of the modulus.
Mohammed Attiab4167152014-03-04 03:29:56 +0200379
380 .. method:: parameters()
381
Mohammed Attia71acc672014-03-04 19:20:45 +0200382 :return: :class:`~cryptography.hazmat.primitives.interfaces.DSAParameters`
Mohammed Attiab4167152014-03-04 03:29:56 +0200383
Mohammed Attia71acc672014-03-04 19:20:45 +0200384 The DSAParameters object associated with this public key.
Mohammed Attiab4167152014-03-04 03:29:56 +0200385
386 .. attribute:: y
387
388 :type: int
389
390 The public key.
391
392
Paul Kehrereda558c2014-02-17 21:18:13 -0600393.. class:: AsymmetricSignatureContext
Paul Kehrere0f0f342014-02-17 19:20:51 -0600394
395 .. versionadded:: 0.2
396
397 .. method:: update(data)
398
Paul Kehrereda558c2014-02-17 21:18:13 -0600399 :param bytes data: The data you want to sign.
Paul Kehrere0f0f342014-02-17 19:20:51 -0600400
401 .. method:: finalize()
402
403 :return bytes signature: The signature.
404
405
Paul Kehrer430202d2014-02-18 13:36:53 -0600406.. class:: AsymmetricVerificationContext
Paul Kehrere0f0f342014-02-17 19:20:51 -0600407
408 .. versionadded:: 0.2
409
410 .. method:: update(data)
411
Paul Kehrereda558c2014-02-17 21:18:13 -0600412 :param bytes data: The data you wish to verify using the signature.
Paul Kehrere0f0f342014-02-17 19:20:51 -0600413
Paul Kehrerdd3780a2014-02-18 13:17:53 -0600414 .. method:: verify()
Paul Kehrere0f0f342014-02-17 19:20:51 -0600415
Paul Kehrerfef1fbd2014-02-26 23:39:37 -0400416 :raises cryptography.exceptions.InvalidSignature: If the signature does
417 not validate.
Paul Kehrere0f0f342014-02-17 19:20:51 -0600418
419
420.. class:: AsymmetricPadding
421
Paul Kehrer19f32d52014-02-17 19:23:06 -0600422 .. versionadded:: 0.2
Paul Kehrere0f0f342014-02-17 19:20:51 -0600423
424 .. attribute:: name
425
Alex Stapletonc5fffd32014-03-18 15:29:00 +0000426Hash algorithms
Paul Kehrere51a2db2014-01-29 11:49:35 -0600427~~~~~~~~~~~~~~~
428
429.. class:: HashAlgorithm
430
Paul Kehrere51a2db2014-01-29 11:49:35 -0600431 .. attribute:: name
432
433 :type: str
434
Paul Kehrer4c75a8c2014-01-29 12:20:37 -0600435 The standard name for the hash algorithm, for example: ``"sha256"`` or
436 ``"whirlpool"``.
Paul Kehrere51a2db2014-01-29 11:49:35 -0600437
438 .. attribute:: digest_size
439
440 :type: int
441
442 The size of the resulting digest in bytes.
443
444 .. attribute:: block_size
445
446 :type: int
447
448 The internal block size of the hash algorithm in bytes.
449
450
Ayrxa0f98502014-04-15 19:17:03 +0800451.. class:: HashContext
452
453 .. attribute:: algorithm
454
455 A :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` that
456 will be used by this context.
457
458 .. method:: update(data)
459
460 :param data bytes: The data you want to hash.
461
462 .. method:: finalize()
463
464 :return: The final digest as bytes.
465
466 .. method:: copy()
467
468 :return: A :class:`~cryptography.hazmat.primitives.interfaces.HashContext`
469 that is a copy of the current context.
470
471
Alex Stapletonc5fffd32014-03-18 15:29:00 +0000472Key derivation functions
Alex Gaynorb2774f52014-01-27 11:05:29 -0800473~~~~~~~~~~~~~~~~~~~~~~~~
474
475.. class:: KeyDerivationFunction
476
Alex Gaynor8454c512014-01-28 07:01:54 -0800477 .. versionadded:: 0.2
478
Alex Gaynorb2774f52014-01-27 11:05:29 -0800479 .. method:: derive(key_material)
480
Alex Gaynor5484f722014-01-28 05:46:15 -0800481 :param key_material bytes: The input key material. Depending on what
482 key derivation function you are using this
483 could be either random material, or a user
Alex Gaynorb2774f52014-01-27 11:05:29 -0800484 supplied password.
Alex Gaynor5484f722014-01-28 05:46:15 -0800485 :return: The new key.
Alex Gaynore19e89f2014-01-28 06:58:43 -0800486 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
487 :meth:`derive` or
488 :meth:`verify` is
489 called more than
490 once.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800491
Alex Gaynor5484f722014-01-28 05:46:15 -0800492 This generates and returns a new key from the supplied key material.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800493
494 .. method:: verify(key_material, expected_key)
495
Alex Gaynor5484f722014-01-28 05:46:15 -0800496 :param key_material bytes: The input key material. This is the same as
Alex Gaynorb2774f52014-01-27 11:05:29 -0800497 ``key_material`` in :meth:`derive`.
Alex Gaynor5484f722014-01-28 05:46:15 -0800498 :param expected_key bytes: The expected result of deriving a new key,
499 this is the same as the return value of
500 :meth:`derive`.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800501 :raises cryptography.exceptions.InvalidKey: This is raised when the
502 derived key does not match
503 the expected key.
Alex Gaynore19e89f2014-01-28 06:58:43 -0800504 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
505 :meth:`derive` or
506 :meth:`verify` is
507 called more than
508 once.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800509
Alex Gaynor5484f722014-01-28 05:46:15 -0800510 This checks whether deriving a new key from the supplied
511 ``key_material`` generates the same key as the ``expected_key``, and
512 raises an exception if they do not match. This can be used for
513 something like checking whether a user's password attempt matches the
514 stored derived key.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800515
Ayrxc8121702014-04-15 19:02:05 +0800516
Ayrx83cd3f82014-04-15 21:56:32 +0800517`CMAC`_
518~~~~~~~
Ayrxc8121702014-04-15 19:02:05 +0800519
520.. class:: CMACContext
521
522 .. versionadded:: 0.4
523
524 .. method:: update(data)
525
526 :param data bytes: The data you want to authenticate.
527
528 .. method:: finalize()
529
Ayrx7964c172014-04-15 21:50:58 +0800530 :return: The message authentication code.
Ayrxc8121702014-04-15 19:02:05 +0800531
532 .. method:: copy()
533
534 :return: A :class:`~cryptography.hazmat.primitives.interfaces.CMACContext`
535 that is a copy of the current context.
536
537
Paul Kehrer8e9c9842014-02-13 12:23:27 -0600538.. _`RSA`: https://en.wikipedia.org/wiki/RSA_(cryptosystem)
539.. _`Chinese remainder theorem`: https://en.wikipedia.org/wiki/Chinese_remainder_theorem
Mohammed Attia604c78f2014-03-04 03:56:08 +0200540.. _`DSA`: https://en.wikipedia.org/wiki/Digital_Signature_Algorithm
Ayrx83cd3f82014-04-15 21:56:32 +0800541.. _`CMAC`: https://en.wikipedia.org/wiki/CMAC