blob: c1c8d2471c7d6e76ad0951d9b3a40714b9883428 [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
Donald Stufftf04317a2013-10-27 16:44:30 -04007.. currentmodule:: cryptography.hazmat.primitives.block
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
David Reid1f3d7182013-10-22 16:55:18 -070019.. class:: BlockCipher(cipher, mode)
Alex Gaynorf6c47e92013-08-08 07:16:01 -070020
Alex Gaynor65678d02013-08-08 15:19:19 -070021 Block ciphers work by encrypting content in chunks, often 64- or 128-bits.
Alex Gaynorb12f76e2013-08-08 19:05:18 -070022 They combine an underlying algorithm (such as AES), with a mode (such as
Paul Kehrerd1afe392013-10-22 08:24:44 -050023 CBC, CTR, or GCM). A simple example of encrypting (and then decrypting)
Paul Kehrer653463f2013-10-21 17:55:01 -050024 content with AES is:
Alex Gaynorf6c47e92013-08-08 07:16:01 -070025
Donald Stufft173de982013-08-12 07:34:39 -040026 .. doctest::
Alex Gaynorf6c47e92013-08-08 07:16:01 -070027
Donald Stufftf04317a2013-10-27 16:44:30 -040028 >>> from cryptography.hazmat.primitives.block import BlockCipher, ciphers, modes
Alex Gaynoracc787a2013-08-10 15:52:40 -040029 >>> cipher = BlockCipher(ciphers.AES(key), modes.CBC(iv))
Paul Kehrer3e0895c2013-10-21 22:19:29 -050030 >>> encryptor = cipher.encryptor()
31 >>> ct = encryptor.update(b"a secret message") + encryptor.finalize()
32 >>> decryptor = cipher.decryptor()
33 >>> decryptor.update(ct) + decryptor.finalize()
Paul Kehrerf6cf9562013-10-22 10:36:00 -050034 'a secret message'
Alex Gaynorf6c47e92013-08-08 07:16:01 -070035
Alex Gaynore62aa402013-08-08 15:23:11 -070036 :param cipher: One of the ciphers described below.
37 :param mode: One of the modes described below.
Alex Gaynor0ca7fdb2013-08-08 07:35:26 -070038
Paul Kehrer5399fd02013-10-21 23:48:25 -050039 .. method:: encryptor()
Alex Gaynor09515f02013-08-08 15:26:55 -070040
David Reid63ba6652013-10-22 14:09:19 -070041 :return: An encrypting
Donald Stufftf04317a2013-10-27 16:44:30 -040042 :class:`~cryptography.hazmat.primitives.interfaces.CipherContext`
David Reid63ba6652013-10-22 14:09:19 -070043 provider.
Alex Gaynore62aa402013-08-08 15:23:11 -070044
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070045 If the backend doesn't support the requested combination of ``cipher``
Alex Gaynor3949f112013-11-02 16:57:10 -070046 and ``mode`` an :class:`cryptography.exceptions.UnsupportedAlgorithm`
47 will be raised.
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070048
Paul Kehrer5399fd02013-10-21 23:48:25 -050049 .. method:: decryptor()
50
David Reid63ba6652013-10-22 14:09:19 -070051 :return: A decrypting
Donald Stufftf04317a2013-10-27 16:44:30 -040052 :class:`~cryptography.hazmat.primitives.interfaces.CipherContext`
David Reid63ba6652013-10-22 14:09:19 -070053 provider.
Paul Kehrer5399fd02013-10-21 23:48:25 -050054
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070055 If the backend doesn't support the requested combination of ``cipher``
Alex Gaynor3949f112013-11-02 16:57:10 -070056 and ``mode`` an :class:`cryptography.exceptions.UnsupportedAlgorithm`
57 will be raised.
Alex Gaynorf1a3fc02013-11-02 14:03:34 -070058
59
Donald Stufftf04317a2013-10-27 16:44:30 -040060.. currentmodule:: cryptography.hazmat.primitives.interfaces
David Reid1f3d7182013-10-22 16:55:18 -070061
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070062.. class:: CipherContext
Paul Kehrer5399fd02013-10-21 23:48:25 -050063
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070064 When calling ``encryptor()`` or ``decryptor()`` on a ``BlockCipher`` object
65 you will receive a return object conforming to the ``CipherContext``
66 interface. You can then call ``update(data)`` with data until you have fed
67 everything into the context. Once that is done call ``finalize()`` to
68 finish the operation and obtain the remainder of the data.
Paul Kehrer5399fd02013-10-21 23:48:25 -050069
70 .. method:: update(data)
71
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070072 :param bytes data: The data you wish to pass into the context.
Paul Kehrer5399fd02013-10-21 23:48:25 -050073 :return bytes: Returns the data that was encrypted or decrypted.
Alex Gaynore62aa402013-08-08 15:23:11 -070074
Alex Gaynord1f02012013-11-01 14:12:35 -070075 When the ``BlockCipher`` was constructed in a mode turns it into a
Alex Gaynorfc09a7c2013-11-01 14:43:02 -070076 stream cipher (e.g.
77 :class:`cryptography.hazmat.primitives.block.modes.CTR`), this will
Alex Gaynorbf2de742013-11-01 14:48:19 -070078 return bytes immediately, however in other modes it will return chunks,
79 whose size is determined by the cipher's block size.
Alex Gaynord1f02012013-11-01 14:12:35 -070080
Alex Gaynore62aa402013-08-08 15:23:11 -070081 .. method:: finalize()
82
Paul Kehrer5399fd02013-10-21 23:48:25 -050083 :return bytes: Returns the remainder of the data.
Alex Gaynord96d1002013-08-08 07:37:26 -070084
85Ciphers
86~~~~~~~
87
Donald Stufftf04317a2013-10-27 16:44:30 -040088.. currentmodule:: cryptography.hazmat.primitives.block.ciphers
David Reid1f3d7182013-10-22 16:55:18 -070089
90.. class:: AES(key)
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070091
Alex Gaynor1e3f81f2013-08-08 11:31:43 -070092 AES (Advanced Encryption Standard) is a block cipher standardized by NIST.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070093 AES is both fast, and cryptographically strong. It is a good default
94 choice for encryption.
95
96 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
Alex Gaynor48ec9a32013-08-08 11:13:46 -070097 This must be kept secret.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070098
David Reid1f3d7182013-10-22 16:55:18 -070099.. class:: Camellia(key)
Paul Kehrerdff22d42013-09-27 13:43:06 -0500100
101 Camellia is a block cipher approved for use by CRYPTREC and ISO/IEC.
102 It is considered to have comparable security and performance to AES, but
103 is not as widely studied or deployed.
104
105 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
106 This must be kept secret.
107
Alex Gaynord96d1002013-08-08 07:37:26 -0700108
David Reid1f3d7182013-10-22 16:55:18 -0700109.. class:: TripleDES(key)
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700110
Alex Gaynor2f355d12013-09-09 18:09:26 -0700111 Triple DES (Data Encryption Standard), sometimes refered to as 3DES, is a
Alex Gaynor17adce62013-10-16 17:04:40 -0700112 block cipher standardized by NIST. Triple DES has known cryptoanalytic
113 flaws, however none of them currently enable a practical attack.
114 Nonetheless, Triples DES is not reccomended for new applications because it
Alex Gaynorfbcc5642013-10-22 08:26:00 -0700115 is incredibly slow; old applications should consider moving away from it.
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700116
117 :param bytes key: The secret key, either ``64``, ``128``, or ``192`` bits
118 (note that DES functionally uses ``56``, ``112``, or
119 ``168`` bits of the key, there is a parity byte in each
120 component of the key), in some materials these are
121 referred to as being up to three separate keys (each
122 ``56`` bits long), they can simply be concatenated to
123 produce the full key. This must be kept secret.
124
Paul Kehrer6022d452013-10-30 17:03:54 -0500125.. class:: CAST5(key)
126
127 CAST5 (also known as CAST-128) is a block cipher approved for use in the
128 Canadian government by their Communications Security Establishment. It is a
129 variable key length cipher and supports keys from 40-128 bits in length.
130
131 :param bytes key: The secret key, 40-128 bits in length (in increments of
132 8). This must be kept secret.
133
Paul Kehrer3446d812013-10-31 17:15:03 -0500134Weak Ciphers
135------------
136
137.. warning::
138
139 These ciphers are considered weak for a variety of reasons. New
140 applications should avoid their use and existing applications should
141 strongly consider migrating away.
142
Paul Kehrer5df0abe2013-10-30 16:57:04 -0500143.. class:: Blowfish(key)
144
145 Blowfish is a block cipher developed by Bruce Schneier. It is known to be
146 susceptible to attacks when using weak keys. The author has recommended
Paul Kehrer3446d812013-10-31 17:15:03 -0500147 that users of Blowfish move to newer algorithms like
148 :class:`AES`.
Paul Kehrer5df0abe2013-10-30 16:57:04 -0500149
150 :param bytes key: The secret key, 32-448 bits in length (in increments of
151 8). This must be kept secret.
152
Alex Gaynord96d1002013-08-08 07:37:26 -0700153Modes
154~~~~~
155
Donald Stufftf04317a2013-10-27 16:44:30 -0400156.. currentmodule:: cryptography.hazmat.primitives.block.modes
David Reid1f3d7182013-10-22 16:55:18 -0700157
158.. class:: CBC(initialization_vector)
Alex Gaynor48ec9a32013-08-08 11:13:46 -0700159
160 CBC (Cipher block chaining) is a mode of operation for block ciphers. It is
161 considered cryptographically strong.
162
163 :param bytes initialization_vector: Must be random bytes. They do not need
164 to be kept secret (they can be included
Alex Gaynor2dc2b862013-08-08 11:58:04 -0700165 in a transmitted message). Must be the
166 same number of bytes as the
Alex Gaynor6badd9b2013-08-08 14:59:53 -0700167 ``block_size`` of the cipher. Do not
168 reuse an ``initialization_vector`` with
169 a given ``key``.
Paul Kehrer13f108f2013-09-09 21:41:03 -0500170
Paul Kehrer45064282013-10-17 13:41:53 -0500171
David Reid1f3d7182013-10-22 16:55:18 -0700172.. class:: CTR(nonce)
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500173
Paul Kehrer45064282013-10-17 13:41:53 -0500174 .. warning::
175
176 Counter mode is not recommended for use with block ciphers that have a
177 block size of less than 128-bits.
178
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500179 CTR (Counter) is a mode of operation for block ciphers. It is considered
Alex Gaynord1f02012013-11-01 14:12:35 -0700180 cryptographically strong. It transforms a block cipher into a stream
181 cipher.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500182
Paul Kehrer89b3dd32013-10-17 14:02:45 -0500183 :param bytes nonce: Should be random bytes. It is critical to never reuse a
184 ``nonce`` with a given key. Any reuse of a nonce
185 with the same key compromises the security of every
186 message encrypted with that key. Must be the same
187 number of bytes as the ``block_size`` of the cipher
188 with a given key. The nonce does not need to be kept
189 secret and may be included alongside the ciphertext.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500190
David Reid1f3d7182013-10-22 16:55:18 -0700191.. class:: OFB(initialization_vector)
Paul Kehrer6f412a02013-09-10 21:30:50 -0500192
193 OFB (Output Feedback) is a mode of operation for block ciphers. It
194 transforms a block cipher into a stream cipher.
195
David Reidf1a39bd2013-09-11 16:28:42 -0700196 :param bytes initialization_vector: Must be random bytes. They do not need
197 to be kept secret (they can be included
198 in a transmitted message). Must be the
199 same number of bytes as the
200 ``block_size`` of the cipher. Do not
201 reuse an ``initialization_vector`` with
202 a given ``key``.
Paul Kehrer6f412a02013-09-10 21:30:50 -0500203
David Reid1f3d7182013-10-22 16:55:18 -0700204.. class:: CFB(initialization_vector)
Paul Kehrer4223df72013-09-11 09:48:04 -0500205
206 CFB (Cipher Feedback) is a mode of operation for block ciphers. It
207 transforms a block cipher into a stream cipher.
208
209 :param bytes initialization_vector: Must be random bytes. They do not need
210 to be kept secret (they can be included
211 in a transmitted message). Must be the
212 same number of bytes as the
213 ``block_size`` of the cipher. Do not
214 reuse an ``initialization_vector`` with
215 a given ``key``.
216
Paul Kehrer13f108f2013-09-09 21:41:03 -0500217
218Insecure Modes
219--------------
220
Alex Gaynorcd413a32013-09-10 18:59:43 -0700221.. warning::
222
223 These modes are insecure. New applications should never make use of them,
224 and existing applications should strongly consider migrating away.
225
226
David Reid1f3d7182013-10-22 16:55:18 -0700227.. class:: ECB()
Paul Kehrer13f108f2013-09-09 21:41:03 -0500228
229 ECB (Electronic Code Book) is the simplest mode of operation for block
Alex Gaynorcd413a32013-09-10 18:59:43 -0700230 ciphers. Each block of data is encrypted in the same way. This means
231 identical plaintext blocks will always result in identical ciphertext
232 blocks, and thus result in information leakage