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 Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 16 | Symmetric encryption is a way to `encrypt`_ or hide the contents of material |
| 17 | where the sender and receiver both use the same secret key. Note that symmetric |
| 18 | encryption is **not** sufficient for most applications because it only |
| 19 | provides secrecy but not authenticity. That means an attacker can't see the |
| 20 | message but an attacker can create bogus messages and force the application to |
| 21 | decrypt them. |
| 22 | |
Alex Gaynor | 9316f4c | 2013-11-15 16:38:42 -0800 | [diff] [blame] | 23 | For this reason it is *strongly* recommended to combine encryption with a |
Alex Gaynor | ab5f011 | 2013-11-08 10:34:00 -0800 | [diff] [blame] | 24 | message authentication code, such as :doc:`HMAC </hazmat/primitives/hmac>`, in |
| 25 | an "encrypt-then-MAC" formulation as `described by Colin Percival`_. |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 26 | |
David Reid | ef0fcf2 | 2013-11-06 11:12:45 -0800 | [diff] [blame] | 27 | .. class:: Cipher(algorithm, mode, backend) |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 28 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 29 | Cipher objects combine an algorithm such as |
| 30 | :class:`~cryptography.hazmat.primitives.ciphers.algorithms.AES` with a |
| 31 | mode like |
Alex Gaynor | ab5f011 | 2013-11-08 10:34:00 -0800 | [diff] [blame] | 32 | :class:`~cryptography.hazmat.primitives.ciphers.modes.CBC` or |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 33 | :class:`~cryptography.hazmat.primitives.ciphers.modes.CTR`. A simple |
| 34 | example of encrypting and then decrypting content with AES is: |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 35 | |
Donald Stufft | 173de98 | 2013-08-12 07:34:39 -0400 | [diff] [blame] | 36 | .. doctest:: |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 37 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 38 | >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes |
Alex Gaynor | f8796b1 | 2013-12-13 20:28:55 -0800 | [diff] [blame] | 39 | >>> from cryptography.hazmat.backends import default_backend |
Alex Gaynor | f56444d | 2013-12-13 15:19:22 -0800 | [diff] [blame] | 40 | >>> backend = default_backend() |
David Reid | 63fa19a | 2013-11-20 10:49:13 -0800 | [diff] [blame] | 41 | >>> cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend) |
Paul Kehrer | 3e0895c | 2013-10-21 22:19:29 -0500 | [diff] [blame] | 42 | >>> encryptor = cipher.encryptor() |
| 43 | >>> ct = encryptor.update(b"a secret message") + encryptor.finalize() |
| 44 | >>> decryptor = cipher.decryptor() |
| 45 | >>> decryptor.update(ct) + decryptor.finalize() |
Paul Kehrer | f6cf956 | 2013-10-22 10:36:00 -0500 | [diff] [blame] | 46 | 'a secret message' |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 47 | |
David Reid | 663295d | 2013-11-20 13:55:08 -0800 | [diff] [blame] | 48 | :param algorithms: A |
| 49 | :class:`~cryptography.hazmat.primitives.interfaces.CipherAlgorithm` |
| 50 | provider such as those described |
| 51 | :ref:`below <symmetric-encryption-algorithms>`. |
| 52 | :param mode: A :class:`~cryptography.hazmat.primitives.interfaces.Mode` |
| 53 | provider such as those described |
| 54 | :ref:`below <symmetric-encryption-modes>`. |
| 55 | :param backend: A |
Alex Gaynor | f8796b1 | 2013-12-13 20:28:55 -0800 | [diff] [blame] | 56 | :class:`~cryptography.hazmat.backends.interfaces.CipherBackend` |
David Reid | 663295d | 2013-11-20 13:55:08 -0800 | [diff] [blame] | 57 | provider. |
Alex Gaynor | 0ca7fdb | 2013-08-08 07:35:26 -0700 | [diff] [blame] | 58 | |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 59 | .. method:: encryptor() |
Alex Gaynor | 09515f0 | 2013-08-08 15:26:55 -0700 | [diff] [blame] | 60 | |
David Reid | 63ba665 | 2013-10-22 14:09:19 -0700 | [diff] [blame] | 61 | :return: An encrypting |
Donald Stufft | f04317a | 2013-10-27 16:44:30 -0400 | [diff] [blame] | 62 | :class:`~cryptography.hazmat.primitives.interfaces.CipherContext` |
David Reid | 63ba665 | 2013-10-22 14:09:19 -0700 | [diff] [blame] | 63 | provider. |
Alex Gaynor | e62aa40 | 2013-08-08 15:23:11 -0700 | [diff] [blame] | 64 | |
Alex Gaynor | f1a3fc0 | 2013-11-02 14:03:34 -0700 | [diff] [blame] | 65 | If the backend doesn't support the requested combination of ``cipher`` |
Alex Gaynor | c6a6f31 | 2014-03-06 14:22:56 -0800 | [diff] [blame] | 66 | and ``mode`` an :class:`~cryptography.exceptions.UnsupportedCipher` |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 67 | exception will be raised. |
Alex Gaynor | f1a3fc0 | 2013-11-02 14:03:34 -0700 | [diff] [blame] | 68 | |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 69 | .. method:: decryptor() |
| 70 | |
David Reid | 63ba665 | 2013-10-22 14:09:19 -0700 | [diff] [blame] | 71 | :return: A decrypting |
Donald Stufft | f04317a | 2013-10-27 16:44:30 -0400 | [diff] [blame] | 72 | :class:`~cryptography.hazmat.primitives.interfaces.CipherContext` |
David Reid | 63ba665 | 2013-10-22 14:09:19 -0700 | [diff] [blame] | 73 | provider. |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 74 | |
Alex Gaynor | f1a3fc0 | 2013-11-02 14:03:34 -0700 | [diff] [blame] | 75 | If the backend doesn't support the requested combination of ``cipher`` |
Alex Gaynor | c6a6f31 | 2014-03-06 14:22:56 -0800 | [diff] [blame] | 76 | and ``mode`` an :class:`cryptography.exceptions.UnsupportedCipher` |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 77 | exception will be raised. |
Alex Gaynor | f1a3fc0 | 2013-11-02 14:03:34 -0700 | [diff] [blame] | 78 | |
David Reid | 663295d | 2013-11-20 13:55:08 -0800 | [diff] [blame] | 79 | .. _symmetric-encryption-algorithms: |
| 80 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 81 | Algorithms |
| 82 | ~~~~~~~~~~ |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 83 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 84 | .. currentmodule:: cryptography.hazmat.primitives.ciphers.algorithms |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 85 | |
| 86 | .. class:: AES(key) |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 87 | |
Alex Gaynor | 1e3f81f | 2013-08-08 11:31:43 -0700 | [diff] [blame] | 88 | AES (Advanced Encryption Standard) is a block cipher standardized by NIST. |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 89 | AES is both fast, and cryptographically strong. It is a good default |
| 90 | choice for encryption. |
| 91 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 92 | :param bytes key: The secret key. This must be kept secret. Either ``128``, |
| 93 | ``192``, or ``256`` bits long. |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 94 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 95 | .. class:: Camellia(key) |
Paul Kehrer | dff22d4 | 2013-09-27 13:43:06 -0500 | [diff] [blame] | 96 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 97 | Camellia is a block cipher approved for use by `CRYPTREC`_ and ISO/IEC. |
| 98 | It is considered to have comparable security and performance to AES but |
Paul Kehrer | dff22d4 | 2013-09-27 13:43:06 -0500 | [diff] [blame] | 99 | is not as widely studied or deployed. |
| 100 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 101 | :param bytes key: The secret key. This must be kept secret. Either ``128``, |
| 102 | ``192``, or ``256`` bits long. |
Paul Kehrer | dff22d4 | 2013-09-27 13:43:06 -0500 | [diff] [blame] | 103 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 104 | .. class:: TripleDES(key) |
Alex Gaynor | aeb714c | 2013-09-09 18:06:14 -0700 | [diff] [blame] | 105 | |
Alex Gaynor | 9316f4c | 2013-11-15 16:38:42 -0800 | [diff] [blame] | 106 | Triple DES (Data Encryption Standard), sometimes referred to as 3DES, is a |
| 107 | block cipher standardized by NIST. Triple DES has known crypto-analytic |
Alex Gaynor | 17adce6 | 2013-10-16 17:04:40 -0700 | [diff] [blame] | 108 | flaws, however none of them currently enable a practical attack. |
Alex Gaynor | 9316f4c | 2013-11-15 16:38:42 -0800 | [diff] [blame] | 109 | Nonetheless, Triples DES is not recommended for new applications because it |
Alex Gaynor | fbcc564 | 2013-10-22 08:26:00 -0700 | [diff] [blame] | 110 | is incredibly slow; old applications should consider moving away from it. |
Alex Gaynor | aeb714c | 2013-09-09 18:06:14 -0700 | [diff] [blame] | 111 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 112 | :param bytes key: The secret key. This must be kept secret. Either ``64``, |
| 113 | ``128``, or ``192`` bits long. DES only uses ``56``, ``112``, or ``168`` |
| 114 | bits of the key as there is a parity byte in each component of the key. |
| 115 | Some writing refers to there being up to three separate keys that are each |
| 116 | ``56`` bits long, they can simply be concatenated to produce the full key. |
Alex Gaynor | aeb714c | 2013-09-09 18:06:14 -0700 | [diff] [blame] | 117 | |
Paul Kehrer | bab0e1a | 2014-02-09 10:51:59 -0600 | [diff] [blame] | 118 | .. class:: CAST5(key) |
| 119 | |
Paul Kehrer | a5011ec | 2014-02-13 12:33:34 -0600 | [diff] [blame] | 120 | .. versionadded:: 0.2 |
| 121 | |
Paul Kehrer | bab0e1a | 2014-02-09 10:51:59 -0600 | [diff] [blame] | 122 | CAST5 (also known as CAST-128) is a block cipher approved for use in the |
| 123 | Canadian government by the `Communications Security Establishment`_. It is |
| 124 | a variable key length cipher and supports keys from 40-128 bits in length. |
| 125 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 126 | :param bytes key: The secret key, This must be kept secret. 40 to 128 bits |
| 127 | in length in increments of 8 bits. |
Paul Kehrer | bab0e1a | 2014-02-09 10:51:59 -0600 | [diff] [blame] | 128 | |
Paul Kehrer | 3446d81 | 2013-10-31 17:15:03 -0500 | [diff] [blame] | 129 | Weak Ciphers |
| 130 | ------------ |
| 131 | |
| 132 | .. warning:: |
| 133 | |
| 134 | These ciphers are considered weak for a variety of reasons. New |
| 135 | applications should avoid their use and existing applications should |
| 136 | strongly consider migrating away. |
| 137 | |
Paul Kehrer | 5df0abe | 2013-10-30 16:57:04 -0500 | [diff] [blame] | 138 | .. class:: Blowfish(key) |
| 139 | |
| 140 | Blowfish is a block cipher developed by Bruce Schneier. It is known to be |
| 141 | susceptible to attacks when using weak keys. The author has recommended |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 142 | that users of Blowfish move to newer algorithms such as :class:`AES`. |
Paul Kehrer | 5df0abe | 2013-10-30 16:57:04 -0500 | [diff] [blame] | 143 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 144 | :param bytes key: The secret key. This must be kept secret. 32 to 448 bits |
| 145 | in length in increments of 8 bits. |
Paul Kehrer | 5df0abe | 2013-10-30 16:57:04 -0500 | [diff] [blame] | 146 | |
Paul Kehrer | 4da28c3 | 2013-11-07 07:50:17 +0800 | [diff] [blame] | 147 | .. class:: ARC4(key) |
| 148 | |
| 149 | ARC4 (Alleged RC4) is a stream cipher with serious weaknesses in its |
| 150 | initial stream output. Its use is strongly discouraged. ARC4 does not use |
| 151 | mode constructions. |
| 152 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 153 | :param bytes key: The secret key. This must be kept secret. Either ``40``, |
| 154 | ``56``, ``64``, ``80``, ``128``, ``192``, or ``256`` bits in length. |
Paul Kehrer | 4da28c3 | 2013-11-07 07:50:17 +0800 | [diff] [blame] | 155 | |
Paul Kehrer | 0994c56 | 2013-11-10 03:19:14 +0800 | [diff] [blame] | 156 | .. doctest:: |
| 157 | |
| 158 | >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes |
Alex Gaynor | f8796b1 | 2013-12-13 20:28:55 -0800 | [diff] [blame] | 159 | >>> from cryptography.hazmat.backends import default_backend |
Paul Kehrer | 0994c56 | 2013-11-10 03:19:14 +0800 | [diff] [blame] | 160 | >>> algorithm = algorithms.ARC4(key) |
Alex Gaynor | f56444d | 2013-12-13 15:19:22 -0800 | [diff] [blame] | 161 | >>> cipher = Cipher(algorithm, mode=None, backend=default_backend()) |
Paul Kehrer | 0994c56 | 2013-11-10 03:19:14 +0800 | [diff] [blame] | 162 | >>> encryptor = cipher.encryptor() |
| 163 | >>> ct = encryptor.update(b"a secret message") |
| 164 | >>> decryptor = cipher.decryptor() |
| 165 | >>> decryptor.update(ct) |
| 166 | 'a secret message' |
| 167 | |
David Reid | 30722b9 | 2013-11-07 13:03:39 -0800 | [diff] [blame] | 168 | |
| 169 | .. _symmetric-encryption-modes: |
| 170 | |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 171 | Modes |
| 172 | ~~~~~ |
| 173 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 174 | .. currentmodule:: cryptography.hazmat.primitives.ciphers.modes |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 175 | |
| 176 | .. class:: CBC(initialization_vector) |
Alex Gaynor | 48ec9a3 | 2013-08-08 11:13:46 -0700 | [diff] [blame] | 177 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 178 | CBC (Cipher Block Chaining) is a mode of operation for block ciphers. It is |
Alex Gaynor | 48ec9a3 | 2013-08-08 11:13:46 -0700 | [diff] [blame] | 179 | considered cryptographically strong. |
| 180 | |
Paul Kehrer | fe2e3c2 | 2014-01-07 20:55:20 -0600 | [diff] [blame] | 181 | **Padding is required when using this mode.** |
| 182 | |
Alex Gaynor | 48ec9a3 | 2013-08-08 11:13:46 -0700 | [diff] [blame] | 183 | :param bytes initialization_vector: Must be random bytes. They do not need |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 184 | to be kept secret and they can be included in a transmitted message. |
| 185 | Must be the same number of bytes as the ``block_size`` of the cipher. |
| 186 | Each time something is encrypted a new ``initialization_vector`` should |
| 187 | be generated. Do not reuse an ``initialization_vector`` with a given |
Alex Gaynor | fb843aa | 2014-02-25 11:37:36 -0800 | [diff] [blame] | 188 | ``key``, and particularly do not use a constant |
| 189 | ``initialization_vector``. |
Alex Gaynor | 8ed651e | 2013-11-07 13:24:31 -0800 | [diff] [blame] | 190 | |
| 191 | A good construction looks like: |
| 192 | |
Alex Gaynor | 989061d | 2013-12-13 20:22:14 -0800 | [diff] [blame] | 193 | .. doctest:: |
Alex Gaynor | 8ed651e | 2013-11-07 13:24:31 -0800 | [diff] [blame] | 194 | |
| 195 | >>> import os |
Alex Gaynor | d83c590 | 2013-12-13 20:43:54 -0800 | [diff] [blame] | 196 | >>> from cryptography.hazmat.primitives.ciphers.modes import CBC |
Alex Gaynor | 8ed651e | 2013-11-07 13:24:31 -0800 | [diff] [blame] | 197 | >>> iv = os.urandom(16) |
| 198 | >>> mode = CBC(iv) |
| 199 | |
| 200 | While the following is bad and will leak information: |
| 201 | |
Alex Gaynor | 989061d | 2013-12-13 20:22:14 -0800 | [diff] [blame] | 202 | .. doctest:: |
Alex Gaynor | 8ed651e | 2013-11-07 13:24:31 -0800 | [diff] [blame] | 203 | |
Alex Gaynor | d83c590 | 2013-12-13 20:43:54 -0800 | [diff] [blame] | 204 | >>> from cryptography.hazmat.primitives.ciphers.modes import CBC |
Alex Gaynor | 8ed651e | 2013-11-07 13:24:31 -0800 | [diff] [blame] | 205 | >>> iv = "a" * 16 |
| 206 | >>> mode = CBC(iv) |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 207 | |
Paul Kehrer | 4506428 | 2013-10-17 13:41:53 -0500 | [diff] [blame] | 208 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 209 | .. class:: CTR(nonce) |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 210 | |
Paul Kehrer | 4506428 | 2013-10-17 13:41:53 -0500 | [diff] [blame] | 211 | .. warning:: |
| 212 | |
| 213 | Counter mode is not recommended for use with block ciphers that have a |
| 214 | block size of less than 128-bits. |
| 215 | |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 216 | 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] | 217 | cryptographically strong. It transforms a block cipher into a stream |
| 218 | cipher. |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 219 | |
Paul Kehrer | fe2e3c2 | 2014-01-07 20:55:20 -0600 | [diff] [blame] | 220 | **This mode does not require padding.** |
| 221 | |
Paul Kehrer | 89b3dd3 | 2013-10-17 14:02:45 -0500 | [diff] [blame] | 222 | :param bytes nonce: Should be random bytes. It is critical to never reuse a |
Alex Gaynor | fb843aa | 2014-02-25 11:37:36 -0800 | [diff] [blame] | 223 | ``nonce`` with a given key. Any reuse of a nonce with the same key |
| 224 | compromises the security of every message encrypted with that key. Must |
| 225 | be the same number of bytes as the ``block_size`` of the cipher with a |
| 226 | given key. The nonce does not need to be kept secret and may be |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 227 | included with the ciphertext. |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 228 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 229 | .. class:: OFB(initialization_vector) |
Paul Kehrer | 6f412a0 | 2013-09-10 21:30:50 -0500 | [diff] [blame] | 230 | |
| 231 | OFB (Output Feedback) is a mode of operation for block ciphers. It |
| 232 | transforms a block cipher into a stream cipher. |
| 233 | |
Paul Kehrer | fe2e3c2 | 2014-01-07 20:55:20 -0600 | [diff] [blame] | 234 | **This mode does not require padding.** |
| 235 | |
David Reid | f1a39bd | 2013-09-11 16:28:42 -0700 | [diff] [blame] | 236 | :param bytes initialization_vector: Must be random bytes. They do not need |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 237 | to be kept secret and they can be included in a transmitted message. |
| 238 | Must be the same number of bytes as the ``block_size`` of the cipher. |
| 239 | Do not reuse an ``initialization_vector`` with a given ``key``. |
Paul Kehrer | 6f412a0 | 2013-09-10 21:30:50 -0500 | [diff] [blame] | 240 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 241 | .. class:: CFB(initialization_vector) |
Paul Kehrer | 4223df7 | 2013-09-11 09:48:04 -0500 | [diff] [blame] | 242 | |
| 243 | CFB (Cipher Feedback) is a mode of operation for block ciphers. It |
| 244 | transforms a block cipher into a stream cipher. |
| 245 | |
Paul Kehrer | fe2e3c2 | 2014-01-07 20:55:20 -0600 | [diff] [blame] | 246 | **This mode does not require padding.** |
| 247 | |
Paul Kehrer | 4223df7 | 2013-09-11 09:48:04 -0500 | [diff] [blame] | 248 | :param bytes initialization_vector: Must be random bytes. They do not need |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 249 | to be kept secret and they can be included in a transmitted message. |
| 250 | Must be the same number of bytes as the ``block_size`` of the cipher. |
| 251 | Do not reuse an ``initialization_vector`` with a given ``key``. |
Paul Kehrer | 4223df7 | 2013-09-11 09:48:04 -0500 | [diff] [blame] | 252 | |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 253 | .. class:: GCM(initialization_vector, tag=None) |
| 254 | |
Paul Kehrer | 5b828b1 | 2013-11-29 17:32:08 -0600 | [diff] [blame] | 255 | .. danger:: |
Paul Kehrer | 26c8c6a | 2013-11-29 16:24:56 -0600 | [diff] [blame] | 256 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 257 | When using this mode you **must** not use the decrypted data until |
Alex Gaynor | 0d23e94 | 2013-12-04 17:28:24 -0600 | [diff] [blame] | 258 | :meth:`~cryptography.hazmat.primitives.interfaces.CipherContext.finalize` |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 259 | has been called. GCM provides **no** guarantees of ciphertext integrity |
Alex Gaynor | d4f9383 | 2013-12-04 16:31:59 -0600 | [diff] [blame] | 260 | until decryption is complete. |
Paul Kehrer | 26c8c6a | 2013-11-29 16:24:56 -0600 | [diff] [blame] | 261 | |
Paul Kehrer | 5578c66 | 2013-12-03 17:37:42 -0600 | [diff] [blame] | 262 | GCM (Galois Counter Mode) is a mode of operation for block ciphers. An |
| 263 | AEAD (authenticated encryption with additional data) mode is a type of |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 264 | block cipher mode that simultaneously encrypts the message as well as |
| 265 | authenticating it. Additional unencrypted data may also be authenticated. |
| 266 | Additional means of verifying integrity such as |
| 267 | :doc:`HMAC </hazmat/primitives/hmac>` are not necessary. |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 268 | |
Paul Kehrer | fe2e3c2 | 2014-01-07 20:55:20 -0600 | [diff] [blame] | 269 | **This mode does not require padding.** |
| 270 | |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 271 | :param bytes initialization_vector: Must be random bytes. They do not need |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 272 | to be kept secret and they can be included in a transmitted message. |
| 273 | NIST `recommends a 96-bit IV length`_ for performance critical |
| 274 | situations but it can be up to 2\ :sup:`64` - 1 bits. Do not reuse an |
Alex Gaynor | fb843aa | 2014-02-25 11:37:36 -0800 | [diff] [blame] | 275 | ``initialization_vector`` with a given ``key``. |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 276 | |
Paul Kehrer | ca73504 | 2013-12-21 17:31:48 -0600 | [diff] [blame] | 277 | .. note:: |
| 278 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 279 | Cryptography will generate a 128-bit tag when finalizing encryption. |
| 280 | You can shorten a tag by truncating it to the desired length but this |
Paul Kehrer | fc73e2d | 2013-12-21 18:41:38 -0600 | [diff] [blame] | 281 | is **not recommended** as it lowers the security margins of the |
Paul Kehrer | 048d6cb | 2013-12-21 18:53:19 -0600 | [diff] [blame] | 282 | authentication (`NIST SP-800-38D`_ recommends 96-bits or greater). |
Paul Kehrer | fc73e2d | 2013-12-21 18:41:38 -0600 | [diff] [blame] | 283 | 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] | 284 | (32-bits). Applications **must** verify the tag is the expected length |
| 285 | to guarantee the expected security margin. |
Paul Kehrer | ca73504 | 2013-12-21 17:31:48 -0600 | [diff] [blame] | 286 | |
Alex Gaynor | fb843aa | 2014-02-25 11:37:36 -0800 | [diff] [blame] | 287 | :param bytes tag: The tag bytes to verify during decryption. When |
| 288 | encrypting this must be ``None``. |
Paul Kehrer | 67abc86 | 2013-11-25 14:29:35 -0600 | [diff] [blame] | 289 | |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 290 | .. testcode:: |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 291 | |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 292 | import os |
| 293 | |
| 294 | from cryptography.hazmat.primitives.ciphers import ( |
| 295 | Cipher, algorithms, modes |
| 296 | ) |
| 297 | |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 298 | def encrypt(key, plaintext, associated_data): |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 299 | # Generate a random 96-bit IV. |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 300 | iv = os.urandom(12) |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 301 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 302 | # Construct a AES-GCM Cipher object with the given key and a |
| 303 | # randomly generated IV. |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 304 | encryptor = Cipher( |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 305 | algorithms.AES(key), |
| 306 | modes.GCM(iv), |
| 307 | backend=default_backend() |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 308 | ).encryptor() |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 309 | |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 310 | # associated_data will be authenticated but not encrypted, |
| 311 | # it must also be passed in on decryption. |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 312 | encryptor.authenticate_additional_data(associated_data) |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 313 | |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 314 | # Encrypt the plaintext and get the associated ciphertext. |
Paul Kehrer | af0b9f5 | 2014-01-07 19:21:49 -0600 | [diff] [blame] | 315 | # GCM does not require padding. |
| 316 | ciphertext = encryptor.update(plaintext) + encryptor.finalize() |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 317 | |
| 318 | return (iv, ciphertext, encryptor.tag) |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 319 | |
| 320 | def decrypt(key, associated_data, iv, ciphertext, tag): |
Alex Gaynor | 007e5e1 | 2014-01-12 14:25:49 -0800 | [diff] [blame] | 321 | if len(tag) != 16: |
| 322 | raise ValueError( |
| 323 | "tag must be 16 bytes -- truncation not supported" |
| 324 | ) |
| 325 | |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 326 | # Construct a Cipher object, with the key, iv, and additionally the |
| 327 | # GCM tag used for authenticating the message. |
| 328 | decryptor = Cipher( |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 329 | algorithms.AES(key), |
| 330 | modes.GCM(iv, tag), |
| 331 | backend=default_backend() |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 332 | ).decryptor() |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 333 | |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 334 | # We put associated_data back in or the tag will fail to verify |
| 335 | # when we finalize the decryptor. |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 336 | decryptor.authenticate_additional_data(associated_data) |
| 337 | |
Paul Kehrer | af0b9f5 | 2014-01-07 19:21:49 -0600 | [diff] [blame] | 338 | # Decryption gets us the authenticated plaintext. |
| 339 | # If the tag does not match an InvalidTag exception will be raised. |
| 340 | return decryptor.update(ciphertext) + decryptor.finalize() |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 341 | |
| 342 | iv, ciphertext, tag = encrypt( |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 343 | key, |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 344 | b"a secret message!", |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 345 | b"authenticated but not encrypted payload" |
| 346 | ) |
| 347 | |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 348 | print(decrypt( |
| 349 | key, |
| 350 | b"authenticated but not encrypted payload", |
| 351 | iv, |
| 352 | ciphertext, |
| 353 | tag |
| 354 | )) |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 355 | |
| 356 | .. testoutput:: |
| 357 | |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 358 | a secret message! |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 359 | |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 360 | |
| 361 | Insecure Modes |
| 362 | -------------- |
| 363 | |
Alex Gaynor | cd413a3 | 2013-09-10 18:59:43 -0700 | [diff] [blame] | 364 | .. warning:: |
| 365 | |
| 366 | These modes are insecure. New applications should never make use of them, |
| 367 | and existing applications should strongly consider migrating away. |
| 368 | |
| 369 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 370 | .. class:: ECB() |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 371 | |
| 372 | ECB (Electronic Code Book) is the simplest mode of operation for block |
Alex Gaynor | cd413a3 | 2013-09-10 18:59:43 -0700 | [diff] [blame] | 373 | ciphers. Each block of data is encrypted in the same way. This means |
| 374 | identical plaintext blocks will always result in identical ciphertext |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 375 | blocks, which can leave `significant patterns in the output`_. |
Alex Gaynor | ab5f011 | 2013-11-08 10:34:00 -0800 | [diff] [blame] | 376 | |
Paul Kehrer | fe2e3c2 | 2014-01-07 20:55:20 -0600 | [diff] [blame] | 377 | **Padding is required when using this mode.** |
| 378 | |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 379 | Interfaces |
| 380 | ---------- |
| 381 | |
| 382 | .. class:: CipherContext |
| 383 | |
| 384 | When calling ``encryptor()`` or ``decryptor()`` on a ``Cipher`` object |
Alex Gaynor | b481889 | 2014-02-06 10:58:50 -0800 | [diff] [blame] | 385 | the result will conform to the ``CipherContext`` interface. You can then |
| 386 | call ``update(data)`` with data until you have fed everything into the |
| 387 | context. Once that is done call ``finalize()`` to finish the operation and |
| 388 | obtain the remainder of the data. |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 389 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 390 | Block ciphers require that the plaintext or ciphertext always be a multiple |
| 391 | of their block size. Because of that **padding** is sometimes required to |
| 392 | make a message the correct size. ``CipherContext`` will not automatically |
| 393 | apply any padding; you'll need to add your own. For block ciphers the |
| 394 | recommended padding is |
| 395 | :class:`cryptography.hazmat.primitives.padding.PKCS7`. If you are using a |
| 396 | stream cipher mode (such as |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 397 | :class:`cryptography.hazmat.primitives.modes.CTR`) you don't have to worry |
| 398 | about this. |
| 399 | |
| 400 | .. method:: update(data) |
| 401 | |
| 402 | :param bytes data: The data you wish to pass into the context. |
| 403 | :return bytes: Returns the data that was encrypted or decrypted. |
| 404 | :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize` |
| 405 | |
| 406 | When the ``Cipher`` was constructed in a mode that turns it into a |
| 407 | stream cipher (e.g. |
| 408 | :class:`cryptography.hazmat.primitives.ciphers.modes.CTR`), this will |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 409 | return bytes immediately, however in other modes it will return chunks |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 410 | whose size is determined by the cipher's block size. |
| 411 | |
| 412 | .. method:: finalize() |
| 413 | |
| 414 | :return bytes: Returns the remainder of the data. |
| 415 | :raises ValueError: This is raised when the data provided isn't |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 416 | a multiple of the algorithm's block size. |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 417 | |
| 418 | Once ``finalize`` is called this object can no longer be used and |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 419 | :meth:`update` and :meth:`finalize` will raise an |
| 420 | :class:`~cryptography.exceptions.AlreadyFinalized` exception. |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 421 | |
| 422 | .. class:: AEADCipherContext |
| 423 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 424 | When calling ``encryptor`` or ``decryptor`` on a ``Cipher`` object |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 425 | with an AEAD mode (e.g. |
Alex Gaynor | b481889 | 2014-02-06 10:58:50 -0800 | [diff] [blame] | 426 | :class:`~cryptography.hazmat.primitives.ciphers.modes.GCM`) the result will |
| 427 | conform to the ``AEADCipherContext`` and ``CipherContext`` interfaces. If |
| 428 | it is an encryption context it will additionally be an |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 429 | ``AEADEncryptionContext`` provider. ``AEADCipherContext`` contains an |
| 430 | additional method :meth:`authenticate_additional_data` for adding |
| 431 | additional authenticated but unencrypted data (see note below). You should |
| 432 | call this before calls to ``update``. When you are done call `finalize`` |
| 433 | to finish the operation. |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 434 | |
| 435 | .. note:: |
| 436 | |
| 437 | In AEAD modes all data passed to ``update()`` will be both encrypted |
| 438 | and authenticated. Do not pass encrypted data to the |
| 439 | ``authenticate_additional_data()`` method. It is meant solely for |
| 440 | additional data you may want to authenticate but leave unencrypted. |
| 441 | |
| 442 | .. method:: authenticate_additional_data(data) |
| 443 | |
| 444 | :param bytes data: Any data you wish to authenticate but not encrypt. |
| 445 | :raises: :class:`~cryptography.exceptions.AlreadyFinalized` |
| 446 | |
| 447 | .. class:: AEADEncryptionContext |
| 448 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 449 | When creating an encryption context using ``encryptor`` on a ``Cipher`` |
| 450 | object with an AEAD mode such as |
| 451 | :class:`~cryptography.hazmat.primitives.ciphers.modes.GCM` an object |
| 452 | conforming to both the ``AEADEncryptionContext`` and ``AEADCipherContext`` |
| 453 | interfaces will be returned. This interface provides one |
| 454 | additional attribute ``tag``. ``tag`` can only be obtained after |
| 455 | ``finalize`` has been called. |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 456 | |
| 457 | .. attribute:: tag |
| 458 | |
| 459 | :return bytes: Returns the tag value as bytes. |
| 460 | :raises: :class:`~cryptography.exceptions.NotYetFinalized` if called |
Alex Gaynor | fb843aa | 2014-02-25 11:37:36 -0800 | [diff] [blame] | 461 | before the context is finalized. |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 462 | |
Alex Gaynor | ab5f011 | 2013-11-08 10:34:00 -0800 | [diff] [blame] | 463 | |
| 464 | .. _`described by Colin Percival`: http://www.daemonology.net/blog/2009-06-11-cryptographic-right-answers.html |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 465 | .. _`recommends a 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] | 466 | .. _`NIST SP-800-38D`: http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf |
Paul Kehrer | bab0e1a | 2014-02-09 10:51:59 -0600 | [diff] [blame] | 467 | .. _`Communications Security Establishment`: http://www.cse-cst.gc.ca |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame^] | 468 | .. _`encrypt`: https://ssd.eff.org/tech/encryption |
| 469 | .. _`CRYPTREC`: http://www.cryptrec.go.jp/english/ |
| 470 | .. _`significant patterns in the output`: http://en.wikipedia.org/wiki/Cipher_block_chaining#Electronic_codebook_.28ECB.29 |