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 | |
Alex Stapleton | c5fffd3 | 2014-03-18 15:29:00 +0000 | [diff] [blame] | 15 | Symmetric ciphers |
Alex Gaynor | 645315b | 2014-06-23 11:55:55 -0700 | [diff] [blame] | 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 | |
Alex Stapleton | c5fffd3 | 2014-03-18 15:29:00 +0000 | [diff] [blame] | 50 | Cipher modes |
Alex Gaynor | 645315b | 2014-06-23 11:55:55 -0700 | [diff] [blame] | 51 | ~~~~~~~~~~~~ |
David Reid | 0a394df | 2013-11-15 16:19:50 -0800 | [diff] [blame] | 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 | |
Alex Stapleton | c5fffd3 | 2014-03-18 15:29:00 +0000 | [diff] [blame] | 106 | Asymmetric interfaces |
Alex Gaynor | 645315b | 2014-06-23 11:55:55 -0700 | [diff] [blame] | 107 | --------------------- |
| 108 | |
| 109 | .. class:: AsymmetricSignatureContext |
| 110 | |
| 111 | .. versionadded:: 0.2 |
| 112 | |
| 113 | .. method:: update(data) |
| 114 | |
| 115 | :param bytes data: The data you want to sign. |
| 116 | |
| 117 | .. method:: finalize() |
| 118 | |
| 119 | :return bytes signature: The signature. |
| 120 | |
| 121 | |
| 122 | .. class:: AsymmetricVerificationContext |
| 123 | |
| 124 | .. versionadded:: 0.2 |
| 125 | |
| 126 | .. method:: update(data) |
| 127 | |
| 128 | :param bytes data: The data you wish to verify using the signature. |
| 129 | |
| 130 | .. method:: verify() |
| 131 | |
| 132 | :raises cryptography.exceptions.InvalidSignature: If the signature does |
| 133 | not validate. |
| 134 | |
| 135 | |
| 136 | .. class:: AsymmetricPadding |
| 137 | |
| 138 | .. versionadded:: 0.2 |
| 139 | |
| 140 | .. attribute:: name |
| 141 | |
| 142 | |
| 143 | RSA |
| 144 | ~~~ |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 145 | |
| 146 | .. class:: RSAPrivateKey |
| 147 | |
Paul Kehrer | 46688b1 | 2014-01-26 13:23:13 -0600 | [diff] [blame] | 148 | .. versionadded:: 0.2 |
Paul Kehrer | 82629f4 | 2014-01-26 12:25:02 -0600 | [diff] [blame] | 149 | |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 150 | An `RSA`_ private key. |
| 151 | |
Paul Kehrer | f2fb02a | 2014-06-19 10:16:42 -0600 | [diff] [blame] | 152 | .. method:: signer(padding, algorithm) |
Paul Kehrer | 01cdfb2 | 2014-04-15 11:27:03 -0400 | [diff] [blame] | 153 | |
| 154 | .. versionadded:: 0.3 |
| 155 | |
| 156 | Sign data which can be verified later by others using the public key. |
| 157 | |
| 158 | :param padding: An instance of a |
| 159 | :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricPadding` |
| 160 | provider. |
| 161 | |
| 162 | :param algorithm: An instance of a |
| 163 | :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` |
| 164 | provider. |
| 165 | |
Paul Kehrer | 01cdfb2 | 2014-04-15 11:27:03 -0400 | [diff] [blame] | 166 | :returns: |
| 167 | :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricSignatureContext` |
| 168 | |
Paul Kehrer | f2fb02a | 2014-06-19 10:16:42 -0600 | [diff] [blame] | 169 | .. method:: decrypt(ciphertext, padding) |
Paul Kehrer | 27f9ca6 | 2014-04-15 17:59:27 -0400 | [diff] [blame] | 170 | |
| 171 | .. versionadded:: 0.4 |
| 172 | |
| 173 | Decrypt data that was encrypted via the public key. |
| 174 | |
| 175 | :param bytes ciphertext: The ciphertext to decrypt. |
| 176 | |
| 177 | :param padding: An instance of a |
| 178 | :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricPadding` |
| 179 | provider. |
| 180 | |
Paul Kehrer | 27f9ca6 | 2014-04-15 17:59:27 -0400 | [diff] [blame] | 181 | :return bytes: Decrypted data. |
| 182 | |
Paul Kehrer | 0e94fbe | 2014-01-26 11:47:21 -0600 | [diff] [blame] | 183 | .. method:: public_key() |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 184 | |
Paul Kehrer | 359b946 | 2014-01-26 12:03:05 -0600 | [diff] [blame] | 185 | :return: :class:`~cryptography.hazmat.primitives.interfaces.RSAPublicKey` |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 186 | |
| 187 | An RSA public key object corresponding to the values of the private key. |
| 188 | |
Alex Stapleton | ee3e6bf | 2014-02-02 21:13:48 +0000 | [diff] [blame] | 189 | .. attribute:: key_size |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 190 | |
| 191 | :type: int |
| 192 | |
| 193 | The bit length of the modulus. |
| 194 | |
Paul Kehrer | f0a48c6 | 2014-06-07 17:04:13 -0500 | [diff] [blame] | 195 | .. class:: RSAPrivateKeyWithNumbers |
| 196 | |
| 197 | .. versionadded:: 0.5 |
| 198 | |
| 199 | Extends :class:`RSAPrivateKey`. |
| 200 | |
| 201 | .. method:: private_numbers() |
| 202 | |
| 203 | Create a |
| 204 | :class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateNumbers` |
| 205 | object. |
| 206 | |
| 207 | :returns: An |
| 208 | :class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateNumbers` |
| 209 | instance. |
| 210 | |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 211 | |
| 212 | .. class:: RSAPublicKey |
| 213 | |
Paul Kehrer | 46688b1 | 2014-01-26 13:23:13 -0600 | [diff] [blame] | 214 | .. versionadded:: 0.2 |
Paul Kehrer | 82629f4 | 2014-01-26 12:25:02 -0600 | [diff] [blame] | 215 | |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 216 | An `RSA`_ public key. |
| 217 | |
Paul Kehrer | f2fb02a | 2014-06-19 10:16:42 -0600 | [diff] [blame] | 218 | .. method:: verifier(signature, padding, algorithm) |
Paul Kehrer | 01cdfb2 | 2014-04-15 11:27:03 -0400 | [diff] [blame] | 219 | |
| 220 | .. versionadded:: 0.3 |
| 221 | |
| 222 | Verify data was signed by the private key associated with this public |
| 223 | key. |
| 224 | |
| 225 | :param bytes signature: The signature to verify. |
| 226 | |
| 227 | :param padding: An instance of a |
| 228 | :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricPadding` |
| 229 | provider. |
| 230 | |
| 231 | :param algorithm: An instance of a |
| 232 | :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` |
| 233 | provider. |
| 234 | |
Paul Kehrer | 01cdfb2 | 2014-04-15 11:27:03 -0400 | [diff] [blame] | 235 | :returns: |
| 236 | :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricVerificationContext` |
| 237 | |
Paul Kehrer | f2fb02a | 2014-06-19 10:16:42 -0600 | [diff] [blame] | 238 | .. method:: encrypt(plaintext, padding) |
Paul Kehrer | 4e602f3 | 2014-04-24 12:07:54 -0500 | [diff] [blame] | 239 | |
| 240 | .. versionadded:: 0.4 |
| 241 | |
| 242 | Encrypt data with the public key. |
| 243 | |
| 244 | :param bytes plaintext: The plaintext to encrypt. |
| 245 | |
| 246 | :param padding: An instance of a |
| 247 | :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricPadding` |
| 248 | provider. |
| 249 | |
Paul Kehrer | 4e602f3 | 2014-04-24 12:07:54 -0500 | [diff] [blame] | 250 | :return bytes: Encrypted data. |
Paul Kehrer | 01cdfb2 | 2014-04-15 11:27:03 -0400 | [diff] [blame] | 251 | |
Alex Stapleton | ee3e6bf | 2014-02-02 21:13:48 +0000 | [diff] [blame] | 252 | .. attribute:: key_size |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 253 | |
| 254 | :type: int |
| 255 | |
| 256 | The bit length of the modulus. |
| 257 | |
Paul Kehrer | ac42323 | 2014-01-25 14:13:09 -0600 | [diff] [blame] | 258 | |
Paul Kehrer | f0a48c6 | 2014-06-07 17:04:13 -0500 | [diff] [blame] | 259 | .. class:: RSAPublicKeyWithNumbers |
| 260 | |
| 261 | .. versionadded:: 0.5 |
| 262 | |
| 263 | Extends :class:`RSAPublicKey`. |
| 264 | |
| 265 | .. method:: public_numbers() |
| 266 | |
| 267 | Create a |
| 268 | :class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicNumbers` |
| 269 | object. |
| 270 | |
| 271 | :returns: An |
| 272 | :class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicNumbers` |
| 273 | instance. |
| 274 | |
| 275 | |
Alex Gaynor | 645315b | 2014-06-23 11:55:55 -0700 | [diff] [blame] | 276 | DSA |
| 277 | ~~~ |
| 278 | |
Mohammed Attia | 71acc67 | 2014-03-04 19:20:45 +0200 | [diff] [blame] | 279 | .. class:: DSAParameters |
Mohammed Attia | b416715 | 2014-03-04 03:29:56 +0200 | [diff] [blame] | 280 | |
| 281 | .. versionadded:: 0.3 |
| 282 | |
| 283 | `DSA`_ parameters. |
| 284 | |
Paul Kehrer | dacb5f9 | 2014-06-27 09:15:07 -0600 | [diff] [blame] | 285 | .. method:: generate_private_key() |
| 286 | |
| 287 | .. versionadded:: 0.5 |
| 288 | |
| 289 | Generate a DSA private key. This method can be used to generate many |
| 290 | new private keys from a single set of parameters. |
| 291 | |
| 292 | :return: A |
| 293 | :class:`~cryptography.hazmat.primitives.interfaces.DSAPrivateKey` |
| 294 | provider. |
| 295 | |
Mohammed Attia | b416715 | 2014-03-04 03:29:56 +0200 | [diff] [blame] | 296 | |
Paul Kehrer | 9842964 | 2014-06-22 16:37:21 -0600 | [diff] [blame] | 297 | .. class:: DSAParametersWithNumbers |
| 298 | |
| 299 | .. versionadded:: 0.5 |
| 300 | |
| 301 | Extends :class:`DSAParameters`. |
| 302 | |
| 303 | .. method:: parameter_numbers() |
| 304 | |
| 305 | Create a |
| 306 | :class:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAParameterNumbers` |
| 307 | object. |
| 308 | |
| 309 | :returns: A |
| 310 | :class:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAParameterNumbers` |
| 311 | instance. |
| 312 | |
| 313 | |
Mohammed Attia | b416715 | 2014-03-04 03:29:56 +0200 | [diff] [blame] | 314 | .. class:: DSAPrivateKey |
| 315 | |
| 316 | .. versionadded:: 0.3 |
| 317 | |
Mohammed Attia | 7a1738a | 2014-03-04 19:17:24 +0200 | [diff] [blame] | 318 | A `DSA`_ private key. |
Mohammed Attia | b416715 | 2014-03-04 03:29:56 +0200 | [diff] [blame] | 319 | |
| 320 | .. method:: public_key() |
| 321 | |
| 322 | :return: :class:`~cryptography.hazmat.primitives.interfaces.DSAPublicKey` |
| 323 | |
| 324 | An DSA public key object corresponding to the values of the private key. |
| 325 | |
| 326 | .. method:: parameters() |
| 327 | |
Mohammed Attia | 71acc67 | 2014-03-04 19:20:45 +0200 | [diff] [blame] | 328 | :return: :class:`~cryptography.hazmat.primitives.interfaces.DSAParameters` |
Mohammed Attia | b416715 | 2014-03-04 03:29:56 +0200 | [diff] [blame] | 329 | |
Mohammed Attia | 71acc67 | 2014-03-04 19:20:45 +0200 | [diff] [blame] | 330 | The DSAParameters object associated with this private key. |
Mohammed Attia | b416715 | 2014-03-04 03:29:56 +0200 | [diff] [blame] | 331 | |
Paul Kehrer | 0b3ff3b | 2014-05-01 15:34:42 -0500 | [diff] [blame] | 332 | .. method:: signer(algorithm, backend) |
| 333 | |
| 334 | .. versionadded:: 0.4 |
| 335 | |
| 336 | Sign data which can be verified later by others using the public key. |
| 337 | |
| 338 | :param algorithm: An instance of a |
| 339 | :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` |
| 340 | provider. |
| 341 | |
| 342 | :param backend: A |
| 343 | :class:`~cryptography.hazmat.backends.interfaces.DSABackend` |
| 344 | provider. |
| 345 | |
| 346 | :returns: |
| 347 | :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricSignatureContext` |
| 348 | |
Mohammed Attia | b416715 | 2014-03-04 03:29:56 +0200 | [diff] [blame] | 349 | .. attribute:: key_size |
| 350 | |
| 351 | :type: int |
| 352 | |
| 353 | The bit length of the modulus. |
| 354 | |
Mohammed Attia | b416715 | 2014-03-04 03:29:56 +0200 | [diff] [blame] | 355 | |
Paul Kehrer | 9842964 | 2014-06-22 16:37:21 -0600 | [diff] [blame] | 356 | .. class:: DSAPrivateKeyWithNumbers |
| 357 | |
| 358 | .. versionadded:: 0.5 |
| 359 | |
| 360 | Extends :class:`DSAPrivateKey`. |
| 361 | |
| 362 | .. method:: private_numbers() |
| 363 | |
| 364 | Create a |
| 365 | :class:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAPrivateNumbers` |
| 366 | object. |
| 367 | |
| 368 | :returns: A |
| 369 | :class:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAPrivateNumbers` |
| 370 | instance. |
| 371 | |
| 372 | |
Mohammed Attia | b416715 | 2014-03-04 03:29:56 +0200 | [diff] [blame] | 373 | .. class:: DSAPublicKey |
| 374 | |
| 375 | .. versionadded:: 0.3 |
| 376 | |
Mohammed Attia | edacb14 | 2014-03-17 12:28:23 +0200 | [diff] [blame] | 377 | A `DSA`_ public key. |
| 378 | |
| 379 | .. attribute:: key_size |
| 380 | |
| 381 | :type: int |
| 382 | |
| 383 | The bit length of the modulus. |
Mohammed Attia | b416715 | 2014-03-04 03:29:56 +0200 | [diff] [blame] | 384 | |
| 385 | .. method:: parameters() |
| 386 | |
Mohammed Attia | 71acc67 | 2014-03-04 19:20:45 +0200 | [diff] [blame] | 387 | :return: :class:`~cryptography.hazmat.primitives.interfaces.DSAParameters` |
Mohammed Attia | b416715 | 2014-03-04 03:29:56 +0200 | [diff] [blame] | 388 | |
Mohammed Attia | 71acc67 | 2014-03-04 19:20:45 +0200 | [diff] [blame] | 389 | The DSAParameters object associated with this public key. |
Mohammed Attia | b416715 | 2014-03-04 03:29:56 +0200 | [diff] [blame] | 390 | |
Mohammed Attia | 59edb61 | 2014-04-25 22:44:40 +0200 | [diff] [blame] | 391 | .. method:: verifier(signature, algorithm, backend) |
Mohammed Attia | b416715 | 2014-03-04 03:29:56 +0200 | [diff] [blame] | 392 | |
Mohammed Attia | 59edb61 | 2014-04-25 22:44:40 +0200 | [diff] [blame] | 393 | .. versionadded:: 0.4 |
Mohammed Attia | b416715 | 2014-03-04 03:29:56 +0200 | [diff] [blame] | 394 | |
Mohammed Attia | 59edb61 | 2014-04-25 22:44:40 +0200 | [diff] [blame] | 395 | Verify data was signed by the private key associated with this public |
| 396 | key. |
| 397 | |
Paul Kehrer | e0aeaf8 | 2014-05-01 11:58:23 -0500 | [diff] [blame] | 398 | :param bytes signature: The signature to verify. DER encoded as |
| 399 | specified in :rfc:`6979`. |
Mohammed Attia | 59edb61 | 2014-04-25 22:44:40 +0200 | [diff] [blame] | 400 | |
| 401 | :param algorithm: An instance of a |
| 402 | :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` |
| 403 | provider. |
| 404 | |
| 405 | :param backend: A |
| 406 | :class:`~cryptography.hazmat.backends.interfaces.DSABackend` |
| 407 | provider. |
| 408 | |
| 409 | :returns: |
| 410 | :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricVerificationContext` |
Mohammed Attia | b416715 | 2014-03-04 03:29:56 +0200 | [diff] [blame] | 411 | |
| 412 | |
Paul Kehrer | 9842964 | 2014-06-22 16:37:21 -0600 | [diff] [blame] | 413 | .. class:: DSAPublicKeyWithNumbers |
| 414 | |
| 415 | .. versionadded:: 0.5 |
| 416 | |
| 417 | Extends :class:`DSAPublicKey`. |
| 418 | |
Paul Kehrer | 5cfd211 | 2014-09-24 14:51:01 -0500 | [diff] [blame] | 419 | .. method:: public_numbers() |
Paul Kehrer | 9842964 | 2014-06-22 16:37:21 -0600 | [diff] [blame] | 420 | |
| 421 | Create a |
| 422 | :class:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAPublicNumbers` |
| 423 | object. |
| 424 | |
| 425 | :returns: A |
| 426 | :class:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAPublicNumbers` |
| 427 | instance. |
| 428 | |
| 429 | |
Alex Stapleton | 085f378 | 2014-04-01 16:18:17 +0100 | [diff] [blame] | 430 | .. class:: EllipticCurve |
| 431 | |
Alex Stapleton | 20c9903 | 2014-05-03 21:06:46 +0100 | [diff] [blame] | 432 | .. versionadded:: 0.5 |
Alex Stapleton | 085f378 | 2014-04-01 16:18:17 +0100 | [diff] [blame] | 433 | |
| 434 | A named elliptic curve. |
| 435 | |
| 436 | .. attribute:: name |
| 437 | |
| 438 | :type: string |
| 439 | |
| 440 | The name of the curve. Usually the name used for the ASN.1 OID such as |
Alex Stapleton | 6e52674 | 2014-05-23 22:06:06 +0100 | [diff] [blame] | 441 | ``secp256k1``. |
Alex Stapleton | 085f378 | 2014-04-01 16:18:17 +0100 | [diff] [blame] | 442 | |
| 443 | .. attribute:: key_size |
| 444 | |
| 445 | :type: int |
| 446 | |
Alex Stapleton | d436569 | 2014-05-26 09:25:25 +0100 | [diff] [blame] | 447 | The bit length of the curve's base point. |
Alex Stapleton | 085f378 | 2014-04-01 16:18:17 +0100 | [diff] [blame] | 448 | |
| 449 | |
Alex Gaynor | 645315b | 2014-06-23 11:55:55 -0700 | [diff] [blame] | 450 | Elliptic Curve |
| 451 | ~~~~~~~~~~~~~~ |
| 452 | |
Alex Stapleton | a1853f9 | 2014-04-18 11:38:28 +0100 | [diff] [blame] | 453 | .. class:: EllipticCurveSignatureAlgorithm |
| 454 | |
Alex Stapleton | 20c9903 | 2014-05-03 21:06:46 +0100 | [diff] [blame] | 455 | .. versionadded:: 0.5 |
Alex Stapleton | a1853f9 | 2014-04-18 11:38:28 +0100 | [diff] [blame] | 456 | |
| 457 | A signature algorithm for use with elliptic curve keys. |
| 458 | |
Alex Stapleton | 80228a1 | 2014-04-20 16:44:26 +0100 | [diff] [blame] | 459 | .. attribute:: algorithm |
| 460 | |
| 461 | :type: :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` |
| 462 | |
| 463 | The digest algorithm to be used with the signature scheme. |
| 464 | |
Alex Stapleton | a1853f9 | 2014-04-18 11:38:28 +0100 | [diff] [blame] | 465 | |
Alex Stapleton | 085f378 | 2014-04-01 16:18:17 +0100 | [diff] [blame] | 466 | .. class:: EllipticCurvePrivateKey |
| 467 | |
Alex Stapleton | 20c9903 | 2014-05-03 21:06:46 +0100 | [diff] [blame] | 468 | .. versionadded:: 0.5 |
Alex Stapleton | 085f378 | 2014-04-01 16:18:17 +0100 | [diff] [blame] | 469 | |
| 470 | An elliptic curve private key for use with an algorithm such as `ECDSA`_ or |
| 471 | `EdDSA`_. |
| 472 | |
Alex Stapleton | 33c9d83 | 2014-05-23 21:31:51 +0100 | [diff] [blame] | 473 | .. classmethod:: signer(signature_algorithm) |
Alex Stapleton | a1853f9 | 2014-04-18 11:38:28 +0100 | [diff] [blame] | 474 | Sign data which can be verified later by others using the public key. |
| 475 | |
| 476 | :param signature_algorithm: An instance of a |
| 477 | :class:`~cryptography.hazmat.primitives.interfaces.EllipticCurveSignatureAlgorithm` |
| 478 | provider. |
| 479 | |
Alex Stapleton | a1853f9 | 2014-04-18 11:38:28 +0100 | [diff] [blame] | 480 | :returns: |
| 481 | :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricSignatureContext` |
| 482 | |
Alex Stapleton | 085f378 | 2014-04-01 16:18:17 +0100 | [diff] [blame] | 483 | |
| 484 | :type: :class:`~cryptography.hazmat.primitives.interfaces.EllipticCurve` |
| 485 | |
| 486 | The elliptic curve for this key. |
| 487 | |
Alex Stapleton | 085f378 | 2014-04-01 16:18:17 +0100 | [diff] [blame] | 488 | .. method:: public_key() |
| 489 | |
| 490 | :return: :class:`~cryptography.hazmat.primitives.interfaces.EllipticCurvePublicKey` |
| 491 | |
| 492 | The EllipticCurvePublicKey object for this private key. |
| 493 | |
| 494 | |
Paul Kehrer | e025be2 | 2014-09-24 11:26:48 -0500 | [diff] [blame] | 495 | .. class:: EllipticCurvePrivateKeyWithNumbers |
| 496 | |
| 497 | .. versionadded:: 0.6 |
| 498 | |
| 499 | Extends :class:`EllipticCurvePrivateKey`. |
| 500 | |
| 501 | .. method:: private_numbers() |
| 502 | |
| 503 | Create a |
| 504 | :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateNumbers` |
| 505 | object. |
| 506 | |
| 507 | :returns: An |
| 508 | :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateNumbers` |
| 509 | instance. |
| 510 | |
| 511 | |
Alex Stapleton | 085f378 | 2014-04-01 16:18:17 +0100 | [diff] [blame] | 512 | .. class:: EllipticCurvePublicKey |
| 513 | |
Alex Stapleton | 20c9903 | 2014-05-03 21:06:46 +0100 | [diff] [blame] | 514 | .. versionadded:: 0.5 |
Alex Stapleton | 085f378 | 2014-04-01 16:18:17 +0100 | [diff] [blame] | 515 | |
| 516 | An elliptic curve public key. |
| 517 | |
Alex Stapleton | e47bafb | 2014-05-17 13:19:15 +0100 | [diff] [blame] | 518 | .. classmethod:: verifier(signature, signature_algorithm) |
Alex Stapleton | a1853f9 | 2014-04-18 11:38:28 +0100 | [diff] [blame] | 519 | Verify data was signed by the private key associated with this public |
| 520 | key. |
| 521 | |
Alex Stapleton | 80228a1 | 2014-04-20 16:44:26 +0100 | [diff] [blame] | 522 | :param bytes signature: The signature to verify. |
| 523 | |
Alex Stapleton | a1853f9 | 2014-04-18 11:38:28 +0100 | [diff] [blame] | 524 | :param signature_algorithm: An instance of a |
| 525 | :class:`~cryptography.hazmat.primitives.interfaces.EllipticCurveSignatureAlgorithm` |
| 526 | provider. |
| 527 | |
Alex Stapleton | a1853f9 | 2014-04-18 11:38:28 +0100 | [diff] [blame] | 528 | :returns: |
| 529 | :class:`~cryptography.hazmat.primitives.interfaces.AsymmetricSignatureContext` |
| 530 | |
Alex Stapleton | 085f378 | 2014-04-01 16:18:17 +0100 | [diff] [blame] | 531 | .. attribute:: curve |
| 532 | |
| 533 | :type: :class:`~cryptography.hazmat.primitives.interfaces.EllipticCurve` |
| 534 | |
| 535 | The elliptic curve for this key. |
| 536 | |
Alex Stapleton | 085f378 | 2014-04-01 16:18:17 +0100 | [diff] [blame] | 537 | |
Paul Kehrer | e025be2 | 2014-09-24 11:26:48 -0500 | [diff] [blame] | 538 | .. class:: EllipticCurvePublicKeyWithNumbers |
| 539 | |
| 540 | .. versionadded:: 0.6 |
| 541 | |
| 542 | Extends :class:`EllipticCurvePublicKey`. |
| 543 | |
Paul Kehrer | 5cfd211 | 2014-09-24 14:51:01 -0500 | [diff] [blame] | 544 | .. method:: public_numbers() |
Paul Kehrer | e025be2 | 2014-09-24 11:26:48 -0500 | [diff] [blame] | 545 | |
| 546 | Create a |
| 547 | :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicNumbers` |
| 548 | object. |
| 549 | |
| 550 | :returns: An |
| 551 | :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicNumbers` |
| 552 | instance. |
| 553 | |
| 554 | |
Alex Stapleton | c5fffd3 | 2014-03-18 15:29:00 +0000 | [diff] [blame] | 555 | Hash algorithms |
Alex Gaynor | 645315b | 2014-06-23 11:55:55 -0700 | [diff] [blame] | 556 | --------------- |
Paul Kehrer | e51a2db | 2014-01-29 11:49:35 -0600 | [diff] [blame] | 557 | |
| 558 | .. class:: HashAlgorithm |
| 559 | |
Paul Kehrer | e51a2db | 2014-01-29 11:49:35 -0600 | [diff] [blame] | 560 | .. attribute:: name |
| 561 | |
| 562 | :type: str |
| 563 | |
Paul Kehrer | 4c75a8c | 2014-01-29 12:20:37 -0600 | [diff] [blame] | 564 | The standard name for the hash algorithm, for example: ``"sha256"`` or |
| 565 | ``"whirlpool"``. |
Paul Kehrer | e51a2db | 2014-01-29 11:49:35 -0600 | [diff] [blame] | 566 | |
| 567 | .. attribute:: digest_size |
| 568 | |
| 569 | :type: int |
| 570 | |
| 571 | The size of the resulting digest in bytes. |
| 572 | |
| 573 | .. attribute:: block_size |
| 574 | |
| 575 | :type: int |
| 576 | |
| 577 | The internal block size of the hash algorithm in bytes. |
| 578 | |
| 579 | |
Ayrx | a0f9850 | 2014-04-15 19:17:03 +0800 | [diff] [blame] | 580 | .. class:: HashContext |
| 581 | |
| 582 | .. attribute:: algorithm |
| 583 | |
| 584 | A :class:`~cryptography.hazmat.primitives.interfaces.HashAlgorithm` that |
| 585 | will be used by this context. |
| 586 | |
| 587 | .. method:: update(data) |
| 588 | |
| 589 | :param data bytes: The data you want to hash. |
| 590 | |
| 591 | .. method:: finalize() |
| 592 | |
| 593 | :return: The final digest as bytes. |
| 594 | |
| 595 | .. method:: copy() |
| 596 | |
| 597 | :return: A :class:`~cryptography.hazmat.primitives.interfaces.HashContext` |
| 598 | that is a copy of the current context. |
| 599 | |
| 600 | |
Alex Stapleton | c5fffd3 | 2014-03-18 15:29:00 +0000 | [diff] [blame] | 601 | Key derivation functions |
Alex Gaynor | 645315b | 2014-06-23 11:55:55 -0700 | [diff] [blame] | 602 | ------------------------ |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 603 | |
| 604 | .. class:: KeyDerivationFunction |
| 605 | |
Alex Gaynor | 8454c51 | 2014-01-28 07:01:54 -0800 | [diff] [blame] | 606 | .. versionadded:: 0.2 |
| 607 | |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 608 | .. method:: derive(key_material) |
| 609 | |
Alex Gaynor | 5484f72 | 2014-01-28 05:46:15 -0800 | [diff] [blame] | 610 | :param key_material bytes: The input key material. Depending on what |
| 611 | key derivation function you are using this |
| 612 | could be either random material, or a user |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 613 | supplied password. |
Alex Gaynor | 5484f72 | 2014-01-28 05:46:15 -0800 | [diff] [blame] | 614 | :return: The new key. |
Alex Gaynor | e19e89f | 2014-01-28 06:58:43 -0800 | [diff] [blame] | 615 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 616 | :meth:`derive` or |
| 617 | :meth:`verify` is |
| 618 | called more than |
| 619 | once. |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 620 | |
Alex Gaynor | 5484f72 | 2014-01-28 05:46:15 -0800 | [diff] [blame] | 621 | This generates and returns a new key from the supplied key material. |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 622 | |
| 623 | .. method:: verify(key_material, expected_key) |
| 624 | |
Alex Gaynor | 5484f72 | 2014-01-28 05:46:15 -0800 | [diff] [blame] | 625 | :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] | 626 | ``key_material`` in :meth:`derive`. |
Alex Gaynor | 5484f72 | 2014-01-28 05:46:15 -0800 | [diff] [blame] | 627 | :param expected_key bytes: The expected result of deriving a new key, |
| 628 | this is the same as the return value of |
| 629 | :meth:`derive`. |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 630 | :raises cryptography.exceptions.InvalidKey: This is raised when the |
| 631 | derived key does not match |
| 632 | the expected key. |
Alex Gaynor | e19e89f | 2014-01-28 06:58:43 -0800 | [diff] [blame] | 633 | :raises cryptography.exceptions.AlreadyFinalized: This is raised when |
| 634 | :meth:`derive` or |
| 635 | :meth:`verify` is |
| 636 | called more than |
| 637 | once. |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 638 | |
Alex Gaynor | 5484f72 | 2014-01-28 05:46:15 -0800 | [diff] [blame] | 639 | This checks whether deriving a new key from the supplied |
| 640 | ``key_material`` generates the same key as the ``expected_key``, and |
| 641 | raises an exception if they do not match. This can be used for |
| 642 | something like checking whether a user's password attempt matches the |
| 643 | stored derived key. |
Alex Gaynor | b2774f5 | 2014-01-27 11:05:29 -0800 | [diff] [blame] | 644 | |
Ayrx | c812170 | 2014-04-15 19:02:05 +0800 | [diff] [blame] | 645 | |
Terry Chia | cc5e445 | 2014-10-12 15:35:21 +0800 | [diff] [blame^] | 646 | `Message Authentication Code`_ |
| 647 | ------------------------------ |
Ayrx | c812170 | 2014-04-15 19:02:05 +0800 | [diff] [blame] | 648 | |
| 649 | .. class:: CMACContext |
| 650 | |
| 651 | .. versionadded:: 0.4 |
| 652 | |
| 653 | .. method:: update(data) |
| 654 | |
| 655 | :param data bytes: The data you want to authenticate. |
| 656 | |
| 657 | .. method:: finalize() |
| 658 | |
Ayrx | 7964c17 | 2014-04-15 21:50:58 +0800 | [diff] [blame] | 659 | :return: The message authentication code. |
Ayrx | c812170 | 2014-04-15 19:02:05 +0800 | [diff] [blame] | 660 | |
| 661 | .. method:: copy() |
| 662 | |
| 663 | :return: A :class:`~cryptography.hazmat.primitives.interfaces.CMACContext` |
| 664 | that is a copy of the current context. |
| 665 | |
Terry Chia | cc5e445 | 2014-10-12 15:35:21 +0800 | [diff] [blame^] | 666 | .. class:: MACContext |
| 667 | |
| 668 | .. versionadded:: 0.7 |
| 669 | |
| 670 | .. method:: update(data) |
| 671 | |
| 672 | :param data bytes: The data you want to authenticate |
| 673 | |
| 674 | .. method:: finalize() |
| 675 | |
| 676 | :return: The message authentication code. |
| 677 | |
| 678 | .. method:: copy() |
| 679 | |
| 680 | :return: A :class:`~cryptography.hazmat.primitives.interfaces.MACContext` |
| 681 | that is a copy of the current context. |
| 682 | |
| 683 | .. method:: verify() |
| 684 | |
| 685 | :param signature bytes: The signature to verify. |
| 686 | |
| 687 | :raises cryptography.exceptions.InvalidSignature: This is raised when |
| 688 | the provided signature does not match the expected signature. |
Ayrx | c812170 | 2014-04-15 19:02:05 +0800 | [diff] [blame] | 689 | |
Paul Kehrer | 8e9c984 | 2014-02-13 12:23:27 -0600 | [diff] [blame] | 690 | .. _`RSA`: https://en.wikipedia.org/wiki/RSA_(cryptosystem) |
| 691 | .. _`Chinese remainder theorem`: https://en.wikipedia.org/wiki/Chinese_remainder_theorem |
Mohammed Attia | 604c78f | 2014-03-04 03:56:08 +0200 | [diff] [blame] | 692 | .. _`DSA`: https://en.wikipedia.org/wiki/Digital_Signature_Algorithm |
Ayrx | 83cd3f8 | 2014-04-15 21:56:32 +0800 | [diff] [blame] | 693 | .. _`CMAC`: https://en.wikipedia.org/wiki/CMAC |
Alex Stapleton | 085f378 | 2014-04-01 16:18:17 +0100 | [diff] [blame] | 694 | .. _`ECDSA`: http://en.wikipedia.org/wiki/ECDSA |
| 695 | .. _`EdDSA`: http://en.wikipedia.org/wiki/EdDSA |