HMAC.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #
  2. # HMAC.py - Implements the HMAC algorithm as described by RFC 2104.
  3. #
  4. # ===================================================================
  5. #
  6. # Copyright (c) 2014, Legrandin <helderijs@gmail.com>
  7. # All rights reserved.
  8. #
  9. # Redistribution and use in source and binary forms, with or without
  10. # modification, are permitted provided that the following conditions
  11. # are met:
  12. #
  13. # 1. Redistributions of source code must retain the above copyright
  14. # notice, this list of conditions and the following disclaimer.
  15. # 2. Redistributions in binary form must reproduce the above copyright
  16. # notice, this list of conditions and the following disclaimer in
  17. # the documentation and/or other materials provided with the
  18. # distribution.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. # POSSIBILITY OF SUCH DAMAGE.
  32. # ===================================================================
  33. from tls.Crypto.Util.py3compat import bord, tobytes, _memoryview
  34. from binascii import unhexlify
  35. from tls.Crypto.Hash import MD5
  36. from tls.Crypto.Hash import BLAKE2s
  37. from tls.Crypto.Util.strxor import strxor
  38. from tls.Crypto.Random import get_random_bytes
  39. __all__ = ['new', 'HMAC']
  40. class HMAC(object):
  41. """An HMAC hash object.
  42. Do not instantiate directly. Use the :func:`new` function.
  43. :ivar digest_size: the size in bytes of the resulting MAC tag
  44. :vartype digest_size: integer
  45. """
  46. def __init__(self, key, msg=b"", digestmod=None):
  47. if digestmod is None:
  48. digestmod = MD5
  49. if msg is None:
  50. msg = b""
  51. # Size of the MAC tag
  52. self.digest_size = digestmod.digest_size
  53. self._digestmod = digestmod
  54. if isinstance(key, _memoryview):
  55. key = key.tobytes()
  56. try:
  57. if len(key) <= digestmod.block_size:
  58. # Step 1 or 2
  59. key_0 = key + b"\x00" * (digestmod.block_size - len(key))
  60. else:
  61. # Step 3
  62. hash_k = digestmod.new(key).digest()
  63. key_0 = hash_k + b"\x00" * (digestmod.block_size - len(hash_k))
  64. except AttributeError:
  65. # Not all hash types have "block_size"
  66. raise ValueError("Hash type incompatible to HMAC")
  67. # Step 4
  68. key_0_ipad = strxor(key_0, b"\x36" * len(key_0))
  69. # Start step 5 and 6
  70. self._inner = digestmod.new(key_0_ipad)
  71. self._inner.update(msg)
  72. # Step 7
  73. key_0_opad = strxor(key_0, b"\x5c" * len(key_0))
  74. # Start step 8 and 9
  75. self._outer = digestmod.new(key_0_opad)
  76. def update(self, msg):
  77. """Authenticate the next chunk of message.
  78. Args:
  79. data (byte string/byte array/memoryview): The next chunk of data
  80. """
  81. self._inner.update(msg)
  82. return self
  83. def _pbkdf2_hmac_assist(self, first_digest, iterations):
  84. """Carry out the expensive inner loop for PBKDF2-HMAC"""
  85. result = self._digestmod._pbkdf2_hmac_assist(
  86. self._inner,
  87. self._outer,
  88. first_digest,
  89. iterations)
  90. return result
  91. def copy(self):
  92. """Return a copy ("clone") of the HMAC object.
  93. The copy will have the same internal state as the original HMAC
  94. object.
  95. This can be used to efficiently compute the MAC tag of byte
  96. strings that share a common initial substring.
  97. :return: An :class:`HMAC`
  98. """
  99. new_hmac = HMAC(b"fake key", digestmod=self._digestmod)
  100. # Syncronize the state
  101. new_hmac._inner = self._inner.copy()
  102. new_hmac._outer = self._outer.copy()
  103. return new_hmac
  104. def digest(self):
  105. """Return the **binary** (non-printable) MAC tag of the message
  106. authenticated so far.
  107. :return: The MAC tag digest, computed over the data processed so far.
  108. Binary form.
  109. :rtype: byte string
  110. """
  111. frozen_outer_hash = self._outer.copy()
  112. frozen_outer_hash.update(self._inner.digest())
  113. return frozen_outer_hash.digest()
  114. def verify(self, mac_tag):
  115. """Verify that a given **binary** MAC (computed by another party)
  116. is valid.
  117. Args:
  118. mac_tag (byte string/byte string/memoryview): the expected MAC of the message.
  119. Raises:
  120. ValueError: if the MAC does not match. It means that the message
  121. has been tampered with or that the MAC key is incorrect.
  122. """
  123. secret = get_random_bytes(16)
  124. mac1 = BLAKE2s.new(digest_bits=160, key=secret, data=mac_tag)
  125. mac2 = BLAKE2s.new(digest_bits=160, key=secret, data=self.digest())
  126. if mac1.digest() != mac2.digest():
  127. raise ValueError("MAC check failed")
  128. def hexdigest(self):
  129. """Return the **printable** MAC tag of the message authenticated so far.
  130. :return: The MAC tag, computed over the data processed so far.
  131. Hexadecimal encoded.
  132. :rtype: string
  133. """
  134. return "".join(["%02x" % bord(x)
  135. for x in tuple(self.digest())])
  136. def hexverify(self, hex_mac_tag):
  137. """Verify that a given **printable** MAC (computed by another party)
  138. is valid.
  139. Args:
  140. hex_mac_tag (string): the expected MAC of the message,
  141. as a hexadecimal string.
  142. Raises:
  143. ValueError: if the MAC does not match. It means that the message
  144. has been tampered with or that the MAC key is incorrect.
  145. """
  146. self.verify(unhexlify(tobytes(hex_mac_tag)))
  147. def new(key, msg=b"", digestmod=None):
  148. """Create a new MAC object.
  149. Args:
  150. key (bytes/bytearray/memoryview):
  151. key for the MAC object.
  152. It must be long enough to match the expected security level of the
  153. MAC.
  154. msg (bytes/bytearray/memoryview):
  155. Optional. The very first chunk of the message to authenticate.
  156. It is equivalent to an early call to :meth:`HMAC.update`.
  157. digestmod (module):
  158. The hash to use to implement the HMAC.
  159. Default is :mod:`Crypto.Hash.MD5`.
  160. Returns:
  161. An :class:`HMAC` object
  162. """
  163. return HMAC(key, msg, digestmod)