GHash.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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/ByteString.h>
  13. #endif
  14. namespace Crypto::Authentication {
  15. void galois_multiply(u32 (&z)[4], const u32 (&x)[4], const u32 (&y)[4]);
  16. struct GHashDigest {
  17. constexpr static size_t Size = 16;
  18. u8 data[Size];
  19. u8 const* immutable_data() const { return data; }
  20. size_t data_length() { return Size; }
  21. };
  22. class GHash final {
  23. public:
  24. using TagType = GHashDigest;
  25. template<size_t N>
  26. explicit GHash(char const (&key)[N])
  27. : GHash({ key, N })
  28. {
  29. }
  30. explicit GHash(ReadonlyBytes key)
  31. {
  32. VERIFY(key.size() >= 16);
  33. for (size_t i = 0; i < 16; i += 4) {
  34. m_key[i / 4] = AK::convert_between_host_and_big_endian(ByteReader::load32(key.offset(i)));
  35. }
  36. }
  37. constexpr static size_t digest_size() { return TagType::Size; }
  38. #ifndef KERNEL
  39. ByteString class_name() const
  40. {
  41. return "GHash";
  42. }
  43. #endif
  44. TagType process(ReadonlyBytes aad, ReadonlyBytes cipher);
  45. private:
  46. u32 m_key[4];
  47. };
  48. }