GHash.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/MemoryStream.h>
  8. #include <AK/Types.h>
  9. #include <AK/Vector.h>
  10. #include <LibCrypto/Authentication/GHash.h>
  11. #include <LibCrypto/BigInt/UnsignedBigInteger.h>
  12. namespace {
  13. static u32 to_u32(const u8* b)
  14. {
  15. return AK::convert_between_host_and_big_endian(*(const u32*)b);
  16. }
  17. static void to_u8s(u8* b, const u32* w)
  18. {
  19. for (auto i = 0; i < 4; ++i) {
  20. auto& e = *((u32*)(b + i * 4));
  21. e = AK::convert_between_host_and_big_endian(w[i]);
  22. }
  23. }
  24. }
  25. namespace Crypto {
  26. namespace Authentication {
  27. GHash::TagType GHash::process(ReadonlyBytes aad, ReadonlyBytes cipher)
  28. {
  29. u32 tag[4] { 0, 0, 0, 0 };
  30. auto transform_one = [&](auto& buf) {
  31. size_t i = 0;
  32. for (; i < buf.size(); i += 16) {
  33. if (i + 16 <= buf.size()) {
  34. for (auto j = 0; j < 4; ++j) {
  35. tag[j] ^= to_u32(buf.offset(i + j * 4));
  36. }
  37. galois_multiply(tag, m_key, tag);
  38. }
  39. }
  40. if (i > buf.size()) {
  41. static u8 buffer[16];
  42. Bytes buffer_bytes { buffer, 16 };
  43. OutputMemoryStream stream { buffer_bytes };
  44. stream.write(buf.slice(i - 16));
  45. stream.fill_to_end(0);
  46. for (auto j = 0; j < 4; ++j) {
  47. tag[j] ^= to_u32(buffer_bytes.offset(j * 4));
  48. }
  49. galois_multiply(tag, m_key, tag);
  50. }
  51. };
  52. transform_one(aad);
  53. transform_one(cipher);
  54. auto aad_bits = 8 * (u64)aad.size();
  55. auto cipher_bits = 8 * (u64)cipher.size();
  56. auto high = [](u64 value) -> u32 { return value >> 32; };
  57. auto low = [](u64 value) -> u32 { return value & 0xffffffff; };
  58. if constexpr (GHASH_PROCESS_DEBUG) {
  59. dbgln("AAD bits: {} : {}", high(aad_bits), low(aad_bits));
  60. dbgln("Cipher bits: {} : {}", high(cipher_bits), low(cipher_bits));
  61. dbgln("Tag bits: {} : {} : {} : {}", tag[0], tag[1], tag[2], tag[3]);
  62. }
  63. tag[0] ^= high(aad_bits);
  64. tag[1] ^= low(aad_bits);
  65. tag[2] ^= high(cipher_bits);
  66. tag[3] ^= low(cipher_bits);
  67. dbgln_if(GHASH_PROCESS_DEBUG, "Tag bits: {} : {} : {} : {}", tag[0], tag[1], tag[2], tag[3]);
  68. galois_multiply(tag, m_key, tag);
  69. TagType digest;
  70. to_u8s(digest.data, tag);
  71. return digest;
  72. }
  73. /// Galois Field multiplication using <x^127 + x^7 + x^2 + x + 1>.
  74. /// Note that x, y, and z are strictly BE.
  75. void galois_multiply(u32 (&z)[4], const u32 (&_x)[4], const u32 (&_y)[4])
  76. {
  77. u32 x[4] { _x[0], _x[1], _x[2], _x[3] };
  78. u32 y[4] { _y[0], _y[1], _y[2], _y[3] };
  79. __builtin_memset(z, 0, sizeof(z));
  80. for (ssize_t i = 127; i > -1; --i) {
  81. if ((y[3 - (i / 32)] >> (i % 32)) & 1) {
  82. z[0] ^= x[0];
  83. z[1] ^= x[1];
  84. z[2] ^= x[2];
  85. z[3] ^= x[3];
  86. }
  87. auto a0 = x[0] & 1;
  88. x[0] >>= 1;
  89. auto a1 = x[1] & 1;
  90. x[1] >>= 1;
  91. x[1] |= a0 << 31;
  92. auto a2 = x[2] & 1;
  93. x[2] >>= 1;
  94. x[2] |= a1 << 31;
  95. auto a3 = x[3] & 1;
  96. x[3] >>= 1;
  97. x[3] |= a2 << 31;
  98. if (a3)
  99. x[0] ^= 0xe1000000;
  100. }
  101. }
  102. }
  103. }