GCM.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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() = default;
  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(ReadonlyBytes in, Bytes out, ReadonlyBytes iv_in, ReadonlyBytes aad, Bytes tag)
  53. {
  54. auto iv_buf_result = ByteBuffer::copy(iv_in);
  55. // Not enough memory to figure out :shrug:
  56. if (!iv_buf_result.has_value()) {
  57. dbgln("GCM::encrypt: Not enough memory to allocate {} bytes for IV", iv_in.size());
  58. return;
  59. }
  60. auto iv = iv_buf_result->bytes();
  61. // Increment the IV for block 0
  62. CTR<T>::increment(iv);
  63. typename T::BlockType block0;
  64. block0.overwrite(iv);
  65. this->cipher().encrypt_block(block0, block0);
  66. // Skip past block 0
  67. CTR<T>::increment(iv);
  68. if (in.is_empty())
  69. CTR<T>::key_stream(out, iv);
  70. else
  71. CTR<T>::encrypt(in, out, iv);
  72. auto auth_tag = m_ghash->process(aad, out);
  73. block0.apply_initialization_vector({ auth_tag.data, array_size(auth_tag.data) });
  74. block0.bytes().copy_to(tag);
  75. }
  76. VerificationConsistency decrypt(ReadonlyBytes in, Bytes out, ReadonlyBytes iv_in, ReadonlyBytes aad, ReadonlyBytes tag)
  77. {
  78. auto iv_buf_result = ByteBuffer::copy(iv_in);
  79. // Not enough memory to figure out :shrug:
  80. if (!iv_buf_result.has_value())
  81. return VerificationConsistency::Inconsistent;
  82. auto iv = iv_buf_result->bytes();
  83. // Increment the IV for block 0
  84. CTR<T>::increment(iv);
  85. typename T::BlockType block0;
  86. block0.overwrite(iv);
  87. this->cipher().encrypt_block(block0, block0);
  88. // Skip past block 0
  89. CTR<T>::increment(iv);
  90. auto auth_tag = m_ghash->process(aad, in);
  91. block0.apply_initialization_vector({ auth_tag.data, array_size(auth_tag.data) });
  92. auto test_consistency = [&] {
  93. if (block0.block_size() != tag.size() || __builtin_memcmp(block0.bytes().data(), tag.data(), tag.size()) != 0)
  94. return VerificationConsistency::Inconsistent;
  95. return VerificationConsistency::Consistent;
  96. };
  97. // FIXME: This block needs constant-time comparisons.
  98. if (in.is_empty()) {
  99. out = {};
  100. return test_consistency();
  101. }
  102. CTR<T>::encrypt(in, out, iv);
  103. return test_consistency();
  104. }
  105. private:
  106. static constexpr auto block_size = T::BlockType::BlockSizeInBits / 8;
  107. u8 m_auth_key_storage[block_size];
  108. Bytes m_auth_key { m_auth_key_storage, block_size };
  109. Optional<Authentication::GHash> m_ghash;
  110. };
  111. }
  112. }