aboutsummaryrefslogtreecommitdiffstats
path: root/docs/hazmat/primitives/symmetric-encryption.rst
diff options
context:
space:
mode:
Diffstat (limited to 'docs/hazmat/primitives/symmetric-encryption.rst')
-rw-r--r--docs/hazmat/primitives/symmetric-encryption.rst142
1 files changed, 133 insertions, 9 deletions
diff --git a/docs/hazmat/primitives/symmetric-encryption.rst b/docs/hazmat/primitives/symmetric-encryption.rst
index 5542e832..2f390175 100644
--- a/docs/hazmat/primitives/symmetric-encryption.rst
+++ b/docs/hazmat/primitives/symmetric-encryption.rst
@@ -12,17 +12,20 @@ Symmetric Encryption
key = binascii.unhexlify(b"0" * 32)
iv = binascii.unhexlify(b"0" * 32)
+ from cryptography.hazmat.bindings import default_backend
+ backend = default_backend()
+
Symmetric encryption is a way to encrypt (hide the plaintext value) material
-where the encrypter and decrypter both use the same key. Note that symmetric
+where the sender and receiver both use the same key. Note that symmetric
encryption is **not** sufficient for most applications, because it only
provides secrecy (an attacker can't see the message) but not authenticity (an
attacker can create bogus messages and force the application to decrypt them).
-For this reason it is *strongly* reccomended to combine encryption with a
+For this reason it is *strongly* recommended to combine encryption with a
message authentication code, such as :doc:`HMAC </hazmat/primitives/hmac>`, in
an "encrypt-then-MAC" formulation as `described by Colin Percival`_.
-.. class:: Cipher(algorithm, mode)
+.. class:: Cipher(algorithm, mode, backend)
Cipher objects combine an algorithm (such as
:class:`~cryptography.hazmat.primitives.ciphers.algorithms.AES`) with a
@@ -34,15 +37,23 @@ an "encrypt-then-MAC" formulation as `described by Colin Percival`_.
.. doctest::
>>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
- >>> cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
+ >>> cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
>>> encryptor = cipher.encryptor()
>>> ct = encryptor.update(b"a secret message") + encryptor.finalize()
>>> decryptor = cipher.decryptor()
>>> decryptor.update(ct) + decryptor.finalize()
'a secret message'
- :param algorithms: One of the algorithms described below.
- :param mode: One of the modes described below.
+ :param algorithms: A
+ :class:`~cryptography.hazmat.primitives.interfaces.CipherAlgorithm`
+ provider such as those described
+ :ref:`below <symmetric-encryption-algorithms>`.
+ :param mode: A :class:`~cryptography.hazmat.primitives.interfaces.Mode`
+ provider such as those described
+ :ref:`below <symmetric-encryption-modes>`.
+ :param backend: A
+ :class:`~cryptography.hazmat.bindings.interfaces.CipherBackend`
+ provider.
.. method:: encryptor()
@@ -75,10 +86,20 @@ an "encrypt-then-MAC" formulation as `described by Colin Percival`_.
everything into the context. Once that is done call ``finalize()`` to
finish the operation and obtain the remainder of the data.
+ Block ciphers require that plaintext or ciphertext always be a multiple of
+ their block size, because of that **padding** is often required to make a
+ message the correct size. ``CipherContext`` will not automatically apply
+ any padding; you'll need to add your own. For block ciphers the recommended
+ padding is :class:`cryptography.hazmat.primitives.padding.PKCS7`. If you
+ are using a stream cipher mode (such as
+ :class:`cryptography.hazmat.primitives.modes.CTR`) you don't have to worry
+ about this.
+
.. method:: update(data)
:param bytes data: The data you wish to pass into the context.
:return bytes: Returns the data that was encrypted or decrypted.
+ :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize`
When the ``Cipher`` was constructed in a mode that turns it into a
stream cipher (e.g.
@@ -89,6 +110,45 @@ an "encrypt-then-MAC" formulation as `described by Colin Percival`_.
.. method:: finalize()
:return bytes: Returns the remainder of the data.
+ :raises ValueError: This is raised when the data provided isn't
+ correctly padded to be a multiple of the
+ algorithm's block size.
+
+ Once ``finalize`` is called this object can no longer be used and
+ :meth:`update` and :meth:`finalize` will raise
+ :class:`~cryptography.exceptions.AlreadyFinalized`.
+
+.. class:: AEADCipherContext
+
+ When calling ``encryptor()`` or ``decryptor()`` on a ``Cipher`` object
+ with an AEAD mode you will receive a return object conforming to the
+ ``AEADCipherContext`` interface (in addition to the ``CipherContext``
+ interface). If it is an encryption context it will additionally be an
+ ``AEADEncryptionContext`` interface. ``AEADCipherContext`` contains an
+ additional method ``authenticate_additional_data`` for adding additional
+ authenticated but unencrypted data. You should call this before calls to
+ ``update``. When you are done call ``finalize()`` to finish the operation.
+
+ .. method:: authenticate_additional_data(data)
+
+ :param bytes data: The data you wish to authenticate but not encrypt.
+ :raises: :class:`~cryptography.exceptions.AlreadyFinalized`
+
+.. class:: AEADEncryptionContext
+
+ When creating an encryption context using ``encryptor()`` on a ``Cipher``
+ object with an AEAD mode you will receive a return object conforming to the
+ ``AEADEncryptionContext`` interface (as well as ``AEADCipherContext``).
+ This interface provides one additional attribute ``tag``. ``tag`` can only
+ be obtained after ``finalize()``.
+
+ .. attribute:: tag
+
+ :return bytes: Returns the tag value as bytes.
+ :raises: :class:`~cryptography.exceptions.NotYetFinalized` if called
+ before the context is finalized.
+
+.. _symmetric-encryption-algorithms:
Algorithms
~~~~~~~~~~
@@ -116,10 +176,10 @@ Algorithms
.. class:: TripleDES(key)
- Triple DES (Data Encryption Standard), sometimes refered to as 3DES, is a
- block cipher standardized by NIST. Triple DES has known cryptoanalytic
+ Triple DES (Data Encryption Standard), sometimes referred to as 3DES, is a
+ block cipher standardized by NIST. Triple DES has known crypto-analytic
flaws, however none of them currently enable a practical attack.
- Nonetheless, Triples DES is not reccomended for new applications because it
+ Nonetheless, Triples DES is not recommended for new applications because it
is incredibly slow; old applications should consider moving away from it.
:param bytes key: The secret key, either ``64``, ``128``, or ``192`` bits
@@ -157,6 +217,27 @@ Weak Ciphers
:param bytes key: The secret key, 32-448 bits in length (in increments of
8). This must be kept secret.
+.. class:: ARC4(key)
+
+ ARC4 (Alleged RC4) is a stream cipher with serious weaknesses in its
+ initial stream output. Its use is strongly discouraged. ARC4 does not use
+ mode constructions.
+
+ :param bytes key: The secret key, ``40``, ``56``, ``64``, ``80``, ``128``,
+ ``192``, or ``256`` bits in length. This must be kept
+ secret.
+
+ .. doctest::
+
+ >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
+ >>> algorithm = algorithms.ARC4(key)
+ >>> cipher = Cipher(algorithm, mode=None, backend=backend)
+ >>> encryptor = cipher.encryptor()
+ >>> ct = encryptor.update(b"a secret message")
+ >>> decryptor = cipher.decryptor()
+ >>> decryptor.update(ct)
+ 'a secret message'
+
.. _symmetric-encryption-modes:
@@ -244,6 +325,48 @@ Modes
reuse an ``initialization_vector`` with
a given ``key``.
+.. class:: GCM(initialization_vector, tag=None)
+
+ .. danger::
+
+ When using this mode you MUST not use the decrypted data until
+ :meth:`~cryptography.hazmat.primitives.interfaces.CipherContext.finalize`
+ has been called. GCM provides NO guarantees of ciphertext integrity
+ until decryption is complete.
+
+ GCM (Galois Counter Mode) is a mode of operation for block ciphers. An
+ AEAD (authenticated encryption with additional data) mode is a type of
+ block cipher mode that encrypts the message as well as authenticating it
+ (and optionally additional data that is not encrypted) simultaneously.
+ Additional means of verifying integrity (like
+ :doc:`HMAC </hazmat/primitives/hmac>`) are not necessary.
+
+ :param bytes initialization_vector: Must be random bytes. They do not need
+ to be kept secret (they can be included
+ in a transmitted message). NIST
+ `recommends 96-bit IV length`_ for
+ performance critical situations, but it
+ can be up to 2\ :sup:`64` - 1 bits.
+ Do not reuse an ``initialization_vector``
+ with a given ``key``.
+
+ :param bytes tag: The tag bytes to verify during decryption. When encrypting
+ this must be None.
+
+ .. doctest::
+
+ >>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
+ >>> cipher = Cipher(algorithms.AES(key), modes.GCM(iv), backend)
+ >>> encryptor = cipher.encryptor()
+ >>> encryptor.authenticate_additional_data(b"authenticated but not encrypted payload")
+ >>> ct = encryptor.update(b"a secret message") + encryptor.finalize()
+ >>> tag = encryptor.tag
+ >>> cipher = Cipher(algorithms.AES(key), modes.GCM(iv, tag), backend)
+ >>> decryptor = cipher.decryptor()
+ >>> decryptor.authenticate_additional_data(b"authenticated but not encrypted payload")
+ >>> decryptor.update(ct) + decryptor.finalize()
+ 'a secret message'
+
Insecure Modes
--------------
@@ -263,3 +386,4 @@ Insecure Modes
.. _`described by Colin Percival`: http://www.daemonology.net/blog/2009-06-11-cryptographic-right-answers.html
+.. _`recommends 96-bit IV length`: http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-spec.pdf