blob: 5542e832ab49c0ba62cc86dc822c7e053badc728 [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
15
Alex Gaynorf6c47e92013-08-08 07:16:01 -070016Symmetric encryption is a way to encrypt (hide the plaintext value) material
Alex Gaynorab5f0112013-11-08 10:34:00 -080017where the encrypter and decrypter both use the same key. Note that symmetric
18encryption 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).
21For this reason it is *strongly* reccomended to combine encryption with a
22message 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
Paul Kehrer051099e2013-11-06 15:53:40 +080025.. class:: Cipher(algorithm, mode)
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
37 >>> cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
Paul Kehrer3e0895c2013-10-21 22:19:29 -050038 >>> encryptor = cipher.encryptor()
39 >>> ct = encryptor.update(b"a secret message") + encryptor.finalize()
40 >>> decryptor = cipher.decryptor()
41 >>> decryptor.update(ct) + decryptor.finalize()
Paul Kehrerf6cf9562013-10-22 10:36:00 -050042 'a secret message'
Alex Gaynorf6c47e92013-08-08 07:16:01 -070043
Paul Kehrer051099e2013-11-06 15:53:40 +080044 :param algorithms: One of the algorithms described below.
Alex Gaynore62aa402013-08-08 15:23:11 -070045 :param mode: One of the modes described below.
Alex Gaynor0ca7fdb2013-08-08 07:35:26 -070046
Paul Kehrer5399fd02013-10-21 23:48:25 -050047 .. method:: encryptor()
Alex Gaynor09515f02013-08-08 15:26:55 -070048
David Reid63ba6652013-10-22 14:09:19 -070049 :return: An encrypting
Donald Stufftf04317a2013-10-27 16:44:30 -040050 :class:`~cryptography.hazmat.primitives.interfaces.CipherContext`
David Reid63ba6652013-10-22 14:09:19 -070051 provider.
Alex Gaynore62aa402013-08-08 15:23:11 -070052
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070053 If the backend doesn't support the requested combination of ``cipher``
Alex Gaynor3949f112013-11-02 16:57:10 -070054 and ``mode`` an :class:`cryptography.exceptions.UnsupportedAlgorithm`
55 will be raised.
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070056
Paul Kehrer5399fd02013-10-21 23:48:25 -050057 .. method:: decryptor()
58
David Reid63ba6652013-10-22 14:09:19 -070059 :return: A decrypting
Donald Stufftf04317a2013-10-27 16:44:30 -040060 :class:`~cryptography.hazmat.primitives.interfaces.CipherContext`
David Reid63ba6652013-10-22 14:09:19 -070061 provider.
Paul Kehrer5399fd02013-10-21 23:48:25 -050062
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070063 If the backend doesn't support the requested combination of ``cipher``
Alex Gaynor3949f112013-11-02 16:57:10 -070064 and ``mode`` an :class:`cryptography.exceptions.UnsupportedAlgorithm`
65 will be raised.
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070066
67
Donald Stufftf04317a2013-10-27 16:44:30 -040068.. currentmodule:: cryptography.hazmat.primitives.interfaces
David Reid1f3d7182013-10-22 16:55:18 -070069
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070070.. class:: CipherContext
Paul Kehrer5399fd02013-10-21 23:48:25 -050071
Paul Kehrer051099e2013-11-06 15:53:40 +080072 When calling ``encryptor()`` or ``decryptor()`` on a ``Cipher`` object
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070073 you will receive a return object conforming to the ``CipherContext``
74 interface. You can then call ``update(data)`` with data until you have fed
75 everything into the context. Once that is done call ``finalize()`` to
76 finish the operation and obtain the remainder of the data.
Paul Kehrer5399fd02013-10-21 23:48:25 -050077
78 .. method:: update(data)
79
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070080 :param bytes data: The data you wish to pass into the context.
Paul Kehrer5399fd02013-10-21 23:48:25 -050081 :return bytes: Returns the data that was encrypted or decrypted.
Alex Gaynore62aa402013-08-08 15:23:11 -070082
Paul Kehrer051099e2013-11-06 15:53:40 +080083 When the ``Cipher`` was constructed in a mode that turns it into a
Alex Gaynorfc09a7c2013-11-01 14:43:02 -070084 stream cipher (e.g.
Paul Kehrer051099e2013-11-06 15:53:40 +080085 :class:`cryptography.hazmat.primitives.ciphers.modes.CTR`), this will
Alex Gaynorbf2de742013-11-01 14:48:19 -070086 return bytes immediately, however in other modes it will return chunks,
87 whose size is determined by the cipher's block size.
Alex Gaynord1f02012013-11-01 14:12:35 -070088
Alex Gaynore62aa402013-08-08 15:23:11 -070089 .. method:: finalize()
90
Paul Kehrer5399fd02013-10-21 23:48:25 -050091 :return bytes: Returns the remainder of the data.
Alex Gaynord96d1002013-08-08 07:37:26 -070092
Paul Kehrer051099e2013-11-06 15:53:40 +080093Algorithms
94~~~~~~~~~~
Alex Gaynord96d1002013-08-08 07:37:26 -070095
Paul Kehrer051099e2013-11-06 15:53:40 +080096.. currentmodule:: cryptography.hazmat.primitives.ciphers.algorithms
David Reid1f3d7182013-10-22 16:55:18 -070097
98.. class:: AES(key)
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070099
Alex Gaynor1e3f81f2013-08-08 11:31:43 -0700100 AES (Advanced Encryption Standard) is a block cipher standardized by NIST.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -0700101 AES is both fast, and cryptographically strong. It is a good default
102 choice for encryption.
103
104 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
Alex Gaynor48ec9a32013-08-08 11:13:46 -0700105 This must be kept secret.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -0700106
David Reid1f3d7182013-10-22 16:55:18 -0700107.. class:: Camellia(key)
Paul Kehrerdff22d42013-09-27 13:43:06 -0500108
109 Camellia is a block cipher approved for use by CRYPTREC and ISO/IEC.
110 It is considered to have comparable security and performance to AES, but
111 is not as widely studied or deployed.
112
113 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
114 This must be kept secret.
115
Alex Gaynord96d1002013-08-08 07:37:26 -0700116
David Reid1f3d7182013-10-22 16:55:18 -0700117.. class:: TripleDES(key)
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700118
Alex Gaynor2f355d12013-09-09 18:09:26 -0700119 Triple DES (Data Encryption Standard), sometimes refered to as 3DES, is a
Alex Gaynor17adce62013-10-16 17:04:40 -0700120 block cipher standardized by NIST. Triple DES has known cryptoanalytic
121 flaws, however none of them currently enable a practical attack.
122 Nonetheless, Triples DES is not reccomended for new applications because it
Alex Gaynorfbcc5642013-10-22 08:26:00 -0700123 is incredibly slow; old applications should consider moving away from it.
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700124
125 :param bytes key: The secret key, either ``64``, ``128``, or ``192`` bits
126 (note that DES functionally uses ``56``, ``112``, or
127 ``168`` bits of the key, there is a parity byte in each
128 component of the key), in some materials these are
129 referred to as being up to three separate keys (each
130 ``56`` bits long), they can simply be concatenated to
131 produce the full key. This must be kept secret.
132
Paul Kehrer6022d452013-10-30 17:03:54 -0500133.. class:: CAST5(key)
134
135 CAST5 (also known as CAST-128) is a block cipher approved for use in the
136 Canadian government by their Communications Security Establishment. It is a
137 variable key length cipher and supports keys from 40-128 bits in length.
138
139 :param bytes key: The secret key, 40-128 bits in length (in increments of
140 8). This must be kept secret.
141
Paul Kehrer3446d812013-10-31 17:15:03 -0500142Weak Ciphers
143------------
144
145.. warning::
146
147 These ciphers are considered weak for a variety of reasons. New
148 applications should avoid their use and existing applications should
149 strongly consider migrating away.
150
Paul Kehrer5df0abe2013-10-30 16:57:04 -0500151.. class:: Blowfish(key)
152
153 Blowfish is a block cipher developed by Bruce Schneier. It is known to be
154 susceptible to attacks when using weak keys. The author has recommended
Alex Gaynorab5f0112013-11-08 10:34:00 -0800155 that users of Blowfish move to newer algorithms, such as :class:`AES`.
Paul Kehrer5df0abe2013-10-30 16:57:04 -0500156
157 :param bytes key: The secret key, 32-448 bits in length (in increments of
158 8). This must be kept secret.
159
David Reid30722b92013-11-07 13:03:39 -0800160
161.. _symmetric-encryption-modes:
162
Alex Gaynord96d1002013-08-08 07:37:26 -0700163Modes
164~~~~~
165
Paul Kehrer051099e2013-11-06 15:53:40 +0800166.. currentmodule:: cryptography.hazmat.primitives.ciphers.modes
David Reid1f3d7182013-10-22 16:55:18 -0700167
168.. class:: CBC(initialization_vector)
Alex Gaynor48ec9a32013-08-08 11:13:46 -0700169
170 CBC (Cipher block chaining) is a mode of operation for block ciphers. It is
171 considered cryptographically strong.
172
173 :param bytes initialization_vector: Must be random bytes. They do not need
174 to be kept secret (they can be included
Alex Gaynor2dc2b862013-08-08 11:58:04 -0700175 in a transmitted message). Must be the
176 same number of bytes as the
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800177 ``block_size`` of the cipher. Each time
Alex Gaynor9de452d2013-11-07 13:28:23 -0800178 something is encrypted a new
Alex Gaynor8ed651e2013-11-07 13:24:31 -0800179 ``initialization_vector`` should be
180 generated. Do not reuse an
181 ``initialization_vector`` with
182 a given ``key``, and particularly do
183 not use a constant
184 ``initialization_vector``.
185
186 A good construction looks like:
187
188 .. code-block:: pycon
189
190 >>> import os
191 >>> iv = os.urandom(16)
192 >>> mode = CBC(iv)
193
194 While the following is bad and will leak information:
195
196 .. code-block:: pycon
197
198 >>> iv = "a" * 16
199 >>> mode = CBC(iv)
Paul Kehrer13f108f2013-09-09 21:41:03 -0500200
Paul Kehrer45064282013-10-17 13:41:53 -0500201
David Reid1f3d7182013-10-22 16:55:18 -0700202.. class:: CTR(nonce)
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500203
Paul Kehrer45064282013-10-17 13:41:53 -0500204 .. warning::
205
206 Counter mode is not recommended for use with block ciphers that have a
207 block size of less than 128-bits.
208
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500209 CTR (Counter) is a mode of operation for block ciphers. It is considered
Alex Gaynord1f02012013-11-01 14:12:35 -0700210 cryptographically strong. It transforms a block cipher into a stream
211 cipher.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500212
Paul Kehrer89b3dd32013-10-17 14:02:45 -0500213 :param bytes nonce: Should be random bytes. It is critical to never reuse a
214 ``nonce`` with a given key. Any reuse of a nonce
215 with the same key compromises the security of every
216 message encrypted with that key. Must be the same
217 number of bytes as the ``block_size`` of the cipher
218 with a given key. The nonce does not need to be kept
219 secret and may be included alongside the ciphertext.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500220
David Reid1f3d7182013-10-22 16:55:18 -0700221.. class:: OFB(initialization_vector)
Paul Kehrer6f412a02013-09-10 21:30:50 -0500222
223 OFB (Output Feedback) is a mode of operation for block ciphers. It
224 transforms a block cipher into a stream cipher.
225
David Reidf1a39bd2013-09-11 16:28:42 -0700226 :param bytes initialization_vector: Must be random bytes. They do not need
227 to be kept secret (they can be included
228 in a transmitted message). Must be the
229 same number of bytes as the
230 ``block_size`` of the cipher. Do not
231 reuse an ``initialization_vector`` with
232 a given ``key``.
Paul Kehrer6f412a02013-09-10 21:30:50 -0500233
David Reid1f3d7182013-10-22 16:55:18 -0700234.. class:: CFB(initialization_vector)
Paul Kehrer4223df72013-09-11 09:48:04 -0500235
236 CFB (Cipher Feedback) is a mode of operation for block ciphers. It
237 transforms a block cipher into a stream cipher.
238
239 :param bytes initialization_vector: Must be random bytes. They do not need
240 to be kept secret (they can be included
241 in a transmitted message). Must be the
242 same number of bytes as the
243 ``block_size`` of the cipher. Do not
244 reuse an ``initialization_vector`` with
245 a given ``key``.
246
Paul Kehrer13f108f2013-09-09 21:41:03 -0500247
248Insecure Modes
249--------------
250
Alex Gaynorcd413a32013-09-10 18:59:43 -0700251.. warning::
252
253 These modes are insecure. New applications should never make use of them,
254 and existing applications should strongly consider migrating away.
255
256
David Reid1f3d7182013-10-22 16:55:18 -0700257.. class:: ECB()
Paul Kehrer13f108f2013-09-09 21:41:03 -0500258
259 ECB (Electronic Code Book) is the simplest mode of operation for block
Alex Gaynorcd413a32013-09-10 18:59:43 -0700260 ciphers. Each block of data is encrypted in the same way. This means
261 identical plaintext blocks will always result in identical ciphertext
262 blocks, and thus result in information leakage
Alex Gaynorab5f0112013-11-08 10:34:00 -0800263
264
265.. _`described by Colin Percival`: http://www.daemonology.net/blog/2009-06-11-cryptographic-right-answers.html