blob: 2233d525d1b935980c22861bfc91d0e35f1d0742 [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
172 :param bytes initialization_vector: Must be random bytes. They do not need
173 to be kept secret (they can be included
Alex Gaynor2dc2b862013-08-08 11:58:04 -0700174 in a transmitted message). Must be the
175 same number of bytes as the
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800176 ``block_size`` of the cipher. Each time
Alex Gaynor9de452d2013-11-07 13:28:23 -0800177 something is encrypted a new
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800178 ``initialization_vector`` should be
179 generated. Do not reuse an
180 ``initialization_vector`` with
181 a given ``key``, and particularly do
182 not use a constant
183 ``initialization_vector``.
184
185 A good construction looks like:
186
Alex Gaynor989061d2013-12-13 20:22:14 -0800187 .. doctest::
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800188
189 >>> import os
Alex Gaynord83c5902013-12-13 20:43:54 -0800190 >>> from cryptography.hazmat.primitives.ciphers.modes import CBC
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800191 >>> iv = os.urandom(16)
192 >>> mode = CBC(iv)
193
194 While the following is bad and will leak information:
195
Alex Gaynor989061d2013-12-13 20:22:14 -0800196 .. doctest::
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800197
Alex Gaynord83c5902013-12-13 20:43:54 -0800198 >>> from cryptography.hazmat.primitives.ciphers.modes import CBC
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800199 >>> iv = "a" * 16
200 >>> mode = CBC(iv)
Paul Kehrer13f108f2013-09-09 21:41:03 -0500201
Paul Kehrer45064282013-10-17 13:41:53 -0500202
David Reid1f3d7182013-10-22 16:55:18 -0700203.. class:: CTR(nonce)
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500204
Paul Kehrer45064282013-10-17 13:41:53 -0500205 .. warning::
206
207 Counter mode is not recommended for use with block ciphers that have a
208 block size of less than 128-bits.
209
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500210 CTR (Counter) is a mode of operation for block ciphers. It is considered
Alex Gaynord1f02012013-11-01 14:12:35 -0700211 cryptographically strong. It transforms a block cipher into a stream
212 cipher.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500213
Paul Kehrer89b3dd32013-10-17 14:02:45 -0500214 :param bytes nonce: Should be random bytes. It is critical to never reuse a
215 ``nonce`` with a given key. Any reuse of a nonce
216 with the same key compromises the security of every
217 message encrypted with that key. Must be the same
218 number of bytes as the ``block_size`` of the cipher
219 with a given key. The nonce does not need to be kept
220 secret and may be included alongside the ciphertext.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500221
David Reid1f3d7182013-10-22 16:55:18 -0700222.. class:: OFB(initialization_vector)
Paul Kehrer6f412a02013-09-10 21:30:50 -0500223
224 OFB (Output Feedback) is a mode of operation for block ciphers. It
225 transforms a block cipher into a stream cipher.
226
David Reidf1a39bd2013-09-11 16:28:42 -0700227 :param bytes initialization_vector: Must be random bytes. They do not need
228 to be kept secret (they can be included
229 in a transmitted message). Must be the
230 same number of bytes as the
231 ``block_size`` of the cipher. Do not
232 reuse an ``initialization_vector`` with
233 a given ``key``.
Paul Kehrer6f412a02013-09-10 21:30:50 -0500234
David Reid1f3d7182013-10-22 16:55:18 -0700235.. class:: CFB(initialization_vector)
Paul Kehrer4223df72013-09-11 09:48:04 -0500236
237 CFB (Cipher Feedback) is a mode of operation for block ciphers. It
238 transforms a block cipher into a stream cipher.
239
240 :param bytes initialization_vector: Must be random bytes. They do not need
241 to be kept secret (they can be included
242 in a transmitted message). Must be the
243 same number of bytes as the
244 ``block_size`` of the cipher. Do not
245 reuse an ``initialization_vector`` with
246 a given ``key``.
247
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600248.. class:: GCM(initialization_vector, tag=None)
249
Paul Kehrer5b828b12013-11-29 17:32:08 -0600250 .. danger::
Paul Kehrer26c8c6a2013-11-29 16:24:56 -0600251
Alex Gaynord4f93832013-12-04 16:31:59 -0600252 When using this mode you MUST not use the decrypted data until
Alex Gaynor0d23e942013-12-04 17:28:24 -0600253 :meth:`~cryptography.hazmat.primitives.interfaces.CipherContext.finalize`
Alex Gaynord4f93832013-12-04 16:31:59 -0600254 has been called. GCM provides NO guarantees of ciphertext integrity
255 until decryption is complete.
Paul Kehrer26c8c6a2013-11-29 16:24:56 -0600256
Paul Kehrer5578c662013-12-03 17:37:42 -0600257 GCM (Galois Counter Mode) is a mode of operation for block ciphers. An
258 AEAD (authenticated encryption with additional data) mode is a type of
259 block cipher mode that encrypts the message as well as authenticating it
260 (and optionally additional data that is not encrypted) simultaneously.
261 Additional means of verifying integrity (like
Paul Kehrer2631c2b2013-11-24 10:20:50 -0600262 :doc:`HMAC </hazmat/primitives/hmac>`) are not necessary.
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600263
264 :param bytes initialization_vector: Must be random bytes. They do not need
265 to be kept secret (they can be included
Paul Kehrer67284372013-12-03 18:58:14 -0600266 in a transmitted message). NIST
267 `recommends 96-bit IV length`_ for
268 performance critical situations, but it
269 can be up to 2\ :sup:`64` - 1 bits.
270 Do not reuse an ``initialization_vector``
271 with a given ``key``.
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600272
Paul Kehrerca735042013-12-21 17:31:48 -0600273 .. note::
274
Paul Kehrerfc73e2d2013-12-21 18:41:38 -0600275 Cryptography will emit a 128-bit tag when finalizing encryption.
276 You can shorten a tag by truncating it to the desired length, but this
277 is **not recommended** as it lowers the security margins of the
Paul Kehrer048d6cb2013-12-21 18:53:19 -0600278 authentication (`NIST SP-800-38D`_ recommends 96-bits or greater).
Paul Kehrerfc73e2d2013-12-21 18:41:38 -0600279 If you must shorten the tag the minimum allowed length is 4 bytes
Paul Kehrer048d6cb2013-12-21 18:53:19 -0600280 (32-bits). Applications **must** verify the tag is the expected length
281 to guarantee the expected security margin.
Paul Kehrerca735042013-12-21 17:31:48 -0600282
Paul Kehrera07925a2013-12-06 11:49:42 -0600283 :param bytes tag: The tag bytes to verify during decryption. When encrypting
284 this must be None.
Paul Kehrer67abc862013-11-25 14:29:35 -0600285
David Reidabb72d22014-01-07 16:06:18 -0800286 .. testcode::
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600287
David Reidabb72d22014-01-07 16:06:18 -0800288 import os
289
290 from cryptography.hazmat.primitives.ciphers import (
291 Cipher, algorithms, modes
292 )
293
David Reid78569d62014-01-07 15:42:17 -0800294 def encrypt(key, plaintext, associated_data):
David Reidabb72d22014-01-07 16:06:18 -0800295 # Generate a random 96-bit IV.
David Reid78569d62014-01-07 15:42:17 -0800296 iv = os.urandom(12)
David Reidabb72d22014-01-07 16:06:18 -0800297
298 # Construct a AES-GCM Cipher object with the given and our randomly
299 # generated IV.
300 encryptor = Cipher(
David Reid78569d62014-01-07 15:42:17 -0800301 algorithms.AES(key),
302 modes.GCM(iv),
303 backend=default_backend()
David Reidabb72d22014-01-07 16:06:18 -0800304 ).encryptor()
David Reid78569d62014-01-07 15:42:17 -0800305
David Reidabb72d22014-01-07 16:06:18 -0800306 # associated_data will be authenticated but not encrypted,
307 # it must also be passed in on decryption.
David Reid78569d62014-01-07 15:42:17 -0800308 encryptor.authenticate_additional_data(associated_data)
David Reid78569d62014-01-07 15:42:17 -0800309
David Reidabb72d22014-01-07 16:06:18 -0800310 # Encrypt the plaintext and get the associated ciphertext.
Paul Kehreraf0b9f52014-01-07 19:21:49 -0600311 # GCM does not require padding.
312 ciphertext = encryptor.update(plaintext) + encryptor.finalize()
David Reidabb72d22014-01-07 16:06:18 -0800313
314 return (iv, ciphertext, encryptor.tag)
David Reid78569d62014-01-07 15:42:17 -0800315
316 def decrypt(key, associated_data, iv, ciphertext, tag):
David Reidabb72d22014-01-07 16:06:18 -0800317 # Construct a Cipher object, with the key, iv, and additionally the
318 # GCM tag used for authenticating the message.
319 decryptor = Cipher(
David Reid78569d62014-01-07 15:42:17 -0800320 algorithms.AES(key),
321 modes.GCM(iv, tag),
322 backend=default_backend()
David Reidabb72d22014-01-07 16:06:18 -0800323 ).decryptor()
David Reid78569d62014-01-07 15:42:17 -0800324
David Reidabb72d22014-01-07 16:06:18 -0800325 # We put associated_data back in or the tag will fail to verify
326 # when we finalize the decryptor.
David Reid78569d62014-01-07 15:42:17 -0800327 decryptor.authenticate_additional_data(associated_data)
328
Paul Kehreraf0b9f52014-01-07 19:21:49 -0600329 # Decryption gets us the authenticated plaintext.
330 # If the tag does not match an InvalidTag exception will be raised.
331 return decryptor.update(ciphertext) + decryptor.finalize()
David Reidabb72d22014-01-07 16:06:18 -0800332
333 iv, ciphertext, tag = encrypt(
David Reid78569d62014-01-07 15:42:17 -0800334 key,
David Reidabb72d22014-01-07 16:06:18 -0800335 b"a secret message!",
David Reid78569d62014-01-07 15:42:17 -0800336 b"authenticated but not encrypted payload"
337 )
338
David Reidabb72d22014-01-07 16:06:18 -0800339 print(decrypt(
340 key,
341 b"authenticated but not encrypted payload",
342 iv,
343 ciphertext,
344 tag
345 ))
David Reid78569d62014-01-07 15:42:17 -0800346
347 .. testoutput::
348
David Reidabb72d22014-01-07 16:06:18 -0800349 a secret message!
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600350
Paul Kehrer13f108f2013-09-09 21:41:03 -0500351
352Insecure Modes
353--------------
354
Alex Gaynorcd413a32013-09-10 18:59:43 -0700355.. warning::
356
357 These modes are insecure. New applications should never make use of them,
358 and existing applications should strongly consider migrating away.
359
360
David Reid1f3d7182013-10-22 16:55:18 -0700361.. class:: ECB()
Paul Kehrer13f108f2013-09-09 21:41:03 -0500362
363 ECB (Electronic Code Book) is the simplest mode of operation for block
Alex Gaynorcd413a32013-09-10 18:59:43 -0700364 ciphers. Each block of data is encrypted in the same way. This means
365 identical plaintext blocks will always result in identical ciphertext
366 blocks, and thus result in information leakage
Alex Gaynorab5f0112013-11-08 10:34:00 -0800367
Paul Kehrerad6d1642014-01-07 19:10:12 -0600368Interfaces
369----------
370
371.. class:: CipherContext
372
373 When calling ``encryptor()`` or ``decryptor()`` on a ``Cipher`` object
374 you will receive a return object conforming to the ``CipherContext``
375 interface. You can then call ``update(data)`` with data until you have fed
376 everything into the context. Once that is done call ``finalize()`` to
377 finish the operation and obtain the remainder of the data.
378
379 Block ciphers require that plaintext or ciphertext always be a multiple of
380 their block size, because of that **padding** is often required to make a
381 message the correct size. ``CipherContext`` will not automatically apply
382 any padding; you'll need to add your own. For block ciphers the recommended
383 padding is :class:`cryptography.hazmat.primitives.padding.PKCS7`. If you
384 are using a stream cipher mode (such as
385 :class:`cryptography.hazmat.primitives.modes.CTR`) you don't have to worry
386 about this.
387
388 .. method:: update(data)
389
390 :param bytes data: The data you wish to pass into the context.
391 :return bytes: Returns the data that was encrypted or decrypted.
392 :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize`
393
394 When the ``Cipher`` was constructed in a mode that turns it into a
395 stream cipher (e.g.
396 :class:`cryptography.hazmat.primitives.ciphers.modes.CTR`), this will
397 return bytes immediately, however in other modes it will return chunks,
398 whose size is determined by the cipher's block size.
399
400 .. method:: finalize()
401
402 :return bytes: Returns the remainder of the data.
403 :raises ValueError: This is raised when the data provided isn't
404 correctly padded to be a multiple of the
405 algorithm's block size.
406
407 Once ``finalize`` is called this object can no longer be used and
408 :meth:`update` and :meth:`finalize` will raise
409 :class:`~cryptography.exceptions.AlreadyFinalized`.
410
411.. class:: AEADCipherContext
412
413 When calling ``encryptor()`` or ``decryptor()`` on a ``Cipher`` object
414 with an AEAD mode (e.g.
415 :class:`~cryptography.hazmat.primitives.ciphers.modes.GCM`) you will receive
416 a return object conforming to the ``AEADCipherContext`` and
417 ``CipherContext`` interfaces. If it is an encryption context it will
418 additionally be an ``AEADEncryptionContext`` interface.
419 ``AEADCipherContext`` contains an additional method
420 ``authenticate_additional_data`` for adding additional authenticated but
421 unencrypted data (see note below). You should call this before calls to
422 ``update``. When you are done call ``finalize()`` to finish the operation.
423
424 .. note::
425
426 In AEAD modes all data passed to ``update()`` will be both encrypted
427 and authenticated. Do not pass encrypted data to the
428 ``authenticate_additional_data()`` method. It is meant solely for
429 additional data you may want to authenticate but leave unencrypted.
430
431 .. method:: authenticate_additional_data(data)
432
433 :param bytes data: Any data you wish to authenticate but not encrypt.
434 :raises: :class:`~cryptography.exceptions.AlreadyFinalized`
435
436.. class:: AEADEncryptionContext
437
438 When creating an encryption context using ``encryptor()`` on a ``Cipher``
439 object with an AEAD mode (e.g.
440 :class:`~cryptography.hazmat.primitives.ciphers.modes.GCM`) you will receive
441 a return object conforming to the ``AEADEncryptionContext`` interface (as
442 well as ``AEADCipherContext``). This interface provides one additional
443 attribute ``tag``. ``tag`` can only be obtained after ``finalize()``.
444
445 .. attribute:: tag
446
447 :return bytes: Returns the tag value as bytes.
448 :raises: :class:`~cryptography.exceptions.NotYetFinalized` if called
449 before the context is finalized.
450
Alex Gaynorab5f0112013-11-08 10:34:00 -0800451
452.. _`described by Colin Percival`: http://www.daemonology.net/blog/2009-06-11-cryptographic-right-answers.html
Paul Kehrer67284372013-12-03 18:58:14 -0600453.. _`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 -0600454.. _`NIST SP-800-38D`: http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf