blob: 1b8d1d735de23eda9929d65683e479c4810265ff [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
Alex Gaynord96d1002013-08-08 07:37:26 -070054
55Modes
56~~~~~
57
Alex Gaynor641a3a02013-08-10 15:46:07 -040058.. class:: cryptography.primitives.block.modes.CBC(initialization_vector)
Alex Gaynor48ec9a32013-08-08 11:13:46 -070059
60 CBC (Cipher block chaining) is a mode of operation for block ciphers. It is
61 considered cryptographically strong.
62
63 :param bytes initialization_vector: Must be random bytes. They do not need
64 to be kept secret (they can be included
Alex Gaynor2dc2b862013-08-08 11:58:04 -070065 in a transmitted message). Must be the
66 same number of bytes as the
Alex Gaynor6badd9b2013-08-08 14:59:53 -070067 ``block_size`` of the cipher. Do not
68 reuse an ``initialization_vector`` with
69 a given ``key``.