GCM.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/OwnPtr.h>
  8. #include <AK/String.h>
  9. #include <AK/StringBuilder.h>
  10. #include <AK/StringView.h>
  11. #include <LibCrypto/Authentication/GHash.h>
  12. #include <LibCrypto/Cipher/Mode/CTR.h>
  13. #include <LibCrypto/Verification.h>
  14. namespace Crypto {
  15. namespace Cipher {
  16. using IncrementFunction = IncrementInplace;
  17. template<typename T>
  18. class GCM : public CTR<T, IncrementFunction> {
  19. public:
  20. constexpr static size_t IVSizeInBits = 128;
  21. virtual ~GCM() { }
  22. template<typename... Args>
  23. explicit constexpr GCM<T>(Args... args)
  24. : CTR<T>(args...)
  25. {
  26. static_assert(T::BlockSizeInBits == 128u, "GCM Mode is only available for 128-bit Ciphers");
  27. __builtin_memset(m_auth_key_storage, 0, block_size);
  28. typename T::BlockType key_block(m_auth_key_storage, block_size);
  29. this->cipher().encrypt_block(key_block, key_block);
  30. key_block.bytes().copy_to(m_auth_key);
  31. m_ghash = Authentication::GHash(m_auth_key);
  32. }
  33. virtual String class_name() const override
  34. {
  35. StringBuilder builder;
  36. builder.append(this->cipher().class_name());
  37. builder.append("_GCM");
  38. return builder.build();
  39. }
  40. virtual size_t IV_length() const override { return IVSizeInBits / 8; }
  41. // FIXME: This overload throws away the auth stuff, think up a better way to return more than a single bytebuffer.
  42. virtual void encrypt(ReadonlyBytes in, Bytes& out, ReadonlyBytes ivec = {}, Bytes* = nullptr) override
  43. {
  44. VERIFY(!ivec.is_empty());
  45. static ByteBuffer dummy;
  46. encrypt(in, out, ivec, dummy, dummy);
  47. }
  48. virtual void decrypt(ReadonlyBytes in, Bytes& out, ReadonlyBytes ivec = {}) override
  49. {
  50. encrypt(in, out, ivec);
  51. }
  52. void encrypt(const ReadonlyBytes& in, Bytes out, const ReadonlyBytes& iv_in, const ReadonlyBytes& aad, Bytes tag)
  53. {
  54. auto iv_buf = ByteBuffer::copy(iv_in.data(), iv_in.size());
  55. auto iv = iv_buf.bytes();
  56. // Increment the IV for block 0
  57. CTR<T>::increment(iv);
  58. typename T::BlockType block0;
  59. block0.overwrite(iv);
  60. this->cipher().encrypt_block(block0, block0);
  61. // Skip past block 0
  62. CTR<T>::increment(iv);
  63. if (in.is_empty())
  64. CTR<T>::key_stream(out, iv);
  65. else
  66. CTR<T>::encrypt(in, out, iv);
  67. auto auth_tag = m_ghash->process(aad, out);
  68. block0.apply_initialization_vector({ auth_tag.data, array_size(auth_tag.data) });
  69. block0.bytes().copy_to(tag);
  70. }
  71. VerificationConsistency decrypt(ReadonlyBytes in, Bytes out, ReadonlyBytes iv_in, ReadonlyBytes aad, ReadonlyBytes tag)
  72. {
  73. auto iv_buf = ByteBuffer::copy(iv_in.data(), iv_in.size());
  74. auto iv = iv_buf.bytes();
  75. // Increment the IV for block 0
  76. CTR<T>::increment(iv);
  77. typename T::BlockType block0;
  78. block0.overwrite(iv);
  79. this->cipher().encrypt_block(block0, block0);
  80. // Skip past block 0
  81. CTR<T>::increment(iv);
  82. auto auth_tag = m_ghash->process(aad, in);
  83. block0.apply_initialization_vector({ auth_tag.data, array_size(auth_tag.data) });
  84. auto test_consistency = [&] {
  85. if (block0.block_size() != tag.size() || __builtin_memcmp(block0.bytes().data(), tag.data(), tag.size()) != 0)
  86. return VerificationConsistency::Inconsistent;
  87. return VerificationConsistency::Consistent;
  88. };
  89. // FIXME: This block needs constant-time comparisons.
  90. if (in.is_empty()) {
  91. out = {};
  92. return test_consistency();
  93. }
  94. CTR<T>::encrypt(in, out, iv);
  95. return test_consistency();
  96. }
  97. private:
  98. static constexpr auto block_size = T::BlockType::BlockSizeInBits / 8;
  99. u8 m_auth_key_storage[block_size];
  100. Bytes m_auth_key { m_auth_key_storage, block_size };
  101. Optional<Authentication::GHash> m_ghash;
  102. };
  103. }
  104. }