SHA3_512.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # -*- coding: utf-8 -*-
  2. #
  3. # ===================================================================
  4. # The contents of this file are dedicated to the public domain. To
  5. # the extent that dedication to the public domain is not available,
  6. # everyone is granted a worldwide, perpetual, royalty-free,
  7. # non-exclusive license to exercise all rights associated with the
  8. # contents of this file for any purpose whatsoever.
  9. # No rights are reserved.
  10. #
  11. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  12. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  13. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  15. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. # SOFTWARE.
  19. # ===================================================================
  20. from tls.Crypto.Util.py3compat import bord
  21. from tls.Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
  22. VoidPointer, SmartPointer,
  23. create_string_buffer,
  24. get_raw_buffer, c_size_t,
  25. c_uint8_ptr)
  26. from tls.Crypto.Hash.keccak import _raw_keccak_lib
  27. class SHA3_512_Hash(object):
  28. """A SHA3-512 hash object.
  29. Do not instantiate directly.
  30. Use the :func:`new` function.
  31. :ivar oid: ASN.1 Object ID
  32. :vartype oid: string
  33. :ivar digest_size: the size in bytes of the resulting hash
  34. :vartype digest_size: integer
  35. """
  36. # The size of the resulting hash in bytes.
  37. digest_size = 64
  38. # ASN.1 Object ID
  39. oid = "2.16.840.1.101.3.4.2.10"
  40. def __init__(self, data, update_after_digest):
  41. self._update_after_digest = update_after_digest
  42. self._digest_done = False
  43. state = VoidPointer()
  44. result = _raw_keccak_lib.keccak_init(state.address_of(),
  45. c_size_t(self.digest_size * 2),
  46. 0x06)
  47. if result:
  48. raise ValueError("Error %d while instantiating SHA-3/512"
  49. % result)
  50. self._state = SmartPointer(state.get(),
  51. _raw_keccak_lib.keccak_destroy)
  52. if data:
  53. self.update(data)
  54. def update(self, data):
  55. """Continue hashing of a message by consuming the next chunk of data.
  56. Args:
  57. data (byte string/byte array/memoryview): The next chunk of the message being hashed.
  58. """
  59. if self._digest_done and not self._update_after_digest:
  60. raise TypeError("You can only call 'digest' or 'hexdigest' on this object")
  61. result = _raw_keccak_lib.keccak_absorb(self._state.get(),
  62. c_uint8_ptr(data),
  63. c_size_t(len(data)))
  64. if result:
  65. raise ValueError("Error %d while updating SHA-3/512"
  66. % result)
  67. return self
  68. def digest(self):
  69. """Return the **binary** (non-printable) digest of the message that has been hashed so far.
  70. :return: The hash digest, computed over the data processed so far.
  71. Binary form.
  72. :rtype: byte string
  73. """
  74. self._digest_done = True
  75. bfr = create_string_buffer(self.digest_size)
  76. result = _raw_keccak_lib.keccak_digest(self._state.get(),
  77. bfr,
  78. c_size_t(self.digest_size))
  79. if result:
  80. raise ValueError("Error %d while instantiating SHA-3/512"
  81. % result)
  82. self._digest_value = get_raw_buffer(bfr)
  83. return self._digest_value
  84. def hexdigest(self):
  85. """Return the **printable** digest of the message that has been hashed so far.
  86. :return: The hash digest, computed over the data processed so far.
  87. Hexadecimal encoded.
  88. :rtype: string
  89. """
  90. return "".join(["%02x" % bord(x) for x in self.digest()])
  91. def new(self):
  92. """Create a fresh SHA3-512 hash object."""
  93. return type(self)(None, self._update_after_digest)
  94. def new(*args, **kwargs):
  95. """Create a new hash object.
  96. Args:
  97. data (byte string/byte array/memoryview):
  98. The very first chunk of the message to hash.
  99. It is equivalent to an early call to :meth:`update`.
  100. update_after_digest (boolean):
  101. Whether :meth:`digest` can be followed by another :meth:`update`
  102. (default: ``False``).
  103. :Return: A :class:`SHA3_512_Hash` hash object
  104. """
  105. data = kwargs.pop("data", None)
  106. update_after_digest = kwargs.pop("update_after_digest", False)
  107. if len(args) == 1:
  108. if data:
  109. raise ValueError("Initial data for hash specified twice")
  110. data = args[0]
  111. if kwargs:
  112. raise TypeError("Unknown parameters: " + str(kwargs))
  113. return SHA3_512_Hash(data, update_after_digest)
  114. # The size of the resulting hash in bytes.
  115. digest_size = SHA3_512_Hash.digest_size