blob: 9768246c56ff434a5d908af62d25c4214b3c0031 [file] [log] [blame]
Alex Gaynorf6c47e92013-08-08 07:16:01 -07001Symmetric Encryption
2====================
3
Donald Stufft173de982013-08-12 07:34:39 -04004.. testsetup::
5
6 import binascii
7 key = binascii.unhexlify(b"0" * 32)
8 iv = binascii.unhexlify(b"0" * 32)
9
10
Alex Gaynorf6c47e92013-08-08 07:16:01 -070011Symmetric encryption is a way to encrypt (hide the plaintext value) material
12where the encrypter and decrypter both use the same key.
13
Alex Gaynor65678d02013-08-08 15:19:19 -070014.. class:: cryptography.primitives.block.BlockCipher(cipher, mode)
Alex Gaynorf6c47e92013-08-08 07:16:01 -070015
Alex Gaynor65678d02013-08-08 15:19:19 -070016 Block ciphers work by encrypting content in chunks, often 64- or 128-bits.
Alex Gaynorb12f76e2013-08-08 19:05:18 -070017 They combine an underlying algorithm (such as AES), with a mode (such as
Paul Kehrerd1afe392013-10-22 08:24:44 -050018 CBC, CTR, or GCM). A simple example of encrypting (and then decrypting)
Paul Kehrer653463f2013-10-21 17:55:01 -050019 content with AES is:
Alex Gaynorf6c47e92013-08-08 07:16:01 -070020
Donald Stufft173de982013-08-12 07:34:39 -040021 .. doctest::
Alex Gaynorf6c47e92013-08-08 07:16:01 -070022
Alex Gaynor641a3a02013-08-10 15:46:07 -040023 >>> from cryptography.primitives.block import BlockCipher, ciphers, modes
Alex Gaynoracc787a2013-08-10 15:52:40 -040024 >>> cipher = BlockCipher(ciphers.AES(key), modes.CBC(iv))
Paul Kehrer3e0895c2013-10-21 22:19:29 -050025 >>> encryptor = cipher.encryptor()
26 >>> ct = encryptor.update(b"a secret message") + encryptor.finalize()
27 >>> decryptor = cipher.decryptor()
28 >>> decryptor.update(ct) + decryptor.finalize()
Paul Kehrerf6cf9562013-10-22 10:36:00 -050029 'a secret message'
Alex Gaynorf6c47e92013-08-08 07:16:01 -070030
Alex Gaynore62aa402013-08-08 15:23:11 -070031 :param cipher: One of the ciphers described below.
32 :param mode: One of the modes described below.
Alex Gaynor0ca7fdb2013-08-08 07:35:26 -070033
Paul Kehrer5399fd02013-10-21 23:48:25 -050034 .. method:: encryptor()
Alex Gaynor09515f02013-08-08 15:26:55 -070035
David Reid63ba6652013-10-22 14:09:19 -070036 :return: An encrypting
37 :class:`~cryptography.primitives.interfaces.CipherContext`
38 provider.
Alex Gaynore62aa402013-08-08 15:23:11 -070039
Paul Kehrer5399fd02013-10-21 23:48:25 -050040 .. method:: decryptor()
41
David Reid63ba6652013-10-22 14:09:19 -070042 :return: A decrypting
43 :class:`~cryptography.primitives.interfaces.CipherContext`
44 provider.
Paul Kehrer5399fd02013-10-21 23:48:25 -050045
Paul Kehrer5399fd02013-10-21 23:48:25 -050046.. class:: cryptography.primitives.interfaces.CipherContext()
47
48 When calling ``encryptor()`` or ``decryptor()`` on a BlockCipher object you
49 will receive a return object conforming to the CipherContext interface. You
50 can then call ``update(data)`` with data until you have fed everything into
51 the context. Once that is done call ``finalize()`` to finish the operation and
52 obtain the remainder of the data.
53
54
55 .. method:: update(data)
56
57 :param bytes data: The text you wish to pass into the context.
58 :return bytes: Returns the data that was encrypted or decrypted.
Alex Gaynore62aa402013-08-08 15:23:11 -070059
60 .. method:: finalize()
61
Paul Kehrer5399fd02013-10-21 23:48:25 -050062 :return bytes: Returns the remainder of the data.
Alex Gaynord96d1002013-08-08 07:37:26 -070063
64Ciphers
65~~~~~~~
66
Alex Gaynor641a3a02013-08-10 15:46:07 -040067.. class:: cryptography.primitives.block.ciphers.AES(key)
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070068
Alex Gaynor1e3f81f2013-08-08 11:31:43 -070069 AES (Advanced Encryption Standard) is a block cipher standardized by NIST.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070070 AES is both fast, and cryptographically strong. It is a good default
71 choice for encryption.
72
73 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
Alex Gaynor48ec9a32013-08-08 11:13:46 -070074 This must be kept secret.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070075
Paul Kehrerdff22d42013-09-27 13:43:06 -050076.. class:: cryptography.primitives.block.ciphers.Camellia(key)
77
78 Camellia is a block cipher approved for use by CRYPTREC and ISO/IEC.
79 It is considered to have comparable security and performance to AES, but
80 is not as widely studied or deployed.
81
82 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
83 This must be kept secret.
84
Alex Gaynord96d1002013-08-08 07:37:26 -070085
Alex Gaynoraeb714c2013-09-09 18:06:14 -070086.. class:: cryptography.primitives.block.ciphers.TripleDES(key)
87
Alex Gaynor2f355d12013-09-09 18:09:26 -070088 Triple DES (Data Encryption Standard), sometimes refered to as 3DES, is a
Alex Gaynor17adce62013-10-16 17:04:40 -070089 block cipher standardized by NIST. Triple DES has known cryptoanalytic
90 flaws, however none of them currently enable a practical attack.
91 Nonetheless, Triples DES is not reccomended for new applications because it
Alex Gaynorfbcc5642013-10-22 08:26:00 -070092 is incredibly slow; old applications should consider moving away from it.
Alex Gaynoraeb714c2013-09-09 18:06:14 -070093
94 :param bytes key: The secret key, either ``64``, ``128``, or ``192`` bits
95 (note that DES functionally uses ``56``, ``112``, or
96 ``168`` bits of the key, there is a parity byte in each
97 component of the key), in some materials these are
98 referred to as being up to three separate keys (each
99 ``56`` bits long), they can simply be concatenated to
100 produce the full key. This must be kept secret.
101
102
Alex Gaynord96d1002013-08-08 07:37:26 -0700103Modes
104~~~~~
105
Alex Gaynor641a3a02013-08-10 15:46:07 -0400106.. class:: cryptography.primitives.block.modes.CBC(initialization_vector)
Alex Gaynor48ec9a32013-08-08 11:13:46 -0700107
108 CBC (Cipher block chaining) is a mode of operation for block ciphers. It is
109 considered cryptographically strong.
110
111 :param bytes initialization_vector: Must be random bytes. They do not need
112 to be kept secret (they can be included
Alex Gaynor2dc2b862013-08-08 11:58:04 -0700113 in a transmitted message). Must be the
114 same number of bytes as the
Alex Gaynor6badd9b2013-08-08 14:59:53 -0700115 ``block_size`` of the cipher. Do not
116 reuse an ``initialization_vector`` with
117 a given ``key``.
Paul Kehrer13f108f2013-09-09 21:41:03 -0500118
Paul Kehrer45064282013-10-17 13:41:53 -0500119
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500120.. class:: cryptography.primitives.block.modes.CTR(nonce)
121
Paul Kehrer45064282013-10-17 13:41:53 -0500122 .. warning::
123
124 Counter mode is not recommended for use with block ciphers that have a
125 block size of less than 128-bits.
126
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500127 CTR (Counter) is a mode of operation for block ciphers. It is considered
128 cryptographically strong.
129
Paul Kehrer89b3dd32013-10-17 14:02:45 -0500130 :param bytes nonce: Should be random bytes. It is critical to never reuse a
131 ``nonce`` with a given key. Any reuse of a nonce
132 with the same key compromises the security of every
133 message encrypted with that key. Must be the same
134 number of bytes as the ``block_size`` of the cipher
135 with a given key. The nonce does not need to be kept
136 secret and may be included alongside the ciphertext.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500137
David Reidf1a39bd2013-09-11 16:28:42 -0700138.. class:: cryptography.primitives.block.modes.OFB(initialization_vector)
Paul Kehrer6f412a02013-09-10 21:30:50 -0500139
140 OFB (Output Feedback) is a mode of operation for block ciphers. It
141 transforms a block cipher into a stream cipher.
142
David Reidf1a39bd2013-09-11 16:28:42 -0700143 :param bytes initialization_vector: Must be random bytes. They do not need
144 to be kept secret (they can be included
145 in a transmitted message). Must be the
146 same number of bytes as the
147 ``block_size`` of the cipher. Do not
148 reuse an ``initialization_vector`` with
149 a given ``key``.
Paul Kehrer6f412a02013-09-10 21:30:50 -0500150
Paul Kehrer4223df72013-09-11 09:48:04 -0500151.. class:: cryptography.primitives.block.modes.CFB(initialization_vector)
152
153 CFB (Cipher Feedback) is a mode of operation for block ciphers. It
154 transforms a block cipher into a stream cipher.
155
156 :param bytes initialization_vector: Must be random bytes. They do not need
157 to be kept secret (they can be included
158 in a transmitted message). Must be the
159 same number of bytes as the
160 ``block_size`` of the cipher. Do not
161 reuse an ``initialization_vector`` with
162 a given ``key``.
163
Paul Kehrer13f108f2013-09-09 21:41:03 -0500164
165Insecure Modes
166--------------
167
Alex Gaynorcd413a32013-09-10 18:59:43 -0700168.. warning::
169
170 These modes are insecure. New applications should never make use of them,
171 and existing applications should strongly consider migrating away.
172
173
Paul Kehrer13f108f2013-09-09 21:41:03 -0500174.. class:: cryptography.primitives.block.modes.ECB()
175
176 ECB (Electronic Code Book) is the simplest mode of operation for block
Alex Gaynorcd413a32013-09-10 18:59:43 -0700177 ciphers. Each block of data is encrypted in the same way. This means
178 identical plaintext blocks will always result in identical ciphertext
179 blocks, and thus result in information leakage