blob: 2d40352554fc5f11ca006469568d7eeb056d485b [file] [log] [blame]
David Reid30722b92013-11-07 13:03:39 -08001.. hazmat::
2
Paul Kehrer45efdbc2015-02-12 10:58:22 -06003.. module:: cryptography.hazmat.primitives.interfaces
4
David Reid30722b92013-11-07 13:03:39 -08005Interfaces
6==========
7
8
9``cryptography`` uses `Abstract Base Classes`_ as interfaces to describe the
David Reidbd18bcd2013-11-07 13:13:30 -080010properties and methods of most primitive constructs. Backends may also use
11this information to influence their operation. Interfaces should also be used
David Reid30722b92013-11-07 13:03:39 -080012to document argument and return types.
13
Alex Gaynore9df2942014-12-12 10:56:26 -080014.. _`Abstract Base Classes`: https://docs.python.org/3/library/abc.html
David Reid30722b92013-11-07 13:03:39 -080015
16
Alex Stapletonc5fffd32014-03-18 15:29:00 +000017Asymmetric interfaces
Alex Gaynor645315b2014-06-23 11:55:55 -070018---------------------
19
20.. class:: AsymmetricSignatureContext
21
22 .. versionadded:: 0.2
23
24 .. method:: update(data)
25
26 :param bytes data: The data you want to sign.
27
28 .. method:: finalize()
29
30 :return bytes signature: The signature.
31
32
33.. class:: AsymmetricVerificationContext
34
35 .. versionadded:: 0.2
36
37 .. method:: update(data)
38
39 :param bytes data: The data you wish to verify using the signature.
40
41 .. method:: verify()
42
43 :raises cryptography.exceptions.InvalidSignature: If the signature does
44 not validate.
45
46
Paul Kehrer64ddb7a2015-02-14 19:20:28 -060047In 0.8 the asymmetric padding interface was moved to the
48:mod:`cryptography.hazmat.primitives.asymmetric.padding` module.
Alex Gaynor645315b2014-06-23 11:55:55 -070049
Paul Kehrerd2fa7d22015-02-12 00:15:02 -060050DSA
51~~~
52
53In 0.8 the DSA key interfaces were moved to the
54:mod:`cryptography.hazmat.primitives.asymmetric.dsa` module.
55
Alex Gaynor645315b2014-06-23 11:55:55 -070056
57RSA
58~~~
Paul Kehrerac423232014-01-25 14:13:09 -060059
Alex Stapletonf79c2312014-12-30 12:50:14 +000060In 0.8 the RSA key interfaces were moved to the
61:mod:`cryptography.hazmat.primitives.asymmetric.rsa` module.
Paul Kehrerf0a48c62014-06-07 17:04:13 -050062
Alex Stapleton085f3782014-04-01 16:18:17 +010063
Alex Gaynor645315b2014-06-23 11:55:55 -070064Elliptic Curve
65~~~~~~~~~~~~~~
66
Paul Kehrer3bc87ab2015-02-12 00:01:53 -060067In 0.8 the EC key interfaces were moved to the
68:mod:`cryptography.hazmat.primitives.asymmetric.ec` module.
Paul Kehrere025be22014-09-24 11:26:48 -050069
70
Alex Stapletonc5fffd32014-03-18 15:29:00 +000071Key derivation functions
Alex Gaynor645315b2014-06-23 11:55:55 -070072------------------------
Alex Gaynorb2774f52014-01-27 11:05:29 -080073
74.. class:: KeyDerivationFunction
75
Alex Gaynor8454c512014-01-28 07:01:54 -080076 .. versionadded:: 0.2
77
Alex Gaynorb2774f52014-01-27 11:05:29 -080078 .. method:: derive(key_material)
79
Alex Gaynore85e3562014-11-22 23:26:30 -080080 :param bytes key_material: The input key material. Depending on what
Alex Gaynor5484f722014-01-28 05:46:15 -080081 key derivation function you are using this
Alex Gaynore85e3562014-11-22 23:26:30 -080082 could be either random bytes, or a user
Alex Gaynorb2774f52014-01-27 11:05:29 -080083 supplied password.
Alex Gaynor5484f722014-01-28 05:46:15 -080084 :return: The new key.
Alex Gaynore19e89f2014-01-28 06:58:43 -080085 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
86 :meth:`derive` or
87 :meth:`verify` is
88 called more than
89 once.
Alex Gaynorb2774f52014-01-27 11:05:29 -080090
Alex Gaynor5484f722014-01-28 05:46:15 -080091 This generates and returns a new key from the supplied key material.
Alex Gaynorb2774f52014-01-27 11:05:29 -080092
93 .. method:: verify(key_material, expected_key)
94
Alex Gaynore85e3562014-11-22 23:26:30 -080095 :param bytes key_material: The input key material. This is the same as
Alex Gaynorb2774f52014-01-27 11:05:29 -080096 ``key_material`` in :meth:`derive`.
Alex Gaynore85e3562014-11-22 23:26:30 -080097 :param bytes expected_key: The expected result of deriving a new key,
Alex Gaynor5484f722014-01-28 05:46:15 -080098 this is the same as the return value of
99 :meth:`derive`.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800100 :raises cryptography.exceptions.InvalidKey: This is raised when the
101 derived key does not match
102 the expected key.
Alex Gaynore19e89f2014-01-28 06:58:43 -0800103 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
104 :meth:`derive` or
105 :meth:`verify` is
106 called more than
107 once.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800108
Alex Gaynor5484f722014-01-28 05:46:15 -0800109 This checks whether deriving a new key from the supplied
110 ``key_material`` generates the same key as the ``expected_key``, and
111 raises an exception if they do not match. This can be used for
112 something like checking whether a user's password attempt matches the
113 stored derived key.
Alex Gaynorb2774f52014-01-27 11:05:29 -0800114
Ayrxc8121702014-04-15 19:02:05 +0800115
Terry Chiacc5e4452014-10-12 15:35:21 +0800116`Message Authentication Code`_
117------------------------------
Ayrxc8121702014-04-15 19:02:05 +0800118
119.. class:: CMACContext
120
Alex Gaynor7d156882014-10-20 10:40:34 -0700121 :class:`CMACContext` has been deprecated in favor of :class:`MACContext`.
Terry Chiac7c82f32014-10-20 12:15:22 +0800122
Ayrxc8121702014-04-15 19:02:05 +0800123 .. versionadded:: 0.4
124
125 .. method:: update(data)
126
Alex Gaynore85e3562014-11-22 23:26:30 -0800127 :param bytes data: The data you want to authenticate.
Ayrxc8121702014-04-15 19:02:05 +0800128
129 .. method:: finalize()
130
Ayrx7964c172014-04-15 21:50:58 +0800131 :return: The message authentication code.
Ayrxc8121702014-04-15 19:02:05 +0800132
133 .. method:: copy()
134
135 :return: A :class:`~cryptography.hazmat.primitives.interfaces.CMACContext`
136 that is a copy of the current context.
137
Terry Chiacc5e4452014-10-12 15:35:21 +0800138.. class:: MACContext
139
140 .. versionadded:: 0.7
141
142 .. method:: update(data)
143
Alex Gaynore85e3562014-11-22 23:26:30 -0800144 :param bytes data: The data you want to authenticate.
Terry Chiacc5e4452014-10-12 15:35:21 +0800145
146 .. method:: finalize()
147
148 :return: The message authentication code.
149
150 .. method:: copy()
151
Alex Gaynor7d156882014-10-20 10:40:34 -0700152 :return: A
153 :class:`~cryptography.hazmat.primitives.interfaces.MACContext` that
154 is a copy of the current context.
Terry Chiacc5e4452014-10-12 15:35:21 +0800155
Alex Gaynor7d156882014-10-20 10:40:34 -0700156 .. method:: verify(signature)
Terry Chiacc5e4452014-10-12 15:35:21 +0800157
Alex Gaynore85e3562014-11-22 23:26:30 -0800158 :param bytes signature: The signature to verify.
Terry Chiacc5e4452014-10-12 15:35:21 +0800159
160 :raises cryptography.exceptions.InvalidSignature: This is raised when
161 the provided signature does not match the expected signature.
Ayrxc8121702014-04-15 19:02:05 +0800162
Paul Kehrer05c122b2014-11-24 08:41:05 -1000163
Ayrx83cd3f82014-04-15 21:56:32 +0800164.. _`CMAC`: https://en.wikipedia.org/wiki/CMAC