blob: e05248ffe0a5e7e428e7f4a7e1e3373ebed9bee5 [file] [log] [blame]
Alex Gaynor2724ff62013-12-20 13:51:42 -08001.. hazmat:: /fernet
Donald Stufftd8f01182013-10-27 16:59:56 -04002
3
Donald Stuffte51fb932013-10-27 17:26:17 -04004Symmetric Encryption
5====================
6
Paul Kehrer051099e2013-11-06 15:53:40 +08007.. currentmodule:: cryptography.hazmat.primitives.ciphers
David Reid1f3d7182013-10-22 16:55:18 -07008
Donald Stufft173de982013-08-12 07:34:39 -04009.. testsetup::
10
11 import binascii
12 key = binascii.unhexlify(b"0" * 32)
13 iv = binascii.unhexlify(b"0" * 32)
14
15
Alex Gaynorf6c47e92013-08-08 07:16:01 -070016Symmetric encryption is a way to encrypt (hide the plaintext value) material
Alex Gaynorb317c7a2013-11-15 16:45:52 -080017where the sender and receiver both use the same key. Note that symmetric
Alex Gaynorab5f0112013-11-08 10:34:00 -080018encryption is **not** sufficient for most applications, because it only
19provides secrecy (an attacker can't see the message) but not authenticity (an
20attacker can create bogus messages and force the application to decrypt them).
Alex Gaynor9316f4c2013-11-15 16:38:42 -080021For this reason it is *strongly* recommended to combine encryption with a
Alex Gaynorab5f0112013-11-08 10:34:00 -080022message authentication code, such as :doc:`HMAC </hazmat/primitives/hmac>`, in
23an "encrypt-then-MAC" formulation as `described by Colin Percival`_.
Alex Gaynorf6c47e92013-08-08 07:16:01 -070024
David Reidef0fcf22013-11-06 11:12:45 -080025.. class:: Cipher(algorithm, mode, backend)
Alex Gaynorf6c47e92013-08-08 07:16:01 -070026
Alex Gaynorab5f0112013-11-08 10:34:00 -080027 Cipher objects combine an algorithm (such as
28 :class:`~cryptography.hazmat.primitives.ciphers.algorithms.AES`) with a
29 mode (such as
30 :class:`~cryptography.hazmat.primitives.ciphers.modes.CBC` or
31 :class:`~cryptography.hazmat.primitives.ciphers.modes.CTR`). A simple
32 example of encrypting (and then decrypting) content with AES is:
Alex Gaynorf6c47e92013-08-08 07:16:01 -070033
Donald Stufft173de982013-08-12 07:34:39 -040034 .. doctest::
Alex Gaynorf6c47e92013-08-08 07:16:01 -070035
Paul Kehrer051099e2013-11-06 15:53:40 +080036 >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
Alex Gaynorf8796b12013-12-13 20:28:55 -080037 >>> from cryptography.hazmat.backends import default_backend
Alex Gaynorf56444d2013-12-13 15:19:22 -080038 >>> backend = default_backend()
David Reid63fa19a2013-11-20 10:49:13 -080039 >>> cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
Paul Kehrer3e0895c2013-10-21 22:19:29 -050040 >>> encryptor = cipher.encryptor()
41 >>> ct = encryptor.update(b"a secret message") + encryptor.finalize()
42 >>> decryptor = cipher.decryptor()
43 >>> decryptor.update(ct) + decryptor.finalize()
Paul Kehrerf6cf9562013-10-22 10:36:00 -050044 'a secret message'
Alex Gaynorf6c47e92013-08-08 07:16:01 -070045
David Reid663295d2013-11-20 13:55:08 -080046 :param algorithms: A
47 :class:`~cryptography.hazmat.primitives.interfaces.CipherAlgorithm`
48 provider such as those described
49 :ref:`below <symmetric-encryption-algorithms>`.
50 :param mode: A :class:`~cryptography.hazmat.primitives.interfaces.Mode`
51 provider such as those described
52 :ref:`below <symmetric-encryption-modes>`.
53 :param backend: A
Alex Gaynorf8796b12013-12-13 20:28:55 -080054 :class:`~cryptography.hazmat.backends.interfaces.CipherBackend`
David Reid663295d2013-11-20 13:55:08 -080055 provider.
Alex Gaynor0ca7fdb2013-08-08 07:35:26 -070056
Paul Kehrer5399fd02013-10-21 23:48:25 -050057 .. method:: encryptor()
Alex Gaynor09515f02013-08-08 15:26:55 -070058
David Reid63ba6652013-10-22 14:09:19 -070059 :return: An encrypting
Donald Stufftf04317a2013-10-27 16:44:30 -040060 :class:`~cryptography.hazmat.primitives.interfaces.CipherContext`
David Reid63ba6652013-10-22 14:09:19 -070061 provider.
Alex Gaynore62aa402013-08-08 15:23:11 -070062
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070063 If the backend doesn't support the requested combination of ``cipher``
Alex Stapleton35cb3652013-12-21 16:29:45 +000064 and ``mode`` an :class:`~cryptography.exceptions.UnsupportedAlgorithm`
Alex Gaynor3949f112013-11-02 16:57:10 -070065 will be raised.
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070066
Paul Kehrer5399fd02013-10-21 23:48:25 -050067 .. method:: decryptor()
68
David Reid63ba6652013-10-22 14:09:19 -070069 :return: A decrypting
Donald Stufftf04317a2013-10-27 16:44:30 -040070 :class:`~cryptography.hazmat.primitives.interfaces.CipherContext`
David Reid63ba6652013-10-22 14:09:19 -070071 provider.
Paul Kehrer5399fd02013-10-21 23:48:25 -050072
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070073 If the backend doesn't support the requested combination of ``cipher``
Alex Gaynor3949f112013-11-02 16:57:10 -070074 and ``mode`` an :class:`cryptography.exceptions.UnsupportedAlgorithm`
75 will be raised.
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070076
77
Donald Stufftf04317a2013-10-27 16:44:30 -040078.. currentmodule:: cryptography.hazmat.primitives.interfaces
David Reid1f3d7182013-10-22 16:55:18 -070079
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070080.. class:: CipherContext
Paul Kehrer5399fd02013-10-21 23:48:25 -050081
Paul Kehrer051099e2013-11-06 15:53:40 +080082 When calling ``encryptor()`` or ``decryptor()`` on a ``Cipher`` object
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070083 you will receive a return object conforming to the ``CipherContext``
84 interface. You can then call ``update(data)`` with data until you have fed
85 everything into the context. Once that is done call ``finalize()`` to
86 finish the operation and obtain the remainder of the data.
Paul Kehrer5399fd02013-10-21 23:48:25 -050087
Alex Gaynor797dd832013-11-22 13:08:58 -080088 Block ciphers require that plaintext or ciphertext always be a multiple of
89 their block size, because of that **padding** is often required to make a
90 message the correct size. ``CipherContext`` will not automatically apply
Alex Gaynor1a278a82013-11-27 13:40:45 -060091 any padding; you'll need to add your own. For block ciphers the recommended
Alex Gaynor797dd832013-11-22 13:08:58 -080092 padding is :class:`cryptography.hazmat.primitives.padding.PKCS7`. If you
93 are using a stream cipher mode (such as
94 :class:`cryptography.hazmat.primitives.modes.CTR`) you don't have to worry
95 about this.
96
Paul Kehrer5399fd02013-10-21 23:48:25 -050097 .. method:: update(data)
98
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070099 :param bytes data: The data you wish to pass into the context.
Paul Kehrer5399fd02013-10-21 23:48:25 -0500100 :return bytes: Returns the data that was encrypted or decrypted.
Alex Gaynor34511c62013-11-13 13:30:30 -0800101 :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize`
Alex Gaynore62aa402013-08-08 15:23:11 -0700102
Paul Kehrer051099e2013-11-06 15:53:40 +0800103 When the ``Cipher`` was constructed in a mode that turns it into a
Alex Gaynorfc09a7c2013-11-01 14:43:02 -0700104 stream cipher (e.g.
Paul Kehrer051099e2013-11-06 15:53:40 +0800105 :class:`cryptography.hazmat.primitives.ciphers.modes.CTR`), this will
Alex Gaynorbf2de742013-11-01 14:48:19 -0700106 return bytes immediately, however in other modes it will return chunks,
107 whose size is determined by the cipher's block size.
Alex Gaynord1f02012013-11-01 14:12:35 -0700108
Alex Gaynore62aa402013-08-08 15:23:11 -0700109 .. method:: finalize()
110
Paul Kehrer5399fd02013-10-21 23:48:25 -0500111 :return bytes: Returns the remainder of the data.
Alex Gaynorbae899a2013-11-22 16:54:55 -0800112 :raises ValueError: This is raised when the data provided isn't
113 correctly padded to be a multiple of the
114 algorithm's block size.
Alex Gaynord96d1002013-08-08 07:37:26 -0700115
Alex Gaynor34511c62013-11-13 13:30:30 -0800116 Once ``finalize`` is called this object can no longer be used and
Alex Gaynor9b70ba32013-11-13 13:49:43 -0800117 :meth:`update` and :meth:`finalize` will raise
Alex Gaynor34511c62013-11-13 13:30:30 -0800118 :class:`~cryptography.exceptions.AlreadyFinalized`.
119
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600120.. class:: AEADCipherContext
121
122 When calling ``encryptor()`` or ``decryptor()`` on a ``Cipher`` object
123 with an AEAD mode you will receive a return object conforming to the
Paul Kehrer5b828b12013-11-29 17:32:08 -0600124 ``AEADCipherContext`` interface (in addition to the ``CipherContext``
Paul Kehrercd28a7c2013-12-03 18:17:57 -0600125 interface). If it is an encryption context it will additionally be an
126 ``AEADEncryptionContext`` interface. ``AEADCipherContext`` contains an
127 additional method ``authenticate_additional_data`` for adding additional
128 authenticated but unencrypted data. You should call this before calls to
129 ``update``. When you are done call ``finalize()`` to finish the operation.
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600130
Paul Kehrerbc31fb22013-11-24 11:03:53 -0600131 .. method:: authenticate_additional_data(data)
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600132
133 :param bytes data: The data you wish to authenticate but not encrypt.
134 :raises: :class:`~cryptography.exceptions.AlreadyFinalized`
135
Paul Kehrer5b828b12013-11-29 17:32:08 -0600136.. class:: AEADEncryptionContext
137
138 When creating an encryption context using ``encryptor()`` on a ``Cipher``
139 object with an AEAD mode you will receive a return object conforming to the
140 ``AEADEncryptionContext`` interface (as well as ``AEADCipherContext``).
141 This interface provides one additional attribute ``tag``. ``tag`` can only
142 be obtained after ``finalize()``.
143
Paul Kehrer65c4e0a2013-11-21 11:20:56 -0600144 .. attribute:: tag
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600145
146 :return bytes: Returns the tag value as bytes.
Paul Kehrer6331daa2013-11-22 13:42:02 -0600147 :raises: :class:`~cryptography.exceptions.NotYetFinalized` if called
148 before the context is finalized.
Paul Kehrer5b828b12013-11-29 17:32:08 -0600149
David Reid663295d2013-11-20 13:55:08 -0800150.. _symmetric-encryption-algorithms:
151
Paul Kehrer051099e2013-11-06 15:53:40 +0800152Algorithms
153~~~~~~~~~~
Alex Gaynord96d1002013-08-08 07:37:26 -0700154
Paul Kehrer051099e2013-11-06 15:53:40 +0800155.. currentmodule:: cryptography.hazmat.primitives.ciphers.algorithms
David Reid1f3d7182013-10-22 16:55:18 -0700156
157.. class:: AES(key)
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -0700158
Alex Gaynor1e3f81f2013-08-08 11:31:43 -0700159 AES (Advanced Encryption Standard) is a block cipher standardized by NIST.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -0700160 AES is both fast, and cryptographically strong. It is a good default
161 choice for encryption.
162
163 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
Alex Gaynor48ec9a32013-08-08 11:13:46 -0700164 This must be kept secret.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -0700165
David Reid1f3d7182013-10-22 16:55:18 -0700166.. class:: Camellia(key)
Paul Kehrerdff22d42013-09-27 13:43:06 -0500167
168 Camellia is a block cipher approved for use by CRYPTREC and ISO/IEC.
169 It is considered to have comparable security and performance to AES, but
170 is not as widely studied or deployed.
171
172 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
173 This must be kept secret.
174
Alex Gaynord96d1002013-08-08 07:37:26 -0700175
David Reid1f3d7182013-10-22 16:55:18 -0700176.. class:: TripleDES(key)
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700177
Alex Gaynor9316f4c2013-11-15 16:38:42 -0800178 Triple DES (Data Encryption Standard), sometimes referred to as 3DES, is a
179 block cipher standardized by NIST. Triple DES has known crypto-analytic
Alex Gaynor17adce62013-10-16 17:04:40 -0700180 flaws, however none of them currently enable a practical attack.
Alex Gaynor9316f4c2013-11-15 16:38:42 -0800181 Nonetheless, Triples DES is not recommended for new applications because it
Alex Gaynorfbcc5642013-10-22 08:26:00 -0700182 is incredibly slow; old applications should consider moving away from it.
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700183
184 :param bytes key: The secret key, either ``64``, ``128``, or ``192`` bits
185 (note that DES functionally uses ``56``, ``112``, or
186 ``168`` bits of the key, there is a parity byte in each
187 component of the key), in some materials these are
188 referred to as being up to three separate keys (each
189 ``56`` bits long), they can simply be concatenated to
190 produce the full key. This must be kept secret.
191
Paul Kehrer6022d452013-10-30 17:03:54 -0500192.. class:: CAST5(key)
193
194 CAST5 (also known as CAST-128) is a block cipher approved for use in the
195 Canadian government by their Communications Security Establishment. It is a
196 variable key length cipher and supports keys from 40-128 bits in length.
197
198 :param bytes key: The secret key, 40-128 bits in length (in increments of
199 8). This must be kept secret.
200
Paul Kehrer3446d812013-10-31 17:15:03 -0500201Weak Ciphers
202------------
203
204.. warning::
205
206 These ciphers are considered weak for a variety of reasons. New
207 applications should avoid their use and existing applications should
208 strongly consider migrating away.
209
Paul Kehrer5df0abe2013-10-30 16:57:04 -0500210.. 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 Gaynorab5f0112013-11-08 10:34:00 -0800214 that users of Blowfish move to newer algorithms, such as :class:`AES`.
Paul Kehrer5df0abe2013-10-30 16:57:04 -0500215
216 :param bytes key: The secret key, 32-448 bits in length (in increments of
217 8). This must be kept secret.
218
Paul Kehrer4da28c32013-11-07 07:50:17 +0800219.. class:: ARC4(key)
220
221 ARC4 (Alleged RC4) is a stream cipher with serious weaknesses in its
222 initial stream output. Its use is strongly discouraged. ARC4 does not use
223 mode constructions.
224
225 :param bytes key: The secret key, ``40``, ``56``, ``64``, ``80``, ``128``,
226 ``192``, or ``256`` bits in length. This must be kept
227 secret.
228
Paul Kehrer0994c562013-11-10 03:19:14 +0800229 .. doctest::
230
231 >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
Alex Gaynorf8796b12013-12-13 20:28:55 -0800232 >>> from cryptography.hazmat.backends import default_backend
Paul Kehrer0994c562013-11-10 03:19:14 +0800233 >>> algorithm = algorithms.ARC4(key)
Alex Gaynorf56444d2013-12-13 15:19:22 -0800234 >>> cipher = Cipher(algorithm, mode=None, backend=default_backend())
Paul Kehrer0994c562013-11-10 03:19:14 +0800235 >>> encryptor = cipher.encryptor()
236 >>> ct = encryptor.update(b"a secret message")
237 >>> decryptor = cipher.decryptor()
238 >>> decryptor.update(ct)
239 'a secret message'
240
David Reid30722b92013-11-07 13:03:39 -0800241
242.. _symmetric-encryption-modes:
243
Alex Gaynord96d1002013-08-08 07:37:26 -0700244Modes
245~~~~~
246
Paul Kehrer051099e2013-11-06 15:53:40 +0800247.. currentmodule:: cryptography.hazmat.primitives.ciphers.modes
David Reid1f3d7182013-10-22 16:55:18 -0700248
249.. class:: CBC(initialization_vector)
Alex Gaynor48ec9a32013-08-08 11:13:46 -0700250
251 CBC (Cipher block chaining) is a mode of operation for block ciphers. It is
252 considered cryptographically strong.
253
254 :param bytes initialization_vector: Must be random bytes. They do not need
255 to be kept secret (they can be included
Alex Gaynor2dc2b862013-08-08 11:58:04 -0700256 in a transmitted message). Must be the
257 same number of bytes as the
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800258 ``block_size`` of the cipher. Each time
Alex Gaynor9de452d2013-11-07 13:28:23 -0800259 something is encrypted a new
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800260 ``initialization_vector`` should be
261 generated. Do not reuse an
262 ``initialization_vector`` with
263 a given ``key``, and particularly do
264 not use a constant
265 ``initialization_vector``.
266
267 A good construction looks like:
268
Alex Gaynor989061d2013-12-13 20:22:14 -0800269 .. doctest::
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800270
271 >>> import os
Alex Gaynord83c5902013-12-13 20:43:54 -0800272 >>> from cryptography.hazmat.primitives.ciphers.modes import CBC
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800273 >>> iv = os.urandom(16)
274 >>> mode = CBC(iv)
275
276 While the following is bad and will leak information:
277
Alex Gaynor989061d2013-12-13 20:22:14 -0800278 .. doctest::
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800279
Alex Gaynord83c5902013-12-13 20:43:54 -0800280 >>> from cryptography.hazmat.primitives.ciphers.modes import CBC
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800281 >>> iv = "a" * 16
282 >>> mode = CBC(iv)
Paul Kehrer13f108f2013-09-09 21:41:03 -0500283
Paul Kehrer45064282013-10-17 13:41:53 -0500284
David Reid1f3d7182013-10-22 16:55:18 -0700285.. class:: CTR(nonce)
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500286
Paul Kehrer45064282013-10-17 13:41:53 -0500287 .. warning::
288
289 Counter mode is not recommended for use with block ciphers that have a
290 block size of less than 128-bits.
291
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500292 CTR (Counter) is a mode of operation for block ciphers. It is considered
Alex Gaynord1f02012013-11-01 14:12:35 -0700293 cryptographically strong. It transforms a block cipher into a stream
294 cipher.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500295
Paul Kehrer89b3dd32013-10-17 14:02:45 -0500296 :param bytes nonce: Should be random bytes. It is critical to never reuse a
297 ``nonce`` with a given key. Any reuse of a nonce
298 with the same key compromises the security of every
299 message encrypted with that key. Must be the same
300 number of bytes as the ``block_size`` of the cipher
301 with a given key. The nonce does not need to be kept
302 secret and may be included alongside the ciphertext.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500303
David Reid1f3d7182013-10-22 16:55:18 -0700304.. class:: OFB(initialization_vector)
Paul Kehrer6f412a02013-09-10 21:30:50 -0500305
306 OFB (Output Feedback) is a mode of operation for block ciphers. It
307 transforms a block cipher into a stream cipher.
308
David Reidf1a39bd2013-09-11 16:28:42 -0700309 :param bytes initialization_vector: Must be random bytes. They do not need
310 to be kept secret (they can be included
311 in a transmitted message). Must be the
312 same number of bytes as the
313 ``block_size`` of the cipher. Do not
314 reuse an ``initialization_vector`` with
315 a given ``key``.
Paul Kehrer6f412a02013-09-10 21:30:50 -0500316
David Reid1f3d7182013-10-22 16:55:18 -0700317.. class:: CFB(initialization_vector)
Paul Kehrer4223df72013-09-11 09:48:04 -0500318
319 CFB (Cipher Feedback) is a mode of operation for block ciphers. It
320 transforms a block cipher into a stream cipher.
321
322 :param bytes initialization_vector: Must be random bytes. They do not need
323 to be kept secret (they can be included
324 in a transmitted message). Must be the
325 same number of bytes as the
326 ``block_size`` of the cipher. Do not
327 reuse an ``initialization_vector`` with
328 a given ``key``.
329
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600330.. class:: GCM(initialization_vector, tag=None)
331
Paul Kehrer5b828b12013-11-29 17:32:08 -0600332 .. danger::
Paul Kehrer26c8c6a2013-11-29 16:24:56 -0600333
Alex Gaynord4f93832013-12-04 16:31:59 -0600334 When using this mode you MUST not use the decrypted data until
Alex Gaynor0d23e942013-12-04 17:28:24 -0600335 :meth:`~cryptography.hazmat.primitives.interfaces.CipherContext.finalize`
Alex Gaynord4f93832013-12-04 16:31:59 -0600336 has been called. GCM provides NO guarantees of ciphertext integrity
337 until decryption is complete.
Paul Kehrer26c8c6a2013-11-29 16:24:56 -0600338
Paul Kehrer5578c662013-12-03 17:37:42 -0600339 GCM (Galois Counter Mode) is a mode of operation for block ciphers. An
340 AEAD (authenticated encryption with additional data) mode is a type of
341 block cipher mode that encrypts the message as well as authenticating it
342 (and optionally additional data that is not encrypted) simultaneously.
343 Additional means of verifying integrity (like
Paul Kehrer2631c2b2013-11-24 10:20:50 -0600344 :doc:`HMAC </hazmat/primitives/hmac>`) are not necessary.
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600345
346 :param bytes initialization_vector: Must be random bytes. They do not need
347 to be kept secret (they can be included
Paul Kehrer67284372013-12-03 18:58:14 -0600348 in a transmitted message). NIST
349 `recommends 96-bit IV length`_ for
350 performance critical situations, but it
351 can be up to 2\ :sup:`64` - 1 bits.
352 Do not reuse an ``initialization_vector``
353 with a given ``key``.
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600354
Paul Kehrerca735042013-12-21 17:31:48 -0600355 .. note::
356
Paul Kehrerfc73e2d2013-12-21 18:41:38 -0600357 Cryptography will emit a 128-bit tag when finalizing encryption.
358 You can shorten a tag by truncating it to the desired length, but this
359 is **not recommended** as it lowers the security margins of the
Paul Kehrer048d6cb2013-12-21 18:53:19 -0600360 authentication (`NIST SP-800-38D`_ recommends 96-bits or greater).
Paul Kehrerfc73e2d2013-12-21 18:41:38 -0600361 If you must shorten the tag the minimum allowed length is 4 bytes
Paul Kehrer048d6cb2013-12-21 18:53:19 -0600362 (32-bits). Applications **must** verify the tag is the expected length
363 to guarantee the expected security margin.
Paul Kehrerca735042013-12-21 17:31:48 -0600364
Paul Kehrera07925a2013-12-06 11:49:42 -0600365 :param bytes tag: The tag bytes to verify during decryption. When encrypting
366 this must be None.
Paul Kehrer67abc862013-11-25 14:29:35 -0600367
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600368 .. doctest::
369
370 >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
Alex Gaynorf8796b12013-12-13 20:28:55 -0800371 >>> from cryptography.hazmat.backends import default_backend
Alex Gaynorf56444d2013-12-13 15:19:22 -0800372 >>> cipher = Cipher(algorithms.AES(key), modes.GCM(iv), backend=default_backend())
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600373 >>> encryptor = cipher.encryptor()
Paul Kehrerbc31fb22013-11-24 11:03:53 -0600374 >>> encryptor.authenticate_additional_data(b"authenticated but not encrypted payload")
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600375 >>> ct = encryptor.update(b"a secret message") + encryptor.finalize()
376 >>> tag = encryptor.tag
Paul Kehrere0b5bb12013-11-29 16:35:04 -0600377 >>> cipher = Cipher(algorithms.AES(key), modes.GCM(iv, tag), backend)
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600378 >>> decryptor = cipher.decryptor()
Paul Kehrerbc31fb22013-11-24 11:03:53 -0600379 >>> decryptor.authenticate_additional_data(b"authenticated but not encrypted payload")
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600380 >>> decryptor.update(ct) + decryptor.finalize()
381 'a secret message'
382
Paul Kehrer13f108f2013-09-09 21:41:03 -0500383
384Insecure Modes
385--------------
386
Alex Gaynorcd413a32013-09-10 18:59:43 -0700387.. warning::
388
389 These modes are insecure. New applications should never make use of them,
390 and existing applications should strongly consider migrating away.
391
392
David Reid1f3d7182013-10-22 16:55:18 -0700393.. class:: ECB()
Paul Kehrer13f108f2013-09-09 21:41:03 -0500394
395 ECB (Electronic Code Book) is the simplest mode of operation for block
Alex Gaynorcd413a32013-09-10 18:59:43 -0700396 ciphers. Each block of data is encrypted in the same way. This means
397 identical plaintext blocks will always result in identical ciphertext
398 blocks, and thus result in information leakage
Alex Gaynorab5f0112013-11-08 10:34:00 -0800399
400
401.. _`described by Colin Percival`: http://www.daemonology.net/blog/2009-06-11-cryptographic-right-answers.html
Paul Kehrer67284372013-12-03 18:58:14 -0600402.. _`recommends 96-bit IV length`: http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-spec.pdf
Paul Kehrera7fbf072013-12-21 18:12:25 -0600403.. _`NIST SP-800-38D`: http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf