blob: 2ee5085b0233898e6d10a8f35432b883d26791a2 [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 Stapleton092351d2014-03-09 17:44:43 +000016Symmetric encryption is a way to `encrypt`_ or hide the contents of material
17where the sender and receiver both use the same secret key. Note that symmetric
18encryption is **not** sufficient for most applications because it only
19provides secrecy but not authenticity. That means an attacker can't see the
20message but an attacker can create bogus messages and force the application to
21decrypt them.
22
Alex Gaynor9316f4c2013-11-15 16:38:42 -080023For this reason it is *strongly* recommended to combine encryption with a
Alex Gaynorab5f0112013-11-08 10:34:00 -080024message authentication code, such as :doc:`HMAC </hazmat/primitives/hmac>`, in
25an "encrypt-then-MAC" formulation as `described by Colin Percival`_.
Alex Gaynorf6c47e92013-08-08 07:16:01 -070026
David Reidef0fcf22013-11-06 11:12:45 -080027.. class:: Cipher(algorithm, mode, backend)
Alex Gaynorf6c47e92013-08-08 07:16:01 -070028
Alex Stapleton092351d2014-03-09 17:44:43 +000029 Cipher objects combine an algorithm such as
30 :class:`~cryptography.hazmat.primitives.ciphers.algorithms.AES` with a
31 mode like
Alex Gaynorab5f0112013-11-08 10:34:00 -080032 :class:`~cryptography.hazmat.primitives.ciphers.modes.CBC` or
Alex Stapleton092351d2014-03-09 17:44:43 +000033 :class:`~cryptography.hazmat.primitives.ciphers.modes.CTR`. A simple
34 example of encrypting and then decrypting content with AES is:
Alex Gaynorf6c47e92013-08-08 07:16:01 -070035
Donald Stufft173de982013-08-12 07:34:39 -040036 .. doctest::
Alex Gaynorf6c47e92013-08-08 07:16:01 -070037
Paul Kehrer051099e2013-11-06 15:53:40 +080038 >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
Alex Gaynorf8796b12013-12-13 20:28:55 -080039 >>> from cryptography.hazmat.backends import default_backend
Alex Gaynorf56444d2013-12-13 15:19:22 -080040 >>> backend = default_backend()
David Reid63fa19a2013-11-20 10:49:13 -080041 >>> cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
Paul Kehrer3e0895c2013-10-21 22:19:29 -050042 >>> encryptor = cipher.encryptor()
43 >>> ct = encryptor.update(b"a secret message") + encryptor.finalize()
44 >>> decryptor = cipher.decryptor()
45 >>> decryptor.update(ct) + decryptor.finalize()
Paul Kehrerf6cf9562013-10-22 10:36:00 -050046 'a secret message'
Alex Gaynorf6c47e92013-08-08 07:16:01 -070047
David Reid663295d2013-11-20 13:55:08 -080048 :param algorithms: A
49 :class:`~cryptography.hazmat.primitives.interfaces.CipherAlgorithm`
50 provider such as those described
51 :ref:`below <symmetric-encryption-algorithms>`.
52 :param mode: A :class:`~cryptography.hazmat.primitives.interfaces.Mode`
53 provider such as those described
54 :ref:`below <symmetric-encryption-modes>`.
55 :param backend: A
Alex Gaynorf8796b12013-12-13 20:28:55 -080056 :class:`~cryptography.hazmat.backends.interfaces.CipherBackend`
David Reid663295d2013-11-20 13:55:08 -080057 provider.
Alex Gaynor0ca7fdb2013-08-08 07:35:26 -070058
Paul Kehrer5399fd02013-10-21 23:48:25 -050059 .. method:: encryptor()
Alex Gaynor09515f02013-08-08 15:26:55 -070060
David Reid63ba6652013-10-22 14:09:19 -070061 :return: An encrypting
Donald Stufftf04317a2013-10-27 16:44:30 -040062 :class:`~cryptography.hazmat.primitives.interfaces.CipherContext`
David Reid63ba6652013-10-22 14:09:19 -070063 provider.
Alex Gaynore62aa402013-08-08 15:23:11 -070064
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070065 If the backend doesn't support the requested combination of ``cipher``
Alex Gaynorc6a6f312014-03-06 14:22:56 -080066 and ``mode`` an :class:`~cryptography.exceptions.UnsupportedCipher`
Alex Stapleton092351d2014-03-09 17:44:43 +000067 exception will be raised.
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070068
Paul Kehrer5399fd02013-10-21 23:48:25 -050069 .. method:: decryptor()
70
David Reid63ba6652013-10-22 14:09:19 -070071 :return: A decrypting
Donald Stufftf04317a2013-10-27 16:44:30 -040072 :class:`~cryptography.hazmat.primitives.interfaces.CipherContext`
David Reid63ba6652013-10-22 14:09:19 -070073 provider.
Paul Kehrer5399fd02013-10-21 23:48:25 -050074
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070075 If the backend doesn't support the requested combination of ``cipher``
Alex Gaynorc6a6f312014-03-06 14:22:56 -080076 and ``mode`` an :class:`cryptography.exceptions.UnsupportedCipher`
Alex Stapleton092351d2014-03-09 17:44:43 +000077 exception will be raised.
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070078
David Reid663295d2013-11-20 13:55:08 -080079.. _symmetric-encryption-algorithms:
80
Paul Kehrer051099e2013-11-06 15:53:40 +080081Algorithms
82~~~~~~~~~~
Alex Gaynord96d1002013-08-08 07:37:26 -070083
Paul Kehrer051099e2013-11-06 15:53:40 +080084.. currentmodule:: cryptography.hazmat.primitives.ciphers.algorithms
David Reid1f3d7182013-10-22 16:55:18 -070085
86.. class:: AES(key)
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070087
Alex Gaynor1e3f81f2013-08-08 11:31:43 -070088 AES (Advanced Encryption Standard) is a block cipher standardized by NIST.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070089 AES is both fast, and cryptographically strong. It is a good default
90 choice for encryption.
91
Alex Stapleton092351d2014-03-09 17:44:43 +000092 :param bytes key: The secret key. This must be kept secret. Either ``128``,
93 ``192``, or ``256`` bits long.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070094
David Reid1f3d7182013-10-22 16:55:18 -070095.. class:: Camellia(key)
Paul Kehrerdff22d42013-09-27 13:43:06 -050096
Alex Stapleton092351d2014-03-09 17:44:43 +000097 Camellia is a block cipher approved for use by `CRYPTREC`_ and ISO/IEC.
98 It is considered to have comparable security and performance to AES but
Paul Kehrerdff22d42013-09-27 13:43:06 -050099 is not as widely studied or deployed.
100
Alex Stapleton092351d2014-03-09 17:44:43 +0000101 :param bytes key: The secret key. This must be kept secret. Either ``128``,
102 ``192``, or ``256`` bits long.
Paul Kehrerdff22d42013-09-27 13:43:06 -0500103
David Reid1f3d7182013-10-22 16:55:18 -0700104.. class:: TripleDES(key)
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700105
Alex Gaynor9316f4c2013-11-15 16:38:42 -0800106 Triple DES (Data Encryption Standard), sometimes referred to as 3DES, is a
107 block cipher standardized by NIST. Triple DES has known crypto-analytic
Alex Gaynor17adce62013-10-16 17:04:40 -0700108 flaws, however none of them currently enable a practical attack.
Alex Gaynor9316f4c2013-11-15 16:38:42 -0800109 Nonetheless, Triples DES is not recommended for new applications because it
Alex Gaynorfbcc5642013-10-22 08:26:00 -0700110 is incredibly slow; old applications should consider moving away from it.
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700111
Alex Stapleton092351d2014-03-09 17:44:43 +0000112 :param bytes key: The secret key. This must be kept secret. Either ``64``,
113 ``128``, or ``192`` bits long. DES only uses ``56``, ``112``, or ``168``
114 bits of the key as there is a parity byte in each component of the key.
115 Some writing refers to there being up to three separate keys that are each
116 ``56`` bits long, they can simply be concatenated to produce the full key.
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700117
Paul Kehrerbab0e1a2014-02-09 10:51:59 -0600118.. class:: CAST5(key)
119
Paul Kehrera5011ec2014-02-13 12:33:34 -0600120 .. versionadded:: 0.2
121
Paul Kehrerbab0e1a2014-02-09 10:51:59 -0600122 CAST5 (also known as CAST-128) is a block cipher approved for use in the
123 Canadian government by the `Communications Security Establishment`_. It is
124 a variable key length cipher and supports keys from 40-128 bits in length.
125
Alex Stapleton092351d2014-03-09 17:44:43 +0000126 :param bytes key: The secret key, This must be kept secret. 40 to 128 bits
127 in length in increments of 8 bits.
Paul Kehrerbab0e1a2014-02-09 10:51:59 -0600128
Paul Kehrer3446d812013-10-31 17:15:03 -0500129Weak Ciphers
130------------
131
132.. warning::
133
134 These ciphers are considered weak for a variety of reasons. New
135 applications should avoid their use and existing applications should
136 strongly consider migrating away.
137
Paul Kehrer5df0abe2013-10-30 16:57:04 -0500138.. class:: Blowfish(key)
139
140 Blowfish is a block cipher developed by Bruce Schneier. It is known to be
141 susceptible to attacks when using weak keys. The author has recommended
Alex Stapleton092351d2014-03-09 17:44:43 +0000142 that users of Blowfish move to newer algorithms such as :class:`AES`.
Paul Kehrer5df0abe2013-10-30 16:57:04 -0500143
Alex Stapleton092351d2014-03-09 17:44:43 +0000144 :param bytes key: The secret key. This must be kept secret. 32 to 448 bits
145 in length in increments of 8 bits.
Paul Kehrer5df0abe2013-10-30 16:57:04 -0500146
Paul Kehrer4da28c32013-11-07 07:50:17 +0800147.. class:: ARC4(key)
148
149 ARC4 (Alleged RC4) is a stream cipher with serious weaknesses in its
150 initial stream output. Its use is strongly discouraged. ARC4 does not use
151 mode constructions.
152
Alex Stapleton092351d2014-03-09 17:44:43 +0000153 :param bytes key: The secret key. This must be kept secret. Either ``40``,
154 ``56``, ``64``, ``80``, ``128``, ``192``, or ``256`` bits in length.
Paul Kehrer4da28c32013-11-07 07:50:17 +0800155
Paul Kehrer0994c562013-11-10 03:19:14 +0800156 .. doctest::
157
158 >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
Alex Gaynorf8796b12013-12-13 20:28:55 -0800159 >>> from cryptography.hazmat.backends import default_backend
Paul Kehrer0994c562013-11-10 03:19:14 +0800160 >>> algorithm = algorithms.ARC4(key)
Alex Gaynorf56444d2013-12-13 15:19:22 -0800161 >>> cipher = Cipher(algorithm, mode=None, backend=default_backend())
Paul Kehrer0994c562013-11-10 03:19:14 +0800162 >>> encryptor = cipher.encryptor()
163 >>> ct = encryptor.update(b"a secret message")
164 >>> decryptor = cipher.decryptor()
165 >>> decryptor.update(ct)
166 'a secret message'
167
David Reid30722b92013-11-07 13:03:39 -0800168
169.. _symmetric-encryption-modes:
170
Alex Gaynord96d1002013-08-08 07:37:26 -0700171Modes
172~~~~~
173
Paul Kehrer051099e2013-11-06 15:53:40 +0800174.. currentmodule:: cryptography.hazmat.primitives.ciphers.modes
David Reid1f3d7182013-10-22 16:55:18 -0700175
176.. class:: CBC(initialization_vector)
Alex Gaynor48ec9a32013-08-08 11:13:46 -0700177
Alex Stapleton092351d2014-03-09 17:44:43 +0000178 CBC (Cipher Block Chaining) is a mode of operation for block ciphers. It is
Alex Gaynor48ec9a32013-08-08 11:13:46 -0700179 considered cryptographically strong.
180
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600181 **Padding is required when using this mode.**
182
Alex Gaynor48ec9a32013-08-08 11:13:46 -0700183 :param bytes initialization_vector: Must be random bytes. They do not need
Alex Stapleton092351d2014-03-09 17:44:43 +0000184 to be kept secret and they can be included in a transmitted message.
185 Must be the same number of bytes as the ``block_size`` of the cipher.
186 Each time something is encrypted a new ``initialization_vector`` should
187 be generated. Do not reuse an ``initialization_vector`` with a given
Alex Gaynorfb843aa2014-02-25 11:37:36 -0800188 ``key``, and particularly do not use a constant
189 ``initialization_vector``.
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800190
191 A good construction looks like:
192
Alex Gaynor989061d2013-12-13 20:22:14 -0800193 .. doctest::
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800194
195 >>> import os
Alex Gaynord83c5902013-12-13 20:43:54 -0800196 >>> from cryptography.hazmat.primitives.ciphers.modes import CBC
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800197 >>> iv = os.urandom(16)
198 >>> mode = CBC(iv)
199
200 While the following is bad and will leak information:
201
Alex Gaynor989061d2013-12-13 20:22:14 -0800202 .. doctest::
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800203
Alex Gaynord83c5902013-12-13 20:43:54 -0800204 >>> from cryptography.hazmat.primitives.ciphers.modes import CBC
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800205 >>> iv = "a" * 16
206 >>> mode = CBC(iv)
Paul Kehrer13f108f2013-09-09 21:41:03 -0500207
Paul Kehrer45064282013-10-17 13:41:53 -0500208
David Reid1f3d7182013-10-22 16:55:18 -0700209.. class:: CTR(nonce)
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500210
Paul Kehrer45064282013-10-17 13:41:53 -0500211 .. warning::
212
213 Counter mode is not recommended for use with block ciphers that have a
214 block size of less than 128-bits.
215
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500216 CTR (Counter) is a mode of operation for block ciphers. It is considered
Alex Gaynord1f02012013-11-01 14:12:35 -0700217 cryptographically strong. It transforms a block cipher into a stream
218 cipher.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500219
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600220 **This mode does not require padding.**
221
Paul Kehrer89b3dd32013-10-17 14:02:45 -0500222 :param bytes nonce: Should be random bytes. It is critical to never reuse a
Alex Gaynorfb843aa2014-02-25 11:37:36 -0800223 ``nonce`` with a given key. Any reuse of a nonce with the same key
224 compromises the security of every message encrypted with that key. Must
225 be the same number of bytes as the ``block_size`` of the cipher with a
226 given key. The nonce does not need to be kept secret and may be
Alex Stapleton092351d2014-03-09 17:44:43 +0000227 included with the ciphertext.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500228
David Reid1f3d7182013-10-22 16:55:18 -0700229.. class:: OFB(initialization_vector)
Paul Kehrer6f412a02013-09-10 21:30:50 -0500230
231 OFB (Output Feedback) is a mode of operation for block ciphers. It
232 transforms a block cipher into a stream cipher.
233
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600234 **This mode does not require padding.**
235
David Reidf1a39bd2013-09-11 16:28:42 -0700236 :param bytes initialization_vector: Must be random bytes. They do not need
Alex Stapleton092351d2014-03-09 17:44:43 +0000237 to be kept secret and they can be included in a transmitted message.
238 Must be the same number of bytes as the ``block_size`` of the cipher.
239 Do not reuse an ``initialization_vector`` with a given ``key``.
Paul Kehrer6f412a02013-09-10 21:30:50 -0500240
David Reid1f3d7182013-10-22 16:55:18 -0700241.. class:: CFB(initialization_vector)
Paul Kehrer4223df72013-09-11 09:48:04 -0500242
243 CFB (Cipher Feedback) is a mode of operation for block ciphers. It
244 transforms a block cipher into a stream cipher.
245
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600246 **This mode does not require padding.**
247
Paul Kehrer4223df72013-09-11 09:48:04 -0500248 :param bytes initialization_vector: Must be random bytes. They do not need
Alex Stapleton092351d2014-03-09 17:44:43 +0000249 to be kept secret and they can be included in a transmitted message.
250 Must be the same number of bytes as the ``block_size`` of the cipher.
251 Do not reuse an ``initialization_vector`` with a given ``key``.
Paul Kehrer4223df72013-09-11 09:48:04 -0500252
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600253.. class:: GCM(initialization_vector, tag=None)
254
Paul Kehrer5b828b12013-11-29 17:32:08 -0600255 .. danger::
Paul Kehrer26c8c6a2013-11-29 16:24:56 -0600256
Alex Stapleton092351d2014-03-09 17:44:43 +0000257 When using this mode you **must** not use the decrypted data until
Alex Gaynor0d23e942013-12-04 17:28:24 -0600258 :meth:`~cryptography.hazmat.primitives.interfaces.CipherContext.finalize`
Alex Stapleton092351d2014-03-09 17:44:43 +0000259 has been called. GCM provides **no** guarantees of ciphertext integrity
Alex Gaynord4f93832013-12-04 16:31:59 -0600260 until decryption is complete.
Paul Kehrer26c8c6a2013-11-29 16:24:56 -0600261
Paul Kehrer5578c662013-12-03 17:37:42 -0600262 GCM (Galois Counter Mode) is a mode of operation for block ciphers. An
263 AEAD (authenticated encryption with additional data) mode is a type of
Alex Stapleton092351d2014-03-09 17:44:43 +0000264 block cipher mode that simultaneously encrypts the message as well as
265 authenticating it. Additional unencrypted data may also be authenticated.
266 Additional means of verifying integrity such as
267 :doc:`HMAC </hazmat/primitives/hmac>` are not necessary.
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600268
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600269 **This mode does not require padding.**
270
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600271 :param bytes initialization_vector: Must be random bytes. They do not need
Alex Stapleton092351d2014-03-09 17:44:43 +0000272 to be kept secret and they can be included in a transmitted message.
273 NIST `recommends a 96-bit IV length`_ for performance critical
274 situations but it can be up to 2\ :sup:`64` - 1 bits. Do not reuse an
Alex Gaynorfb843aa2014-02-25 11:37:36 -0800275 ``initialization_vector`` with a given ``key``.
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600276
Paul Kehrerca735042013-12-21 17:31:48 -0600277 .. note::
278
Alex Stapleton092351d2014-03-09 17:44:43 +0000279 Cryptography will generate a 128-bit tag when finalizing encryption.
280 You can shorten a tag by truncating it to the desired length but this
Paul Kehrerfc73e2d2013-12-21 18:41:38 -0600281 is **not recommended** as it lowers the security margins of the
Paul Kehrer048d6cb2013-12-21 18:53:19 -0600282 authentication (`NIST SP-800-38D`_ recommends 96-bits or greater).
Paul Kehrerfc73e2d2013-12-21 18:41:38 -0600283 If you must shorten the tag the minimum allowed length is 4 bytes
Paul Kehrer048d6cb2013-12-21 18:53:19 -0600284 (32-bits). Applications **must** verify the tag is the expected length
285 to guarantee the expected security margin.
Paul Kehrerca735042013-12-21 17:31:48 -0600286
Alex Gaynorfb843aa2014-02-25 11:37:36 -0800287 :param bytes tag: The tag bytes to verify during decryption. When
288 encrypting this must be ``None``.
Paul Kehrer67abc862013-11-25 14:29:35 -0600289
David Reidabb72d22014-01-07 16:06:18 -0800290 .. testcode::
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600291
David Reidabb72d22014-01-07 16:06:18 -0800292 import os
293
294 from cryptography.hazmat.primitives.ciphers import (
295 Cipher, algorithms, modes
296 )
297
David Reid78569d62014-01-07 15:42:17 -0800298 def encrypt(key, plaintext, associated_data):
David Reidabb72d22014-01-07 16:06:18 -0800299 # Generate a random 96-bit IV.
David Reid78569d62014-01-07 15:42:17 -0800300 iv = os.urandom(12)
David Reidabb72d22014-01-07 16:06:18 -0800301
Alex Stapleton092351d2014-03-09 17:44:43 +0000302 # Construct a AES-GCM Cipher object with the given key and a
303 # randomly generated IV.
David Reidabb72d22014-01-07 16:06:18 -0800304 encryptor = Cipher(
David Reid78569d62014-01-07 15:42:17 -0800305 algorithms.AES(key),
306 modes.GCM(iv),
307 backend=default_backend()
David Reidabb72d22014-01-07 16:06:18 -0800308 ).encryptor()
David Reid78569d62014-01-07 15:42:17 -0800309
David Reidabb72d22014-01-07 16:06:18 -0800310 # associated_data will be authenticated but not encrypted,
311 # it must also be passed in on decryption.
David Reid78569d62014-01-07 15:42:17 -0800312 encryptor.authenticate_additional_data(associated_data)
David Reid78569d62014-01-07 15:42:17 -0800313
David Reidabb72d22014-01-07 16:06:18 -0800314 # Encrypt the plaintext and get the associated ciphertext.
Paul Kehreraf0b9f52014-01-07 19:21:49 -0600315 # GCM does not require padding.
316 ciphertext = encryptor.update(plaintext) + encryptor.finalize()
David Reidabb72d22014-01-07 16:06:18 -0800317
318 return (iv, ciphertext, encryptor.tag)
David Reid78569d62014-01-07 15:42:17 -0800319
320 def decrypt(key, associated_data, iv, ciphertext, tag):
Alex Gaynor007e5e12014-01-12 14:25:49 -0800321 if len(tag) != 16:
322 raise ValueError(
323 "tag must be 16 bytes -- truncation not supported"
324 )
325
David Reidabb72d22014-01-07 16:06:18 -0800326 # Construct a Cipher object, with the key, iv, and additionally the
327 # GCM tag used for authenticating the message.
328 decryptor = Cipher(
David Reid78569d62014-01-07 15:42:17 -0800329 algorithms.AES(key),
330 modes.GCM(iv, tag),
331 backend=default_backend()
David Reidabb72d22014-01-07 16:06:18 -0800332 ).decryptor()
David Reid78569d62014-01-07 15:42:17 -0800333
David Reidabb72d22014-01-07 16:06:18 -0800334 # We put associated_data back in or the tag will fail to verify
335 # when we finalize the decryptor.
David Reid78569d62014-01-07 15:42:17 -0800336 decryptor.authenticate_additional_data(associated_data)
337
Paul Kehreraf0b9f52014-01-07 19:21:49 -0600338 # Decryption gets us the authenticated plaintext.
339 # If the tag does not match an InvalidTag exception will be raised.
340 return decryptor.update(ciphertext) + decryptor.finalize()
David Reidabb72d22014-01-07 16:06:18 -0800341
342 iv, ciphertext, tag = encrypt(
David Reid78569d62014-01-07 15:42:17 -0800343 key,
David Reidabb72d22014-01-07 16:06:18 -0800344 b"a secret message!",
David Reid78569d62014-01-07 15:42:17 -0800345 b"authenticated but not encrypted payload"
346 )
347
David Reidabb72d22014-01-07 16:06:18 -0800348 print(decrypt(
349 key,
350 b"authenticated but not encrypted payload",
351 iv,
352 ciphertext,
353 tag
354 ))
David Reid78569d62014-01-07 15:42:17 -0800355
356 .. testoutput::
357
David Reidabb72d22014-01-07 16:06:18 -0800358 a secret message!
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600359
Paul Kehrer13f108f2013-09-09 21:41:03 -0500360
361Insecure Modes
362--------------
363
Alex Gaynorcd413a32013-09-10 18:59:43 -0700364.. warning::
365
366 These modes are insecure. New applications should never make use of them,
367 and existing applications should strongly consider migrating away.
368
369
David Reid1f3d7182013-10-22 16:55:18 -0700370.. class:: ECB()
Paul Kehrer13f108f2013-09-09 21:41:03 -0500371
372 ECB (Electronic Code Book) is the simplest mode of operation for block
Alex Gaynorcd413a32013-09-10 18:59:43 -0700373 ciphers. Each block of data is encrypted in the same way. This means
374 identical plaintext blocks will always result in identical ciphertext
Alex Stapleton092351d2014-03-09 17:44:43 +0000375 blocks, which can leave `significant patterns in the output`_.
Alex Gaynorab5f0112013-11-08 10:34:00 -0800376
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600377 **Padding is required when using this mode.**
378
Paul Kehrerad6d1642014-01-07 19:10:12 -0600379Interfaces
380----------
381
382.. class:: CipherContext
383
384 When calling ``encryptor()`` or ``decryptor()`` on a ``Cipher`` object
Alex Gaynorb4818892014-02-06 10:58:50 -0800385 the result will conform to the ``CipherContext`` interface. You can then
386 call ``update(data)`` with data until you have fed everything into the
387 context. Once that is done call ``finalize()`` to finish the operation and
388 obtain the remainder of the data.
Paul Kehrerad6d1642014-01-07 19:10:12 -0600389
Alex Stapleton092351d2014-03-09 17:44:43 +0000390 Block ciphers require that the plaintext or ciphertext always be a multiple
391 of their block size. Because of that **padding** is sometimes required to
392 make a message the correct size. ``CipherContext`` will not automatically
393 apply any padding; you'll need to add your own. For block ciphers the
394 recommended padding is
395 :class:`cryptography.hazmat.primitives.padding.PKCS7`. If you are using a
396 stream cipher mode (such as
Paul Kehrerad6d1642014-01-07 19:10:12 -0600397 :class:`cryptography.hazmat.primitives.modes.CTR`) you don't have to worry
398 about this.
399
400 .. method:: update(data)
401
402 :param bytes data: The data you wish to pass into the context.
403 :return bytes: Returns the data that was encrypted or decrypted.
404 :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize`
405
406 When the ``Cipher`` was constructed in a mode that turns it into a
407 stream cipher (e.g.
408 :class:`cryptography.hazmat.primitives.ciphers.modes.CTR`), this will
Alex Stapleton092351d2014-03-09 17:44:43 +0000409 return bytes immediately, however in other modes it will return chunks
Paul Kehrerad6d1642014-01-07 19:10:12 -0600410 whose size is determined by the cipher's block size.
411
412 .. method:: finalize()
413
414 :return bytes: Returns the remainder of the data.
415 :raises ValueError: This is raised when the data provided isn't
Alex Stapleton092351d2014-03-09 17:44:43 +0000416 a multiple of the algorithm's block size.
Paul Kehrerad6d1642014-01-07 19:10:12 -0600417
418 Once ``finalize`` is called this object can no longer be used and
Alex Stapleton092351d2014-03-09 17:44:43 +0000419 :meth:`update` and :meth:`finalize` will raise an
420 :class:`~cryptography.exceptions.AlreadyFinalized` exception.
Paul Kehrerad6d1642014-01-07 19:10:12 -0600421
422.. class:: AEADCipherContext
423
Alex Stapleton092351d2014-03-09 17:44:43 +0000424 When calling ``encryptor`` or ``decryptor`` on a ``Cipher`` object
Paul Kehrerad6d1642014-01-07 19:10:12 -0600425 with an AEAD mode (e.g.
Alex Gaynorb4818892014-02-06 10:58:50 -0800426 :class:`~cryptography.hazmat.primitives.ciphers.modes.GCM`) the result will
427 conform to the ``AEADCipherContext`` and ``CipherContext`` interfaces. If
428 it is an encryption context it will additionally be an
Alex Stapleton092351d2014-03-09 17:44:43 +0000429 ``AEADEncryptionContext`` provider. ``AEADCipherContext`` contains an
430 additional method :meth:`authenticate_additional_data` for adding
431 additional authenticated but unencrypted data (see note below). You should
432 call this before calls to ``update``. When you are done call `finalize``
433 to finish the operation.
Paul Kehrerad6d1642014-01-07 19:10:12 -0600434
435 .. note::
436
437 In AEAD modes all data passed to ``update()`` will be both encrypted
438 and authenticated. Do not pass encrypted data to the
439 ``authenticate_additional_data()`` method. It is meant solely for
440 additional data you may want to authenticate but leave unencrypted.
441
442 .. method:: authenticate_additional_data(data)
443
444 :param bytes data: Any data you wish to authenticate but not encrypt.
445 :raises: :class:`~cryptography.exceptions.AlreadyFinalized`
446
447.. class:: AEADEncryptionContext
448
Alex Stapleton092351d2014-03-09 17:44:43 +0000449 When creating an encryption context using ``encryptor`` on a ``Cipher``
450 object with an AEAD mode such as
451 :class:`~cryptography.hazmat.primitives.ciphers.modes.GCM` an object
452 conforming to both the ``AEADEncryptionContext`` and ``AEADCipherContext``
453 interfaces will be returned. This interface provides one
454 additional attribute ``tag``. ``tag`` can only be obtained after
455 ``finalize`` has been called.
Paul Kehrerad6d1642014-01-07 19:10:12 -0600456
457 .. attribute:: tag
458
459 :return bytes: Returns the tag value as bytes.
460 :raises: :class:`~cryptography.exceptions.NotYetFinalized` if called
Alex Gaynorfb843aa2014-02-25 11:37:36 -0800461 before the context is finalized.
Paul Kehrerad6d1642014-01-07 19:10:12 -0600462
Alex Gaynorab5f0112013-11-08 10:34:00 -0800463
464.. _`described by Colin Percival`: http://www.daemonology.net/blog/2009-06-11-cryptographic-right-answers.html
Alex Stapleton092351d2014-03-09 17:44:43 +0000465.. _`recommends a 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 -0600466.. _`NIST SP-800-38D`: http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf
Paul Kehrerbab0e1a2014-02-09 10:51:59 -0600467.. _`Communications Security Establishment`: http://www.cse-cst.gc.ca
Alex Stapleton092351d2014-03-09 17:44:43 +0000468.. _`encrypt`: https://ssd.eff.org/tech/encryption
469.. _`CRYPTREC`: http://www.cryptrec.go.jp/english/
470.. _`significant patterns in the output`: http://en.wikipedia.org/wiki/Cipher_block_chaining#Electronic_codebook_.28ECB.29