Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 1 | .. hazmat:: |
Donald Stufft | d8f0118 | 2013-10-27 16:59:56 -0400 | [diff] [blame] | 2 | |
| 3 | |
Donald Stufft | e51fb93 | 2013-10-27 17:26:17 -0400 | [diff] [blame] | 4 | Symmetric Encryption |
| 5 | ==================== |
| 6 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 7 | .. currentmodule:: cryptography.hazmat.primitives.ciphers |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 8 | |
Donald Stufft | 173de98 | 2013-08-12 07:34:39 -0400 | [diff] [blame] | 9 | .. testsetup:: |
| 10 | |
| 11 | import binascii |
| 12 | key = binascii.unhexlify(b"0" * 32) |
| 13 | iv = binascii.unhexlify(b"0" * 32) |
| 14 | |
| 15 | |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 16 | Symmetric encryption is a way to encrypt (hide the plaintext value) material |
Alex Gaynor | b317c7a | 2013-11-15 16:45:52 -0800 | [diff] [blame] | 17 | where the sender and receiver both use the same key. Note that symmetric |
Alex Gaynor | ab5f011 | 2013-11-08 10:34:00 -0800 | [diff] [blame] | 18 | encryption is **not** sufficient for most applications, because it only |
| 19 | provides secrecy (an attacker can't see the message) but not authenticity (an |
| 20 | attacker can create bogus messages and force the application to decrypt them). |
Alex Gaynor | 9316f4c | 2013-11-15 16:38:42 -0800 | [diff] [blame] | 21 | For this reason it is *strongly* recommended to combine encryption with a |
Alex Gaynor | ab5f011 | 2013-11-08 10:34:00 -0800 | [diff] [blame] | 22 | message authentication code, such as :doc:`HMAC </hazmat/primitives/hmac>`, in |
| 23 | an "encrypt-then-MAC" formulation as `described by Colin Percival`_. |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 24 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 25 | .. class:: Cipher(algorithm, mode) |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 26 | |
Alex Gaynor | ab5f011 | 2013-11-08 10:34:00 -0800 | [diff] [blame] | 27 | Cipher objects combine an algorithm (such as |
| 28 | :class:`~cryptography.hazmat.primitives.ciphers.algorithms.AES`) with a |
| 29 | mode (such as |
| 30 | :class:`~cryptography.hazmat.primitives.ciphers.modes.CBC` or |
| 31 | :class:`~cryptography.hazmat.primitives.ciphers.modes.CTR`). A simple |
| 32 | example of encrypting (and then decrypting) content with AES is: |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 33 | |
Donald Stufft | 173de98 | 2013-08-12 07:34:39 -0400 | [diff] [blame] | 34 | .. doctest:: |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 35 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 36 | >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes |
| 37 | >>> cipher = Cipher(algorithms.AES(key), modes.CBC(iv)) |
Paul Kehrer | 3e0895c | 2013-10-21 22:19:29 -0500 | [diff] [blame] | 38 | >>> encryptor = cipher.encryptor() |
| 39 | >>> ct = encryptor.update(b"a secret message") + encryptor.finalize() |
| 40 | >>> decryptor = cipher.decryptor() |
| 41 | >>> decryptor.update(ct) + decryptor.finalize() |
Paul Kehrer | f6cf956 | 2013-10-22 10:36:00 -0500 | [diff] [blame] | 42 | 'a secret message' |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 43 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 44 | :param algorithms: One of the algorithms described below. |
Alex Gaynor | e62aa40 | 2013-08-08 15:23:11 -0700 | [diff] [blame] | 45 | :param mode: One of the modes described below. |
Alex Gaynor | 0ca7fdb | 2013-08-08 07:35:26 -0700 | [diff] [blame] | 46 | |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 47 | .. method:: encryptor() |
Alex Gaynor | 09515f0 | 2013-08-08 15:26:55 -0700 | [diff] [blame] | 48 | |
David Reid | 63ba665 | 2013-10-22 14:09:19 -0700 | [diff] [blame] | 49 | :return: An encrypting |
Donald Stufft | f04317a | 2013-10-27 16:44:30 -0400 | [diff] [blame] | 50 | :class:`~cryptography.hazmat.primitives.interfaces.CipherContext` |
David Reid | 63ba665 | 2013-10-22 14:09:19 -0700 | [diff] [blame] | 51 | provider. |
Alex Gaynor | e62aa40 | 2013-08-08 15:23:11 -0700 | [diff] [blame] | 52 | |
Alex Gaynor | f1a3fc0 | 2013-11-02 14:03:34 -0700 | [diff] [blame] | 53 | If the backend doesn't support the requested combination of ``cipher`` |
Alex Gaynor | 3949f11 | 2013-11-02 16:57:10 -0700 | [diff] [blame] | 54 | and ``mode`` an :class:`cryptography.exceptions.UnsupportedAlgorithm` |
| 55 | will be raised. |
Alex Gaynor | f1a3fc0 | 2013-11-02 14:03:34 -0700 | [diff] [blame] | 56 | |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 57 | .. method:: decryptor() |
| 58 | |
David Reid | 63ba665 | 2013-10-22 14:09:19 -0700 | [diff] [blame] | 59 | :return: A decrypting |
Donald Stufft | f04317a | 2013-10-27 16:44:30 -0400 | [diff] [blame] | 60 | :class:`~cryptography.hazmat.primitives.interfaces.CipherContext` |
David Reid | 63ba665 | 2013-10-22 14:09:19 -0700 | [diff] [blame] | 61 | provider. |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 62 | |
Alex Gaynor | f1a3fc0 | 2013-11-02 14:03:34 -0700 | [diff] [blame] | 63 | If the backend doesn't support the requested combination of ``cipher`` |
Alex Gaynor | 3949f11 | 2013-11-02 16:57:10 -0700 | [diff] [blame] | 64 | and ``mode`` an :class:`cryptography.exceptions.UnsupportedAlgorithm` |
| 65 | will be raised. |
Alex Gaynor | f1a3fc0 | 2013-11-02 14:03:34 -0700 | [diff] [blame] | 66 | |
| 67 | |
Donald Stufft | f04317a | 2013-10-27 16:44:30 -0400 | [diff] [blame] | 68 | .. currentmodule:: cryptography.hazmat.primitives.interfaces |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 69 | |
Alex Gaynor | b2d5efd | 2013-10-29 11:15:30 -0700 | [diff] [blame] | 70 | .. class:: CipherContext |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 71 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 72 | When calling ``encryptor()`` or ``decryptor()`` on a ``Cipher`` object |
Alex Gaynor | b2d5efd | 2013-10-29 11:15:30 -0700 | [diff] [blame] | 73 | you will receive a return object conforming to the ``CipherContext`` |
| 74 | interface. You can then call ``update(data)`` with data until you have fed |
| 75 | everything into the context. Once that is done call ``finalize()`` to |
| 76 | finish the operation and obtain the remainder of the data. |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 77 | |
Alex Gaynor | 797dd83 | 2013-11-22 13:08:58 -0800 | [diff] [blame] | 78 | Block ciphers require that plaintext or ciphertext always be a multiple of |
| 79 | their block size, because of that **padding** is often required to make a |
| 80 | message the correct size. ``CipherContext`` will not automatically apply |
| 81 | any padding; you'll need to add your own. For block ciphers the reccomended |
| 82 | padding is :class:`cryptography.hazmat.primitives.padding.PKCS7`. If you |
| 83 | are using a stream cipher mode (such as |
| 84 | :class:`cryptography.hazmat.primitives.modes.CTR`) you don't have to worry |
| 85 | about this. |
| 86 | |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 87 | .. method:: update(data) |
| 88 | |
Alex Gaynor | b2d5efd | 2013-10-29 11:15:30 -0700 | [diff] [blame] | 89 | :param bytes data: The data you wish to pass into the context. |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 90 | :return bytes: Returns the data that was encrypted or decrypted. |
Alex Gaynor | 34511c6 | 2013-11-13 13:30:30 -0800 | [diff] [blame] | 91 | :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize` |
Alex Gaynor | e62aa40 | 2013-08-08 15:23:11 -0700 | [diff] [blame] | 92 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 93 | When the ``Cipher`` was constructed in a mode that turns it into a |
Alex Gaynor | fc09a7c | 2013-11-01 14:43:02 -0700 | [diff] [blame] | 94 | stream cipher (e.g. |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 95 | :class:`cryptography.hazmat.primitives.ciphers.modes.CTR`), this will |
Alex Gaynor | bf2de74 | 2013-11-01 14:48:19 -0700 | [diff] [blame] | 96 | return bytes immediately, however in other modes it will return chunks, |
| 97 | whose size is determined by the cipher's block size. |
Alex Gaynor | d1f0201 | 2013-11-01 14:12:35 -0700 | [diff] [blame] | 98 | |
Alex Gaynor | e62aa40 | 2013-08-08 15:23:11 -0700 | [diff] [blame] | 99 | .. method:: finalize() |
| 100 | |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 101 | :return bytes: Returns the remainder of the data. |
Alex Gaynor | 797dd83 | 2013-11-22 13:08:58 -0800 | [diff] [blame] | 102 | :raises cryptography.exceptions.IncorrectPadding: This is raised when |
| 103 | the data provided |
| 104 | isn't correctly |
| 105 | padded to be a |
| 106 | multiple of the |
| 107 | algorithm's block |
| 108 | size. |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 109 | |
Alex Gaynor | 34511c6 | 2013-11-13 13:30:30 -0800 | [diff] [blame] | 110 | Once ``finalize`` is called this object can no longer be used and |
Alex Gaynor | 9b70ba3 | 2013-11-13 13:49:43 -0800 | [diff] [blame] | 111 | :meth:`update` and :meth:`finalize` will raise |
Alex Gaynor | 34511c6 | 2013-11-13 13:30:30 -0800 | [diff] [blame] | 112 | :class:`~cryptography.exceptions.AlreadyFinalized`. |
| 113 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 114 | Algorithms |
| 115 | ~~~~~~~~~~ |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 116 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 117 | .. currentmodule:: cryptography.hazmat.primitives.ciphers.algorithms |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 118 | |
| 119 | .. class:: AES(key) |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 120 | |
Alex Gaynor | 1e3f81f | 2013-08-08 11:31:43 -0700 | [diff] [blame] | 121 | AES (Advanced Encryption Standard) is a block cipher standardized by NIST. |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 122 | AES is both fast, and cryptographically strong. It is a good default |
| 123 | choice for encryption. |
| 124 | |
| 125 | :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits. |
Alex Gaynor | 48ec9a3 | 2013-08-08 11:13:46 -0700 | [diff] [blame] | 126 | This must be kept secret. |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 127 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 128 | .. class:: Camellia(key) |
Paul Kehrer | dff22d4 | 2013-09-27 13:43:06 -0500 | [diff] [blame] | 129 | |
| 130 | Camellia is a block cipher approved for use by CRYPTREC and ISO/IEC. |
| 131 | It is considered to have comparable security and performance to AES, but |
| 132 | is not as widely studied or deployed. |
| 133 | |
| 134 | :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits. |
| 135 | This must be kept secret. |
| 136 | |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 137 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 138 | .. class:: TripleDES(key) |
Alex Gaynor | aeb714c | 2013-09-09 18:06:14 -0700 | [diff] [blame] | 139 | |
Alex Gaynor | 9316f4c | 2013-11-15 16:38:42 -0800 | [diff] [blame] | 140 | Triple DES (Data Encryption Standard), sometimes referred to as 3DES, is a |
| 141 | block cipher standardized by NIST. Triple DES has known crypto-analytic |
Alex Gaynor | 17adce6 | 2013-10-16 17:04:40 -0700 | [diff] [blame] | 142 | flaws, however none of them currently enable a practical attack. |
Alex Gaynor | 9316f4c | 2013-11-15 16:38:42 -0800 | [diff] [blame] | 143 | Nonetheless, Triples DES is not recommended for new applications because it |
Alex Gaynor | fbcc564 | 2013-10-22 08:26:00 -0700 | [diff] [blame] | 144 | is incredibly slow; old applications should consider moving away from it. |
Alex Gaynor | aeb714c | 2013-09-09 18:06:14 -0700 | [diff] [blame] | 145 | |
| 146 | :param bytes key: The secret key, either ``64``, ``128``, or ``192`` bits |
| 147 | (note that DES functionally uses ``56``, ``112``, or |
| 148 | ``168`` bits of the key, there is a parity byte in each |
| 149 | component of the key), in some materials these are |
| 150 | referred to as being up to three separate keys (each |
| 151 | ``56`` bits long), they can simply be concatenated to |
| 152 | produce the full key. This must be kept secret. |
| 153 | |
Paul Kehrer | 6022d45 | 2013-10-30 17:03:54 -0500 | [diff] [blame] | 154 | .. class:: CAST5(key) |
| 155 | |
| 156 | CAST5 (also known as CAST-128) is a block cipher approved for use in the |
| 157 | Canadian government by their Communications Security Establishment. It is a |
| 158 | variable key length cipher and supports keys from 40-128 bits in length. |
| 159 | |
| 160 | :param bytes key: The secret key, 40-128 bits in length (in increments of |
| 161 | 8). This must be kept secret. |
| 162 | |
Paul Kehrer | 3446d81 | 2013-10-31 17:15:03 -0500 | [diff] [blame] | 163 | Weak Ciphers |
| 164 | ------------ |
| 165 | |
| 166 | .. warning:: |
| 167 | |
| 168 | These ciphers are considered weak for a variety of reasons. New |
| 169 | applications should avoid their use and existing applications should |
| 170 | strongly consider migrating away. |
| 171 | |
Paul Kehrer | 5df0abe | 2013-10-30 16:57:04 -0500 | [diff] [blame] | 172 | .. class:: Blowfish(key) |
| 173 | |
| 174 | Blowfish is a block cipher developed by Bruce Schneier. It is known to be |
| 175 | susceptible to attacks when using weak keys. The author has recommended |
Alex Gaynor | ab5f011 | 2013-11-08 10:34:00 -0800 | [diff] [blame] | 176 | that users of Blowfish move to newer algorithms, such as :class:`AES`. |
Paul Kehrer | 5df0abe | 2013-10-30 16:57:04 -0500 | [diff] [blame] | 177 | |
| 178 | :param bytes key: The secret key, 32-448 bits in length (in increments of |
| 179 | 8). This must be kept secret. |
| 180 | |
Paul Kehrer | 4da28c3 | 2013-11-07 07:50:17 +0800 | [diff] [blame] | 181 | .. class:: ARC4(key) |
| 182 | |
| 183 | ARC4 (Alleged RC4) is a stream cipher with serious weaknesses in its |
| 184 | initial stream output. Its use is strongly discouraged. ARC4 does not use |
| 185 | mode constructions. |
| 186 | |
| 187 | :param bytes key: The secret key, ``40``, ``56``, ``64``, ``80``, ``128``, |
| 188 | ``192``, or ``256`` bits in length. This must be kept |
| 189 | secret. |
| 190 | |
Paul Kehrer | 0994c56 | 2013-11-10 03:19:14 +0800 | [diff] [blame] | 191 | .. doctest:: |
| 192 | |
| 193 | >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes |
| 194 | >>> algorithm = algorithms.ARC4(key) |
| 195 | >>> cipher = Cipher(algorithm, mode=None) |
| 196 | >>> encryptor = cipher.encryptor() |
| 197 | >>> ct = encryptor.update(b"a secret message") |
| 198 | >>> decryptor = cipher.decryptor() |
| 199 | >>> decryptor.update(ct) |
| 200 | 'a secret message' |
| 201 | |
David Reid | 30722b9 | 2013-11-07 13:03:39 -0800 | [diff] [blame] | 202 | |
| 203 | .. _symmetric-encryption-modes: |
| 204 | |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 205 | Modes |
| 206 | ~~~~~ |
| 207 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 208 | .. currentmodule:: cryptography.hazmat.primitives.ciphers.modes |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 209 | |
| 210 | .. class:: CBC(initialization_vector) |
Alex Gaynor | 48ec9a3 | 2013-08-08 11:13:46 -0700 | [diff] [blame] | 211 | |
| 212 | CBC (Cipher block chaining) is a mode of operation for block ciphers. It is |
| 213 | considered cryptographically strong. |
| 214 | |
| 215 | :param bytes initialization_vector: Must be random bytes. They do not need |
| 216 | to be kept secret (they can be included |
Alex Gaynor | 2dc2b86 | 2013-08-08 11:58:04 -0700 | [diff] [blame] | 217 | in a transmitted message). Must be the |
| 218 | same number of bytes as the |
Alex Gaynor | 8ed651e | 2013-11-07 13:24:31 -0800 | [diff] [blame] | 219 | ``block_size`` of the cipher. Each time |
Alex Gaynor | 9de452d | 2013-11-07 13:28:23 -0800 | [diff] [blame] | 220 | something is encrypted a new |
Alex Gaynor | 8ed651e | 2013-11-07 13:24:31 -0800 | [diff] [blame] | 221 | ``initialization_vector`` should be |
| 222 | generated. Do not reuse an |
| 223 | ``initialization_vector`` with |
| 224 | a given ``key``, and particularly do |
| 225 | not use a constant |
| 226 | ``initialization_vector``. |
| 227 | |
| 228 | A good construction looks like: |
| 229 | |
| 230 | .. code-block:: pycon |
| 231 | |
| 232 | >>> import os |
| 233 | >>> iv = os.urandom(16) |
| 234 | >>> mode = CBC(iv) |
| 235 | |
| 236 | While the following is bad and will leak information: |
| 237 | |
| 238 | .. code-block:: pycon |
| 239 | |
| 240 | >>> iv = "a" * 16 |
| 241 | >>> mode = CBC(iv) |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 242 | |
Paul Kehrer | 4506428 | 2013-10-17 13:41:53 -0500 | [diff] [blame] | 243 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 244 | .. class:: CTR(nonce) |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 245 | |
Paul Kehrer | 4506428 | 2013-10-17 13:41:53 -0500 | [diff] [blame] | 246 | .. warning:: |
| 247 | |
| 248 | Counter mode is not recommended for use with block ciphers that have a |
| 249 | block size of less than 128-bits. |
| 250 | |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 251 | CTR (Counter) is a mode of operation for block ciphers. It is considered |
Alex Gaynor | d1f0201 | 2013-11-01 14:12:35 -0700 | [diff] [blame] | 252 | cryptographically strong. It transforms a block cipher into a stream |
| 253 | cipher. |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 254 | |
Paul Kehrer | 89b3dd3 | 2013-10-17 14:02:45 -0500 | [diff] [blame] | 255 | :param bytes nonce: Should be random bytes. It is critical to never reuse a |
| 256 | ``nonce`` with a given key. Any reuse of a nonce |
| 257 | with the same key compromises the security of every |
| 258 | message encrypted with that key. Must be the same |
| 259 | number of bytes as the ``block_size`` of the cipher |
| 260 | with a given key. The nonce does not need to be kept |
| 261 | secret and may be included alongside the ciphertext. |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 262 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 263 | .. class:: OFB(initialization_vector) |
Paul Kehrer | 6f412a0 | 2013-09-10 21:30:50 -0500 | [diff] [blame] | 264 | |
| 265 | OFB (Output Feedback) is a mode of operation for block ciphers. It |
| 266 | transforms a block cipher into a stream cipher. |
| 267 | |
David Reid | f1a39bd | 2013-09-11 16:28:42 -0700 | [diff] [blame] | 268 | :param bytes initialization_vector: Must be random bytes. They do not need |
| 269 | to be kept secret (they can be included |
| 270 | in a transmitted message). Must be the |
| 271 | same number of bytes as the |
| 272 | ``block_size`` of the cipher. Do not |
| 273 | reuse an ``initialization_vector`` with |
| 274 | a given ``key``. |
Paul Kehrer | 6f412a0 | 2013-09-10 21:30:50 -0500 | [diff] [blame] | 275 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 276 | .. class:: CFB(initialization_vector) |
Paul Kehrer | 4223df7 | 2013-09-11 09:48:04 -0500 | [diff] [blame] | 277 | |
| 278 | CFB (Cipher Feedback) is a mode of operation for block ciphers. It |
| 279 | transforms a block cipher into a stream cipher. |
| 280 | |
| 281 | :param bytes initialization_vector: Must be random bytes. They do not need |
| 282 | to be kept secret (they can be included |
| 283 | in a transmitted message). Must be the |
| 284 | same number of bytes as the |
| 285 | ``block_size`` of the cipher. Do not |
| 286 | reuse an ``initialization_vector`` with |
| 287 | a given ``key``. |
| 288 | |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 289 | |
| 290 | Insecure Modes |
| 291 | -------------- |
| 292 | |
Alex Gaynor | cd413a3 | 2013-09-10 18:59:43 -0700 | [diff] [blame] | 293 | .. warning:: |
| 294 | |
| 295 | These modes are insecure. New applications should never make use of them, |
| 296 | and existing applications should strongly consider migrating away. |
| 297 | |
| 298 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 299 | .. class:: ECB() |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 300 | |
| 301 | ECB (Electronic Code Book) is the simplest mode of operation for block |
Alex Gaynor | cd413a3 | 2013-09-10 18:59:43 -0700 | [diff] [blame] | 302 | ciphers. Each block of data is encrypted in the same way. This means |
| 303 | identical plaintext blocks will always result in identical ciphertext |
| 304 | blocks, and thus result in information leakage |
Alex Gaynor | ab5f011 | 2013-11-08 10:34:00 -0800 | [diff] [blame] | 305 | |
| 306 | |
| 307 | .. _`described by Colin Percival`: http://www.daemonology.net/blog/2009-06-11-cryptographic-right-answers.html |