blob: 6ec6de62b61225470606870a546d60506a06bc17 [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 Kehrer27f9ca62014-04-15 17:59:27 -0400136 .. method:: decrypt(ciphertext, padding, backend)
137
138 .. versionadded:: 0.4
139
140 Decrypt data that was encrypted via the public key.
141
142 :param bytes ciphertext: The ciphertext to decrypt.
143
144 :param padding: An instance of a
145 :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricPadding`
146 provider.
147
148 :param backend: A
149 :class:`~cryptography.hazmat.backends.interfaces.RSABackend`
150 provider.
151
152 :return bytes: Decrypted data.
153
Paul Kehrer0e94fbe2014-01-26 11:47:21 -0600154 .. method:: public_key()
Paul Kehrerac423232014-01-25 14:13:09 -0600155
Paul Kehrer359b9462014-01-26 12:03:05 -0600156 :return: :class:`~cryptography.hazmat.primitives.interfaces.RSAPublicKey`
Paul Kehrerac423232014-01-25 14:13:09 -0600157
158 An RSA public key object corresponding to the values of the private key.
159
160 .. attribute:: modulus
161
Paul Kehrerd527b302014-01-26 11:41:38 -0600162 :type: int
Paul Kehrerac423232014-01-25 14:13:09 -0600163
Paul Kehrer0e94fbe2014-01-26 11:47:21 -0600164 The public modulus.
Paul Kehrerac423232014-01-25 14:13:09 -0600165
166 .. attribute:: public_exponent
167
168 :type: int
169
Paul Kehrer0e94fbe2014-01-26 11:47:21 -0600170 The public exponent.
Paul Kehrerac423232014-01-25 14:13:09 -0600171
Alex Gaynor2649a692014-02-03 07:14:16 -0800172 .. attribute:: private_exponent
173
174 :type: int
175
176 The private exponent.
177
Alex Stapletonee3e6bf2014-02-02 21:13:48 +0000178 .. attribute:: key_size
Paul Kehrerac423232014-01-25 14:13:09 -0600179
180 :type: int
181
182 The bit length of the modulus.
183
184 .. attribute:: p
185
Paul Kehrerd527b302014-01-26 11:41:38 -0600186 :type: int
Paul Kehrerac423232014-01-25 14:13:09 -0600187
Alex Gaynor2a918742014-01-26 16:53:44 -0600188 ``p``, one of the two primes composing the :attr:`modulus`.
Paul Kehrerac423232014-01-25 14:13:09 -0600189
190 .. attribute:: q
191
Paul Kehrerd527b302014-01-26 11:41:38 -0600192 :type: int
Paul Kehrerac423232014-01-25 14:13:09 -0600193
Alex Gaynor2a918742014-01-26 16:53:44 -0600194 ``q``, one of the two primes composing the :attr:`modulus`.
Paul Kehrerac423232014-01-25 14:13:09 -0600195
196 .. attribute:: d
197
Paul Kehrerd527b302014-01-26 11:41:38 -0600198 :type: int
Paul Kehrerac423232014-01-25 14:13:09 -0600199
Alex Gaynor2649a692014-02-03 07:14:16 -0800200 The private exponent. Alias for :attr:`private_exponent`.
Paul Kehrerac423232014-01-25 14:13:09 -0600201
Paul Kehrer8e9c9842014-02-13 12:23:27 -0600202 .. attribute:: dmp1
203
204 :type: int
205
206 A `Chinese remainder theorem`_ coefficient used to speed up RSA
207 operations. Calculated as: d mod (p-1)
208
209 .. attribute:: dmq1
210
211 :type: int
212
213 A `Chinese remainder theorem`_ coefficient used to speed up RSA
214 operations. Calculated as: d mod (q-1)
215
216 .. attribute:: iqmp
217
218 :type: int
219
220 A `Chinese remainder theorem`_ coefficient used to speed up RSA
221 operations. Calculated as: q\ :sup:`-1` mod p
222
Paul Kehrerac423232014-01-25 14:13:09 -0600223 .. attribute:: n
224
Paul Kehrerd527b302014-01-26 11:41:38 -0600225 :type: int
Paul Kehrerac423232014-01-25 14:13:09 -0600226
Alex Gaynor2a918742014-01-26 16:53:44 -0600227 The public modulus. Alias for :attr:`modulus`.
Paul Kehrerac423232014-01-25 14:13:09 -0600228
229 .. attribute:: e
230
231 :type: int
232
Alex Gaynor2a918742014-01-26 16:53:44 -0600233 The public exponent. Alias for :attr:`public_exponent`.
Paul Kehrerac423232014-01-25 14:13:09 -0600234
235
236.. class:: RSAPublicKey
237
Paul Kehrer46688b12014-01-26 13:23:13 -0600238 .. versionadded:: 0.2
Paul Kehrer82629f42014-01-26 12:25:02 -0600239
Paul Kehrerac423232014-01-25 14:13:09 -0600240 An `RSA`_ public key.
241
Paul Kehrer01cdfb22014-04-15 11:27:03 -0400242 .. method:: verifier(signature, padding, algorithm, backend)
243
244 .. versionadded:: 0.3
245
246 Verify data was signed by the private key associated with this public
247 key.
248
249 :param bytes signature: The signature to verify.
250
251 :param padding: An instance of a
252 :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricPadding`
253 provider.
254
255 :param algorithm: An instance of a
256 :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
257 provider.
258
259 :param backend: A
260 :class:`~cryptography.hazmat.backends.interfaces.RSABackend`
261 provider.
262
263 :returns:
264 :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricVerificationContext`
265
Paul Kehrer4e602f32014-04-24 12:07:54 -0500266 .. method:: encrypt(plaintext, padding, backend)
267
268 .. versionadded:: 0.4
269
270 Encrypt data with the public key.
271
272 :param bytes plaintext: The plaintext to encrypt.
273
274 :param padding: An instance of a
275 :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricPadding`
276 provider.
277
278 :param backend: A
279 :class:`~cryptography.hazmat.backends.interfaces.RSABackend`
280 provider.
281
282 :return bytes: Encrypted data.
Paul Kehrer01cdfb22014-04-15 11:27:03 -0400283
Paul Kehrerac423232014-01-25 14:13:09 -0600284 .. attribute:: modulus
285
Paul Kehrerd527b302014-01-26 11:41:38 -0600286 :type: int
Paul Kehrerac423232014-01-25 14:13:09 -0600287
Paul Kehrer0e94fbe2014-01-26 11:47:21 -0600288 The public modulus.
Paul Kehrerac423232014-01-25 14:13:09 -0600289
Alex Stapletonee3e6bf2014-02-02 21:13:48 +0000290 .. attribute:: key_size
Paul Kehrerac423232014-01-25 14:13:09 -0600291
292 :type: int
293
294 The bit length of the modulus.
295
296 .. attribute:: public_exponent
297
298 :type: int
299
Paul Kehrer0e94fbe2014-01-26 11:47:21 -0600300 The public exponent.
Paul Kehrerac423232014-01-25 14:13:09 -0600301
302 .. attribute:: n
303
Paul Kehrerd527b302014-01-26 11:41:38 -0600304 :type: int
Paul Kehrerac423232014-01-25 14:13:09 -0600305
Alex Gaynor2a918742014-01-26 16:53:44 -0600306 The public modulus. Alias for :attr:`modulus`.
Paul Kehrerac423232014-01-25 14:13:09 -0600307
308 .. attribute:: e
309
310 :type: int
311
Alex Gaynor2a918742014-01-26 16:53:44 -0600312 The public exponent. Alias for :attr:`public_exponent`.
313
Paul Kehrerac423232014-01-25 14:13:09 -0600314
Mohammed Attia71acc672014-03-04 19:20:45 +0200315.. class:: DSAParameters
Mohammed Attiab4167152014-03-04 03:29:56 +0200316
317 .. versionadded:: 0.3
318
319 `DSA`_ parameters.
320
321 .. attribute:: modulus
322
323 :type: int
324
Mohammed Attia7a1738a2014-03-04 19:17:24 +0200325 The prime modulus that is used in generating the DSA key pair and used
Mohammed Attiab4167152014-03-04 03:29:56 +0200326 in the DSA signing and verification processes.
327
328 .. attribute:: subgroup_order
329
330 :type: int
331
Mohammed Attia7a1738a2014-03-04 19:17:24 +0200332 The subgroup order that is used in generating the DSA key pair
Mohammed Attiab4167152014-03-04 03:29:56 +0200333 by the generator and used in the DSA signing and verification
334 processes.
335
336 .. attribute:: generator
337
338 :type: int
339
Mohammed Attiacb9a6c22014-03-04 04:16:35 +0200340 The generator that is used in generating the DSA key pair and used
Mohammed Attia7a1738a2014-03-04 19:17:24 +0200341 in the DSA signing and verification processes.
Mohammed Attiab4167152014-03-04 03:29:56 +0200342
343 .. attribute:: p
344
345 :type: int
346
Mohammed Attia7a1738a2014-03-04 19:17:24 +0200347 The prime modulus that is used in generating the DSA key pair and used
Mohammed Attia70324512014-03-04 03:34:39 +0200348 in the DSA signing and verification processes. Alias for :attr:`modulus`.
Mohammed Attiab4167152014-03-04 03:29:56 +0200349
350 .. attribute:: q
351
352 :type: int
353
Mohammed Attia7a1738a2014-03-04 19:17:24 +0200354 The subgroup order that is used in generating the DSA key pair
Mohammed Attiab4167152014-03-04 03:29:56 +0200355 by the generator and used in the DSA signing and verification
Mohammed Attia70324512014-03-04 03:34:39 +0200356 processes. Alias for :attr:`subgroup_order`.
Mohammed Attiab4167152014-03-04 03:29:56 +0200357
358 .. attribute:: g
359
360 :type: int
361
Mohammed Attiacb9a6c22014-03-04 04:16:35 +0200362 The generator that is used in generating the DSA key pair and used
Mohammed Attia70324512014-03-04 03:34:39 +0200363 in the DSA signing and verification processes. Alias for :attr:`generator`.
Mohammed Attiab4167152014-03-04 03:29:56 +0200364
365
366.. class:: DSAPrivateKey
367
368 .. versionadded:: 0.3
369
Mohammed Attia7a1738a2014-03-04 19:17:24 +0200370 A `DSA`_ private key.
Mohammed Attiab4167152014-03-04 03:29:56 +0200371
372 .. method:: public_key()
373
374 :return: :class:`~cryptography.hazmat.primitives.interfaces.DSAPublicKey`
375
376 An DSA public key object corresponding to the values of the private key.
377
378 .. method:: parameters()
379
Mohammed Attia71acc672014-03-04 19:20:45 +0200380 :return: :class:`~cryptography.hazmat.primitives.interfaces.DSAParameters`
Mohammed Attiab4167152014-03-04 03:29:56 +0200381
Mohammed Attia71acc672014-03-04 19:20:45 +0200382 The DSAParameters object associated with this private key.
Mohammed Attiab4167152014-03-04 03:29:56 +0200383
Paul Kehrer0b3ff3b2014-05-01 15:34:42 -0500384 .. method:: signer(algorithm, backend)
385
386 .. versionadded:: 0.4
387
388 Sign data which can be verified later by others using the public key.
389
390 :param algorithm: An instance of a
391 :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
392 provider.
393
394 :param backend: A
395 :class:`~cryptography.hazmat.backends.interfaces.DSABackend`
396 provider.
397
398 :returns:
399 :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricSignatureContext`
400
Mohammed Attiab4167152014-03-04 03:29:56 +0200401 .. attribute:: key_size
402
403 :type: int
404
405 The bit length of the modulus.
406
407 .. attribute:: x
408
409 :type: int
410
411 The private key.
412
413 .. attribute:: y
414
415 :type: int
416
417 The public key.
418
419
420.. class:: DSAPublicKey
421
422 .. versionadded:: 0.3
423
Mohammed Attiaedacb142014-03-17 12:28:23 +0200424 A `DSA`_ public key.
425
426 .. attribute:: key_size
427
428 :type: int
429
430 The bit length of the modulus.
Mohammed Attiab4167152014-03-04 03:29:56 +0200431
Mohammed Attia59edb612014-04-25 22:44:40 +0200432 .. attribute:: y
433
434 :type: int
435
436 The public key.
437
Mohammed Attiab4167152014-03-04 03:29:56 +0200438 .. method:: parameters()
439
Mohammed Attia71acc672014-03-04 19:20:45 +0200440 :return: :class:`~cryptography.hazmat.primitives.interfaces.DSAParameters`
Mohammed Attiab4167152014-03-04 03:29:56 +0200441
Mohammed Attia71acc672014-03-04 19:20:45 +0200442 The DSAParameters object associated with this public key.
Mohammed Attiab4167152014-03-04 03:29:56 +0200443
Mohammed Attia59edb612014-04-25 22:44:40 +0200444 .. method:: verifier(signature, algorithm, backend)
Mohammed Attiab4167152014-03-04 03:29:56 +0200445
Mohammed Attia59edb612014-04-25 22:44:40 +0200446 .. versionadded:: 0.4
Mohammed Attiab4167152014-03-04 03:29:56 +0200447
Mohammed Attia59edb612014-04-25 22:44:40 +0200448 Verify data was signed by the private key associated with this public
449 key.
450
Paul Kehrere0aeaf82014-05-01 11:58:23 -0500451 :param bytes signature: The signature to verify. DER encoded as
452 specified in :rfc:`6979`.
Mohammed Attia59edb612014-04-25 22:44:40 +0200453
454 :param algorithm: An instance of a
455 :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm`
456 provider.
457
458 :param backend: A
459 :class:`~cryptography.hazmat.backends.interfaces.DSABackend`
460 provider.
461
462 :returns:
463 :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricVerificationContext`
Mohammed Attiab4167152014-03-04 03:29:56 +0200464
465
Alex Stapleton085f3782014-04-01 16:18:17 +0100466.. class:: EllipticCurve
467
468 .. versionadded:: 0.4
469
470 A named elliptic curve.
471
472 .. attribute:: name
473
474 :type: string
475
476 The name of the curve. Usually the name used for the ASN.1 OID such as
477 "secp256k1".
478
479 .. attribute:: key_size
480
481 :type: int
482
483 The bit length of the curves base point.
484
485
486.. class:: EllipticCurvePrivateKey
487
488 .. versionadded:: 0.4
489
490 An elliptic curve private key for use with an algorithm such as `ECDSA`_ or
491 `EdDSA`_.
492
493 .. attribute:: curve
494
495 :type: :class:`~cryptography.hazmat.primitives.interfaces.EllipticCurve`
496
497 The elliptic curve for this key.
498
499 .. attribute:: private_key
500
501 :type: int
502
503 The private key.
504
505 .. attribute:: key_size
506
507 :type: int
508
509 The bit length of the curves base point.
510
511 .. attribute:: x
512
513 :type: int
514
515 The affine x component of the public point used for verifying.
516
517 .. attribute:: y
518
519 :type: int
520
521 The affine y component of the public point used for verifying.
522
523 .. method:: public_key()
524
525 :return: :class:`~cryptography.hazmat.primitives.interfaces.EllipticCurvePublicKey`
526
527 The EllipticCurvePublicKey object for this private key.
528
529
530.. class:: EllipticCurvePublicKey
531
532 .. versionadded:: 0.4
533
534 An elliptic curve public key.
535
536 .. attribute:: curve
537
538 :type: :class:`~cryptography.hazmat.primitives.interfaces.EllipticCurve`
539
540 The elliptic curve for this key.
541
542 .. attribute:: x
543
544 :type: int
545
546 The affine x component of the public point used for verifying.
547
548 .. attribute:: y
549
550 :type: int
551
552 The affine y component of the public point used for verifying.
553
554 .. attribute:: key_size
555
556 :type: int
557
558 The bit length of the curves base point.
559
560
Paul Kehrereda558c2014-02-17 21:18:13 -0600561.. class:: AsymmetricSignatureContext
Paul Kehrere0f0f342014-02-17 19:20:51 -0600562
563 .. versionadded:: 0.2
564
565 .. method:: update(data)
566
Paul Kehrereda558c2014-02-17 21:18:13 -0600567 :param bytes data: The data you want to sign.
Paul Kehrere0f0f342014-02-17 19:20:51 -0600568
569 .. method:: finalize()
570
571 :return bytes signature: The signature.
572
573
Paul Kehrer430202d2014-02-18 13:36:53 -0600574.. class:: AsymmetricVerificationContext
Paul Kehrere0f0f342014-02-17 19:20:51 -0600575
576 .. versionadded:: 0.2
577
578 .. method:: update(data)
579
Paul Kehrereda558c2014-02-17 21:18:13 -0600580 :param bytes data: The data you wish to verify using the signature.
Paul Kehrere0f0f342014-02-17 19:20:51 -0600581
Paul Kehrerdd3780a2014-02-18 13:17:53 -0600582 .. method:: verify()
Paul Kehrere0f0f342014-02-17 19:20:51 -0600583
Paul Kehrerfef1fbd2014-02-26 23:39:37 -0400584 :raises cryptography.exceptions.InvalidSignature: If the signature does
585 not validate.
Paul Kehrere0f0f342014-02-17 19:20:51 -0600586
587
588.. class:: AsymmetricPadding
589
Paul Kehrer19f32d52014-02-17 19:23:06 -0600590 .. versionadded:: 0.2
Paul Kehrere0f0f342014-02-17 19:20:51 -0600591
592 .. attribute:: name
593
Alex Stapletonc5fffd32014-03-18 15:29:00 +0000594Hash algorithms
Paul Kehrere51a2db2014-01-29 11:49:35 -0600595~~~~~~~~~~~~~~~
596
597.. class:: HashAlgorithm
598
Paul Kehrere51a2db2014-01-29 11:49:35 -0600599 .. attribute:: name
600
601 :type: str
602
Paul Kehrer4c75a8c2014-01-29 12:20:37 -0600603 The standard name for the hash algorithm, for example: ``"sha256"`` or
604 ``"whirlpool"``.
Paul Kehrere51a2db2014-01-29 11:49:35 -0600605
606 .. attribute:: digest_size
607
608 :type: int
609
610 The size of the resulting digest in bytes.
611
612 .. attribute:: block_size
613
614 :type: int
615
616 The internal block size of the hash algorithm in bytes.
617
618
Ayrxa0f98502014-04-15 19:17:03 +0800619.. class:: HashContext
620
621 .. attribute:: algorithm
622
623 A :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` that
624 will be used by this context.
625
626 .. method:: update(data)
627
628 :param data bytes: The data you want to hash.
629
630 .. method:: finalize()
631
632 :return: The final digest as bytes.
633
634 .. method:: copy()
635
636 :return: A :class:`~cryptography.hazmat.primitives.interfaces.HashContext`
637 that is a copy of the current context.
638
639
Alex Stapletonc5fffd32014-03-18 15:29:00 +0000640Key derivation functions
Alex Gaynorb2774f52014-01-27 11:05:29 -0800641~~~~~~~~~~~~~~~~~~~~~~~~
642
643.. class:: KeyDerivationFunction
644
Alex Gaynor8454c512014-01-28 07:01:54 -0800645 .. versionadded:: 0.2
646
Alex Gaynorb2774f52014-01-27 11:05:29 -0800647 .. method:: derive(key_material)
648
Alex Gaynor5484f722014-01-28 05:46:15 -0800649 :param key_material bytes: The input key material. Depending on what
650 key derivation function you are using this
651 could be either random material, or a user
Alex Gaynorb2774f52014-01-27 11:05:29 -0800652 supplied password.
Alex Gaynor5484f722014-01-28 05:46:15 -0800653 :return: The new key.
Alex Gaynore19e89f2014-01-28 06:58:43 -0800654 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
655 :meth:`derive` or
656 :meth:`verify` is
657 called more than
658 once.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800659
Alex Gaynor5484f722014-01-28 05:46:15 -0800660 This generates and returns a new key from the supplied key material.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800661
662 .. method:: verify(key_material, expected_key)
663
Alex Gaynor5484f722014-01-28 05:46:15 -0800664 :param key_material bytes: The input key material. This is the same as
Alex Gaynorb2774f52014-01-27 11:05:29 -0800665 ``key_material`` in :meth:`derive`.
Alex Gaynor5484f722014-01-28 05:46:15 -0800666 :param expected_key bytes: The expected result of deriving a new key,
667 this is the same as the return value of
668 :meth:`derive`.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800669 :raises cryptography.exceptions.InvalidKey: This is raised when the
670 derived key does not match
671 the expected key.
Alex Gaynore19e89f2014-01-28 06:58:43 -0800672 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
673 :meth:`derive` or
674 :meth:`verify` is
675 called more than
676 once.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800677
Alex Gaynor5484f722014-01-28 05:46:15 -0800678 This checks whether deriving a new key from the supplied
679 ``key_material`` generates the same key as the ``expected_key``, and
680 raises an exception if they do not match. This can be used for
681 something like checking whether a user's password attempt matches the
682 stored derived key.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800683
Ayrxc8121702014-04-15 19:02:05 +0800684
Ayrx83cd3f82014-04-15 21:56:32 +0800685`CMAC`_
686~~~~~~~
Ayrxc8121702014-04-15 19:02:05 +0800687
688.. class:: CMACContext
689
690 .. versionadded:: 0.4
691
692 .. method:: update(data)
693
694 :param data bytes: The data you want to authenticate.
695
696 .. method:: finalize()
697
Ayrx7964c172014-04-15 21:50:58 +0800698 :return: The message authentication code.
Ayrxc8121702014-04-15 19:02:05 +0800699
700 .. method:: copy()
701
702 :return: A :class:`~cryptography.hazmat.primitives.interfaces.CMACContext`
703 that is a copy of the current context.
704
705
Paul Kehrer8e9c9842014-02-13 12:23:27 -0600706.. _`RSA`: https://en.wikipedia.org/wiki/RSA_(cryptosystem)
707.. _`Chinese remainder theorem`: https://en.wikipedia.org/wiki/Chinese_remainder_theorem
Mohammed Attia604c78f2014-03-04 03:56:08 +0200708.. _`DSA`: https://en.wikipedia.org/wiki/Digital_Signature_Algorithm
Ayrx83cd3f82014-04-15 21:56:32 +0800709.. _`CMAC`: https://en.wikipedia.org/wiki/CMAC
Alex Stapleton085f3782014-04-01 16:18:17 +0100710.. _`ECDSA`: http://en.wikipedia.org/wiki/ECDSA
711.. _`EdDSA`: http://en.wikipedia.org/wiki/EdDSA