HMAC.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/ByteBuffer.h>
  8. #include <AK/StringBuilder.h>
  9. #include <AK/StringView.h>
  10. #include <AK/Types.h>
  11. #include <AK/Vector.h>
  12. #ifndef KERNEL
  13. # include <AK/DeprecatedString.h>
  14. #endif
  15. constexpr static auto IPAD = 0x36;
  16. constexpr static auto OPAD = 0x5c;
  17. namespace Crypto {
  18. namespace Authentication {
  19. template<typename HashT>
  20. class HMAC {
  21. public:
  22. using HashType = HashT;
  23. using TagType = typename HashType::DigestType;
  24. constexpr size_t digest_size() const { return m_inner_hasher.digest_size(); }
  25. template<typename KeyBufferType, typename... Args>
  26. HMAC(KeyBufferType key, Args... args)
  27. : m_inner_hasher(args...)
  28. , m_outer_hasher(args...)
  29. {
  30. derive_key(key);
  31. reset();
  32. }
  33. TagType process(u8 const* message, size_t length)
  34. {
  35. reset();
  36. update(message, length);
  37. return digest();
  38. }
  39. void update(u8 const* message, size_t length)
  40. {
  41. m_inner_hasher.update(message, length);
  42. }
  43. TagType process(ReadonlyBytes span) { return process(span.data(), span.size()); }
  44. TagType process(StringView string) { return process((u8 const*)string.characters_without_null_termination(), string.length()); }
  45. void update(ReadonlyBytes span) { return update(span.data(), span.size()); }
  46. void update(StringView string) { return update((u8 const*)string.characters_without_null_termination(), string.length()); }
  47. TagType digest()
  48. {
  49. m_outer_hasher.update(m_inner_hasher.digest().immutable_data(), m_inner_hasher.digest_size());
  50. auto result = m_outer_hasher.digest();
  51. reset();
  52. return result;
  53. }
  54. void reset()
  55. {
  56. m_inner_hasher.reset();
  57. m_outer_hasher.reset();
  58. m_inner_hasher.update(m_key_data, m_inner_hasher.block_size());
  59. m_outer_hasher.update(m_key_data + m_inner_hasher.block_size(), m_outer_hasher.block_size());
  60. }
  61. #ifndef KERNEL
  62. DeprecatedString class_name() const
  63. {
  64. StringBuilder builder;
  65. builder.append("HMAC-"sv);
  66. builder.append(m_inner_hasher.class_name());
  67. return builder.to_deprecated_string();
  68. }
  69. #endif
  70. private:
  71. void derive_key(u8 const* key, size_t length)
  72. {
  73. auto block_size = m_inner_hasher.block_size();
  74. // Note: The block size of all the current hash functions is 512 bits.
  75. Vector<u8, 64> v_key;
  76. v_key.resize(block_size);
  77. auto key_buffer = v_key.span();
  78. // m_key_data is zero'd, so copying the data in
  79. // the first few bytes leaves the rest zero, which
  80. // is exactly what we want (zero padding)
  81. if (length > block_size) {
  82. m_inner_hasher.update(key, length);
  83. auto digest = m_inner_hasher.digest();
  84. // FIXME: should we check if the hash function creates more data than its block size?
  85. key_buffer.overwrite(0, digest.immutable_data(), m_inner_hasher.digest_size());
  86. } else {
  87. key_buffer.overwrite(0, key, length);
  88. }
  89. // fill out the inner and outer padded keys
  90. auto* i_key = m_key_data;
  91. auto* o_key = m_key_data + block_size;
  92. for (size_t i = 0; i < block_size; ++i) {
  93. auto key_byte = key_buffer[i];
  94. i_key[i] = key_byte ^ IPAD;
  95. o_key[i] = key_byte ^ OPAD;
  96. }
  97. }
  98. void derive_key(ReadonlyBytes key) { derive_key(key.data(), key.size()); }
  99. void derive_key(StringView key) { derive_key(key.bytes()); }
  100. HashType m_inner_hasher, m_outer_hasher;
  101. u8 m_key_data[2048];
  102. };
  103. }
  104. }