HMAC.h 3.6 KB

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