aboutsummaryrefslogtreecommitdiffstats
path: root/docs/hazmat/primitives/constant-time.rst
blob: 632e7c6865a863f55bd0a0bbe23c666139751078 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
.. hazmat::

Constant time functions
=======================

.. currentmodule:: cryptography.hazmat.primitives.constant_time

This module contains functions for operating with secret data in a way that
does not leak information about that data through how long it takes to perform
the operation. These functions should be used whenever operating on secret data
along with data that is user supplied.

An example would be comparing a HMAC signature received from a client to the
one generated by the server code for authentication purposes.

For more information about this sort of issue, see `Coda Hale's blog post`_
about the timing attacks on KeyCzar and Java's ``MessageDigest.isEqual()``.


.. function:: bytes_eq(a, b)

    Compare ``a`` and ``b`` to one another in constant time if they are of the
    same length.

    .. doctest::

        >>> from cryptography.hazmat.primitives import constant_time
        >>> constant_time.bytes_eq(b"foo", b"foo")
        True
        >>> constant_time.bytes_eq(b"foo", b"bar")
        False

    :param a bytes: The left-hand side.
    :param b bytes: The right-hand side.
    :returns boolean: True if ``a`` has the same bytes as ``b``.


.. _`Coda Hale's blog post`: http://codahale.com/a-lesson-in-timing-attacks/