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 | |
Alex Stapleton | c5fffd3 | 2014-03-18 15:29:00 +0000 | [diff] [blame] | 4 | Symmetric encryption |
Donald Stufft | e51fb93 | 2013-10-27 17:26:17 -0400 | [diff] [blame] | 5 | ==================== |
| 6 | |
Paul Kehrer | 7c5c9fe | 2015-02-14 10:27:14 -0600 | [diff] [blame] | 7 | .. module:: cryptography.hazmat.primitives.ciphers |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 8 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 9 | Symmetric encryption is a way to `encrypt`_ or hide the contents of material |
| 10 | where the sender and receiver both use the same secret key. Note that symmetric |
| 11 | encryption is **not** sufficient for most applications because it only |
| 12 | provides secrecy but not authenticity. That means an attacker can't see the |
| 13 | message but an attacker can create bogus messages and force the application to |
| 14 | decrypt them. |
| 15 | |
Paul Kehrer | 006670c | 2014-05-09 11:12:43 -0500 | [diff] [blame] | 16 | For this reason it is **strongly** recommended to combine encryption with a |
Alex Gaynor | 969f18e | 2014-05-17 20:07:35 -0700 | [diff] [blame] | 17 | message authentication code, such as :doc:`HMAC </hazmat/primitives/mac/hmac>`, |
| 18 | in an "encrypt-then-MAC" formulation as `described by Colin Percival`_. |
Paul Kehrer | afa84f1 | 2017-05-27 15:11:24 -0500 | [diff] [blame] | 19 | ``cryptography`` includes a recipe named :doc:`/fernet` that does this for you. |
| 20 | **To minimize the risk of security issues you should evaluate Fernet to see if |
| 21 | it fits your needs before implementing anything using this module.** |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 22 | |
David Reid | ef0fcf2 | 2013-11-06 11:12:45 -0800 | [diff] [blame] | 23 | .. class:: Cipher(algorithm, mode, backend) |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 24 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 25 | Cipher objects combine an algorithm such as |
| 26 | :class:`~cryptography.hazmat.primitives.ciphers.algorithms.AES` with a |
| 27 | mode like |
Alex Gaynor | ab5f011 | 2013-11-08 10:34:00 -0800 | [diff] [blame] | 28 | :class:`~cryptography.hazmat.primitives.ciphers.modes.CBC` or |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 29 | :class:`~cryptography.hazmat.primitives.ciphers.modes.CTR`. A simple |
| 30 | example of encrypting and then decrypting content with AES is: |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 31 | |
Donald Stufft | 173de98 | 2013-08-12 07:34:39 -0400 | [diff] [blame] | 32 | .. doctest:: |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 33 | |
Gregory Haynes | 0f98611 | 2014-12-30 09:43:24 -0800 | [diff] [blame] | 34 | >>> import os |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 35 | >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes |
Alex Gaynor | f8796b1 | 2013-12-13 20:28:55 -0800 | [diff] [blame] | 36 | >>> from cryptography.hazmat.backends import default_backend |
Alex Gaynor | f56444d | 2013-12-13 15:19:22 -0800 | [diff] [blame] | 37 | >>> backend = default_backend() |
Gregory Haynes | e261d94 | 2015-01-03 09:17:41 -0800 | [diff] [blame] | 38 | >>> key = os.urandom(32) |
Gregory Haynes | e591778 | 2015-01-03 21:58:25 -0800 | [diff] [blame] | 39 | >>> iv = os.urandom(16) |
Gregory Haynes | e261d94 | 2015-01-03 09:17:41 -0800 | [diff] [blame] | 40 | >>> cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend) |
Paul Kehrer | 3e0895c | 2013-10-21 22:19:29 -0500 | [diff] [blame] | 41 | >>> encryptor = cipher.encryptor() |
| 42 | >>> ct = encryptor.update(b"a secret message") + encryptor.finalize() |
| 43 | >>> decryptor = cipher.decryptor() |
| 44 | >>> decryptor.update(ct) + decryptor.finalize() |
Paul Kehrer | 056c9dd | 2018-05-12 15:17:06 -0400 | [diff] [blame] | 45 | b'a secret message' |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 46 | |
David Reid | 663295d | 2013-11-20 13:55:08 -0800 | [diff] [blame] | 47 | :param algorithms: A |
Paul Kehrer | 7c5c9fe | 2015-02-14 10:27:14 -0600 | [diff] [blame] | 48 | :class:`~cryptography.hazmat.primitives.ciphers.CipherAlgorithm` |
Gabriel Orisaka | 617fe4b | 2016-07-31 10:49:59 -0300 | [diff] [blame] | 49 | instance such as those described |
David Reid | 663295d | 2013-11-20 13:55:08 -0800 | [diff] [blame] | 50 | :ref:`below <symmetric-encryption-algorithms>`. |
Paul Kehrer | 513b7cb | 2015-02-12 17:31:24 -0600 | [diff] [blame] | 51 | :param mode: A :class:`~cryptography.hazmat.primitives.ciphers.modes.Mode` |
Gabriel Orisaka | 617fe4b | 2016-07-31 10:49:59 -0300 | [diff] [blame] | 52 | instance such as those described |
David Reid | 663295d | 2013-11-20 13:55:08 -0800 | [diff] [blame] | 53 | :ref:`below <symmetric-encryption-modes>`. |
| 54 | :param backend: A |
Alex Gaynor | f8796b1 | 2013-12-13 20:28:55 -0800 | [diff] [blame] | 55 | :class:`~cryptography.hazmat.backends.interfaces.CipherBackend` |
Gabriel Orisaka | 617fe4b | 2016-07-31 10:49:59 -0300 | [diff] [blame] | 56 | instance. |
Alex Gaynor | 0ca7fdb | 2013-08-08 07:35:26 -0700 | [diff] [blame] | 57 | |
Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 58 | :raises cryptography.exceptions.UnsupportedAlgorithm: This is raised if the |
Ayrx | f56c54e | 2014-03-16 14:36:17 +0800 | [diff] [blame] | 59 | provided ``backend`` does not implement |
| 60 | :class:`~cryptography.hazmat.backends.interfaces.CipherBackend` |
| 61 | |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 62 | .. method:: encryptor() |
Alex Gaynor | 09515f0 | 2013-08-08 15:26:55 -0700 | [diff] [blame] | 63 | |
David Reid | 63ba665 | 2013-10-22 14:09:19 -0700 | [diff] [blame] | 64 | :return: An encrypting |
Paul Kehrer | 7c5c9fe | 2015-02-14 10:27:14 -0600 | [diff] [blame] | 65 | :class:`~cryptography.hazmat.primitives.ciphers.CipherContext` |
Gabriel Orisaka | 617fe4b | 2016-07-31 10:49:59 -0300 | [diff] [blame] | 66 | instance. |
Alex Gaynor | e62aa40 | 2013-08-08 15:23:11 -0700 | [diff] [blame] | 67 | |
Alex Gaynor | f1a3fc0 | 2013-11-02 14:03:34 -0700 | [diff] [blame] | 68 | If the backend doesn't support the requested combination of ``cipher`` |
Alex Gaynor | 7a489db | 2014-03-22 15:09:34 -0700 | [diff] [blame] | 69 | and ``mode`` an :class:`~cryptography.exceptions.UnsupportedAlgorithm` |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 70 | exception will be raised. |
Alex Gaynor | f1a3fc0 | 2013-11-02 14:03:34 -0700 | [diff] [blame] | 71 | |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 72 | .. method:: decryptor() |
| 73 | |
David Reid | 63ba665 | 2013-10-22 14:09:19 -0700 | [diff] [blame] | 74 | :return: A decrypting |
Paul Kehrer | 7c5c9fe | 2015-02-14 10:27:14 -0600 | [diff] [blame] | 75 | :class:`~cryptography.hazmat.primitives.ciphers.CipherContext` |
Gabriel Orisaka | 617fe4b | 2016-07-31 10:49:59 -0300 | [diff] [blame] | 76 | instance. |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 77 | |
Alex Gaynor | f1a3fc0 | 2013-11-02 14:03:34 -0700 | [diff] [blame] | 78 | If the backend doesn't support the requested combination of ``cipher`` |
Alex Gaynor | fdf6330 | 2014-04-29 18:26:11 -0700 | [diff] [blame] | 79 | and ``mode`` an :class:`~cryptography.exceptions.UnsupportedAlgorithm` |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 80 | exception will be raised. |
Alex Gaynor | f1a3fc0 | 2013-11-02 14:03:34 -0700 | [diff] [blame] | 81 | |
David Reid | 663295d | 2013-11-20 13:55:08 -0800 | [diff] [blame] | 82 | .. _symmetric-encryption-algorithms: |
| 83 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 84 | Algorithms |
| 85 | ~~~~~~~~~~ |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 86 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 87 | .. currentmodule:: cryptography.hazmat.primitives.ciphers.algorithms |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 88 | |
| 89 | .. class:: AES(key) |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 90 | |
Alex Gaynor | 1e3f81f | 2013-08-08 11:31:43 -0700 | [diff] [blame] | 91 | AES (Advanced Encryption Standard) is a block cipher standardized by NIST. |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 92 | AES is both fast, and cryptographically strong. It is a good default |
| 93 | choice for encryption. |
| 94 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 95 | :param key: The secret key. This must be kept secret. Either ``128``, |
Paul Kehrer | 1aac78c | 2017-10-11 19:49:57 +0800 | [diff] [blame] | 96 | ``192``, or ``256`` :term:`bits` long. |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 97 | :type key: :term:`bytes-like` |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 98 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 99 | .. class:: Camellia(key) |
Paul Kehrer | dff22d4 | 2013-09-27 13:43:06 -0500 | [diff] [blame] | 100 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 101 | Camellia is a block cipher approved for use by `CRYPTREC`_ and ISO/IEC. |
| 102 | It is considered to have comparable security and performance to AES but |
Paul Kehrer | dff22d4 | 2013-09-27 13:43:06 -0500 | [diff] [blame] | 103 | is not as widely studied or deployed. |
| 104 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 105 | :param key: The secret key. This must be kept secret. Either ``128``, |
Paul Kehrer | 1aac78c | 2017-10-11 19:49:57 +0800 | [diff] [blame] | 106 | ``192``, or ``256`` :term:`bits` long. |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 107 | :type key: :term:`bytes-like` |
Paul Kehrer | dff22d4 | 2013-09-27 13:43:06 -0500 | [diff] [blame] | 108 | |
Paul Kehrer | 62ebb42 | 2017-09-28 23:46:49 +0800 | [diff] [blame] | 109 | .. class:: ChaCha20(key) |
| 110 | |
| 111 | .. versionadded:: 2.1 |
| 112 | |
| 113 | .. note:: |
| 114 | |
| 115 | In most cases users should use |
| 116 | :class:`~cryptography.hazmat.primitives.ciphers.aead.ChaCha20Poly1305` |
| 117 | instead of this class. `ChaCha20` alone does not provide integrity |
| 118 | so it must be combined with a MAC to be secure. |
| 119 | :class:`~cryptography.hazmat.primitives.ciphers.aead.ChaCha20Poly1305` |
| 120 | does this for you. |
| 121 | |
| 122 | ChaCha20 is a stream cipher used in several IETF protocols. It is |
| 123 | standardized in :rfc:`7539`. |
| 124 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 125 | :param key: The secret key. This must be kept secret. ``256`` |
Paul Kehrer | 1aac78c | 2017-10-11 19:49:57 +0800 | [diff] [blame] | 126 | :term:`bits` (32 bytes) in length. |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 127 | :type key: :term:`bytes-like` |
Paul Kehrer | 62ebb42 | 2017-09-28 23:46:49 +0800 | [diff] [blame] | 128 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 129 | :param nonce: Should be unique, a :term:`nonce`. It is |
Paul Kehrer | 62ebb42 | 2017-09-28 23:46:49 +0800 | [diff] [blame] | 130 | critical to never reuse a ``nonce`` with a given key. Any reuse of a |
| 131 | nonce with the same key compromises the security of every message |
| 132 | encrypted with that key. The nonce does not need to be kept secret |
Paul Kehrer | 1aac78c | 2017-10-11 19:49:57 +0800 | [diff] [blame] | 133 | and may be included with the ciphertext. This must be ``128`` |
| 134 | :term:`bits` in length. |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 135 | :type nonce: :term:`bytes-like` |
Paul Kehrer | 62ebb42 | 2017-09-28 23:46:49 +0800 | [diff] [blame] | 136 | |
| 137 | .. note:: |
| 138 | |
| 139 | In :rfc:`7539` the nonce is defined as a 96-bit value that is later |
| 140 | concatenated with a block counter (encoded as a 32-bit |
| 141 | little-endian). If you have a separate nonce and block counter |
| 142 | you will need to concatenate it yourself before passing it. For |
Alex Gaynor | c0c70fb | 2017-12-29 11:09:30 -0500 | [diff] [blame] | 143 | example, if you have an initial block counter of 2 and a 96-bit |
Paul Kehrer | 62ebb42 | 2017-09-28 23:46:49 +0800 | [diff] [blame] | 144 | nonce the concatenated nonce would be |
| 145 | ``struct.pack("<i", 2) + nonce``. |
| 146 | |
| 147 | .. doctest:: |
| 148 | |
| 149 | >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes |
| 150 | >>> from cryptography.hazmat.backends import default_backend |
| 151 | >>> nonce = os.urandom(16) |
| 152 | >>> algorithm = algorithms.ChaCha20(key, nonce) |
| 153 | >>> cipher = Cipher(algorithm, mode=None, backend=default_backend()) |
| 154 | >>> encryptor = cipher.encryptor() |
| 155 | >>> ct = encryptor.update(b"a secret message") |
| 156 | >>> decryptor = cipher.decryptor() |
| 157 | >>> decryptor.update(ct) |
Paul Kehrer | 056c9dd | 2018-05-12 15:17:06 -0400 | [diff] [blame] | 158 | b'a secret message' |
Paul Kehrer | 62ebb42 | 2017-09-28 23:46:49 +0800 | [diff] [blame] | 159 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 160 | .. class:: TripleDES(key) |
Alex Gaynor | aeb714c | 2013-09-09 18:06:14 -0700 | [diff] [blame] | 161 | |
Alex Gaynor | 9316f4c | 2013-11-15 16:38:42 -0800 | [diff] [blame] | 162 | Triple DES (Data Encryption Standard), sometimes referred to as 3DES, is a |
| 163 | block cipher standardized by NIST. Triple DES has known crypto-analytic |
Alex Gaynor | 17adce6 | 2013-10-16 17:04:40 -0700 | [diff] [blame] | 164 | flaws, however none of them currently enable a practical attack. |
Alex Chan | ee9710f | 2016-09-04 17:40:06 +0100 | [diff] [blame] | 165 | Nonetheless, Triple DES is not recommended for new applications because it |
Alex Gaynor | fbcc564 | 2013-10-22 08:26:00 -0700 | [diff] [blame] | 166 | is incredibly slow; old applications should consider moving away from it. |
Alex Gaynor | aeb714c | 2013-09-09 18:06:14 -0700 | [diff] [blame] | 167 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 168 | :param key: The secret key. This must be kept secret. Either ``64``, |
Paul Kehrer | 1aac78c | 2017-10-11 19:49:57 +0800 | [diff] [blame] | 169 | ``128``, or ``192`` :term:`bits` long. DES only uses ``56``, ``112``, |
| 170 | or ``168`` bits of the key as there is a parity byte in each component |
| 171 | of the key. Some writing refers to there being up to three separate |
| 172 | keys that are each ``56`` bits long, they can simply be concatenated |
| 173 | to produce the full key. |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 174 | :type key: :term:`bytes-like` |
Alex Gaynor | aeb714c | 2013-09-09 18:06:14 -0700 | [diff] [blame] | 175 | |
Paul Kehrer | bab0e1a | 2014-02-09 10:51:59 -0600 | [diff] [blame] | 176 | .. class:: CAST5(key) |
| 177 | |
Paul Kehrer | a5011ec | 2014-02-13 12:33:34 -0600 | [diff] [blame] | 178 | .. versionadded:: 0.2 |
| 179 | |
Paul Kehrer | bab0e1a | 2014-02-09 10:51:59 -0600 | [diff] [blame] | 180 | CAST5 (also known as CAST-128) is a block cipher approved for use in the |
| 181 | Canadian government by the `Communications Security Establishment`_. It is |
Paul Kehrer | 1aac78c | 2017-10-11 19:49:57 +0800 | [diff] [blame] | 182 | a variable key length cipher and supports keys from 40-128 :term:`bits` in |
| 183 | length. |
Paul Kehrer | bab0e1a | 2014-02-09 10:51:59 -0600 | [diff] [blame] | 184 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 185 | :param key: The secret key, This must be kept secret. 40 to 128 |
Paul Kehrer | 1aac78c | 2017-10-11 19:49:57 +0800 | [diff] [blame] | 186 | :term:`bits` in length in increments of 8 bits. |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 187 | :type key: :term:`bytes-like` |
Paul Kehrer | bab0e1a | 2014-02-09 10:51:59 -0600 | [diff] [blame] | 188 | |
Paul Kehrer | 7e914c9 | 2014-04-09 09:12:29 -0500 | [diff] [blame] | 189 | .. class:: SEED(key) |
| 190 | |
| 191 | .. versionadded:: 0.4 |
| 192 | |
Alex Stapleton | 19e97bd | 2014-05-02 21:57:59 +0100 | [diff] [blame] | 193 | SEED is a block cipher developed by the Korea Information Security Agency |
| 194 | (KISA). It is defined in :rfc:`4269` and is used broadly throughout South |
Paul Kehrer | 7e914c9 | 2014-04-09 09:12:29 -0500 | [diff] [blame] | 195 | Korean industry, but rarely found elsewhere. |
| 196 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 197 | :param key: The secret key. This must be kept secret. ``128`` |
Paul Kehrer | 1aac78c | 2017-10-11 19:49:57 +0800 | [diff] [blame] | 198 | :term:`bits` in length. |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 199 | :type key: :term:`bytes-like` |
Paul Kehrer | 7e914c9 | 2014-04-09 09:12:29 -0500 | [diff] [blame] | 200 | |
Alex Stapleton | c5fffd3 | 2014-03-18 15:29:00 +0000 | [diff] [blame] | 201 | Weak ciphers |
Paul Kehrer | 3446d81 | 2013-10-31 17:15:03 -0500 | [diff] [blame] | 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 Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [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 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 216 | :param key: The secret key. This must be kept secret. 32 to 448 |
Paul Kehrer | 1aac78c | 2017-10-11 19:49:57 +0800 | [diff] [blame] | 217 | :term:`bits` in length in increments of 8 bits. |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 218 | :type key: :term:`bytes-like` |
Paul Kehrer | 5df0abe | 2013-10-30 16:57:04 -0500 | [diff] [blame] | 219 | |
Paul Kehrer | 4da28c3 | 2013-11-07 07:50:17 +0800 | [diff] [blame] | 220 | .. class:: ARC4(key) |
| 221 | |
| 222 | ARC4 (Alleged RC4) is a stream cipher with serious weaknesses in its |
| 223 | initial stream output. Its use is strongly discouraged. ARC4 does not use |
| 224 | mode constructions. |
| 225 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 226 | :param key: The secret key. This must be kept secret. Either ``40``, |
Paul Kehrer | 1aac78c | 2017-10-11 19:49:57 +0800 | [diff] [blame] | 227 | ``56``, ``64``, ``80``, ``128``, ``192``, or ``256`` :term:`bits` in |
| 228 | length. |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 229 | :type key: :term:`bytes-like` |
Paul Kehrer | 4da28c3 | 2013-11-07 07:50:17 +0800 | [diff] [blame] | 230 | |
Paul Kehrer | 0994c56 | 2013-11-10 03:19:14 +0800 | [diff] [blame] | 231 | .. doctest:: |
| 232 | |
| 233 | >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes |
Alex Gaynor | f8796b1 | 2013-12-13 20:28:55 -0800 | [diff] [blame] | 234 | >>> from cryptography.hazmat.backends import default_backend |
Paul Kehrer | 0994c56 | 2013-11-10 03:19:14 +0800 | [diff] [blame] | 235 | >>> algorithm = algorithms.ARC4(key) |
Alex Gaynor | f56444d | 2013-12-13 15:19:22 -0800 | [diff] [blame] | 236 | >>> cipher = Cipher(algorithm, mode=None, backend=default_backend()) |
Paul Kehrer | 0994c56 | 2013-11-10 03:19:14 +0800 | [diff] [blame] | 237 | >>> encryptor = cipher.encryptor() |
| 238 | >>> ct = encryptor.update(b"a secret message") |
| 239 | >>> decryptor = cipher.decryptor() |
| 240 | >>> decryptor.update(ct) |
Paul Kehrer | 056c9dd | 2018-05-12 15:17:06 -0400 | [diff] [blame] | 241 | b'a secret message' |
Paul Kehrer | 0994c56 | 2013-11-10 03:19:14 +0800 | [diff] [blame] | 242 | |
Paul Kehrer | e5dc122 | 2014-02-20 16:19:32 -0600 | [diff] [blame] | 243 | .. class:: IDEA(key) |
| 244 | |
| 245 | IDEA (`International Data Encryption Algorithm`_) is a block cipher created |
| 246 | in 1991. It is an optional component of the `OpenPGP`_ standard. This cipher |
| 247 | is susceptible to attacks when using weak keys. It is recommended that you |
| 248 | do not use this cipher for new applications. |
| 249 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 250 | :param key: The secret key. This must be kept secret. ``128`` |
Paul Kehrer | 1aac78c | 2017-10-11 19:49:57 +0800 | [diff] [blame] | 251 | :term:`bits` in length. |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 252 | :type key: :term:`bytes-like` |
Paul Kehrer | e5dc122 | 2014-02-20 16:19:32 -0600 | [diff] [blame] | 253 | |
David Reid | 30722b9 | 2013-11-07 13:03:39 -0800 | [diff] [blame] | 254 | |
| 255 | .. _symmetric-encryption-modes: |
| 256 | |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 257 | Modes |
| 258 | ~~~~~ |
| 259 | |
Paul Kehrer | 2129d50 | 2015-02-13 12:37:34 -0600 | [diff] [blame] | 260 | .. module:: cryptography.hazmat.primitives.ciphers.modes |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 261 | |
| 262 | .. class:: CBC(initialization_vector) |
Alex Gaynor | 48ec9a3 | 2013-08-08 11:13:46 -0700 | [diff] [blame] | 263 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 264 | 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] | 265 | considered cryptographically strong. |
| 266 | |
Paul Kehrer | fe2e3c2 | 2014-01-07 20:55:20 -0600 | [diff] [blame] | 267 | **Padding is required when using this mode.** |
| 268 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 269 | :param initialization_vector: Must be :doc:`random bytes |
Alex Gaynor | 9b6fd8e | 2014-12-19 10:29:56 -0800 | [diff] [blame] | 270 | </random-numbers>`. They do not need to be kept secret and they can be |
| 271 | included in a transmitted message. Must be the same number of bytes as |
| 272 | the ``block_size`` of the cipher. Each time something is encrypted a |
| 273 | new ``initialization_vector`` should be generated. Do not reuse an |
| 274 | ``initialization_vector`` with a given ``key``, and particularly do not |
| 275 | use a constant ``initialization_vector``. |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 276 | :type initialization_vector: :term:`bytes-like` |
Alex Gaynor | 8ed651e | 2013-11-07 13:24:31 -0800 | [diff] [blame] | 277 | |
| 278 | A good construction looks like: |
| 279 | |
Alex Gaynor | 989061d | 2013-12-13 20:22:14 -0800 | [diff] [blame] | 280 | .. doctest:: |
Alex Gaynor | 8ed651e | 2013-11-07 13:24:31 -0800 | [diff] [blame] | 281 | |
| 282 | >>> import os |
Alex Gaynor | d83c590 | 2013-12-13 20:43:54 -0800 | [diff] [blame] | 283 | >>> from cryptography.hazmat.primitives.ciphers.modes import CBC |
Alex Gaynor | 8ed651e | 2013-11-07 13:24:31 -0800 | [diff] [blame] | 284 | >>> iv = os.urandom(16) |
| 285 | >>> mode = CBC(iv) |
| 286 | |
| 287 | While the following is bad and will leak information: |
| 288 | |
Alex Gaynor | 989061d | 2013-12-13 20:22:14 -0800 | [diff] [blame] | 289 | .. doctest:: |
Alex Gaynor | 8ed651e | 2013-11-07 13:24:31 -0800 | [diff] [blame] | 290 | |
Alex Gaynor | d83c590 | 2013-12-13 20:43:54 -0800 | [diff] [blame] | 291 | >>> from cryptography.hazmat.primitives.ciphers.modes import CBC |
Paul Kehrer | 056c9dd | 2018-05-12 15:17:06 -0400 | [diff] [blame] | 292 | >>> iv = b"a" * 16 |
Alex Gaynor | 8ed651e | 2013-11-07 13:24:31 -0800 | [diff] [blame] | 293 | >>> mode = CBC(iv) |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 294 | |
Paul Kehrer | 4506428 | 2013-10-17 13:41:53 -0500 | [diff] [blame] | 295 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 296 | .. class:: CTR(nonce) |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 297 | |
Paul Kehrer | 4506428 | 2013-10-17 13:41:53 -0500 | [diff] [blame] | 298 | .. warning:: |
| 299 | |
| 300 | Counter mode is not recommended for use with block ciphers that have a |
Paul Kehrer | 1aac78c | 2017-10-11 19:49:57 +0800 | [diff] [blame] | 301 | block size of less than 128-:term:`bits`. |
Paul Kehrer | 4506428 | 2013-10-17 13:41:53 -0500 | [diff] [blame] | 302 | |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 303 | 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] | 304 | cryptographically strong. It transforms a block cipher into a stream |
| 305 | cipher. |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 306 | |
Paul Kehrer | fe2e3c2 | 2014-01-07 20:55:20 -0600 | [diff] [blame] | 307 | **This mode does not require padding.** |
| 308 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 309 | :param nonce: Should be unique, a :term:`nonce`. It is |
Alex Gaynor | 9b6fd8e | 2014-12-19 10:29:56 -0800 | [diff] [blame] | 310 | critical to never reuse a ``nonce`` with a given key. Any reuse of a |
| 311 | nonce with the same key compromises the security of every message |
| 312 | encrypted with that key. Must be the same number of bytes as the |
| 313 | ``block_size`` of the cipher with a given key. The nonce does not need |
| 314 | to be kept secret and may be included with the ciphertext. |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 315 | :type nonce: :term:`bytes-like` |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 316 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 317 | .. class:: OFB(initialization_vector) |
Paul Kehrer | 6f412a0 | 2013-09-10 21:30:50 -0500 | [diff] [blame] | 318 | |
| 319 | OFB (Output Feedback) is a mode of operation for block ciphers. It |
| 320 | transforms a block cipher into a stream cipher. |
| 321 | |
Paul Kehrer | fe2e3c2 | 2014-01-07 20:55:20 -0600 | [diff] [blame] | 322 | **This mode does not require padding.** |
| 323 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 324 | :param initialization_vector: Must be :doc:`random bytes |
Alex Gaynor | 9b6fd8e | 2014-12-19 10:29:56 -0800 | [diff] [blame] | 325 | </random-numbers>`. They do not need to be kept secret and they can be |
| 326 | included in a transmitted message. Must be the same number of bytes as |
| 327 | the ``block_size`` of the cipher. Do not reuse an |
| 328 | ``initialization_vector`` with a given ``key``. |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 329 | :type initialization_vector: :term:`bytes-like` |
Paul Kehrer | 6f412a0 | 2013-09-10 21:30:50 -0500 | [diff] [blame] | 330 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 331 | .. class:: CFB(initialization_vector) |
Paul Kehrer | 4223df7 | 2013-09-11 09:48:04 -0500 | [diff] [blame] | 332 | |
| 333 | CFB (Cipher Feedback) is a mode of operation for block ciphers. It |
| 334 | transforms a block cipher into a stream cipher. |
| 335 | |
Paul Kehrer | fe2e3c2 | 2014-01-07 20:55:20 -0600 | [diff] [blame] | 336 | **This mode does not require padding.** |
| 337 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 338 | :param initialization_vector: Must be :doc:`random bytes |
Alex Gaynor | 9b6fd8e | 2014-12-19 10:29:56 -0800 | [diff] [blame] | 339 | </random-numbers>`. They do not need to be kept secret and they can be |
| 340 | included in a transmitted message. Must be the same number of bytes as |
| 341 | the ``block_size`` of the cipher. Do not reuse an |
| 342 | ``initialization_vector`` with a given ``key``. |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 343 | :type initialization_vector: :term:`bytes-like` |
Paul Kehrer | 4223df7 | 2013-09-11 09:48:04 -0500 | [diff] [blame] | 344 | |
Paul Kehrer | 2a947c4 | 2014-05-15 17:22:08 -0400 | [diff] [blame] | 345 | .. class:: CFB8(initialization_vector) |
| 346 | |
| 347 | CFB (Cipher Feedback) is a mode of operation for block ciphers. It |
| 348 | transforms a block cipher into a stream cipher. The CFB8 variant uses an |
| 349 | 8-bit shift register. |
| 350 | |
| 351 | **This mode does not require padding.** |
| 352 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 353 | :param initialization_vector: Must be :doc:`random bytes |
Alex Gaynor | 9b6fd8e | 2014-12-19 10:29:56 -0800 | [diff] [blame] | 354 | </random-numbers>`. They do not need to be kept secret and they can be |
| 355 | included in a transmitted message. Must be the same number of bytes as |
| 356 | the ``block_size`` of the cipher. Do not reuse an |
| 357 | ``initialization_vector`` with a given ``key``. |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 358 | :type initialization_vector: :term:`bytes-like` |
Paul Kehrer | 2a947c4 | 2014-05-15 17:22:08 -0400 | [diff] [blame] | 359 | |
Alex Gaynor | cc5224f | 2014-06-30 09:25:48 -0700 | [diff] [blame] | 360 | .. class:: GCM(initialization_vector, tag=None, min_tag_length=16) |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 361 | |
Paul Kehrer | 5b828b1 | 2013-11-29 17:32:08 -0600 | [diff] [blame] | 362 | .. danger:: |
Paul Kehrer | 26c8c6a | 2013-11-29 16:24:56 -0600 | [diff] [blame] | 363 | |
Paul Kehrer | a217358 | 2017-07-17 13:10:14 +0200 | [diff] [blame] | 364 | If you are encrypting data that can fit into memory you should strongly |
| 365 | consider using |
| 366 | :class:`~cryptography.hazmat.primitives.ciphers.aead.AESGCM` instead |
| 367 | of this. |
| 368 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 369 | When using this mode you **must** not use the decrypted data until |
Philipp Gesang | 2e84daa | 2017-05-02 15:28:33 +0200 | [diff] [blame] | 370 | the appropriate finalization method |
| 371 | (:meth:`~cryptography.hazmat.primitives.ciphers.CipherContext.finalize` |
| 372 | or |
| 373 | :meth:`~cryptography.hazmat.primitives.ciphers.AEADDecryptionContext.finalize_with_tag`) |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 374 | has been called. GCM provides **no** guarantees of ciphertext integrity |
Alex Gaynor | d4f9383 | 2013-12-04 16:31:59 -0600 | [diff] [blame] | 375 | until decryption is complete. |
Paul Kehrer | 26c8c6a | 2013-11-29 16:24:56 -0600 | [diff] [blame] | 376 | |
Paul Kehrer | 5578c66 | 2013-12-03 17:37:42 -0600 | [diff] [blame] | 377 | GCM (Galois Counter Mode) is a mode of operation for block ciphers. An |
| 378 | AEAD (authenticated encryption with additional data) mode is a type of |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 379 | block cipher mode that simultaneously encrypts the message as well as |
| 380 | authenticating it. Additional unencrypted data may also be authenticated. |
| 381 | Additional means of verifying integrity such as |
Ayrx | fa4a6b2 | 2014-04-16 23:03:14 +0800 | [diff] [blame] | 382 | :doc:`HMAC </hazmat/primitives/mac/hmac>` are not necessary. |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 383 | |
Paul Kehrer | fe2e3c2 | 2014-01-07 20:55:20 -0600 | [diff] [blame] | 384 | **This mode does not require padding.** |
| 385 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 386 | :param initialization_vector: Must be unique, a :term:`nonce`. |
Eeshan Garg | 9475900 | 2015-05-20 20:35:33 +0530 | [diff] [blame] | 387 | They do not need to be kept secret and they can be included in a |
| 388 | transmitted message. NIST `recommends a 96-bit IV length`_ for |
| 389 | performance critical situations but it can be up to 2\ :sup:`64` - 1 |
Paul Kehrer | 1aac78c | 2017-10-11 19:49:57 +0800 | [diff] [blame] | 390 | :term:`bits`. Do not reuse an ``initialization_vector`` with a given |
| 391 | ``key``. |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 392 | :type initialization_vector: :term:`bytes-like` |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 393 | |
Paul Kehrer | ca73504 | 2013-12-21 17:31:48 -0600 | [diff] [blame] | 394 | .. note:: |
| 395 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 396 | Cryptography will generate a 128-bit tag when finalizing encryption. |
| 397 | You can shorten a tag by truncating it to the desired length but this |
Alex Gaynor | d625609 | 2018-07-05 23:04:46 -0400 | [diff] [blame] | 398 | is **not recommended** as it makes it easier to forge messages, and |
| 399 | also potentially leaks the key (`NIST SP-800-38D`_ recommends |
| 400 | 96-:term:`bits` or greater). Applications wishing to allow truncation |
| 401 | can pass the ``min_tag_length`` parameter. |
Alex Gaynor | 8f1b8e8 | 2014-06-29 20:43:29 -0700 | [diff] [blame] | 402 | |
| 403 | .. versionchanged:: 0.5 |
| 404 | |
| 405 | The ``min_tag_length`` parameter was added in ``0.5``, previously |
Alex Gaynor | cc5224f | 2014-06-30 09:25:48 -0700 | [diff] [blame] | 406 | truncation down to ``4`` bytes was always allowed. |
Paul Kehrer | ca73504 | 2013-12-21 17:31:48 -0600 | [diff] [blame] | 407 | |
Alex Gaynor | fb843aa | 2014-02-25 11:37:36 -0800 | [diff] [blame] | 408 | :param bytes tag: The tag bytes to verify during decryption. When |
Philipp Gesang | 2e84daa | 2017-05-02 15:28:33 +0200 | [diff] [blame] | 409 | encrypting this must be ``None``. When decrypting, it may be ``None`` |
| 410 | if the tag is supplied on finalization using |
| 411 | :meth:`~cryptography.hazmat.primitives.ciphers.AEADDecryptionContext.finalize_with_tag`. |
| 412 | Otherwise, the tag is mandatory. |
Paul Kehrer | 67abc86 | 2013-11-25 14:29:35 -0600 | [diff] [blame] | 413 | |
Paul Kehrer | c563b57 | 2018-07-18 00:15:55 +0800 | [diff] [blame] | 414 | :param int min_tag_length: The minimum length ``tag`` must be. By default |
Alex Gaynor | cc5224f | 2014-06-30 09:25:48 -0700 | [diff] [blame] | 415 | this is ``16``, meaning tag truncation is not allowed. Allowing tag |
| 416 | truncation is strongly discouraged for most applications. |
| 417 | |
Paul Kehrer | 12a1cac | 2018-07-17 22:56:12 +0800 | [diff] [blame] | 418 | :raises ValueError: This is raised if ``len(tag) < min_tag_length`` or the |
| 419 | ``initialization_vector`` is too short. |
Alex Gaynor | 8f1b8e8 | 2014-06-29 20:43:29 -0700 | [diff] [blame] | 420 | |
Philipp Gesang | 2e84daa | 2017-05-02 15:28:33 +0200 | [diff] [blame] | 421 | :raises NotImplementedError: This is raised if the version of the OpenSSL |
| 422 | backend used is 1.0.1 or earlier. |
| 423 | |
Alex Gaynor | 09828cd | 2016-02-10 15:21:36 -0500 | [diff] [blame] | 424 | An example of securely encrypting and decrypting data with ``AES`` in the |
| 425 | ``GCM`` mode looks like: |
| 426 | |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 427 | .. testcode:: |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 428 | |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 429 | import os |
| 430 | |
gdmnbt | 2a0b834 | 2017-03-24 01:19:20 +0100 | [diff] [blame] | 431 | from cryptography.hazmat.backends import default_backend |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 432 | from cryptography.hazmat.primitives.ciphers import ( |
| 433 | Cipher, algorithms, modes |
| 434 | ) |
| 435 | |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 436 | def encrypt(key, plaintext, associated_data): |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 437 | # Generate a random 96-bit IV. |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 438 | iv = os.urandom(12) |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 439 | |
Alex Gaynor | ebb1cb9 | 2014-06-10 09:36:39 -0700 | [diff] [blame] | 440 | # Construct an AES-GCM Cipher object with the given key and a |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 441 | # randomly generated IV. |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 442 | encryptor = Cipher( |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 443 | algorithms.AES(key), |
| 444 | modes.GCM(iv), |
| 445 | backend=default_backend() |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 446 | ).encryptor() |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 447 | |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 448 | # associated_data will be authenticated but not encrypted, |
| 449 | # it must also be passed in on decryption. |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 450 | encryptor.authenticate_additional_data(associated_data) |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 451 | |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 452 | # Encrypt the plaintext and get the associated ciphertext. |
Paul Kehrer | af0b9f5 | 2014-01-07 19:21:49 -0600 | [diff] [blame] | 453 | # GCM does not require padding. |
| 454 | ciphertext = encryptor.update(plaintext) + encryptor.finalize() |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 455 | |
| 456 | return (iv, ciphertext, encryptor.tag) |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 457 | |
| 458 | def decrypt(key, associated_data, iv, ciphertext, tag): |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 459 | # Construct a Cipher object, with the key, iv, and additionally the |
| 460 | # GCM tag used for authenticating the message. |
| 461 | decryptor = Cipher( |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 462 | algorithms.AES(key), |
| 463 | modes.GCM(iv, tag), |
| 464 | backend=default_backend() |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 465 | ).decryptor() |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 466 | |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 467 | # We put associated_data back in or the tag will fail to verify |
| 468 | # when we finalize the decryptor. |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 469 | decryptor.authenticate_additional_data(associated_data) |
| 470 | |
Paul Kehrer | af0b9f5 | 2014-01-07 19:21:49 -0600 | [diff] [blame] | 471 | # Decryption gets us the authenticated plaintext. |
| 472 | # If the tag does not match an InvalidTag exception will be raised. |
| 473 | return decryptor.update(ciphertext) + decryptor.finalize() |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 474 | |
| 475 | iv, ciphertext, tag = encrypt( |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 476 | key, |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 477 | b"a secret message!", |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 478 | b"authenticated but not encrypted payload" |
| 479 | ) |
| 480 | |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 481 | print(decrypt( |
| 482 | key, |
| 483 | b"authenticated but not encrypted payload", |
| 484 | iv, |
| 485 | ciphertext, |
| 486 | tag |
| 487 | )) |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 488 | |
| 489 | .. testoutput:: |
| 490 | |
Paul Kehrer | 056c9dd | 2018-05-12 15:17:06 -0400 | [diff] [blame] | 491 | b'a secret message!' |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 492 | |
Paul Kehrer | a397d75 | 2017-10-02 10:03:20 +0800 | [diff] [blame] | 493 | .. class:: XTS(tweak) |
| 494 | |
| 495 | .. versionadded:: 2.1 |
| 496 | |
| 497 | .. warning:: |
| 498 | |
| 499 | XTS mode is meant for disk encryption and should not be used in other |
| 500 | contexts. ``cryptography`` only supports XTS mode with |
| 501 | :class:`~cryptography.hazmat.primitives.ciphers.algorithms.AES`. |
| 502 | |
| 503 | .. note:: |
| 504 | |
| 505 | AES XTS keys are double length. This means that to do AES-128 |
| 506 | encryption in XTS mode you need a 256-bit key. Similarly, AES-256 |
| 507 | requires passing a 512-bit key. AES 192 is not supported in XTS mode. |
| 508 | |
| 509 | XTS (XEX-based tweaked-codebook mode with ciphertext stealing) is a mode |
| 510 | of operation for the AES block cipher that is used for `disk encryption`_. |
| 511 | |
| 512 | **This mode does not require padding.** |
| 513 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 514 | :param tweak: The tweak is a 16 byte value typically derived from |
Paul Kehrer | a397d75 | 2017-10-02 10:03:20 +0800 | [diff] [blame] | 515 | something like the disk sector number. A given ``(tweak, key)`` pair |
| 516 | should not be reused, although doing so is less catastrophic than |
| 517 | in CTR mode. |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 518 | :type tweak: :term:`bytes-like` |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 519 | |
Alex Stapleton | c5fffd3 | 2014-03-18 15:29:00 +0000 | [diff] [blame] | 520 | Insecure modes |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 521 | -------------- |
| 522 | |
Alex Gaynor | cd413a3 | 2013-09-10 18:59:43 -0700 | [diff] [blame] | 523 | .. warning:: |
| 524 | |
| 525 | These modes are insecure. New applications should never make use of them, |
| 526 | and existing applications should strongly consider migrating away. |
| 527 | |
| 528 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 529 | .. class:: ECB() |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 530 | |
| 531 | ECB (Electronic Code Book) is the simplest mode of operation for block |
Alex Gaynor | cd413a3 | 2013-09-10 18:59:43 -0700 | [diff] [blame] | 532 | ciphers. Each block of data is encrypted in the same way. This means |
| 533 | identical plaintext blocks will always result in identical ciphertext |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 534 | blocks, which can leave `significant patterns in the output`_. |
Alex Gaynor | ab5f011 | 2013-11-08 10:34:00 -0800 | [diff] [blame] | 535 | |
Paul Kehrer | fe2e3c2 | 2014-01-07 20:55:20 -0600 | [diff] [blame] | 536 | **Padding is required when using this mode.** |
| 537 | |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 538 | Interfaces |
Paul Kehrer | e3ff364 | 2017-06-04 11:48:32 -1000 | [diff] [blame] | 539 | ~~~~~~~~~~ |
Paul Kehrer | 513b7cb | 2015-02-12 17:31:24 -0600 | [diff] [blame] | 540 | |
Paul Kehrer | 7c5c9fe | 2015-02-14 10:27:14 -0600 | [diff] [blame] | 541 | .. currentmodule:: cryptography.hazmat.primitives.ciphers |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 542 | |
| 543 | .. class:: CipherContext |
| 544 | |
| 545 | When calling ``encryptor()`` or ``decryptor()`` on a ``Cipher`` object |
Alex Gaynor | b481889 | 2014-02-06 10:58:50 -0800 | [diff] [blame] | 546 | the result will conform to the ``CipherContext`` interface. You can then |
| 547 | call ``update(data)`` with data until you have fed everything into the |
| 548 | context. Once that is done call ``finalize()`` to finish the operation and |
| 549 | obtain the remainder of the data. |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 550 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 551 | Block ciphers require that the plaintext or ciphertext always be a multiple |
| 552 | of their block size. Because of that **padding** is sometimes required to |
| 553 | make a message the correct size. ``CipherContext`` will not automatically |
| 554 | apply any padding; you'll need to add your own. For block ciphers the |
| 555 | recommended padding is |
Alex Gaynor | d99fc65 | 2014-06-25 10:24:03 -0700 | [diff] [blame] | 556 | :class:`~cryptography.hazmat.primitives.padding.PKCS7`. If you are using a |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 557 | stream cipher mode (such as |
Paul Kehrer | 45efdbc | 2015-02-12 10:58:22 -0600 | [diff] [blame] | 558 | :class:`~cryptography.hazmat.primitives.ciphers.modes.CTR`) you don't have |
| 559 | to worry about this. |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 560 | |
| 561 | .. method:: update(data) |
| 562 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 563 | :param data: The data you wish to pass into the context. |
| 564 | :type data: :term:`bytes-like` |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 565 | :return bytes: Returns the data that was encrypted or decrypted. |
| 566 | :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize` |
| 567 | |
| 568 | When the ``Cipher`` was constructed in a mode that turns it into a |
| 569 | stream cipher (e.g. |
Alex Gaynor | d99fc65 | 2014-06-25 10:24:03 -0700 | [diff] [blame] | 570 | :class:`~cryptography.hazmat.primitives.ciphers.modes.CTR`), this will |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 571 | return bytes immediately, however in other modes it will return chunks |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 572 | whose size is determined by the cipher's block size. |
| 573 | |
Paul Kehrer | 9b34ca9 | 2017-02-16 22:20:38 -0600 | [diff] [blame] | 574 | .. method:: update_into(data, buf) |
| 575 | |
| 576 | .. versionadded:: 1.8 |
| 577 | |
| 578 | .. warning:: |
| 579 | |
| 580 | This method allows you to avoid a memory copy by passing a writable |
| 581 | buffer and reading the resulting data. You are responsible for |
| 582 | correctly sizing the buffer and properly handling the data. This |
| 583 | method should only be used when extremely high performance is a |
| 584 | requirement and you will be making many small calls to |
| 585 | ``update_into``. |
| 586 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 587 | :param data: The data you wish to pass into the context. |
| 588 | :type data: :term:`bytes-like` |
Paul Kehrer | 9b34ca9 | 2017-02-16 22:20:38 -0600 | [diff] [blame] | 589 | :param buf: A writable Python buffer that the data will be written |
| 590 | into. This buffer should be ``len(data) + n - 1`` bytes where ``n`` |
| 591 | is the block size (in bytes) of the cipher and mode combination. |
| 592 | :return int: Number of bytes written. |
| 593 | :raises NotImplementedError: This is raised if the version of ``cffi`` |
| 594 | used is too old (this can happen on older PyPy releases). |
| 595 | :raises ValueError: This is raised if the supplied buffer is too small. |
| 596 | |
| 597 | .. doctest:: |
| 598 | |
| 599 | >>> import os |
| 600 | >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes |
| 601 | >>> from cryptography.hazmat.backends import default_backend |
| 602 | >>> backend = default_backend() |
| 603 | >>> key = os.urandom(32) |
| 604 | >>> iv = os.urandom(16) |
| 605 | >>> cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend) |
| 606 | >>> encryptor = cipher.encryptor() |
| 607 | >>> # the buffer needs to be at least len(data) + n - 1 where n is cipher/mode block size in bytes |
| 608 | >>> buf = bytearray(31) |
| 609 | >>> len_encrypted = encryptor.update_into(b"a secret message", buf) |
| 610 | >>> # get the ciphertext from the buffer reading only the bytes written to it (len_encrypted) |
| 611 | >>> ct = bytes(buf[:len_encrypted]) + encryptor.finalize() |
| 612 | >>> decryptor = cipher.decryptor() |
| 613 | >>> len_decrypted = decryptor.update_into(ct, buf) |
| 614 | >>> # get the plaintext from the buffer reading only the bytes written (len_decrypted) |
| 615 | >>> bytes(buf[:len_decrypted]) + decryptor.finalize() |
Paul Kehrer | 056c9dd | 2018-05-12 15:17:06 -0400 | [diff] [blame] | 616 | b'a secret message' |
Paul Kehrer | 9b34ca9 | 2017-02-16 22:20:38 -0600 | [diff] [blame] | 617 | |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 618 | .. method:: finalize() |
| 619 | |
| 620 | :return bytes: Returns the remainder of the data. |
| 621 | :raises ValueError: This is raised when the data provided isn't |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 622 | a multiple of the algorithm's block size. |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 623 | |
| 624 | Once ``finalize`` is called this object can no longer be used and |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 625 | :meth:`update` and :meth:`finalize` will raise an |
| 626 | :class:`~cryptography.exceptions.AlreadyFinalized` exception. |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 627 | |
| 628 | .. class:: AEADCipherContext |
| 629 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 630 | When calling ``encryptor`` or ``decryptor`` on a ``Cipher`` object |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 631 | with an AEAD mode (e.g. |
Alex Gaynor | b481889 | 2014-02-06 10:58:50 -0800 | [diff] [blame] | 632 | :class:`~cryptography.hazmat.primitives.ciphers.modes.GCM`) the result will |
| 633 | conform to the ``AEADCipherContext`` and ``CipherContext`` interfaces. If |
Philipp Gesang | 2e84daa | 2017-05-02 15:28:33 +0200 | [diff] [blame] | 634 | it is an encryption or decryption context it will additionally be an |
| 635 | ``AEADEncryptionContext`` or ``AEADDecryptionContext`` instance, |
| 636 | respectively. ``AEADCipherContext`` contains an additional method |
| 637 | :meth:`authenticate_additional_data` for adding additional authenticated |
| 638 | but unencrypted data (see note below). You should call this before calls to |
| 639 | ``update``. When you are done call ``finalize`` to finish the operation. |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 640 | |
| 641 | .. note:: |
| 642 | |
| 643 | In AEAD modes all data passed to ``update()`` will be both encrypted |
| 644 | and authenticated. Do not pass encrypted data to the |
| 645 | ``authenticate_additional_data()`` method. It is meant solely for |
| 646 | additional data you may want to authenticate but leave unencrypted. |
| 647 | |
| 648 | .. method:: authenticate_additional_data(data) |
| 649 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 650 | :param data: Any data you wish to authenticate but not encrypt. |
| 651 | :type data: :term:`bytes-like` |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 652 | :raises: :class:`~cryptography.exceptions.AlreadyFinalized` |
| 653 | |
| 654 | .. class:: AEADEncryptionContext |
| 655 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 656 | When creating an encryption context using ``encryptor`` on a ``Cipher`` |
| 657 | object with an AEAD mode such as |
| 658 | :class:`~cryptography.hazmat.primitives.ciphers.modes.GCM` an object |
| 659 | conforming to both the ``AEADEncryptionContext`` and ``AEADCipherContext`` |
| 660 | interfaces will be returned. This interface provides one |
| 661 | additional attribute ``tag``. ``tag`` can only be obtained after |
| 662 | ``finalize`` has been called. |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 663 | |
| 664 | .. attribute:: tag |
| 665 | |
| 666 | :return bytes: Returns the tag value as bytes. |
| 667 | :raises: :class:`~cryptography.exceptions.NotYetFinalized` if called |
Alex Gaynor | fb843aa | 2014-02-25 11:37:36 -0800 | [diff] [blame] | 668 | before the context is finalized. |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 669 | |
Philipp Gesang | 2e84daa | 2017-05-02 15:28:33 +0200 | [diff] [blame] | 670 | .. class:: AEADDecryptionContext |
| 671 | |
| 672 | .. versionadded:: 1.9 |
| 673 | |
| 674 | When creating an encryption context using ``decryptor`` on a ``Cipher`` |
| 675 | object with an AEAD mode such as |
| 676 | :class:`~cryptography.hazmat.primitives.ciphers.modes.GCM` an object |
| 677 | conforming to both the ``AEADDecryptionContext`` and ``AEADCipherContext`` |
| 678 | interfaces will be returned. This interface provides one additional method |
| 679 | :meth:`finalize_with_tag` that allows passing the authentication tag for |
| 680 | validation after the ciphertext has been decrypted. |
| 681 | |
| 682 | .. method:: finalize_with_tag(tag) |
| 683 | |
Paul Kehrer | 5fb1021 | 2017-05-02 12:04:53 -0500 | [diff] [blame] | 684 | .. note:: |
| 685 | |
| 686 | This method is not supported when compiled against OpenSSL 1.0.1. |
| 687 | |
Philipp Gesang | 2e84daa | 2017-05-02 15:28:33 +0200 | [diff] [blame] | 688 | :param bytes tag: The tag bytes to verify after decryption. |
| 689 | :return bytes: Returns the remainder of the data. |
| 690 | :raises ValueError: This is raised when the data provided isn't |
| 691 | a multiple of the algorithm's block size, if ``min_tag_length`` is |
| 692 | less than 4, or if ``len(tag) < min_tag_length``. |
Paul Kehrer | d4378e4 | 2018-07-17 21:49:03 +0800 | [diff] [blame] | 693 | ``min_tag_length`` is an argument to the ``GCM`` constructor. |
Philipp Gesang | 2e84daa | 2017-05-02 15:28:33 +0200 | [diff] [blame] | 694 | :raises NotImplementedError: This is raised if the version of the |
| 695 | OpenSSL backend used is 1.0.1 or earlier. |
| 696 | |
| 697 | If the authentication tag was not already supplied to the constructor |
| 698 | of the :class:`~cryptography.hazmat.primitives.ciphers.modes.GCM` mode |
| 699 | object, this method must be used instead of |
| 700 | :meth:`~cryptography.hazmat.primitives.ciphers.CipherContext.finalize`. |
| 701 | |
Paul Kehrer | 513b7cb | 2015-02-12 17:31:24 -0600 | [diff] [blame] | 702 | .. class:: CipherAlgorithm |
| 703 | |
| 704 | A named symmetric encryption algorithm. |
| 705 | |
| 706 | .. attribute:: name |
| 707 | |
| 708 | :type: str |
| 709 | |
| 710 | The standard name for the mode, for example, "AES", "Camellia", or |
| 711 | "Blowfish". |
| 712 | |
| 713 | .. attribute:: key_size |
| 714 | |
| 715 | :type: int |
| 716 | |
Paul Kehrer | 1aac78c | 2017-10-11 19:49:57 +0800 | [diff] [blame] | 717 | The number of :term:`bits` in the key being used. |
Paul Kehrer | 513b7cb | 2015-02-12 17:31:24 -0600 | [diff] [blame] | 718 | |
| 719 | |
| 720 | .. class:: BlockCipherAlgorithm |
| 721 | |
| 722 | A block cipher algorithm. |
| 723 | |
| 724 | .. attribute:: block_size |
| 725 | |
| 726 | :type: int |
| 727 | |
Paul Kehrer | 1aac78c | 2017-10-11 19:49:57 +0800 | [diff] [blame] | 728 | The number of :term:`bits` in a block. |
Paul Kehrer | 513b7cb | 2015-02-12 17:31:24 -0600 | [diff] [blame] | 729 | |
| 730 | Interfaces used by the symmetric cipher modes described in |
| 731 | :ref:`Symmetric Encryption Modes <symmetric-encryption-modes>`. |
| 732 | |
| 733 | .. currentmodule:: cryptography.hazmat.primitives.ciphers.modes |
| 734 | |
| 735 | .. class:: Mode |
| 736 | |
| 737 | A named cipher mode. |
| 738 | |
| 739 | .. attribute:: name |
| 740 | |
| 741 | :type: str |
| 742 | |
| 743 | This should be the standard shorthand name for the mode, for example |
| 744 | Cipher-Block Chaining mode is "CBC". |
| 745 | |
| 746 | The name may be used by a backend to influence the operation of a |
| 747 | cipher in conjunction with the algorithm's name. |
| 748 | |
| 749 | .. method:: validate_for_algorithm(algorithm) |
| 750 | |
Alex Gaynor | 2c52605 | 2015-02-24 10:55:28 -0800 | [diff] [blame] | 751 | :param cryptography.hazmat.primitives.ciphers.CipherAlgorithm algorithm: |
Paul Kehrer | 513b7cb | 2015-02-12 17:31:24 -0600 | [diff] [blame] | 752 | |
| 753 | Checks that the combination of this mode with the provided algorithm |
| 754 | meets any necessary invariants. This should raise an exception if they |
| 755 | are not met. |
| 756 | |
| 757 | For example, the |
| 758 | :class:`~cryptography.hazmat.primitives.ciphers.modes.CBC` mode uses |
| 759 | this method to check that the provided initialization vector's length |
| 760 | matches the block size of the algorithm. |
| 761 | |
| 762 | |
| 763 | .. class:: ModeWithInitializationVector |
| 764 | |
| 765 | A cipher mode with an initialization vector. |
| 766 | |
| 767 | .. attribute:: initialization_vector |
| 768 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 769 | :type: :term:`bytes-like` |
Paul Kehrer | 513b7cb | 2015-02-12 17:31:24 -0600 | [diff] [blame] | 770 | |
| 771 | Exact requirements of the initialization are described by the |
| 772 | documentation of individual modes. |
| 773 | |
| 774 | |
| 775 | .. class:: ModeWithNonce |
| 776 | |
| 777 | A cipher mode with a nonce. |
| 778 | |
| 779 | .. attribute:: nonce |
| 780 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 781 | :type: :term:`bytes-like` |
Paul Kehrer | 513b7cb | 2015-02-12 17:31:24 -0600 | [diff] [blame] | 782 | |
| 783 | Exact requirements of the nonce are described by the documentation of |
| 784 | individual modes. |
| 785 | |
| 786 | |
Paul Kehrer | 4ab6059 | 2015-02-13 09:06:48 -0600 | [diff] [blame] | 787 | .. class:: ModeWithAuthenticationTag |
| 788 | |
| 789 | A cipher mode with an authentication tag. |
| 790 | |
| 791 | .. attribute:: tag |
| 792 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 793 | :type: :term:`bytes-like` |
Paul Kehrer | 4ab6059 | 2015-02-13 09:06:48 -0600 | [diff] [blame] | 794 | |
| 795 | Exact requirements of the tag are described by the documentation of |
| 796 | individual modes. |
| 797 | |
Paul Kehrer | a397d75 | 2017-10-02 10:03:20 +0800 | [diff] [blame] | 798 | |
| 799 | .. class:: ModeWithTweak |
| 800 | |
| 801 | .. versionadded:: 2.1 |
| 802 | |
| 803 | A cipher mode with a tweak. |
| 804 | |
| 805 | .. attribute:: tweak |
| 806 | |
Paul Kehrer | 3c68250 | 2018-12-10 12:13:31 +0800 | [diff] [blame^] | 807 | :type: :term:`bytes-like` |
Paul Kehrer | a397d75 | 2017-10-02 10:03:20 +0800 | [diff] [blame] | 808 | |
| 809 | Exact requirements of the tweak are described by the documentation of |
| 810 | individual modes. |
| 811 | |
Paul Kehrer | a8b1c6e | 2017-06-04 11:48:24 -1000 | [diff] [blame] | 812 | Exceptions |
| 813 | ~~~~~~~~~~ |
| 814 | |
| 815 | .. currentmodule:: cryptography.exceptions |
| 816 | |
| 817 | |
| 818 | .. class:: InvalidTag |
| 819 | |
| 820 | This is raised if an authenticated encryption tag fails to verify during |
| 821 | decryption. |
| 822 | |
Paul Kehrer | 4ab6059 | 2015-02-13 09:06:48 -0600 | [diff] [blame] | 823 | |
Alex Gaynor | ab5f011 | 2013-11-08 10:34:00 -0800 | [diff] [blame] | 824 | |
Alex Gaynor | 5e5990a | 2018-10-23 21:34:44 -0400 | [diff] [blame] | 825 | .. _`described by Colin Percival`: https://www.daemonology.net/blog/2009-06-11-cryptographic-right-answers.html |
Alex Gaynor | 20721c9 | 2017-09-20 04:39:45 -0400 | [diff] [blame] | 826 | .. _`recommends a 96-bit IV length`: https://csrc.nist.gov/publications/detail/sp/800-38d/final |
Alex Gaynor | 53e4505 | 2017-09-20 09:57:47 -0400 | [diff] [blame] | 827 | .. _`NIST SP-800-38D`: https://csrc.nist.gov/publications/detail/sp/800-38d/final |
Alex Gaynor | 111aff2 | 2015-02-17 14:47:49 -0800 | [diff] [blame] | 828 | .. _`Communications Security Establishment`: https://www.cse-cst.gc.ca |
Alex Gaynor | 6201230 | 2015-02-17 10:49:22 -0800 | [diff] [blame] | 829 | .. _`encrypt`: https://ssd.eff.org/en/module/what-encryption |
Alex Gaynor | 6422d83 | 2016-03-06 21:40:57 -0500 | [diff] [blame] | 830 | .. _`CRYPTREC`: https://www.cryptrec.go.jp/english/ |
Alex Gaynor | 543031a | 2015-02-16 20:27:35 -0800 | [diff] [blame] | 831 | .. _`significant patterns in the output`: https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_Codebook_.28ECB.29 |
Paul Kehrer | e5dc122 | 2014-02-20 16:19:32 -0600 | [diff] [blame] | 832 | .. _`International Data Encryption Algorithm`: https://en.wikipedia.org/wiki/International_Data_Encryption_Algorithm |
Alex Gaynor | 9569f5c | 2018-10-23 23:47:29 -0400 | [diff] [blame] | 833 | .. _`OpenPGP`: https://www.openpgp.org/ |
Paul Kehrer | a397d75 | 2017-10-02 10:03:20 +0800 | [diff] [blame] | 834 | .. _`disk encryption`: https://en.wikipedia.org/wiki/Disk_encryption_theory#XTS |