Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 1 | """HMAC (Keyed-Hashing for Message Authentication) Python module. |
| 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 |
| 7 | |
Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 8 | trans_5C = bytes((x ^ 0x5C) for x in range(256)) |
| 9 | trans_36 = bytes((x ^ 0x36) for x in range(256)) |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 10 | |
Andrew M. Kuchling | 1ccdff9 | 2001-11-02 21:49:20 +0000 | [diff] [blame] | 11 | # The size of the digests returned by HMAC depends on the underlying |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 12 | # 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] | 13 | digest_size = None |
| 14 | |
Tim Peters | 934d31b | 2004-03-20 20:11:29 +0000 | [diff] [blame] | 15 | # A unique object passed by HMAC.copy() to the HMAC constructor, in order |
| 16 | # that the latter return very quickly. HMAC("") in contrast is quite |
| 17 | # expensive. |
| 18 | _secret_backdoor_key = [] |
| 19 | |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 20 | class HMAC: |
Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 21 | """RFC 2104 HMAC class. Also complies with RFC 4231. |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 22 | |
Andrew M. Kuchling | 1ccdff9 | 2001-11-02 21:49:20 +0000 | [diff] [blame] | 23 | This supports the API for Cryptographic Hash Functions (PEP 247). |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 24 | """ |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 25 | blocksize = 64 # 512-bit HMAC; can be changed in subclasses. |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 26 | |
| 27 | def __init__(self, key, msg = None, digestmod = None): |
| 28 | """Create a new HMAC object. |
| 29 | |
| 30 | key: key for the keyed hash object. |
| 31 | msg: Initial input for the hash, if provided. |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 32 | digestmod: A module supporting PEP 247. *OR* |
| 33 | A hashlib constructor returning a new hash object. |
| 34 | Defaults to hashlib.md5. |
Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 35 | |
| 36 | Note: key and msg must be bytes objects. |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 37 | """ |
Tim Peters | 934d31b | 2004-03-20 20:11:29 +0000 | [diff] [blame] | 38 | |
| 39 | if key is _secret_backdoor_key: # cheap |
| 40 | return |
| 41 | |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 42 | if not isinstance(key, bytes): |
| 43 | raise TypeError("expected bytes, but got %r" % type(key).__name__) |
Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 44 | |
Raymond Hettinger | 7fdfc2d | 2002-05-31 17:49:10 +0000 | [diff] [blame] | 45 | if digestmod is None: |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 46 | import hashlib |
| 47 | digestmod = hashlib.md5 |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 49 | if hasattr(digestmod, '__call__'): |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 50 | self.digest_cons = digestmod |
| 51 | else: |
Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 52 | self.digest_cons = lambda d=b'': digestmod.new(d) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 53 | |
| 54 | self.outer = self.digest_cons() |
| 55 | self.inner = self.digest_cons() |
| 56 | self.digest_size = self.inner.digest_size |
Tim Peters | 8876848 | 2001-11-13 21:51:26 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 58 | if hasattr(self.inner, 'block_size'): |
| 59 | blocksize = self.inner.block_size |
| 60 | if blocksize < 16: |
| 61 | # Very low blocksize, most likely a legacy value like |
| 62 | # Lib/sha.py and Lib/md5.py have. |
| 63 | _warnings.warn('block_size of %d seems too small; using our ' |
| 64 | 'default of %d.' % (blocksize, self.blocksize), |
| 65 | RuntimeWarning, 2) |
| 66 | blocksize = self.blocksize |
| 67 | else: |
| 68 | _warnings.warn('No block_size attribute on given digest object; ' |
| 69 | 'Assuming %d.' % (self.blocksize), |
| 70 | RuntimeWarning, 2) |
| 71 | blocksize = self.blocksize |
| 72 | |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 73 | if len(key) > blocksize: |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 74 | key = self.digest_cons(key).digest() |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 76 | key = key + bytes(blocksize - len(key)) |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 77 | self.outer.update(key.translate(trans_5C)) |
| 78 | self.inner.update(key.translate(trans_36)) |
Raymond Hettinger | 094662a | 2002-06-01 01:29:16 +0000 | [diff] [blame] | 79 | if msg is not None: |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 80 | self.update(msg) |
| 81 | |
| 82 | ## def clear(self): |
| 83 | ## raise NotImplementedError, "clear() method not available in HMAC." |
| 84 | |
| 85 | def update(self, msg): |
| 86 | """Update this hashing object with the string msg. |
| 87 | """ |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 88 | if not isinstance(msg, bytes): |
| 89 | raise TypeError("expected bytes, but got %r" % type(msg).__name__) |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 90 | self.inner.update(msg) |
| 91 | |
| 92 | def copy(self): |
| 93 | """Return a separate copy of this hashing object. |
| 94 | |
| 95 | An update to this copy won't affect the original object. |
| 96 | """ |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 97 | other = self.__class__(_secret_backdoor_key) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 98 | other.digest_cons = self.digest_cons |
Tim Peters | 934d31b | 2004-03-20 20:11:29 +0000 | [diff] [blame] | 99 | other.digest_size = self.digest_size |
Andrew M. Kuchling | 1ccdff9 | 2001-11-02 21:49:20 +0000 | [diff] [blame] | 100 | other.inner = self.inner.copy() |
| 101 | other.outer = self.outer.copy() |
| 102 | return other |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 103 | |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 104 | def _current(self): |
| 105 | """Return a hash object for the current state. |
| 106 | |
| 107 | To be used only internally with digest() and hexdigest(). |
| 108 | """ |
| 109 | h = self.outer.copy() |
| 110 | h.update(self.inner.digest()) |
| 111 | return h |
| 112 | |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 113 | def digest(self): |
| 114 | """Return the hash value of this hashing object. |
| 115 | |
| 116 | This returns a string containing 8-bit data. The object is |
| 117 | not altered in any way by this function; you can continue |
| 118 | updating the object after calling this function. |
| 119 | """ |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 120 | h = self._current() |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 121 | return h.digest() |
| 122 | |
| 123 | def hexdigest(self): |
| 124 | """Like digest(), but returns a string of hexadecimal digits instead. |
| 125 | """ |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 126 | h = self._current() |
| 127 | return h.hexdigest() |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 128 | |
| 129 | def new(key, msg = None, digestmod = None): |
| 130 | """Create a new hashing object and return it. |
| 131 | |
| 132 | key: The starting key for the hash. |
| 133 | msg: if available, will immediately be hashed into the object's starting |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 134 | state. |
Guido van Rossum | 8ceef41 | 2001-09-11 15:54:00 +0000 | [diff] [blame] | 135 | |
| 136 | You can now feed arbitrary strings into the object using its update() |
| 137 | method, and can ask for the hash value at any time by calling its digest() |
| 138 | method. |
| 139 | """ |
| 140 | return HMAC(key, msg, digestmod) |