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 | f6cf956 | 2013-10-22 10:36:00 -0500 | [diff] [blame] | 45 | 'a secret message' |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 46 | |
David Reid | 663295d | 2013-11-20 13:55:08 -0800 | [diff] [blame] | 47 | :param algorithms: A |
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 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 95 | :param bytes key: The secret key. This must be kept secret. Either ``128``, |
| 96 | ``192``, or ``256`` bits long. |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 97 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 98 | .. class:: Camellia(key) |
Paul Kehrer | dff22d4 | 2013-09-27 13:43:06 -0500 | [diff] [blame] | 99 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 100 | Camellia is a block cipher approved for use by `CRYPTREC`_ and ISO/IEC. |
| 101 | It is considered to have comparable security and performance to AES but |
Paul Kehrer | dff22d4 | 2013-09-27 13:43:06 -0500 | [diff] [blame] | 102 | is not as widely studied or deployed. |
| 103 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 104 | :param bytes key: The secret key. This must be kept secret. Either ``128``, |
| 105 | ``192``, or ``256`` bits long. |
Paul Kehrer | dff22d4 | 2013-09-27 13:43:06 -0500 | [diff] [blame] | 106 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 107 | .. class:: TripleDES(key) |
Alex Gaynor | aeb714c | 2013-09-09 18:06:14 -0700 | [diff] [blame] | 108 | |
Alex Gaynor | 9316f4c | 2013-11-15 16:38:42 -0800 | [diff] [blame] | 109 | Triple DES (Data Encryption Standard), sometimes referred to as 3DES, is a |
| 110 | block cipher standardized by NIST. Triple DES has known crypto-analytic |
Alex Gaynor | 17adce6 | 2013-10-16 17:04:40 -0700 | [diff] [blame] | 111 | flaws, however none of them currently enable a practical attack. |
Alex Chan | ee9710f | 2016-09-04 17:40:06 +0100 | [diff] [blame] | 112 | Nonetheless, Triple DES is not recommended for new applications because it |
Alex Gaynor | fbcc564 | 2013-10-22 08:26:00 -0700 | [diff] [blame] | 113 | is incredibly slow; old applications should consider moving away from it. |
Alex Gaynor | aeb714c | 2013-09-09 18:06:14 -0700 | [diff] [blame] | 114 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 115 | :param bytes key: The secret key. This must be kept secret. Either ``64``, |
| 116 | ``128``, or ``192`` bits long. DES only uses ``56``, ``112``, or ``168`` |
| 117 | bits of the key as there is a parity byte in each component of the key. |
| 118 | Some writing refers to there being up to three separate keys that are each |
| 119 | ``56`` bits long, they can simply be concatenated to produce the full key. |
Alex Gaynor | aeb714c | 2013-09-09 18:06:14 -0700 | [diff] [blame] | 120 | |
Paul Kehrer | bab0e1a | 2014-02-09 10:51:59 -0600 | [diff] [blame] | 121 | .. class:: CAST5(key) |
| 122 | |
Paul Kehrer | a5011ec | 2014-02-13 12:33:34 -0600 | [diff] [blame] | 123 | .. versionadded:: 0.2 |
| 124 | |
Paul Kehrer | bab0e1a | 2014-02-09 10:51:59 -0600 | [diff] [blame] | 125 | CAST5 (also known as CAST-128) is a block cipher approved for use in the |
| 126 | Canadian government by the `Communications Security Establishment`_. It is |
| 127 | a variable key length cipher and supports keys from 40-128 bits in length. |
| 128 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 129 | :param bytes key: The secret key, This must be kept secret. 40 to 128 bits |
| 130 | in length in increments of 8 bits. |
Paul Kehrer | bab0e1a | 2014-02-09 10:51:59 -0600 | [diff] [blame] | 131 | |
Paul Kehrer | 7e914c9 | 2014-04-09 09:12:29 -0500 | [diff] [blame] | 132 | .. class:: SEED(key) |
| 133 | |
| 134 | .. versionadded:: 0.4 |
| 135 | |
Alex Stapleton | 19e97bd | 2014-05-02 21:57:59 +0100 | [diff] [blame] | 136 | SEED is a block cipher developed by the Korea Information Security Agency |
| 137 | (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] | 138 | Korean industry, but rarely found elsewhere. |
| 139 | |
| 140 | :param bytes key: The secret key. This must be kept secret. ``128`` bits in |
| 141 | length. |
| 142 | |
Alex Stapleton | c5fffd3 | 2014-03-18 15:29:00 +0000 | [diff] [blame] | 143 | Weak ciphers |
Paul Kehrer | 3446d81 | 2013-10-31 17:15:03 -0500 | [diff] [blame] | 144 | ------------ |
| 145 | |
| 146 | .. warning:: |
| 147 | |
| 148 | These ciphers are considered weak for a variety of reasons. New |
| 149 | applications should avoid their use and existing applications should |
| 150 | strongly consider migrating away. |
| 151 | |
Paul Kehrer | 5df0abe | 2013-10-30 16:57:04 -0500 | [diff] [blame] | 152 | .. class:: Blowfish(key) |
| 153 | |
| 154 | Blowfish is a block cipher developed by Bruce Schneier. It is known to be |
| 155 | susceptible to attacks when using weak keys. The author has recommended |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 156 | that users of Blowfish move to newer algorithms such as :class:`AES`. |
Paul Kehrer | 5df0abe | 2013-10-30 16:57:04 -0500 | [diff] [blame] | 157 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 158 | :param bytes key: The secret key. This must be kept secret. 32 to 448 bits |
| 159 | in length in increments of 8 bits. |
Paul Kehrer | 5df0abe | 2013-10-30 16:57:04 -0500 | [diff] [blame] | 160 | |
Paul Kehrer | 4da28c3 | 2013-11-07 07:50:17 +0800 | [diff] [blame] | 161 | .. class:: ARC4(key) |
| 162 | |
| 163 | ARC4 (Alleged RC4) is a stream cipher with serious weaknesses in its |
| 164 | initial stream output. Its use is strongly discouraged. ARC4 does not use |
| 165 | mode constructions. |
| 166 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 167 | :param bytes key: The secret key. This must be kept secret. Either ``40``, |
| 168 | ``56``, ``64``, ``80``, ``128``, ``192``, or ``256`` bits in length. |
Paul Kehrer | 4da28c3 | 2013-11-07 07:50:17 +0800 | [diff] [blame] | 169 | |
Paul Kehrer | 0994c56 | 2013-11-10 03:19:14 +0800 | [diff] [blame] | 170 | .. doctest:: |
| 171 | |
| 172 | >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes |
Alex Gaynor | f8796b1 | 2013-12-13 20:28:55 -0800 | [diff] [blame] | 173 | >>> from cryptography.hazmat.backends import default_backend |
Paul Kehrer | 0994c56 | 2013-11-10 03:19:14 +0800 | [diff] [blame] | 174 | >>> algorithm = algorithms.ARC4(key) |
Alex Gaynor | f56444d | 2013-12-13 15:19:22 -0800 | [diff] [blame] | 175 | >>> cipher = Cipher(algorithm, mode=None, backend=default_backend()) |
Paul Kehrer | 0994c56 | 2013-11-10 03:19:14 +0800 | [diff] [blame] | 176 | >>> encryptor = cipher.encryptor() |
| 177 | >>> ct = encryptor.update(b"a secret message") |
| 178 | >>> decryptor = cipher.decryptor() |
| 179 | >>> decryptor.update(ct) |
| 180 | 'a secret message' |
| 181 | |
Paul Kehrer | e5dc122 | 2014-02-20 16:19:32 -0600 | [diff] [blame] | 182 | .. class:: IDEA(key) |
| 183 | |
| 184 | IDEA (`International Data Encryption Algorithm`_) is a block cipher created |
| 185 | in 1991. It is an optional component of the `OpenPGP`_ standard. This cipher |
| 186 | is susceptible to attacks when using weak keys. It is recommended that you |
| 187 | do not use this cipher for new applications. |
| 188 | |
Paul Kehrer | 7e914c9 | 2014-04-09 09:12:29 -0500 | [diff] [blame] | 189 | :param bytes key: The secret key. This must be kept secret. ``128`` bits in |
Paul Kehrer | 7ba0c01 | 2014-03-08 11:33:35 -0400 | [diff] [blame] | 190 | length. |
Paul Kehrer | e5dc122 | 2014-02-20 16:19:32 -0600 | [diff] [blame] | 191 | |
David Reid | 30722b9 | 2013-11-07 13:03:39 -0800 | [diff] [blame] | 192 | |
| 193 | .. _symmetric-encryption-modes: |
| 194 | |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 195 | Modes |
| 196 | ~~~~~ |
| 197 | |
Paul Kehrer | 2129d50 | 2015-02-13 12:37:34 -0600 | [diff] [blame] | 198 | .. module:: cryptography.hazmat.primitives.ciphers.modes |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 199 | |
| 200 | .. class:: CBC(initialization_vector) |
Alex Gaynor | 48ec9a3 | 2013-08-08 11:13:46 -0700 | [diff] [blame] | 201 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 202 | 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] | 203 | considered cryptographically strong. |
| 204 | |
Paul Kehrer | fe2e3c2 | 2014-01-07 20:55:20 -0600 | [diff] [blame] | 205 | **Padding is required when using this mode.** |
| 206 | |
Alex Gaynor | 9b6fd8e | 2014-12-19 10:29:56 -0800 | [diff] [blame] | 207 | :param bytes initialization_vector: Must be :doc:`random bytes |
| 208 | </random-numbers>`. They do not need to be kept secret and they can be |
| 209 | included in a transmitted message. Must be the same number of bytes as |
| 210 | the ``block_size`` of the cipher. Each time something is encrypted a |
| 211 | new ``initialization_vector`` should be generated. Do not reuse an |
| 212 | ``initialization_vector`` with a given ``key``, and particularly do not |
| 213 | use a constant ``initialization_vector``. |
Alex Gaynor | 8ed651e | 2013-11-07 13:24:31 -0800 | [diff] [blame] | 214 | |
| 215 | A good construction looks like: |
| 216 | |
Alex Gaynor | 989061d | 2013-12-13 20:22:14 -0800 | [diff] [blame] | 217 | .. doctest:: |
Alex Gaynor | 8ed651e | 2013-11-07 13:24:31 -0800 | [diff] [blame] | 218 | |
| 219 | >>> import os |
Alex Gaynor | d83c590 | 2013-12-13 20:43:54 -0800 | [diff] [blame] | 220 | >>> from cryptography.hazmat.primitives.ciphers.modes import CBC |
Alex Gaynor | 8ed651e | 2013-11-07 13:24:31 -0800 | [diff] [blame] | 221 | >>> iv = os.urandom(16) |
| 222 | >>> mode = CBC(iv) |
| 223 | |
| 224 | While the following is bad and will leak information: |
| 225 | |
Alex Gaynor | 989061d | 2013-12-13 20:22:14 -0800 | [diff] [blame] | 226 | .. doctest:: |
Alex Gaynor | 8ed651e | 2013-11-07 13:24:31 -0800 | [diff] [blame] | 227 | |
Alex Gaynor | d83c590 | 2013-12-13 20:43:54 -0800 | [diff] [blame] | 228 | >>> from cryptography.hazmat.primitives.ciphers.modes import CBC |
Alex Gaynor | 8ed651e | 2013-11-07 13:24:31 -0800 | [diff] [blame] | 229 | >>> iv = "a" * 16 |
| 230 | >>> mode = CBC(iv) |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 231 | |
Paul Kehrer | 4506428 | 2013-10-17 13:41:53 -0500 | [diff] [blame] | 232 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 233 | .. class:: CTR(nonce) |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 234 | |
Paul Kehrer | 4506428 | 2013-10-17 13:41:53 -0500 | [diff] [blame] | 235 | .. warning:: |
| 236 | |
| 237 | Counter mode is not recommended for use with block ciphers that have a |
| 238 | block size of less than 128-bits. |
| 239 | |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 240 | 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] | 241 | cryptographically strong. It transforms a block cipher into a stream |
| 242 | cipher. |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 243 | |
Paul Kehrer | fe2e3c2 | 2014-01-07 20:55:20 -0600 | [diff] [blame] | 244 | **This mode does not require padding.** |
| 245 | |
Eeshan Garg | 9475900 | 2015-05-20 20:35:33 +0530 | [diff] [blame] | 246 | :param bytes nonce: Should be unique, a :term:`nonce`. It is |
Alex Gaynor | 9b6fd8e | 2014-12-19 10:29:56 -0800 | [diff] [blame] | 247 | critical to never reuse a ``nonce`` with a given key. Any reuse of a |
| 248 | nonce with the same key compromises the security of every message |
| 249 | encrypted with that key. Must be the same number of bytes as the |
| 250 | ``block_size`` of the cipher with a given key. The nonce does not need |
| 251 | to be kept secret and may be included with the ciphertext. |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 252 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 253 | .. class:: OFB(initialization_vector) |
Paul Kehrer | 6f412a0 | 2013-09-10 21:30:50 -0500 | [diff] [blame] | 254 | |
| 255 | OFB (Output Feedback) is a mode of operation for block ciphers. It |
| 256 | transforms a block cipher into a stream cipher. |
| 257 | |
Paul Kehrer | fe2e3c2 | 2014-01-07 20:55:20 -0600 | [diff] [blame] | 258 | **This mode does not require padding.** |
| 259 | |
Alex Gaynor | 9b6fd8e | 2014-12-19 10:29:56 -0800 | [diff] [blame] | 260 | :param bytes initialization_vector: Must be :doc:`random bytes |
| 261 | </random-numbers>`. They do not need to be kept secret and they can be |
| 262 | included in a transmitted message. Must be the same number of bytes as |
| 263 | the ``block_size`` of the cipher. Do not reuse an |
| 264 | ``initialization_vector`` with a given ``key``. |
Paul Kehrer | 6f412a0 | 2013-09-10 21:30:50 -0500 | [diff] [blame] | 265 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 266 | .. class:: CFB(initialization_vector) |
Paul Kehrer | 4223df7 | 2013-09-11 09:48:04 -0500 | [diff] [blame] | 267 | |
| 268 | CFB (Cipher Feedback) is a mode of operation for block ciphers. It |
| 269 | transforms a block cipher into a stream cipher. |
| 270 | |
Paul Kehrer | fe2e3c2 | 2014-01-07 20:55:20 -0600 | [diff] [blame] | 271 | **This mode does not require padding.** |
| 272 | |
Alex Gaynor | 9b6fd8e | 2014-12-19 10:29:56 -0800 | [diff] [blame] | 273 | :param bytes initialization_vector: Must be :doc:`random bytes |
| 274 | </random-numbers>`. They do not need to be kept secret and they can be |
| 275 | included in a transmitted message. Must be the same number of bytes as |
| 276 | the ``block_size`` of the cipher. Do not reuse an |
| 277 | ``initialization_vector`` with a given ``key``. |
Paul Kehrer | 4223df7 | 2013-09-11 09:48:04 -0500 | [diff] [blame] | 278 | |
Paul Kehrer | 2a947c4 | 2014-05-15 17:22:08 -0400 | [diff] [blame] | 279 | .. class:: CFB8(initialization_vector) |
| 280 | |
| 281 | CFB (Cipher Feedback) is a mode of operation for block ciphers. It |
| 282 | transforms a block cipher into a stream cipher. The CFB8 variant uses an |
| 283 | 8-bit shift register. |
| 284 | |
| 285 | **This mode does not require padding.** |
| 286 | |
Alex Gaynor | 9b6fd8e | 2014-12-19 10:29:56 -0800 | [diff] [blame] | 287 | :param bytes initialization_vector: Must be :doc:`random bytes |
| 288 | </random-numbers>`. They do not need to be kept secret and they can be |
| 289 | included in a transmitted message. Must be the same number of bytes as |
| 290 | the ``block_size`` of the cipher. Do not reuse an |
| 291 | ``initialization_vector`` with a given ``key``. |
Paul Kehrer | 2a947c4 | 2014-05-15 17:22:08 -0400 | [diff] [blame] | 292 | |
Alex Gaynor | cc5224f | 2014-06-30 09:25:48 -0700 | [diff] [blame] | 293 | .. class:: GCM(initialization_vector, tag=None, min_tag_length=16) |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 294 | |
Paul Kehrer | 5b828b1 | 2013-11-29 17:32:08 -0600 | [diff] [blame] | 295 | .. danger:: |
Paul Kehrer | 26c8c6a | 2013-11-29 16:24:56 -0600 | [diff] [blame] | 296 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 297 | When using this mode you **must** not use the decrypted data until |
Philipp Gesang | 2e84daa | 2017-05-02 15:28:33 +0200 | [diff] [blame] | 298 | the appropriate finalization method |
| 299 | (:meth:`~cryptography.hazmat.primitives.ciphers.CipherContext.finalize` |
| 300 | or |
| 301 | :meth:`~cryptography.hazmat.primitives.ciphers.AEADDecryptionContext.finalize_with_tag`) |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 302 | has been called. GCM provides **no** guarantees of ciphertext integrity |
Alex Gaynor | d4f9383 | 2013-12-04 16:31:59 -0600 | [diff] [blame] | 303 | until decryption is complete. |
Paul Kehrer | 26c8c6a | 2013-11-29 16:24:56 -0600 | [diff] [blame] | 304 | |
Paul Kehrer | 5578c66 | 2013-12-03 17:37:42 -0600 | [diff] [blame] | 305 | GCM (Galois Counter Mode) is a mode of operation for block ciphers. An |
| 306 | AEAD (authenticated encryption with additional data) mode is a type of |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 307 | block cipher mode that simultaneously encrypts the message as well as |
| 308 | authenticating it. Additional unencrypted data may also be authenticated. |
| 309 | Additional means of verifying integrity such as |
Ayrx | fa4a6b2 | 2014-04-16 23:03:14 +0800 | [diff] [blame] | 310 | :doc:`HMAC </hazmat/primitives/mac/hmac>` are not necessary. |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 311 | |
Paul Kehrer | fe2e3c2 | 2014-01-07 20:55:20 -0600 | [diff] [blame] | 312 | **This mode does not require padding.** |
| 313 | |
Eeshan Garg | 9475900 | 2015-05-20 20:35:33 +0530 | [diff] [blame] | 314 | :param bytes initialization_vector: Must be unique, a :term:`nonce`. |
| 315 | They do not need to be kept secret and they can be included in a |
| 316 | transmitted message. NIST `recommends a 96-bit IV length`_ for |
| 317 | performance critical situations but it can be up to 2\ :sup:`64` - 1 |
| 318 | bits. Do not reuse an ``initialization_vector`` with a given ``key``. |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 319 | |
Paul Kehrer | ca73504 | 2013-12-21 17:31:48 -0600 | [diff] [blame] | 320 | .. note:: |
| 321 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 322 | Cryptography will generate a 128-bit tag when finalizing encryption. |
| 323 | You can shorten a tag by truncating it to the desired length but this |
Paul Kehrer | fc73e2d | 2013-12-21 18:41:38 -0600 | [diff] [blame] | 324 | is **not recommended** as it lowers the security margins of the |
Alex Gaynor | cc5224f | 2014-06-30 09:25:48 -0700 | [diff] [blame] | 325 | authentication (`NIST SP-800-38D`_ recommends 96-bits or greater). |
| 326 | Applications wishing to allow truncation must pass the |
Alex Gaynor | 8f1b8e8 | 2014-06-29 20:43:29 -0700 | [diff] [blame] | 327 | ``min_tag_length`` parameter. |
| 328 | |
| 329 | .. versionchanged:: 0.5 |
| 330 | |
| 331 | The ``min_tag_length`` parameter was added in ``0.5``, previously |
Alex Gaynor | cc5224f | 2014-06-30 09:25:48 -0700 | [diff] [blame] | 332 | truncation down to ``4`` bytes was always allowed. |
Paul Kehrer | ca73504 | 2013-12-21 17:31:48 -0600 | [diff] [blame] | 333 | |
Alex Gaynor | fb843aa | 2014-02-25 11:37:36 -0800 | [diff] [blame] | 334 | :param bytes tag: The tag bytes to verify during decryption. When |
Philipp Gesang | 2e84daa | 2017-05-02 15:28:33 +0200 | [diff] [blame] | 335 | encrypting this must be ``None``. When decrypting, it may be ``None`` |
| 336 | if the tag is supplied on finalization using |
| 337 | :meth:`~cryptography.hazmat.primitives.ciphers.AEADDecryptionContext.finalize_with_tag`. |
| 338 | Otherwise, the tag is mandatory. |
Paul Kehrer | 67abc86 | 2013-11-25 14:29:35 -0600 | [diff] [blame] | 339 | |
Alex Gaynor | 8f1b8e8 | 2014-06-29 20:43:29 -0700 | [diff] [blame] | 340 | :param bytes min_tag_length: The minimum length ``tag`` must be. By default |
Alex Gaynor | cc5224f | 2014-06-30 09:25:48 -0700 | [diff] [blame] | 341 | this is ``16``, meaning tag truncation is not allowed. Allowing tag |
| 342 | truncation is strongly discouraged for most applications. |
| 343 | |
| 344 | :raises ValueError: This is raised if ``len(tag) < min_tag_length``. |
Alex Gaynor | 8f1b8e8 | 2014-06-29 20:43:29 -0700 | [diff] [blame] | 345 | |
Philipp Gesang | 2e84daa | 2017-05-02 15:28:33 +0200 | [diff] [blame] | 346 | :raises NotImplementedError: This is raised if the version of the OpenSSL |
| 347 | backend used is 1.0.1 or earlier. |
| 348 | |
Alex Gaynor | 09828cd | 2016-02-10 15:21:36 -0500 | [diff] [blame] | 349 | An example of securely encrypting and decrypting data with ``AES`` in the |
| 350 | ``GCM`` mode looks like: |
| 351 | |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 352 | .. testcode:: |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 353 | |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 354 | import os |
| 355 | |
gdmnbt | 2a0b834 | 2017-03-24 01:19:20 +0100 | [diff] [blame] | 356 | from cryptography.hazmat.backends import default_backend |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 357 | from cryptography.hazmat.primitives.ciphers import ( |
| 358 | Cipher, algorithms, modes |
| 359 | ) |
| 360 | |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 361 | def encrypt(key, plaintext, associated_data): |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 362 | # Generate a random 96-bit IV. |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 363 | iv = os.urandom(12) |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 364 | |
Alex Gaynor | ebb1cb9 | 2014-06-10 09:36:39 -0700 | [diff] [blame] | 365 | # Construct an AES-GCM Cipher object with the given key and a |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 366 | # randomly generated IV. |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 367 | encryptor = Cipher( |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 368 | algorithms.AES(key), |
| 369 | modes.GCM(iv), |
| 370 | backend=default_backend() |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 371 | ).encryptor() |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 372 | |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 373 | # associated_data will be authenticated but not encrypted, |
| 374 | # it must also be passed in on decryption. |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 375 | encryptor.authenticate_additional_data(associated_data) |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 376 | |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 377 | # Encrypt the plaintext and get the associated ciphertext. |
Paul Kehrer | af0b9f5 | 2014-01-07 19:21:49 -0600 | [diff] [blame] | 378 | # GCM does not require padding. |
| 379 | ciphertext = encryptor.update(plaintext) + encryptor.finalize() |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 380 | |
| 381 | return (iv, ciphertext, encryptor.tag) |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 382 | |
| 383 | def decrypt(key, associated_data, iv, ciphertext, tag): |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 384 | # Construct a Cipher object, with the key, iv, and additionally the |
| 385 | # GCM tag used for authenticating the message. |
| 386 | decryptor = Cipher( |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 387 | algorithms.AES(key), |
| 388 | modes.GCM(iv, tag), |
| 389 | backend=default_backend() |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 390 | ).decryptor() |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 391 | |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 392 | # We put associated_data back in or the tag will fail to verify |
| 393 | # when we finalize the decryptor. |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 394 | decryptor.authenticate_additional_data(associated_data) |
| 395 | |
Paul Kehrer | af0b9f5 | 2014-01-07 19:21:49 -0600 | [diff] [blame] | 396 | # Decryption gets us the authenticated plaintext. |
| 397 | # If the tag does not match an InvalidTag exception will be raised. |
| 398 | return decryptor.update(ciphertext) + decryptor.finalize() |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 399 | |
| 400 | iv, ciphertext, tag = encrypt( |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 401 | key, |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 402 | b"a secret message!", |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 403 | b"authenticated but not encrypted payload" |
| 404 | ) |
| 405 | |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 406 | print(decrypt( |
| 407 | key, |
| 408 | b"authenticated but not encrypted payload", |
| 409 | iv, |
| 410 | ciphertext, |
| 411 | tag |
| 412 | )) |
David Reid | 78569d6 | 2014-01-07 15:42:17 -0800 | [diff] [blame] | 413 | |
| 414 | .. testoutput:: |
| 415 | |
David Reid | abb72d2 | 2014-01-07 16:06:18 -0800 | [diff] [blame] | 416 | a secret message! |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 417 | |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 418 | |
Alex Stapleton | c5fffd3 | 2014-03-18 15:29:00 +0000 | [diff] [blame] | 419 | Insecure modes |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 420 | -------------- |
| 421 | |
Alex Gaynor | cd413a3 | 2013-09-10 18:59:43 -0700 | [diff] [blame] | 422 | .. warning:: |
| 423 | |
| 424 | These modes are insecure. New applications should never make use of them, |
| 425 | and existing applications should strongly consider migrating away. |
| 426 | |
| 427 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 428 | .. class:: ECB() |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 429 | |
| 430 | ECB (Electronic Code Book) is the simplest mode of operation for block |
Alex Gaynor | cd413a3 | 2013-09-10 18:59:43 -0700 | [diff] [blame] | 431 | ciphers. Each block of data is encrypted in the same way. This means |
| 432 | identical plaintext blocks will always result in identical ciphertext |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 433 | blocks, which can leave `significant patterns in the output`_. |
Alex Gaynor | ab5f011 | 2013-11-08 10:34:00 -0800 | [diff] [blame] | 434 | |
Paul Kehrer | fe2e3c2 | 2014-01-07 20:55:20 -0600 | [diff] [blame] | 435 | **Padding is required when using this mode.** |
| 436 | |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 437 | Interfaces |
| 438 | ---------- |
Paul Kehrer | 513b7cb | 2015-02-12 17:31:24 -0600 | [diff] [blame] | 439 | |
Paul Kehrer | 7c5c9fe | 2015-02-14 10:27:14 -0600 | [diff] [blame] | 440 | .. currentmodule:: cryptography.hazmat.primitives.ciphers |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 441 | |
| 442 | .. class:: CipherContext |
| 443 | |
| 444 | When calling ``encryptor()`` or ``decryptor()`` on a ``Cipher`` object |
Alex Gaynor | b481889 | 2014-02-06 10:58:50 -0800 | [diff] [blame] | 445 | the result will conform to the ``CipherContext`` interface. You can then |
| 446 | call ``update(data)`` with data until you have fed everything into the |
| 447 | context. Once that is done call ``finalize()`` to finish the operation and |
| 448 | obtain the remainder of the data. |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 449 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 450 | Block ciphers require that the plaintext or ciphertext always be a multiple |
| 451 | of their block size. Because of that **padding** is sometimes required to |
| 452 | make a message the correct size. ``CipherContext`` will not automatically |
| 453 | apply any padding; you'll need to add your own. For block ciphers the |
| 454 | recommended padding is |
Alex Gaynor | d99fc65 | 2014-06-25 10:24:03 -0700 | [diff] [blame] | 455 | :class:`~cryptography.hazmat.primitives.padding.PKCS7`. If you are using a |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 456 | stream cipher mode (such as |
Paul Kehrer | 45efdbc | 2015-02-12 10:58:22 -0600 | [diff] [blame] | 457 | :class:`~cryptography.hazmat.primitives.ciphers.modes.CTR`) you don't have |
| 458 | to worry about this. |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 459 | |
| 460 | .. method:: update(data) |
| 461 | |
| 462 | :param bytes data: The data you wish to pass into the context. |
| 463 | :return bytes: Returns the data that was encrypted or decrypted. |
| 464 | :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize` |
| 465 | |
| 466 | When the ``Cipher`` was constructed in a mode that turns it into a |
| 467 | stream cipher (e.g. |
Alex Gaynor | d99fc65 | 2014-06-25 10:24:03 -0700 | [diff] [blame] | 468 | :class:`~cryptography.hazmat.primitives.ciphers.modes.CTR`), this will |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 469 | return bytes immediately, however in other modes it will return chunks |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 470 | whose size is determined by the cipher's block size. |
| 471 | |
Paul Kehrer | 9b34ca9 | 2017-02-16 22:20:38 -0600 | [diff] [blame] | 472 | .. method:: update_into(data, buf) |
| 473 | |
| 474 | .. versionadded:: 1.8 |
| 475 | |
| 476 | .. warning:: |
| 477 | |
| 478 | This method allows you to avoid a memory copy by passing a writable |
| 479 | buffer and reading the resulting data. You are responsible for |
| 480 | correctly sizing the buffer and properly handling the data. This |
| 481 | method should only be used when extremely high performance is a |
| 482 | requirement and you will be making many small calls to |
| 483 | ``update_into``. |
| 484 | |
| 485 | :param bytes data: The data you wish to pass into the context. |
| 486 | :param buf: A writable Python buffer that the data will be written |
| 487 | into. This buffer should be ``len(data) + n - 1`` bytes where ``n`` |
| 488 | is the block size (in bytes) of the cipher and mode combination. |
| 489 | :return int: Number of bytes written. |
| 490 | :raises NotImplementedError: This is raised if the version of ``cffi`` |
| 491 | used is too old (this can happen on older PyPy releases). |
| 492 | :raises ValueError: This is raised if the supplied buffer is too small. |
| 493 | |
| 494 | .. doctest:: |
| 495 | |
| 496 | >>> import os |
| 497 | >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes |
| 498 | >>> from cryptography.hazmat.backends import default_backend |
| 499 | >>> backend = default_backend() |
| 500 | >>> key = os.urandom(32) |
| 501 | >>> iv = os.urandom(16) |
| 502 | >>> cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend) |
| 503 | >>> encryptor = cipher.encryptor() |
| 504 | >>> # the buffer needs to be at least len(data) + n - 1 where n is cipher/mode block size in bytes |
| 505 | >>> buf = bytearray(31) |
| 506 | >>> len_encrypted = encryptor.update_into(b"a secret message", buf) |
| 507 | >>> # get the ciphertext from the buffer reading only the bytes written to it (len_encrypted) |
| 508 | >>> ct = bytes(buf[:len_encrypted]) + encryptor.finalize() |
| 509 | >>> decryptor = cipher.decryptor() |
| 510 | >>> len_decrypted = decryptor.update_into(ct, buf) |
| 511 | >>> # get the plaintext from the buffer reading only the bytes written (len_decrypted) |
| 512 | >>> bytes(buf[:len_decrypted]) + decryptor.finalize() |
| 513 | 'a secret message' |
| 514 | |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 515 | .. method:: finalize() |
| 516 | |
| 517 | :return bytes: Returns the remainder of the data. |
| 518 | :raises ValueError: This is raised when the data provided isn't |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 519 | a multiple of the algorithm's block size. |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 520 | |
| 521 | Once ``finalize`` is called this object can no longer be used and |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 522 | :meth:`update` and :meth:`finalize` will raise an |
| 523 | :class:`~cryptography.exceptions.AlreadyFinalized` exception. |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 524 | |
| 525 | .. class:: AEADCipherContext |
| 526 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 527 | When calling ``encryptor`` or ``decryptor`` on a ``Cipher`` object |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 528 | with an AEAD mode (e.g. |
Alex Gaynor | b481889 | 2014-02-06 10:58:50 -0800 | [diff] [blame] | 529 | :class:`~cryptography.hazmat.primitives.ciphers.modes.GCM`) the result will |
| 530 | conform to the ``AEADCipherContext`` and ``CipherContext`` interfaces. If |
Philipp Gesang | 2e84daa | 2017-05-02 15:28:33 +0200 | [diff] [blame] | 531 | it is an encryption or decryption context it will additionally be an |
| 532 | ``AEADEncryptionContext`` or ``AEADDecryptionContext`` instance, |
| 533 | respectively. ``AEADCipherContext`` contains an additional method |
| 534 | :meth:`authenticate_additional_data` for adding additional authenticated |
| 535 | but unencrypted data (see note below). You should call this before calls to |
| 536 | ``update``. When you are done call ``finalize`` to finish the operation. |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 537 | |
| 538 | .. note:: |
| 539 | |
| 540 | In AEAD modes all data passed to ``update()`` will be both encrypted |
| 541 | and authenticated. Do not pass encrypted data to the |
| 542 | ``authenticate_additional_data()`` method. It is meant solely for |
| 543 | additional data you may want to authenticate but leave unencrypted. |
| 544 | |
| 545 | .. method:: authenticate_additional_data(data) |
| 546 | |
| 547 | :param bytes data: Any data you wish to authenticate but not encrypt. |
| 548 | :raises: :class:`~cryptography.exceptions.AlreadyFinalized` |
| 549 | |
| 550 | .. class:: AEADEncryptionContext |
| 551 | |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 552 | When creating an encryption context using ``encryptor`` on a ``Cipher`` |
| 553 | object with an AEAD mode such as |
| 554 | :class:`~cryptography.hazmat.primitives.ciphers.modes.GCM` an object |
| 555 | conforming to both the ``AEADEncryptionContext`` and ``AEADCipherContext`` |
| 556 | interfaces will be returned. This interface provides one |
| 557 | additional attribute ``tag``. ``tag`` can only be obtained after |
| 558 | ``finalize`` has been called. |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 559 | |
| 560 | .. attribute:: tag |
| 561 | |
| 562 | :return bytes: Returns the tag value as bytes. |
| 563 | :raises: :class:`~cryptography.exceptions.NotYetFinalized` if called |
Alex Gaynor | fb843aa | 2014-02-25 11:37:36 -0800 | [diff] [blame] | 564 | before the context is finalized. |
Paul Kehrer | ad6d164 | 2014-01-07 19:10:12 -0600 | [diff] [blame] | 565 | |
Philipp Gesang | 2e84daa | 2017-05-02 15:28:33 +0200 | [diff] [blame] | 566 | .. class:: AEADDecryptionContext |
| 567 | |
| 568 | .. versionadded:: 1.9 |
| 569 | |
| 570 | When creating an encryption context using ``decryptor`` on a ``Cipher`` |
| 571 | object with an AEAD mode such as |
| 572 | :class:`~cryptography.hazmat.primitives.ciphers.modes.GCM` an object |
| 573 | conforming to both the ``AEADDecryptionContext`` and ``AEADCipherContext`` |
| 574 | interfaces will be returned. This interface provides one additional method |
| 575 | :meth:`finalize_with_tag` that allows passing the authentication tag for |
| 576 | validation after the ciphertext has been decrypted. |
| 577 | |
| 578 | .. method:: finalize_with_tag(tag) |
| 579 | |
Paul Kehrer | 5fb1021 | 2017-05-02 12:04:53 -0500 | [diff] [blame] | 580 | .. note:: |
| 581 | |
| 582 | This method is not supported when compiled against OpenSSL 1.0.1. |
| 583 | |
Philipp Gesang | 2e84daa | 2017-05-02 15:28:33 +0200 | [diff] [blame] | 584 | :param bytes tag: The tag bytes to verify after decryption. |
| 585 | :return bytes: Returns the remainder of the data. |
| 586 | :raises ValueError: This is raised when the data provided isn't |
| 587 | a multiple of the algorithm's block size, if ``min_tag_length`` is |
| 588 | less than 4, or if ``len(tag) < min_tag_length``. |
| 589 | :raises NotImplementedError: This is raised if the version of the |
| 590 | OpenSSL backend used is 1.0.1 or earlier. |
| 591 | |
| 592 | If the authentication tag was not already supplied to the constructor |
| 593 | of the :class:`~cryptography.hazmat.primitives.ciphers.modes.GCM` mode |
| 594 | object, this method must be used instead of |
| 595 | :meth:`~cryptography.hazmat.primitives.ciphers.CipherContext.finalize`. |
| 596 | |
Paul Kehrer | 513b7cb | 2015-02-12 17:31:24 -0600 | [diff] [blame] | 597 | .. class:: CipherAlgorithm |
| 598 | |
| 599 | A named symmetric encryption algorithm. |
| 600 | |
| 601 | .. attribute:: name |
| 602 | |
| 603 | :type: str |
| 604 | |
| 605 | The standard name for the mode, for example, "AES", "Camellia", or |
| 606 | "Blowfish". |
| 607 | |
| 608 | .. attribute:: key_size |
| 609 | |
| 610 | :type: int |
| 611 | |
| 612 | The number of bits in the key being used. |
| 613 | |
| 614 | |
| 615 | .. class:: BlockCipherAlgorithm |
| 616 | |
| 617 | A block cipher algorithm. |
| 618 | |
| 619 | .. attribute:: block_size |
| 620 | |
| 621 | :type: int |
| 622 | |
| 623 | The number of bits in a block. |
| 624 | |
| 625 | Interfaces used by the symmetric cipher modes described in |
| 626 | :ref:`Symmetric Encryption Modes <symmetric-encryption-modes>`. |
| 627 | |
| 628 | .. currentmodule:: cryptography.hazmat.primitives.ciphers.modes |
| 629 | |
| 630 | .. class:: Mode |
| 631 | |
| 632 | A named cipher mode. |
| 633 | |
| 634 | .. attribute:: name |
| 635 | |
| 636 | :type: str |
| 637 | |
| 638 | This should be the standard shorthand name for the mode, for example |
| 639 | Cipher-Block Chaining mode is "CBC". |
| 640 | |
| 641 | The name may be used by a backend to influence the operation of a |
| 642 | cipher in conjunction with the algorithm's name. |
| 643 | |
| 644 | .. method:: validate_for_algorithm(algorithm) |
| 645 | |
Alex Gaynor | 2c52605 | 2015-02-24 10:55:28 -0800 | [diff] [blame] | 646 | :param cryptography.hazmat.primitives.ciphers.CipherAlgorithm algorithm: |
Paul Kehrer | 513b7cb | 2015-02-12 17:31:24 -0600 | [diff] [blame] | 647 | |
| 648 | Checks that the combination of this mode with the provided algorithm |
| 649 | meets any necessary invariants. This should raise an exception if they |
| 650 | are not met. |
| 651 | |
| 652 | For example, the |
| 653 | :class:`~cryptography.hazmat.primitives.ciphers.modes.CBC` mode uses |
| 654 | this method to check that the provided initialization vector's length |
| 655 | matches the block size of the algorithm. |
| 656 | |
| 657 | |
| 658 | .. class:: ModeWithInitializationVector |
| 659 | |
| 660 | A cipher mode with an initialization vector. |
| 661 | |
| 662 | .. attribute:: initialization_vector |
| 663 | |
| 664 | :type: bytes |
| 665 | |
| 666 | Exact requirements of the initialization are described by the |
| 667 | documentation of individual modes. |
| 668 | |
| 669 | |
| 670 | .. class:: ModeWithNonce |
| 671 | |
| 672 | A cipher mode with a nonce. |
| 673 | |
| 674 | .. attribute:: nonce |
| 675 | |
| 676 | :type: bytes |
| 677 | |
| 678 | Exact requirements of the nonce are described by the documentation of |
| 679 | individual modes. |
| 680 | |
| 681 | |
Paul Kehrer | 4ab6059 | 2015-02-13 09:06:48 -0600 | [diff] [blame] | 682 | .. class:: ModeWithAuthenticationTag |
| 683 | |
| 684 | A cipher mode with an authentication tag. |
| 685 | |
| 686 | .. attribute:: tag |
| 687 | |
| 688 | :type: bytes |
| 689 | |
| 690 | Exact requirements of the tag are described by the documentation of |
| 691 | individual modes. |
| 692 | |
| 693 | |
Alex Gaynor | ab5f011 | 2013-11-08 10:34:00 -0800 | [diff] [blame] | 694 | |
| 695 | .. _`described by Colin Percival`: http://www.daemonology.net/blog/2009-06-11-cryptographic-right-answers.html |
Alex Stapleton | 092351d | 2014-03-09 17:44:43 +0000 | [diff] [blame] | 696 | .. _`recommends a 96-bit IV length`: http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-spec.pdf |
Alex Gaynor | 64f1f42 | 2017-02-27 22:25:37 -0500 | [diff] [blame] | 697 | .. _`NIST SP-800-38D`: http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38D.pdf |
Alex Gaynor | 111aff2 | 2015-02-17 14:47:49 -0800 | [diff] [blame] | 698 | .. _`Communications Security Establishment`: https://www.cse-cst.gc.ca |
Alex Gaynor | 6201230 | 2015-02-17 10:49:22 -0800 | [diff] [blame] | 699 | .. _`encrypt`: https://ssd.eff.org/en/module/what-encryption |
Alex Gaynor | 6422d83 | 2016-03-06 21:40:57 -0500 | [diff] [blame] | 700 | .. _`CRYPTREC`: https://www.cryptrec.go.jp/english/ |
Alex Gaynor | 543031a | 2015-02-16 20:27:35 -0800 | [diff] [blame] | 701 | .. _`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] | 702 | .. _`International Data Encryption Algorithm`: https://en.wikipedia.org/wiki/International_Data_Encryption_Algorithm |
Alex Gaynor | e51236d | 2016-11-06 10:13:35 -0500 | [diff] [blame] | 703 | .. _`OpenPGP`: http://openpgp.org |