David Reid | 30722b9 | 2013-11-07 13:03:39 -0800 | [diff] [blame] | 1 | .. hazmat:: |
| 2 | |
Paul Kehrer | 45efdbc | 2015-02-12 10:58:22 -0600 | [diff] [blame] | 3 | .. module:: cryptography.hazmat.primitives.interfaces |
| 4 | |
David Reid | 30722b9 | 2013-11-07 13:03:39 -0800 | [diff] [blame] | 5 | Interfaces |
| 6 | ========== |
| 7 | |
| 8 | |
| 9 | ``cryptography`` uses `Abstract Base Classes`_ as interfaces to describe the |
David Reid | bd18bcd | 2013-11-07 13:13:30 -0800 | [diff] [blame] | 10 | properties and methods of most primitive constructs. Backends may also use |
| 11 | this information to influence their operation. Interfaces should also be used |
David Reid | 30722b9 | 2013-11-07 13:03:39 -0800 | [diff] [blame] | 12 | to document argument and return types. |
| 13 | |
Alex Gaynor | e9df294 | 2014-12-12 10:56:26 -0800 | [diff] [blame] | 14 | .. _`Abstract Base Classes`: https://docs.python.org/3/library/abc.html |
David Reid | 30722b9 | 2013-11-07 13:03:39 -0800 | [diff] [blame] | 15 | |
| 16 | |
Alex Stapleton | c5fffd3 | 2014-03-18 15:29:00 +0000 | [diff] [blame] | 17 | Asymmetric interfaces |
Alex Gaynor | 645315b | 2014-06-23 11:55:55 -0700 | [diff] [blame] | 18 | --------------------- |
| 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 | |
| 47 | .. class:: AsymmetricPadding |
| 48 | |
| 49 | .. versionadded:: 0.2 |
| 50 | |
| 51 | .. attribute:: name |
| 52 | |
Paul Kehrer | d2fa7d2 | 2015-02-12 00:15:02 -0600 | [diff] [blame] | 53 | DSA |
| 54 | ~~~ |
| 55 | |
| 56 | In 0.8 the DSA key interfaces were moved to the |
| 57 | :mod:`cryptography.hazmat.primitives.asymmetric.dsa` module. |
| 58 | |
Alex Gaynor | 645315b | 2014-06-23 11:55:55 -0700 | [diff] [blame] | 59 | |
| 60 | RSA |
| 61 | ~~~ |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 62 | |
Alex Stapleton | f79c231 | 2014-12-30 12:50:14 +0000 | [diff] [blame] | 63 | In 0.8 the RSA key interfaces were moved to the |
| 64 | :mod:`cryptography.hazmat.primitives.asymmetric.rsa` module. |
Paul Kehrer | f0a48c6 | 2014-06-07 17:04:13 -0500 | [diff] [blame] | 65 | |
Alex Stapleton | 085f378 | 2014-04-01 16:18:17 +0100 | [diff] [blame] | 66 | |
Alex Gaynor | 645315b | 2014-06-23 11:55:55 -0700 | [diff] [blame] | 67 | Elliptic Curve |
| 68 | ~~~~~~~~~~~~~~ |
| 69 | |
Paul Kehrer | 3bc87ab | 2015-02-12 00:01:53 -0600 | [diff] [blame] | 70 | In 0.8 the EC key interfaces were moved to the |
| 71 | :mod:`cryptography.hazmat.primitives.asymmetric.ec` module. |
Paul Kehrer | e025be2 | 2014-09-24 11:26:48 -0500 | [diff] [blame] | 72 | |
| 73 | |
Alex Stapleton | c5fffd3 | 2014-03-18 15:29:00 +0000 | [diff] [blame] | 74 | Key derivation functions |
Alex Gaynor | 645315b | 2014-06-23 11:55:55 -0700 | [diff] [blame] | 75 | ------------------------ |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 76 | |
| 77 | .. class:: KeyDerivationFunction |
| 78 | |
Alex Gaynor | 8454c51 | 2014-01-28 07:01:54 -0800 | [diff] [blame] | 79 | .. versionadded:: 0.2 |
| 80 | |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 81 | .. method:: derive(key_material) |
| 82 | |
Alex Gaynor | e85e356 | 2014-11-22 23:26:30 -0800 | [diff] [blame] | 83 | :param bytes key_material: The input key material. Depending on what |
Alex Gaynor | 5484f72 | 2014-01-28 05:46:15 -0800 | [diff] [blame] | 84 | key derivation function you are using this |
Alex Gaynor | e85e356 | 2014-11-22 23:26:30 -0800 | [diff] [blame] | 85 | could be either random bytes, or a user |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 86 | supplied password. |
Alex Gaynor | 5484f72 | 2014-01-28 05:46:15 -0800 | [diff] [blame] | 87 | :return: The new key. |
Alex Gaynor | e19e89f | 2014-01-28 06:58:43 -0800 | [diff] [blame] | 88 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 89 | :meth:`derive` or |
| 90 | :meth:`verify` is |
| 91 | called more than |
| 92 | once. |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 93 | |
Alex Gaynor | 5484f72 | 2014-01-28 05:46:15 -0800 | [diff] [blame] | 94 | This generates and returns a new key from the supplied key material. |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 95 | |
| 96 | .. method:: verify(key_material, expected_key) |
| 97 | |
Alex Gaynor | e85e356 | 2014-11-22 23:26:30 -0800 | [diff] [blame] | 98 | :param bytes key_material: The input key material. This is the same as |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 99 | ``key_material`` in :meth:`derive`. |
Alex Gaynor | e85e356 | 2014-11-22 23:26:30 -0800 | [diff] [blame] | 100 | :param bytes expected_key: The expected result of deriving a new key, |
Alex Gaynor | 5484f72 | 2014-01-28 05:46:15 -0800 | [diff] [blame] | 101 | this is the same as the return value of |
| 102 | :meth:`derive`. |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 103 | :raises cryptography.exceptions.InvalidKey: This is raised when the |
| 104 | derived key does not match |
| 105 | the expected key. |
Alex Gaynor | e19e89f | 2014-01-28 06:58:43 -0800 | [diff] [blame] | 106 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 107 | :meth:`derive` or |
| 108 | :meth:`verify` is |
| 109 | called more than |
| 110 | once. |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 111 | |
Alex Gaynor | 5484f72 | 2014-01-28 05:46:15 -0800 | [diff] [blame] | 112 | This checks whether deriving a new key from the supplied |
| 113 | ``key_material`` generates the same key as the ``expected_key``, and |
| 114 | raises an exception if they do not match. This can be used for |
| 115 | something like checking whether a user's password attempt matches the |
| 116 | stored derived key. |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 117 | |
Ayrx | c812170 | 2014-04-15 19:02:05 +0800 | [diff] [blame] | 118 | |
Terry Chia | cc5e445 | 2014-10-12 15:35:21 +0800 | [diff] [blame] | 119 | `Message Authentication Code`_ |
| 120 | ------------------------------ |
Ayrx | c812170 | 2014-04-15 19:02:05 +0800 | [diff] [blame] | 121 | |
| 122 | .. class:: CMACContext |
| 123 | |
Alex Gaynor | 7d15688 | 2014-10-20 10:40:34 -0700 | [diff] [blame] | 124 | :class:`CMACContext` has been deprecated in favor of :class:`MACContext`. |
Terry Chia | c7c82f3 | 2014-10-20 12:15:22 +0800 | [diff] [blame] | 125 | |
Ayrx | c812170 | 2014-04-15 19:02:05 +0800 | [diff] [blame] | 126 | .. versionadded:: 0.4 |
| 127 | |
| 128 | .. method:: update(data) |
| 129 | |
Alex Gaynor | e85e356 | 2014-11-22 23:26:30 -0800 | [diff] [blame] | 130 | :param bytes data: The data you want to authenticate. |
Ayrx | c812170 | 2014-04-15 19:02:05 +0800 | [diff] [blame] | 131 | |
| 132 | .. method:: finalize() |
| 133 | |
Ayrx | 7964c17 | 2014-04-15 21:50:58 +0800 | [diff] [blame] | 134 | :return: The message authentication code. |
Ayrx | c812170 | 2014-04-15 19:02:05 +0800 | [diff] [blame] | 135 | |
| 136 | .. method:: copy() |
| 137 | |
| 138 | :return: A :class:`~cryptography.hazmat.primitives.interfaces.CMACContext` |
| 139 | that is a copy of the current context. |
| 140 | |
Terry Chia | cc5e445 | 2014-10-12 15:35:21 +0800 | [diff] [blame] | 141 | .. class:: MACContext |
| 142 | |
| 143 | .. versionadded:: 0.7 |
| 144 | |
| 145 | .. method:: update(data) |
| 146 | |
Alex Gaynor | e85e356 | 2014-11-22 23:26:30 -0800 | [diff] [blame] | 147 | :param bytes data: The data you want to authenticate. |
Terry Chia | cc5e445 | 2014-10-12 15:35:21 +0800 | [diff] [blame] | 148 | |
| 149 | .. method:: finalize() |
| 150 | |
| 151 | :return: The message authentication code. |
| 152 | |
| 153 | .. method:: copy() |
| 154 | |
Alex Gaynor | 7d15688 | 2014-10-20 10:40:34 -0700 | [diff] [blame] | 155 | :return: A |
| 156 | :class:`~cryptography.hazmat.primitives.interfaces.MACContext` that |
| 157 | is a copy of the current context. |
Terry Chia | cc5e445 | 2014-10-12 15:35:21 +0800 | [diff] [blame] | 158 | |
Alex Gaynor | 7d15688 | 2014-10-20 10:40:34 -0700 | [diff] [blame] | 159 | .. method:: verify(signature) |
Terry Chia | cc5e445 | 2014-10-12 15:35:21 +0800 | [diff] [blame] | 160 | |
Alex Gaynor | e85e356 | 2014-11-22 23:26:30 -0800 | [diff] [blame] | 161 | :param bytes signature: The signature to verify. |
Terry Chia | cc5e445 | 2014-10-12 15:35:21 +0800 | [diff] [blame] | 162 | |
| 163 | :raises cryptography.exceptions.InvalidSignature: This is raised when |
| 164 | the provided signature does not match the expected signature. |
Ayrx | c812170 | 2014-04-15 19:02:05 +0800 | [diff] [blame] | 165 | |
Paul Kehrer | 05c122b | 2014-11-24 08:41:05 -1000 | [diff] [blame] | 166 | |
Ayrx | 83cd3f8 | 2014-04-15 21:56:32 +0800 | [diff] [blame] | 167 | .. _`CMAC`: https://en.wikipedia.org/wiki/CMAC |