blob: 2306c5b719e9e7c6733ae26ac7896fc42a395ba3 [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
David Reid663295d2013-11-20 13:55:08 -080077.. _symmetric-encryption-algorithms:
78
Paul Kehrer051099e2013-11-06 15:53:40 +080079Algorithms
80~~~~~~~~~~
Alex Gaynord96d1002013-08-08 07:37:26 -070081
Paul Kehrer051099e2013-11-06 15:53:40 +080082.. currentmodule:: cryptography.hazmat.primitives.ciphers.algorithms
David Reid1f3d7182013-10-22 16:55:18 -070083
84.. class:: AES(key)
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070085
Alex Gaynor1e3f81f2013-08-08 11:31:43 -070086 AES (Advanced Encryption Standard) is a block cipher standardized by NIST.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070087 AES is both fast, and cryptographically strong. It is a good default
88 choice for encryption.
89
90 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
Alex Gaynorfb843aa2014-02-25 11:37:36 -080091 This must be kept secret.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070092
David Reid1f3d7182013-10-22 16:55:18 -070093.. class:: Camellia(key)
Paul Kehrerdff22d42013-09-27 13:43:06 -050094
95 Camellia is a block cipher approved for use by CRYPTREC and ISO/IEC.
96 It is considered to have comparable security and performance to AES, but
97 is not as widely studied or deployed.
98
99 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
Alex Gaynorfb843aa2014-02-25 11:37:36 -0800100 This must be kept secret.
Paul Kehrerdff22d42013-09-27 13:43:06 -0500101
David Reid1f3d7182013-10-22 16:55:18 -0700102.. class:: TripleDES(key)
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700103
Alex Gaynor9316f4c2013-11-15 16:38:42 -0800104 Triple DES (Data Encryption Standard), sometimes referred to as 3DES, is a
105 block cipher standardized by NIST. Triple DES has known crypto-analytic
Alex Gaynor17adce62013-10-16 17:04:40 -0700106 flaws, however none of them currently enable a practical attack.
Alex Gaynor9316f4c2013-11-15 16:38:42 -0800107 Nonetheless, Triples DES is not recommended for new applications because it
Alex Gaynorfbcc5642013-10-22 08:26:00 -0700108 is incredibly slow; old applications should consider moving away from it.
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700109
110 :param bytes key: The secret key, either ``64``, ``128``, or ``192`` bits
Alex Gaynorfb843aa2014-02-25 11:37:36 -0800111 (note that DES functionally uses ``56``, ``112``, or ``168`` bits of
112 the key, there is a parity byte in each component of the key), in some
113 materials these are referred to as being up to three separate keys
114 (each ``56`` bits long), they can simply be concatenated to produce the
115 full key. This must be kept secret.
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700116
Paul Kehrerbab0e1a2014-02-09 10:51:59 -0600117.. class:: CAST5(key)
118
Paul Kehrera5011ec2014-02-13 12:33:34 -0600119 .. versionadded:: 0.2
120
Paul Kehrerbab0e1a2014-02-09 10:51:59 -0600121 CAST5 (also known as CAST-128) is a block cipher approved for use in the
122 Canadian government by the `Communications Security Establishment`_. It is
123 a variable key length cipher and supports keys from 40-128 bits in length.
124
125 :param bytes key: The secret key, 40-128 bits in length (in increments of
Alex Gaynorfb843aa2014-02-25 11:37:36 -0800126 8). This must be kept secret.
Paul Kehrerbab0e1a2014-02-09 10:51:59 -0600127
Paul Kehrer3446d812013-10-31 17:15:03 -0500128Weak Ciphers
129------------
130
131.. warning::
132
133 These ciphers are considered weak for a variety of reasons. New
134 applications should avoid their use and existing applications should
135 strongly consider migrating away.
136
Paul Kehrer5df0abe2013-10-30 16:57:04 -0500137.. class:: Blowfish(key)
138
139 Blowfish is a block cipher developed by Bruce Schneier. It is known to be
140 susceptible to attacks when using weak keys. The author has recommended
Alex Gaynorab5f0112013-11-08 10:34:00 -0800141 that users of Blowfish move to newer algorithms, such as :class:`AES`.
Paul Kehrer5df0abe2013-10-30 16:57:04 -0500142
143 :param bytes key: The secret key, 32-448 bits in length (in increments of
Alex Gaynorfb843aa2014-02-25 11:37:36 -0800144 8). This must be kept secret.
Paul Kehrer5df0abe2013-10-30 16:57:04 -0500145
Paul Kehrer4da28c32013-11-07 07:50:17 +0800146.. class:: ARC4(key)
147
148 ARC4 (Alleged RC4) is a stream cipher with serious weaknesses in its
149 initial stream output. Its use is strongly discouraged. ARC4 does not use
150 mode constructions.
151
152 :param bytes key: The secret key, ``40``, ``56``, ``64``, ``80``, ``128``,
Alex Gaynorfb843aa2014-02-25 11:37:36 -0800153 ``192``, or ``256`` bits in length. This must be kept secret.
Paul Kehrer4da28c32013-11-07 07:50:17 +0800154
Paul Kehrer0994c562013-11-10 03:19:14 +0800155 .. doctest::
156
157 >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
Alex Gaynorf8796b12013-12-13 20:28:55 -0800158 >>> from cryptography.hazmat.backends import default_backend
Paul Kehrer0994c562013-11-10 03:19:14 +0800159 >>> algorithm = algorithms.ARC4(key)
Alex Gaynorf56444d2013-12-13 15:19:22 -0800160 >>> cipher = Cipher(algorithm, mode=None, backend=default_backend())
Paul Kehrer0994c562013-11-10 03:19:14 +0800161 >>> encryptor = cipher.encryptor()
162 >>> ct = encryptor.update(b"a secret message")
163 >>> decryptor = cipher.decryptor()
164 >>> decryptor.update(ct)
165 'a secret message'
166
David Reid30722b92013-11-07 13:03:39 -0800167
168.. _symmetric-encryption-modes:
169
Alex Gaynord96d1002013-08-08 07:37:26 -0700170Modes
171~~~~~
172
Paul Kehrer051099e2013-11-06 15:53:40 +0800173.. currentmodule:: cryptography.hazmat.primitives.ciphers.modes
David Reid1f3d7182013-10-22 16:55:18 -0700174
175.. class:: CBC(initialization_vector)
Alex Gaynor48ec9a32013-08-08 11:13:46 -0700176
177 CBC (Cipher block chaining) is a mode of operation for block ciphers. It is
178 considered cryptographically strong.
179
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600180 **Padding is required when using this mode.**
181
Alex Gaynor48ec9a32013-08-08 11:13:46 -0700182 :param bytes initialization_vector: Must be random bytes. They do not need
Alex Gaynorfb843aa2014-02-25 11:37:36 -0800183 to be kept secret (they can be included in a transmitted message). Must
184 be the same number of bytes as the ``block_size`` of the cipher. Each
185 time something is encrypted a new ``initialization_vector`` should be
186 generated. Do not reuse an ``initialization_vector`` with a given
187 ``key``, and particularly do not use a constant
188 ``initialization_vector``.
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800189
190 A good construction looks like:
191
Alex Gaynor989061d2013-12-13 20:22:14 -0800192 .. doctest::
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800193
194 >>> import os
Alex Gaynord83c5902013-12-13 20:43:54 -0800195 >>> from cryptography.hazmat.primitives.ciphers.modes import CBC
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800196 >>> iv = os.urandom(16)
197 >>> mode = CBC(iv)
198
199 While the following is bad and will leak information:
200
Alex Gaynor989061d2013-12-13 20:22:14 -0800201 .. doctest::
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800202
Alex Gaynord83c5902013-12-13 20:43:54 -0800203 >>> from cryptography.hazmat.primitives.ciphers.modes import CBC
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800204 >>> iv = "a" * 16
205 >>> mode = CBC(iv)
Paul Kehrer13f108f2013-09-09 21:41:03 -0500206
Paul Kehrer45064282013-10-17 13:41:53 -0500207
David Reid1f3d7182013-10-22 16:55:18 -0700208.. class:: CTR(nonce)
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500209
Paul Kehrer45064282013-10-17 13:41:53 -0500210 .. warning::
211
212 Counter mode is not recommended for use with block ciphers that have a
213 block size of less than 128-bits.
214
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500215 CTR (Counter) is a mode of operation for block ciphers. It is considered
Alex Gaynord1f02012013-11-01 14:12:35 -0700216 cryptographically strong. It transforms a block cipher into a stream
217 cipher.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500218
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600219 **This mode does not require padding.**
220
Paul Kehrer89b3dd32013-10-17 14:02:45 -0500221 :param bytes nonce: Should be random bytes. It is critical to never reuse a
Alex Gaynorfb843aa2014-02-25 11:37:36 -0800222 ``nonce`` with a given key. Any reuse of a nonce with the same key
223 compromises the security of every message encrypted with that key. Must
224 be the same number of bytes as the ``block_size`` of the cipher with a
225 given key. The nonce does not need to be kept secret and may be
226 included alongside the ciphertext.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500227
David Reid1f3d7182013-10-22 16:55:18 -0700228.. class:: OFB(initialization_vector)
Paul Kehrer6f412a02013-09-10 21:30:50 -0500229
230 OFB (Output Feedback) is a mode of operation for block ciphers. It
231 transforms a block cipher into a stream cipher.
232
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600233 **This mode does not require padding.**
234
David Reidf1a39bd2013-09-11 16:28:42 -0700235 :param bytes initialization_vector: Must be random bytes. They do not need
Alex Gaynorfb843aa2014-02-25 11:37:36 -0800236 to be kept secret (they can be included in a transmitted message). Must
237 be the same number of bytes as the ``block_size`` of the cipher. Do not
238 reuse an ``initialization_vector`` with a given ``key``.
Paul Kehrer6f412a02013-09-10 21:30:50 -0500239
David Reid1f3d7182013-10-22 16:55:18 -0700240.. class:: CFB(initialization_vector)
Paul Kehrer4223df72013-09-11 09:48:04 -0500241
242 CFB (Cipher Feedback) is a mode of operation for block ciphers. It
243 transforms a block cipher into a stream cipher.
244
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600245 **This mode does not require padding.**
246
Paul Kehrer4223df72013-09-11 09:48:04 -0500247 :param bytes initialization_vector: Must be random bytes. They do not need
Alex Gaynorfb843aa2014-02-25 11:37:36 -0800248 to be kept secret (they can be included in a transmitted message). Must
249 be the same number of bytes as the ``block_size`` of the cipher. Do not
250 reuse an ``initialization_vector`` with a given ``key``.
Paul Kehrer4223df72013-09-11 09:48:04 -0500251
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600252.. class:: GCM(initialization_vector, tag=None)
253
Paul Kehrer5b828b12013-11-29 17:32:08 -0600254 .. danger::
Paul Kehrer26c8c6a2013-11-29 16:24:56 -0600255
Alex Gaynord4f93832013-12-04 16:31:59 -0600256 When using this mode you MUST not use the decrypted data until
Alex Gaynor0d23e942013-12-04 17:28:24 -0600257 :meth:`~cryptography.hazmat.primitives.interfaces.CipherContext.finalize`
Alex Gaynord4f93832013-12-04 16:31:59 -0600258 has been called. GCM provides NO guarantees of ciphertext integrity
259 until decryption is complete.
Paul Kehrer26c8c6a2013-11-29 16:24:56 -0600260
Paul Kehrer5578c662013-12-03 17:37:42 -0600261 GCM (Galois Counter Mode) is a mode of operation for block ciphers. An
262 AEAD (authenticated encryption with additional data) mode is a type of
263 block cipher mode that encrypts the message as well as authenticating it
264 (and optionally additional data that is not encrypted) simultaneously.
265 Additional means of verifying integrity (like
Paul Kehrer2631c2b2013-11-24 10:20:50 -0600266 :doc:`HMAC </hazmat/primitives/hmac>`) are not necessary.
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600267
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600268 **This mode does not require padding.**
269
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600270 :param bytes initialization_vector: Must be random bytes. They do not need
Alex Gaynorfb843aa2014-02-25 11:37:36 -0800271 to be kept secret (they can be included in a transmitted message). NIST
272 `recommends 96-bit IV length`_ for performance critical situations, but
273 it can be up to 2\ :sup:`64` - 1 bits. Do not reuse an
274 ``initialization_vector`` with a given ``key``.
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600275
Paul Kehrerca735042013-12-21 17:31:48 -0600276 .. note::
277
Paul Kehrerfc73e2d2013-12-21 18:41:38 -0600278 Cryptography will emit a 128-bit tag when finalizing encryption.
279 You can shorten a tag by truncating it to the desired length, but this
280 is **not recommended** as it lowers the security margins of the
Paul Kehrer048d6cb2013-12-21 18:53:19 -0600281 authentication (`NIST SP-800-38D`_ recommends 96-bits or greater).
Paul Kehrerfc73e2d2013-12-21 18:41:38 -0600282 If you must shorten the tag the minimum allowed length is 4 bytes
Paul Kehrer048d6cb2013-12-21 18:53:19 -0600283 (32-bits). Applications **must** verify the tag is the expected length
284 to guarantee the expected security margin.
Paul Kehrerca735042013-12-21 17:31:48 -0600285
Alex Gaynorfb843aa2014-02-25 11:37:36 -0800286 :param bytes tag: The tag bytes to verify during decryption. When
287 encrypting this must be ``None``.
Paul Kehrer67abc862013-11-25 14:29:35 -0600288
David Reidabb72d22014-01-07 16:06:18 -0800289 .. testcode::
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600290
David Reidabb72d22014-01-07 16:06:18 -0800291 import os
292
293 from cryptography.hazmat.primitives.ciphers import (
294 Cipher, algorithms, modes
295 )
296
David Reid78569d62014-01-07 15:42:17 -0800297 def encrypt(key, plaintext, associated_data):
David Reidabb72d22014-01-07 16:06:18 -0800298 # Generate a random 96-bit IV.
David Reid78569d62014-01-07 15:42:17 -0800299 iv = os.urandom(12)
David Reidabb72d22014-01-07 16:06:18 -0800300
301 # Construct a AES-GCM Cipher object with the given and our randomly
302 # generated IV.
303 encryptor = Cipher(
David Reid78569d62014-01-07 15:42:17 -0800304 algorithms.AES(key),
305 modes.GCM(iv),
306 backend=default_backend()
David Reidabb72d22014-01-07 16:06:18 -0800307 ).encryptor()
David Reid78569d62014-01-07 15:42:17 -0800308
David Reidabb72d22014-01-07 16:06:18 -0800309 # associated_data will be authenticated but not encrypted,
310 # it must also be passed in on decryption.
David Reid78569d62014-01-07 15:42:17 -0800311 encryptor.authenticate_additional_data(associated_data)
David Reid78569d62014-01-07 15:42:17 -0800312
David Reidabb72d22014-01-07 16:06:18 -0800313 # Encrypt the plaintext and get the associated ciphertext.
Paul Kehreraf0b9f52014-01-07 19:21:49 -0600314 # GCM does not require padding.
315 ciphertext = encryptor.update(plaintext) + encryptor.finalize()
David Reidabb72d22014-01-07 16:06:18 -0800316
317 return (iv, ciphertext, encryptor.tag)
David Reid78569d62014-01-07 15:42:17 -0800318
319 def decrypt(key, associated_data, iv, ciphertext, tag):
Alex Gaynor007e5e12014-01-12 14:25:49 -0800320 if len(tag) != 16:
321 raise ValueError(
322 "tag must be 16 bytes -- truncation not supported"
323 )
324
David Reidabb72d22014-01-07 16:06:18 -0800325 # Construct a Cipher object, with the key, iv, and additionally the
326 # GCM tag used for authenticating the message.
327 decryptor = Cipher(
David Reid78569d62014-01-07 15:42:17 -0800328 algorithms.AES(key),
329 modes.GCM(iv, tag),
330 backend=default_backend()
David Reidabb72d22014-01-07 16:06:18 -0800331 ).decryptor()
David Reid78569d62014-01-07 15:42:17 -0800332
David Reidabb72d22014-01-07 16:06:18 -0800333 # We put associated_data back in or the tag will fail to verify
334 # when we finalize the decryptor.
David Reid78569d62014-01-07 15:42:17 -0800335 decryptor.authenticate_additional_data(associated_data)
336
Paul Kehreraf0b9f52014-01-07 19:21:49 -0600337 # Decryption gets us the authenticated plaintext.
338 # If the tag does not match an InvalidTag exception will be raised.
339 return decryptor.update(ciphertext) + decryptor.finalize()
David Reidabb72d22014-01-07 16:06:18 -0800340
341 iv, ciphertext, tag = encrypt(
David Reid78569d62014-01-07 15:42:17 -0800342 key,
David Reidabb72d22014-01-07 16:06:18 -0800343 b"a secret message!",
David Reid78569d62014-01-07 15:42:17 -0800344 b"authenticated but not encrypted payload"
345 )
346
David Reidabb72d22014-01-07 16:06:18 -0800347 print(decrypt(
348 key,
349 b"authenticated but not encrypted payload",
350 iv,
351 ciphertext,
352 tag
353 ))
David Reid78569d62014-01-07 15:42:17 -0800354
355 .. testoutput::
356
David Reidabb72d22014-01-07 16:06:18 -0800357 a secret message!
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600358
Paul Kehrer13f108f2013-09-09 21:41:03 -0500359
360Insecure Modes
361--------------
362
Alex Gaynorcd413a32013-09-10 18:59:43 -0700363.. warning::
364
365 These modes are insecure. New applications should never make use of them,
366 and existing applications should strongly consider migrating away.
367
368
David Reid1f3d7182013-10-22 16:55:18 -0700369.. class:: ECB()
Paul Kehrer13f108f2013-09-09 21:41:03 -0500370
371 ECB (Electronic Code Book) is the simplest mode of operation for block
Alex Gaynorcd413a32013-09-10 18:59:43 -0700372 ciphers. Each block of data is encrypted in the same way. This means
373 identical plaintext blocks will always result in identical ciphertext
374 blocks, and thus result in information leakage
Alex Gaynorab5f0112013-11-08 10:34:00 -0800375
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600376 **Padding is required when using this mode.**
377
Paul Kehrerad6d1642014-01-07 19:10:12 -0600378Interfaces
379----------
380
381.. class:: CipherContext
382
383 When calling ``encryptor()`` or ``decryptor()`` on a ``Cipher`` object
Alex Gaynorb4818892014-02-06 10:58:50 -0800384 the result will conform to the ``CipherContext`` interface. You can then
385 call ``update(data)`` with data until you have fed everything into the
386 context. Once that is done call ``finalize()`` to finish the operation and
387 obtain the remainder of the data.
Paul Kehrerad6d1642014-01-07 19:10:12 -0600388
389 Block ciphers require that plaintext or ciphertext always be a multiple of
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600390 their block size, because of that **padding** is sometimes required to make
391 a message the correct size. ``CipherContext`` will not automatically apply
Paul Kehrerad6d1642014-01-07 19:10:12 -0600392 any padding; you'll need to add your own. For block ciphers the recommended
393 padding is :class:`cryptography.hazmat.primitives.padding.PKCS7`. If you
394 are using a stream cipher mode (such as
395 :class:`cryptography.hazmat.primitives.modes.CTR`) you don't have to worry
396 about this.
397
398 .. method:: update(data)
399
400 :param bytes data: The data you wish to pass into the context.
401 :return bytes: Returns the data that was encrypted or decrypted.
402 :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize`
403
404 When the ``Cipher`` was constructed in a mode that turns it into a
405 stream cipher (e.g.
406 :class:`cryptography.hazmat.primitives.ciphers.modes.CTR`), this will
407 return bytes immediately, however in other modes it will return chunks,
408 whose size is determined by the cipher's block size.
409
410 .. method:: finalize()
411
412 :return bytes: Returns the remainder of the data.
413 :raises ValueError: This is raised when the data provided isn't
Alex Gaynorfb843aa2014-02-25 11:37:36 -0800414 correctly padded to be a multiple of the algorithm's block size.
Paul Kehrerad6d1642014-01-07 19:10:12 -0600415
416 Once ``finalize`` is called this object can no longer be used and
417 :meth:`update` and :meth:`finalize` will raise
418 :class:`~cryptography.exceptions.AlreadyFinalized`.
419
420.. class:: AEADCipherContext
421
422 When calling ``encryptor()`` or ``decryptor()`` on a ``Cipher`` object
423 with an AEAD mode (e.g.
Alex Gaynorb4818892014-02-06 10:58:50 -0800424 :class:`~cryptography.hazmat.primitives.ciphers.modes.GCM`) the result will
425 conform to the ``AEADCipherContext`` and ``CipherContext`` interfaces. If
426 it is an encryption context it will additionally be an
427 ``AEADEncryptionContext`` interface. ``AEADCipherContext`` contains an
428 additional method ``authenticate_additional_data`` for adding additional
429 authenticated but unencrypted data (see note below). You should call this
430 before calls to ``update``. When you are done call ``finalize()`` to finish
431 the operation.
Paul Kehrerad6d1642014-01-07 19:10:12 -0600432
433 .. note::
434
435 In AEAD modes all data passed to ``update()`` will be both encrypted
436 and authenticated. Do not pass encrypted data to the
437 ``authenticate_additional_data()`` method. It is meant solely for
438 additional data you may want to authenticate but leave unencrypted.
439
440 .. method:: authenticate_additional_data(data)
441
442 :param bytes data: Any data you wish to authenticate but not encrypt.
443 :raises: :class:`~cryptography.exceptions.AlreadyFinalized`
444
445.. class:: AEADEncryptionContext
446
447 When creating an encryption context using ``encryptor()`` on a ``Cipher``
448 object with an AEAD mode (e.g.
449 :class:`~cryptography.hazmat.primitives.ciphers.modes.GCM`) you will receive
450 a return object conforming to the ``AEADEncryptionContext`` interface (as
451 well as ``AEADCipherContext``). This interface provides one additional
452 attribute ``tag``. ``tag`` can only be obtained after ``finalize()``.
453
454 .. attribute:: tag
455
456 :return bytes: Returns the tag value as bytes.
457 :raises: :class:`~cryptography.exceptions.NotYetFinalized` if called
Alex Gaynorfb843aa2014-02-25 11:37:36 -0800458 before the context is finalized.
Paul Kehrerad6d1642014-01-07 19:10:12 -0600459
Alex Gaynorab5f0112013-11-08 10:34:00 -0800460
461.. _`described by Colin Percival`: http://www.daemonology.net/blog/2009-06-11-cryptographic-right-answers.html
Paul Kehrer67284372013-12-03 18:58:14 -0600462.. _`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 -0600463.. _`NIST SP-800-38D`: http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf
Paul Kehrerbab0e1a2014-02-09 10:51:59 -0600464.. _`Communications Security Establishment`: http://www.cse-cst.gc.ca