GHash.cpp 3.0 KB

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