Gregory P. Smith | f33c57d5 | 2019-10-17 20:30:42 -0700 | [diff] [blame] | 1 | """HMAC (Keyed-Hashing for Message Authentication) module. |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 2 | |
| 3 | Implements the HMAC algorithm as described by RFC 2104. |
| 4 | """ |
| 5 | |
Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 6 | import warnings as _warnings |
Christian Heimes | 2f050c7 | 2018-01-27 09:53:43 +0100 | [diff] [blame] | 7 | try: |
| 8 | import _hashlib as _hashopenssl |
| 9 | except ImportError: |
| 10 | _hashopenssl = None |
| 11 | _openssl_md_meths = None |
Christian Heimes | db5aed9 | 2020-05-27 21:50:06 +0200 | [diff] [blame] | 12 | from _operator import _compare_digest as compare_digest |
Christian Heimes | 2f050c7 | 2018-01-27 09:53:43 +0100 | [diff] [blame] | 13 | else: |
| 14 | _openssl_md_meths = frozenset(_hashopenssl.openssl_md_meth_names) |
Christian Heimes | db5aed9 | 2020-05-27 21:50:06 +0200 | [diff] [blame] | 15 | compare_digest = _hashopenssl.compare_digest |
Christian Heimes | 634919a | 2013-11-20 17:23:06 +0100 | [diff] [blame] | 16 | import hashlib as _hashlib |
Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 18 | trans_5C = bytes((x ^ 0x5C) for x in range(256)) |
| 19 | trans_36 = bytes((x ^ 0x36) for x in range(256)) |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 20 | |
Andrew M. Kuchling | 1ccdff9 | 2001-11-02 21:49:20 +0000 | [diff] [blame] | 21 | # The size of the digests returned by HMAC depends on the underlying |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 22 | # hashing module used. Use digest_size from the instance of HMAC instead. |
Andrew M. Kuchling | 1ccdff9 | 2001-11-02 21:49:20 +0000 | [diff] [blame] | 23 | digest_size = None |
| 24 | |
Tim Peters | 934d31b | 2004-03-20 20:11:29 +0000 | [diff] [blame] | 25 | |
Charles-François Natali | 7feb9f4 | 2012-05-13 19:53:07 +0200 | [diff] [blame] | 26 | |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 27 | class HMAC: |
Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 28 | """RFC 2104 HMAC class. Also complies with RFC 4231. |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 29 | |
Andrew M. Kuchling | 1ccdff9 | 2001-11-02 21:49:20 +0000 | [diff] [blame] | 30 | This supports the API for Cryptographic Hash Functions (PEP 247). |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 31 | """ |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 32 | blocksize = 64 # 512-bit HMAC; can be changed in subclasses. |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 33 | |
Christian Heimes | 837f9e4 | 2020-05-17 01:05:40 +0200 | [diff] [blame] | 34 | __slots__ = ( |
| 35 | "_digest_cons", "_inner", "_outer", "block_size", "digest_size" |
| 36 | ) |
| 37 | |
Gregory P. Smith | f33c57d5 | 2019-10-17 20:30:42 -0700 | [diff] [blame] | 38 | def __init__(self, key, msg=None, digestmod=''): |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 39 | """Create a new HMAC object. |
| 40 | |
Gregory P. Smith | f33c57d5 | 2019-10-17 20:30:42 -0700 | [diff] [blame] | 41 | key: bytes or buffer, key for the keyed hash object. |
| 42 | msg: bytes or buffer, Initial input for the hash or None. |
| 43 | digestmod: A hash name suitable for hashlib.new(). *OR* |
| 44 | A hashlib constructor returning a new hash object. *OR* |
| 45 | A module supporting PEP 247. |
Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 46 | |
Gregory P. Smith | f33c57d5 | 2019-10-17 20:30:42 -0700 | [diff] [blame] | 47 | Required as of 3.8, despite its position after the optional |
| 48 | msg argument. Passing it as a keyword argument is |
| 49 | recommended, though not required for legacy API reasons. |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 50 | """ |
Tim Peters | 934d31b | 2004-03-20 20:11:29 +0000 | [diff] [blame] | 51 | |
Christian Heimes | 04926ae | 2013-07-01 13:08:42 +0200 | [diff] [blame] | 52 | if not isinstance(key, (bytes, bytearray)): |
| 53 | raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__) |
Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 54 | |
Gregory P. Smith | f33c57d5 | 2019-10-17 20:30:42 -0700 | [diff] [blame] | 55 | if not digestmod: |
| 56 | raise TypeError("Missing required parameter 'digestmod'.") |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 57 | |
Florent Xicluna | 5d1155c | 2011-10-28 14:45:05 +0200 | [diff] [blame] | 58 | if callable(digestmod): |
Christian Heimes | 837f9e4 | 2020-05-17 01:05:40 +0200 | [diff] [blame] | 59 | self._digest_cons = digestmod |
Christian Heimes | 634919a | 2013-11-20 17:23:06 +0100 | [diff] [blame] | 60 | elif isinstance(digestmod, str): |
Christian Heimes | 837f9e4 | 2020-05-17 01:05:40 +0200 | [diff] [blame] | 61 | self._digest_cons = lambda d=b'': _hashlib.new(digestmod, d) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 62 | else: |
Christian Heimes | 837f9e4 | 2020-05-17 01:05:40 +0200 | [diff] [blame] | 63 | self._digest_cons = lambda d=b'': digestmod.new(d) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 64 | |
Christian Heimes | 837f9e4 | 2020-05-17 01:05:40 +0200 | [diff] [blame] | 65 | self._outer = self._digest_cons() |
| 66 | self._inner = self._digest_cons() |
| 67 | self.digest_size = self._inner.digest_size |
Tim Peters | 8876848 | 2001-11-13 21:51:26 +0000 | [diff] [blame] | 68 | |
Christian Heimes | 837f9e4 | 2020-05-17 01:05:40 +0200 | [diff] [blame] | 69 | if hasattr(self._inner, 'block_size'): |
| 70 | blocksize = self._inner.block_size |
Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 71 | if blocksize < 16: |
Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 72 | _warnings.warn('block_size of %d seems too small; using our ' |
| 73 | 'default of %d.' % (blocksize, self.blocksize), |
| 74 | RuntimeWarning, 2) |
| 75 | blocksize = self.blocksize |
| 76 | else: |
| 77 | _warnings.warn('No block_size attribute on given digest object; ' |
| 78 | 'Assuming %d.' % (self.blocksize), |
| 79 | RuntimeWarning, 2) |
| 80 | blocksize = self.blocksize |
| 81 | |
Christian Heimes | c4ab110 | 2013-11-20 17:35:06 +0100 | [diff] [blame] | 82 | # self.blocksize is the default blocksize. self.block_size is |
| 83 | # effective block size as well as the public API attribute. |
| 84 | self.block_size = blocksize |
| 85 | |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 86 | if len(key) > blocksize: |
Christian Heimes | 837f9e4 | 2020-05-17 01:05:40 +0200 | [diff] [blame] | 87 | key = self._digest_cons(key).digest() |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 88 | |
Serhiy Storchaka | 5f1a518 | 2016-09-11 14:41:02 +0300 | [diff] [blame] | 89 | key = key.ljust(blocksize, b'\0') |
Christian Heimes | 837f9e4 | 2020-05-17 01:05:40 +0200 | [diff] [blame] | 90 | self._outer.update(key.translate(trans_5C)) |
| 91 | self._inner.update(key.translate(trans_36)) |
Raymond Hettinger | 094662a | 2002-06-01 01:29:16 +0000 | [diff] [blame] | 92 | if msg is not None: |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 93 | self.update(msg) |
| 94 | |
Christian Heimes | c4ab110 | 2013-11-20 17:35:06 +0100 | [diff] [blame] | 95 | @property |
| 96 | def name(self): |
Christian Heimes | 837f9e4 | 2020-05-17 01:05:40 +0200 | [diff] [blame] | 97 | return "hmac-" + self._inner.name |
| 98 | |
| 99 | @property |
| 100 | def digest_cons(self): |
| 101 | return self._digest_cons |
| 102 | |
| 103 | @property |
| 104 | def inner(self): |
| 105 | return self._inner |
| 106 | |
| 107 | @property |
| 108 | def outer(self): |
| 109 | return self._outer |
Christian Heimes | c4ab110 | 2013-11-20 17:35:06 +0100 | [diff] [blame] | 110 | |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 111 | def update(self, msg): |
Gregory P. Smith | f33c57d5 | 2019-10-17 20:30:42 -0700 | [diff] [blame] | 112 | """Feed data from msg into this hashing object.""" |
Christian Heimes | 837f9e4 | 2020-05-17 01:05:40 +0200 | [diff] [blame] | 113 | self._inner.update(msg) |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 114 | |
| 115 | def copy(self): |
| 116 | """Return a separate copy of this hashing object. |
| 117 | |
| 118 | An update to this copy won't affect the original object. |
| 119 | """ |
Benjamin Peterson | 0cc7444 | 2010-08-21 02:45:15 +0000 | [diff] [blame] | 120 | # Call __new__ directly to avoid the expensive __init__. |
| 121 | other = self.__class__.__new__(self.__class__) |
Christian Heimes | 837f9e4 | 2020-05-17 01:05:40 +0200 | [diff] [blame] | 122 | other._digest_cons = self._digest_cons |
Tim Peters | 934d31b | 2004-03-20 20:11:29 +0000 | [diff] [blame] | 123 | other.digest_size = self.digest_size |
Christian Heimes | 837f9e4 | 2020-05-17 01:05:40 +0200 | [diff] [blame] | 124 | other._inner = self._inner.copy() |
| 125 | other._outer = self._outer.copy() |
Andrew M. Kuchling | 1ccdff9 | 2001-11-02 21:49:20 +0000 | [diff] [blame] | 126 | return other |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 127 | |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 128 | def _current(self): |
| 129 | """Return a hash object for the current state. |
| 130 | |
| 131 | To be used only internally with digest() and hexdigest(). |
| 132 | """ |
Christian Heimes | 837f9e4 | 2020-05-17 01:05:40 +0200 | [diff] [blame] | 133 | h = self._outer.copy() |
| 134 | h.update(self._inner.digest()) |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 135 | return h |
| 136 | |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 137 | def digest(self): |
| 138 | """Return the hash value of this hashing object. |
| 139 | |
Gregory P. Smith | f33c57d5 | 2019-10-17 20:30:42 -0700 | [diff] [blame] | 140 | This returns the hmac value as bytes. The object is |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 141 | not altered in any way by this function; you can continue |
| 142 | updating the object after calling this function. |
| 143 | """ |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 144 | h = self._current() |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 145 | return h.digest() |
| 146 | |
| 147 | def hexdigest(self): |
| 148 | """Like digest(), but returns a string of hexadecimal digits instead. |
| 149 | """ |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 150 | h = self._current() |
| 151 | return h.hexdigest() |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 152 | |
Gregory P. Smith | f33c57d5 | 2019-10-17 20:30:42 -0700 | [diff] [blame] | 153 | def new(key, msg=None, digestmod=''): |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 154 | """Create a new hashing object and return it. |
| 155 | |
Gregory P. Smith | f33c57d5 | 2019-10-17 20:30:42 -0700 | [diff] [blame] | 156 | key: bytes or buffer, The starting key for the hash. |
| 157 | msg: bytes or buffer, Initial input for the hash, or None. |
| 158 | digestmod: A hash name suitable for hashlib.new(). *OR* |
| 159 | A hashlib constructor returning a new hash object. *OR* |
| 160 | A module supporting PEP 247. |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 161 | |
Gregory P. Smith | f33c57d5 | 2019-10-17 20:30:42 -0700 | [diff] [blame] | 162 | Required as of 3.8, despite its position after the optional |
| 163 | msg argument. Passing it as a keyword argument is |
| 164 | recommended, though not required for legacy API reasons. |
| 165 | |
| 166 | You can now feed arbitrary bytes into the object using its update() |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 167 | method, and can ask for the hash value at any time by calling its digest() |
Gregory P. Smith | f33c57d5 | 2019-10-17 20:30:42 -0700 | [diff] [blame] | 168 | or hexdigest() methods. |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 169 | """ |
| 170 | return HMAC(key, msg, digestmod) |
Christian Heimes | 2f050c7 | 2018-01-27 09:53:43 +0100 | [diff] [blame] | 171 | |
| 172 | |
| 173 | def digest(key, msg, digest): |
Gregory P. Smith | f33c57d5 | 2019-10-17 20:30:42 -0700 | [diff] [blame] | 174 | """Fast inline implementation of HMAC. |
Christian Heimes | 2f050c7 | 2018-01-27 09:53:43 +0100 | [diff] [blame] | 175 | |
Gregory P. Smith | f33c57d5 | 2019-10-17 20:30:42 -0700 | [diff] [blame] | 176 | key: bytes or buffer, The key for the keyed hash object. |
| 177 | msg: bytes or buffer, Input message. |
Christian Heimes | 2f050c7 | 2018-01-27 09:53:43 +0100 | [diff] [blame] | 178 | digest: A hash name suitable for hashlib.new() for best performance. *OR* |
| 179 | A hashlib constructor returning a new hash object. *OR* |
| 180 | A module supporting PEP 247. |
Christian Heimes | 2f050c7 | 2018-01-27 09:53:43 +0100 | [diff] [blame] | 181 | """ |
| 182 | if (_hashopenssl is not None and |
| 183 | isinstance(digest, str) and digest in _openssl_md_meths): |
| 184 | return _hashopenssl.hmac_digest(key, msg, digest) |
| 185 | |
| 186 | if callable(digest): |
| 187 | digest_cons = digest |
| 188 | elif isinstance(digest, str): |
| 189 | digest_cons = lambda d=b'': _hashlib.new(digest, d) |
| 190 | else: |
| 191 | digest_cons = lambda d=b'': digest.new(d) |
| 192 | |
| 193 | inner = digest_cons() |
| 194 | outer = digest_cons() |
| 195 | blocksize = getattr(inner, 'block_size', 64) |
| 196 | if len(key) > blocksize: |
| 197 | key = digest_cons(key).digest() |
| 198 | key = key + b'\x00' * (blocksize - len(key)) |
| 199 | inner.update(key.translate(trans_36)) |
| 200 | outer.update(key.translate(trans_5C)) |
| 201 | inner.update(msg) |
| 202 | outer.update(inner.digest()) |
| 203 | return outer.digest() |