hmac.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #pragma once
  2. #include <array>
  3. #include <cstddef>
  4. #include <cstdint>
  5. #include <span>
  6. namespace sunrise::middleware::crypto::hmac {
  7. /** The widest digest any algorithm here produces. */
  8. inline constexpr std::size_t kMaximumDigestSize = 32;
  9. /** One finished digest and the bytes of it that are meaningful. */
  10. struct Digest {
  11. std::array<std::byte, kMaximumDigestSize> bytes{};
  12. std::size_t size{};
  13. };
  14. /** Digest algorithms the peer can pick, numbered with the ids it picks them by. */
  15. enum class Algorithm : std::uint8_t {
  16. sha256 = 0,
  17. sha1 = 2,
  18. murmur3 = 4,
  19. };
  20. /**
  21. * Authenticates two buffers as one message.
  22. * Two buffers, not one: the record skips the bytes between its header and its body.
  23. * @param algorithm Digest to key.
  24. * @param key Authentication key.
  25. * @param first Leading covered bytes.
  26. * @param second Trailing covered bytes.
  27. * @param output Receives the digest only on success.
  28. * @return True when Windows completed the operation.
  29. */
  30. [[nodiscard]] bool authenticate(Algorithm algorithm,
  31. std::span<const std::byte> key,
  32. std::span<const std::byte> first,
  33. std::span<const std::byte> second,
  34. Digest& output) noexcept;
  35. } // namespace sunrise::middleware::crypto::hmac