blob: 31ceea8a3985c8e859b04909c07a24dd2e114d19 [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
Paul Kehrer5399fd02013-10-21 23:48:25 -050045 .. method:: decryptor()
46
David Reid63ba6652013-10-22 14:09:19 -070047 :return: A decrypting
Donald Stufftf04317a2013-10-27 16:44:30 -040048 :class:`~cryptography.hazmat.primitives.interfaces.CipherContext`
David Reid63ba6652013-10-22 14:09:19 -070049 provider.
Paul Kehrer5399fd02013-10-21 23:48:25 -050050
Donald Stufftf04317a2013-10-27 16:44:30 -040051.. currentmodule:: cryptography.hazmat.primitives.interfaces
David Reid1f3d7182013-10-22 16:55:18 -070052
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070053.. class:: CipherContext
Paul Kehrer5399fd02013-10-21 23:48:25 -050054
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070055 When calling ``encryptor()`` or ``decryptor()`` on a ``BlockCipher`` object
56 you will receive a return object conforming to the ``CipherContext``
57 interface. You can then call ``update(data)`` with data until you have fed
58 everything into the context. Once that is done call ``finalize()`` to
59 finish the operation and obtain the remainder of the data.
Paul Kehrer5399fd02013-10-21 23:48:25 -050060
61 .. method:: update(data)
62
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070063 :param bytes data: The data you wish to pass into the context.
Paul Kehrer5399fd02013-10-21 23:48:25 -050064 :return bytes: Returns the data that was encrypted or decrypted.
Alex Gaynore62aa402013-08-08 15:23:11 -070065
66 .. method:: finalize()
67
Paul Kehrer5399fd02013-10-21 23:48:25 -050068 :return bytes: Returns the remainder of the data.
Alex Gaynord96d1002013-08-08 07:37:26 -070069
70Ciphers
71~~~~~~~
72
Donald Stufftf04317a2013-10-27 16:44:30 -040073.. currentmodule:: cryptography.hazmat.primitives.block.ciphers
David Reid1f3d7182013-10-22 16:55:18 -070074
75.. class:: AES(key)
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070076
Alex Gaynor1e3f81f2013-08-08 11:31:43 -070077 AES (Advanced Encryption Standard) is a block cipher standardized by NIST.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070078 AES is both fast, and cryptographically strong. It is a good default
79 choice for encryption.
80
81 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
Alex Gaynor48ec9a32013-08-08 11:13:46 -070082 This must be kept secret.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070083
David Reid1f3d7182013-10-22 16:55:18 -070084.. class:: Camellia(key)
Paul Kehrerdff22d42013-09-27 13:43:06 -050085
86 Camellia is a block cipher approved for use by CRYPTREC and ISO/IEC.
87 It is considered to have comparable security and performance to AES, but
88 is not as widely studied or deployed.
89
90 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
91 This must be kept secret.
92
Alex Gaynord96d1002013-08-08 07:37:26 -070093
David Reid1f3d7182013-10-22 16:55:18 -070094.. class:: TripleDES(key)
Alex Gaynoraeb714c2013-09-09 18:06:14 -070095
Alex Gaynor2f355d12013-09-09 18:09:26 -070096 Triple DES (Data Encryption Standard), sometimes refered to as 3DES, is a
Alex Gaynor17adce62013-10-16 17:04:40 -070097 block cipher standardized by NIST. Triple DES has known cryptoanalytic
98 flaws, however none of them currently enable a practical attack.
99 Nonetheless, Triples DES is not reccomended for new applications because it
Alex Gaynorfbcc5642013-10-22 08:26:00 -0700100 is incredibly slow; old applications should consider moving away from it.
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700101
102 :param bytes key: The secret key, either ``64``, ``128``, or ``192`` bits
103 (note that DES functionally uses ``56``, ``112``, or
104 ``168`` bits of the key, there is a parity byte in each
105 component of the key), in some materials these are
106 referred to as being up to three separate keys (each
107 ``56`` bits long), they can simply be concatenated to
108 produce the full key. This must be kept secret.
109
Paul Kehrer3446d812013-10-31 17:15:03 -0500110Weak Ciphers
111------------
112
113.. warning::
114
115 These ciphers are considered weak for a variety of reasons. New
116 applications should avoid their use and existing applications should
117 strongly consider migrating away.
118
Paul Kehrer5df0abe2013-10-30 16:57:04 -0500119.. class:: Blowfish(key)
120
121 Blowfish is a block cipher developed by Bruce Schneier. It is known to be
122 susceptible to attacks when using weak keys. The author has recommended
Paul Kehrer3446d812013-10-31 17:15:03 -0500123 that users of Blowfish move to newer algorithms like
124 :class:`AES`.
Paul Kehrer5df0abe2013-10-30 16:57:04 -0500125
126 :param bytes key: The secret key, 32-448 bits in length (in increments of
127 8). This must be kept secret.
128
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700129
Alex Gaynord96d1002013-08-08 07:37:26 -0700130Modes
131~~~~~
132
Donald Stufftf04317a2013-10-27 16:44:30 -0400133.. currentmodule:: cryptography.hazmat.primitives.block.modes
David Reid1f3d7182013-10-22 16:55:18 -0700134
135.. class:: CBC(initialization_vector)
Alex Gaynor48ec9a32013-08-08 11:13:46 -0700136
137 CBC (Cipher block chaining) is a mode of operation for block ciphers. It is
138 considered cryptographically strong.
139
140 :param bytes initialization_vector: Must be random bytes. They do not need
141 to be kept secret (they can be included
Alex Gaynor2dc2b862013-08-08 11:58:04 -0700142 in a transmitted message). Must be the
143 same number of bytes as the
Alex Gaynor6badd9b2013-08-08 14:59:53 -0700144 ``block_size`` of the cipher. Do not
145 reuse an ``initialization_vector`` with
146 a given ``key``.
Paul Kehrer13f108f2013-09-09 21:41:03 -0500147
Paul Kehrer45064282013-10-17 13:41:53 -0500148
David Reid1f3d7182013-10-22 16:55:18 -0700149.. class:: CTR(nonce)
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500150
Paul Kehrer45064282013-10-17 13:41:53 -0500151 .. warning::
152
153 Counter mode is not recommended for use with block ciphers that have a
154 block size of less than 128-bits.
155
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500156 CTR (Counter) is a mode of operation for block ciphers. It is considered
157 cryptographically strong.
158
Paul Kehrer89b3dd32013-10-17 14:02:45 -0500159 :param bytes nonce: Should be random bytes. It is critical to never reuse a
160 ``nonce`` with a given key. Any reuse of a nonce
161 with the same key compromises the security of every
162 message encrypted with that key. Must be the same
163 number of bytes as the ``block_size`` of the cipher
164 with a given key. The nonce does not need to be kept
165 secret and may be included alongside the ciphertext.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500166
David Reid1f3d7182013-10-22 16:55:18 -0700167.. class:: OFB(initialization_vector)
Paul Kehrer6f412a02013-09-10 21:30:50 -0500168
169 OFB (Output Feedback) is a mode of operation for block ciphers. It
170 transforms a block cipher into a stream cipher.
171
David Reidf1a39bd2013-09-11 16:28:42 -0700172 :param bytes initialization_vector: Must be random bytes. They do not need
173 to be kept secret (they can be included
174 in a transmitted message). Must be the
175 same number of bytes as the
176 ``block_size`` of the cipher. Do not
177 reuse an ``initialization_vector`` with
178 a given ``key``.
Paul Kehrer6f412a02013-09-10 21:30:50 -0500179
David Reid1f3d7182013-10-22 16:55:18 -0700180.. class:: CFB(initialization_vector)
Paul Kehrer4223df72013-09-11 09:48:04 -0500181
182 CFB (Cipher Feedback) is a mode of operation for block ciphers. It
183 transforms a block cipher into a stream cipher.
184
185 :param bytes initialization_vector: Must be random bytes. They do not need
186 to be kept secret (they can be included
187 in a transmitted message). Must be the
188 same number of bytes as the
189 ``block_size`` of the cipher. Do not
190 reuse an ``initialization_vector`` with
191 a given ``key``.
192
Paul Kehrer13f108f2013-09-09 21:41:03 -0500193
194Insecure Modes
195--------------
196
Alex Gaynorcd413a32013-09-10 18:59:43 -0700197.. warning::
198
199 These modes are insecure. New applications should never make use of them,
200 and existing applications should strongly consider migrating away.
201
202
David Reid1f3d7182013-10-22 16:55:18 -0700203.. class:: ECB()
Paul Kehrer13f108f2013-09-09 21:41:03 -0500204
205 ECB (Electronic Code Book) is the simplest mode of operation for block
Alex Gaynorcd413a32013-09-10 18:59:43 -0700206 ciphers. Each block of data is encrypted in the same way. This means
207 identical plaintext blocks will always result in identical ciphertext
208 blocks, and thus result in information leakage