KMAC256.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # ===================================================================
  2. #
  3. # Copyright (c) 2021, 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 Crypto.Util.py3compat import is_bytes
  31. from .KMAC128 import KMAC_Hash
  32. from . import cSHAKE256
  33. def new(**kwargs):
  34. """Create a new KMAC256 object.
  35. Args:
  36. key (bytes/bytearray/memoryview):
  37. The key to use to compute the MAC.
  38. It must be at least 256 bits long (32 bytes).
  39. data (bytes/bytearray/memoryview):
  40. Optional. The very first chunk of the message to authenticate.
  41. It is equivalent to an early call to :meth:`KMAC_Hash.update`.
  42. mac_len (integer):
  43. Optional. The size of the authentication tag, in bytes.
  44. Default is 64. Minimum is 8.
  45. custom (bytes/bytearray/memoryview):
  46. Optional. A customization byte string (``S`` in SP 800-185).
  47. Returns:
  48. A :class:`KMAC_Hash` hash object
  49. """
  50. key = kwargs.pop("key", None)
  51. if not is_bytes(key):
  52. raise TypeError("You must pass a key to KMAC256")
  53. if len(key) < 32:
  54. raise ValueError("The key must be at least 256 bits long (32 bytes)")
  55. data = kwargs.pop("data", None)
  56. mac_len = kwargs.pop("mac_len", 64)
  57. if mac_len < 8:
  58. raise ValueError("'mac_len' must be 8 bytes or more")
  59. custom = kwargs.pop("custom", b"")
  60. if kwargs:
  61. raise TypeError("Unknown parameters: " + str(kwargs))
  62. return KMAC_Hash(data, key, mac_len, custom, "20", cSHAKE256, 136)