forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys.cpp
237 lines (198 loc) · 7.72 KB
/
keys.cpp
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2017-2020 Telegram Systems LLP
*/
#include "keys.hpp"
#include "auto/tl/ton_api.hpp"
#include "td/utils/overloaded.h"
#include "tl-utils/tl-utils.hpp"
#include "encryptor.h"
#include "encryptor.hpp"
#include "crypto/Ed25519.h"
namespace ton {
PublicKeyHash::PublicKeyHash(const tl_object_ptr<ton_api::PublicKey> &value) {
value_ = get_tl_object_sha_bits256(value);
}
PublicKey::PublicKey(const tl_object_ptr<ton_api::PublicKey> &id) {
ton_api::downcast_call(
*const_cast<ton_api::PublicKey *>(id.get()),
td::overloaded([&](const ton_api::pub_ed25519 &obj) { this->pub_key_ = pubkeys::Ed25519{obj}; },
[&](const ton_api::pub_aes &obj) { this->pub_key_ = pubkeys::AES{obj}; },
[&](const ton_api::pub_unenc &obj) { this->pub_key_ = pubkeys::Unenc{obj}; },
[&](const ton_api::pub_overlay &obj) { this->pub_key_ = pubkeys::Overlay{obj}; }));
}
PublicKeyHash PublicKey::compute_short_id() const {
return PublicKeyHash{get_tl_object_sha_bits256(tl())};
}
td::uint32 PublicKey::serialized_size() const {
td::uint32 res = 0;
pub_key_.visit([&](auto &obj) { res = obj.serialized_size(); });
return res;
}
tl_object_ptr<ton_api::PublicKey> PublicKey::tl() const {
tl_object_ptr<ton_api::PublicKey> res;
pub_key_.visit([&](auto &obj) { res = obj.tl(); });
return res;
}
td::BufferSlice PublicKey::export_as_slice() const {
return serialize_tl_object(tl(), true);
}
td::Result<PublicKey> PublicKey::import(td::Slice s) {
TRY_RESULT(x, fetch_tl_object<ton_api::PublicKey>(s, true));
return PublicKey{x};
}
td::Result<std::unique_ptr<Encryptor>> pubkeys::Ed25519::create_encryptor() const {
return std::make_unique<EncryptorEd25519>(data_);
}
td::Result<std::unique_ptr<Encryptor>> pubkeys::AES::create_encryptor() const {
return std::make_unique<EncryptorAES>(data_);
}
td::Result<std::unique_ptr<Encryptor>> pubkeys::Unenc::create_encryptor() const {
return std::make_unique<EncryptorNone>();
}
td::Result<std::unique_ptr<Encryptor>> pubkeys::Overlay::create_encryptor() const {
return std::make_unique<EncryptorOverlay>();
}
td::Result<std::unique_ptr<Encryptor>> PublicKey::create_encryptor() const {
td::Result<std::unique_ptr<Encryptor>> res;
pub_key_.visit([&](auto &obj) { res = obj.create_encryptor(); });
return res;
}
td::Result<td::actor::ActorOwn<EncryptorAsync>> PublicKey::create_encryptor_async() const {
TRY_RESULT(encryptor, create_encryptor());
return td::actor::create_actor<EncryptorAsync>("encryptor", std::move(encryptor));
}
bool PublicKey::empty() const {
return pub_key_.get_offset() == pub_key_.offset<Empty>();
}
tl_object_ptr<ton_api::PublicKey> privkeys::Ed25519::pub_tl() const {
td::Ed25519::PrivateKey pkey(td::SecureString(as_slice(data_)));
auto r_public_key = pkey.get_public_key();
if (r_public_key.is_error()) {
return nullptr;
} else {
auto public_key = r_public_key.ok().as_octet_string();
td::Bits256 X;
as_slice(X).copy_from(public_key);
return create_tl_object<ton_api::pub_ed25519>(X);
}
}
pubkeys::Ed25519 privkeys::Ed25519::pub() const {
td::Ed25519::PrivateKey pkey(td::SecureString(as_slice(data_)));
return pubkeys::Ed25519{pkey.get_public_key().move_as_ok()};
}
privkeys::Ed25519 privkeys::Ed25519::random() {
while (true) {
auto key = td::Ed25519::generate_private_key();
if (key.is_error()) {
LOG(WARNING) << "failed to generate private key: " << key.move_as_error();
}
return Ed25519{key.move_as_ok()};
}
}
privkeys::Ed25519::Ed25519(td::Ed25519::PrivateKey key) {
auto s = key.as_octet_string();
CHECK(s.length() == 32);
data_.as_slice().copy_from(td::Slice(s));
}
td::Result<std::unique_ptr<Decryptor>> privkeys::Ed25519::create_decryptor() const {
return std::make_unique<DecryptorEd25519>(data_);
}
td::Result<std::unique_ptr<Decryptor>> privkeys::AES::create_decryptor() const {
return std::make_unique<DecryptorAES>(data_);
}
td::Result<std::unique_ptr<Decryptor>> privkeys::Unenc::create_decryptor() const {
return std::make_unique<DecryptorNone>();
}
td::Result<std::unique_ptr<Decryptor>> privkeys::Overlay::create_decryptor() const {
return std::make_unique<DecryptorFail>();
}
pubkeys::Ed25519::Ed25519(td::Ed25519::PublicKey key) {
auto s = key.as_octet_string();
CHECK(s.length() == 32);
data_.as_slice().copy_from(td::Slice(s));
}
PrivateKey::PrivateKey(const tl_object_ptr<ton_api::PrivateKey> &id) {
ton_api::downcast_call(
*const_cast<ton_api::PrivateKey *>(id.get()),
td::overloaded([&](const ton_api::pk_ed25519 &obj) { this->priv_key_ = privkeys::Ed25519{obj}; },
[&](const ton_api::pk_aes &obj) { this->priv_key_ = privkeys::AES{obj}; },
[&](const ton_api::pk_unenc &obj) { this->priv_key_ = privkeys::Unenc{obj}; },
[&](const ton_api::pk_overlay &obj) { this->priv_key_ = privkeys::Overlay{obj}; }));
}
bool PrivateKey::empty() const {
return priv_key_.get_offset() == priv_key_.offset<Empty>();
}
PublicKey PrivateKey::compute_public_key() const {
PublicKey res;
priv_key_.visit([&](auto &obj) { res = obj.pub(); });
return res;
}
PublicKeyHash PrivateKey::compute_short_id() const {
tl_object_ptr<ton_api::PublicKey> res;
priv_key_.visit([&](auto &obj) { res = obj.pub_tl(); });
return PublicKeyHash{res};
}
td::SecureString PrivateKey::export_as_slice() const {
td::SecureString res;
priv_key_.visit([&](auto &obj) { res = obj.export_as_slice(); });
return res;
}
bool PrivateKey::exportable() const {
bool res;
priv_key_.visit([&](auto &obj) { res = obj.exportable(); });
return res;
}
td::Result<PrivateKey> PrivateKey::import(td::Slice s) {
if (s.size() < 4) {
return td::Status::Error(ErrorCode::protoviolation, "too short key");
}
td::int32 id;
td::MutableSlice{reinterpret_cast<char *>(&id), 4}.copy_from(s.copy().truncate(4));
s.remove_prefix(4);
switch (id) {
case ton_api::pk_ed25519::ID: {
auto R = privkeys::Ed25519::import(s);
if (R.is_error()) {
return R.move_as_error();
} else {
return R.move_as_ok();
}
} break;
case ton_api::pk_aes::ID: {
auto R = privkeys::AES::import(s);
if (R.is_error()) {
return R.move_as_error();
} else {
return R.move_as_ok();
}
} break;
default:
return td::Status::Error(ErrorCode::protoviolation, PSTRING() << "unknown magic " << id);
}
}
tl_object_ptr<ton_api::PrivateKey> PrivateKey::tl() const {
tl_object_ptr<ton_api::PrivateKey> res;
priv_key_.visit([&](auto &obj) { res = obj.tl(); });
return res;
}
td::Result<std::unique_ptr<Decryptor>> PrivateKey::create_decryptor() const {
td::Result<std::unique_ptr<Decryptor>> res;
priv_key_.visit([&](auto &obj) { res = obj.create_decryptor(); });
return res;
}
td::Result<td::actor::ActorOwn<DecryptorAsync>> PrivateKey::create_decryptor_async() const {
TRY_RESULT(decryptor, create_decryptor());
return td::actor::create_actor<DecryptorAsync>("decryptor", std::move(decryptor));
}
} // namespace ton