HMAC.h 3.6 KB

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