GHash.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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::Authentication {
  23. GHash::TagType GHash::process(ReadonlyBytes aad, ReadonlyBytes cipher)
  24. {
  25. u32 tag[4] { 0, 0, 0, 0 };
  26. auto transform_one = [&](auto& buf) {
  27. size_t i = 0;
  28. for (; i < buf.size(); i += 16) {
  29. if (i + 16 <= buf.size()) {
  30. for (auto j = 0; j < 4; ++j) {
  31. tag[j] ^= to_u32(buf.offset(i + j * 4));
  32. }
  33. galois_multiply(tag, m_key, tag);
  34. }
  35. }
  36. if (i > buf.size()) {
  37. u8 buffer[16] = {};
  38. Bytes buffer_bytes { buffer, 16 };
  39. buf.slice(i - 16).copy_to(buffer_bytes);
  40. for (auto j = 0; j < 4; ++j) {
  41. tag[j] ^= to_u32(buffer_bytes.offset(j * 4));
  42. }
  43. galois_multiply(tag, m_key, tag);
  44. }
  45. };
  46. transform_one(aad);
  47. transform_one(cipher);
  48. auto aad_bits = 8 * (u64)aad.size();
  49. auto cipher_bits = 8 * (u64)cipher.size();
  50. auto high = [](u64 value) -> u32 { return value >> 32; };
  51. auto low = [](u64 value) -> u32 { return value & 0xffffffff; };
  52. if constexpr (GHASH_PROCESS_DEBUG) {
  53. dbgln("AAD bits: {} : {}", high(aad_bits), low(aad_bits));
  54. dbgln("Cipher bits: {} : {}", high(cipher_bits), low(cipher_bits));
  55. dbgln("Tag bits: {} : {} : {} : {}", tag[0], tag[1], tag[2], tag[3]);
  56. }
  57. tag[0] ^= high(aad_bits);
  58. tag[1] ^= low(aad_bits);
  59. tag[2] ^= high(cipher_bits);
  60. tag[3] ^= low(cipher_bits);
  61. dbgln_if(GHASH_PROCESS_DEBUG, "Tag bits: {} : {} : {} : {}", tag[0], tag[1], tag[2], tag[3]);
  62. galois_multiply(tag, m_key, tag);
  63. TagType digest;
  64. to_u8s(digest.data, tag);
  65. return digest;
  66. }
  67. /// Galois Field multiplication using <x^127 + x^7 + x^2 + x + 1>.
  68. /// Note that x, y, and z are strictly BE.
  69. void galois_multiply(u32 (&z)[4], const u32 (&_x)[4], const u32 (&_y)[4])
  70. {
  71. u32 x[4] { _x[0], _x[1], _x[2], _x[3] };
  72. u32 y[4] { _y[0], _y[1], _y[2], _y[3] };
  73. __builtin_memset(z, 0, sizeof(z));
  74. #pragma GCC unroll 16
  75. for (ssize_t i = 127; i > -1; --i) {
  76. auto r = -((y[3 - (i / 32)] >> (i % 32)) & 1);
  77. z[0] ^= x[0] & r;
  78. z[1] ^= x[1] & r;
  79. z[2] ^= x[2] & r;
  80. z[3] ^= x[3] & r;
  81. auto a0 = x[0] & 1;
  82. x[0] >>= 1;
  83. auto a1 = x[1] & 1;
  84. x[1] >>= 1;
  85. x[1] |= a0 << 31;
  86. auto a2 = x[2] & 1;
  87. x[2] >>= 1;
  88. x[2] |= a1 << 31;
  89. auto a3 = x[3] & 1;
  90. x[3] >>= 1;
  91. x[3] |= a2 << 31;
  92. x[0] ^= 0xe1000000 & -a3;
  93. }
  94. }
  95. }