blob: 6029d1a9cfa2414970ce84e459b426f49444493c [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
Paul Kehrer0d6203f2015-02-16 13:17:14 -060020In 0.8 the asymmetric signature and verification interfaces were moved to the
21:mod:`cryptography.hazmat.primitives.asymmetric` module.
Alex Gaynor645315b2014-06-23 11:55:55 -070022
Paul Kehrer64ddb7a2015-02-14 19:20:28 -060023In 0.8 the asymmetric padding interface was moved to the
24:mod:`cryptography.hazmat.primitives.asymmetric.padding` module.
Alex Gaynor645315b2014-06-23 11:55:55 -070025
Paul Kehrerd2fa7d22015-02-12 00:15:02 -060026DSA
27~~~
28
29In 0.8 the DSA key interfaces were moved to the
30:mod:`cryptography.hazmat.primitives.asymmetric.dsa` module.
31
Alex Gaynor645315b2014-06-23 11:55:55 -070032
33RSA
34~~~
Paul Kehrerac423232014-01-25 14:13:09 -060035
Alex Stapletonf79c2312014-12-30 12:50:14 +000036In 0.8 the RSA key interfaces were moved to the
37:mod:`cryptography.hazmat.primitives.asymmetric.rsa` module.
Paul Kehrerf0a48c62014-06-07 17:04:13 -050038
Alex Stapleton085f3782014-04-01 16:18:17 +010039
Alex Gaynor645315b2014-06-23 11:55:55 -070040Elliptic Curve
41~~~~~~~~~~~~~~
42
Paul Kehrer3bc87ab2015-02-12 00:01:53 -060043In 0.8 the EC key interfaces were moved to the
44:mod:`cryptography.hazmat.primitives.asymmetric.ec` module.
Paul Kehrere025be22014-09-24 11:26:48 -050045
46
Alex Stapletonc5fffd32014-03-18 15:29:00 +000047Key derivation functions
Alex Gaynor645315b2014-06-23 11:55:55 -070048------------------------
Alex Gaynorb2774f52014-01-27 11:05:29 -080049
50.. class:: KeyDerivationFunction
51
Alex Gaynor8454c512014-01-28 07:01:54 -080052 .. versionadded:: 0.2
53
Alex Gaynorb2774f52014-01-27 11:05:29 -080054 .. method:: derive(key_material)
55
Alex Gaynore85e3562014-11-22 23:26:30 -080056 :param bytes key_material: The input key material. Depending on what
Alex Gaynor5484f722014-01-28 05:46:15 -080057 key derivation function you are using this
Alex Gaynore85e3562014-11-22 23:26:30 -080058 could be either random bytes, or a user
Alex Gaynorb2774f52014-01-27 11:05:29 -080059 supplied password.
Alex Gaynor5484f722014-01-28 05:46:15 -080060 :return: The new key.
Alex Gaynore19e89f2014-01-28 06:58:43 -080061 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
62 :meth:`derive` or
63 :meth:`verify` is
64 called more than
65 once.
Alex Gaynorb2774f52014-01-27 11:05:29 -080066
Alex Gaynor5484f722014-01-28 05:46:15 -080067 This generates and returns a new key from the supplied key material.
Alex Gaynorb2774f52014-01-27 11:05:29 -080068
69 .. method:: verify(key_material, expected_key)
70
Alex Gaynore85e3562014-11-22 23:26:30 -080071 :param bytes key_material: The input key material. This is the same as
Alex Gaynorb2774f52014-01-27 11:05:29 -080072 ``key_material`` in :meth:`derive`.
Alex Gaynore85e3562014-11-22 23:26:30 -080073 :param bytes expected_key: The expected result of deriving a new key,
Alex Gaynor5484f722014-01-28 05:46:15 -080074 this is the same as the return value of
75 :meth:`derive`.
Alex Gaynorb2774f52014-01-27 11:05:29 -080076 :raises cryptography.exceptions.InvalidKey: This is raised when the
77 derived key does not match
78 the expected key.
Alex Gaynore19e89f2014-01-28 06:58:43 -080079 :raises cryptography.exceptions.AlreadyFinalized: This is raised when
80 :meth:`derive` or
81 :meth:`verify` is
82 called more than
83 once.
Alex Gaynorb2774f52014-01-27 11:05:29 -080084
Alex Gaynor5484f722014-01-28 05:46:15 -080085 This checks whether deriving a new key from the supplied
86 ``key_material`` generates the same key as the ``expected_key``, and
87 raises an exception if they do not match. This can be used for
88 something like checking whether a user's password attempt matches the
89 stored derived key.
Alex Gaynorb2774f52014-01-27 11:05:29 -080090
Ayrxc8121702014-04-15 19:02:05 +080091
Terry Chiacc5e4452014-10-12 15:35:21 +080092`Message Authentication Code`_
93------------------------------
Ayrxc8121702014-04-15 19:02:05 +080094
95.. class:: CMACContext
96
Alex Gaynor7d156882014-10-20 10:40:34 -070097 :class:`CMACContext` has been deprecated in favor of :class:`MACContext`.
Terry Chiac7c82f32014-10-20 12:15:22 +080098
Ayrxc8121702014-04-15 19:02:05 +080099 .. versionadded:: 0.4
100
101 .. method:: update(data)
102
Alex Gaynore85e3562014-11-22 23:26:30 -0800103 :param bytes data: The data you want to authenticate.
Ayrxc8121702014-04-15 19:02:05 +0800104
105 .. method:: finalize()
106
Ayrx7964c172014-04-15 21:50:58 +0800107 :return: The message authentication code.
Ayrxc8121702014-04-15 19:02:05 +0800108
109 .. method:: copy()
110
111 :return: A :class:`~cryptography.hazmat.primitives.interfaces.CMACContext`
112 that is a copy of the current context.
113
Terry Chiacc5e4452014-10-12 15:35:21 +0800114.. class:: MACContext
115
116 .. versionadded:: 0.7
117
118 .. method:: update(data)
119
Alex Gaynore85e3562014-11-22 23:26:30 -0800120 :param bytes data: The data you want to authenticate.
Terry Chiacc5e4452014-10-12 15:35:21 +0800121
122 .. method:: finalize()
123
124 :return: The message authentication code.
125
126 .. method:: copy()
127
Alex Gaynor7d156882014-10-20 10:40:34 -0700128 :return: A
129 :class:`~cryptography.hazmat.primitives.interfaces.MACContext` that
130 is a copy of the current context.
Terry Chiacc5e4452014-10-12 15:35:21 +0800131
Alex Gaynor7d156882014-10-20 10:40:34 -0700132 .. method:: verify(signature)
Terry Chiacc5e4452014-10-12 15:35:21 +0800133
Alex Gaynore85e3562014-11-22 23:26:30 -0800134 :param bytes signature: The signature to verify.
Terry Chiacc5e4452014-10-12 15:35:21 +0800135
136 :raises cryptography.exceptions.InvalidSignature: This is raised when
137 the provided signature does not match the expected signature.
Ayrxc8121702014-04-15 19:02:05 +0800138
Paul Kehrer05c122b2014-11-24 08:41:05 -1000139
Ayrx83cd3f82014-04-15 21:56:32 +0800140.. _`CMAC`: https://en.wikipedia.org/wiki/CMAC