blob: 42d2090cd372b877a92ad96d58089890bdf376b7 [file] [log] [blame]
Alex Gaynoraf82d5e2013-10-29 17:07:24 -07001.. hazmat::
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
David Reidef0fcf22013-11-06 11:12:45 -080015 from cryptography.hazmat.bindings import default_backend
16 backend = default_backend()
17
Donald Stufft173de982013-08-12 07:34:39 -040018
Alex Gaynorf6c47e92013-08-08 07:16:01 -070019Symmetric encryption is a way to encrypt (hide the plaintext value) material
Alex Gaynorb317c7a2013-11-15 16:45:52 -080020where the sender and receiver both use the same key. Note that symmetric
Alex Gaynorab5f0112013-11-08 10:34:00 -080021encryption is **not** sufficient for most applications, because it only
22provides secrecy (an attacker can't see the message) but not authenticity (an
23attacker can create bogus messages and force the application to decrypt them).
Alex Gaynor9316f4c2013-11-15 16:38:42 -080024For this reason it is *strongly* recommended to combine encryption with a
Alex Gaynorab5f0112013-11-08 10:34:00 -080025message authentication code, such as :doc:`HMAC </hazmat/primitives/hmac>`, in
26an "encrypt-then-MAC" formulation as `described by Colin Percival`_.
Alex Gaynorf6c47e92013-08-08 07:16:01 -070027
David Reidef0fcf22013-11-06 11:12:45 -080028.. class:: Cipher(algorithm, mode, backend)
Alex Gaynorf6c47e92013-08-08 07:16:01 -070029
Alex Gaynorab5f0112013-11-08 10:34:00 -080030 Cipher objects combine an algorithm (such as
31 :class:`~cryptography.hazmat.primitives.ciphers.algorithms.AES`) with a
32 mode (such as
33 :class:`~cryptography.hazmat.primitives.ciphers.modes.CBC` or
34 :class:`~cryptography.hazmat.primitives.ciphers.modes.CTR`). A simple
35 example of encrypting (and then decrypting) content with AES is:
Alex Gaynorf6c47e92013-08-08 07:16:01 -070036
Donald Stufft173de982013-08-12 07:34:39 -040037 .. doctest::
Alex Gaynorf6c47e92013-08-08 07:16:01 -070038
David Reidef0fcf22013-11-06 11:12:45 -080039 >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, mode
40 >>> cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend)
Paul Kehrer3e0895c2013-10-21 22:19:29 -050041 >>> encryptor = cipher.encryptor()
42 >>> ct = encryptor.update(b"a secret message") + encryptor.finalize()
43 >>> decryptor = cipher.decryptor()
44 >>> decryptor.update(ct) + decryptor.finalize()
Paul Kehrerf6cf9562013-10-22 10:36:00 -050045 'a secret message'
Alex Gaynorf6c47e92013-08-08 07:16:01 -070046
Paul Kehrer051099e2013-11-06 15:53:40 +080047 :param algorithms: One of the algorithms described below.
Alex Gaynore62aa402013-08-08 15:23:11 -070048 :param mode: One of the modes described below.
Alex Gaynor0ca7fdb2013-08-08 07:35:26 -070049
Paul Kehrer5399fd02013-10-21 23:48:25 -050050 .. method:: encryptor()
Alex Gaynor09515f02013-08-08 15:26:55 -070051
David Reid63ba6652013-10-22 14:09:19 -070052 :return: An encrypting
Donald Stufftf04317a2013-10-27 16:44:30 -040053 :class:`~cryptography.hazmat.primitives.interfaces.CipherContext`
David Reid63ba6652013-10-22 14:09:19 -070054 provider.
Alex Gaynore62aa402013-08-08 15:23:11 -070055
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070056 If the backend doesn't support the requested combination of ``cipher``
Alex Gaynor3949f112013-11-02 16:57:10 -070057 and ``mode`` an :class:`cryptography.exceptions.UnsupportedAlgorithm`
58 will be raised.
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070059
Paul Kehrer5399fd02013-10-21 23:48:25 -050060 .. method:: decryptor()
61
David Reid63ba6652013-10-22 14:09:19 -070062 :return: A decrypting
Donald Stufftf04317a2013-10-27 16:44:30 -040063 :class:`~cryptography.hazmat.primitives.interfaces.CipherContext`
David Reid63ba6652013-10-22 14:09:19 -070064 provider.
Paul Kehrer5399fd02013-10-21 23:48:25 -050065
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070066 If the backend doesn't support the requested combination of ``cipher``
Alex Gaynor3949f112013-11-02 16:57:10 -070067 and ``mode`` an :class:`cryptography.exceptions.UnsupportedAlgorithm`
68 will be raised.
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070069
70
Donald Stufftf04317a2013-10-27 16:44:30 -040071.. currentmodule:: cryptography.hazmat.primitives.interfaces
David Reid1f3d7182013-10-22 16:55:18 -070072
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070073.. class:: CipherContext
Paul Kehrer5399fd02013-10-21 23:48:25 -050074
Paul Kehrer051099e2013-11-06 15:53:40 +080075 When calling ``encryptor()`` or ``decryptor()`` on a ``Cipher`` object
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070076 you will receive a return object conforming to the ``CipherContext``
77 interface. You can then call ``update(data)`` with data until you have fed
78 everything into the context. Once that is done call ``finalize()`` to
79 finish the operation and obtain the remainder of the data.
Paul Kehrer5399fd02013-10-21 23:48:25 -050080
81 .. method:: update(data)
82
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070083 :param bytes data: The data you wish to pass into the context.
Paul Kehrer5399fd02013-10-21 23:48:25 -050084 :return bytes: Returns the data that was encrypted or decrypted.
Alex Gaynor34511c62013-11-13 13:30:30 -080085 :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize`
Alex Gaynore62aa402013-08-08 15:23:11 -070086
Paul Kehrer051099e2013-11-06 15:53:40 +080087 When the ``Cipher`` was constructed in a mode that turns it into a
Alex Gaynorfc09a7c2013-11-01 14:43:02 -070088 stream cipher (e.g.
Paul Kehrer051099e2013-11-06 15:53:40 +080089 :class:`cryptography.hazmat.primitives.ciphers.modes.CTR`), this will
Alex Gaynorbf2de742013-11-01 14:48:19 -070090 return bytes immediately, however in other modes it will return chunks,
91 whose size is determined by the cipher's block size.
Alex Gaynord1f02012013-11-01 14:12:35 -070092
Alex Gaynore62aa402013-08-08 15:23:11 -070093 .. method:: finalize()
94
Paul Kehrer5399fd02013-10-21 23:48:25 -050095 :return bytes: Returns the remainder of the data.
Alex Gaynord96d1002013-08-08 07:37:26 -070096
Alex Gaynor34511c62013-11-13 13:30:30 -080097 Once ``finalize`` is called this object can no longer be used and
Alex Gaynor9b70ba32013-11-13 13:49:43 -080098 :meth:`update` and :meth:`finalize` will raise
Alex Gaynor34511c62013-11-13 13:30:30 -080099 :class:`~cryptography.exceptions.AlreadyFinalized`.
100
Paul Kehrer051099e2013-11-06 15:53:40 +0800101Algorithms
102~~~~~~~~~~
Alex Gaynord96d1002013-08-08 07:37:26 -0700103
Paul Kehrer051099e2013-11-06 15:53:40 +0800104.. currentmodule:: cryptography.hazmat.primitives.ciphers.algorithms
David Reid1f3d7182013-10-22 16:55:18 -0700105
106.. class:: AES(key)
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -0700107
Alex Gaynor1e3f81f2013-08-08 11:31:43 -0700108 AES (Advanced Encryption Standard) is a block cipher standardized by NIST.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -0700109 AES is both fast, and cryptographically strong. It is a good default
110 choice for encryption.
111
112 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
Alex Gaynor48ec9a32013-08-08 11:13:46 -0700113 This must be kept secret.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -0700114
David Reid1f3d7182013-10-22 16:55:18 -0700115.. class:: Camellia(key)
Paul Kehrerdff22d42013-09-27 13:43:06 -0500116
117 Camellia is a block cipher approved for use by CRYPTREC and ISO/IEC.
118 It is considered to have comparable security and performance to AES, but
119 is not as widely studied or deployed.
120
121 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
122 This must be kept secret.
123
Alex Gaynord96d1002013-08-08 07:37:26 -0700124
David Reid1f3d7182013-10-22 16:55:18 -0700125.. class:: TripleDES(key)
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700126
Alex Gaynor9316f4c2013-11-15 16:38:42 -0800127 Triple DES (Data Encryption Standard), sometimes referred to as 3DES, is a
128 block cipher standardized by NIST. Triple DES has known crypto-analytic
Alex Gaynor17adce62013-10-16 17:04:40 -0700129 flaws, however none of them currently enable a practical attack.
Alex Gaynor9316f4c2013-11-15 16:38:42 -0800130 Nonetheless, Triples DES is not recommended for new applications because it
Alex Gaynorfbcc5642013-10-22 08:26:00 -0700131 is incredibly slow; old applications should consider moving away from it.
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700132
133 :param bytes key: The secret key, either ``64``, ``128``, or ``192`` bits
134 (note that DES functionally uses ``56``, ``112``, or
135 ``168`` bits of the key, there is a parity byte in each
136 component of the key), in some materials these are
137 referred to as being up to three separate keys (each
138 ``56`` bits long), they can simply be concatenated to
139 produce the full key. This must be kept secret.
140
Paul Kehrer6022d452013-10-30 17:03:54 -0500141.. class:: CAST5(key)
142
143 CAST5 (also known as CAST-128) is a block cipher approved for use in the
144 Canadian government by their Communications Security Establishment. It is a
145 variable key length cipher and supports keys from 40-128 bits in length.
146
147 :param bytes key: The secret key, 40-128 bits in length (in increments of
148 8). This must be kept secret.
149
Paul Kehrer3446d812013-10-31 17:15:03 -0500150Weak Ciphers
151------------
152
153.. warning::
154
155 These ciphers are considered weak for a variety of reasons. New
156 applications should avoid their use and existing applications should
157 strongly consider migrating away.
158
Paul Kehrer5df0abe2013-10-30 16:57:04 -0500159.. class:: Blowfish(key)
160
161 Blowfish is a block cipher developed by Bruce Schneier. It is known to be
162 susceptible to attacks when using weak keys. The author has recommended
Alex Gaynorab5f0112013-11-08 10:34:00 -0800163 that users of Blowfish move to newer algorithms, such as :class:`AES`.
Paul Kehrer5df0abe2013-10-30 16:57:04 -0500164
165 :param bytes key: The secret key, 32-448 bits in length (in increments of
166 8). This must be kept secret.
167
Paul Kehrer4da28c32013-11-07 07:50:17 +0800168.. class:: ARC4(key)
169
170 ARC4 (Alleged RC4) is a stream cipher with serious weaknesses in its
171 initial stream output. Its use is strongly discouraged. ARC4 does not use
172 mode constructions.
173
174 :param bytes key: The secret key, ``40``, ``56``, ``64``, ``80``, ``128``,
175 ``192``, or ``256`` bits in length. This must be kept
176 secret.
177
Paul Kehrer0994c562013-11-10 03:19:14 +0800178 .. doctest::
179
180 >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
181 >>> algorithm = algorithms.ARC4(key)
182 >>> cipher = Cipher(algorithm, mode=None)
183 >>> encryptor = cipher.encryptor()
184 >>> ct = encryptor.update(b"a secret message")
185 >>> decryptor = cipher.decryptor()
186 >>> decryptor.update(ct)
187 'a secret message'
188
David Reid30722b92013-11-07 13:03:39 -0800189
190.. _symmetric-encryption-modes:
191
Alex Gaynord96d1002013-08-08 07:37:26 -0700192Modes
193~~~~~
194
Paul Kehrer051099e2013-11-06 15:53:40 +0800195.. currentmodule:: cryptography.hazmat.primitives.ciphers.modes
David Reid1f3d7182013-10-22 16:55:18 -0700196
197.. class:: CBC(initialization_vector)
Alex Gaynor48ec9a32013-08-08 11:13:46 -0700198
199 CBC (Cipher block chaining) is a mode of operation for block ciphers. It is
200 considered cryptographically strong.
201
202 :param bytes initialization_vector: Must be random bytes. They do not need
203 to be kept secret (they can be included
Alex Gaynor2dc2b862013-08-08 11:58:04 -0700204 in a transmitted message). Must be the
205 same number of bytes as the
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800206 ``block_size`` of the cipher. Each time
Alex Gaynor9de452d2013-11-07 13:28:23 -0800207 something is encrypted a new
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800208 ``initialization_vector`` should be
209 generated. Do not reuse an
210 ``initialization_vector`` with
211 a given ``key``, and particularly do
212 not use a constant
213 ``initialization_vector``.
214
215 A good construction looks like:
216
217 .. code-block:: pycon
218
219 >>> import os
220 >>> iv = os.urandom(16)
221 >>> mode = CBC(iv)
222
223 While the following is bad and will leak information:
224
225 .. code-block:: pycon
226
227 >>> iv = "a" * 16
228 >>> mode = CBC(iv)
Paul Kehrer13f108f2013-09-09 21:41:03 -0500229
Paul Kehrer45064282013-10-17 13:41:53 -0500230
David Reid1f3d7182013-10-22 16:55:18 -0700231.. class:: CTR(nonce)
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500232
Paul Kehrer45064282013-10-17 13:41:53 -0500233 .. warning::
234
235 Counter mode is not recommended for use with block ciphers that have a
236 block size of less than 128-bits.
237
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500238 CTR (Counter) is a mode of operation for block ciphers. It is considered
Alex Gaynord1f02012013-11-01 14:12:35 -0700239 cryptographically strong. It transforms a block cipher into a stream
240 cipher.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500241
Paul Kehrer89b3dd32013-10-17 14:02:45 -0500242 :param bytes nonce: Should be random bytes. It is critical to never reuse a
243 ``nonce`` with a given key. Any reuse of a nonce
244 with the same key compromises the security of every
245 message encrypted with that key. Must be the same
246 number of bytes as the ``block_size`` of the cipher
247 with a given key. The nonce does not need to be kept
248 secret and may be included alongside the ciphertext.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500249
David Reid1f3d7182013-10-22 16:55:18 -0700250.. class:: OFB(initialization_vector)
Paul Kehrer6f412a02013-09-10 21:30:50 -0500251
252 OFB (Output Feedback) is a mode of operation for block ciphers. It
253 transforms a block cipher into a stream cipher.
254
David Reidf1a39bd2013-09-11 16:28:42 -0700255 :param bytes initialization_vector: Must be random bytes. They do not need
256 to be kept secret (they can be included
257 in a transmitted message). Must be the
258 same number of bytes as the
259 ``block_size`` of the cipher. Do not
260 reuse an ``initialization_vector`` with
261 a given ``key``.
Paul Kehrer6f412a02013-09-10 21:30:50 -0500262
David Reid1f3d7182013-10-22 16:55:18 -0700263.. class:: CFB(initialization_vector)
Paul Kehrer4223df72013-09-11 09:48:04 -0500264
265 CFB (Cipher Feedback) is a mode of operation for block ciphers. It
266 transforms a block cipher into a stream cipher.
267
268 :param bytes initialization_vector: Must be random bytes. They do not need
269 to be kept secret (they can be included
270 in a transmitted message). Must be the
271 same number of bytes as the
272 ``block_size`` of the cipher. Do not
273 reuse an ``initialization_vector`` with
274 a given ``key``.
275
Paul Kehrer13f108f2013-09-09 21:41:03 -0500276
277Insecure Modes
278--------------
279
Alex Gaynorcd413a32013-09-10 18:59:43 -0700280.. warning::
281
282 These modes are insecure. New applications should never make use of them,
283 and existing applications should strongly consider migrating away.
284
285
David Reid1f3d7182013-10-22 16:55:18 -0700286.. class:: ECB()
Paul Kehrer13f108f2013-09-09 21:41:03 -0500287
288 ECB (Electronic Code Book) is the simplest mode of operation for block
Alex Gaynorcd413a32013-09-10 18:59:43 -0700289 ciphers. Each block of data is encrypted in the same way. This means
290 identical plaintext blocks will always result in identical ciphertext
291 blocks, and thus result in information leakage
Alex Gaynorab5f0112013-11-08 10:34:00 -0800292
293
294.. _`described by Colin Percival`: http://www.daemonology.net/blog/2009-06-11-cryptographic-right-answers.html