blob: 7d954046ae32b4009ae4dc6dccfd80183536bf95 [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 Gaynor48ec9a32013-08-08 11:13:46 -070091 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.
100 This must be kept secret.
101
Alex Gaynord96d1002013-08-08 07:37:26 -0700102
David Reid1f3d7182013-10-22 16:55:18 -0700103.. class:: TripleDES(key)
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700104
Alex Gaynor9316f4c2013-11-15 16:38:42 -0800105 Triple DES (Data Encryption Standard), sometimes referred to as 3DES, is a
106 block cipher standardized by NIST. Triple DES has known crypto-analytic
Alex Gaynor17adce62013-10-16 17:04:40 -0700107 flaws, however none of them currently enable a practical attack.
Alex Gaynor9316f4c2013-11-15 16:38:42 -0800108 Nonetheless, Triples DES is not recommended for new applications because it
Alex Gaynorfbcc5642013-10-22 08:26:00 -0700109 is incredibly slow; old applications should consider moving away from it.
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700110
111 :param bytes key: The secret key, either ``64``, ``128``, or ``192`` bits
112 (note that DES functionally uses ``56``, ``112``, or
113 ``168`` bits of the key, there is a parity byte in each
114 component of the key), in some materials these are
115 referred to as being up to three separate keys (each
116 ``56`` bits long), they can simply be concatenated to
117 produce the full key. This must be kept secret.
118
Paul Kehrer3446d812013-10-31 17:15:03 -0500119Weak Ciphers
120------------
121
122.. warning::
123
124 These ciphers are considered weak for a variety of reasons. New
125 applications should avoid their use and existing applications should
126 strongly consider migrating away.
127
Paul Kehrer5df0abe2013-10-30 16:57:04 -0500128.. class:: Blowfish(key)
129
130 Blowfish is a block cipher developed by Bruce Schneier. It is known to be
131 susceptible to attacks when using weak keys. The author has recommended
Alex Gaynorab5f0112013-11-08 10:34:00 -0800132 that users of Blowfish move to newer algorithms, such as :class:`AES`.
Paul Kehrer5df0abe2013-10-30 16:57:04 -0500133
134 :param bytes key: The secret key, 32-448 bits in length (in increments of
135 8). This must be kept secret.
136
Paul Kehrer4da28c32013-11-07 07:50:17 +0800137.. class:: ARC4(key)
138
139 ARC4 (Alleged RC4) is a stream cipher with serious weaknesses in its
140 initial stream output. Its use is strongly discouraged. ARC4 does not use
141 mode constructions.
142
143 :param bytes key: The secret key, ``40``, ``56``, ``64``, ``80``, ``128``,
144 ``192``, or ``256`` bits in length. This must be kept
145 secret.
146
Paul Kehrer0994c562013-11-10 03:19:14 +0800147 .. doctest::
148
149 >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
Alex Gaynorf8796b12013-12-13 20:28:55 -0800150 >>> from cryptography.hazmat.backends import default_backend
Paul Kehrer0994c562013-11-10 03:19:14 +0800151 >>> algorithm = algorithms.ARC4(key)
Alex Gaynorf56444d2013-12-13 15:19:22 -0800152 >>> cipher = Cipher(algorithm, mode=None, backend=default_backend())
Paul Kehrer0994c562013-11-10 03:19:14 +0800153 >>> encryptor = cipher.encryptor()
154 >>> ct = encryptor.update(b"a secret message")
155 >>> decryptor = cipher.decryptor()
156 >>> decryptor.update(ct)
157 'a secret message'
158
David Reid30722b92013-11-07 13:03:39 -0800159
160.. _symmetric-encryption-modes:
161
Alex Gaynord96d1002013-08-08 07:37:26 -0700162Modes
163~~~~~
164
Paul Kehrer051099e2013-11-06 15:53:40 +0800165.. currentmodule:: cryptography.hazmat.primitives.ciphers.modes
David Reid1f3d7182013-10-22 16:55:18 -0700166
167.. class:: CBC(initialization_vector)
Alex Gaynor48ec9a32013-08-08 11:13:46 -0700168
169 CBC (Cipher block chaining) is a mode of operation for block ciphers. It is
170 considered cryptographically strong.
171
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600172 **Padding is required when using this mode.**
173
Alex Gaynor48ec9a32013-08-08 11:13:46 -0700174 :param bytes initialization_vector: Must be random bytes. They do not need
175 to be kept secret (they can be included
Alex Gaynor2dc2b862013-08-08 11:58:04 -0700176 in a transmitted message). Must be the
177 same number of bytes as the
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800178 ``block_size`` of the cipher. Each time
Alex Gaynor9de452d2013-11-07 13:28:23 -0800179 something is encrypted a new
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800180 ``initialization_vector`` should be
181 generated. Do not reuse an
182 ``initialization_vector`` with
183 a given ``key``, and particularly do
184 not use a constant
185 ``initialization_vector``.
186
187 A good construction looks like:
188
Alex Gaynor989061d2013-12-13 20:22:14 -0800189 .. doctest::
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800190
191 >>> import os
Alex Gaynord83c5902013-12-13 20:43:54 -0800192 >>> from cryptography.hazmat.primitives.ciphers.modes import CBC
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800193 >>> iv = os.urandom(16)
194 >>> mode = CBC(iv)
195
196 While the following is bad and will leak information:
197
Alex Gaynor989061d2013-12-13 20:22:14 -0800198 .. doctest::
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800199
Alex Gaynord83c5902013-12-13 20:43:54 -0800200 >>> from cryptography.hazmat.primitives.ciphers.modes import CBC
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800201 >>> iv = "a" * 16
202 >>> mode = CBC(iv)
Paul Kehrer13f108f2013-09-09 21:41:03 -0500203
Paul Kehrer45064282013-10-17 13:41:53 -0500204
David Reid1f3d7182013-10-22 16:55:18 -0700205.. class:: CTR(nonce)
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500206
Paul Kehrer45064282013-10-17 13:41:53 -0500207 .. warning::
208
209 Counter mode is not recommended for use with block ciphers that have a
210 block size of less than 128-bits.
211
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500212 CTR (Counter) is a mode of operation for block ciphers. It is considered
Alex Gaynord1f02012013-11-01 14:12:35 -0700213 cryptographically strong. It transforms a block cipher into a stream
214 cipher.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500215
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600216 **This mode does not require padding.**
217
Paul Kehrer89b3dd32013-10-17 14:02:45 -0500218 :param bytes nonce: Should be random bytes. It is critical to never reuse a
219 ``nonce`` with a given key. Any reuse of a nonce
220 with the same key compromises the security of every
221 message encrypted with that key. Must be the same
222 number of bytes as the ``block_size`` of the cipher
223 with a given key. The nonce does not need to be kept
224 secret and may be included alongside the ciphertext.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500225
David Reid1f3d7182013-10-22 16:55:18 -0700226.. class:: OFB(initialization_vector)
Paul Kehrer6f412a02013-09-10 21:30:50 -0500227
228 OFB (Output Feedback) is a mode of operation for block ciphers. It
229 transforms a block cipher into a stream cipher.
230
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600231 **This mode does not require padding.**
232
David Reidf1a39bd2013-09-11 16:28:42 -0700233 :param bytes initialization_vector: Must be random bytes. They do not need
234 to be kept secret (they can be included
235 in a transmitted message). Must be the
236 same number of bytes as the
237 ``block_size`` of the cipher. Do not
238 reuse an ``initialization_vector`` with
239 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
249 to be kept secret (they can be included
250 in a transmitted message). Must be the
251 same number of bytes as the
252 ``block_size`` of the cipher. Do not
253 reuse an ``initialization_vector`` with
254 a given ``key``.
255
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600256.. class:: GCM(initialization_vector, tag=None)
257
Paul Kehrer5b828b12013-11-29 17:32:08 -0600258 .. danger::
Paul Kehrer26c8c6a2013-11-29 16:24:56 -0600259
Alex Gaynord4f93832013-12-04 16:31:59 -0600260 When using this mode you MUST not use the decrypted data until
Alex Gaynor0d23e942013-12-04 17:28:24 -0600261 :meth:`~cryptography.hazmat.primitives.interfaces.CipherContext.finalize`
Alex Gaynord4f93832013-12-04 16:31:59 -0600262 has been called. GCM provides NO guarantees of ciphertext integrity
263 until decryption is complete.
Paul Kehrer26c8c6a2013-11-29 16:24:56 -0600264
Paul Kehrer5578c662013-12-03 17:37:42 -0600265 GCM (Galois Counter Mode) is a mode of operation for block ciphers. An
266 AEAD (authenticated encryption with additional data) mode is a type of
267 block cipher mode that encrypts the message as well as authenticating it
268 (and optionally additional data that is not encrypted) simultaneously.
269 Additional means of verifying integrity (like
Paul Kehrer2631c2b2013-11-24 10:20:50 -0600270 :doc:`HMAC </hazmat/primitives/hmac>`) are not necessary.
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600271
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600272 **This mode does not require padding.**
273
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600274 :param bytes initialization_vector: Must be random bytes. They do not need
275 to be kept secret (they can be included
Paul Kehrer67284372013-12-03 18:58:14 -0600276 in a transmitted message). NIST
277 `recommends 96-bit IV length`_ for
278 performance critical situations, but it
279 can be up to 2\ :sup:`64` - 1 bits.
280 Do not reuse an ``initialization_vector``
281 with a given ``key``.
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600282
Paul Kehrerca735042013-12-21 17:31:48 -0600283 .. note::
284
Paul Kehrerfc73e2d2013-12-21 18:41:38 -0600285 Cryptography will emit a 128-bit tag when finalizing encryption.
286 You can shorten a tag by truncating it to the desired length, but this
287 is **not recommended** as it lowers the security margins of the
Paul Kehrer048d6cb2013-12-21 18:53:19 -0600288 authentication (`NIST SP-800-38D`_ recommends 96-bits or greater).
Paul Kehrerfc73e2d2013-12-21 18:41:38 -0600289 If you must shorten the tag the minimum allowed length is 4 bytes
Paul Kehrer048d6cb2013-12-21 18:53:19 -0600290 (32-bits). Applications **must** verify the tag is the expected length
291 to guarantee the expected security margin.
Paul Kehrerca735042013-12-21 17:31:48 -0600292
Paul Kehrera07925a2013-12-06 11:49:42 -0600293 :param bytes tag: The tag bytes to verify during decryption. When encrypting
294 this must be None.
Paul Kehrer67abc862013-11-25 14:29:35 -0600295
David Reidabb72d22014-01-07 16:06:18 -0800296 .. testcode::
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600297
David Reidabb72d22014-01-07 16:06:18 -0800298 import os
299
300 from cryptography.hazmat.primitives.ciphers import (
301 Cipher, algorithms, modes
302 )
303
David Reid78569d62014-01-07 15:42:17 -0800304 def encrypt(key, plaintext, associated_data):
David Reidabb72d22014-01-07 16:06:18 -0800305 # Generate a random 96-bit IV.
David Reid78569d62014-01-07 15:42:17 -0800306 iv = os.urandom(12)
David Reidabb72d22014-01-07 16:06:18 -0800307
308 # Construct a AES-GCM Cipher object with the given and our randomly
309 # generated IV.
310 encryptor = Cipher(
David Reid78569d62014-01-07 15:42:17 -0800311 algorithms.AES(key),
312 modes.GCM(iv),
313 backend=default_backend()
David Reidabb72d22014-01-07 16:06:18 -0800314 ).encryptor()
David Reid78569d62014-01-07 15:42:17 -0800315
David Reidabb72d22014-01-07 16:06:18 -0800316 # associated_data will be authenticated but not encrypted,
317 # it must also be passed in on decryption.
David Reid78569d62014-01-07 15:42:17 -0800318 encryptor.authenticate_additional_data(associated_data)
David Reid78569d62014-01-07 15:42:17 -0800319
David Reidabb72d22014-01-07 16:06:18 -0800320 # Encrypt the plaintext and get the associated ciphertext.
Paul Kehreraf0b9f52014-01-07 19:21:49 -0600321 # GCM does not require padding.
322 ciphertext = encryptor.update(plaintext) + encryptor.finalize()
David Reidabb72d22014-01-07 16:06:18 -0800323
324 return (iv, ciphertext, encryptor.tag)
David Reid78569d62014-01-07 15:42:17 -0800325
326 def decrypt(key, associated_data, iv, ciphertext, tag):
Alex Gaynor007e5e12014-01-12 14:25:49 -0800327 if len(tag) != 16:
328 raise ValueError(
329 "tag must be 16 bytes -- truncation not supported"
330 )
331
David Reidabb72d22014-01-07 16:06:18 -0800332 # Construct a Cipher object, with the key, iv, and additionally the
333 # GCM tag used for authenticating the message.
334 decryptor = Cipher(
David Reid78569d62014-01-07 15:42:17 -0800335 algorithms.AES(key),
336 modes.GCM(iv, tag),
337 backend=default_backend()
David Reidabb72d22014-01-07 16:06:18 -0800338 ).decryptor()
David Reid78569d62014-01-07 15:42:17 -0800339
David Reidabb72d22014-01-07 16:06:18 -0800340 # We put associated_data back in or the tag will fail to verify
341 # when we finalize the decryptor.
David Reid78569d62014-01-07 15:42:17 -0800342 decryptor.authenticate_additional_data(associated_data)
343
Paul Kehreraf0b9f52014-01-07 19:21:49 -0600344 # Decryption gets us the authenticated plaintext.
345 # If the tag does not match an InvalidTag exception will be raised.
346 return decryptor.update(ciphertext) + decryptor.finalize()
David Reidabb72d22014-01-07 16:06:18 -0800347
348 iv, ciphertext, tag = encrypt(
David Reid78569d62014-01-07 15:42:17 -0800349 key,
David Reidabb72d22014-01-07 16:06:18 -0800350 b"a secret message!",
David Reid78569d62014-01-07 15:42:17 -0800351 b"authenticated but not encrypted payload"
352 )
353
David Reidabb72d22014-01-07 16:06:18 -0800354 print(decrypt(
355 key,
356 b"authenticated but not encrypted payload",
357 iv,
358 ciphertext,
359 tag
360 ))
David Reid78569d62014-01-07 15:42:17 -0800361
362 .. testoutput::
363
David Reidabb72d22014-01-07 16:06:18 -0800364 a secret message!
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600365
Paul Kehrer13f108f2013-09-09 21:41:03 -0500366
367Insecure Modes
368--------------
369
Alex Gaynorcd413a32013-09-10 18:59:43 -0700370.. warning::
371
372 These modes are insecure. New applications should never make use of them,
373 and existing applications should strongly consider migrating away.
374
375
David Reid1f3d7182013-10-22 16:55:18 -0700376.. class:: ECB()
Paul Kehrer13f108f2013-09-09 21:41:03 -0500377
378 ECB (Electronic Code Book) is the simplest mode of operation for block
Alex Gaynorcd413a32013-09-10 18:59:43 -0700379 ciphers. Each block of data is encrypted in the same way. This means
380 identical plaintext blocks will always result in identical ciphertext
381 blocks, and thus result in information leakage
Alex Gaynorab5f0112013-11-08 10:34:00 -0800382
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600383 **Padding is required when using this mode.**
384
Paul Kehrerad6d1642014-01-07 19:10:12 -0600385Interfaces
386----------
387
388.. class:: CipherContext
389
390 When calling ``encryptor()`` or ``decryptor()`` on a ``Cipher`` object
391 you will receive a return object conforming to the ``CipherContext``
392 interface. You can then call ``update(data)`` with data until you have fed
393 everything into the context. Once that is done call ``finalize()`` to
394 finish the operation and obtain the remainder of the data.
395
396 Block ciphers require that plaintext or ciphertext always be a multiple of
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600397 their block size, because of that **padding** is sometimes required to make
398 a message the correct size. ``CipherContext`` will not automatically apply
Paul Kehrerad6d1642014-01-07 19:10:12 -0600399 any padding; you'll need to add your own. For block ciphers the recommended
400 padding is :class:`cryptography.hazmat.primitives.padding.PKCS7`. If you
401 are using a stream cipher mode (such as
402 :class:`cryptography.hazmat.primitives.modes.CTR`) you don't have to worry
403 about this.
404
405 .. method:: update(data)
406
407 :param bytes data: The data you wish to pass into the context.
408 :return bytes: Returns the data that was encrypted or decrypted.
409 :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize`
410
411 When the ``Cipher`` was constructed in a mode that turns it into a
412 stream cipher (e.g.
413 :class:`cryptography.hazmat.primitives.ciphers.modes.CTR`), this will
414 return bytes immediately, however in other modes it will return chunks,
415 whose size is determined by the cipher's block size.
416
417 .. method:: finalize()
418
419 :return bytes: Returns the remainder of the data.
420 :raises ValueError: This is raised when the data provided isn't
421 correctly padded to be a multiple of the
422 algorithm's block size.
423
424 Once ``finalize`` is called this object can no longer be used and
425 :meth:`update` and :meth:`finalize` will raise
426 :class:`~cryptography.exceptions.AlreadyFinalized`.
427
428.. class:: AEADCipherContext
429
430 When calling ``encryptor()`` or ``decryptor()`` on a ``Cipher`` object
431 with an AEAD mode (e.g.
432 :class:`~cryptography.hazmat.primitives.ciphers.modes.GCM`) you will receive
433 a return object conforming to the ``AEADCipherContext`` and
434 ``CipherContext`` interfaces. If it is an encryption context it will
435 additionally be an ``AEADEncryptionContext`` interface.
436 ``AEADCipherContext`` contains an additional method
437 ``authenticate_additional_data`` for adding additional authenticated but
438 unencrypted data (see note below). You should call this before calls to
439 ``update``. When you are done call ``finalize()`` to finish the operation.
440
441 .. note::
442
443 In AEAD modes all data passed to ``update()`` will be both encrypted
444 and authenticated. Do not pass encrypted data to the
445 ``authenticate_additional_data()`` method. It is meant solely for
446 additional data you may want to authenticate but leave unencrypted.
447
448 .. method:: authenticate_additional_data(data)
449
450 :param bytes data: Any data you wish to authenticate but not encrypt.
451 :raises: :class:`~cryptography.exceptions.AlreadyFinalized`
452
453.. class:: AEADEncryptionContext
454
455 When creating an encryption context using ``encryptor()`` on a ``Cipher``
456 object with an AEAD mode (e.g.
457 :class:`~cryptography.hazmat.primitives.ciphers.modes.GCM`) you will receive
458 a return object conforming to the ``AEADEncryptionContext`` interface (as
459 well as ``AEADCipherContext``). This interface provides one additional
460 attribute ``tag``. ``tag`` can only be obtained after ``finalize()``.
461
462 .. attribute:: tag
463
464 :return bytes: Returns the tag value as bytes.
465 :raises: :class:`~cryptography.exceptions.NotYetFinalized` if called
466 before the context is finalized.
467
Alex Gaynorab5f0112013-11-08 10:34:00 -0800468
469.. _`described by Colin Percival`: http://www.daemonology.net/blog/2009-06-11-cryptographic-right-answers.html
Paul Kehrer67284372013-12-03 18:58:14 -0600470.. _`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 -0600471.. _`NIST SP-800-38D`: http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf