Donald Stufft | d8f0118 | 2013-10-27 16:59:56 -0400 | [diff] [blame] | 1 | .. danger:: |
| 2 | |
| 3 | This is a "Hazardous Materials" module. You should **ONLY** use it if |
| 4 | you're 100% absolutely sure that you know what you're doing because this |
| 5 | module is full of land mines, dragons, and dinosaurs with laser guns. |
| 6 | |
| 7 | |
Donald Stufft | e51fb93 | 2013-10-27 17:26:17 -0400 | [diff] [blame] | 8 | Symmetric Encryption |
| 9 | ==================== |
| 10 | |
Donald Stufft | f04317a | 2013-10-27 16:44:30 -0400 | [diff] [blame] | 11 | .. currentmodule:: cryptography.hazmat.primitives.block |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 12 | |
Donald Stufft | 173de98 | 2013-08-12 07:34:39 -0400 | [diff] [blame] | 13 | .. testsetup:: |
| 14 | |
| 15 | import binascii |
| 16 | key = binascii.unhexlify(b"0" * 32) |
| 17 | iv = binascii.unhexlify(b"0" * 32) |
| 18 | |
| 19 | |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 20 | Symmetric encryption is a way to encrypt (hide the plaintext value) material |
| 21 | where the encrypter and decrypter both use the same key. |
| 22 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 23 | .. class:: BlockCipher(cipher, mode) |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 24 | |
Alex Gaynor | 65678d0 | 2013-08-08 15:19:19 -0700 | [diff] [blame] | 25 | Block ciphers work by encrypting content in chunks, often 64- or 128-bits. |
Alex Gaynor | b12f76e | 2013-08-08 19:05:18 -0700 | [diff] [blame] | 26 | They combine an underlying algorithm (such as AES), with a mode (such as |
Paul Kehrer | d1afe39 | 2013-10-22 08:24:44 -0500 | [diff] [blame] | 27 | CBC, CTR, or GCM). A simple example of encrypting (and then decrypting) |
Paul Kehrer | 653463f | 2013-10-21 17:55:01 -0500 | [diff] [blame] | 28 | content with AES is: |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 29 | |
Donald Stufft | 173de98 | 2013-08-12 07:34:39 -0400 | [diff] [blame] | 30 | .. doctest:: |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 31 | |
Donald Stufft | f04317a | 2013-10-27 16:44:30 -0400 | [diff] [blame] | 32 | >>> from cryptography.hazmat.primitives.block import BlockCipher, ciphers, modes |
Alex Gaynor | acc787a | 2013-08-10 15:52:40 -0400 | [diff] [blame] | 33 | >>> cipher = BlockCipher(ciphers.AES(key), modes.CBC(iv)) |
Paul Kehrer | 3e0895c | 2013-10-21 22:19:29 -0500 | [diff] [blame] | 34 | >>> encryptor = cipher.encryptor() |
| 35 | >>> ct = encryptor.update(b"a secret message") + encryptor.finalize() |
| 36 | >>> decryptor = cipher.decryptor() |
| 37 | >>> decryptor.update(ct) + decryptor.finalize() |
Paul Kehrer | f6cf956 | 2013-10-22 10:36:00 -0500 | [diff] [blame] | 38 | 'a secret message' |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 39 | |
Alex Gaynor | e62aa40 | 2013-08-08 15:23:11 -0700 | [diff] [blame] | 40 | :param cipher: One of the ciphers described below. |
| 41 | :param mode: One of the modes described below. |
Alex Gaynor | 0ca7fdb | 2013-08-08 07:35:26 -0700 | [diff] [blame] | 42 | |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 43 | .. method:: encryptor() |
Alex Gaynor | 09515f0 | 2013-08-08 15:26:55 -0700 | [diff] [blame] | 44 | |
David Reid | 63ba665 | 2013-10-22 14:09:19 -0700 | [diff] [blame] | 45 | :return: An encrypting |
Donald Stufft | f04317a | 2013-10-27 16:44:30 -0400 | [diff] [blame] | 46 | :class:`~cryptography.hazmat.primitives.interfaces.CipherContext` |
David Reid | 63ba665 | 2013-10-22 14:09:19 -0700 | [diff] [blame] | 47 | provider. |
Alex Gaynor | e62aa40 | 2013-08-08 15:23:11 -0700 | [diff] [blame] | 48 | |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 49 | .. method:: decryptor() |
| 50 | |
David Reid | 63ba665 | 2013-10-22 14:09:19 -0700 | [diff] [blame] | 51 | :return: A decrypting |
Donald Stufft | f04317a | 2013-10-27 16:44:30 -0400 | [diff] [blame] | 52 | :class:`~cryptography.hazmat.primitives.interfaces.CipherContext` |
David Reid | 63ba665 | 2013-10-22 14:09:19 -0700 | [diff] [blame] | 53 | provider. |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 54 | |
Donald Stufft | f04317a | 2013-10-27 16:44:30 -0400 | [diff] [blame] | 55 | .. currentmodule:: cryptography.hazmat.primitives.interfaces |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 56 | |
Alex Gaynor | b2d5efd | 2013-10-29 11:15:30 -0700 | [diff] [blame^] | 57 | .. class:: CipherContext |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 58 | |
Alex Gaynor | b2d5efd | 2013-10-29 11:15:30 -0700 | [diff] [blame^] | 59 | When calling ``encryptor()`` or ``decryptor()`` on a ``BlockCipher`` object |
| 60 | you will receive a return object conforming to the ``CipherContext`` |
| 61 | interface. You can then call ``update(data)`` with data until you have fed |
| 62 | everything into the context. Once that is done call ``finalize()`` to |
| 63 | finish the operation and obtain the remainder of the data. |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 64 | |
| 65 | .. method:: update(data) |
| 66 | |
Alex Gaynor | b2d5efd | 2013-10-29 11:15:30 -0700 | [diff] [blame^] | 67 | :param bytes data: The data you wish to pass into the context. |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 68 | :return bytes: Returns the data that was encrypted or decrypted. |
Alex Gaynor | e62aa40 | 2013-08-08 15:23:11 -0700 | [diff] [blame] | 69 | |
| 70 | .. method:: finalize() |
| 71 | |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 72 | :return bytes: Returns the remainder of the data. |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 73 | |
| 74 | Ciphers |
| 75 | ~~~~~~~ |
| 76 | |
Donald Stufft | f04317a | 2013-10-27 16:44:30 -0400 | [diff] [blame] | 77 | .. currentmodule:: cryptography.hazmat.primitives.block.ciphers |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 78 | |
| 79 | .. class:: AES(key) |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 80 | |
Alex Gaynor | 1e3f81f | 2013-08-08 11:31:43 -0700 | [diff] [blame] | 81 | AES (Advanced Encryption Standard) is a block cipher standardized by NIST. |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 82 | AES is both fast, and cryptographically strong. It is a good default |
| 83 | choice for encryption. |
| 84 | |
| 85 | :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits. |
Alex Gaynor | 48ec9a3 | 2013-08-08 11:13:46 -0700 | [diff] [blame] | 86 | This must be kept secret. |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 87 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 88 | .. class:: Camellia(key) |
Paul Kehrer | dff22d4 | 2013-09-27 13:43:06 -0500 | [diff] [blame] | 89 | |
| 90 | Camellia is a block cipher approved for use by CRYPTREC and ISO/IEC. |
| 91 | It is considered to have comparable security and performance to AES, but |
| 92 | is not as widely studied or deployed. |
| 93 | |
| 94 | :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits. |
| 95 | This must be kept secret. |
| 96 | |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 97 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 98 | .. class:: TripleDES(key) |
Alex Gaynor | aeb714c | 2013-09-09 18:06:14 -0700 | [diff] [blame] | 99 | |
Alex Gaynor | 2f355d1 | 2013-09-09 18:09:26 -0700 | [diff] [blame] | 100 | Triple DES (Data Encryption Standard), sometimes refered to as 3DES, is a |
Alex Gaynor | 17adce6 | 2013-10-16 17:04:40 -0700 | [diff] [blame] | 101 | block cipher standardized by NIST. Triple DES has known cryptoanalytic |
| 102 | flaws, however none of them currently enable a practical attack. |
| 103 | Nonetheless, Triples DES is not reccomended for new applications because it |
Alex Gaynor | fbcc564 | 2013-10-22 08:26:00 -0700 | [diff] [blame] | 104 | is incredibly slow; old applications should consider moving away from it. |
Alex Gaynor | aeb714c | 2013-09-09 18:06:14 -0700 | [diff] [blame] | 105 | |
| 106 | :param bytes key: The secret key, either ``64``, ``128``, or ``192`` bits |
| 107 | (note that DES functionally uses ``56``, ``112``, or |
| 108 | ``168`` bits of the key, there is a parity byte in each |
| 109 | component of the key), in some materials these are |
| 110 | referred to as being up to three separate keys (each |
| 111 | ``56`` bits long), they can simply be concatenated to |
| 112 | produce the full key. This must be kept secret. |
| 113 | |
| 114 | |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 115 | Modes |
| 116 | ~~~~~ |
| 117 | |
Donald Stufft | f04317a | 2013-10-27 16:44:30 -0400 | [diff] [blame] | 118 | .. currentmodule:: cryptography.hazmat.primitives.block.modes |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 119 | |
| 120 | .. class:: CBC(initialization_vector) |
Alex Gaynor | 48ec9a3 | 2013-08-08 11:13:46 -0700 | [diff] [blame] | 121 | |
| 122 | CBC (Cipher block chaining) is a mode of operation for block ciphers. It is |
| 123 | considered cryptographically strong. |
| 124 | |
| 125 | :param bytes initialization_vector: Must be random bytes. They do not need |
| 126 | to be kept secret (they can be included |
Alex Gaynor | 2dc2b86 | 2013-08-08 11:58:04 -0700 | [diff] [blame] | 127 | in a transmitted message). Must be the |
| 128 | same number of bytes as the |
Alex Gaynor | 6badd9b | 2013-08-08 14:59:53 -0700 | [diff] [blame] | 129 | ``block_size`` of the cipher. Do not |
| 130 | reuse an ``initialization_vector`` with |
| 131 | a given ``key``. |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 132 | |
Paul Kehrer | 4506428 | 2013-10-17 13:41:53 -0500 | [diff] [blame] | 133 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 134 | .. class:: CTR(nonce) |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 135 | |
Paul Kehrer | 4506428 | 2013-10-17 13:41:53 -0500 | [diff] [blame] | 136 | .. warning:: |
| 137 | |
| 138 | Counter mode is not recommended for use with block ciphers that have a |
| 139 | block size of less than 128-bits. |
| 140 | |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 141 | CTR (Counter) is a mode of operation for block ciphers. It is considered |
| 142 | cryptographically strong. |
| 143 | |
Paul Kehrer | 89b3dd3 | 2013-10-17 14:02:45 -0500 | [diff] [blame] | 144 | :param bytes nonce: Should be random bytes. It is critical to never reuse a |
| 145 | ``nonce`` with a given key. Any reuse of a nonce |
| 146 | with the same key compromises the security of every |
| 147 | message encrypted with that key. Must be the same |
| 148 | number of bytes as the ``block_size`` of the cipher |
| 149 | with a given key. The nonce does not need to be kept |
| 150 | secret and may be included alongside the ciphertext. |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 151 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 152 | .. class:: OFB(initialization_vector) |
Paul Kehrer | 6f412a0 | 2013-09-10 21:30:50 -0500 | [diff] [blame] | 153 | |
| 154 | OFB (Output Feedback) is a mode of operation for block ciphers. It |
| 155 | transforms a block cipher into a stream cipher. |
| 156 | |
David Reid | f1a39bd | 2013-09-11 16:28:42 -0700 | [diff] [blame] | 157 | :param bytes initialization_vector: Must be random bytes. They do not need |
| 158 | to be kept secret (they can be included |
| 159 | in a transmitted message). Must be the |
| 160 | same number of bytes as the |
| 161 | ``block_size`` of the cipher. Do not |
| 162 | reuse an ``initialization_vector`` with |
| 163 | a given ``key``. |
Paul Kehrer | 6f412a0 | 2013-09-10 21:30:50 -0500 | [diff] [blame] | 164 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 165 | .. class:: CFB(initialization_vector) |
Paul Kehrer | 4223df7 | 2013-09-11 09:48:04 -0500 | [diff] [blame] | 166 | |
| 167 | CFB (Cipher Feedback) is a mode of operation for block ciphers. It |
| 168 | transforms a block cipher into a stream cipher. |
| 169 | |
| 170 | :param bytes initialization_vector: Must be random bytes. They do not need |
| 171 | to be kept secret (they can be included |
| 172 | in a transmitted message). Must be the |
| 173 | same number of bytes as the |
| 174 | ``block_size`` of the cipher. Do not |
| 175 | reuse an ``initialization_vector`` with |
| 176 | a given ``key``. |
| 177 | |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 178 | |
| 179 | Insecure Modes |
| 180 | -------------- |
| 181 | |
Alex Gaynor | cd413a3 | 2013-09-10 18:59:43 -0700 | [diff] [blame] | 182 | .. warning:: |
| 183 | |
| 184 | These modes are insecure. New applications should never make use of them, |
| 185 | and existing applications should strongly consider migrating away. |
| 186 | |
| 187 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 188 | .. class:: ECB() |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 189 | |
| 190 | ECB (Electronic Code Book) is the simplest mode of operation for block |
Alex Gaynor | cd413a3 | 2013-09-10 18:59:43 -0700 | [diff] [blame] | 191 | ciphers. Each block of data is encrypted in the same way. This means |
| 192 | identical plaintext blocks will always result in identical ciphertext |
| 193 | blocks, and thus result in information leakage |