blob: d91dde9db4354f8d3b304b88afadc134a8a6ff70 [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
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
111 (note that DES functionally uses ``56``, ``112``, or
112 ``168`` bits of the key, there is a parity byte in each
113 component of the key), in some materials these are
114 referred to as being up to three separate keys (each
115 ``56`` bits long), they can simply be concatenated to
116 produce the full key. This must be kept secret.
117
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
126 :param bytes key: The secret key, 40-128 bits in length (in increments of
127 8). This must be kept secret.
128
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 Gaynorab5f0112013-11-08 10:34:00 -0800142 that users of Blowfish move to newer algorithms, such as :class:`AES`.
Paul Kehrer5df0abe2013-10-30 16:57:04 -0500143
144 :param bytes key: The secret key, 32-448 bits in length (in increments of
145 8). This must be kept secret.
146
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
153 :param bytes key: The secret key, ``40``, ``56``, ``64``, ``80``, ``128``,
154 ``192``, or ``256`` bits in length. This must be kept
155 secret.
156
Paul Kehrer0994c562013-11-10 03:19:14 +0800157 .. doctest::
158
159 >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
Alex Gaynorf8796b12013-12-13 20:28:55 -0800160 >>> from cryptography.hazmat.backends import default_backend
Paul Kehrer0994c562013-11-10 03:19:14 +0800161 >>> algorithm = algorithms.ARC4(key)
Alex Gaynorf56444d2013-12-13 15:19:22 -0800162 >>> cipher = Cipher(algorithm, mode=None, backend=default_backend())
Paul Kehrer0994c562013-11-10 03:19:14 +0800163 >>> encryptor = cipher.encryptor()
164 >>> ct = encryptor.update(b"a secret message")
165 >>> decryptor = cipher.decryptor()
166 >>> decryptor.update(ct)
167 'a secret message'
168
David Reid30722b92013-11-07 13:03:39 -0800169
170.. _symmetric-encryption-modes:
171
Alex Gaynord96d1002013-08-08 07:37:26 -0700172Modes
173~~~~~
174
Paul Kehrer051099e2013-11-06 15:53:40 +0800175.. currentmodule:: cryptography.hazmat.primitives.ciphers.modes
David Reid1f3d7182013-10-22 16:55:18 -0700176
177.. class:: CBC(initialization_vector)
Alex Gaynor48ec9a32013-08-08 11:13:46 -0700178
179 CBC (Cipher block chaining) is a mode of operation for block ciphers. It is
180 considered cryptographically strong.
181
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600182 **Padding is required when using this mode.**
183
Alex Gaynor48ec9a32013-08-08 11:13:46 -0700184 :param bytes initialization_vector: Must be random bytes. They do not need
185 to be kept secret (they can be included
Alex Gaynor2dc2b862013-08-08 11:58:04 -0700186 in a transmitted message). Must be the
187 same number of bytes as the
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800188 ``block_size`` of the cipher. Each time
Alex Gaynor9de452d2013-11-07 13:28:23 -0800189 something is encrypted a new
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800190 ``initialization_vector`` should be
191 generated. Do not reuse an
192 ``initialization_vector`` with
193 a given ``key``, and particularly do
194 not use a constant
195 ``initialization_vector``.
196
197 A good construction looks like:
198
Alex Gaynor989061d2013-12-13 20:22:14 -0800199 .. doctest::
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800200
201 >>> import os
Alex Gaynord83c5902013-12-13 20:43:54 -0800202 >>> from cryptography.hazmat.primitives.ciphers.modes import CBC
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800203 >>> iv = os.urandom(16)
204 >>> mode = CBC(iv)
205
206 While the following is bad and will leak information:
207
Alex Gaynor989061d2013-12-13 20:22:14 -0800208 .. doctest::
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800209
Alex Gaynord83c5902013-12-13 20:43:54 -0800210 >>> from cryptography.hazmat.primitives.ciphers.modes import CBC
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800211 >>> iv = "a" * 16
212 >>> mode = CBC(iv)
Paul Kehrer13f108f2013-09-09 21:41:03 -0500213
Paul Kehrer45064282013-10-17 13:41:53 -0500214
David Reid1f3d7182013-10-22 16:55:18 -0700215.. class:: CTR(nonce)
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500216
Paul Kehrer45064282013-10-17 13:41:53 -0500217 .. warning::
218
219 Counter mode is not recommended for use with block ciphers that have a
220 block size of less than 128-bits.
221
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500222 CTR (Counter) is a mode of operation for block ciphers. It is considered
Alex Gaynord1f02012013-11-01 14:12:35 -0700223 cryptographically strong. It transforms a block cipher into a stream
224 cipher.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500225
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600226 **This mode does not require padding.**
227
Paul Kehrer89b3dd32013-10-17 14:02:45 -0500228 :param bytes nonce: Should be random bytes. It is critical to never reuse a
229 ``nonce`` with a given key. Any reuse of a nonce
230 with the same key compromises the security of every
231 message encrypted with that key. Must be the same
232 number of bytes as the ``block_size`` of the cipher
233 with a given key. The nonce does not need to be kept
234 secret and may be included alongside the ciphertext.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500235
David Reid1f3d7182013-10-22 16:55:18 -0700236.. class:: OFB(initialization_vector)
Paul Kehrer6f412a02013-09-10 21:30:50 -0500237
238 OFB (Output Feedback) is a mode of operation for block ciphers. It
239 transforms a block cipher into a stream cipher.
240
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600241 **This mode does not require padding.**
242
David Reidf1a39bd2013-09-11 16:28:42 -0700243 :param bytes initialization_vector: Must be random bytes. They do not need
244 to be kept secret (they can be included
245 in a transmitted message). Must be the
246 same number of bytes as the
247 ``block_size`` of the cipher. Do not
248 reuse an ``initialization_vector`` with
249 a given ``key``.
Paul Kehrer6f412a02013-09-10 21:30:50 -0500250
David Reid1f3d7182013-10-22 16:55:18 -0700251.. class:: CFB(initialization_vector)
Paul Kehrer4223df72013-09-11 09:48:04 -0500252
253 CFB (Cipher Feedback) is a mode of operation for block ciphers. It
254 transforms a block cipher into a stream cipher.
255
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600256 **This mode does not require padding.**
257
Paul Kehrer4223df72013-09-11 09:48:04 -0500258 :param bytes initialization_vector: Must be random bytes. They do not need
259 to be kept secret (they can be included
260 in a transmitted message). Must be the
261 same number of bytes as the
262 ``block_size`` of the cipher. Do not
263 reuse an ``initialization_vector`` with
264 a given ``key``.
265
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600266.. class:: GCM(initialization_vector, tag=None)
267
Paul Kehrer5b828b12013-11-29 17:32:08 -0600268 .. danger::
Paul Kehrer26c8c6a2013-11-29 16:24:56 -0600269
Alex Gaynord4f93832013-12-04 16:31:59 -0600270 When using this mode you MUST not use the decrypted data until
Alex Gaynor0d23e942013-12-04 17:28:24 -0600271 :meth:`~cryptography.hazmat.primitives.interfaces.CipherContext.finalize`
Alex Gaynord4f93832013-12-04 16:31:59 -0600272 has been called. GCM provides NO guarantees of ciphertext integrity
273 until decryption is complete.
Paul Kehrer26c8c6a2013-11-29 16:24:56 -0600274
Paul Kehrer5578c662013-12-03 17:37:42 -0600275 GCM (Galois Counter Mode) is a mode of operation for block ciphers. An
276 AEAD (authenticated encryption with additional data) mode is a type of
277 block cipher mode that encrypts the message as well as authenticating it
278 (and optionally additional data that is not encrypted) simultaneously.
279 Additional means of verifying integrity (like
Paul Kehrer2631c2b2013-11-24 10:20:50 -0600280 :doc:`HMAC </hazmat/primitives/hmac>`) are not necessary.
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600281
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600282 **This mode does not require padding.**
283
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600284 :param bytes initialization_vector: Must be random bytes. They do not need
285 to be kept secret (they can be included
Paul Kehrer67284372013-12-03 18:58:14 -0600286 in a transmitted message). NIST
287 `recommends 96-bit IV length`_ for
288 performance critical situations, but it
289 can be up to 2\ :sup:`64` - 1 bits.
290 Do not reuse an ``initialization_vector``
291 with a given ``key``.
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600292
Paul Kehrerca735042013-12-21 17:31:48 -0600293 .. note::
294
Paul Kehrerfc73e2d2013-12-21 18:41:38 -0600295 Cryptography will emit a 128-bit tag when finalizing encryption.
296 You can shorten a tag by truncating it to the desired length, but this
297 is **not recommended** as it lowers the security margins of the
Paul Kehrer048d6cb2013-12-21 18:53:19 -0600298 authentication (`NIST SP-800-38D`_ recommends 96-bits or greater).
Paul Kehrerfc73e2d2013-12-21 18:41:38 -0600299 If you must shorten the tag the minimum allowed length is 4 bytes
Paul Kehrer048d6cb2013-12-21 18:53:19 -0600300 (32-bits). Applications **must** verify the tag is the expected length
301 to guarantee the expected security margin.
Paul Kehrerca735042013-12-21 17:31:48 -0600302
Paul Kehrera07925a2013-12-06 11:49:42 -0600303 :param bytes tag: The tag bytes to verify during decryption. When encrypting
304 this must be None.
Paul Kehrer67abc862013-11-25 14:29:35 -0600305
David Reidabb72d22014-01-07 16:06:18 -0800306 .. testcode::
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600307
David Reidabb72d22014-01-07 16:06:18 -0800308 import os
309
310 from cryptography.hazmat.primitives.ciphers import (
311 Cipher, algorithms, modes
312 )
313
David Reid78569d62014-01-07 15:42:17 -0800314 def encrypt(key, plaintext, associated_data):
David Reidabb72d22014-01-07 16:06:18 -0800315 # Generate a random 96-bit IV.
David Reid78569d62014-01-07 15:42:17 -0800316 iv = os.urandom(12)
David Reidabb72d22014-01-07 16:06:18 -0800317
318 # Construct a AES-GCM Cipher object with the given and our randomly
319 # generated IV.
320 encryptor = Cipher(
David Reid78569d62014-01-07 15:42:17 -0800321 algorithms.AES(key),
322 modes.GCM(iv),
323 backend=default_backend()
David Reidabb72d22014-01-07 16:06:18 -0800324 ).encryptor()
David Reid78569d62014-01-07 15:42:17 -0800325
David Reidabb72d22014-01-07 16:06:18 -0800326 # associated_data will be authenticated but not encrypted,
327 # it must also be passed in on decryption.
David Reid78569d62014-01-07 15:42:17 -0800328 encryptor.authenticate_additional_data(associated_data)
David Reid78569d62014-01-07 15:42:17 -0800329
David Reidabb72d22014-01-07 16:06:18 -0800330 # Encrypt the plaintext and get the associated ciphertext.
Paul Kehreraf0b9f52014-01-07 19:21:49 -0600331 # GCM does not require padding.
332 ciphertext = encryptor.update(plaintext) + encryptor.finalize()
David Reidabb72d22014-01-07 16:06:18 -0800333
334 return (iv, ciphertext, encryptor.tag)
David Reid78569d62014-01-07 15:42:17 -0800335
336 def decrypt(key, associated_data, iv, ciphertext, tag):
Alex Gaynor007e5e12014-01-12 14:25:49 -0800337 if len(tag) != 16:
338 raise ValueError(
339 "tag must be 16 bytes -- truncation not supported"
340 )
341
David Reidabb72d22014-01-07 16:06:18 -0800342 # Construct a Cipher object, with the key, iv, and additionally the
343 # GCM tag used for authenticating the message.
344 decryptor = Cipher(
David Reid78569d62014-01-07 15:42:17 -0800345 algorithms.AES(key),
346 modes.GCM(iv, tag),
347 backend=default_backend()
David Reidabb72d22014-01-07 16:06:18 -0800348 ).decryptor()
David Reid78569d62014-01-07 15:42:17 -0800349
David Reidabb72d22014-01-07 16:06:18 -0800350 # We put associated_data back in or the tag will fail to verify
351 # when we finalize the decryptor.
David Reid78569d62014-01-07 15:42:17 -0800352 decryptor.authenticate_additional_data(associated_data)
353
Paul Kehreraf0b9f52014-01-07 19:21:49 -0600354 # Decryption gets us the authenticated plaintext.
355 # If the tag does not match an InvalidTag exception will be raised.
356 return decryptor.update(ciphertext) + decryptor.finalize()
David Reidabb72d22014-01-07 16:06:18 -0800357
358 iv, ciphertext, tag = encrypt(
David Reid78569d62014-01-07 15:42:17 -0800359 key,
David Reidabb72d22014-01-07 16:06:18 -0800360 b"a secret message!",
David Reid78569d62014-01-07 15:42:17 -0800361 b"authenticated but not encrypted payload"
362 )
363
David Reidabb72d22014-01-07 16:06:18 -0800364 print(decrypt(
365 key,
366 b"authenticated but not encrypted payload",
367 iv,
368 ciphertext,
369 tag
370 ))
David Reid78569d62014-01-07 15:42:17 -0800371
372 .. testoutput::
373
David Reidabb72d22014-01-07 16:06:18 -0800374 a secret message!
Paul Kehrer22e80cb2013-11-20 21:27:00 -0600375
Paul Kehrer13f108f2013-09-09 21:41:03 -0500376
377Insecure Modes
378--------------
379
Alex Gaynorcd413a32013-09-10 18:59:43 -0700380.. warning::
381
382 These modes are insecure. New applications should never make use of them,
383 and existing applications should strongly consider migrating away.
384
385
David Reid1f3d7182013-10-22 16:55:18 -0700386.. class:: ECB()
Paul Kehrer13f108f2013-09-09 21:41:03 -0500387
388 ECB (Electronic Code Book) is the simplest mode of operation for block
Alex Gaynorcd413a32013-09-10 18:59:43 -0700389 ciphers. Each block of data is encrypted in the same way. This means
390 identical plaintext blocks will always result in identical ciphertext
391 blocks, and thus result in information leakage
Alex Gaynorab5f0112013-11-08 10:34:00 -0800392
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600393 **Padding is required when using this mode.**
394
Paul Kehrerad6d1642014-01-07 19:10:12 -0600395Interfaces
396----------
397
398.. class:: CipherContext
399
400 When calling ``encryptor()`` or ``decryptor()`` on a ``Cipher`` object
Alex Gaynorb4818892014-02-06 10:58:50 -0800401 the result will conform to the ``CipherContext`` interface. You can then
402 call ``update(data)`` with data until you have fed everything into the
403 context. Once that is done call ``finalize()`` to finish the operation and
404 obtain the remainder of the data.
Paul Kehrerad6d1642014-01-07 19:10:12 -0600405
406 Block ciphers require that plaintext or ciphertext always be a multiple of
Paul Kehrerfe2e3c22014-01-07 20:55:20 -0600407 their block size, because of that **padding** is sometimes required to make
408 a message the correct size. ``CipherContext`` will not automatically apply
Paul Kehrerad6d1642014-01-07 19:10:12 -0600409 any padding; you'll need to add your own. For block ciphers the recommended
410 padding is :class:`cryptography.hazmat.primitives.padding.PKCS7`. If you
411 are using a stream cipher mode (such as
412 :class:`cryptography.hazmat.primitives.modes.CTR`) you don't have to worry
413 about this.
414
415 .. method:: update(data)
416
417 :param bytes data: The data you wish to pass into the context.
418 :return bytes: Returns the data that was encrypted or decrypted.
419 :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize`
420
421 When the ``Cipher`` was constructed in a mode that turns it into a
422 stream cipher (e.g.
423 :class:`cryptography.hazmat.primitives.ciphers.modes.CTR`), this will
424 return bytes immediately, however in other modes it will return chunks,
425 whose size is determined by the cipher's block size.
426
427 .. method:: finalize()
428
429 :return bytes: Returns the remainder of the data.
430 :raises ValueError: This is raised when the data provided isn't
431 correctly padded to be a multiple of the
432 algorithm's block size.
433
434 Once ``finalize`` is called this object can no longer be used and
435 :meth:`update` and :meth:`finalize` will raise
436 :class:`~cryptography.exceptions.AlreadyFinalized`.
437
438.. class:: AEADCipherContext
439
440 When calling ``encryptor()`` or ``decryptor()`` on a ``Cipher`` object
441 with an AEAD mode (e.g.
Alex Gaynorb4818892014-02-06 10:58:50 -0800442 :class:`~cryptography.hazmat.primitives.ciphers.modes.GCM`) the result will
443 conform to the ``AEADCipherContext`` and ``CipherContext`` interfaces. If
444 it is an encryption context it will additionally be an
445 ``AEADEncryptionContext`` interface. ``AEADCipherContext`` contains an
446 additional method ``authenticate_additional_data`` for adding additional
447 authenticated but unencrypted data (see note below). You should call this
448 before calls to ``update``. When you are done call ``finalize()`` to finish
449 the operation.
Paul Kehrerad6d1642014-01-07 19:10:12 -0600450
451 .. note::
452
453 In AEAD modes all data passed to ``update()`` will be both encrypted
454 and authenticated. Do not pass encrypted data to the
455 ``authenticate_additional_data()`` method. It is meant solely for
456 additional data you may want to authenticate but leave unencrypted.
457
458 .. method:: authenticate_additional_data(data)
459
460 :param bytes data: Any data you wish to authenticate but not encrypt.
461 :raises: :class:`~cryptography.exceptions.AlreadyFinalized`
462
463.. class:: AEADEncryptionContext
464
465 When creating an encryption context using ``encryptor()`` on a ``Cipher``
466 object with an AEAD mode (e.g.
467 :class:`~cryptography.hazmat.primitives.ciphers.modes.GCM`) you will receive
468 a return object conforming to the ``AEADEncryptionContext`` interface (as
469 well as ``AEADCipherContext``). This interface provides one additional
470 attribute ``tag``. ``tag`` can only be obtained after ``finalize()``.
471
472 .. attribute:: tag
473
474 :return bytes: Returns the tag value as bytes.
475 :raises: :class:`~cryptography.exceptions.NotYetFinalized` if called
476 before the context is finalized.
477
Alex Gaynorab5f0112013-11-08 10:34:00 -0800478
479.. _`described by Colin Percival`: http://www.daemonology.net/blog/2009-06-11-cryptographic-right-answers.html
Paul Kehrer67284372013-12-03 18:58:14 -0600480.. _`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 -0600481.. _`NIST SP-800-38D`: http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf
Paul Kehrerbab0e1a2014-02-09 10:51:59 -0600482.. _`Communications Security Establishment`: http://www.cse-cst.gc.ca