Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
HMAC.h
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#ifndef _HMAC_H
7#define _HMAC_H
8
9#include "CryptoConstants.h"
10#include "Define.h"
11#include "Errors.h"
12#include <array>
13#include <string>
14#include <string_view>
15#include <openssl/hmac.h>
16#include <openssl/evp.h>
17
18class BigNumber;
19
20namespace SkyFire::Impl
21{
22 struct HMACImpl
23 {
24 typedef EVP_MD const* (*HashCreator)();
25
26 static EVP_MAC* MakeMAC() { return EVP_MAC_fetch(NULL, "HMAC", NULL); }
27 static EVP_MAC_CTX* MakeCTX(EVP_MAC* mac) { return EVP_MAC_CTX_new(mac); }
28 static void DestroyCTX(EVP_MAC_CTX* ctx) { EVP_MAC_CTX_free(ctx); }
29 static void DestroyMAC(EVP_MAC* mac) { EVP_MAC_free(mac); }
30 };
31
32 template <HMACImpl::HashCreator HashCreator, size_t DigestLength>
34 {
35 public:
36 static constexpr size_t DIGEST_LENGTH = DigestLength;
37 using Digest = std::array<uint8, DIGEST_LENGTH>;
38
39 template <typename Container>
40 static Digest GetDigestOf(Container const& seed, uint8 const* data, size_t len)
41 {
42 GenericHMAC hash(seed);
43 hash.UpdateData(data, len);
44 hash.Finalize();
45 return hash.GetDigest();
46 }
47
48 template <typename Container, typename... Ts>
49 static auto GetDigestOf(Container const& seed, Ts&&... pack) -> std::enable_if_t<!(std::is_integral_v<std::decay_t<Ts>> || ...), Digest>
50 {
51 GenericHMAC hash(seed);
52 (hash.UpdateData(std::forward<Ts>(pack)), ...);
53 hash.Finalize();
54 return hash.GetDigest();
55 }
56
57 GenericHMAC(uint8 const* seed, size_t len) : _mac(HMACImpl::MakeMAC()), _ctx(HMACImpl::MakeCTX(_mac))
58 {
59 int result = EVP_MAC_init(_ctx, seed, len, _params);
60 ASSERT(result == 1);
61 }
62 template <typename Container>
63 GenericHMAC(Container const& container) : GenericHMAC(std::data(container), std::size(container)) {}
64
66 {
67 if (!_ctx)
68 return;
71 _ctx = NULL;
72 _mac = NULL;
73 }
74
75 void UpdateData(uint8 const* data, size_t len)
76 {
77 int result = EVP_MAC_update(_ctx, data, len);
78 ASSERT(result == 1);
79 }
80 void UpdateData(std::string_view str) { UpdateData(reinterpret_cast<uint8 const*>(str.data()), str.size()); }
81 void UpdateData(std::string const& str) { UpdateData(std::string_view(str)); } /* explicit overload to avoid using the container template */
82 void UpdateData(char const* str) { UpdateData(std::string_view(str)); } /* explicit overload to avoid using the container template */
83 template <typename Container>
84 void UpdateData(Container const& c) { UpdateData(std::data(c), std::size(c)); }
85
86 void Finalize()
87 {
88 size_t length = 0;
89 int result = EVP_MAC_final(_ctx, _digest.data(), &length, sizeof(_digest));
90 ASSERT(result == 1);
91 ASSERT(length == DIGEST_LENGTH);
94 _ctx = NULL;
95 _mac = NULL;
96 }
97
98 Digest const& GetDigest() const { return _digest; }
99 private:
100 EVP_MAC* _mac;
101 EVP_MAC_CTX* _ctx;
103 OSSL_PARAM _params[2] = { OSSL_PARAM_construct_utf8_string("digest", const_cast<char*>("SHA1"), 0), OSSL_PARAM_construct_end() };
104 };
105}
106
107namespace SkyFire::Crypto
108{
111}
112#endif
std::uint8_t uint8
Definition Define.h:79
#define ASSERT
Definition Errors.h:29
static Digest GetDigestOf(Container const &seed, uint8 const *data, size_t len)
Definition HMAC.h:40
void UpdateData(std::string_view str)
Definition HMAC.h:80
void UpdateData(uint8 const *data, size_t len)
Definition HMAC.h:75
GenericHMAC(Container const &container)
Definition HMAC.h:63
void UpdateData(char const *str)
Definition HMAC.h:82
Digest const & GetDigest() const
Definition HMAC.h:98
void UpdateData(std::string const &str)
Definition HMAC.h:81
GenericHMAC(uint8 const *seed, size_t len)
Definition HMAC.h:57
std::array< uint8, DIGEST_LENGTH > Digest
Definition HMAC.h:37
void UpdateData(Container const &c)
Definition HMAC.h:84
static auto GetDigestOf(Container const &seed, Ts &&... pack) -> std::enable_if_t<!(std::is_integral_v< std::decay_t< Ts > >||...), Digest >
Definition HMAC.h:49
SkyFire::Impl::GenericHMAC< EVP_sha256, Constants::SHA256_DIGEST_LENGTH_BYTES > HMAC_SHA256
Definition HMAC.h:110
SkyFire::Impl::GenericHMAC< EVP_sha1, Constants::SHA1_DIGEST_LENGTH_BYTES > HMAC_SHA1
Definition HMAC.h:109
static void DestroyMAC(EVP_MAC *mac)
Definition HMAC.h:29
static EVP_MAC * MakeMAC()
Definition HMAC.h:26
static void DestroyCTX(EVP_MAC_CTX *ctx)
Definition HMAC.h:28
static EVP_MAC_CTX * MakeCTX(EVP_MAC *mac)
Definition HMAC.h:27