aboutsummaryrefslogtreecommitdiffstats
path: root/docs/hazmat/primitives/aead.rst
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2017-06-07 18:08:57 -1000
committerAlex Gaynor <alex.gaynor@gmail.com>2017-06-08 00:08:57 -0400
commit7e53d911577881d87ce30291cef68e24f3c1b763 (patch)
tree3a8a0b43fdaae7d3d44549f7282048f5f3f1db58 /docs/hazmat/primitives/aead.rst
parentf12955cd242664cffbaa041ef815579a8d6b2d3a (diff)
downloadcryptography-7e53d911577881d87ce30291cef68e24f3c1b763.tar.gz
cryptography-7e53d911577881d87ce30291cef68e24f3c1b763.tar.bz2
cryptography-7e53d911577881d87ce30291cef68e24f3c1b763.zip
ChaCha20Poly1305 support (#3680)
* chacha20poly1305 support * add chacha20poly1305 backend and some fixes * refactor * forgot to remove this * pep8 * review feedback and a lot of type/value checking * review feedback * raise unsupportedalgorithm when creating a ChaCha20Poly1305 object if it's not supported. * switch to ciphertext||tag * typo * remove a branch we don't need * review feedback * decrypts is *also* a word * use reasons
Diffstat (limited to 'docs/hazmat/primitives/aead.rst')
-rw-r--r--docs/hazmat/primitives/aead.rst79
1 files changed, 79 insertions, 0 deletions
diff --git a/docs/hazmat/primitives/aead.rst b/docs/hazmat/primitives/aead.rst
new file mode 100644
index 00000000..54343b86
--- /dev/null
+++ b/docs/hazmat/primitives/aead.rst
@@ -0,0 +1,79 @@
+.. hazmat::
+
+
+Authenticated encryption
+========================
+
+.. module:: cryptography.hazmat.primitives.ciphers.aead
+
+Authenticated encryption with associated data (AEAD) are encryption schemes
+which provide both confidentiality and integrity for their ciphertext. They
+also support providing integrity for associated data which is not encrypted.
+
+.. class:: ChaCha20Poly1305(key)
+
+ .. versionadded:: 2.0
+
+ The ChaCha20Poly1305 construction is defined in :rfc:`7539` section 2.8.
+ It is a stream cipher combined with a MAC that offers strong integrity
+ guarantees.
+
+ :param bytes key: A 32-byte key. This **must** be kept secret.
+
+ :raises cryptography.exceptions.UnsupportedAlgorithm: If the version of
+ OpenSSL does not support ChaCha20Poly1305.
+
+ .. doctest::
+
+ >>> import os
+ >>> from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305
+ >>> data = b"a secret message"
+ >>> aad = b"authenticated but unencrypted data"
+ >>> key = ChaCha20Poly1305.generate_key()
+ >>> chacha = ChaCha20Poly1305(key)
+ >>> nonce = os.urandom(12)
+ >>> ct = chacha.encrypt(nonce, data, aad)
+ >>> chacha.decrypt(nonce, ct, aad)
+ 'a secret message'
+
+ .. classmethod:: generate_key()
+
+ Securely generates a random ChaCha20Poly1305 key.
+
+ :returns bytes: A 32 byte key.
+
+ .. method:: encrypt(nonce, data, associated_data)
+
+ .. warning::
+
+ Reuse of a ``nonce`` with a given ``key`` compromises the security
+ of any message with that ``nonce`` and ``key`` pair.
+
+ Encrypts the ``data`` provided and authenticates the
+ ``associated_data``. The output of this can be passed directly
+ to the ``decrypt`` method.
+
+ :param bytes nonce: A 12 byte value. **NEVER REUSE A NONCE** with a
+ key.
+ :param bytes data: The data to encrypt.
+ :param bytes associated_data: Additional data that should be
+ authenticated with the key, but does not need to be encrypted. Can
+ be ``None``.
+ :returns bytes: The ciphertext bytes with the 16 byte tag appended.
+
+ .. method:: decrypt(nonce, data, associated_data)
+
+ Decrypts the ``data`` and authenticates the ``associated_data``. If you
+ called encrypt with ``associated_data`` you must pass the same
+ ``associated_data`` in decrypt or the integrity check will fail.
+
+ :param bytes nonce: A 12 byte value. **NEVER REUSE A NONCE** with a
+ key.
+ :param bytes data: The data to decrypt (with tag appended).
+ :param bytes associated_data: Additional data to authenticate. Can be
+ ``None`` if none was passed during encryption.
+ :returns bytes: The original plaintext.
+ :raises cryptography.exceptions.InvalidTag: If the authentication tag
+ doesn't validate this exception will be raised. This will occur
+ when the ciphertext has been changed, but will also occur when the
+ key, nonce, or associated data are wrong.