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