blob: 4b37d396f2ff6cb66ffb74f8a543d7c5f395f157 [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
17where the encrypter and decrypter both use the same key.
18
Paul Kehrer051099e2013-11-06 15:53:40 +080019.. class:: Cipher(algorithm, mode)
Alex Gaynorf6c47e92013-08-08 07:16:01 -070020
Paul Kehrer051099e2013-11-06 15:53:40 +080021 Cipher objects combine an algorithm (such as AES) with a mode (such as
Paul Kehrerd1afe392013-10-22 08:24:44 -050022 CBC, CTR, or GCM). A simple example of encrypting (and then decrypting)
Paul Kehrer653463f2013-10-21 17:55:01 -050023 content with AES is:
Alex Gaynorf6c47e92013-08-08 07:16:01 -070024
Donald Stufft173de982013-08-12 07:34:39 -040025 .. doctest::
Alex Gaynorf6c47e92013-08-08 07:16:01 -070026
Paul Kehrer051099e2013-11-06 15:53:40 +080027 >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
28 >>> cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
Paul Kehrer3e0895c2013-10-21 22:19:29 -050029 >>> encryptor = cipher.encryptor()
30 >>> ct = encryptor.update(b"a secret message") + encryptor.finalize()
31 >>> decryptor = cipher.decryptor()
32 >>> decryptor.update(ct) + decryptor.finalize()
Paul Kehrerf6cf9562013-10-22 10:36:00 -050033 'a secret message'
Alex Gaynorf6c47e92013-08-08 07:16:01 -070034
Paul Kehrer051099e2013-11-06 15:53:40 +080035 :param algorithms: One of the algorithms described below.
Alex Gaynore62aa402013-08-08 15:23:11 -070036 :param mode: One of the modes described below.
Alex Gaynor0ca7fdb2013-08-08 07:35:26 -070037
Paul Kehrer5399fd02013-10-21 23:48:25 -050038 .. method:: encryptor()
Alex Gaynor09515f02013-08-08 15:26:55 -070039
David Reid63ba6652013-10-22 14:09:19 -070040 :return: An encrypting
Donald Stufftf04317a2013-10-27 16:44:30 -040041 :class:`~cryptography.hazmat.primitives.interfaces.CipherContext`
David Reid63ba6652013-10-22 14:09:19 -070042 provider.
Alex Gaynore62aa402013-08-08 15:23:11 -070043
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070044 If the backend doesn't support the requested combination of ``cipher``
Alex Gaynor3949f112013-11-02 16:57:10 -070045 and ``mode`` an :class:`cryptography.exceptions.UnsupportedAlgorithm`
46 will be raised.
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070047
Paul Kehrer5399fd02013-10-21 23:48:25 -050048 .. method:: decryptor()
49
David Reid63ba6652013-10-22 14:09:19 -070050 :return: A decrypting
Donald Stufftf04317a2013-10-27 16:44:30 -040051 :class:`~cryptography.hazmat.primitives.interfaces.CipherContext`
David Reid63ba6652013-10-22 14:09:19 -070052 provider.
Paul Kehrer5399fd02013-10-21 23:48:25 -050053
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070054 If the backend doesn't support the requested combination of ``cipher``
Alex Gaynor3949f112013-11-02 16:57:10 -070055 and ``mode`` an :class:`cryptography.exceptions.UnsupportedAlgorithm`
56 will be raised.
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070057
58
Donald Stufftf04317a2013-10-27 16:44:30 -040059.. currentmodule:: cryptography.hazmat.primitives.interfaces
David Reid1f3d7182013-10-22 16:55:18 -070060
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070061.. class:: CipherContext
Paul Kehrer5399fd02013-10-21 23:48:25 -050062
Paul Kehrer051099e2013-11-06 15:53:40 +080063 When calling ``encryptor()`` or ``decryptor()`` on a ``Cipher`` object
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070064 you will receive a return object conforming to the ``CipherContext``
65 interface. You can then call ``update(data)`` with data until you have fed
66 everything into the context. Once that is done call ``finalize()`` to
67 finish the operation and obtain the remainder of the data.
Paul Kehrer5399fd02013-10-21 23:48:25 -050068
69 .. method:: update(data)
70
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070071 :param bytes data: The data you wish to pass into the context.
Paul Kehrer5399fd02013-10-21 23:48:25 -050072 :return bytes: Returns the data that was encrypted or decrypted.
Alex Gaynore62aa402013-08-08 15:23:11 -070073
Paul Kehrer051099e2013-11-06 15:53:40 +080074 When the ``Cipher`` was constructed in a mode that turns it into a
Alex Gaynorfc09a7c2013-11-01 14:43:02 -070075 stream cipher (e.g.
Paul Kehrer051099e2013-11-06 15:53:40 +080076 :class:`cryptography.hazmat.primitives.ciphers.modes.CTR`), this will
Alex Gaynorbf2de742013-11-01 14:48:19 -070077 return bytes immediately, however in other modes it will return chunks,
78 whose size is determined by the cipher's block size.
Alex Gaynord1f02012013-11-01 14:12:35 -070079
Alex Gaynore62aa402013-08-08 15:23:11 -070080 .. method:: finalize()
81
Paul Kehrer5399fd02013-10-21 23:48:25 -050082 :return bytes: Returns the remainder of the data.
Alex Gaynord96d1002013-08-08 07:37:26 -070083
Paul Kehrer051099e2013-11-06 15:53:40 +080084Algorithms
85~~~~~~~~~~
Alex Gaynord96d1002013-08-08 07:37:26 -070086
Paul Kehrer051099e2013-11-06 15:53:40 +080087.. currentmodule:: cryptography.hazmat.primitives.ciphers.algorithms
David Reid1f3d7182013-10-22 16:55:18 -070088
89.. class:: AES(key)
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070090
Alex Gaynor1e3f81f2013-08-08 11:31:43 -070091 AES (Advanced Encryption Standard) is a block cipher standardized by NIST.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070092 AES is both fast, and cryptographically strong. It is a good default
93 choice for encryption.
94
95 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
Alex Gaynor48ec9a32013-08-08 11:13:46 -070096 This must be kept secret.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070097
David Reid1f3d7182013-10-22 16:55:18 -070098.. class:: Camellia(key)
Paul Kehrerdff22d42013-09-27 13:43:06 -050099
100 Camellia is a block cipher approved for use by CRYPTREC and ISO/IEC.
101 It is considered to have comparable security and performance to AES, but
102 is not as widely studied or deployed.
103
104 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
105 This must be kept secret.
106
Alex Gaynord96d1002013-08-08 07:37:26 -0700107
David Reid1f3d7182013-10-22 16:55:18 -0700108.. class:: TripleDES(key)
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700109
Alex Gaynor2f355d12013-09-09 18:09:26 -0700110 Triple DES (Data Encryption Standard), sometimes refered to as 3DES, is a
Alex Gaynor17adce62013-10-16 17:04:40 -0700111 block cipher standardized by NIST. Triple DES has known cryptoanalytic
112 flaws, however none of them currently enable a practical attack.
113 Nonetheless, Triples DES is not reccomended for new applications because it
Alex Gaynorfbcc5642013-10-22 08:26:00 -0700114 is incredibly slow; old applications should consider moving away from it.
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700115
116 :param bytes key: The secret key, either ``64``, ``128``, or ``192`` bits
117 (note that DES functionally uses ``56``, ``112``, or
118 ``168`` bits of the key, there is a parity byte in each
119 component of the key), in some materials these are
120 referred to as being up to three separate keys (each
121 ``56`` bits long), they can simply be concatenated to
122 produce the full key. This must be kept secret.
123
Paul Kehrer6022d452013-10-30 17:03:54 -0500124.. class:: CAST5(key)
125
126 CAST5 (also known as CAST-128) is a block cipher approved for use in the
127 Canadian government by their Communications Security Establishment. It is a
128 variable key length cipher and supports keys from 40-128 bits in length.
129
130 :param bytes key: The secret key, 40-128 bits in length (in increments of
131 8). This must be kept secret.
132
Paul Kehrer3446d812013-10-31 17:15:03 -0500133Weak Ciphers
134------------
135
136.. warning::
137
138 These ciphers are considered weak for a variety of reasons. New
139 applications should avoid their use and existing applications should
140 strongly consider migrating away.
141
Paul Kehrer5df0abe2013-10-30 16:57:04 -0500142.. class:: Blowfish(key)
143
144 Blowfish is a block cipher developed by Bruce Schneier. It is known to be
145 susceptible to attacks when using weak keys. The author has recommended
Paul Kehrer3446d812013-10-31 17:15:03 -0500146 that users of Blowfish move to newer algorithms like
147 :class:`AES`.
Paul Kehrer5df0abe2013-10-30 16:57:04 -0500148
149 :param bytes key: The secret key, 32-448 bits in length (in increments of
150 8). This must be kept secret.
151
David Reid30722b92013-11-07 13:03:39 -0800152
153.. _symmetric-encryption-modes:
154
Alex Gaynord96d1002013-08-08 07:37:26 -0700155Modes
156~~~~~
157
Paul Kehrer051099e2013-11-06 15:53:40 +0800158.. currentmodule:: cryptography.hazmat.primitives.ciphers.modes
David Reid1f3d7182013-10-22 16:55:18 -0700159
160.. class:: CBC(initialization_vector)
Alex Gaynor48ec9a32013-08-08 11:13:46 -0700161
162 CBC (Cipher block chaining) is a mode of operation for block ciphers. It is
163 considered cryptographically strong.
164
165 :param bytes initialization_vector: Must be random bytes. They do not need
166 to be kept secret (they can be included
Alex Gaynor2dc2b862013-08-08 11:58:04 -0700167 in a transmitted message). Must be the
168 same number of bytes as the
Alex Gaynor6badd9b2013-08-08 14:59:53 -0700169 ``block_size`` of the cipher. Do not
170 reuse an ``initialization_vector`` with
171 a given ``key``.
Paul Kehrer13f108f2013-09-09 21:41:03 -0500172
Paul Kehrer45064282013-10-17 13:41:53 -0500173
David Reid1f3d7182013-10-22 16:55:18 -0700174.. class:: CTR(nonce)
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500175
Paul Kehrer45064282013-10-17 13:41:53 -0500176 .. warning::
177
178 Counter mode is not recommended for use with block ciphers that have a
179 block size of less than 128-bits.
180
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500181 CTR (Counter) is a mode of operation for block ciphers. It is considered
Alex Gaynord1f02012013-11-01 14:12:35 -0700182 cryptographically strong. It transforms a block cipher into a stream
183 cipher.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500184
Paul Kehrer89b3dd32013-10-17 14:02:45 -0500185 :param bytes nonce: Should be random bytes. It is critical to never reuse a
186 ``nonce`` with a given key. Any reuse of a nonce
187 with the same key compromises the security of every
188 message encrypted with that key. Must be the same
189 number of bytes as the ``block_size`` of the cipher
190 with a given key. The nonce does not need to be kept
191 secret and may be included alongside the ciphertext.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500192
David Reid1f3d7182013-10-22 16:55:18 -0700193.. class:: OFB(initialization_vector)
Paul Kehrer6f412a02013-09-10 21:30:50 -0500194
195 OFB (Output Feedback) is a mode of operation for block ciphers. It
196 transforms a block cipher into a stream cipher.
197
David Reidf1a39bd2013-09-11 16:28:42 -0700198 :param bytes initialization_vector: Must be random bytes. They do not need
199 to be kept secret (they can be included
200 in a transmitted message). Must be the
201 same number of bytes as the
202 ``block_size`` of the cipher. Do not
203 reuse an ``initialization_vector`` with
204 a given ``key``.
Paul Kehrer6f412a02013-09-10 21:30:50 -0500205
David Reid1f3d7182013-10-22 16:55:18 -0700206.. class:: CFB(initialization_vector)
Paul Kehrer4223df72013-09-11 09:48:04 -0500207
208 CFB (Cipher Feedback) is a mode of operation for block ciphers. It
209 transforms a block cipher into a stream cipher.
210
211 :param bytes initialization_vector: Must be random bytes. They do not need
212 to be kept secret (they can be included
213 in a transmitted message). Must be the
214 same number of bytes as the
215 ``block_size`` of the cipher. Do not
216 reuse an ``initialization_vector`` with
217 a given ``key``.
218
Paul Kehrer13f108f2013-09-09 21:41:03 -0500219
220Insecure Modes
221--------------
222
Alex Gaynorcd413a32013-09-10 18:59:43 -0700223.. warning::
224
225 These modes are insecure. New applications should never make use of them,
226 and existing applications should strongly consider migrating away.
227
228
David Reid1f3d7182013-10-22 16:55:18 -0700229.. class:: ECB()
Paul Kehrer13f108f2013-09-09 21:41:03 -0500230
231 ECB (Electronic Code Book) is the simplest mode of operation for block
Alex Gaynorcd413a32013-09-10 18:59:43 -0700232 ciphers. Each block of data is encrypted in the same way. This means
233 identical plaintext blocks will always result in identical ciphertext
234 blocks, and thus result in information leakage