blob: e65e48d4ccac68e64b250a8743e5e3449aadd8df [file] [log] [blame]
Alex Gaynorf6c47e92013-08-08 07:16:01 -07001Symmetric Encryption
2====================
3
Donald Stufftd8f01182013-10-27 16:59:56 -04004.. danger::
5
6 This is a "Hazardous Materials" module. You should **ONLY** use it if
7 you're 100% absolutely sure that you know what you're doing because this
8 module is full of land mines, dragons, and dinosaurs with laser guns.
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
57.. class:: CipherContext()
Paul Kehrer5399fd02013-10-21 23:48:25 -050058
59 When calling ``encryptor()`` or ``decryptor()`` on a BlockCipher object you
60 will receive a return object conforming to the CipherContext interface. You
61 can then call ``update(data)`` with data until you have fed everything into
62 the context. Once that is done call ``finalize()`` to finish the operation and
63 obtain the remainder of the data.
64
65
66 .. method:: update(data)
67
68 :param bytes data: The text you wish to pass into the context.
69 :return bytes: Returns the data that was encrypted or decrypted.
Alex Gaynore62aa402013-08-08 15:23:11 -070070
71 .. method:: finalize()
72
Paul Kehrer5399fd02013-10-21 23:48:25 -050073 :return bytes: Returns the remainder of the data.
Alex Gaynord96d1002013-08-08 07:37:26 -070074
75Ciphers
76~~~~~~~
77
Donald Stufftf04317a2013-10-27 16:44:30 -040078.. currentmodule:: cryptography.hazmat.primitives.block.ciphers
David Reid1f3d7182013-10-22 16:55:18 -070079
80.. class:: AES(key)
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070081
Alex Gaynor1e3f81f2013-08-08 11:31:43 -070082 AES (Advanced Encryption Standard) is a block cipher standardized by NIST.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070083 AES is both fast, and cryptographically strong. It is a good default
84 choice for encryption.
85
86 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
Alex Gaynor48ec9a32013-08-08 11:13:46 -070087 This must be kept secret.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070088
David Reid1f3d7182013-10-22 16:55:18 -070089.. class:: Camellia(key)
Paul Kehrerdff22d42013-09-27 13:43:06 -050090
91 Camellia is a block cipher approved for use by CRYPTREC and ISO/IEC.
92 It is considered to have comparable security and performance to AES, but
93 is not as widely studied or deployed.
94
95 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
96 This must be kept secret.
97
Alex Gaynord96d1002013-08-08 07:37:26 -070098
David Reid1f3d7182013-10-22 16:55:18 -070099.. class:: TripleDES(key)
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700100
Alex Gaynor2f355d12013-09-09 18:09:26 -0700101 Triple DES (Data Encryption Standard), sometimes refered to as 3DES, is a
Alex Gaynor17adce62013-10-16 17:04:40 -0700102 block cipher standardized by NIST. Triple DES has known cryptoanalytic
103 flaws, however none of them currently enable a practical attack.
104 Nonetheless, Triples DES is not reccomended for new applications because it
Alex Gaynorfbcc5642013-10-22 08:26:00 -0700105 is incredibly slow; old applications should consider moving away from it.
Alex Gaynoraeb714c2013-09-09 18:06:14 -0700106
107 :param bytes key: The secret key, either ``64``, ``128``, or ``192`` bits
108 (note that DES functionally uses ``56``, ``112``, or
109 ``168`` bits of the key, there is a parity byte in each
110 component of the key), in some materials these are
111 referred to as being up to three separate keys (each
112 ``56`` bits long), they can simply be concatenated to
113 produce the full key. This must be kept secret.
114
115
Alex Gaynord96d1002013-08-08 07:37:26 -0700116Modes
117~~~~~
118
Donald Stufftf04317a2013-10-27 16:44:30 -0400119.. currentmodule:: cryptography.hazmat.primitives.block.modes
David Reid1f3d7182013-10-22 16:55:18 -0700120
121.. class:: CBC(initialization_vector)
Alex Gaynor48ec9a32013-08-08 11:13:46 -0700122
123 CBC (Cipher block chaining) is a mode of operation for block ciphers. It is
124 considered cryptographically strong.
125
126 :param bytes initialization_vector: Must be random bytes. They do not need
127 to be kept secret (they can be included
Alex Gaynor2dc2b862013-08-08 11:58:04 -0700128 in a transmitted message). Must be the
129 same number of bytes as the
Alex Gaynor6badd9b2013-08-08 14:59:53 -0700130 ``block_size`` of the cipher. Do not
131 reuse an ``initialization_vector`` with
132 a given ``key``.
Paul Kehrer13f108f2013-09-09 21:41:03 -0500133
Paul Kehrer45064282013-10-17 13:41:53 -0500134
David Reid1f3d7182013-10-22 16:55:18 -0700135.. class:: CTR(nonce)
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500136
Paul Kehrer45064282013-10-17 13:41:53 -0500137 .. warning::
138
139 Counter mode is not recommended for use with block ciphers that have a
140 block size of less than 128-bits.
141
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500142 CTR (Counter) is a mode of operation for block ciphers. It is considered
143 cryptographically strong.
144
Paul Kehrer89b3dd32013-10-17 14:02:45 -0500145 :param bytes nonce: Should be random bytes. It is critical to never reuse a
146 ``nonce`` with a given key. Any reuse of a nonce
147 with the same key compromises the security of every
148 message encrypted with that key. Must be the same
149 number of bytes as the ``block_size`` of the cipher
150 with a given key. The nonce does not need to be kept
151 secret and may be included alongside the ciphertext.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500152
David Reid1f3d7182013-10-22 16:55:18 -0700153.. class:: OFB(initialization_vector)
Paul Kehrer6f412a02013-09-10 21:30:50 -0500154
155 OFB (Output Feedback) is a mode of operation for block ciphers. It
156 transforms a block cipher into a stream cipher.
157
David Reidf1a39bd2013-09-11 16:28:42 -0700158 :param bytes initialization_vector: Must be random bytes. They do not need
159 to be kept secret (they can be included
160 in a transmitted message). Must be the
161 same number of bytes as the
162 ``block_size`` of the cipher. Do not
163 reuse an ``initialization_vector`` with
164 a given ``key``.
Paul Kehrer6f412a02013-09-10 21:30:50 -0500165
David Reid1f3d7182013-10-22 16:55:18 -0700166.. class:: CFB(initialization_vector)
Paul Kehrer4223df72013-09-11 09:48:04 -0500167
168 CFB (Cipher Feedback) is a mode of operation for block ciphers. It
169 transforms a block cipher into a stream cipher.
170
171 :param bytes initialization_vector: Must be random bytes. They do not need
172 to be kept secret (they can be included
173 in a transmitted message). Must be the
174 same number of bytes as the
175 ``block_size`` of the cipher. Do not
176 reuse an ``initialization_vector`` with
177 a given ``key``.
178
Paul Kehrer13f108f2013-09-09 21:41:03 -0500179
180Insecure Modes
181--------------
182
Alex Gaynorcd413a32013-09-10 18:59:43 -0700183.. warning::
184
185 These modes are insecure. New applications should never make use of them,
186 and existing applications should strongly consider migrating away.
187
188
David Reid1f3d7182013-10-22 16:55:18 -0700189.. class:: ECB()
Paul Kehrer13f108f2013-09-09 21:41:03 -0500190
191 ECB (Electronic Code Book) is the simplest mode of operation for block
Alex Gaynorcd413a32013-09-10 18:59:43 -0700192 ciphers. Each block of data is encrypted in the same way. This means
193 identical plaintext blocks will always result in identical ciphertext
194 blocks, and thus result in information leakage