aboutsummaryrefslogtreecommitdiffstats
path: root/docs/hazmat/primitives
diff options
context:
space:
mode:
Diffstat (limited to 'docs/hazmat/primitives')
-rw-r--r--docs/hazmat/primitives/symmetric-encryption.rst88
-rw-r--r--docs/hazmat/primitives/twofactor.rst138
2 files changed, 136 insertions, 90 deletions
diff --git a/docs/hazmat/primitives/symmetric-encryption.rst b/docs/hazmat/primitives/symmetric-encryption.rst
index d91dde9d..2306c5b7 100644
--- a/docs/hazmat/primitives/symmetric-encryption.rst
+++ b/docs/hazmat/primitives/symmetric-encryption.rst
@@ -88,7 +88,7 @@ Algorithms
choice for encryption.
:param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
- This must be kept secret.
+ This must be kept secret.
.. class:: Camellia(key)
@@ -97,7 +97,7 @@ Algorithms
is not as widely studied or deployed.
:param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
- This must be kept secret.
+ This must be kept secret.
.. class:: TripleDES(key)
@@ -108,12 +108,11 @@ Algorithms
is incredibly slow; old applications should consider moving away from it.
:param bytes key: The secret key, either ``64``, ``128``, or ``192`` bits
- (note that DES functionally uses ``56``, ``112``, or
- ``168`` bits of the key, there is a parity byte in each
- component of the key), in some materials these are
- referred to as being up to three separate keys (each
- ``56`` bits long), they can simply be concatenated to
- produce the full key. This must be kept secret.
+ (note that DES functionally uses ``56``, ``112``, or ``168`` bits of
+ the key, there is a parity byte in each component of the key), in some
+ materials these are referred to as being up to three separate keys
+ (each ``56`` bits long), they can simply be concatenated to produce the
+ full key. This must be kept secret.
.. class:: CAST5(key)
@@ -124,7 +123,7 @@ Algorithms
a variable key length cipher and supports keys from 40-128 bits in length.
:param bytes key: The secret key, 40-128 bits in length (in increments of
- 8). This must be kept secret.
+ 8). This must be kept secret.
Weak Ciphers
------------
@@ -142,7 +141,7 @@ Weak Ciphers
that users of Blowfish move to newer algorithms, such as :class:`AES`.
:param bytes key: The secret key, 32-448 bits in length (in increments of
- 8). This must be kept secret.
+ 8). This must be kept secret.
.. class:: ARC4(key)
@@ -151,8 +150,7 @@ Weak Ciphers
mode constructions.
:param bytes key: The secret key, ``40``, ``56``, ``64``, ``80``, ``128``,
- ``192``, or ``256`` bits in length. This must be kept
- secret.
+ ``192``, or ``256`` bits in length. This must be kept secret.
.. doctest::
@@ -182,17 +180,12 @@ Modes
**Padding is required when using this mode.**
:param bytes initialization_vector: Must be random bytes. They do not need
- to be kept secret (they can be included
- in a transmitted message). Must be the
- same number of bytes as the
- ``block_size`` of the cipher. Each time
- something is encrypted a new
- ``initialization_vector`` should be
- generated. Do not reuse an
- ``initialization_vector`` with
- a given ``key``, and particularly do
- not use a constant
- ``initialization_vector``.
+ to be kept secret (they can be included in a transmitted message). Must
+ be the same number of bytes as the ``block_size`` of the cipher. Each
+ time something is encrypted a new ``initialization_vector`` should be
+ generated. Do not reuse an ``initialization_vector`` with a given
+ ``key``, and particularly do not use a constant
+ ``initialization_vector``.
A good construction looks like:
@@ -226,12 +219,11 @@ Modes
**This mode does not require padding.**
:param bytes nonce: Should be random bytes. It is critical to never reuse a
- ``nonce`` with a given key. Any reuse of a nonce
- with the same key compromises the security of every
- message encrypted with that key. Must be the same
- number of bytes as the ``block_size`` of the cipher
- with a given key. The nonce does not need to be kept
- secret and may be included alongside the ciphertext.
+ ``nonce`` with a given key. Any reuse of a nonce with the same key
+ compromises the security of every message encrypted with that key. Must
+ be the same number of bytes as the ``block_size`` of the cipher with a
+ given key. The nonce does not need to be kept secret and may be
+ included alongside the ciphertext.
.. class:: OFB(initialization_vector)
@@ -241,12 +233,9 @@ Modes
**This mode does not require padding.**
:param bytes initialization_vector: Must be random bytes. They do not need
- to be kept secret (they can be included
- in a transmitted message). Must be the
- same number of bytes as the
- ``block_size`` of the cipher. Do not
- reuse an ``initialization_vector`` with
- a given ``key``.
+ to be kept secret (they can be included in a transmitted message). Must
+ be the same number of bytes as the ``block_size`` of the cipher. Do not
+ reuse an ``initialization_vector`` with a given ``key``.
.. class:: CFB(initialization_vector)
@@ -256,12 +245,9 @@ Modes
**This mode does not require padding.**
:param bytes initialization_vector: Must be random bytes. They do not need
- to be kept secret (they can be included
- in a transmitted message). Must be the
- same number of bytes as the
- ``block_size`` of the cipher. Do not
- reuse an ``initialization_vector`` with
- a given ``key``.
+ to be kept secret (they can be included in a transmitted message). Must
+ be the same number of bytes as the ``block_size`` of the cipher. Do not
+ reuse an ``initialization_vector`` with a given ``key``.
.. class:: GCM(initialization_vector, tag=None)
@@ -282,13 +268,10 @@ Modes
**This mode does not require padding.**
: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``.
+ 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``.
.. note::
@@ -300,8 +283,8 @@ Modes
(32-bits). Applications **must** verify the tag is the expected length
to guarantee the expected security margin.
- :param bytes tag: The tag bytes to verify during decryption. When encrypting
- this must be None.
+ :param bytes tag: The tag bytes to verify during decryption. When
+ encrypting this must be ``None``.
.. testcode::
@@ -428,8 +411,7 @@ Interfaces
: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.
+ 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
@@ -473,7 +455,7 @@ Interfaces
:return bytes: Returns the tag value as bytes.
:raises: :class:`~cryptography.exceptions.NotYetFinalized` if called
- before the context is finalized.
+ before the context is finalized.
.. _`described by Colin Percival`: http://www.daemonology.net/blog/2009-06-11-cryptographic-right-answers.html
diff --git a/docs/hazmat/primitives/twofactor.rst b/docs/hazmat/primitives/twofactor.rst
index 9d661612..3df1a147 100644
--- a/docs/hazmat/primitives/twofactor.rst
+++ b/docs/hazmat/primitives/twofactor.rst
@@ -13,14 +13,14 @@ codes (HMAC).
.. currentmodule:: cryptography.hazmat.primitives.twofactor.hotp
-.. class:: HOTP(key, length, backend)
+.. class:: HOTP(key, length, algorithm, backend)
.. versionadded:: 0.3
- HOTP objects take a ``key`` and ``length`` parameter. The ``key``
- should be randomly generated bytes and is recommended to be 160 bits in
- length. The ``length`` parameter controls the length of the generated
- one time password and must be >= 6 and <= 8.
+ HOTP objects take a ``key``, ``length`` and ``algorithm`` parameter. The
+ ``key`` should be randomly generated bytes and is recommended to be 160
+ bits in length. The ``length`` parameter controls the length of the
+ generated one time password and must be >= 6 and <= 8.
This is an implementation of :rfc:`4226`.
@@ -29,55 +29,64 @@ codes (HMAC).
>>> import os
>>> from cryptography.hazmat.backends import default_backend
>>> from cryptography.hazmat.primitives.twofactor.hotp import HOTP
-
- >>> key = b"12345678901234567890"
- >>> hotp = HOTP(key, 6, backend=default_backend())
- >>> hotp.generate(0)
- '755224'
- >>> hotp.verify(b"755224", 0)
-
- :param bytes key: Secret key as ``bytes``. This value must be generated in a
- cryptographically secure fashion and be at least 128 bits.
- It is recommended that the key be 160 bits.
+ >>> from cryptography.hazmat.primitives.hashes import SHA1
+ >>> key = os.urandom(16)
+ >>> hotp = HOTP(key, 6, SHA1(), backend=default_backend())
+ >>> hotp_value = hotp.generate(0)
+ >>> hotp.verify(hotp_value, 0)
+
+ :param bytes key: Per-user secret key. This value must be kept secret
+ and be at least 128 bits. It is recommended that the
+ key be 160 bits.
:param int length: Length of generated one time password as ``int``.
+ :param HashAlgorithm algorithm: A
+ :class:`~cryptography.hazmat.primitives.hashes`
+ provider.
:param backend: A
:class:`~cryptography.hazmat.backends.interfaces.HMACBackend`
provider.
- :raises ValueError: This is raised if the provided ``key`` is shorter 128 bits
- or if the ``length`` parameter is not between 6 to 8.
-
+ :raises ValueError: This is raised if the provided ``key`` is shorter than
+ 128 bits or if the ``length`` parameter is not 6, 7 or 8.
+ :raises UnsupportedAlgorithm: This is raised if the provided ``algorithm``
+ is not :class:`~cryptography.hazmat.primitives.hashes.SHA1()`,
+ :class:`~cryptography.hazmat.primitives.hashes.SHA256()` or
+ :class:`~cryptography.hazmat.primitives.hashes.SHA512()`.
.. method:: generate(counter)
- :param int counter: The counter value used to generate the one time password.
+ :param int counter: The counter value used to generate the one time
+ password.
:return bytes: A one time password value.
.. method:: verify(hotp, counter)
:param bytes hotp: The one time password value to validate.
- :param bytes counter: The counter value to validate against.
- :raises cryptography.exceptions.InvalidToken: This is raised when the supplied HOTP
- does not match the expected HOTP.
+ :param int counter: The counter value to validate against.
+ :raises cryptography.exceptions.InvalidToken: This is raised when the
+ supplied HOTP does not match the expected HOTP.
Throttling
-----------
+~~~~~~~~~~
-Due to the fact that the HOTP algorithm generates rather short tokens that are 6 - 8 digits
-long, brute force attacks are possible. It is highly recommended that the server that
-validates the token implement a throttling scheme that locks out the account for a period of
-time after a number of failed attempts. The number of allowed attempts should be as low as
-possible while still ensuring that usability is not significantly impacted.
+Due to the fact that the HOTP algorithm generates rather short tokens that are
+6 - 8 digits long, brute force attacks are possible. It is highly recommended
+that the server that validates the token implement a throttling scheme that
+locks out the account for a period of time after a number of failed attempts.
+The number of allowed attempts should be as low as possible while still
+ensuring that usability is not significantly impacted.
Re-synchronization of the Counter
----------------------------------
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-The server's counter value should only be incremented on a successful HOTP authentication.
-However, the counter on the client is incremented every time a new HOTP value is requested.
-This can lead to the counter value being out of synchronization between the client and server.
+The server's counter value should only be incremented on a successful HOTP
+authentication. However, the counter on the client is incremented every time a
+new HOTP value is requested. This can lead to the counter value being out of
+synchronization between the client and server.
-Due to this, it is highly recommended that the server sets a look-ahead window that allows the
-server to calculate the next ``x`` HOTP values and check them against the supplied HOTP value.
-This can be accomplished with something similar to the following code.
+Due to this, it is highly recommended that the server sets a look-ahead window
+that allows the server to calculate the next ``x`` HOTP values and check them
+against the supplied HOTP value. This can be accomplished with something
+similar to the following code.
.. code-block:: python
@@ -86,11 +95,66 @@ This can be accomplished with something similar to the following code.
correct_counter = None
otp = HOTP(key, 6, default_backend())
- for count in range(counter, counter+look_ahead):
+ for count in range(counter, counter + look_ahead):
try:
otp.verify(hotp, count)
correct_counter = count
except InvalidToken:
pass
- return correct_counter \ No newline at end of file
+ return correct_counter
+
+.. currentmodule:: cryptography.hazmat.primitives.twofactor.totp
+
+.. class:: TOTP(key, length, algorithm, time_step, backend)
+
+ TOTP objects take a ``key``, ``length``, ``algorithm`` and ``time_step``
+ parameter. The ``key`` should be randomly generated bytes and is recommended
+ to be as long as your hash function's output (e.g 256-bit for SHA256).
+ The ``length`` parameter controls the length of the generated one time
+ password and must be >= 6 and <= 8.
+
+ This is an implementation of :rfc:`6238`.
+
+ .. doctest::
+
+ >>> import os
+ >>> import time
+ >>> from cryptography.hazmat.backends import default_backend
+ >>> from cryptography.hazmat.primitives.twofactor.totp import TOTP
+ >>> from cryptography.hazmat.primitives.hashes import SHA1
+ >>> key = os.urandom(16)
+ >>> totp = TOTP(key, 8, SHA1(), 30, backend=default_backend())
+ >>> time_value = time.time()
+ >>> totp_value = totp.generate(time_value)
+ >>> totp.verify(totp_value, time_value)
+
+ :param bytes key: Per-user secret key. This value must be kept secret
+ and be at least 128 bits. It is recommended that the
+ key be 160 bits.
+ :param int length: Length of generated one time password as ``int``.
+ :param HashAlgorithm algorithm: A
+ :class:`~cryptography.hazmat.primitives.hashes`
+ provider.
+ :param int time_step: The time step size. The recommended size is 30.
+ :param backend: A
+ :class:`~cryptography.hazmat.backends.interfaces.HMACBackend`
+ provider.
+ :raises ValueError: This is raised if the provided ``key`` is shorter than
+ 128 bits or if the ``length`` parameter is not 6, 7 or 8.
+ :raises UnsupportedAlgorithm: This is raised if the provided ``algorithm``
+ is not :class:`~cryptography.hazmat.primitives.hashes.SHA1()`,
+ :class:`~cryptography.hazmat.primitives.hashes.SHA256()` or
+ :class:`~cryptography.hazmat.primitives.hashes.SHA512()`.
+
+ .. method:: generate(time)
+
+ :param int time: The time value used to generate the one time password.
+ :return bytes: A one time password value.
+
+ .. method:: verify(totp, time)
+
+ :param bytes totp: The one time password value to validate.
+ :param int time: The time value to validate against.
+ :raises cryptography.exceptions.InvalidToken: This is raised when the
+ supplied TOTP does not match the expected TOTP.