Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 1 | Symmetric Encryption |
| 2 | ==================== |
| 3 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame^] | 4 | .. currentmodule:: cryptography.primitives.block |
| 5 | |
Donald Stufft | 173de98 | 2013-08-12 07:34:39 -0400 | [diff] [blame] | 6 | .. testsetup:: |
| 7 | |
| 8 | import binascii |
| 9 | key = binascii.unhexlify(b"0" * 32) |
| 10 | iv = binascii.unhexlify(b"0" * 32) |
| 11 | |
| 12 | |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 13 | Symmetric encryption is a way to encrypt (hide the plaintext value) material |
| 14 | where the encrypter and decrypter both use the same key. |
| 15 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame^] | 16 | .. class:: BlockCipher(cipher, mode) |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 17 | |
Alex Gaynor | 65678d0 | 2013-08-08 15:19:19 -0700 | [diff] [blame] | 18 | 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] | 19 | They combine an underlying algorithm (such as AES), with a mode (such as |
Paul Kehrer | d1afe39 | 2013-10-22 08:24:44 -0500 | [diff] [blame] | 20 | CBC, CTR, or GCM). A simple example of encrypting (and then decrypting) |
Paul Kehrer | 653463f | 2013-10-21 17:55:01 -0500 | [diff] [blame] | 21 | content with AES is: |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 22 | |
Donald Stufft | 173de98 | 2013-08-12 07:34:39 -0400 | [diff] [blame] | 23 | .. doctest:: |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 24 | |
Alex Gaynor | 641a3a0 | 2013-08-10 15:46:07 -0400 | [diff] [blame] | 25 | >>> from cryptography.primitives.block import BlockCipher, ciphers, modes |
Alex Gaynor | acc787a | 2013-08-10 15:52:40 -0400 | [diff] [blame] | 26 | >>> cipher = BlockCipher(ciphers.AES(key), modes.CBC(iv)) |
Paul Kehrer | 3e0895c | 2013-10-21 22:19:29 -0500 | [diff] [blame] | 27 | >>> encryptor = cipher.encryptor() |
| 28 | >>> ct = encryptor.update(b"a secret message") + encryptor.finalize() |
| 29 | >>> decryptor = cipher.decryptor() |
| 30 | >>> decryptor.update(ct) + decryptor.finalize() |
Paul Kehrer | f6cf956 | 2013-10-22 10:36:00 -0500 | [diff] [blame] | 31 | 'a secret message' |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 32 | |
Alex Gaynor | e62aa40 | 2013-08-08 15:23:11 -0700 | [diff] [blame] | 33 | :param cipher: One of the ciphers described below. |
| 34 | :param mode: One of the modes described below. |
Alex Gaynor | 0ca7fdb | 2013-08-08 07:35:26 -0700 | [diff] [blame] | 35 | |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 36 | .. method:: encryptor() |
Alex Gaynor | 09515f0 | 2013-08-08 15:26:55 -0700 | [diff] [blame] | 37 | |
David Reid | 63ba665 | 2013-10-22 14:09:19 -0700 | [diff] [blame] | 38 | :return: An encrypting |
| 39 | :class:`~cryptography.primitives.interfaces.CipherContext` |
| 40 | provider. |
Alex Gaynor | e62aa40 | 2013-08-08 15:23:11 -0700 | [diff] [blame] | 41 | |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 42 | .. method:: decryptor() |
| 43 | |
David Reid | 63ba665 | 2013-10-22 14:09:19 -0700 | [diff] [blame] | 44 | :return: A decrypting |
| 45 | :class:`~cryptography.primitives.interfaces.CipherContext` |
| 46 | provider. |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 47 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame^] | 48 | .. currentmodule:: cryptography.primitives.interfaces |
| 49 | |
| 50 | .. class:: CipherContext() |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 51 | |
| 52 | When calling ``encryptor()`` or ``decryptor()`` on a BlockCipher object you |
| 53 | will receive a return object conforming to the CipherContext interface. You |
| 54 | can then call ``update(data)`` with data until you have fed everything into |
| 55 | the context. Once that is done call ``finalize()`` to finish the operation and |
| 56 | obtain the remainder of the data. |
| 57 | |
| 58 | |
| 59 | .. method:: update(data) |
| 60 | |
| 61 | :param bytes data: The text you wish to pass into the context. |
| 62 | :return bytes: Returns the data that was encrypted or decrypted. |
Alex Gaynor | e62aa40 | 2013-08-08 15:23:11 -0700 | [diff] [blame] | 63 | |
| 64 | .. method:: finalize() |
| 65 | |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 66 | :return bytes: Returns the remainder of the data. |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 67 | |
| 68 | Ciphers |
| 69 | ~~~~~~~ |
| 70 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame^] | 71 | .. currentmodule:: cryptography.primitives.block.ciphers |
| 72 | |
| 73 | .. class:: AES(key) |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 74 | |
Alex Gaynor | 1e3f81f | 2013-08-08 11:31:43 -0700 | [diff] [blame] | 75 | AES (Advanced Encryption Standard) is a block cipher standardized by NIST. |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 76 | AES is both fast, and cryptographically strong. It is a good default |
| 77 | choice for encryption. |
| 78 | |
| 79 | :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits. |
Alex Gaynor | 48ec9a3 | 2013-08-08 11:13:46 -0700 | [diff] [blame] | 80 | This must be kept secret. |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 81 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame^] | 82 | .. class:: Camellia(key) |
Paul Kehrer | dff22d4 | 2013-09-27 13:43:06 -0500 | [diff] [blame] | 83 | |
| 84 | Camellia is a block cipher approved for use by CRYPTREC and ISO/IEC. |
| 85 | It is considered to have comparable security and performance to AES, but |
| 86 | is not as widely studied or deployed. |
| 87 | |
| 88 | :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits. |
| 89 | This must be kept secret. |
| 90 | |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 91 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame^] | 92 | .. class:: TripleDES(key) |
Alex Gaynor | aeb714c | 2013-09-09 18:06:14 -0700 | [diff] [blame] | 93 | |
Alex Gaynor | 2f355d1 | 2013-09-09 18:09:26 -0700 | [diff] [blame] | 94 | Triple DES (Data Encryption Standard), sometimes refered to as 3DES, is a |
Alex Gaynor | 17adce6 | 2013-10-16 17:04:40 -0700 | [diff] [blame] | 95 | block cipher standardized by NIST. Triple DES has known cryptoanalytic |
| 96 | flaws, however none of them currently enable a practical attack. |
| 97 | Nonetheless, Triples DES is not reccomended for new applications because it |
Alex Gaynor | fbcc564 | 2013-10-22 08:26:00 -0700 | [diff] [blame] | 98 | is incredibly slow; old applications should consider moving away from it. |
Alex Gaynor | aeb714c | 2013-09-09 18:06:14 -0700 | [diff] [blame] | 99 | |
| 100 | :param bytes key: The secret key, either ``64``, ``128``, or ``192`` bits |
| 101 | (note that DES functionally uses ``56``, ``112``, or |
| 102 | ``168`` bits of the key, there is a parity byte in each |
| 103 | component of the key), in some materials these are |
| 104 | referred to as being up to three separate keys (each |
| 105 | ``56`` bits long), they can simply be concatenated to |
| 106 | produce the full key. This must be kept secret. |
| 107 | |
| 108 | |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 109 | Modes |
| 110 | ~~~~~ |
| 111 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame^] | 112 | .. currentmodule:: cryptography.primitives.block.modes |
| 113 | |
| 114 | .. class:: CBC(initialization_vector) |
Alex Gaynor | 48ec9a3 | 2013-08-08 11:13:46 -0700 | [diff] [blame] | 115 | |
| 116 | CBC (Cipher block chaining) is a mode of operation for block ciphers. It is |
| 117 | considered cryptographically strong. |
| 118 | |
| 119 | :param bytes initialization_vector: Must be random bytes. They do not need |
| 120 | to be kept secret (they can be included |
Alex Gaynor | 2dc2b86 | 2013-08-08 11:58:04 -0700 | [diff] [blame] | 121 | in a transmitted message). Must be the |
| 122 | same number of bytes as the |
Alex Gaynor | 6badd9b | 2013-08-08 14:59:53 -0700 | [diff] [blame] | 123 | ``block_size`` of the cipher. Do not |
| 124 | reuse an ``initialization_vector`` with |
| 125 | a given ``key``. |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 126 | |
Paul Kehrer | 4506428 | 2013-10-17 13:41:53 -0500 | [diff] [blame] | 127 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame^] | 128 | .. class:: CTR(nonce) |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 129 | |
Paul Kehrer | 4506428 | 2013-10-17 13:41:53 -0500 | [diff] [blame] | 130 | .. warning:: |
| 131 | |
| 132 | Counter mode is not recommended for use with block ciphers that have a |
| 133 | block size of less than 128-bits. |
| 134 | |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 135 | CTR (Counter) is a mode of operation for block ciphers. It is considered |
| 136 | cryptographically strong. |
| 137 | |
Paul Kehrer | 89b3dd3 | 2013-10-17 14:02:45 -0500 | [diff] [blame] | 138 | :param bytes nonce: Should be random bytes. It is critical to never reuse a |
| 139 | ``nonce`` with a given key. Any reuse of a nonce |
| 140 | with the same key compromises the security of every |
| 141 | message encrypted with that key. Must be the same |
| 142 | number of bytes as the ``block_size`` of the cipher |
| 143 | with a given key. The nonce does not need to be kept |
| 144 | secret and may be included alongside the ciphertext. |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 145 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame^] | 146 | .. class:: OFB(initialization_vector) |
Paul Kehrer | 6f412a0 | 2013-09-10 21:30:50 -0500 | [diff] [blame] | 147 | |
| 148 | OFB (Output Feedback) is a mode of operation for block ciphers. It |
| 149 | transforms a block cipher into a stream cipher. |
| 150 | |
David Reid | f1a39bd | 2013-09-11 16:28:42 -0700 | [diff] [blame] | 151 | :param bytes initialization_vector: Must be random bytes. They do not need |
| 152 | to be kept secret (they can be included |
| 153 | in a transmitted message). Must be the |
| 154 | same number of bytes as the |
| 155 | ``block_size`` of the cipher. Do not |
| 156 | reuse an ``initialization_vector`` with |
| 157 | a given ``key``. |
Paul Kehrer | 6f412a0 | 2013-09-10 21:30:50 -0500 | [diff] [blame] | 158 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame^] | 159 | .. class:: CFB(initialization_vector) |
Paul Kehrer | 4223df7 | 2013-09-11 09:48:04 -0500 | [diff] [blame] | 160 | |
| 161 | CFB (Cipher Feedback) is a mode of operation for block ciphers. It |
| 162 | transforms a block cipher into a stream cipher. |
| 163 | |
| 164 | :param bytes initialization_vector: Must be random bytes. They do not need |
| 165 | to be kept secret (they can be included |
| 166 | in a transmitted message). Must be the |
| 167 | same number of bytes as the |
| 168 | ``block_size`` of the cipher. Do not |
| 169 | reuse an ``initialization_vector`` with |
| 170 | a given ``key``. |
| 171 | |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 172 | |
| 173 | Insecure Modes |
| 174 | -------------- |
| 175 | |
Alex Gaynor | cd413a3 | 2013-09-10 18:59:43 -0700 | [diff] [blame] | 176 | .. warning:: |
| 177 | |
| 178 | These modes are insecure. New applications should never make use of them, |
| 179 | and existing applications should strongly consider migrating away. |
| 180 | |
| 181 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame^] | 182 | .. class:: ECB() |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 183 | |
| 184 | ECB (Electronic Code Book) is the simplest mode of operation for block |
Alex Gaynor | cd413a3 | 2013-09-10 18:59:43 -0700 | [diff] [blame] | 185 | ciphers. Each block of data is encrypted in the same way. This means |
| 186 | identical plaintext blocks will always result in identical ciphertext |
| 187 | blocks, and thus result in information leakage |