blob: a8d9485d05c5553c0c399025dec7ce5abac0c163 [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 Kehrer653463f2013-10-21 17:55:01 -050018 CBC, CTR, or GCM). A simple example of encrypting (and then decrypting)
19 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 Kehrer653463f2013-10-21 17:55:01 -050025 >>> encrypt = cipher.encryptor()
26 >>> ct = encrypt.update(b"a secret message") + encrypt.finalize()
27 >>> decrypt = cipher.decryptor()
28 >>> decrypt.update(ct) + decrypt.finalize()
Donald Stufft173de982013-08-12 07:34:39 -040029 '...'
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
Alex Gaynor09515f02013-08-08 15:26:55 -070034 ``encrypt()`` should be called repeatedly with new plaintext, and once the
35 full plaintext is fed in, ``finalize()`` should be called.
36
Alex Gaynore62aa402013-08-08 15:23:11 -070037 .. method:: encrypt(plaintext)
38
39 :param bytes plaintext: The text you wish to encrypt.
40 :return bytes: Returns the ciphertext that was added.
41
42 .. method:: finalize()
43
44 :return bytes: Returns the remainder of the ciphertext.
Alex Gaynord96d1002013-08-08 07:37:26 -070045
46Ciphers
47~~~~~~~
48
Alex Gaynor641a3a02013-08-10 15:46:07 -040049.. class:: cryptography.primitives.block.ciphers.AES(key)
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070050
Alex Gaynor1e3f81f2013-08-08 11:31:43 -070051 AES (Advanced Encryption Standard) is a block cipher standardized by NIST.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070052 AES is both fast, and cryptographically strong. It is a good default
53 choice for encryption.
54
55 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
Alex Gaynor48ec9a32013-08-08 11:13:46 -070056 This must be kept secret.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070057
Paul Kehrerdff22d42013-09-27 13:43:06 -050058.. class:: cryptography.primitives.block.ciphers.Camellia(key)
59
60 Camellia is a block cipher approved for use by CRYPTREC and ISO/IEC.
61 It is considered to have comparable security and performance to AES, but
62 is not as widely studied or deployed.
63
64 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
65 This must be kept secret.
66
Alex Gaynord96d1002013-08-08 07:37:26 -070067
68Modes
69~~~~~
70
Alex Gaynor641a3a02013-08-10 15:46:07 -040071.. class:: cryptography.primitives.block.modes.CBC(initialization_vector)
Alex Gaynor48ec9a32013-08-08 11:13:46 -070072
73 CBC (Cipher block chaining) is a mode of operation for block ciphers. It is
74 considered cryptographically strong.
75
76 :param bytes initialization_vector: Must be random bytes. They do not need
77 to be kept secret (they can be included
Alex Gaynor2dc2b862013-08-08 11:58:04 -070078 in a transmitted message). Must be the
79 same number of bytes as the
Alex Gaynor6badd9b2013-08-08 14:59:53 -070080 ``block_size`` of the cipher. Do not
81 reuse an ``initialization_vector`` with
82 a given ``key``.
Paul Kehrer13f108f2013-09-09 21:41:03 -050083
Paul Kehrer45064282013-10-17 13:41:53 -050084
Paul Kehrerd0ec60e2013-10-16 08:46:50 -050085.. class:: cryptography.primitives.block.modes.CTR(nonce)
86
Paul Kehrer45064282013-10-17 13:41:53 -050087 .. warning::
88
89 Counter mode is not recommended for use with block ciphers that have a
90 block size of less than 128-bits.
91
Paul Kehrerd0ec60e2013-10-16 08:46:50 -050092 CTR (Counter) is a mode of operation for block ciphers. It is considered
93 cryptographically strong.
94
Paul Kehrer89b3dd32013-10-17 14:02:45 -050095 :param bytes nonce: Should be random bytes. It is critical to never reuse a
96 ``nonce`` with a given key. Any reuse of a nonce
97 with the same key compromises the security of every
98 message encrypted with that key. Must be the same
99 number of bytes as the ``block_size`` of the cipher
100 with a given key. The nonce does not need to be kept
101 secret and may be included alongside the ciphertext.
Paul Kehrerd0ec60e2013-10-16 08:46:50 -0500102
David Reidf1a39bd2013-09-11 16:28:42 -0700103.. class:: cryptography.primitives.block.modes.OFB(initialization_vector)
Paul Kehrer6f412a02013-09-10 21:30:50 -0500104
105 OFB (Output Feedback) is a mode of operation for block ciphers. It
106 transforms a block cipher into a stream cipher.
107
David Reidf1a39bd2013-09-11 16:28:42 -0700108 :param bytes initialization_vector: Must be random bytes. They do not need
109 to be kept secret (they can be included
110 in a transmitted message). Must be the
111 same number of bytes as the
112 ``block_size`` of the cipher. Do not
113 reuse an ``initialization_vector`` with
114 a given ``key``.
Paul Kehrer6f412a02013-09-10 21:30:50 -0500115
Paul Kehrer4223df72013-09-11 09:48:04 -0500116.. class:: cryptography.primitives.block.modes.CFB(initialization_vector)
117
118 CFB (Cipher Feedback) is a mode of operation for block ciphers. It
119 transforms a block cipher into a stream cipher.
120
121 :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``.
128
Paul Kehrer13f108f2013-09-09 21:41:03 -0500129
130Insecure Modes
131--------------
132
Alex Gaynorcd413a32013-09-10 18:59:43 -0700133.. warning::
134
135 These modes are insecure. New applications should never make use of them,
136 and existing applications should strongly consider migrating away.
137
138
Paul Kehrer13f108f2013-09-09 21:41:03 -0500139.. class:: cryptography.primitives.block.modes.ECB()
140
141 ECB (Electronic Code Book) is the simplest mode of operation for block
Alex Gaynorcd413a32013-09-10 18:59:43 -0700142 ciphers. Each block of data is encrypted in the same way. This means
143 identical plaintext blocks will always result in identical ciphertext
144 blocks, and thus result in information leakage