1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Ed25519.h"
#include "CryptoHash.h"
#include "Memory.h"
#include <ed25519/ed25519.h>
#include <openssl/pem.h>
#include <memory>
namespace Trinity::Crypto
{
Ed25519::Ed25519() = default;
Ed25519::Ed25519(Ed25519 const& right)
{
*this = right;
}
Ed25519::Ed25519(Ed25519&& right) noexcept
{
*this = std::move(right);
}
Ed25519::~Ed25519()
{
EVP_PKEY_free(_key);
}
Ed25519& Ed25519::operator=(Ed25519 const& right)
{
if (this == &right)
return *this;
_key = right._key; // EVP_PKEY uses reference counting internally, just copy the pointer
EVP_PKEY_up_ref(_key); // Bump reference count for PKEY, as every instance of this class holds two references to PKEY and destructor decrements it twice
return *this;
}
Ed25519& Ed25519::operator=(Ed25519&& right) noexcept
{
if (this == &right)
return *this;
_key = std::exchange(right._key, EVP_PKEY_new());
return *this;
}
bool Ed25519::LoadFromFile(std::string const& fileName)
{
if (_key)
{
EVP_PKEY_free(_key);
_key = nullptr;
}
auto keyBIO = make_unique_ptr_with_deleter(BIO_new_file(fileName.c_str(), "r"), &BIO_free);
if (!keyBIO)
return false;
_key = EVP_PKEY_new();
if (!PEM_read_bio_PrivateKey(keyBIO.get(), &_key, nullptr, nullptr))
return false;
return true;
}
bool Ed25519::LoadFromString(std::string const& keyPem)
{
if (_key)
{
EVP_PKEY_free(_key);
_key = nullptr;
}
auto keyBIO = make_unique_ptr_with_deleter(BIO_new_mem_buf(
const_cast<char*>(keyPem.c_str()) /*api hack - this function assumes memory is readonly but lacks const modifier*/,
keyPem.length() + 1), &BIO_free);
if (!keyBIO)
return false;
_key = EVP_PKEY_new();
if (!PEM_read_bio_PrivateKey(keyBIO.get(), &_key, nullptr, nullptr))
return false;
return true;
}
bool Ed25519::LoadFromByteArray(std::array<uint8, 32> const& keyBytes)
{
if (_key)
{
EVP_PKEY_free(_key);
_key = nullptr;
}
_key = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, nullptr, keyBytes.data(), keyBytes.size());
if (!_key)
return false;
return true;
}
bool Ed25519::Sign(uint8 const* message, std::size_t messageLength, std::vector<uint8>& output)
{
constexpr size_t KeySize = 32;
uint8 publicKey[KeySize] = {};
std::size_t keyLength = KeySize;
EVP_PKEY_get_raw_public_key(_key, publicKey, &keyLength);
uint8 privateKey[KeySize] = {};
keyLength = KeySize;
EVP_PKEY_get_raw_private_key(_key, privateKey, &keyLength);
output.resize(64);
int result = ED25519_sign(output.data(), message, messageLength, publicKey, privateKey);
return result != 0;
}
bool Ed25519::SignWithContext(uint8 const* message, std::size_t messageLength, std::vector<uint8> const& context, std::vector<uint8>& output)
{
constexpr size_t KeySize = 32;
uint8 publicKey[KeySize] = {};
std::size_t keyLength = KeySize;
EVP_PKEY_get_raw_public_key(_key, publicKey, &keyLength);
uint8 privateKey[KeySize] = {};
keyLength = KeySize;
EVP_PKEY_get_raw_private_key(_key, privateKey, &keyLength);
output.resize(64);
int result = ED25519_sign_ctx(output.data(), message, messageLength, publicKey, privateKey, context.data(), context.size());
return result != 0;
}
}
|