SHAKE128.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # ===================================================================
  2. #
  3. # Copyright (c) 2015, Legrandin <helderijs@gmail.com>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. #
  10. # 1. Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in
  14. # the documentation and/or other materials provided with the
  15. # distribution.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. # POSSIBILITY OF SUCH DAMAGE.
  29. # ===================================================================
  30. from tls.Crypto.Util.py3compat import bord
  31. from tls.Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
  32. VoidPointer, SmartPointer,
  33. create_string_buffer,
  34. get_raw_buffer, c_size_t,
  35. c_uint8_ptr)
  36. from tls.Crypto.Hash.keccak import _raw_keccak_lib
  37. class SHAKE128_XOF(object):
  38. """A SHAKE128 hash object.
  39. Do not instantiate directly.
  40. Use the :func:`new` function.
  41. :ivar oid: ASN.1 Object ID
  42. :vartype oid: string
  43. """
  44. # ASN.1 Object ID
  45. oid = "2.16.840.1.101.3.4.2.11"
  46. def __init__(self, data=None):
  47. state = VoidPointer()
  48. result = _raw_keccak_lib.keccak_init(state.address_of(),
  49. c_size_t(32),
  50. 0x1F)
  51. if result:
  52. raise ValueError("Error %d while instantiating SHAKE128"
  53. % result)
  54. self._state = SmartPointer(state.get(),
  55. _raw_keccak_lib.keccak_destroy)
  56. self._is_squeezing = False
  57. if data:
  58. self.update(data)
  59. def update(self, data):
  60. """Continue hashing of a message by consuming the next chunk of data.
  61. Args:
  62. data (byte string/byte array/memoryview): The next chunk of the message being hashed.
  63. """
  64. if self._is_squeezing:
  65. raise TypeError("You cannot call 'update' after the first 'read'")
  66. result = _raw_keccak_lib.keccak_absorb(self._state.get(),
  67. c_uint8_ptr(data),
  68. c_size_t(len(data)))
  69. if result:
  70. raise ValueError("Error %d while updating SHAKE128 state"
  71. % result)
  72. return self
  73. def read(self, length):
  74. """
  75. Compute the next piece of XOF output.
  76. .. note::
  77. You cannot use :meth:`update` anymore after the first call to
  78. :meth:`read`.
  79. Args:
  80. length (integer): the amount of bytes this method must return
  81. :return: the next piece of XOF output (of the given length)
  82. :rtype: byte string
  83. """
  84. self._is_squeezing = True
  85. bfr = create_string_buffer(length)
  86. result = _raw_keccak_lib.keccak_squeeze(self._state.get(),
  87. bfr,
  88. c_size_t(length))
  89. if result:
  90. raise ValueError("Error %d while extracting from SHAKE128"
  91. % result)
  92. return get_raw_buffer(bfr)
  93. def new(self, data=None):
  94. return type(self)(data=data)
  95. def new(data=None):
  96. """Return a fresh instance of a SHAKE128 object.
  97. Args:
  98. data (bytes/bytearray/memoryview):
  99. The very first chunk of the message to hash.
  100. It is equivalent to an early call to :meth:`update`.
  101. Optional.
  102. :Return: A :class:`SHAKE128_XOF` object
  103. """
  104. return SHAKE128_XOF(data=data)