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