| 1 |
:mod:`Crypto.Cipher` package |
| 2 |
============================ |
| 3 |
|
| 4 |
Introduction |
| 5 |
------------ |
| 6 |
|
| 7 |
The :mod:`Crypto.Cipher` package contains algorithms for protecting the confidentiality |
| 8 |
of data. |
| 9 |
|
| 10 |
There are three types of encryption algorithms: |
| 11 |
|
| 12 |
1. **Symmetric ciphers**: all parties use the same key, for both |
| 13 |
decrypting and encrypting data. |
| 14 |
Symmetric ciphers are typically very fast and can process |
| 15 |
very large amount of data. |
| 16 |
|
| 17 |
2. **Asymmetric ciphers**: senders and receivers use different keys. |
| 18 |
Senders encrypt with *public* keys (non-secret) whereas receivers |
| 19 |
decrypt with *private* keys (secret). |
| 20 |
Asymmetric ciphers are typically very slow and can process |
| 21 |
only very small payloads. Example: :doc:`oaep`. |
| 22 |
|
| 23 |
3. **Hybrid ciphers**: the two types of ciphers above can be combined |
| 24 |
in a construction that inherits the benefits of both. |
| 25 |
An *asymmetric* cipher is used to protect a short-lived |
| 26 |
symmetric key, |
| 27 |
and a *symmetric* cipher (under that key) encrypts |
| 28 |
the actual message. |
| 29 |
|
| 30 |
API principles |
| 31 |
-------------- |
| 32 |
|
| 33 |
.. figure:: simple_mode.png |
| 34 |
:align: center |
| 35 |
:figwidth: 50% |
| 36 |
|
| 37 |
Generic state diagram for a cipher object |
| 38 |
|
| 39 |
The base API of a cipher is fairly simple: |
| 40 |
|
| 41 |
* You instantiate a cipher object by calling the :func:`new` |
| 42 |
function from the relevant cipher module (e.g. :func:`Crypto.Cipher.AES.new`). |
| 43 |
The first parameter is always the *cryptographic key*; |
| 44 |
its length depends on the particular cipher. |
| 45 |
You can (and sometimes must) pass additional cipher- or mode-specific parameters |
| 46 |
to :func:`new` (such as a *nonce* or a *mode of operation*). |
| 47 |
|
| 48 |
* For encrypting data, you call the :func:`encrypt` method of the cipher |
| 49 |
object with the plaintext. The method returns the piece of ciphertext. |
| 50 |
Alternatively, with the ``output`` parameter you can specify |
| 51 |
a pre-allocated buffer for the result. |
| 52 |
|
| 53 |
For most algorithms, you may call :func:`encrypt` multiple times |
| 54 |
(i.e. once for each piece of plaintext). |
| 55 |
|
| 56 |
* For decrypting data, you call the :func:`decrypt` method of the cipher |
| 57 |
object with the ciphertext. The method returns the piece of plaintext. |
| 58 |
The ``output`` parameter can be passed here too. |
| 59 |
|
| 60 |
For most algorithms, you may call :func:`decrypt` multiple times |
| 61 |
(i.e. once for each piece of ciphertext). |
| 62 |
|
| 63 |
.. note:: |
| 64 |
|
| 65 |
Plaintexts and ciphertexts (input/output) can only be ``bytes``, |
| 66 |
``bytearray`` or ``memoryview``. |
| 67 |
In Python 3, you cannot pass strings. |
| 68 |
In Python 2, you cannot pass Unicode strings. |
| 69 |
|
| 70 |
Often, the sender has to deliver to the receiver other data in addition |
| 71 |
to ciphertext alone (e.g. **initialization vectors** or **nonces**, **MAC tags**, etc). |
| 72 |
|
| 73 |
This is a basic example:: |
| 74 |
|
| 75 |
>>> from Crypto.Cipher import Salsa20 |
| 76 |
>>> |
| 77 |
>>> key = b'0123456789012345' |
| 78 |
>>> cipher = Salsa20.new(key) |
| 79 |
>>> ciphertext = cipher.encrypt(b'The secret I want to send.') |
| 80 |
>>> ciphertext += cipher.encrypt(b'The second part of the secret.') |
| 81 |
>>> print cipher.nonce # A byte string you must send to the receiver too |
| 82 |
|
| 83 |
|
| 84 |
Symmetric ciphers |
| 85 |
----------------- |
| 86 |
|
| 87 |
There are two types of symmetric ciphers: |
| 88 |
|
| 89 |
* **Stream ciphers**: the most natural kind of ciphers: |
| 90 |
they encrypt data one byte at a time. |
| 91 |
See :doc:`chacha20` and :doc:`salsa20`. |
| 92 |
|
| 93 |
* **Block ciphers**: ciphers that can only operate on a fixed amount |
| 94 |
of data. The most important block cipher is :doc:`aes`, which has |
| 95 |
a block size of 128 bits (16 bytes). |
| 96 |
|
| 97 |
In general, a block cipher is mostly useful only together with |
| 98 |
a :doc:`mode of operation <classic>`, which allows one to encrypt |
| 99 |
a variable amount of data. Some modes (like CTR) effectively turn |
| 100 |
a block cipher into a stream cipher. |
| 101 |
|
| 102 |
The widespread consensus is that ciphers that provide |
| 103 |
only confidentiality, without any form of authentication, are undesirable. |
| 104 |
Instead, primitives have been defined to integrate symmetric encryption and |
| 105 |
authentication (MAC). For instance: |
| 106 |
|
| 107 |
* :doc:`Modern modes of operation <modern>` for block ciphers (like GCM). |
| 108 |
* Stream ciphers paired with a MAC function, like :doc:`chacha20_poly1305`. |
| 109 |
|
| 110 |
.. toctree:: |
| 111 |
:hidden: |
| 112 |
|
| 113 |
Classic modes of operation <classic> |
| 114 |
Modern modes of operation <modern> |
| 115 |
|
| 116 |
Legacy ciphers |
| 117 |
-------------- |
| 118 |
|
| 119 |
A number of ciphers are implemented in this library purely for backward compatibility purposes. |
| 120 |
They are deprecated or even fully broken and should not be used in new designs. |
| 121 |
|
| 122 |
* :doc:`des` and :doc:`des3` (block ciphers) |
| 123 |
* :doc:`arc2` (block cipher) |
| 124 |
* :doc:`arc4` (stream cipher) |
| 125 |
* :doc:`blowfish` (block cipher) |
| 126 |
* :doc:`cast` (block cipher) |
| 127 |
* :doc:`pkcs1_v1_5` (asymmetric cipher) |