Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
d41ee9d8
Commit
d41ee9d8
authored
Jun 24, 2012
by
Antoine Pitrou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Larry's suggested rewording of the compare_digest() docs.
parent
12dc1060
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
36 deletions
+20
-36
Doc/library/hmac.rst
Doc/library/hmac.rst
+12
-29
Modules/operator.c
Modules/operator.c
+8
-7
No files found.
Doc/library/hmac.rst
View file @
d41ee9d8
...
@@ -54,11 +54,10 @@ An HMAC object has the following methods:
...
@@ -54,11 +54,10 @@ An HMAC object has the following methods:
.. warning::
.. warning::
The output of :meth:`hexdigest` should not be compared directly to an
When comparing the output of :meth:`hexdigest` to an externally-supplied
externally-supplied digest during a verification routine. Instead, the
digest during a verification routine, it is recommended to use the
externally supplied digest should be converted to a :class:`bytes`
:func:`compare_digest` function instead of the ``==`` operator
value and compared to the output of :meth:`digest` with
to reduce the vulnerability to timing attacks.
:func:`compare_digest`.
.. method:: HMAC.copy()
.. method:: HMAC.copy()
...
@@ -71,32 +70,16 @@ This module also provides the following helper function:
...
@@ -71,32 +70,16 @@ This module also provides the following helper function:
.. function:: compare_digest(a, b)
.. function:: compare_digest(a, b)
Returns the equivalent of ``a == b``, but avoids content based
Return ``a == b``. This function uses an approach designed to prevent
short circuiting behaviour to reduce the vulnerability to timing
timing analysis, making it appropriate for cryptography. *a* and *b*
analysis. The inputs must either both support the buffer protocol (e.g.
must both be of the same type: either :class:`str` (ASCII only, as e.g.
:class:`bytes` and :class:`bytearray` instances) or be ASCII only
returned by :meth:`HMAC.hexdigest`), or any type that supports the
:class:`str` instances as returned by :meth:`hexdigest`.
:term:`buffer protocol` (e.g. :class:`bytes`).
:class:`bytes` and :class:`str` instances can't be mixed.
Using a short circuiting comparison (that is, one that terminates as soon
as it finds any difference between the values) to check digests for
correctness can be problematic, as it introduces a potential
vulnerability when an attacker can control both the message to be checked
*and* the purported signature value. By keeping the plaintext consistent
and supplying different signature values, an attacker may be able to use
timing variations to search the signature space for the expected value in
O(n) time rather than the desired O(2**n).
.. note::
.. note::
If *a* and *b* are different lengths, or if an error occurs,
While this function reduces the likelihood of leaking the contents of
a timing attack may be able to infer information about the types
the expected digest via a timing attack, it still may leak some timing
and lengths of *a* and *b*, but not their values.
information when the input values differ in lengths as well as in error
cases like unsupported types or non ASCII strings. When the inputs have
different length the timing depends solely on the length of ``b``. It
is assumed that the expected length of the digest is not a secret, as
it is typically published as part of a file format, network protocol
or API definition.
.. versionadded:: 3.3
.. versionadded:: 3.3
...
...
Modules/operator.c
View file @
d41ee9d8
...
@@ -211,14 +211,15 @@ _tscmp(const unsigned char *a, const unsigned char *b,
...
@@ -211,14 +211,15 @@ _tscmp(const unsigned char *a, const unsigned char *b,
PyDoc_STRVAR
(
compare_digest__doc__
,
PyDoc_STRVAR
(
compare_digest__doc__
,
"compare_digest(a, b) -> bool
\n
"
"compare_digest(a, b) -> bool
\n
"
"
\n
"
"
\n
"
"Return the equivalent of 'a == b', but avoid any short circuiting to
\n
"
"Return ``a == b``. This function uses an approach designed to prevent
\n
"
"counterfeit timing analysis of input data. The function should be used to
\n
"
"timing analysis, making it appropriate for cryptography. *a* and *b*
\n
"
"compare cryptographic secrets. a and b must both either support the buffer
\n
"
"must both be of the same type: either `str` (ASCII only, as e.g.
\n
"
"protocol (e.g. bytes) or be ASCII only str instances at the same time.
\n
"
"returned by HMAC.hexdigest()), or any type that supports the buffer
\n
"
"protocol, (e.g. `bytes`).
\n
"
"
\n
"
"
\n
"
"Note: I
n case of an error or different lengths the function may disclose
\n
"
"Note: I
f *a* and *b* are different lengths, or if an error occurs,
\n
"
"
some timing information about the types and lengths of a and b.
\n
"
);
"
a timing attack may be able to infer information about the types
\n
"
"and lengths of *a* and *b*, but not their values.
\n
"
);
static
PyObject
*
static
PyObject
*
compare_digest
(
PyObject
*
self
,
PyObject
*
args
)
compare_digest
(
PyObject
*
self
,
PyObject
*
args
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment