blob: 9a5bce076180f1b1289c27095d9d16b49d114694 [file] [log] [blame]
Donald Stufftd8f01182013-10-27 16:59:56 -04001.. danger::
2
3 This is a "Hazardous Materials" module. You should **ONLY** use it if
4 you're 100% absolutely sure that you know what you're doing because this
5 module is full of land mines, dragons, and dinosaurs with laser guns.
6
7
Donald Stuffte51fb932013-10-27 17:26:17 -04008Symmetric Encryption
9====================
10
Donald Stufftf04317a2013-10-27 16:44:30 -040011.. currentmodule:: cryptography.hazmat.primitives.block
David Reid1f3d7182013-10-22 16:55:18 -070012
Donald Stufft173de982013-08-12 07:34:39 -040013.. testsetup::
14
15 import binascii
16 key = binascii.unhexlify(b"0" * 32)
17 iv = binascii.unhexlify(b"0" * 32)
18
19
Alex Gaynorf6c47e92013-08-08 07:16:01 -070020Symmetric encryption is a way to encrypt (hide the plaintext value) material
21where the encrypter and decrypter both use the same key.
22
David Reid1f3d7182013-10-22 16:55:18 -070023.. class:: BlockCipher(cipher, mode)
Alex Gaynorf6c47e92013-08-08 07:16:01 -070024
Alex Gaynor65678d02013-08-08 15:19:19 -070025 Block ciphers work by encrypting content in chunks, often 64- or 128-bits.
Alex Gaynorb12f76e2013-08-08 19:05:18 -070026 They combine an underlying algorithm (such as AES), with a mode (such as
Paul Kehrerd1afe392013-10-22 08:24:44 -050027 CBC, CTR, or GCM). A simple example of encrypting (and then decrypting)
Paul Kehrer653463f2013-10-21 17:55:01 -050028 content with AES is:
Alex Gaynorf6c47e92013-08-08 07:16:01 -070029
Donald Stufft173de982013-08-12 07:34:39 -040030 .. doctest::
Alex Gaynorf6c47e92013-08-08 07:16:01 -070031
Donald Stufftf04317a2013-10-27 16:44:30 -040032 >>> from cryptography.hazmat.primitives.block import BlockCipher, ciphers, modes
Alex Gaynoracc787a2013-08-10 15:52:40 -040033 >>> cipher = BlockCipher(ciphers.AES(key), modes.CBC(iv))
Paul Kehrer3e0895c2013-10-21 22:19:29 -050034 >>> encryptor = cipher.encryptor()
35 >>> ct = encryptor.update(b"a secret message") + encryptor.finalize()
36 >>> decryptor = cipher.decryptor()
37 >>> decryptor.update(ct) + decryptor.finalize()
Paul Kehrerf6cf9562013-10-22 10:36:00 -050038 'a secret message'
Alex Gaynorf6c47e92013-08-08 07:16:01 -070039
Alex Gaynore62aa402013-08-08 15:23:11 -070040 :param cipher: One of the ciphers described below.
41 :param mode: One of the modes described below.
Alex Gaynor0ca7fdb2013-08-08 07:35:26 -070042
Paul Kehrer5399fd02013-10-21 23:48:25 -050043 .. method:: encryptor()
Alex Gaynor09515f02013-08-08 15:26:55 -070044
David Reid63ba6652013-10-22 14:09:19 -070045 :return: An encrypting
Donald Stufftf04317a2013-10-27 16:44:30 -040046 :class:`~cryptography.hazmat.primitives.interfaces.CipherContext`
David Reid63ba6652013-10-22 14:09:19 -070047 provider.
Alex Gaynore62aa402013-08-08 15:23:11 -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
Donald Stufftf04317a2013-10-27 16:44:30 -040055.. currentmodule:: cryptography.hazmat.primitives.interfaces
David Reid1f3d7182013-10-22 16:55:18 -070056
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070057.. class:: CipherContext
Paul Kehrer5399fd02013-10-21 23:48:25 -050058
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070059 When calling ``encryptor()`` or ``decryptor()`` on a ``BlockCipher`` object
60 you will receive a return object conforming to the ``CipherContext``
61 interface. You can then call ``update(data)`` with data until you have fed
62 everything into the context. Once that is done call ``finalize()`` to
63 finish the operation and obtain the remainder of the data.
Paul Kehrer5399fd02013-10-21 23:48:25 -050064
65 .. method:: update(data)
66
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070067 :param bytes data: The data you wish to pass into the context.
Paul Kehrer5399fd02013-10-21 23:48:25 -050068 :return bytes: Returns the data that was encrypted or decrypted.
Alex Gaynore62aa402013-08-08 15:23:11 -070069
70 .. method:: finalize()
71
Paul Kehrer5399fd02013-10-21 23:48:25 -050072 :return bytes: Returns the remainder of the data.
Alex Gaynord96d1002013-08-08 07:37:26 -070073
74Ciphers
75~~~~~~~
76
Donald Stufftf04317a2013-10-27 16:44:30 -040077.. currentmodule:: cryptography.hazmat.primitives.block.ciphers
David Reid1f3d7182013-10-22 16:55:18 -070078
79.. class:: AES(key)
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070080
Alex Gaynor1e3f81f2013-08-08 11:31:43 -070081 AES (Advanced Encryption Standard) is a block cipher standardized by NIST.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070082 AES is both fast, and cryptographically strong. It is a good default
83 choice for encryption.
84
85 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
Alex Gaynor48ec9a32013-08-08 11:13:46 -070086 This must be kept secret.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070087
David Reid1f3d7182013-10-22 16:55:18 -070088.. class:: Camellia(key)
Paul Kehrerdff22d42013-09-27 13:43:06 -050089
90 Camellia is a block cipher approved for use by CRYPTREC and ISO/IEC.
91 It is considered to have comparable security and performance to AES, but
92 is not as widely studied or deployed.
93
94 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
95 This must be kept secret.
96
Alex Gaynord96d1002013-08-08 07:37:26 -070097
David Reid1f3d7182013-10-22 16:55:18 -070098.. class:: TripleDES(key)
Alex Gaynoraeb714c2013-09-09 18:06:14 -070099
Alex Gaynor2f355d12013-09-09 18:09:26 -0700100 Triple DES (Data Encryption Standard), sometimes refered to as 3DES, is a
Alex Gaynor17adce62013-10-16 17:04:40 -0700101 block cipher standardized by NIST. Triple DES has known cryptoanalytic
102 flaws, however none of them currently enable a practical attack.
103 Nonetheless, Triples DES is not reccomended for new applications because it
Alex Gaynorfbcc5642013-10-22 08:26:00 -0700104 is incredibly slow; old applications should consider moving away from it.
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700105
106 :param bytes key: The secret key, either ``64``, ``128``, or ``192`` bits
107 (note that DES functionally uses ``56``, ``112``, or
108 ``168`` bits of the key, there is a parity byte in each
109 component of the key), in some materials these are
110 referred to as being up to three separate keys (each
111 ``56`` bits long), they can simply be concatenated to
112 produce the full key. This must be kept secret.
113
114
Alex Gaynord96d1002013-08-08 07:37:26 -0700115Modes
116~~~~~
117
Donald Stufftf04317a2013-10-27 16:44:30 -0400118.. currentmodule:: cryptography.hazmat.primitives.block.modes
David Reid1f3d7182013-10-22 16:55:18 -0700119
120.. class:: CBC(initialization_vector)
Alex Gaynor48ec9a32013-08-08 11:13:46 -0700121
122 CBC (Cipher block chaining) is a mode of operation for block ciphers. It is
123 considered cryptographically strong.
124
125 :param bytes initialization_vector: Must be random bytes. They do not need
126 to be kept secret (they can be included
Alex Gaynor2dc2b862013-08-08 11:58:04 -0700127 in a transmitted message). Must be the
128 same number of bytes as the
Alex Gaynor6badd9b2013-08-08 14:59:53 -0700129 ``block_size`` of the cipher. Do not
130 reuse an ``initialization_vector`` with
131 a given ``key``.
Paul Kehrer13f108f2013-09-09 21:41:03 -0500132
Paul Kehrer45064282013-10-17 13:41:53 -0500133
David Reid1f3d7182013-10-22 16:55:18 -0700134.. class:: CTR(nonce)
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500135
Paul Kehrer45064282013-10-17 13:41:53 -0500136 .. warning::
137
138 Counter mode is not recommended for use with block ciphers that have a
139 block size of less than 128-bits.
140
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500141 CTR (Counter) is a mode of operation for block ciphers. It is considered
142 cryptographically strong.
143
Paul Kehrer89b3dd32013-10-17 14:02:45 -0500144 :param bytes nonce: Should be random bytes. It is critical to never reuse a
145 ``nonce`` with a given key. Any reuse of a nonce
146 with the same key compromises the security of every
147 message encrypted with that key. Must be the same
148 number of bytes as the ``block_size`` of the cipher
149 with a given key. The nonce does not need to be kept
150 secret and may be included alongside the ciphertext.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500151
David Reid1f3d7182013-10-22 16:55:18 -0700152.. class:: OFB(initialization_vector)
Paul Kehrer6f412a02013-09-10 21:30:50 -0500153
154 OFB (Output Feedback) is a mode of operation for block ciphers. It
155 transforms a block cipher into a stream cipher.
156
David Reidf1a39bd2013-09-11 16:28:42 -0700157 :param bytes initialization_vector: Must be random bytes. They do not need
158 to be kept secret (they can be included
159 in a transmitted message). Must be the
160 same number of bytes as the
161 ``block_size`` of the cipher. Do not
162 reuse an ``initialization_vector`` with
163 a given ``key``.
Paul Kehrer6f412a02013-09-10 21:30:50 -0500164
David Reid1f3d7182013-10-22 16:55:18 -0700165.. class:: CFB(initialization_vector)
Paul Kehrer4223df72013-09-11 09:48:04 -0500166
167 CFB (Cipher Feedback) is a mode of operation for block ciphers. It
168 transforms a block cipher into a stream cipher.
169
170 :param bytes initialization_vector: Must be random bytes. They do not need
171 to be kept secret (they can be included
172 in a transmitted message). Must be the
173 same number of bytes as the
174 ``block_size`` of the cipher. Do not
175 reuse an ``initialization_vector`` with
176 a given ``key``.
177
Paul Kehrer13f108f2013-09-09 21:41:03 -0500178
179Insecure Modes
180--------------
181
Alex Gaynorcd413a32013-09-10 18:59:43 -0700182.. warning::
183
184 These modes are insecure. New applications should never make use of them,
185 and existing applications should strongly consider migrating away.
186
187
David Reid1f3d7182013-10-22 16:55:18 -0700188.. class:: ECB()
Paul Kehrer13f108f2013-09-09 21:41:03 -0500189
190 ECB (Electronic Code Book) is the simplest mode of operation for block
Alex Gaynorcd413a32013-09-10 18:59:43 -0700191 ciphers. Each block of data is encrypted in the same way. This means
192 identical plaintext blocks will always result in identical ciphertext
193 blocks, and thus result in information leakage