forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys.hpp
477 lines (432 loc) · 12.1 KB
/
keys.hpp
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
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
*/
#pragma once
#include "td/utils/int_types.h"
#include "td/utils/buffer.h"
#include "auto/tl/ton_api.h"
#include "td/utils/UInt.h"
#include "td/utils/Variant.h"
#include "td/actor/actor.h"
#include "crypto/common/bitstring.h"
#include "crypto/Ed25519.h"
#include "common/errorcode.h"
namespace ton {
class Encryptor;
class EncryptorAsync;
class Decryptor;
class DecryptorAsync;
class PublicKeyHash {
public:
explicit PublicKeyHash(td::Bits256 value) : value_(value) {
}
explicit PublicKeyHash(const tl_object_ptr<ton_api::PublicKey> &value);
PublicKeyHash() {
}
static PublicKeyHash zero() {
return PublicKeyHash{td::Bits256::zero()};
}
explicit PublicKeyHash(td::Slice data) {
CHECK(data.size() == 32);
value_.as_slice().copy_from(data);
}
td::UInt256 uint256_value() const {
td::UInt256 x;
x.as_slice().copy_from(value_.as_slice());
return x;
}
td::Bits256 bits256_value() const {
return value_;
}
auto tl() const {
return value_;
}
bool operator<(const PublicKeyHash &with) const {
return value_ < with.value_;
}
bool operator==(const PublicKeyHash &with) const {
return value_ == with.value_;
}
bool operator!=(const PublicKeyHash &with) const {
return value_ != with.value_;
}
td::Slice as_slice() const {
return td::as_slice(value_);
}
bool is_zero() const {
return value_.is_zero();
}
private:
td::Bits256 value_;
};
namespace pubkeys {
class Ed25519 {
private:
td::Bits256 data_;
public:
Ed25519(const ton_api::pub_ed25519 &obj) {
data_ = obj.key_;
}
Ed25519(td::Bits256 id) : data_(id) {
}
Ed25519() {
}
Ed25519(td::Ed25519::PublicKey pk);
td::Ed25519::PublicKey export_key() {
return td::Ed25519::PublicKey{td::SecureString(data_.as_slice())};
}
auto raw() const {
return data_;
}
td::uint32 serialized_size() const {
return 36;
}
tl_object_ptr<ton_api::pub_ed25519> tl() const {
return create_tl_object<ton_api::pub_ed25519>(data_);
}
bool operator==(const Ed25519 &with) const {
return data_ == with.data_;
}
bool operator!=(const Ed25519 &with) const {
return data_ != with.data_;
}
};
class AES {
private:
td::Bits256 data_;
public:
~AES() {
data_.set_zero_s();
}
AES(const ton_api::pub_aes &obj) {
data_ = obj.key_;
}
AES(td::Slice data) {
CHECK(data.size() == 32);
data_.as_slice().copy_from(data);
}
AES(td::Bits256 data) : data_(data) {
}
td::uint32 serialized_size() const {
return 36;
}
tl_object_ptr<ton_api::pub_aes> tl() const {
return create_tl_object<ton_api::pub_aes>(data_);
}
bool operator==(const AES &with) const {
return data_ == with.data_;
}
bool operator!=(const AES &with) const {
return data_ != with.data_;
}
};
class Unenc {
private:
td::SharedSlice data_;
public:
Unenc(const ton_api::pub_unenc &obj) {
data_ = td::SharedSlice{obj.data_.as_slice()};
}
Unenc(const Unenc &obj) {
data_ = obj.data_.clone();
}
explicit Unenc(td::BufferSlice data) : data_(td::SharedSlice{data.as_slice()}) {
}
explicit Unenc(td::Slice data) : data_(td::SharedSlice{data}) {
}
explicit Unenc(td::SharedSlice data) : data_(std::move(data)) {
}
td::uint32 serialized_size() const {
return static_cast<td::uint32>(data_.size()) + 8;
}
tl_object_ptr<ton_api::pub_unenc> tl() const {
return create_tl_object<ton_api::pub_unenc>(data_.clone_as_buffer_slice());
}
bool operator==(const Unenc &with) const {
return data_.as_slice() == with.data_.as_slice();
}
bool operator!=(const Unenc &with) const {
return data_.as_slice() != with.data_.as_slice();
}
}; // namespace pubkeys
class Overlay {
private:
td::SharedSlice data_;
public:
Overlay(const ton_api::pub_overlay &obj) {
data_ = td::SharedSlice{obj.name_.as_slice()};
}
Overlay(const Overlay &obj) {
data_ = obj.data_.clone();
}
explicit Overlay(td::BufferSlice data) : data_(td::SharedSlice{data.as_slice()}) {
}
explicit Overlay(td::Slice data) : data_(td::SharedSlice{data}) {
}
explicit Overlay(td::SharedSlice data) : data_(std::move(data)) {
}
td::uint32 serialized_size() const {
return static_cast<td::uint32>(data_.size()) + 8;
}
tl_object_ptr<ton_api::pub_overlay> tl() const {
return create_tl_object<ton_api::pub_overlay>(data_.clone_as_buffer_slice());
}
bool operator==(const Overlay &with) const {
return data_.as_slice() == with.data_.as_slice();
}
bool operator!=(const Overlay &with) const {
return data_.as_slice() != with.data_.as_slice();
}
};
} // namespace pubkeys
class PublicKey {
private:
class Empty {
public:
tl_object_ptr<ton_api::PublicKey> tl() const {
UNREACHABLE();
}
td::uint32 serialized_size() const {
UNREACHABLE();
}
bool operator==(const Empty &with) const {
return false;
}
bool operator!=(const Empty &with) const {
return true;
}
};
td::Variant<Empty, pubkeys::Ed25519, pubkeys::AES, pubkeys::Unenc, pubkeys::Overlay> pub_key_{Empty{}};
public:
explicit PublicKey(const tl_object_ptr<ton_api::PublicKey> &id);
PublicKey() {
}
PublicKey(pubkeys::Ed25519 pub) : pub_key_(std::move(pub)) {
}
PublicKey(pubkeys::AES pub) : pub_key_(std::move(pub)) {
}
PublicKey(pubkeys::Unenc pub) : pub_key_(std::move(pub)) {
}
PublicKey(pubkeys::Overlay pub) : pub_key_(std::move(pub)) {
}
bool empty() const;
PublicKeyHash compute_short_id() const;
td::uint32 serialized_size() const;
tl_object_ptr<ton_api::PublicKey> tl() const;
td::BufferSlice export_as_slice() const;
static td::Result<PublicKey> import(td::Slice s);
pubkeys::Ed25519 ed25519_value() const {
CHECK(pub_key_.get_offset() == pub_key_.offset<pubkeys::Ed25519>());
return pub_key_.get<pubkeys::Ed25519>();
}
td::Result<std::unique_ptr<Encryptor>> create_encryptor() const;
td::Result<td::actor::ActorOwn<EncryptorAsync>> create_encryptor_async() const;
bool operator==(const PublicKey &with) const {
return pub_key_ == with.pub_key_;
}
bool operator!=(const PublicKey &with) const {
return !(pub_key_ == with.pub_key_);
}
};
namespace privkeys {
class Ed25519 {
private:
td::Bits256 data_;
public:
~Ed25519() {
data_.set_zero_s();
}
Ed25519(const ton_api::pk_ed25519 &obj) {
data_ = obj.key_;
}
Ed25519(td::Bits256 id) : data_(id) {
id.set_zero_s();
}
Ed25519(td::Slice data) {
CHECK(data.size() == 32);
data_.as_slice().copy_from(data);
}
Ed25519() {
}
Ed25519(td::Ed25519::PrivateKey pk);
td::Ed25519::PrivateKey export_key() {
return td::Ed25519::PrivateKey{td::SecureString(data_.as_slice())};
}
td::SecureString export_as_slice() const {
td::SecureString s{36};
auto id = ton_api::pk_ed25519::ID;
s.as_mutable_slice().copy_from(td::Slice{reinterpret_cast<const td::uint8 *>(&id), 4});
s.as_mutable_slice().remove_prefix(4).copy_from(data_.as_slice());
return s;
}
bool exportable() const {
return true;
}
static td::Result<Ed25519> import(td::Slice slice) {
if (slice.size() != 32) {
return td::Status::Error(ErrorCode::error, "bad length");
}
return Ed25519{slice};
}
tl_object_ptr<ton_api::pk_ed25519> tl() const {
return create_tl_object<ton_api::pk_ed25519>(data_);
}
tl_object_ptr<ton_api::PublicKey> pub_tl() const;
pubkeys::Ed25519 pub() const;
static Ed25519 random();
};
class AES {
private:
td::Bits256 data_;
public:
~AES() {
data_.set_zero_s();
}
AES(const ton_api::pk_aes &obj) {
data_ = obj.key_;
}
AES(td::Slice data) {
CHECK(data.size() == 32);
data_.as_slice().copy_from(data);
}
td::SecureString export_as_slice() const {
td::SecureString s{40};
auto id = ton_api::pk_aes::ID;
s.as_mutable_slice().copy_from(td::Slice{reinterpret_cast<const td::uint8 *>(&id), 4});
s.as_mutable_slice().remove_prefix(4).copy_from(data_.as_slice());
return s;
}
bool exportable() const {
return true;
}
static td::Result<AES> import(td::Slice slice) {
if (slice.size() != 32) {
return td::Status::Error(ErrorCode::error, "bad length");
}
return AES{slice};
}
tl_object_ptr<ton_api::pk_aes> tl() const {
return create_tl_object<ton_api::pk_aes>(data_);
}
tl_object_ptr<ton_api::PublicKey> pub_tl() const {
return create_tl_object<ton_api::pub_aes>(data_);
}
pubkeys::AES pub() const {
return pubkeys::AES{data_};
}
};
class Unenc {
private:
td::SharedSlice data_;
public:
Unenc(const ton_api::pk_unenc &obj) {
data_ = td::SharedSlice{obj.data_.as_slice()};
}
Unenc(const Unenc &obj) {
data_ = obj.data_.clone();
}
explicit Unenc(td::BufferSlice data) : data_(td::SharedSlice{data.as_slice()}) {
}
explicit Unenc(td::Slice data) : data_(td::SharedSlice{data}) {
}
explicit Unenc(td::SharedSlice data) : data_(std::move(data)) {
}
td::SecureString export_as_slice() const {
UNREACHABLE();
}
bool exportable() const {
return false;
}
tl_object_ptr<ton_api::pk_unenc> tl() const {
return create_tl_object<ton_api::pk_unenc>(data_.clone_as_buffer_slice());
}
tl_object_ptr<ton_api::PublicKey> pub_tl() const {
return create_tl_object<ton_api::pub_unenc>(data_.clone_as_buffer_slice());
}
pubkeys::Unenc pub() const {
return pubkeys::Unenc{data_.clone()};
}
};
class Overlay {
private:
td::SharedSlice data_;
public:
Overlay(const ton_api::pk_overlay &obj) {
data_ = td::SharedSlice{obj.name_.as_slice()};
}
Overlay(const Overlay &obj) {
data_ = obj.data_.clone();
}
explicit Overlay(td::BufferSlice data) : data_(td::SharedSlice{data.as_slice()}) {
}
explicit Overlay(td::Slice data) : data_(td::SharedSlice{data}) {
}
explicit Overlay(td::SharedSlice data) : data_(std::move(data)) {
}
td::SecureString export_as_slice() const {
UNREACHABLE();
}
bool exportable() const {
return false;
}
tl_object_ptr<ton_api::pk_overlay> tl() const {
return create_tl_object<ton_api::pk_overlay>(data_.clone_as_buffer_slice());
}
tl_object_ptr<ton_api::PublicKey> pub_tl() const {
return create_tl_object<ton_api::pub_overlay>(data_.clone_as_buffer_slice());
}
pubkeys::Overlay pub() const {
return pubkeys::Overlay{data_.clone()};
}
};
} // namespace privkeys
class PrivateKey {
private:
class Empty {
public:
td::SecureString export_as_slice() const {
UNREACHABLE();
}
bool exportable() const {
return false;
}
tl_object_ptr<ton_api::PrivateKey> tl() const {
UNREACHABLE();
}
tl_object_ptr<ton_api::PublicKey> pub_tl() const {
UNREACHABLE();
}
PublicKey pub() const {
UNREACHABLE();
}
};
td::Variant<Empty, privkeys::Ed25519, privkeys::AES, privkeys::Unenc, privkeys::Overlay> priv_key_{Empty{}};
public:
explicit PrivateKey(const tl_object_ptr<ton_api::PrivateKey> &pk);
template <class T>
PrivateKey(T key) : priv_key_(std::move(key)) {
}
PrivateKey() {
}
bool empty() const;
PublicKey compute_public_key() const;
PublicKeyHash compute_short_id() const;
td::SecureString export_as_slice() const;
static td::Result<PrivateKey> import(td::Slice s);
bool exportable() const;
tl_object_ptr<ton_api::PrivateKey> tl() const;
td::Result<std::unique_ptr<Decryptor>> create_decryptor() const;
td::Result<td::actor::ActorOwn<DecryptorAsync>> create_decryptor_async() const;
};
} // namespace ton