| 1 |
ChaCha20-Poly1305 and XChaCha20-Poly1305 |
| 2 |
======================================== |
| 3 |
|
| 4 |
ChaCha20-Poly1305 is an authenticated cipher with associated data (AEAD). |
| 5 |
It works with a 32 byte secret key and a nonce |
| 6 |
which **must never** be reused across encryptions performed under the same key. |
| 7 |
The cipher produces a 16 byte tag that the receiver must use to validate the message. |
| 8 |
|
| 9 |
There are three variants of the algorithm, defined by the length of the nonce: |
| 10 |
|
| 11 |
.. csv-table:: |
| 12 |
:header: Nonce length, Description, Max plaintext, If random nonce and same key |
| 13 |
:widths: 5, 50, 20, 20 |
| 14 |
|
| 15 |
"8 bytes", "Based on Bernstein's original ChaCha20.", "No limitations", "Max 200 000 messages" |
| 16 |
"12 bytes (default)", "Version used in TLS and specified in `RFC7539`_.", "256 GB", "Max 13 billion messages" |
| 17 |
"24 bytes", "XChaCha20-Poly1305, still in `draft stage <https://tools.ietf.org/html/draft-arciszewski-xchacha-03>`_.", "256 GB", "No limitations" |
| 18 |
|
| 19 |
The API of the cipher and its finite state machine are the same as for the :doc:`modern modes of operation of block ciphers <modern>`. |
| 20 |
|
| 21 |
You create a new cipher by calling :meth:`Crypto.Cipher.ChaCha20_Poly1305.new`. |
| 22 |
|
| 23 |
This is an example of how ChaCha20-Poly1305 (TLS version) can encrypt and authenticate data:: |
| 24 |
|
| 25 |
>>> import json |
| 26 |
>>> from base64 import b64encode |
| 27 |
>>> from Crypto.Cipher import ChaCha20_Poly1305 |
| 28 |
>>> from Crypto.Random import get_random_bytes |
| 29 |
>>> |
| 30 |
>>> header = b"header" |
| 31 |
>>> plaintext = b'Attack at dawn' |
| 32 |
>>> key = get_random_bytes(32) |
| 33 |
>>> cipher = ChaCha20_Poly1305.new(key=key) |
| 34 |
>>> cipher.update(header) |
| 35 |
>>> ciphertext, tag = cipher.encrypt_and_digest(plaintext) |
| 36 |
>>> |
| 37 |
>>> jk = [ 'nonce', 'header', 'ciphertext', 'tag' ] |
| 38 |
>>> jv = [ b64encode(x).decode('utf-8') for x in (cipher.nonce, header, ciphertext, tag) ] |
| 39 |
>>> result = json.dumps(dict(zip(jk, jv))) |
| 40 |
>>> print(result) |
| 41 |
{"nonce": "4EE/9uqhoZ3mQXmm", "header": "aGVhZGVy", "ciphertext": "Wmmo4Vzn+eS3tUPv2a8=", "tag": "/FgVbM8qhzssPRY80T0iVA=="} |
| 42 |
|
| 43 |
In the example above, a 96 bit (12 byte) nonce is automatically created. |
| 44 |
It can be accessed as the ``nonce`` member in the ``cipher`` object. |
| 45 |
|
| 46 |
This is how you decrypt the data and check its authenticity:: |
| 47 |
|
| 48 |
>>> import json |
| 49 |
>>> from base64 import b64decode |
| 50 |
>>> from Crypto.Cipher import ChaCha20_Poly1305 |
| 51 |
>>> |
| 52 |
>>> # We assume that the key was securely shared beforehand |
| 53 |
>>> try: |
| 54 |
>>> b64 = json.loads(json_input) |
| 55 |
>>> jk = [ 'nonce', 'header', 'ciphertext', 'tag' ] |
| 56 |
>>> jv = {k:b64decode(b64[k]) for k in jk} |
| 57 |
>>> |
| 58 |
>>> cipher = ChaCha20_Poly1305.new(key=key, nonce=jv['nonce']) |
| 59 |
>>> cipher.update(jv['header']) |
| 60 |
>>> plaintext = cipher.decrypt_and_verify(jv['ciphertext'], jv['tag']) |
| 61 |
>>> print("The message was: " + plaintext) |
| 62 |
>>> except (ValueError, KeyError): |
| 63 |
>>> print("Incorrect decryption") |
| 64 |
|
| 65 |
.. _RFC7539: https://tools.ietf.org/html/rfc7539 |
| 66 |
|
| 67 |
.. automodule:: Crypto.Cipher.ChaCha20_Poly1305 |
| 68 |
:members: |