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