David Reid | 30722b9 | 2013-11-07 13:03:39 -0800 | [diff] [blame] | 1 | .. hazmat:: |
| 2 | |
| 3 | Interfaces |
| 4 | ========== |
| 5 | |
| 6 | |
| 7 | ``cryptography`` uses `Abstract Base Classes`_ as interfaces to describe the |
David Reid | bd18bcd | 2013-11-07 13:13:30 -0800 | [diff] [blame] | 8 | properties and methods of most primitive constructs. Backends may also use |
| 9 | this information to influence their operation. Interfaces should also be used |
David Reid | 30722b9 | 2013-11-07 13:03:39 -0800 | [diff] [blame] | 10 | to document argument and return types. |
| 11 | |
David Reid | 9ed25e4 | 2013-11-07 13:15:27 -0800 | [diff] [blame] | 12 | .. _`Abstract Base Classes`: http://docs.python.org/3.2/library/abc.html |
David Reid | 30722b9 | 2013-11-07 13:03:39 -0800 | [diff] [blame] | 13 | |
| 14 | |
David Reid | 0a394df | 2013-11-15 16:19:50 -0800 | [diff] [blame] | 15 | Symmetric Ciphers |
| 16 | ~~~~~~~~~~~~~~~~~ |
David Reid | 30722b9 | 2013-11-07 13:03:39 -0800 | [diff] [blame] | 17 | |
| 18 | .. currentmodule:: cryptography.hazmat.primitives.interfaces |
| 19 | |
David Reid | 0a394df | 2013-11-15 16:19:50 -0800 | [diff] [blame] | 20 | |
| 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 Reid | 668d480 | 2013-12-17 11:53:43 -0800 | [diff] [blame] | 39 | .. 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 | |
David Reid | 0a394df | 2013-11-15 16:19:50 -0800 | [diff] [blame] | 50 | Cipher Modes |
| 51 | ------------ |
| 52 | |
David Reid | 30722b9 | 2013-11-07 13:03:39 -0800 | [diff] [blame] | 53 | Interfaces 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 Gaynor | 9626b5a | 2013-11-19 16:49:26 -0800 | [diff] [blame] | 70 | .. 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 Reid | 30722b9 | 2013-11-07 13:03:39 -0800 | [diff] [blame] | 82 | |
| 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 Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 105 | |
| 106 | Asymmetric Interfaces |
| 107 | ~~~~~~~~~~~~~~~~~~~~~ |
| 108 | |
| 109 | .. class:: RSAPrivateKey |
| 110 | |
Paul Kehrer | 46688b1 | 2014-01-26 13:23:13 -0600 | [diff] [blame] | 111 | .. versionadded:: 0.2 |
Paul Kehrer | 82629f4 | 2014-01-26 12:25:02 -0600 | [diff] [blame] | 112 | |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 113 | An `RSA`_ private key. |
| 114 | |
Paul Kehrer | 0e94fbe | 2014-01-26 11:47:21 -0600 | [diff] [blame] | 115 | .. method:: public_key() |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 116 | |
Paul Kehrer | 359b946 | 2014-01-26 12:03:05 -0600 | [diff] [blame] | 117 | :return: :class:`~cryptography.hazmat.primitives.interfaces.RSAPublicKey` |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 118 | |
| 119 | An RSA public key object corresponding to the values of the private key. |
| 120 | |
| 121 | .. attribute:: modulus |
| 122 | |
Paul Kehrer | d527b30 | 2014-01-26 11:41:38 -0600 | [diff] [blame] | 123 | :type: int |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 124 | |
Paul Kehrer | 0e94fbe | 2014-01-26 11:47:21 -0600 | [diff] [blame] | 125 | The public modulus. |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 126 | |
| 127 | .. attribute:: public_exponent |
| 128 | |
| 129 | :type: int |
| 130 | |
Paul Kehrer | 0e94fbe | 2014-01-26 11:47:21 -0600 | [diff] [blame] | 131 | The public exponent. |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 132 | |
Alex Gaynor | 2649a69 | 2014-02-03 07:14:16 -0800 | [diff] [blame] | 133 | .. attribute:: private_exponent |
| 134 | |
| 135 | :type: int |
| 136 | |
| 137 | The private exponent. |
| 138 | |
Alex Stapleton | ee3e6bf | 2014-02-02 21:13:48 +0000 | [diff] [blame] | 139 | .. attribute:: key_size |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 140 | |
| 141 | :type: int |
| 142 | |
| 143 | The bit length of the modulus. |
| 144 | |
| 145 | .. attribute:: p |
| 146 | |
Paul Kehrer | d527b30 | 2014-01-26 11:41:38 -0600 | [diff] [blame] | 147 | :type: int |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 148 | |
Alex Gaynor | 2a91874 | 2014-01-26 16:53:44 -0600 | [diff] [blame] | 149 | ``p``, one of the two primes composing the :attr:`modulus`. |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 150 | |
| 151 | .. attribute:: q |
| 152 | |
Paul Kehrer | d527b30 | 2014-01-26 11:41:38 -0600 | [diff] [blame] | 153 | :type: int |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 154 | |
Alex Gaynor | 2a91874 | 2014-01-26 16:53:44 -0600 | [diff] [blame] | 155 | ``q``, one of the two primes composing the :attr:`modulus`. |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 156 | |
| 157 | .. attribute:: d |
| 158 | |
Paul Kehrer | d527b30 | 2014-01-26 11:41:38 -0600 | [diff] [blame] | 159 | :type: int |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 160 | |
Alex Gaynor | 2649a69 | 2014-02-03 07:14:16 -0800 | [diff] [blame] | 161 | The private exponent. Alias for :attr:`private_exponent`. |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 162 | |
| 163 | .. attribute:: n |
| 164 | |
Paul Kehrer | d527b30 | 2014-01-26 11:41:38 -0600 | [diff] [blame] | 165 | :type: int |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 166 | |
Alex Gaynor | 2a91874 | 2014-01-26 16:53:44 -0600 | [diff] [blame] | 167 | The public modulus. Alias for :attr:`modulus`. |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 168 | |
| 169 | .. attribute:: e |
| 170 | |
| 171 | :type: int |
| 172 | |
Alex Gaynor | 2a91874 | 2014-01-26 16:53:44 -0600 | [diff] [blame] | 173 | The public exponent. Alias for :attr:`public_exponent`. |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 174 | |
| 175 | |
| 176 | .. class:: RSAPublicKey |
| 177 | |
Paul Kehrer | 46688b1 | 2014-01-26 13:23:13 -0600 | [diff] [blame] | 178 | .. versionadded:: 0.2 |
Paul Kehrer | 82629f4 | 2014-01-26 12:25:02 -0600 | [diff] [blame] | 179 | |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 180 | An `RSA`_ public key. |
| 181 | |
| 182 | .. attribute:: modulus |
| 183 | |
Paul Kehrer | d527b30 | 2014-01-26 11:41:38 -0600 | [diff] [blame] | 184 | :type: int |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 185 | |
Paul Kehrer | 0e94fbe | 2014-01-26 11:47:21 -0600 | [diff] [blame] | 186 | The public modulus. |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 187 | |
Alex Stapleton | ee3e6bf | 2014-02-02 21:13:48 +0000 | [diff] [blame] | 188 | .. attribute:: key_size |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 189 | |
| 190 | :type: int |
| 191 | |
| 192 | The bit length of the modulus. |
| 193 | |
| 194 | .. attribute:: public_exponent |
| 195 | |
| 196 | :type: int |
| 197 | |
Paul Kehrer | 0e94fbe | 2014-01-26 11:47:21 -0600 | [diff] [blame] | 198 | The public exponent. |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 199 | |
| 200 | .. attribute:: n |
| 201 | |
Paul Kehrer | d527b30 | 2014-01-26 11:41:38 -0600 | [diff] [blame] | 202 | :type: int |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 203 | |
Alex Gaynor | 2a91874 | 2014-01-26 16:53:44 -0600 | [diff] [blame] | 204 | The public modulus. Alias for :attr:`modulus`. |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 205 | |
| 206 | .. attribute:: e |
| 207 | |
| 208 | :type: int |
| 209 | |
Alex Gaynor | 2a91874 | 2014-01-26 16:53:44 -0600 | [diff] [blame] | 210 | The public exponent. Alias for :attr:`public_exponent`. |
| 211 | |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 212 | |
Paul Kehrer | e51a2db | 2014-01-29 11:49:35 -0600 | [diff] [blame] | 213 | Hash Algorithms |
| 214 | ~~~~~~~~~~~~~~~ |
| 215 | |
| 216 | .. class:: HashAlgorithm |
| 217 | |
Paul Kehrer | e51a2db | 2014-01-29 11:49:35 -0600 | [diff] [blame] | 218 | .. attribute:: name |
| 219 | |
| 220 | :type: str |
| 221 | |
Paul Kehrer | 4c75a8c | 2014-01-29 12:20:37 -0600 | [diff] [blame] | 222 | The standard name for the hash algorithm, for example: ``"sha256"`` or |
| 223 | ``"whirlpool"``. |
Paul Kehrer | e51a2db | 2014-01-29 11:49:35 -0600 | [diff] [blame] | 224 | |
| 225 | .. attribute:: digest_size |
| 226 | |
| 227 | :type: int |
| 228 | |
| 229 | The size of the resulting digest in bytes. |
| 230 | |
| 231 | .. attribute:: block_size |
| 232 | |
| 233 | :type: int |
| 234 | |
| 235 | The internal block size of the hash algorithm in bytes. |
| 236 | |
| 237 | |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 238 | Key Derivation Functions |
| 239 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 240 | |
| 241 | .. class:: KeyDerivationFunction |
| 242 | |
Alex Gaynor | 8454c51 | 2014-01-28 07:01:54 -0800 | [diff] [blame] | 243 | .. versionadded:: 0.2 |
| 244 | |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 245 | .. method:: derive(key_material) |
| 246 | |
Alex Gaynor | 5484f72 | 2014-01-28 05:46:15 -0800 | [diff] [blame] | 247 | :param key_material bytes: The input key material. Depending on what |
| 248 | key derivation function you are using this |
| 249 | could be either random material, or a user |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 250 | supplied password. |
Alex Gaynor | 5484f72 | 2014-01-28 05:46:15 -0800 | [diff] [blame] | 251 | :return: The new key. |
Alex Gaynor | e19e89f | 2014-01-28 06:58:43 -0800 | [diff] [blame] | 252 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 253 | :meth:`derive` or |
| 254 | :meth:`verify` is |
| 255 | called more than |
| 256 | once. |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 257 | |
Alex Gaynor | 5484f72 | 2014-01-28 05:46:15 -0800 | [diff] [blame] | 258 | This generates and returns a new key from the supplied key material. |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 259 | |
| 260 | .. method:: verify(key_material, expected_key) |
| 261 | |
Alex Gaynor | 5484f72 | 2014-01-28 05:46:15 -0800 | [diff] [blame] | 262 | :param key_material bytes: The input key material. This is the same as |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 263 | ``key_material`` in :meth:`derive`. |
Alex Gaynor | 5484f72 | 2014-01-28 05:46:15 -0800 | [diff] [blame] | 264 | :param expected_key bytes: The expected result of deriving a new key, |
| 265 | this is the same as the return value of |
| 266 | :meth:`derive`. |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 267 | :raises cryptography.exceptions.InvalidKey: This is raised when the |
| 268 | derived key does not match |
| 269 | the expected key. |
Alex Gaynor | e19e89f | 2014-01-28 06:58:43 -0800 | [diff] [blame] | 270 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 271 | :meth:`derive` or |
| 272 | :meth:`verify` is |
| 273 | called more than |
| 274 | once. |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 275 | |
Alex Gaynor | 5484f72 | 2014-01-28 05:46:15 -0800 | [diff] [blame] | 276 | This checks whether deriving a new key from the supplied |
| 277 | ``key_material`` generates the same key as the ``expected_key``, and |
| 278 | raises an exception if they do not match. This can be used for |
| 279 | something like checking whether a user's password attempt matches the |
| 280 | stored derived key. |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 281 | |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 282 | .. _`RSA`: http://en.wikipedia.org/wiki/RSA_(cryptosystem) |