blob: 96bd68f0904b726ec2c8277ae362660cb761be77 [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
18 CBC, CTR, or GCM). A simple example of encrypting content with AES is:
Alex Gaynorf6c47e92013-08-08 07:16:01 -070019
Donald Stufft173de982013-08-12 07:34:39 -040020 .. doctest::
Alex Gaynorf6c47e92013-08-08 07:16:01 -070021
Alex Gaynor641a3a02013-08-10 15:46:07 -040022 >>> from cryptography.primitives.block import BlockCipher, ciphers, modes
Alex Gaynoracc787a2013-08-10 15:52:40 -040023 >>> cipher = BlockCipher(ciphers.AES(key), modes.CBC(iv))
Donald Stufft292112b2013-08-11 14:32:17 -040024 >>> cipher.encrypt(b"a secret message") + cipher.finalize()
Donald Stufft173de982013-08-12 07:34:39 -040025 '...'
Alex Gaynorf6c47e92013-08-08 07:16:01 -070026
Alex Gaynore62aa402013-08-08 15:23:11 -070027 :param cipher: One of the ciphers described below.
28 :param mode: One of the modes described below.
Alex Gaynor0ca7fdb2013-08-08 07:35:26 -070029
Alex Gaynor09515f02013-08-08 15:26:55 -070030 ``encrypt()`` should be called repeatedly with new plaintext, and once the
31 full plaintext is fed in, ``finalize()`` should be called.
32
Alex Gaynore62aa402013-08-08 15:23:11 -070033 .. method:: encrypt(plaintext)
34
35 :param bytes plaintext: The text you wish to encrypt.
36 :return bytes: Returns the ciphertext that was added.
37
38 .. method:: finalize()
39
40 :return bytes: Returns the remainder of the ciphertext.
Alex Gaynord96d1002013-08-08 07:37:26 -070041
42Ciphers
43~~~~~~~
44
Alex Gaynor641a3a02013-08-10 15:46:07 -040045.. class:: cryptography.primitives.block.ciphers.AES(key)
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070046
Alex Gaynor1e3f81f2013-08-08 11:31:43 -070047 AES (Advanced Encryption Standard) is a block cipher standardized by NIST.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070048 AES is both fast, and cryptographically strong. It is a good default
49 choice for encryption.
50
51 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
Alex Gaynor48ec9a32013-08-08 11:13:46 -070052 This must be kept secret.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070053
Paul Kehrerdff22d42013-09-27 13:43:06 -050054.. class:: cryptography.primitives.block.ciphers.Camellia(key)
55
56 Camellia is a block cipher approved for use by CRYPTREC and ISO/IEC.
57 It is considered to have comparable security and performance to AES, but
58 is not as widely studied or deployed.
59
60 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
61 This must be kept secret.
62
Alex Gaynord96d1002013-08-08 07:37:26 -070063
Alex Gaynoraeb714c2013-09-09 18:06:14 -070064.. class:: cryptography.primitives.block.ciphers.TripleDES(key)
65
Alex Gaynor2f355d12013-09-09 18:09:26 -070066 Triple DES (Data Encryption Standard), sometimes refered to as 3DES, is a
Alex Gaynor17adce62013-10-16 17:04:40 -070067 block cipher standardized by NIST. Triple DES has known cryptoanalytic
68 flaws, however none of them currently enable a practical attack.
69 Nonetheless, Triples DES is not reccomended for new applications because it
Alex Gaynorfbcc5642013-10-22 08:26:00 -070070 is incredibly slow; old applications should consider moving away from it.
Alex Gaynoraeb714c2013-09-09 18:06:14 -070071
72 :param bytes key: The secret key, either ``64``, ``128``, or ``192`` bits
73 (note that DES functionally uses ``56``, ``112``, or
74 ``168`` bits of the key, there is a parity byte in each
75 component of the key), in some materials these are
76 referred to as being up to three separate keys (each
77 ``56`` bits long), they can simply be concatenated to
78 produce the full key. This must be kept secret.
79
80
Alex Gaynord96d1002013-08-08 07:37:26 -070081Modes
82~~~~~
83
Alex Gaynor641a3a02013-08-10 15:46:07 -040084.. class:: cryptography.primitives.block.modes.CBC(initialization_vector)
Alex Gaynor48ec9a32013-08-08 11:13:46 -070085
86 CBC (Cipher block chaining) is a mode of operation for block ciphers. It is
87 considered cryptographically strong.
88
89 :param bytes initialization_vector: Must be random bytes. They do not need
90 to be kept secret (they can be included
Alex Gaynor2dc2b862013-08-08 11:58:04 -070091 in a transmitted message). Must be the
92 same number of bytes as the
Alex Gaynor6badd9b2013-08-08 14:59:53 -070093 ``block_size`` of the cipher. Do not
94 reuse an ``initialization_vector`` with
95 a given ``key``.
Paul Kehrer13f108f2013-09-09 21:41:03 -050096
Paul Kehrer45064282013-10-17 13:41:53 -050097
Paul Kehrerd0ec60e2013-10-16 08:46:50 -050098.. class:: cryptography.primitives.block.modes.CTR(nonce)
99
Paul Kehrer45064282013-10-17 13:41:53 -0500100 .. warning::
101
102 Counter mode is not recommended for use with block ciphers that have a
103 block size of less than 128-bits.
104
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500105 CTR (Counter) is a mode of operation for block ciphers. It is considered
106 cryptographically strong.
107
Paul Kehrer89b3dd32013-10-17 14:02:45 -0500108 :param bytes nonce: Should be random bytes. It is critical to never reuse a
109 ``nonce`` with a given key. Any reuse of a nonce
110 with the same key compromises the security of every
111 message encrypted with that key. Must be the same
112 number of bytes as the ``block_size`` of the cipher
113 with a given key. The nonce does not need to be kept
114 secret and may be included alongside the ciphertext.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500115
David Reidf1a39bd2013-09-11 16:28:42 -0700116.. class:: cryptography.primitives.block.modes.OFB(initialization_vector)
Paul Kehrer6f412a02013-09-10 21:30:50 -0500117
118 OFB (Output Feedback) is a mode of operation for block ciphers. It
119 transforms a block cipher into a stream cipher.
120
David Reidf1a39bd2013-09-11 16:28:42 -0700121 :param bytes initialization_vector: Must be random bytes. They do not need
122 to be kept secret (they can be included
123 in a transmitted message). Must be the
124 same number of bytes as the
125 ``block_size`` of the cipher. Do not
126 reuse an ``initialization_vector`` with
127 a given ``key``.
Paul Kehrer6f412a02013-09-10 21:30:50 -0500128
Paul Kehrer4223df72013-09-11 09:48:04 -0500129.. class:: cryptography.primitives.block.modes.CFB(initialization_vector)
130
131 CFB (Cipher Feedback) is a mode of operation for block ciphers. It
132 transforms a block cipher into a stream cipher.
133
134 :param bytes initialization_vector: Must be random bytes. They do not need
135 to be kept secret (they can be included
136 in a transmitted message). Must be the
137 same number of bytes as the
138 ``block_size`` of the cipher. Do not
139 reuse an ``initialization_vector`` with
140 a given ``key``.
141
Paul Kehrer13f108f2013-09-09 21:41:03 -0500142
143Insecure Modes
144--------------
145
Alex Gaynorcd413a32013-09-10 18:59:43 -0700146.. warning::
147
148 These modes are insecure. New applications should never make use of them,
149 and existing applications should strongly consider migrating away.
150
151
Paul Kehrer13f108f2013-09-09 21:41:03 -0500152.. class:: cryptography.primitives.block.modes.ECB()
153
154 ECB (Electronic Code Book) is the simplest mode of operation for block
Alex Gaynorcd413a32013-09-10 18:59:43 -0700155 ciphers. Each block of data is encrypted in the same way. This means
156 identical plaintext blocks will always result in identical ciphertext
157 blocks, and thus result in information leakage