Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 1 | Symmetric Encryption |
| 2 | ==================== |
| 3 | |
Donald Stufft | 173de98 | 2013-08-12 07:34:39 -0400 | [diff] [blame] | 4 | .. testsetup:: |
| 5 | |
| 6 | import binascii |
| 7 | key = binascii.unhexlify(b"0" * 32) |
| 8 | iv = binascii.unhexlify(b"0" * 32) |
| 9 | |
| 10 | |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 11 | Symmetric encryption is a way to encrypt (hide the plaintext value) material |
| 12 | where the encrypter and decrypter both use the same key. |
| 13 | |
Alex Gaynor | 65678d0 | 2013-08-08 15:19:19 -0700 | [diff] [blame] | 14 | .. class:: cryptography.primitives.block.BlockCipher(cipher, mode) |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 15 | |
Alex Gaynor | 65678d0 | 2013-08-08 15:19:19 -0700 | [diff] [blame] | 16 | Block ciphers work by encrypting content in chunks, often 64- or 128-bits. |
Alex Gaynor | b12f76e | 2013-08-08 19:05:18 -0700 | [diff] [blame] | 17 | They combine an underlying algorithm (such as AES), with a mode (such as |
| 18 | CBC, CTR, or GCM). A simple example of encrypting content with AES is: |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 19 | |
Donald Stufft | 173de98 | 2013-08-12 07:34:39 -0400 | [diff] [blame] | 20 | .. doctest:: |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 21 | |
Alex Gaynor | 641a3a0 | 2013-08-10 15:46:07 -0400 | [diff] [blame] | 22 | >>> from cryptography.primitives.block import BlockCipher, ciphers, modes |
Alex Gaynor | acc787a | 2013-08-10 15:52:40 -0400 | [diff] [blame] | 23 | >>> cipher = BlockCipher(ciphers.AES(key), modes.CBC(iv)) |
Donald Stufft | 292112b | 2013-08-11 14:32:17 -0400 | [diff] [blame] | 24 | >>> cipher.encrypt(b"a secret message") + cipher.finalize() |
Donald Stufft | 173de98 | 2013-08-12 07:34:39 -0400 | [diff] [blame] | 25 | '...' |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 26 | |
Alex Gaynor | e62aa40 | 2013-08-08 15:23:11 -0700 | [diff] [blame] | 27 | :param cipher: One of the ciphers described below. |
| 28 | :param mode: One of the modes described below. |
Alex Gaynor | 0ca7fdb | 2013-08-08 07:35:26 -0700 | [diff] [blame] | 29 | |
Alex Gaynor | 09515f0 | 2013-08-08 15:26:55 -0700 | [diff] [blame] | 30 | ``encrypt()`` should be called repeatedly with new plaintext, and once the |
| 31 | full plaintext is fed in, ``finalize()`` should be called. |
| 32 | |
Alex Gaynor | e62aa40 | 2013-08-08 15:23:11 -0700 | [diff] [blame] | 33 | .. method:: encrypt(plaintext) |
| 34 | |
| 35 | :param bytes plaintext: The text you wish to encrypt. |
| 36 | :return bytes: Returns the ciphertext that was added. |
| 37 | |
| 38 | .. method:: finalize() |
| 39 | |
| 40 | :return bytes: Returns the remainder of the ciphertext. |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 41 | |
| 42 | Ciphers |
| 43 | ~~~~~~~ |
| 44 | |
Alex Gaynor | 641a3a0 | 2013-08-10 15:46:07 -0400 | [diff] [blame] | 45 | .. class:: cryptography.primitives.block.ciphers.AES(key) |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 46 | |
Alex Gaynor | 1e3f81f | 2013-08-08 11:31:43 -0700 | [diff] [blame] | 47 | AES (Advanced Encryption Standard) is a block cipher standardized by NIST. |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 48 | AES is both fast, and cryptographically strong. It is a good default |
| 49 | choice for encryption. |
| 50 | |
| 51 | :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits. |
Alex Gaynor | 48ec9a3 | 2013-08-08 11:13:46 -0700 | [diff] [blame] | 52 | This must be kept secret. |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 53 | |
Paul Kehrer | dff22d4 | 2013-09-27 13:43:06 -0500 | [diff] [blame] | 54 | .. class:: cryptography.primitives.block.ciphers.Camellia(key) |
| 55 | |
| 56 | Camellia is a block cipher approved for use by CRYPTREC and ISO/IEC. |
| 57 | It is considered to have comparable security and performance to AES, but |
| 58 | is not as widely studied or deployed. |
| 59 | |
| 60 | :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits. |
| 61 | This must be kept secret. |
| 62 | |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 63 | |
| 64 | Modes |
| 65 | ~~~~~ |
| 66 | |
Alex Gaynor | 641a3a0 | 2013-08-10 15:46:07 -0400 | [diff] [blame] | 67 | .. class:: cryptography.primitives.block.modes.CBC(initialization_vector) |
Alex Gaynor | 48ec9a3 | 2013-08-08 11:13:46 -0700 | [diff] [blame] | 68 | |
| 69 | CBC (Cipher block chaining) is a mode of operation for block ciphers. It is |
| 70 | considered cryptographically strong. |
| 71 | |
| 72 | :param bytes initialization_vector: Must be random bytes. They do not need |
| 73 | to be kept secret (they can be included |
Alex Gaynor | 2dc2b86 | 2013-08-08 11:58:04 -0700 | [diff] [blame] | 74 | in a transmitted message). Must be the |
| 75 | same number of bytes as the |
Alex Gaynor | 6badd9b | 2013-08-08 14:59:53 -0700 | [diff] [blame] | 76 | ``block_size`` of the cipher. Do not |
| 77 | reuse an ``initialization_vector`` with |
| 78 | a given ``key``. |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 79 | |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame^] | 80 | .. class:: cryptography.primitives.block.modes.CTR(nonce) |
| 81 | |
| 82 | CTR (Counter) is a mode of operation for block ciphers. It is considered |
| 83 | cryptographically strong. |
| 84 | |
| 85 | :param bytes nonce: Must be random bytes. They do not need to be kept |
| 86 | secret (they can be included in a transmitted |
| 87 | message). Must be the same number of bytes as the |
| 88 | ``block_size`` of the cipher. It is critical to |
| 89 | never reuse a ``nonce`` with a given ``key``. Unlike |
| 90 | CBC, reusing a nonce compromises the security of |
| 91 | all data encrypted under the key (see: two time pad). |
| 92 | |
David Reid | f1a39bd | 2013-09-11 16:28:42 -0700 | [diff] [blame] | 93 | .. class:: cryptography.primitives.block.modes.OFB(initialization_vector) |
Paul Kehrer | 6f412a0 | 2013-09-10 21:30:50 -0500 | [diff] [blame] | 94 | |
| 95 | OFB (Output Feedback) is a mode of operation for block ciphers. It |
| 96 | transforms a block cipher into a stream cipher. |
| 97 | |
David Reid | f1a39bd | 2013-09-11 16:28:42 -0700 | [diff] [blame] | 98 | :param bytes initialization_vector: Must be random bytes. They do not need |
| 99 | to be kept secret (they can be included |
| 100 | in a transmitted message). Must be the |
| 101 | same number of bytes as the |
| 102 | ``block_size`` of the cipher. Do not |
| 103 | reuse an ``initialization_vector`` with |
| 104 | a given ``key``. |
Paul Kehrer | 6f412a0 | 2013-09-10 21:30:50 -0500 | [diff] [blame] | 105 | |
Paul Kehrer | 4223df7 | 2013-09-11 09:48:04 -0500 | [diff] [blame] | 106 | .. class:: cryptography.primitives.block.modes.CFB(initialization_vector) |
| 107 | |
| 108 | CFB (Cipher Feedback) is a mode of operation for block ciphers. It |
| 109 | transforms a block cipher into a stream cipher. |
| 110 | |
| 111 | :param bytes initialization_vector: Must be random bytes. They do not need |
| 112 | to be kept secret (they can be included |
| 113 | in a transmitted message). Must be the |
| 114 | same number of bytes as the |
| 115 | ``block_size`` of the cipher. Do not |
| 116 | reuse an ``initialization_vector`` with |
| 117 | a given ``key``. |
| 118 | |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 119 | |
| 120 | Insecure Modes |
| 121 | -------------- |
| 122 | |
Alex Gaynor | cd413a3 | 2013-09-10 18:59:43 -0700 | [diff] [blame] | 123 | .. warning:: |
| 124 | |
| 125 | These modes are insecure. New applications should never make use of them, |
| 126 | and existing applications should strongly consider migrating away. |
| 127 | |
| 128 | |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 129 | .. class:: cryptography.primitives.block.modes.ECB() |
| 130 | |
| 131 | ECB (Electronic Code Book) is the simplest mode of operation for block |
Alex Gaynor | cd413a3 | 2013-09-10 18:59:43 -0700 | [diff] [blame] | 132 | ciphers. Each block of data is encrypted in the same way. This means |
| 133 | identical plaintext blocks will always result in identical ciphertext |
| 134 | blocks, and thus result in information leakage |