Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 1 | Symmetric Encryption |
| 2 | ==================== |
| 3 | |
| 4 | Symmetric encryption is a way to encrypt (hide the plaintext value) material |
| 5 | where the encrypter and decrypter both use the same key. |
| 6 | |
Alex Gaynor | 65678d0 | 2013-08-08 15:19:19 -0700 | [diff] [blame^] | 7 | .. class:: cryptography.primitives.block.BlockCipher(cipher, mode) |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 8 | |
Alex Gaynor | 65678d0 | 2013-08-08 15:19:19 -0700 | [diff] [blame^] | 9 | Block ciphers work by encrypting content in chunks, often 64- or 128-bits. |
| 10 | Theycombine an underlying algorithm (such as AES), with a mode (such as CBC, |
| 11 | CTR, or GCM). A simple example of encrypting content with AES is: |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 12 | |
Alex Gaynor | 65678d0 | 2013-08-08 15:19:19 -0700 | [diff] [blame^] | 13 | .. code-block:: pycon |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 14 | |
Alex Gaynor | 65678d0 | 2013-08-08 15:19:19 -0700 | [diff] [blame^] | 15 | >>> from cryptography.primitives.block import BlockCipher, cipher, mode |
| 16 | >>> cipher = BlockCipher(cipher.AES(key), mode.CBC(iv)) |
| 17 | >>> cipher.encrypt("my secret message") + cipher.finalize() |
| 18 | # The ciphertext |
| 19 | [...] |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 20 | |
Alex Gaynor | 65678d0 | 2013-08-08 15:19:19 -0700 | [diff] [blame^] | 21 | Here ``key`` is the encryption key (which must be kept secret), and ``iv`` |
| 22 | is the initialization vector (which must be random). Exactly what form |
| 23 | these values should take is described for each of the ciphers and modes. |
Alex Gaynor | 0ca7fdb | 2013-08-08 07:35:26 -0700 | [diff] [blame] | 24 | |
Alex Gaynor | 65678d0 | 2013-08-08 15:19:19 -0700 | [diff] [blame^] | 25 | ``encrypt()`` should be called repeatedly with additional plaintext, and it |
| 26 | will return the encrypted bytes, if there isn't enough data, it will buffer |
| 27 | it internally. ``finalize()`` should be called at the end, and will return |
| 28 | whatever data is left. |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 29 | |
| 30 | Ciphers |
| 31 | ~~~~~~~ |
| 32 | |
Alex Gaynor | 4dd1c27 | 2013-08-08 11:39:21 -0700 | [diff] [blame] | 33 | .. class:: cryptography.primitives.block.cipher.AES(key) |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 34 | |
Alex Gaynor | 1e3f81f | 2013-08-08 11:31:43 -0700 | [diff] [blame] | 35 | AES (Advanced Encryption Standard) is a block cipher standardized by NIST. |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 36 | AES is both fast, and cryptographically strong. It is a good default |
| 37 | choice for encryption. |
| 38 | |
| 39 | :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits. |
Alex Gaynor | 48ec9a3 | 2013-08-08 11:13:46 -0700 | [diff] [blame] | 40 | This must be kept secret. |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 41 | |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 42 | |
| 43 | Modes |
| 44 | ~~~~~ |
| 45 | |
Alex Gaynor | 4dd1c27 | 2013-08-08 11:39:21 -0700 | [diff] [blame] | 46 | .. class:: cryptography.primitives.block.mode.CBC(initialization_vector) |
Alex Gaynor | 48ec9a3 | 2013-08-08 11:13:46 -0700 | [diff] [blame] | 47 | |
| 48 | CBC (Cipher block chaining) is a mode of operation for block ciphers. It is |
| 49 | considered cryptographically strong. |
| 50 | |
| 51 | :param bytes initialization_vector: Must be random bytes. They do not need |
| 52 | to be kept secret (they can be included |
Alex Gaynor | 2dc2b86 | 2013-08-08 11:58:04 -0700 | [diff] [blame] | 53 | in a transmitted message). Must be the |
| 54 | same number of bytes as the |
Alex Gaynor | 6badd9b | 2013-08-08 14:59:53 -0700 | [diff] [blame] | 55 | ``block_size`` of the cipher. Do not |
| 56 | reuse an ``initialization_vector`` with |
| 57 | a given ``key``. |