Alex Gaynor | af82d5e | 2013-10-29 17:07:24 -0700 | [diff] [blame] | 1 | .. hazmat:: |
Donald Stufft | d8f0118 | 2013-10-27 16:59:56 -0400 | [diff] [blame] | 2 | |
| 3 | |
Donald Stufft | e51fb93 | 2013-10-27 17:26:17 -0400 | [diff] [blame] | 4 | Symmetric Encryption |
| 5 | ==================== |
| 6 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 7 | .. currentmodule:: cryptography.hazmat.primitives.ciphers |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 8 | |
Donald Stufft | 173de98 | 2013-08-12 07:34:39 -0400 | [diff] [blame] | 9 | .. testsetup:: |
| 10 | |
| 11 | import binascii |
| 12 | key = binascii.unhexlify(b"0" * 32) |
| 13 | iv = binascii.unhexlify(b"0" * 32) |
| 14 | |
| 15 | |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 16 | Symmetric encryption is a way to encrypt (hide the plaintext value) material |
| 17 | where the encrypter and decrypter both use the same key. |
| 18 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 19 | .. class:: Cipher(algorithm, mode) |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 20 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 21 | Cipher objects combine an algorithm (such as AES) with a mode (such as |
Paul Kehrer | d1afe39 | 2013-10-22 08:24:44 -0500 | [diff] [blame] | 22 | CBC, CTR, or GCM). A simple example of encrypting (and then decrypting) |
Paul Kehrer | 653463f | 2013-10-21 17:55:01 -0500 | [diff] [blame] | 23 | content with AES is: |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 24 | |
Donald Stufft | 173de98 | 2013-08-12 07:34:39 -0400 | [diff] [blame] | 25 | .. doctest:: |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 26 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 27 | >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes |
| 28 | >>> cipher = Cipher(algorithms.AES(key), modes.CBC(iv)) |
Paul Kehrer | 3e0895c | 2013-10-21 22:19:29 -0500 | [diff] [blame] | 29 | >>> encryptor = cipher.encryptor() |
| 30 | >>> ct = encryptor.update(b"a secret message") + encryptor.finalize() |
| 31 | >>> decryptor = cipher.decryptor() |
| 32 | >>> decryptor.update(ct) + decryptor.finalize() |
Paul Kehrer | f6cf956 | 2013-10-22 10:36:00 -0500 | [diff] [blame] | 33 | 'a secret message' |
Alex Gaynor | f6c47e9 | 2013-08-08 07:16:01 -0700 | [diff] [blame] | 34 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 35 | :param algorithms: One of the algorithms described below. |
Alex Gaynor | e62aa40 | 2013-08-08 15:23:11 -0700 | [diff] [blame] | 36 | :param mode: One of the modes described below. |
Alex Gaynor | 0ca7fdb | 2013-08-08 07:35:26 -0700 | [diff] [blame] | 37 | |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 38 | .. method:: encryptor() |
Alex Gaynor | 09515f0 | 2013-08-08 15:26:55 -0700 | [diff] [blame] | 39 | |
David Reid | 63ba665 | 2013-10-22 14:09:19 -0700 | [diff] [blame] | 40 | :return: An encrypting |
Donald Stufft | f04317a | 2013-10-27 16:44:30 -0400 | [diff] [blame] | 41 | :class:`~cryptography.hazmat.primitives.interfaces.CipherContext` |
David Reid | 63ba665 | 2013-10-22 14:09:19 -0700 | [diff] [blame] | 42 | provider. |
Alex Gaynor | e62aa40 | 2013-08-08 15:23:11 -0700 | [diff] [blame] | 43 | |
Alex Gaynor | f1a3fc0 | 2013-11-02 14:03:34 -0700 | [diff] [blame] | 44 | If the backend doesn't support the requested combination of ``cipher`` |
Alex Gaynor | 3949f11 | 2013-11-02 16:57:10 -0700 | [diff] [blame] | 45 | and ``mode`` an :class:`cryptography.exceptions.UnsupportedAlgorithm` |
| 46 | will be raised. |
Alex Gaynor | f1a3fc0 | 2013-11-02 14:03:34 -0700 | [diff] [blame] | 47 | |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 48 | .. method:: decryptor() |
| 49 | |
David Reid | 63ba665 | 2013-10-22 14:09:19 -0700 | [diff] [blame] | 50 | :return: A decrypting |
Donald Stufft | f04317a | 2013-10-27 16:44:30 -0400 | [diff] [blame] | 51 | :class:`~cryptography.hazmat.primitives.interfaces.CipherContext` |
David Reid | 63ba665 | 2013-10-22 14:09:19 -0700 | [diff] [blame] | 52 | provider. |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 53 | |
Alex Gaynor | f1a3fc0 | 2013-11-02 14:03:34 -0700 | [diff] [blame] | 54 | If the backend doesn't support the requested combination of ``cipher`` |
Alex Gaynor | 3949f11 | 2013-11-02 16:57:10 -0700 | [diff] [blame] | 55 | and ``mode`` an :class:`cryptography.exceptions.UnsupportedAlgorithm` |
| 56 | will be raised. |
Alex Gaynor | f1a3fc0 | 2013-11-02 14:03:34 -0700 | [diff] [blame] | 57 | |
| 58 | |
Donald Stufft | f04317a | 2013-10-27 16:44:30 -0400 | [diff] [blame] | 59 | .. currentmodule:: cryptography.hazmat.primitives.interfaces |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 60 | |
Alex Gaynor | b2d5efd | 2013-10-29 11:15:30 -0700 | [diff] [blame] | 61 | .. class:: CipherContext |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 62 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 63 | When calling ``encryptor()`` or ``decryptor()`` on a ``Cipher`` object |
Alex Gaynor | b2d5efd | 2013-10-29 11:15:30 -0700 | [diff] [blame] | 64 | you will receive a return object conforming to the ``CipherContext`` |
| 65 | interface. You can then call ``update(data)`` with data until you have fed |
| 66 | everything into the context. Once that is done call ``finalize()`` to |
| 67 | finish the operation and obtain the remainder of the data. |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 68 | |
| 69 | .. method:: update(data) |
| 70 | |
Alex Gaynor | b2d5efd | 2013-10-29 11:15:30 -0700 | [diff] [blame] | 71 | :param bytes data: The data you wish to pass into the context. |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 72 | :return bytes: Returns the data that was encrypted or decrypted. |
Alex Gaynor | e62aa40 | 2013-08-08 15:23:11 -0700 | [diff] [blame] | 73 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 74 | When the ``Cipher`` was constructed in a mode that turns it into a |
Alex Gaynor | fc09a7c | 2013-11-01 14:43:02 -0700 | [diff] [blame] | 75 | stream cipher (e.g. |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 76 | :class:`cryptography.hazmat.primitives.ciphers.modes.CTR`), this will |
Alex Gaynor | bf2de74 | 2013-11-01 14:48:19 -0700 | [diff] [blame] | 77 | return bytes immediately, however in other modes it will return chunks, |
| 78 | whose size is determined by the cipher's block size. |
Alex Gaynor | d1f0201 | 2013-11-01 14:12:35 -0700 | [diff] [blame] | 79 | |
Alex Gaynor | e62aa40 | 2013-08-08 15:23:11 -0700 | [diff] [blame] | 80 | .. method:: finalize() |
| 81 | |
Paul Kehrer | 5399fd0 | 2013-10-21 23:48:25 -0500 | [diff] [blame] | 82 | :return bytes: Returns the remainder of the data. |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 83 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 84 | Algorithms |
| 85 | ~~~~~~~~~~ |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 86 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 87 | .. currentmodule:: cryptography.hazmat.primitives.ciphers.algorithms |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 88 | |
| 89 | .. class:: AES(key) |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 90 | |
Alex Gaynor | 1e3f81f | 2013-08-08 11:31:43 -0700 | [diff] [blame] | 91 | AES (Advanced Encryption Standard) is a block cipher standardized by NIST. |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 92 | AES is both fast, and cryptographically strong. It is a good default |
| 93 | choice for encryption. |
| 94 | |
| 95 | :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits. |
Alex Gaynor | 48ec9a3 | 2013-08-08 11:13:46 -0700 | [diff] [blame] | 96 | This must be kept secret. |
Alex Gaynor | 5ba2dfa | 2013-08-08 11:04:44 -0700 | [diff] [blame] | 97 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 98 | .. class:: Camellia(key) |
Paul Kehrer | dff22d4 | 2013-09-27 13:43:06 -0500 | [diff] [blame] | 99 | |
| 100 | Camellia is a block cipher approved for use by CRYPTREC and ISO/IEC. |
| 101 | It is considered to have comparable security and performance to AES, but |
| 102 | is not as widely studied or deployed. |
| 103 | |
| 104 | :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits. |
| 105 | This must be kept secret. |
| 106 | |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 107 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 108 | .. class:: TripleDES(key) |
Alex Gaynor | aeb714c | 2013-09-09 18:06:14 -0700 | [diff] [blame] | 109 | |
Alex Gaynor | 2f355d1 | 2013-09-09 18:09:26 -0700 | [diff] [blame] | 110 | Triple DES (Data Encryption Standard), sometimes refered to as 3DES, is a |
Alex Gaynor | 17adce6 | 2013-10-16 17:04:40 -0700 | [diff] [blame] | 111 | block cipher standardized by NIST. Triple DES has known cryptoanalytic |
| 112 | flaws, however none of them currently enable a practical attack. |
| 113 | Nonetheless, Triples DES is not reccomended for new applications because it |
Alex Gaynor | fbcc564 | 2013-10-22 08:26:00 -0700 | [diff] [blame] | 114 | is incredibly slow; old applications should consider moving away from it. |
Alex Gaynor | aeb714c | 2013-09-09 18:06:14 -0700 | [diff] [blame] | 115 | |
| 116 | :param bytes key: The secret key, either ``64``, ``128``, or ``192`` bits |
| 117 | (note that DES functionally uses ``56``, ``112``, or |
| 118 | ``168`` bits of the key, there is a parity byte in each |
| 119 | component of the key), in some materials these are |
| 120 | referred to as being up to three separate keys (each |
| 121 | ``56`` bits long), they can simply be concatenated to |
| 122 | produce the full key. This must be kept secret. |
| 123 | |
Paul Kehrer | 6022d45 | 2013-10-30 17:03:54 -0500 | [diff] [blame] | 124 | .. class:: CAST5(key) |
| 125 | |
| 126 | CAST5 (also known as CAST-128) is a block cipher approved for use in the |
| 127 | Canadian government by their Communications Security Establishment. It is a |
| 128 | variable key length cipher and supports keys from 40-128 bits in length. |
| 129 | |
| 130 | :param bytes key: The secret key, 40-128 bits in length (in increments of |
| 131 | 8). This must be kept secret. |
| 132 | |
Paul Kehrer | 3446d81 | 2013-10-31 17:15:03 -0500 | [diff] [blame] | 133 | Weak Ciphers |
| 134 | ------------ |
| 135 | |
| 136 | .. warning:: |
| 137 | |
| 138 | These ciphers are considered weak for a variety of reasons. New |
| 139 | applications should avoid their use and existing applications should |
| 140 | strongly consider migrating away. |
| 141 | |
Paul Kehrer | 5df0abe | 2013-10-30 16:57:04 -0500 | [diff] [blame] | 142 | .. class:: Blowfish(key) |
| 143 | |
| 144 | Blowfish is a block cipher developed by Bruce Schneier. It is known to be |
| 145 | susceptible to attacks when using weak keys. The author has recommended |
Paul Kehrer | 3446d81 | 2013-10-31 17:15:03 -0500 | [diff] [blame] | 146 | that users of Blowfish move to newer algorithms like |
| 147 | :class:`AES`. |
Paul Kehrer | 5df0abe | 2013-10-30 16:57:04 -0500 | [diff] [blame] | 148 | |
| 149 | :param bytes key: The secret key, 32-448 bits in length (in increments of |
| 150 | 8). This must be kept secret. |
| 151 | |
David Reid | 30722b9 | 2013-11-07 13:03:39 -0800 | [diff] [blame^] | 152 | |
| 153 | .. _symmetric-encryption-modes: |
| 154 | |
Alex Gaynor | d96d100 | 2013-08-08 07:37:26 -0700 | [diff] [blame] | 155 | Modes |
| 156 | ~~~~~ |
| 157 | |
Paul Kehrer | 051099e | 2013-11-06 15:53:40 +0800 | [diff] [blame] | 158 | .. currentmodule:: cryptography.hazmat.primitives.ciphers.modes |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 159 | |
| 160 | .. class:: CBC(initialization_vector) |
Alex Gaynor | 48ec9a3 | 2013-08-08 11:13:46 -0700 | [diff] [blame] | 161 | |
| 162 | CBC (Cipher block chaining) is a mode of operation for block ciphers. It is |
| 163 | considered cryptographically strong. |
| 164 | |
| 165 | :param bytes initialization_vector: Must be random bytes. They do not need |
| 166 | to be kept secret (they can be included |
Alex Gaynor | 2dc2b86 | 2013-08-08 11:58:04 -0700 | [diff] [blame] | 167 | in a transmitted message). Must be the |
| 168 | same number of bytes as the |
Alex Gaynor | 6badd9b | 2013-08-08 14:59:53 -0700 | [diff] [blame] | 169 | ``block_size`` of the cipher. Do not |
| 170 | reuse an ``initialization_vector`` with |
| 171 | a given ``key``. |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 172 | |
Paul Kehrer | 4506428 | 2013-10-17 13:41:53 -0500 | [diff] [blame] | 173 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 174 | .. class:: CTR(nonce) |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 175 | |
Paul Kehrer | 4506428 | 2013-10-17 13:41:53 -0500 | [diff] [blame] | 176 | .. warning:: |
| 177 | |
| 178 | Counter mode is not recommended for use with block ciphers that have a |
| 179 | block size of less than 128-bits. |
| 180 | |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 181 | CTR (Counter) is a mode of operation for block ciphers. It is considered |
Alex Gaynor | d1f0201 | 2013-11-01 14:12:35 -0700 | [diff] [blame] | 182 | cryptographically strong. It transforms a block cipher into a stream |
| 183 | cipher. |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 184 | |
Paul Kehrer | 89b3dd3 | 2013-10-17 14:02:45 -0500 | [diff] [blame] | 185 | :param bytes nonce: Should be random bytes. It is critical to never reuse a |
| 186 | ``nonce`` with a given key. Any reuse of a nonce |
| 187 | with the same key compromises the security of every |
| 188 | message encrypted with that key. Must be the same |
| 189 | number of bytes as the ``block_size`` of the cipher |
| 190 | with a given key. The nonce does not need to be kept |
| 191 | secret and may be included alongside the ciphertext. |
Paul Kehrer | d0ec60e | 2013-10-16 08:46:50 -0500 | [diff] [blame] | 192 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 193 | .. class:: OFB(initialization_vector) |
Paul Kehrer | 6f412a0 | 2013-09-10 21:30:50 -0500 | [diff] [blame] | 194 | |
| 195 | OFB (Output Feedback) is a mode of operation for block ciphers. It |
| 196 | transforms a block cipher into a stream cipher. |
| 197 | |
David Reid | f1a39bd | 2013-09-11 16:28:42 -0700 | [diff] [blame] | 198 | :param bytes initialization_vector: Must be random bytes. They do not need |
| 199 | to be kept secret (they can be included |
| 200 | in a transmitted message). Must be the |
| 201 | same number of bytes as the |
| 202 | ``block_size`` of the cipher. Do not |
| 203 | reuse an ``initialization_vector`` with |
| 204 | a given ``key``. |
Paul Kehrer | 6f412a0 | 2013-09-10 21:30:50 -0500 | [diff] [blame] | 205 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 206 | .. class:: CFB(initialization_vector) |
Paul Kehrer | 4223df7 | 2013-09-11 09:48:04 -0500 | [diff] [blame] | 207 | |
| 208 | CFB (Cipher Feedback) is a mode of operation for block ciphers. It |
| 209 | transforms a block cipher into a stream cipher. |
| 210 | |
| 211 | :param bytes initialization_vector: Must be random bytes. They do not need |
| 212 | to be kept secret (they can be included |
| 213 | in a transmitted message). Must be the |
| 214 | same number of bytes as the |
| 215 | ``block_size`` of the cipher. Do not |
| 216 | reuse an ``initialization_vector`` with |
| 217 | a given ``key``. |
| 218 | |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 219 | |
| 220 | Insecure Modes |
| 221 | -------------- |
| 222 | |
Alex Gaynor | cd413a3 | 2013-09-10 18:59:43 -0700 | [diff] [blame] | 223 | .. warning:: |
| 224 | |
| 225 | These modes are insecure. New applications should never make use of them, |
| 226 | and existing applications should strongly consider migrating away. |
| 227 | |
| 228 | |
David Reid | 1f3d718 | 2013-10-22 16:55:18 -0700 | [diff] [blame] | 229 | .. class:: ECB() |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 230 | |
| 231 | ECB (Electronic Code Book) is the simplest mode of operation for block |
Alex Gaynor | cd413a3 | 2013-09-10 18:59:43 -0700 | [diff] [blame] | 232 | ciphers. Each block of data is encrypted in the same way. This means |
| 233 | identical plaintext blocks will always result in identical ciphertext |
| 234 | blocks, and thus result in information leakage |