blob: 2ba140bd346c3c739a96240875c61b8dd0939c09 [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
146.. class:: RSAPrivateKey
147
Paul Kehrer46688b12014-01-26 13:23:13 -0600148 .. versionadded:: 0.2
Paul Kehrer82629f42014-01-26 12:25:02 -0600149
Paul Kehrerac423232014-01-25 14:13:09 -0600150 An `RSA`_ private key.
151
Paul Kehrerf2fb02a2014-06-19 10:16:42 -0600152 .. method:: signer(padding, algorithm)
Paul Kehrer01cdfb22014-04-15 11:27:03 -0400153
154 .. versionadded:: 0.3
155
156 Sign data which can be verified later by others using the public key.
157
158 :param padding: An instance of a
159 :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricPadding`
160 provider.
161
162 :param algorithm: An instance of a
163 :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
164 provider.
165
Paul Kehrer01cdfb22014-04-15 11:27:03 -0400166 :returns:
167 :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricSignatureContext`
168
Paul Kehrerf2fb02a2014-06-19 10:16:42 -0600169 .. method:: decrypt(ciphertext, padding)
Paul Kehrer27f9ca62014-04-15 17:59:27 -0400170
171 .. versionadded:: 0.4
172
Alex Gaynore85e3562014-11-22 23:26:30 -0800173 Decrypt data that was encrypted with the public key.
Paul Kehrer27f9ca62014-04-15 17:59:27 -0400174
175 :param bytes ciphertext: The ciphertext to decrypt.
176
Alex Gaynore85e3562014-11-22 23:26:30 -0800177 :param padding: An instance of an
Paul Kehrer27f9ca62014-04-15 17:59:27 -0400178 :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricPadding`
179 provider.
180
Paul Kehrer27f9ca62014-04-15 17:59:27 -0400181 :return bytes: Decrypted data.
182
Paul Kehrer0e94fbe2014-01-26 11:47:21 -0600183 .. method:: public_key()
Paul Kehrerac423232014-01-25 14:13:09 -0600184
Paul Kehrer359b9462014-01-26 12:03:05 -0600185 :return: :class:`~cryptography.hazmat.primitives.interfaces.RSAPublicKey`
Paul Kehrerac423232014-01-25 14:13:09 -0600186
187 An RSA public key object corresponding to the values of the private key.
188
Alex Stapletonee3e6bf2014-02-02 21:13:48 +0000189 .. attribute:: key_size
Paul Kehrerac423232014-01-25 14:13:09 -0600190
191 :type: int
192
193 The bit length of the modulus.
194
Paul Kehrerf0a48c62014-06-07 17:04:13 -0500195.. class:: RSAPrivateKeyWithNumbers
196
197 .. versionadded:: 0.5
198
199 Extends :class:`RSAPrivateKey`.
200
201 .. method:: private_numbers()
202
203 Create a
204 :class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateNumbers`
205 object.
206
207 :returns: An
208 :class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateNumbers`
209 instance.
210
Paul Kehrerac423232014-01-25 14:13:09 -0600211
212.. class:: RSAPublicKey
213
Paul Kehrer46688b12014-01-26 13:23:13 -0600214 .. versionadded:: 0.2
Paul Kehrer82629f42014-01-26 12:25:02 -0600215
Paul Kehrerac423232014-01-25 14:13:09 -0600216 An `RSA`_ public key.
217
Paul Kehrerf2fb02a2014-06-19 10:16:42 -0600218 .. method:: verifier(signature, padding, algorithm)
Paul Kehrer01cdfb22014-04-15 11:27:03 -0400219
220 .. versionadded:: 0.3
221
222 Verify data was signed by the private key associated with this public
223 key.
224
225 :param bytes signature: The signature to verify.
226
227 :param padding: An instance of a
228 :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricPadding`
229 provider.
230
231 :param algorithm: An instance of a
232 :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
233 provider.
234
Paul Kehrer01cdfb22014-04-15 11:27:03 -0400235 :returns:
236 :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricVerificationContext`
237
Paul Kehrerf2fb02a2014-06-19 10:16:42 -0600238 .. method:: encrypt(plaintext, padding)
Paul Kehrer4e602f32014-04-24 12:07:54 -0500239
240 .. versionadded:: 0.4
241
242 Encrypt data with the public key.
243
244 :param bytes plaintext: The plaintext to encrypt.
245
246 :param padding: An instance of a
247 :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricPadding`
248 provider.
249
Paul Kehrer4e602f32014-04-24 12:07:54 -0500250 :return bytes: Encrypted data.
Paul Kehrer01cdfb22014-04-15 11:27:03 -0400251
Alex Stapletonee3e6bf2014-02-02 21:13:48 +0000252 .. attribute:: key_size
Paul Kehrerac423232014-01-25 14:13:09 -0600253
254 :type: int
255
256 The bit length of the modulus.
257
Paul Kehrerac423232014-01-25 14:13:09 -0600258
Paul Kehrerf0a48c62014-06-07 17:04:13 -0500259.. class:: RSAPublicKeyWithNumbers
260
261 .. versionadded:: 0.5
262
263 Extends :class:`RSAPublicKey`.
264
265 .. method:: public_numbers()
266
267 Create a
268 :class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicNumbers`
269 object.
270
271 :returns: An
272 :class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicNumbers`
273 instance.
274
275
Alex Stapleton085f3782014-04-01 16:18:17 +0100276.. class:: EllipticCurve
277
Alex Stapleton20c99032014-05-03 21:06:46 +0100278 .. versionadded:: 0.5
Alex Stapleton085f3782014-04-01 16:18:17 +0100279
280 A named elliptic curve.
281
282 .. attribute:: name
283
284 :type: string
285
286 The name of the curve. Usually the name used for the ASN.1 OID such as
Alex Stapleton6e526742014-05-23 22:06:06 +0100287 ``secp256k1``.
Alex Stapleton085f3782014-04-01 16:18:17 +0100288
289 .. attribute:: key_size
290
291 :type: int
292
Alex Stapletond4365692014-05-26 09:25:25 +0100293 The bit length of the curve's base point.
Alex Stapleton085f3782014-04-01 16:18:17 +0100294
295
Alex Gaynor645315b2014-06-23 11:55:55 -0700296Elliptic Curve
297~~~~~~~~~~~~~~
298
Alex Stapletona1853f92014-04-18 11:38:28 +0100299.. class:: EllipticCurveSignatureAlgorithm
300
Alex Stapleton20c99032014-05-03 21:06:46 +0100301 .. versionadded:: 0.5
Alex Stapletona1853f92014-04-18 11:38:28 +0100302
303 A signature algorithm for use with elliptic curve keys.
304
Alex Stapleton80228a12014-04-20 16:44:26 +0100305 .. attribute:: algorithm
306
307 :type: :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
308
309 The digest algorithm to be used with the signature scheme.
310
Alex Stapletona1853f92014-04-18 11:38:28 +0100311
Alex Stapleton085f3782014-04-01 16:18:17 +0100312.. class:: EllipticCurvePrivateKey
313
Alex Stapleton20c99032014-05-03 21:06:46 +0100314 .. versionadded:: 0.5
Alex Stapleton085f3782014-04-01 16:18:17 +0100315
316 An elliptic curve private key for use with an algorithm such as `ECDSA`_ or
317 `EdDSA`_.
318
Alex Gaynor93f135a2014-11-17 09:20:58 -0800319 .. method:: signer(signature_algorithm)
320
Alex Stapletona1853f92014-04-18 11:38:28 +0100321 Sign data which can be verified later by others using the public key.
Alex Gaynor93f135a2014-11-17 09:20:58 -0800322 The signature is formatted as DER-encoded bytes, as specified in
323 :rfc:`6979`.
Alex Stapletona1853f92014-04-18 11:38:28 +0100324
325 :param signature_algorithm: An instance of a
326 :class:`~cryptography.hazmat.primitives.interfaces.EllipticCurveSignatureAlgorithm`
327 provider.
328
Alex Stapletona1853f92014-04-18 11:38:28 +0100329 :returns:
330 :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricSignatureContext`
331
Alex Stapleton085f3782014-04-01 16:18:17 +0100332
333 :type: :class:`~cryptography.hazmat.primitives.interfaces.EllipticCurve`
334
Alex Stapleton085f3782014-04-01 16:18:17 +0100335 .. method:: public_key()
336
337 :return: :class:`~cryptography.hazmat.primitives.interfaces.EllipticCurvePublicKey`
338
339 The EllipticCurvePublicKey object for this private key.
340
341
Paul Kehrere025be22014-09-24 11:26:48 -0500342.. class:: EllipticCurvePrivateKeyWithNumbers
343
344 .. versionadded:: 0.6
345
346 Extends :class:`EllipticCurvePrivateKey`.
347
348 .. method:: private_numbers()
349
350 Create a
351 :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateNumbers`
352 object.
353
354 :returns: An
355 :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateNumbers`
356 instance.
357
358
Alex Stapleton085f3782014-04-01 16:18:17 +0100359.. class:: EllipticCurvePublicKey
360
Alex Stapleton20c99032014-05-03 21:06:46 +0100361 .. versionadded:: 0.5
Alex Stapleton085f3782014-04-01 16:18:17 +0100362
363 An elliptic curve public key.
364
Alex Gaynore85e3562014-11-22 23:26:30 -0800365 .. method:: verifier(signature, signature_algorithm)
366
Alex Stapletona1853f92014-04-18 11:38:28 +0100367 Verify data was signed by the private key associated with this public
368 key.
369
Alex Gaynor93f135a2014-11-17 09:20:58 -0800370 :param bytes signature: The signature to verify. DER encoded as
371 specified in :rfc:`6979`.
Alex Stapleton80228a12014-04-20 16:44:26 +0100372
Alex Stapletona1853f92014-04-18 11:38:28 +0100373 :param signature_algorithm: An instance of a
374 :class:`~cryptography.hazmat.primitives.interfaces.EllipticCurveSignatureAlgorithm`
375 provider.
376
Alex Stapletona1853f92014-04-18 11:38:28 +0100377 :returns:
378 :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricSignatureContext`
379
Alex Stapleton085f3782014-04-01 16:18:17 +0100380 .. attribute:: curve
381
382 :type: :class:`~cryptography.hazmat.primitives.interfaces.EllipticCurve`
383
384 The elliptic curve for this key.
385
Alex Stapleton085f3782014-04-01 16:18:17 +0100386
Paul Kehrere025be22014-09-24 11:26:48 -0500387.. class:: EllipticCurvePublicKeyWithNumbers
388
389 .. versionadded:: 0.6
390
391 Extends :class:`EllipticCurvePublicKey`.
392
Paul Kehrer5cfd2112014-09-24 14:51:01 -0500393 .. method:: public_numbers()
Paul Kehrere025be22014-09-24 11:26:48 -0500394
395 Create a
396 :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicNumbers`
397 object.
398
399 :returns: An
400 :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicNumbers`
401 instance.
402
403
Alex Stapletonc5fffd32014-03-18 15:29:00 +0000404Hash algorithms
Alex Gaynor645315b2014-06-23 11:55:55 -0700405---------------
Paul Kehrere51a2db2014-01-29 11:49:35 -0600406
407.. class:: HashAlgorithm
408
Paul Kehrere51a2db2014-01-29 11:49:35 -0600409 .. attribute:: name
410
411 :type: str
412
Paul Kehrer4c75a8c2014-01-29 12:20:37 -0600413 The standard name for the hash algorithm, for example: ``"sha256"`` or
414 ``"whirlpool"``.
Paul Kehrere51a2db2014-01-29 11:49:35 -0600415
416 .. attribute:: digest_size
417
418 :type: int
419
420 The size of the resulting digest in bytes.
421
422 .. attribute:: block_size
423
424 :type: int
425
426 The internal block size of the hash algorithm in bytes.
427
428
Ayrxa0f98502014-04-15 19:17:03 +0800429.. class:: HashContext
430
431 .. attribute:: algorithm
432
433 A :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` that
434 will be used by this context.
435
436 .. method:: update(data)
437
Alex Gaynore85e3562014-11-22 23:26:30 -0800438 :param bytes data: The data you want to hash.
Ayrxa0f98502014-04-15 19:17:03 +0800439
440 .. method:: finalize()
441
442 :return: The final digest as bytes.
443
444 .. method:: copy()
445
446 :return: A :class:`~cryptography.hazmat.primitives.interfaces.HashContext`
447 that is a copy of the current context.
448
449
Alex Stapletonc5fffd32014-03-18 15:29:00 +0000450Key derivation functions
Alex Gaynor645315b2014-06-23 11:55:55 -0700451------------------------
Alex Gaynorb2774f52014-01-27 11:05:29 -0800452
453.. class:: KeyDerivationFunction
454
Alex Gaynor8454c512014-01-28 07:01:54 -0800455 .. versionadded:: 0.2
456
Alex Gaynorb2774f52014-01-27 11:05:29 -0800457 .. method:: derive(key_material)
458
Alex Gaynore85e3562014-11-22 23:26:30 -0800459 :param bytes key_material: The input key material. Depending on what
Alex Gaynor5484f722014-01-28 05:46:15 -0800460 key derivation function you are using this
Alex Gaynore85e3562014-11-22 23:26:30 -0800461 could be either random bytes, or a user
Alex Gaynorb2774f52014-01-27 11:05:29 -0800462 supplied password.
Alex Gaynor5484f722014-01-28 05:46:15 -0800463 :return: The new key.
Alex Gaynore19e89f2014-01-28 06:58:43 -0800464 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
465 :meth:`derive` or
466 :meth:`verify` is
467 called more than
468 once.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800469
Alex Gaynor5484f722014-01-28 05:46:15 -0800470 This generates and returns a new key from the supplied key material.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800471
472 .. method:: verify(key_material, expected_key)
473
Alex Gaynore85e3562014-11-22 23:26:30 -0800474 :param bytes key_material: The input key material. This is the same as
Alex Gaynorb2774f52014-01-27 11:05:29 -0800475 ``key_material`` in :meth:`derive`.
Alex Gaynore85e3562014-11-22 23:26:30 -0800476 :param bytes expected_key: The expected result of deriving a new key,
Alex Gaynor5484f722014-01-28 05:46:15 -0800477 this is the same as the return value of
478 :meth:`derive`.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800479 :raises cryptography.exceptions.InvalidKey: This is raised when the
480 derived key does not match
481 the expected key.
Alex Gaynore19e89f2014-01-28 06:58:43 -0800482 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
483 :meth:`derive` or
484 :meth:`verify` is
485 called more than
486 once.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800487
Alex Gaynor5484f722014-01-28 05:46:15 -0800488 This checks whether deriving a new key from the supplied
489 ``key_material`` generates the same key as the ``expected_key``, and
490 raises an exception if they do not match. This can be used for
491 something like checking whether a user's password attempt matches the
492 stored derived key.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800493
Ayrxc8121702014-04-15 19:02:05 +0800494
Terry Chiacc5e4452014-10-12 15:35:21 +0800495`Message Authentication Code`_
496------------------------------
Ayrxc8121702014-04-15 19:02:05 +0800497
498.. class:: CMACContext
499
Alex Gaynor7d156882014-10-20 10:40:34 -0700500 :class:`CMACContext` has been deprecated in favor of :class:`MACContext`.
Terry Chiac7c82f32014-10-20 12:15:22 +0800501
Ayrxc8121702014-04-15 19:02:05 +0800502 .. versionadded:: 0.4
503
504 .. method:: update(data)
505
Alex Gaynore85e3562014-11-22 23:26:30 -0800506 :param bytes data: The data you want to authenticate.
Ayrxc8121702014-04-15 19:02:05 +0800507
508 .. method:: finalize()
509
Ayrx7964c172014-04-15 21:50:58 +0800510 :return: The message authentication code.
Ayrxc8121702014-04-15 19:02:05 +0800511
512 .. method:: copy()
513
514 :return: A :class:`~cryptography.hazmat.primitives.interfaces.CMACContext`
515 that is a copy of the current context.
516
Terry Chiacc5e4452014-10-12 15:35:21 +0800517.. class:: MACContext
518
519 .. versionadded:: 0.7
520
521 .. method:: update(data)
522
Alex Gaynore85e3562014-11-22 23:26:30 -0800523 :param bytes data: The data you want to authenticate.
Terry Chiacc5e4452014-10-12 15:35:21 +0800524
525 .. method:: finalize()
526
527 :return: The message authentication code.
528
529 .. method:: copy()
530
Alex Gaynor7d156882014-10-20 10:40:34 -0700531 :return: A
532 :class:`~cryptography.hazmat.primitives.interfaces.MACContext` that
533 is a copy of the current context.
Terry Chiacc5e4452014-10-12 15:35:21 +0800534
Alex Gaynor7d156882014-10-20 10:40:34 -0700535 .. method:: verify(signature)
Terry Chiacc5e4452014-10-12 15:35:21 +0800536
Alex Gaynore85e3562014-11-22 23:26:30 -0800537 :param bytes signature: The signature to verify.
Terry Chiacc5e4452014-10-12 15:35:21 +0800538
539 :raises cryptography.exceptions.InvalidSignature: This is raised when
540 the provided signature does not match the expected signature.
Ayrxc8121702014-04-15 19:02:05 +0800541
Paul Kehrer05c122b2014-11-24 08:41:05 -1000542
Paul Kehrer8e9c9842014-02-13 12:23:27 -0600543.. _`RSA`: https://en.wikipedia.org/wiki/RSA_(cryptosystem)
544.. _`Chinese remainder theorem`: https://en.wikipedia.org/wiki/Chinese_remainder_theorem
Mohammed Attia604c78f2014-03-04 03:56:08 +0200545.. _`DSA`: https://en.wikipedia.org/wiki/Digital_Signature_Algorithm
Ayrx83cd3f82014-04-15 21:56:32 +0800546.. _`CMAC`: https://en.wikipedia.org/wiki/CMAC
Alex Gaynore9df2942014-12-12 10:56:26 -0800547.. _`ECDSA`: https://en.wikipedia.org/wiki/ECDSA
548.. _`EdDSA`: https://en.wikipedia.org/wiki/EdDSA