David Reid | 2a746ce | 2013-11-15 15:32:14 -0800 | [diff] [blame] | 1 | .. hazmat:: |
| 2 | |
| 3 | Backend Interfaces |
| 4 | ================== |
| 5 | |
Alex Gaynor | f8796b1 | 2013-12-13 20:28:55 -0800 | [diff] [blame] | 6 | .. currentmodule:: cryptography.hazmat.backends.interfaces |
David Reid | 2a746ce | 2013-11-15 15:32:14 -0800 | [diff] [blame] | 7 | |
| 8 | |
David Reid | 6b9df81 | 2013-11-18 14:13:02 -0800 | [diff] [blame] | 9 | Backend implementations may provide a number of interfaces to support operations |
| 10 | such as :doc:`/hazmat/primitives/symmetric-encryption`, |
| 11 | :doc:`/hazmat/primitives/cryptographic-hashes`, and |
| 12 | :doc:`/hazmat/primitives/hmac`. |
| 13 | |
| 14 | A specific ``backend`` may provide one or more of these interfaces. |
| 15 | |
| 16 | |
David Reid | 2a746ce | 2013-11-15 15:32:14 -0800 | [diff] [blame] | 17 | .. class:: CipherBackend |
| 18 | |
Alex Stapleton | 63b3de2 | 2014-02-08 09:43:16 +0000 | [diff] [blame] | 19 | A backend that provides methods for using ciphers for encryption |
David Reid | 5973f4c | 2013-11-18 11:29:44 -0800 | [diff] [blame] | 20 | and decryption. |
| 21 | |
Alex Gaynor | 585c99c | 2014-02-04 16:10:10 -0800 | [diff] [blame] | 22 | The following backends implement this interface: |
| 23 | |
| 24 | * :doc:`/hazmat/backends/openssl` |
| 25 | * :doc:`/hazmat/backends/commoncrypto` |
| 26 | |
David Reid | 2a746ce | 2013-11-15 15:32:14 -0800 | [diff] [blame] | 27 | .. method:: cipher_supported(cipher, mode) |
| 28 | |
David Reid | 5973f4c | 2013-11-18 11:29:44 -0800 | [diff] [blame] | 29 | Check if a ``cipher`` and ``mode`` combination is supported by |
| 30 | this backend. |
| 31 | |
| 32 | :param cipher: An instance of a |
| 33 | :class:`~cryptography.hazmat.primitives.interfaces.CipherAlgorithm` |
| 34 | provider. |
| 35 | :param mode: An instance of a |
| 36 | :class:`~cryptography.hazmat.primitives.interfaces.Mode` provider. |
| 37 | |
| 38 | :returns: ``True`` if the specified ``cipher`` and ``mode`` combination |
| 39 | is supported by this backend, otherwise ``False`` |
David Reid | 2a746ce | 2013-11-15 15:32:14 -0800 | [diff] [blame] | 40 | |
David Reid | 2a746ce | 2013-11-15 15:32:14 -0800 | [diff] [blame] | 41 | |
| 42 | .. method:: create_symmetric_encryption_ctx(cipher, mode) |
| 43 | |
David Reid | 5973f4c | 2013-11-18 11:29:44 -0800 | [diff] [blame] | 44 | Create a |
Paul Kehrer | 446cc2a | 2014-01-29 14:39:30 -0600 | [diff] [blame] | 45 | :class:`~cryptography.hazmat.primitives.interfaces.CipherContext` that |
David Reid | 5973f4c | 2013-11-18 11:29:44 -0800 | [diff] [blame] | 46 | can be used for encrypting data with the symmetric ``cipher`` using |
| 47 | the given ``mode``. |
| 48 | |
| 49 | :param cipher: An instance of a |
| 50 | :class:`~cryptography.hazmat.primitives.interfaces.CipherAlgorithm` |
| 51 | provider. |
| 52 | :param mode: An instance of a |
| 53 | :class:`~cryptography.hazmat.primitives.interfaces.Mode` provider. |
| 54 | |
| 55 | :returns: |
| 56 | :class:`~cryptography.hazmat.primitives.interfaces.CipherContext` |
| 57 | |
Paul Kehrer | a07925a | 2013-12-06 11:49:42 -0600 | [diff] [blame] | 58 | :raises ValueError: When tag is not None in an AEAD mode |
| 59 | |
David Reid | 2a746ce | 2013-11-15 15:32:14 -0800 | [diff] [blame] | 60 | |
| 61 | .. method:: create_symmetric_decryption_ctx(cipher, mode) |
| 62 | |
David Reid | 5973f4c | 2013-11-18 11:29:44 -0800 | [diff] [blame] | 63 | Create a |
Paul Kehrer | 446cc2a | 2014-01-29 14:39:30 -0600 | [diff] [blame] | 64 | :class:`~cryptography.hazmat.primitives.interfaces.CipherContext` that |
David Reid | 5973f4c | 2013-11-18 11:29:44 -0800 | [diff] [blame] | 65 | can be used for decrypting data with the symmetric ``cipher`` using |
| 66 | the given ``mode``. |
| 67 | |
| 68 | :param cipher: An instance of a |
| 69 | :class:`~cryptography.hazmat.primitives.interfaces.CipherAlgorithm` |
| 70 | provider. |
| 71 | :param mode: An instance of a |
| 72 | :class:`~cryptography.hazmat.primitives.interfaces.Mode` provider. |
| 73 | |
| 74 | :returns: |
| 75 | :class:`~cryptography.hazmat.primitives.interfaces.CipherContext` |
David Reid | 2a746ce | 2013-11-15 15:32:14 -0800 | [diff] [blame] | 76 | |
Paul Kehrer | a07925a | 2013-12-06 11:49:42 -0600 | [diff] [blame] | 77 | :raises ValueError: When tag is None in an AEAD mode |
| 78 | |
David Reid | 2a746ce | 2013-11-15 15:32:14 -0800 | [diff] [blame] | 79 | |
| 80 | .. class:: HashBackend |
| 81 | |
David Reid | 5973f4c | 2013-11-18 11:29:44 -0800 | [diff] [blame] | 82 | A backend with methods for using cryptographic hash functions. |
| 83 | |
Alex Gaynor | 585c99c | 2014-02-04 16:10:10 -0800 | [diff] [blame] | 84 | The following backends implement this interface: |
| 85 | |
| 86 | * :doc:`/hazmat/backends/openssl` |
| 87 | * :doc:`/hazmat/backends/commoncrypto` |
| 88 | |
David Reid | 2a746ce | 2013-11-15 15:32:14 -0800 | [diff] [blame] | 89 | .. method:: hash_supported(algorithm) |
| 90 | |
David Reid | 6624a44 | 2013-11-18 12:44:30 -0800 | [diff] [blame] | 91 | Check if the specified ``algorithm`` is supported by this backend. |
| 92 | |
David Reid | 5973f4c | 2013-11-18 11:29:44 -0800 | [diff] [blame] | 93 | :param algorithm: An instance of a |
| 94 | :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` |
| 95 | provider. |
| 96 | |
| 97 | :returns: ``True`` if the specified ``algorithm`` is supported by this |
| 98 | backend, otherwise ``False``. |
| 99 | |
David Reid | 2a746ce | 2013-11-15 15:32:14 -0800 | [diff] [blame] | 100 | |
| 101 | .. method:: create_hash_ctx(algorithm) |
| 102 | |
David Reid | 6624a44 | 2013-11-18 12:44:30 -0800 | [diff] [blame] | 103 | Create a |
Paul Kehrer | 446cc2a | 2014-01-29 14:39:30 -0600 | [diff] [blame] | 104 | :class:`~cryptography.hazmat.primitives.interfaces.HashContext` that |
David Reid | 6624a44 | 2013-11-18 12:44:30 -0800 | [diff] [blame] | 105 | uses the specified ``algorithm`` to calculate a message digest. |
| 106 | |
David Reid | 5973f4c | 2013-11-18 11:29:44 -0800 | [diff] [blame] | 107 | :param algorithm: An instance of a |
| 108 | :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` |
| 109 | provider. |
| 110 | |
| 111 | :returns: |
| 112 | :class:`~cryptography.hazmat.primitives.interfaces.HashContext` |
David Reid | 2a746ce | 2013-11-15 15:32:14 -0800 | [diff] [blame] | 113 | |
| 114 | |
| 115 | .. class:: HMACBackend |
| 116 | |
David Reid | 5973f4c | 2013-11-18 11:29:44 -0800 | [diff] [blame] | 117 | A backend with methods for using cryptographic hash functions as message |
| 118 | authentication codes. |
| 119 | |
Alex Gaynor | 585c99c | 2014-02-04 16:10:10 -0800 | [diff] [blame] | 120 | The following backends implement this interface: |
| 121 | |
| 122 | * :doc:`/hazmat/backends/openssl` |
| 123 | * :doc:`/hazmat/backends/commoncrypto` |
| 124 | |
Paul Kehrer | 90ae866 | 2013-12-23 17:21:00 -0600 | [diff] [blame] | 125 | .. method:: hmac_supported(algorithm) |
| 126 | |
| 127 | Check if the specified ``algorithm`` is supported by this backend. |
| 128 | |
| 129 | :param algorithm: An instance of a |
| 130 | :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` |
| 131 | provider. |
| 132 | |
| 133 | :returns: ``True`` if the specified ``algorithm`` is supported for HMAC |
| 134 | by this backend, otherwise ``False``. |
| 135 | |
David Reid | 2a746ce | 2013-11-15 15:32:14 -0800 | [diff] [blame] | 136 | .. method:: create_hmac_ctx(algorithm) |
| 137 | |
David Reid | 6624a44 | 2013-11-18 12:44:30 -0800 | [diff] [blame] | 138 | Create a |
Paul Kehrer | 446cc2a | 2014-01-29 14:39:30 -0600 | [diff] [blame] | 139 | :class:`~cryptography.hazmat.primitives.interfaces.HashContext` that |
Paul Kehrer | 4f776c4 | 2013-12-23 17:25:54 -0600 | [diff] [blame] | 140 | uses the specified ``algorithm`` to calculate a hash-based message |
| 141 | authentication code. |
David Reid | 6624a44 | 2013-11-18 12:44:30 -0800 | [diff] [blame] | 142 | |
David Reid | 5973f4c | 2013-11-18 11:29:44 -0800 | [diff] [blame] | 143 | :param algorithm: An instance of a |
| 144 | :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` |
| 145 | provider. |
| 146 | |
| 147 | :returns: |
| 148 | :class:`~cryptography.hazmat.primitives.interfaces.HashContext` |
Paul Kehrer | 1050ddf | 2014-01-27 21:04:03 -0600 | [diff] [blame] | 149 | |
| 150 | |
Paul Kehrer | 98e40e6 | 2014-01-28 15:07:49 -0600 | [diff] [blame] | 151 | .. class:: PBKDF2HMACBackend |
Paul Kehrer | 1050ddf | 2014-01-27 21:04:03 -0600 | [diff] [blame] | 152 | |
Paul Kehrer | 5d1af21 | 2014-01-28 12:19:32 -0600 | [diff] [blame] | 153 | .. versionadded:: 0.2 |
| 154 | |
Paul Kehrer | 98e40e6 | 2014-01-28 15:07:49 -0600 | [diff] [blame] | 155 | A backend with methods for using PBKDF2 using HMAC as a PRF. |
Paul Kehrer | 1050ddf | 2014-01-27 21:04:03 -0600 | [diff] [blame] | 156 | |
Alex Gaynor | 585c99c | 2014-02-04 16:10:10 -0800 | [diff] [blame] | 157 | The following backends implement this interface: |
| 158 | |
| 159 | * :doc:`/hazmat/backends/openssl` |
| 160 | * :doc:`/hazmat/backends/commoncrypto` |
| 161 | |
Paul Kehrer | 98e40e6 | 2014-01-28 15:07:49 -0600 | [diff] [blame] | 162 | .. method:: pbkdf2_hmac_supported(algorithm) |
Paul Kehrer | 1050ddf | 2014-01-27 21:04:03 -0600 | [diff] [blame] | 163 | |
| 164 | Check if the specified ``algorithm`` is supported by this backend. |
| 165 | |
Paul Kehrer | 589b908 | 2014-01-28 21:25:41 -0600 | [diff] [blame] | 166 | :param algorithm: An instance of a |
Paul Kehrer | 1050ddf | 2014-01-27 21:04:03 -0600 | [diff] [blame] | 167 | :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` |
| 168 | provider. |
| 169 | |
| 170 | :returns: ``True`` if the specified ``algorithm`` is supported for |
Paul Kehrer | 98e40e6 | 2014-01-28 15:07:49 -0600 | [diff] [blame] | 171 | PBKDF2 HMAC by this backend, otherwise ``False``. |
Paul Kehrer | 1050ddf | 2014-01-27 21:04:03 -0600 | [diff] [blame] | 172 | |
Paul Kehrer | 98e40e6 | 2014-01-28 15:07:49 -0600 | [diff] [blame] | 173 | .. method:: derive_pbkdf2_hmac(self, algorithm, length, salt, iterations, |
| 174 | key_material) |
Paul Kehrer | 1050ddf | 2014-01-27 21:04:03 -0600 | [diff] [blame] | 175 | |
| 176 | :param algorithm: An instance of a |
| 177 | :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` |
| 178 | provider. |
| 179 | |
| 180 | :param int length: The desired length of the derived key. Maximum is |
Paul Kehrer | 98e40e6 | 2014-01-28 15:07:49 -0600 | [diff] [blame] | 181 | (2\ :sup:`32` - 1) * ``algorithm.digest_size`` |
Paul Kehrer | 1050ddf | 2014-01-27 21:04:03 -0600 | [diff] [blame] | 182 | |
Paul Kehrer | b6d764c | 2014-01-27 22:32:11 -0600 | [diff] [blame] | 183 | :param bytes salt: A salt. |
Paul Kehrer | 1050ddf | 2014-01-27 21:04:03 -0600 | [diff] [blame] | 184 | |
| 185 | :param int iterations: The number of iterations to perform of the hash |
Paul Kehrer | c58b478 | 2014-01-29 13:56:25 -0600 | [diff] [blame] | 186 | function. This can be used to control the length of time the |
| 187 | operation takes. Higher numbers help mitigate brute force attacks |
| 188 | against derived keys. |
Paul Kehrer | 1050ddf | 2014-01-27 21:04:03 -0600 | [diff] [blame] | 189 | |
| 190 | :param bytes key_material: The key material to use as a basis for |
| 191 | the derived key. This is typically a password. |
| 192 | |
| 193 | :return bytes: Derived key. |
Alex Stapleton | 209a132 | 2014-02-07 20:26:44 +0000 | [diff] [blame] | 194 | |
| 195 | |
| 196 | .. class:: RSABackend |
| 197 | |
| 198 | .. versionadded:: 0.2 |
| 199 | |
| 200 | A backend with methods for using RSA. |
| 201 | |
Alex Stapleton | e009ad2 | 2014-02-08 17:23:46 +0000 | [diff] [blame] | 202 | .. method:: generate_rsa_private_key(public_exponent, key_size) |
Alex Stapleton | 209a132 | 2014-02-07 20:26:44 +0000 | [diff] [blame] | 203 | |
| 204 | :param int public_exponent: The public exponent of the new key. |
| 205 | Often one of the small Fermat primes 3, 5, 17, 257 or 65537. |
| 206 | |
Alex Stapleton | e009ad2 | 2014-02-08 17:23:46 +0000 | [diff] [blame] | 207 | :param int key_size: The length in bits of the modulus. Should be |
Alex Stapleton | 209a132 | 2014-02-07 20:26:44 +0000 | [diff] [blame] | 208 | at least 2048. |
| 209 | |
| 210 | :return: A new instance of a |
| 211 | :class:`~cryptography.hazmat.primitives.interfaces.RSAPrivateKey` |
| 212 | provider. |
| 213 | |
| 214 | :raises ValueError: If the public_exponent is not valid. |