blob: 809636e42b8c0bdfc5bda0f1debc844745ad323d [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`hmac` --- Keyed-Hashing for Message Authentication
2========================================================
3
4.. module:: hmac
Georg Brandl80b75fd2010-10-17 09:43:35 +00005 :synopsis: Keyed-Hashing for Message Authentication (HMAC) implementation
6 for Python.
Georg Brandl116aa622007-08-15 14:28:22 +00007.. moduleauthor:: Gerhard Häring <ghaering@users.sourceforge.net>
8.. sectionauthor:: Gerhard Häring <ghaering@users.sourceforge.net>
9
Raymond Hettinger469271d2011-01-27 20:38:46 +000010**Source code:** :source:`Lib/hmac.py`
11
12--------------
Georg Brandl116aa622007-08-15 14:28:22 +000013
Georg Brandl116aa622007-08-15 14:28:22 +000014This module implements the HMAC algorithm as described by :rfc:`2104`.
15
16
Georg Brandl036490d2009-05-17 13:00:36 +000017.. function:: new(key, msg=None, digestmod=None)
Georg Brandl116aa622007-08-15 14:28:22 +000018
Georg Brandl80b75fd2010-10-17 09:43:35 +000019 Return a new hmac object. *key* is a bytes object giving the secret key. If
20 *msg* is present, the method call ``update(msg)`` is made. *digestmod* is
21 the digest constructor or module for the HMAC object to use. It defaults to
22 the :func:`hashlib.md5` constructor.
Georg Brandl116aa622007-08-15 14:28:22 +000023
Georg Brandl116aa622007-08-15 14:28:22 +000024
25An HMAC object has the following methods:
26
Georg Brandl9701eb62012-02-05 09:25:22 +010027.. method:: HMAC.update(msg)
Georg Brandl116aa622007-08-15 14:28:22 +000028
Georg Brandl80b75fd2010-10-17 09:43:35 +000029 Update the hmac object with the bytes object *msg*. Repeated calls are
30 equivalent to a single call with the concatenation of all the arguments:
31 ``m.update(a); m.update(b)`` is equivalent to ``m.update(a + b)``.
Georg Brandl116aa622007-08-15 14:28:22 +000032
33
Georg Brandl9701eb62012-02-05 09:25:22 +010034.. method:: HMAC.digest()
Georg Brandl116aa622007-08-15 14:28:22 +000035
Georg Brandl80b75fd2010-10-17 09:43:35 +000036 Return the digest of the bytes passed to the :meth:`update` method so far.
37 This bytes object will be the same length as the *digest_size* of the digest
38 given to the constructor. It may contain non-ASCII bytes, including NUL
39 bytes.
Georg Brandl116aa622007-08-15 14:28:22 +000040
Charles-François Natali7feb9f42012-05-13 19:53:07 +020041 .. warning::
42
43 When comparing the output of :meth:`digest` to an externally-supplied
44 digest during a verification routine, it is recommended to use the
Nick Coghlan807770e2012-06-15 21:14:08 +100045 :func:`compare_digest` function instead of the ``==`` operator
46 to reduce the vulnerability to timing attacks.
Charles-François Natali7feb9f42012-05-13 19:53:07 +020047
Georg Brandl116aa622007-08-15 14:28:22 +000048
Georg Brandl9701eb62012-02-05 09:25:22 +010049.. method:: HMAC.hexdigest()
Georg Brandl116aa622007-08-15 14:28:22 +000050
Georg Brandl80b75fd2010-10-17 09:43:35 +000051 Like :meth:`digest` except the digest is returned as a string twice the
52 length containing only hexadecimal digits. This may be used to exchange the
53 value safely in email or other non-binary environments.
Georg Brandl116aa622007-08-15 14:28:22 +000054
Charles-François Natali7feb9f42012-05-13 19:53:07 +020055 .. warning::
56
Antoine Pitrou9df73da2012-06-24 16:03:50 +020057 When comparing the output of :meth:`hexdigest` to an externally-supplied
58 digest during a verification routine, it is recommended to use the
59 :func:`compare_digest` function instead of the ``==`` operator
60 to reduce the vulnerability to timing attacks.
Charles-François Natali7feb9f42012-05-13 19:53:07 +020061
Georg Brandl116aa622007-08-15 14:28:22 +000062
Georg Brandl9701eb62012-02-05 09:25:22 +010063.. method:: HMAC.copy()
Georg Brandl116aa622007-08-15 14:28:22 +000064
65 Return a copy ("clone") of the hmac object. This can be used to efficiently
66 compute the digests of strings that share a common initial substring.
67
68
Charles-François Natali7feb9f42012-05-13 19:53:07 +020069This module also provides the following helper function:
70
Nick Coghlan807770e2012-06-15 21:14:08 +100071.. function:: compare_digest(a, b)
Charles-François Natali7feb9f42012-05-13 19:53:07 +020072
Georg Brandla1bc35f2012-06-24 16:07:33 +020073 Return ``a == b``. This function uses an approach designed to prevent timing
Georg Brandl3b44d812012-06-24 16:10:47 +020074 analysis by avoiding content based short circuiting behaviour, making it
Antoine Pitrou1524d752012-06-24 16:18:48 +020075 appropriate for cryptography. *a* and *b* must both be of the same type:
76 either :class:`str` (ASCII only, as e.g. returned by
77 :meth:`HMAC.hexdigest`), or any type that supports the buffer protocol
78 (e.g. :class:`bytes`).
Nick Coghlan807770e2012-06-15 21:14:08 +100079
Georg Brandla1bc35f2012-06-24 16:07:33 +020080 Using a short circuiting comparison (that is, one that terminates as soon as
81 it finds any difference between the values) to check digests for correctness
82 can be problematic, as it introduces a potential vulnerability when an
83 attacker can control both the message to be checked *and* the purported
84 signature value. By keeping the plaintext consistent and supplying different
85 signature values, an attacker may be able to use timing variations to search
86 the signature space for the expected value in O(n) time rather than the
87 desired O(2**n).
Charles-François Natali7feb9f42012-05-13 19:53:07 +020088
89 .. note::
90
Antoine Pitrouf61e7912012-06-24 16:21:10 +020091 If *a* and *b* are of different lengths, or if an error occurs,
92 a timing attack may be able to reveal information about the types
93 and lengths of *a* and *b*, but not their values.
94
Charles-François Natali7feb9f42012-05-13 19:53:07 +020095
Charles-François Natali9b704ec2012-05-15 21:00:32 +020096 .. versionadded:: 3.3
Charles-François Natali7feb9f42012-05-13 19:53:07 +020097
Georg Brandla1bc35f2012-06-24 16:07:33 +020098
Georg Brandl116aa622007-08-15 14:28:22 +000099.. seealso::
100
101 Module :mod:`hashlib`
Ezio Melotti0639d5a2009-12-19 23:26:38 +0000102 The Python module providing secure hash functions.