1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-03-09 15:40:10 +00:00

Merge branch 'testnet' into accelerator

This commit is contained in:
SpyCheese 2024-08-15 15:42:33 +03:00
commit 3950b7eb9a
18 changed files with 238 additions and 116 deletions

View file

@ -29,28 +29,6 @@
namespace ton {
td::Result<std::unique_ptr<Encryptor>> Encryptor::create(const ton_api::PublicKey *id) {
td::Result<std::unique_ptr<Encryptor>> res;
ton_api::downcast_call(
*const_cast<ton_api::PublicKey *>(id),
td::overloaded([&](const ton_api::pub_unenc &obj) { res = std::make_unique<EncryptorNone>(); },
[&](const ton_api::pub_ed25519 &obj) { res = std::make_unique<EncryptorEd25519>(obj.key_); },
[&](const ton_api::pub_overlay &obj) { res = std::make_unique<EncryptorOverlay>(); },
[&](const ton_api::pub_aes &obj) { res = std::make_unique<EncryptorAES>(obj.key_); }));
return res;
}
td::Result<std::unique_ptr<Decryptor>> Decryptor::create(const ton_api::PrivateKey *id) {
td::Result<std::unique_ptr<Decryptor>> res;
ton_api::downcast_call(
*const_cast<ton_api::PrivateKey *>(id),
td::overloaded([&](const ton_api::pk_unenc &obj) { res = std::make_unique<DecryptorNone>(); },
[&](const ton_api::pk_ed25519 &obj) { res = std::make_unique<DecryptorEd25519>(obj.key_); },
[&](const ton_api::pk_overlay &obj) { res = std::make_unique<DecryptorFail>(); },
[&](const ton_api::pk_aes &obj) { res = std::make_unique<DecryptorAES>(obj.key_); }));
return res;
}
td::Result<td::BufferSlice> EncryptorEd25519::encrypt(td::Slice data) {
TRY_RESULT_PREFIX(pk, td::Ed25519::generate_private_key(), "failed to generate private key: ");
TRY_RESULT_PREFIX(pubkey, pk.get_public_key(), "failed to get public key from private: ");

View file

@ -31,7 +31,6 @@ class Encryptor {
virtual td::Result<td::BufferSlice> encrypt(td::Slice data) = 0;
virtual td::Status check_signature(td::Slice message, td::Slice signature) = 0;
virtual ~Encryptor() = default;
static td::Result<std::unique_ptr<Encryptor>> create(const ton_api::PublicKey *id);
};
class Decryptor {
@ -40,7 +39,6 @@ class Decryptor {
virtual td::Result<td::BufferSlice> sign(td::Slice data) = 0;
virtual std::vector<td::Result<td::BufferSlice>> sign_batch(std::vector<td::Slice> data);
virtual ~Decryptor() = default;
static td::Result<std::unique_ptr<Decryptor>> create(const ton_api::PrivateKey *id);
};
class EncryptorAsync : public td::actor::Actor {
@ -61,16 +59,6 @@ class EncryptorAsync : public td::actor::Actor {
void encrypt(td::BufferSlice data, td::Promise<td::BufferSlice> promise) {
promise.set_result(encryptor_->encrypt(data.as_slice()));
}
template <class T>
static td::Result<td::actor::ActorOwn<EncryptorAsync>> create(T &id) {
TRY_RESULT(d, Encryptor::create(id));
return td::actor::create_actor<EncryptorAsync>("encryptor", std::move(d));
}
template <class T>
static td::Result<td::actor::ActorOwn<EncryptorAsync>> create(T *id) {
TRY_RESULT(d, Encryptor::create(id));
return td::actor::create_actor<EncryptorAsync>("encryptor", std::move(d));
}
};
class DecryptorAsync : public td::actor::Actor {
@ -94,16 +82,6 @@ class DecryptorAsync : public td::actor::Actor {
}
return decryptor_->sign_batch(v);
}
template <class T>
static td::Result<td::actor::ActorOwn<DecryptorAsync>> create(T &id) {
TRY_RESULT(d, Decryptor::create(id));
return td::actor::create_actor<DecryptorAsync>("decryptor", std::move(d));
}
template <class T>
static td::Result<td::actor::ActorOwn<DecryptorAsync>> create(T *id) {
TRY_RESULT(d, Decryptor::create(id));
return td::actor::create_actor<DecryptorAsync>("decryptor", std::move(d));
}
};
} // namespace ton

View file

@ -83,7 +83,7 @@ class EncryptorEd25519 : public Encryptor {
td::Result<td::BufferSlice> encrypt(td::Slice data) override;
td::Status check_signature(td::Slice message, td::Slice signature) override;
EncryptorEd25519(td::Bits256 key) : pub_(td::SecureString(as_slice(key))) {
EncryptorEd25519(const td::Bits256& key) : pub_(td::SecureString(as_slice(key))) {
}
};
@ -94,7 +94,7 @@ class DecryptorEd25519 : public Decryptor {
public:
td::Result<td::BufferSlice> decrypt(td::Slice data) override;
td::Result<td::BufferSlice> sign(td::Slice data) override;
DecryptorEd25519(td::Bits256 key) : pk_(td::SecureString(as_slice(key))) {
DecryptorEd25519(const td::Bits256& key) : pk_(td::SecureString(as_slice(key))) {
}
};
@ -129,12 +129,15 @@ class EncryptorAES : public Encryptor {
td::Bits256 shared_secret_;
public:
~EncryptorAES() override {
shared_secret_.set_zero_s();
}
td::Result<td::BufferSlice> encrypt(td::Slice data) override;
td::Status check_signature(td::Slice message, td::Slice signature) override {
return td::Status::Error("can no sign channel messages");
}
EncryptorAES(td::Bits256 shared_secret) : shared_secret_(shared_secret) {
EncryptorAES(const td::Bits256& shared_secret) : shared_secret_(shared_secret) {
}
};
@ -143,11 +146,14 @@ class DecryptorAES : public Decryptor {
td::Bits256 shared_secret_;
public:
~DecryptorAES() override {
shared_secret_.set_zero_s();
}
td::Result<td::BufferSlice> decrypt(td::Slice data) override;
td::Result<td::BufferSlice> sign(td::Slice data) override {
return td::Status::Error("can no sign channel messages");
}
DecryptorAES(td::Bits256 shared_secret) : shared_secret_(shared_secret) {
DecryptorAES(const td::Bits256& shared_secret) : shared_secret_(shared_secret) {
}
};

View file

@ -21,6 +21,7 @@
#include "td/utils/overloaded.h"
#include "tl-utils/tl-utils.hpp"
#include "encryptor.h"
#include "encryptor.hpp"
#include "crypto/Ed25519.h"
namespace ton {
@ -63,12 +64,31 @@ td::Result<PublicKey> PublicKey::import(td::Slice s) {
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 {
return Encryptor::create(tl().get());
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 {
return EncryptorAsync::create(tl().get());
TRY_RESULT(encryptor, create_encryptor());
return td::actor::create_actor<EncryptorAsync>("encryptor", std::move(encryptor));
}
bool PublicKey::empty() const {
@ -109,6 +129,22 @@ privkeys::Ed25519::Ed25519(td::Ed25519::PrivateKey key) {
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);
@ -188,11 +224,14 @@ tl_object_ptr<ton_api::PrivateKey> PrivateKey::tl() const {
}
td::Result<std::unique_ptr<Decryptor>> PrivateKey::create_decryptor() const {
return Decryptor::create(tl().get());
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 {
return DecryptorAsync::create(tl().get());
TRY_RESULT(decryptor, create_decryptor());
return td::actor::create_actor<DecryptorAsync>("decryptor", std::move(decryptor));
}
} // namespace ton

View file

@ -110,6 +110,7 @@ class Ed25519 {
tl_object_ptr<ton_api::pub_ed25519> tl() const {
return create_tl_object<ton_api::pub_ed25519>(data_);
}
td::Result<std::unique_ptr<Encryptor>> create_encryptor() const;
bool operator==(const Ed25519 &with) const {
return data_ == with.data_;
}
@ -141,6 +142,7 @@ class AES {
tl_object_ptr<ton_api::pub_aes> tl() const {
return create_tl_object<ton_api::pub_aes>(data_);
}
td::Result<std::unique_ptr<Encryptor>> create_encryptor() const;
bool operator==(const AES &with) const {
return data_ == with.data_;
}
@ -172,6 +174,7 @@ class Unenc {
tl_object_ptr<ton_api::pub_unenc> tl() const {
return create_tl_object<ton_api::pub_unenc>(data_.clone_as_buffer_slice());
}
td::Result<std::unique_ptr<Encryptor>> create_encryptor() const;
bool operator==(const Unenc &with) const {
return data_.as_slice() == with.data_.as_slice();
}
@ -203,6 +206,7 @@ class Overlay {
tl_object_ptr<ton_api::pub_overlay> tl() const {
return create_tl_object<ton_api::pub_overlay>(data_.clone_as_buffer_slice());
}
td::Result<std::unique_ptr<Encryptor>> create_encryptor() const;
bool operator==(const Overlay &with) const {
return data_.as_slice() == with.data_.as_slice();
}
@ -223,6 +227,9 @@ class PublicKey {
td::uint32 serialized_size() const {
UNREACHABLE();
}
td::Result<std::unique_ptr<Encryptor>> create_encryptor() const {
UNREACHABLE();
}
bool operator==(const Empty &with) const {
return false;
}
@ -320,6 +327,7 @@ class Ed25519 {
}
tl_object_ptr<ton_api::PublicKey> pub_tl() const;
pubkeys::Ed25519 pub() const;
td::Result<std::unique_ptr<Decryptor>> create_decryptor() const;
static Ed25519 random();
};
@ -363,6 +371,7 @@ class AES {
pubkeys::AES pub() const {
return pubkeys::AES{data_};
}
td::Result<std::unique_ptr<Decryptor>> create_decryptor() const;
};
class Unenc {
@ -397,6 +406,7 @@ class Unenc {
pubkeys::Unenc pub() const {
return pubkeys::Unenc{data_.clone()};
}
td::Result<std::unique_ptr<Decryptor>> create_decryptor() const;
};
class Overlay {
@ -431,6 +441,7 @@ class Overlay {
pubkeys::Overlay pub() const {
return pubkeys::Overlay{data_.clone()};
}
td::Result<std::unique_ptr<Decryptor>> create_decryptor() const;
};
} // namespace privkeys
@ -454,6 +465,9 @@ class PrivateKey {
PublicKey pub() const {
UNREACHABLE();
}
td::Result<std::unique_ptr<Decryptor>> create_decryptor() const {
UNREACHABLE();
}
};
td::Variant<Empty, privkeys::Ed25519, privkeys::AES, privkeys::Unenc, privkeys::Overlay> priv_key_{Empty{}};