blob: 4f185af3e7fa49931c2f8cf32e6610bc91d7aa12 [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
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
173 Decrypt data that was encrypted via the public key.
174
175 :param bytes ciphertext: The ciphertext to decrypt.
176
177 :param padding: An instance of a
178 :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 Gaynor645315b2014-06-23 11:55:55 -0700276DSA
277~~~
278
Mohammed Attia71acc672014-03-04 19:20:45 +0200279.. class:: DSAParameters
Mohammed Attiab4167152014-03-04 03:29:56 +0200280
281 .. versionadded:: 0.3
282
283 `DSA`_ parameters.
284
Paul Kehrerdacb5f92014-06-27 09:15:07 -0600285 .. method:: generate_private_key()
286
287 .. versionadded:: 0.5
288
289 Generate a DSA private key. This method can be used to generate many
290 new private keys from a single set of parameters.
291
292 :return: A
293 :class:`~cryptography.hazmat.primitives.interfaces.DSAPrivateKey`
294 provider.
295
Mohammed Attiab4167152014-03-04 03:29:56 +0200296
Paul Kehrer98429642014-06-22 16:37:21 -0600297.. class:: DSAParametersWithNumbers
298
299 .. versionadded:: 0.5
300
301 Extends :class:`DSAParameters`.
302
303 .. method:: parameter_numbers()
304
305 Create a
306 :class:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAParameterNumbers`
307 object.
308
309 :returns: A
310 :class:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAParameterNumbers`
311 instance.
312
313
Mohammed Attiab4167152014-03-04 03:29:56 +0200314.. class:: DSAPrivateKey
315
316 .. versionadded:: 0.3
317
Mohammed Attia7a1738a2014-03-04 19:17:24 +0200318 A `DSA`_ private key.
Mohammed Attiab4167152014-03-04 03:29:56 +0200319
320 .. method:: public_key()
321
322 :return: :class:`~cryptography.hazmat.primitives.interfaces.DSAPublicKey`
323
324 An DSA public key object corresponding to the values of the private key.
325
326 .. method:: parameters()
327
Mohammed Attia71acc672014-03-04 19:20:45 +0200328 :return: :class:`~cryptography.hazmat.primitives.interfaces.DSAParameters`
Mohammed Attiab4167152014-03-04 03:29:56 +0200329
Mohammed Attia71acc672014-03-04 19:20:45 +0200330 The DSAParameters object associated with this private key.
Mohammed Attiab4167152014-03-04 03:29:56 +0200331
Paul Kehrer0b3ff3b2014-05-01 15:34:42 -0500332 .. method:: signer(algorithm, backend)
333
334 .. versionadded:: 0.4
335
336 Sign data which can be verified later by others using the public key.
Alex Gaynor93f135a2014-11-17 09:20:58 -0800337 The signature is formatted as DER-encoded bytes, as specified in
338 :rfc:`6979`.
Paul Kehrer0b3ff3b2014-05-01 15:34:42 -0500339
340 :param algorithm: An instance of a
341 :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
342 provider.
343
344 :param backend: A
345 :class:`~cryptography.hazmat.backends.interfaces.DSABackend`
346 provider.
347
348 :returns:
349 :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricSignatureContext`
350
Mohammed Attiab4167152014-03-04 03:29:56 +0200351 .. attribute:: key_size
352
353 :type: int
354
355 The bit length of the modulus.
356
Mohammed Attiab4167152014-03-04 03:29:56 +0200357
Paul Kehrer98429642014-06-22 16:37:21 -0600358.. class:: DSAPrivateKeyWithNumbers
359
360 .. versionadded:: 0.5
361
362 Extends :class:`DSAPrivateKey`.
363
364 .. method:: private_numbers()
365
366 Create a
367 :class:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAPrivateNumbers`
368 object.
369
370 :returns: A
371 :class:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAPrivateNumbers`
372 instance.
373
374
Mohammed Attiab4167152014-03-04 03:29:56 +0200375.. class:: DSAPublicKey
376
377 .. versionadded:: 0.3
378
Mohammed Attiaedacb142014-03-17 12:28:23 +0200379 A `DSA`_ public key.
380
381 .. attribute:: key_size
382
383 :type: int
384
385 The bit length of the modulus.
Mohammed Attiab4167152014-03-04 03:29:56 +0200386
387 .. method:: parameters()
388
Mohammed Attia71acc672014-03-04 19:20:45 +0200389 :return: :class:`~cryptography.hazmat.primitives.interfaces.DSAParameters`
Mohammed Attiab4167152014-03-04 03:29:56 +0200390
Mohammed Attia71acc672014-03-04 19:20:45 +0200391 The DSAParameters object associated with this public key.
Mohammed Attiab4167152014-03-04 03:29:56 +0200392
Mohammed Attia59edb612014-04-25 22:44:40 +0200393 .. method:: verifier(signature, algorithm, backend)
Mohammed Attiab4167152014-03-04 03:29:56 +0200394
Mohammed Attia59edb612014-04-25 22:44:40 +0200395 .. versionadded:: 0.4
Mohammed Attiab4167152014-03-04 03:29:56 +0200396
Mohammed Attia59edb612014-04-25 22:44:40 +0200397 Verify data was signed by the private key associated with this public
398 key.
399
Paul Kehrere0aeaf82014-05-01 11:58:23 -0500400 :param bytes signature: The signature to verify. DER encoded as
401 specified in :rfc:`6979`.
Mohammed Attia59edb612014-04-25 22:44:40 +0200402
403 :param algorithm: An instance of a
404 :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
405 provider.
406
407 :param backend: A
408 :class:`~cryptography.hazmat.backends.interfaces.DSABackend`
409 provider.
410
411 :returns:
412 :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricVerificationContext`
Mohammed Attiab4167152014-03-04 03:29:56 +0200413
414
Paul Kehrer98429642014-06-22 16:37:21 -0600415.. class:: DSAPublicKeyWithNumbers
416
417 .. versionadded:: 0.5
418
419 Extends :class:`DSAPublicKey`.
420
Paul Kehrer5cfd2112014-09-24 14:51:01 -0500421 .. method:: public_numbers()
Paul Kehrer98429642014-06-22 16:37:21 -0600422
423 Create a
424 :class:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAPublicNumbers`
425 object.
426
427 :returns: A
428 :class:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAPublicNumbers`
429 instance.
430
431
Alex Stapleton085f3782014-04-01 16:18:17 +0100432.. class:: EllipticCurve
433
Alex Stapleton20c99032014-05-03 21:06:46 +0100434 .. versionadded:: 0.5
Alex Stapleton085f3782014-04-01 16:18:17 +0100435
436 A named elliptic curve.
437
438 .. attribute:: name
439
440 :type: string
441
442 The name of the curve. Usually the name used for the ASN.1 OID such as
Alex Stapleton6e526742014-05-23 22:06:06 +0100443 ``secp256k1``.
Alex Stapleton085f3782014-04-01 16:18:17 +0100444
445 .. attribute:: key_size
446
447 :type: int
448
Alex Stapletond4365692014-05-26 09:25:25 +0100449 The bit length of the curve's base point.
Alex Stapleton085f3782014-04-01 16:18:17 +0100450
451
Alex Gaynor645315b2014-06-23 11:55:55 -0700452Elliptic Curve
453~~~~~~~~~~~~~~
454
Alex Stapletona1853f92014-04-18 11:38:28 +0100455.. class:: EllipticCurveSignatureAlgorithm
456
Alex Stapleton20c99032014-05-03 21:06:46 +0100457 .. versionadded:: 0.5
Alex Stapletona1853f92014-04-18 11:38:28 +0100458
459 A signature algorithm for use with elliptic curve keys.
460
Alex Stapleton80228a12014-04-20 16:44:26 +0100461 .. attribute:: algorithm
462
463 :type: :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
464
465 The digest algorithm to be used with the signature scheme.
466
Alex Stapletona1853f92014-04-18 11:38:28 +0100467
Alex Stapleton085f3782014-04-01 16:18:17 +0100468.. class:: EllipticCurvePrivateKey
469
Alex Stapleton20c99032014-05-03 21:06:46 +0100470 .. versionadded:: 0.5
Alex Stapleton085f3782014-04-01 16:18:17 +0100471
472 An elliptic curve private key for use with an algorithm such as `ECDSA`_ or
473 `EdDSA`_.
474
Alex Gaynor93f135a2014-11-17 09:20:58 -0800475 .. method:: signer(signature_algorithm)
476
Alex Stapletona1853f92014-04-18 11:38:28 +0100477 Sign data which can be verified later by others using the public key.
Alex Gaynor93f135a2014-11-17 09:20:58 -0800478 The signature is formatted as DER-encoded bytes, as specified in
479 :rfc:`6979`.
Alex Stapletona1853f92014-04-18 11:38:28 +0100480
481 :param signature_algorithm: An instance of a
482 :class:`~cryptography.hazmat.primitives.interfaces.EllipticCurveSignatureAlgorithm`
483 provider.
484
Alex Stapletona1853f92014-04-18 11:38:28 +0100485 :returns:
486 :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricSignatureContext`
487
Alex Stapleton085f3782014-04-01 16:18:17 +0100488
489 :type: :class:`~cryptography.hazmat.primitives.interfaces.EllipticCurve`
490
Alex Stapleton085f3782014-04-01 16:18:17 +0100491 .. method:: public_key()
492
493 :return: :class:`~cryptography.hazmat.primitives.interfaces.EllipticCurvePublicKey`
494
495 The EllipticCurvePublicKey object for this private key.
496
497
Paul Kehrere025be22014-09-24 11:26:48 -0500498.. class:: EllipticCurvePrivateKeyWithNumbers
499
500 .. versionadded:: 0.6
501
502 Extends :class:`EllipticCurvePrivateKey`.
503
504 .. method:: private_numbers()
505
506 Create a
507 :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateNumbers`
508 object.
509
510 :returns: An
511 :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateNumbers`
512 instance.
513
514
Alex Stapleton085f3782014-04-01 16:18:17 +0100515.. class:: EllipticCurvePublicKey
516
Alex Stapleton20c99032014-05-03 21:06:46 +0100517 .. versionadded:: 0.5
Alex Stapleton085f3782014-04-01 16:18:17 +0100518
519 An elliptic curve public key.
520
Alex Stapletone47bafb2014-05-17 13:19:15 +0100521 .. classmethod:: verifier(signature, signature_algorithm)
Alex Stapletona1853f92014-04-18 11:38:28 +0100522 Verify data was signed by the private key associated with this public
523 key.
524
Alex Gaynor93f135a2014-11-17 09:20:58 -0800525 :param bytes signature: The signature to verify. DER encoded as
526 specified in :rfc:`6979`.
Alex Stapleton80228a12014-04-20 16:44:26 +0100527
Alex Stapletona1853f92014-04-18 11:38:28 +0100528 :param signature_algorithm: An instance of a
529 :class:`~cryptography.hazmat.primitives.interfaces.EllipticCurveSignatureAlgorithm`
530 provider.
531
Alex Stapletona1853f92014-04-18 11:38:28 +0100532 :returns:
533 :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricSignatureContext`
534
Alex Stapleton085f3782014-04-01 16:18:17 +0100535 .. attribute:: curve
536
537 :type: :class:`~cryptography.hazmat.primitives.interfaces.EllipticCurve`
538
539 The elliptic curve for this key.
540
Alex Stapleton085f3782014-04-01 16:18:17 +0100541
Paul Kehrere025be22014-09-24 11:26:48 -0500542.. class:: EllipticCurvePublicKeyWithNumbers
543
544 .. versionadded:: 0.6
545
546 Extends :class:`EllipticCurvePublicKey`.
547
Paul Kehrer5cfd2112014-09-24 14:51:01 -0500548 .. method:: public_numbers()
Paul Kehrere025be22014-09-24 11:26:48 -0500549
550 Create a
551 :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicNumbers`
552 object.
553
554 :returns: An
555 :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicNumbers`
556 instance.
557
558
Alex Stapletonc5fffd32014-03-18 15:29:00 +0000559Hash algorithms
Alex Gaynor645315b2014-06-23 11:55:55 -0700560---------------
Paul Kehrere51a2db2014-01-29 11:49:35 -0600561
562.. class:: HashAlgorithm
563
Paul Kehrere51a2db2014-01-29 11:49:35 -0600564 .. attribute:: name
565
566 :type: str
567
Paul Kehrer4c75a8c2014-01-29 12:20:37 -0600568 The standard name for the hash algorithm, for example: ``"sha256"`` or
569 ``"whirlpool"``.
Paul Kehrere51a2db2014-01-29 11:49:35 -0600570
571 .. attribute:: digest_size
572
573 :type: int
574
575 The size of the resulting digest in bytes.
576
577 .. attribute:: block_size
578
579 :type: int
580
581 The internal block size of the hash algorithm in bytes.
582
583
Ayrxa0f98502014-04-15 19:17:03 +0800584.. class:: HashContext
585
586 .. attribute:: algorithm
587
588 A :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` that
589 will be used by this context.
590
591 .. method:: update(data)
592
593 :param data bytes: The data you want to hash.
594
595 .. method:: finalize()
596
597 :return: The final digest as bytes.
598
599 .. method:: copy()
600
601 :return: A :class:`~cryptography.hazmat.primitives.interfaces.HashContext`
602 that is a copy of the current context.
603
604
Alex Stapletonc5fffd32014-03-18 15:29:00 +0000605Key derivation functions
Alex Gaynor645315b2014-06-23 11:55:55 -0700606------------------------
Alex Gaynorb2774f52014-01-27 11:05:29 -0800607
608.. class:: KeyDerivationFunction
609
Alex Gaynor8454c512014-01-28 07:01:54 -0800610 .. versionadded:: 0.2
611
Alex Gaynorb2774f52014-01-27 11:05:29 -0800612 .. method:: derive(key_material)
613
Alex Gaynor5484f722014-01-28 05:46:15 -0800614 :param key_material bytes: The input key material. Depending on what
615 key derivation function you are using this
616 could be either random material, or a user
Alex Gaynorb2774f52014-01-27 11:05:29 -0800617 supplied password.
Alex Gaynor5484f722014-01-28 05:46:15 -0800618 :return: The new key.
Alex Gaynore19e89f2014-01-28 06:58:43 -0800619 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
620 :meth:`derive` or
621 :meth:`verify` is
622 called more than
623 once.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800624
Alex Gaynor5484f722014-01-28 05:46:15 -0800625 This generates and returns a new key from the supplied key material.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800626
627 .. method:: verify(key_material, expected_key)
628
Alex Gaynor5484f722014-01-28 05:46:15 -0800629 :param key_material bytes: The input key material. This is the same as
Alex Gaynorb2774f52014-01-27 11:05:29 -0800630 ``key_material`` in :meth:`derive`.
Alex Gaynor5484f722014-01-28 05:46:15 -0800631 :param expected_key bytes: The expected result of deriving a new key,
632 this is the same as the return value of
633 :meth:`derive`.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800634 :raises cryptography.exceptions.InvalidKey: This is raised when the
635 derived key does not match
636 the expected key.
Alex Gaynore19e89f2014-01-28 06:58:43 -0800637 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
638 :meth:`derive` or
639 :meth:`verify` is
640 called more than
641 once.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800642
Alex Gaynor5484f722014-01-28 05:46:15 -0800643 This checks whether deriving a new key from the supplied
644 ``key_material`` generates the same key as the ``expected_key``, and
645 raises an exception if they do not match. This can be used for
646 something like checking whether a user's password attempt matches the
647 stored derived key.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800648
Ayrxc8121702014-04-15 19:02:05 +0800649
Terry Chiacc5e4452014-10-12 15:35:21 +0800650`Message Authentication Code`_
651------------------------------
Ayrxc8121702014-04-15 19:02:05 +0800652
653.. class:: CMACContext
654
Alex Gaynor7d156882014-10-20 10:40:34 -0700655 :class:`CMACContext` has been deprecated in favor of :class:`MACContext`.
Terry Chiac7c82f32014-10-20 12:15:22 +0800656
Ayrxc8121702014-04-15 19:02:05 +0800657 .. versionadded:: 0.4
658
659 .. method:: update(data)
660
661 :param data bytes: The data you want to authenticate.
662
663 .. method:: finalize()
664
Ayrx7964c172014-04-15 21:50:58 +0800665 :return: The message authentication code.
Ayrxc8121702014-04-15 19:02:05 +0800666
667 .. method:: copy()
668
669 :return: A :class:`~cryptography.hazmat.primitives.interfaces.CMACContext`
670 that is a copy of the current context.
671
Terry Chiacc5e4452014-10-12 15:35:21 +0800672.. class:: MACContext
673
674 .. versionadded:: 0.7
675
676 .. method:: update(data)
677
Terry Chia0fc591f2014-10-12 22:21:01 +0800678 :param data bytes: The data you want to authenticate.
Terry Chiacc5e4452014-10-12 15:35:21 +0800679
680 .. method:: finalize()
681
682 :return: The message authentication code.
683
684 .. method:: copy()
685
Alex Gaynor7d156882014-10-20 10:40:34 -0700686 :return: A
687 :class:`~cryptography.hazmat.primitives.interfaces.MACContext` that
688 is a copy of the current context.
Terry Chiacc5e4452014-10-12 15:35:21 +0800689
Alex Gaynor7d156882014-10-20 10:40:34 -0700690 .. method:: verify(signature)
Terry Chiacc5e4452014-10-12 15:35:21 +0800691
692 :param signature bytes: The signature to verify.
693
694 :raises cryptography.exceptions.InvalidSignature: This is raised when
695 the provided signature does not match the expected signature.
Ayrxc8121702014-04-15 19:02:05 +0800696
Paul Kehrer8e9c9842014-02-13 12:23:27 -0600697.. _`RSA`: https://en.wikipedia.org/wiki/RSA_(cryptosystem)
698.. _`Chinese remainder theorem`: https://en.wikipedia.org/wiki/Chinese_remainder_theorem
Mohammed Attia604c78f2014-03-04 03:56:08 +0200699.. _`DSA`: https://en.wikipedia.org/wiki/Digital_Signature_Algorithm
Ayrx83cd3f82014-04-15 21:56:32 +0800700.. _`CMAC`: https://en.wikipedia.org/wiki/CMAC
Alex Stapleton085f3782014-04-01 16:18:17 +0100701.. _`ECDSA`: http://en.wikipedia.org/wiki/ECDSA
702.. _`EdDSA`: http://en.wikipedia.org/wiki/EdDSA