TrinityCore
Loading...
Searching...
No Matches
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 virtual OSSL_LIB_CTX* GetLib() const = 0;
47 virtual std::unique_ptr<OSSL_PARAM[]> GetParams() const = 0;
48 };
49
51 {
52 public:
53 std::unique_ptr<EVP_MD, EVP_MD_Deleter> GetGenerator() const override;
54
55 OSSL_LIB_CTX* GetLib() const override;
56 std::unique_ptr<OSSL_PARAM[]> GetParams() const override;
57 };
58
60 {
61 public:
62 explicit HMAC_SHA256(uint8 const* key, size_t keyLength) : _key(key), _keyLength(keyLength) { }
63
64 std::unique_ptr<EVP_MD, EVP_MD_Deleter> GetGenerator() const override;
65
66 OSSL_LIB_CTX* GetLib() const override;
67 std::unique_ptr<OSSL_PARAM[]> GetParams() const override;
68
69 private:
70 uint8 const* _key;
71 size_t _keyLength;
72 };
73
75 RsaSignature(RsaSignature const& other);
76 RsaSignature(RsaSignature&& other) noexcept;
78
79 RsaSignature& operator=(RsaSignature const& right);
80 RsaSignature& operator=(RsaSignature&& right) noexcept;
81
82 bool LoadKeyFromFile(std::string const& fileName);
83
84 bool LoadKeyFromString(std::string const& keyPem);
85
86 template <std::size_t N>
87 bool Sign(std::array<uint8, N> const& message, DigestGenerator& generator, std::vector<uint8>& output)
88 {
89 return this->Sign(message.data(), message.size(), generator, output);
90 }
91
92 bool Sign(uint8 const* message, std::size_t messageLength, DigestGenerator& generator, std::vector<uint8>& output);
93
94private:
95 EVP_MD_CTX* _ctx = nullptr;
96 EVP_PKEY* _key = nullptr;
97};
98}
99}
100
101#endif // TRINITYCORE_RSA_H
uint8_t uint8
Definition Define.h:156
#define TC_COMMON_API
Definition Define.h:99
virtual std::unique_ptr< OSSL_PARAM[]> GetParams() const =0
virtual std::unique_ptr< EVP_MD, EVP_MD_Deleter > GetGenerator() const =0
virtual OSSL_LIB_CTX * GetLib() const =0
HMAC_SHA256(uint8 const *key, size_t keyLength)
Definition RSA.h:62
bool Sign(std::array< uint8, N > const &message, DigestGenerator &generator, std::vector< uint8 > &output)
Definition RSA.h:87