GHash.h 1.2 KB

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