GHash.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/ByteReader.h>
  8. #include <AK/Endian.h>
  9. #include <AK/Types.h>
  10. #include <LibCrypto/Hash/HashFunction.h>
  11. #ifndef KERNEL
  12. # include <AK/DeprecatedString.h>
  13. #endif
  14. namespace Crypto {
  15. namespace Authentication {
  16. void galois_multiply(u32 (&z)[4], const u32 (&x)[4], const u32 (&y)[4]);
  17. struct GHashDigest {
  18. constexpr static size_t Size = 16;
  19. u8 data[Size];
  20. u8 const* immutable_data() const { return data; }
  21. size_t data_length() { return Size; }
  22. };
  23. class GHash final {
  24. public:
  25. using TagType = GHashDigest;
  26. template<size_t N>
  27. explicit GHash(char const (&key)[N])
  28. : GHash({ key, N })
  29. {
  30. }
  31. explicit GHash(ReadonlyBytes key)
  32. {
  33. VERIFY(key.size() >= 16);
  34. for (size_t i = 0; i < 16; i += 4) {
  35. m_key[i / 4] = AK::convert_between_host_and_big_endian(ByteReader::load32(key.offset(i)));
  36. }
  37. }
  38. constexpr static size_t digest_size() { return TagType::Size; }
  39. #ifndef KERNEL
  40. DeprecatedString class_name() const
  41. {
  42. return "GHash";
  43. }
  44. #endif
  45. TagType process(ReadonlyBytes aad, ReadonlyBytes cipher);
  46. private:
  47. u32 m_key[4];
  48. };
  49. }
  50. }