blob: a1f8ba325a91495ed76a26c1faaa169f3dd5b128 [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
64Modes
65~~~~~
66
Alex Gaynor641a3a02013-08-10 15:46:07 -040067.. class:: cryptography.primitives.block.modes.CBC(initialization_vector)
Alex Gaynor48ec9a32013-08-08 11:13:46 -070068
69 CBC (Cipher block chaining) is a mode of operation for block ciphers. It is
70 considered cryptographically strong.
71
72 :param bytes initialization_vector: Must be random bytes. They do not need
73 to be kept secret (they can be included
Alex Gaynor2dc2b862013-08-08 11:58:04 -070074 in a transmitted message). Must be the
75 same number of bytes as the
Alex Gaynor6badd9b2013-08-08 14:59:53 -070076 ``block_size`` of the cipher. Do not
77 reuse an ``initialization_vector`` with
78 a given ``key``.
Paul Kehrer13f108f2013-09-09 21:41:03 -050079
Paul Kehrer45064282013-10-17 13:41:53 -050080
Paul Kehrerd0ec60e2013-10-16 08:46:50 -050081.. class:: cryptography.primitives.block.modes.CTR(nonce)
82
Paul Kehrer45064282013-10-17 13:41:53 -050083 .. warning::
84
85 Counter mode is not recommended for use with block ciphers that have a
86 block size of less than 128-bits.
87
Paul Kehrerd0ec60e2013-10-16 08:46:50 -050088 CTR (Counter) is a mode of operation for block ciphers. It is considered
89 cryptographically strong.
90
Paul Kehrer45064282013-10-17 13:41:53 -050091 :param bytes nonce: Recommended to be random. It is critical to never reuse
92 a ```nonce``` (or its subsequent incremented values)
93 with a given key. Any reuse of the nonce with the same
94 key compromises the security of every message encrypted
95 with that key. Must be the same number of bytes as the
96 ```block_size``` of the cipher with a given key. The
97 nonce does not need to be kept secret and may be
98 included alongside the ciphertext.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -050099
David Reidf1a39bd2013-09-11 16:28:42 -0700100.. class:: cryptography.primitives.block.modes.OFB(initialization_vector)
Paul Kehrer6f412a02013-09-10 21:30:50 -0500101
102 OFB (Output Feedback) is a mode of operation for block ciphers. It
103 transforms a block cipher into a stream cipher.
104
David Reidf1a39bd2013-09-11 16:28:42 -0700105 :param bytes initialization_vector: Must be random bytes. They do not need
106 to be kept secret (they can be included
107 in a transmitted message). Must be the
108 same number of bytes as the
109 ``block_size`` of the cipher. Do not
110 reuse an ``initialization_vector`` with
111 a given ``key``.
Paul Kehrer6f412a02013-09-10 21:30:50 -0500112
Paul Kehrer4223df72013-09-11 09:48:04 -0500113.. class:: cryptography.primitives.block.modes.CFB(initialization_vector)
114
115 CFB (Cipher Feedback) is a mode of operation for block ciphers. It
116 transforms a block cipher into a stream cipher.
117
118 :param bytes initialization_vector: Must be random bytes. They do not need
119 to be kept secret (they can be included
120 in a transmitted message). Must be the
121 same number of bytes as the
122 ``block_size`` of the cipher. Do not
123 reuse an ``initialization_vector`` with
124 a given ``key``.
125
Paul Kehrer13f108f2013-09-09 21:41:03 -0500126
127Insecure Modes
128--------------
129
Alex Gaynorcd413a32013-09-10 18:59:43 -0700130.. warning::
131
132 These modes are insecure. New applications should never make use of them,
133 and existing applications should strongly consider migrating away.
134
135
Paul Kehrer13f108f2013-09-09 21:41:03 -0500136.. class:: cryptography.primitives.block.modes.ECB()
137
138 ECB (Electronic Code Book) is the simplest mode of operation for block
Alex Gaynorcd413a32013-09-10 18:59:43 -0700139 ciphers. Each block of data is encrypted in the same way. This means
140 identical plaintext blocks will always result in identical ciphertext
141 blocks, and thus result in information leakage