GCM.h 4.4 KB

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