TrinityCore
RSA.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITYCORE_RSA_H
19#define TRINITYCORE_RSA_H
20
21#include "Define.h"
22#include <openssl/evp.h>
23#include <array>
24#include <memory>
25#include <string>
26#include <vector>
27
28namespace Trinity
29{
30namespace Crypto
31{
33{
34public:
36 {
37 public:
39 {
40 void operator()(EVP_MD* md) const;
41 };
42
43 virtual ~DigestGenerator() = default;
44 virtual std::unique_ptr<EVP_MD, EVP_MD_Deleter> GetGenerator() const = 0;
45
46#if OPENSSL_VERSION_NUMBER >= 0x30000000L
47 virtual OSSL_LIB_CTX* GetLib() const = 0;
48 virtual std::unique_ptr<OSSL_PARAM[]> GetParams() const = 0;
49#else
50 virtual void PostInitCustomizeContext(EVP_MD_CTX* ctx) = 0;
51#endif
52 };
53
55 {
56 public:
57 std::unique_ptr<EVP_MD, EVP_MD_Deleter> GetGenerator() const override;
58
59#if OPENSSL_VERSION_NUMBER >= 0x30000000L
60 OSSL_LIB_CTX* GetLib() const override;
61 std::unique_ptr<OSSL_PARAM[]> GetParams() const override;
62#else
63 void PostInitCustomizeContext(EVP_MD_CTX* ctx) override;
64#endif
65 };
66
68 {
69 public:
70 explicit HMAC_SHA256(uint8 const* key, size_t keyLength) : _key(key), _keyLength(keyLength) { }
71
72 std::unique_ptr<EVP_MD, EVP_MD_Deleter> GetGenerator() const override;
73
74#if OPENSSL_VERSION_NUMBER >= 0x30000000L
75 OSSL_LIB_CTX* GetLib() const override;
76 std::unique_ptr<OSSL_PARAM[]> GetParams() const override;
77#else
78 void PostInitCustomizeContext(EVP_MD_CTX* ctx) override;
79#endif
80
81 private:
82 uint8 const* _key;
83 size_t _keyLength;
84 };
85
87 RsaSignature(RsaSignature const& other);
88 RsaSignature(RsaSignature&& other) noexcept;
90
91 RsaSignature& operator=(RsaSignature const& right);
92 RsaSignature& operator=(RsaSignature&& right) noexcept;
93
94 bool LoadKeyFromFile(std::string const& fileName);
95
96 bool LoadKeyFromString(std::string const& keyPem);
97
98 template <std::size_t N>
99 bool Sign(std::array<uint8, N> const& message, DigestGenerator& generator, std::vector<uint8>& output)
100 {
101 return this->Sign(message.data(), message.size(), generator, output);
102 }
103
104 bool Sign(uint8 const* message, std::size_t messageLength, DigestGenerator& generator, std::vector<uint8>& output);
105
106private:
107 EVP_MD_CTX* _ctx = nullptr;
108 EVP_PKEY* _key = nullptr;
109};
110}
111}
112
113#endif // TRINITYCORE_RSA_H
uint8_t uint8
Definition: Define.h:144
#define TC_COMMON_API
Definition: Define.h:99
virtual std::unique_ptr< EVP_MD, EVP_MD_Deleter > GetGenerator() const =0
virtual void PostInitCustomizeContext(EVP_MD_CTX *ctx)=0
HMAC_SHA256(uint8 const *key, size_t keyLength)
Definition: RSA.h:70
bool Sign(std::array< uint8, N > const &message, DigestGenerator &generator, std::vector< uint8 > &output)
Definition: RSA.h:99