Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Commit

Permalink
Autocommit from upstream on Mon Jun 24 20:55:06 UTC 2019
Browse files Browse the repository at this point in the history
  • Loading branch information
poma committed Jun 24, 2019
1 parent 039709f commit 9f2397f
Show file tree
Hide file tree
Showing 113 changed files with 3,693 additions and 1,555 deletions.
1 change: 0 additions & 1 deletion adnl/adnl-ext-client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ class AdnlExtClientImpl : public AdnlExtClient {
}
}
void conn_ready(td::actor::ActorId<AdnlExtConnection> conn) {
LOG(ERROR) << "conn ready";
if (!conn_.empty() && conn_.get() == conn) {
callback_->on_ready();
}
Expand Down
3 changes: 0 additions & 3 deletions adnl/adnl-ext-connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class AdnlExtConnection : public td::actor::Actor, public td::ObserverBase {

void start_up() override {
self_ = actor_id(this);
LOG(INFO) << "Start";
// Subscribe for socket updates
// NB: Interface will be changed
td::actor::SchedulerContext::get()->get_poll().subscribe(buffered_fd_.get_poll_info().extract_pollable_fd(this),
Expand All @@ -98,7 +97,6 @@ class AdnlExtConnection : public td::actor::Actor, public td::ObserverBase {
}

void tear_down() override {
LOG(INFO) << "Close";
if (callback_) {
callback_->on_close(actor_id(this));
callback_ = nullptr;
Expand All @@ -123,7 +121,6 @@ class AdnlExtConnection : public td::actor::Actor, public td::ObserverBase {
void alarm() override {
alarm_timestamp() = fail_at_;
if (fail_at_.is_in_past()) {
LOG(INFO) << "Close because of timeout";
stop();
} else if (is_client_ && !ping_sent_) {
if (send_ping_at_.is_in_past()) {
Expand Down
21 changes: 14 additions & 7 deletions adnl/adnl-node-id.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ namespace ton {

class AdnlNodeIdShort {
public:
explicit AdnlNodeIdShort(PublicKeyHash &hash) : hash_(hash) {
explicit AdnlNodeIdShort(const PublicKeyHash &hash) : hash_(hash) {
}
explicit AdnlNodeIdShort(PublicKeyHash &&hash) : hash_(std::move(hash)) {
}
AdnlNodeIdShort() {
}
explicit AdnlNodeIdShort(td::Slice data) : hash_(data) {
}
explicit AdnlNodeIdShort(td::UInt256 value) : hash_(value) {
explicit AdnlNodeIdShort(td::Bits256 value) : hash_(value) {
}
explicit AdnlNodeIdShort(tl_object_ptr<ton_api::adnl_id_short> obj) : hash_(obj->id_) {
}

const auto &pubkey_hash() const {
Expand All @@ -31,8 +33,8 @@ class AdnlNodeIdShort {
bool operator<(const AdnlNodeIdShort &with) const {
return hash_ < with.hash_;
}
auto tl() const {
return hash_.tl();
tl_object_ptr<ton_api::adnl_id_short> tl() const {
return create_tl_object<ton_api::adnl_id_short>(hash_.tl());
}
auto as_slice() {
return hash_.as_slice();
Expand All @@ -58,12 +60,17 @@ class AdnlNodeIdShort {
};

class AdnlNodeIdFull {
private:
explicit AdnlNodeIdFull(const tl_object_ptr<ton_api::PublicKey> &pub) : pub_(pub) {
}

public:
explicit AdnlNodeIdFull(PublicKey &pub) : pub_(pub) {
explicit AdnlNodeIdFull(const PublicKey &pub) : pub_(pub) {
}
explicit AdnlNodeIdFull(PublicKey &&pub) : pub_(std::move(pub)) {
}
explicit AdnlNodeIdFull(const tl_object_ptr<ton_api::PublicKey> &pub) : pub_(pub) {
static td::Result<AdnlNodeIdFull> create(const tl_object_ptr<ton_api::PublicKey> &pub) {
return AdnlNodeIdFull{pub};
}
AdnlNodeIdFull() {
}
Expand Down Expand Up @@ -95,7 +102,7 @@ class AdnlNodeIdFull {
namespace td {

inline StringBuilder &operator<<(StringBuilder &stream, const ton::AdnlNodeIdShort &value) {
return stream << value.uint256_value();
return stream << value.bits256_value();
}

} // namespace td
4 changes: 2 additions & 2 deletions adnl/adnl-query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace ton {
void AdnlQuery::alarm() {
promise_.set_error(td::Status::Error(ErrorCode::timeout, "adnl query timeout"));
hangup();
stop();
}
void AdnlQuery::result(td::BufferSlice data) {
promise_.set_value(std::move(data));
alarm_timestamp() = td::Timestamp::never();
hangup();
stop();
}

AdnlQueryId AdnlQuery::random_query_id() {
Expand Down
2 changes: 1 addition & 1 deletion adnl/adnl-query.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace ton {

class AdnlPeerPair;

using AdnlQueryId = td::UInt256;
using AdnlQueryId = td::Bits256;

class AdnlQuery : public td::actor::Actor {
public:
Expand Down
8 changes: 5 additions & 3 deletions common/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
#include "common/status.h"
#include "keys/keys.hpp"

#include "crypto/common/bitstring.h"

namespace td {

template <size_t size>
StringBuilder &operator<<(StringBuilder &stream, const UInt<size> &x) {
template <unsigned size>
StringBuilder &operator<<(StringBuilder &stream, const td::BitArray<size> &x) {
return stream << td::base64_encode(as_slice(x));
}

inline StringBuilder &operator<<(StringBuilder &stream, const ton::PublicKeyHash &value) {
return stream << value.uint256_value();
return stream << value.bits256_value();
}

} // namespace td
9 changes: 9 additions & 0 deletions crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ set (TON_CRYPTO_SOURCE
openssl/residue.cpp
openssl/rand.cpp
vm/stack.cpp
vm/atom.cpp
vm/continuation.cpp
vm/dict.cpp
vm/dispatch.cpp
Expand Down Expand Up @@ -59,7 +60,9 @@ set (TON_CRYPTO_SOURCE
tl/tlblib.hpp

vm/arithops.h
vm/atom.h
vm/boc.h
vm/box.hpp
vm/cellops.h
vm/continuation.h
vm/contops.h
Expand Down Expand Up @@ -238,10 +241,16 @@ target_include_directories(ton_block PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SO
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/block> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>)
target_link_libraries(ton_block PUBLIC ton_crypto tdutils tdactor tl_api)

set(TURN_OFF_LSAN cd .)
if (TON_USE_ASAN AND NOT WIN32)
set(TURN_OFF_LSAN export LSAN_OPTIONS=detect_leaks=0)
endif()

if (NOT CMAKE_CROSSCOMPILING)
set(GenerateTlbCmd tlbc)
add_custom_target(tlb_generate_block
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/block
COMMAND ${TURN_OFF_LSAN}
COMMAND ${GenerateTlbCmd} -o block-auto -n block::gen -z block.tlb
COMMENT "Generate block tlb source files"
DEPENDS tlbc block/block.tlb
Expand Down
71 changes: 71 additions & 0 deletions crypto/Ed25519.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,48 @@ EVP_PKEY *X25519_key_to_PKEY(string key, bool is_private) {
return func(EVP_PKEY_ED25519, nullptr, Slice(key).ubegin(), key.size());
}

Result<string> X25519_pem_from_PKEY(EVP_PKEY *pkey, bool is_private, Slice password) {
BIO *mem_bio = BIO_new(BIO_s_mem());
SCOPE_EXIT {
BIO_vfree(mem_bio);
};
if (is_private) {
PEM_write_bio_PrivateKey(mem_bio, pkey, EVP_aes_256_cbc(), const_cast<unsigned char *>(password.ubegin()),
narrow_cast<int>(password.size()), nullptr, nullptr);
} else {
PEM_write_bio_PUBKEY(mem_bio, pkey);
}
char *data_ptr = nullptr;
auto data_size = BIO_get_mem_data(mem_bio, &data_ptr);
LOG(ERROR) << password;
return std::string(data_ptr, data_size);
}

static int password_cb(char *buf, int size, int rwflag, void *u) {
auto &password = *reinterpret_cast<Slice *>(u);
auto password_size = narrow_cast<int>(password.size());
if (size < password_size) {
return -1;
}
if (rwflag == 0) {
MutableSlice(buf, size).copy_from(password);
LOG(ERROR) << "Use: " << Slice(buf, password_size);
}
return password_size;
}

EVP_PKEY *X25519_pem_to_PKEY(Slice pem, Slice password) {
LOG(ERROR) << pem;
LOG(ERROR) << password;
BIO *mem_bio = BIO_new_mem_buf(pem.ubegin(), narrow_cast<int>(pem.size()));
SCOPE_EXIT {
BIO_vfree(mem_bio);
};
//EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u);

return PEM_read_bio_PrivateKey(mem_bio, nullptr, password_cb, &password);
}

} // namespace detail

Result<Ed25519::PrivateKey> Ed25519::generate_private_key() {
Expand Down Expand Up @@ -140,6 +182,27 @@ Result<Ed25519::PublicKey> Ed25519::PrivateKey::get_public_key() const {
return Ed25519::PublicKey(key);
}

Result<string> Ed25519::PrivateKey::as_pem(Slice password) const {
auto pkey = detail::X25519_key_to_PKEY(octet_string_, true);
if (pkey == nullptr) {
return Status::Error("Can't import private key");
}
SCOPE_EXIT {
EVP_PKEY_free(pkey);
};

return detail::X25519_pem_from_PKEY(pkey, true, password);
}

Result<Ed25519::PrivateKey> Ed25519::PrivateKey::from_pem(Slice pem, Slice password) {
auto pkey = detail::X25519_pem_to_PKEY(pem, password);
if (pkey == nullptr) {
return Status::Error("Can't import private key from pem");
}
TRY_RESULT(key, detail::X25519_key_from_PKEY(pkey, true));
return Ed25519::PrivateKey(key);
}

Result<string> Ed25519::PrivateKey::sign(Slice data) const {
auto pkey = detail::X25519_key_to_PKEY(octet_string_, true);
if (pkey == nullptr) {
Expand Down Expand Up @@ -297,6 +360,14 @@ Result<Ed25519::PublicKey> Ed25519::PrivateKey::get_public_key() const {
return PublicKey(Slice(public_key, 32));
}

Result<string> Ed25519::PrivateKey::as_pem(Slice password) const {
return Status::Error("Not supported");
}

Result<Ed25519::PrivateKey> Ed25519::PrivateKey::from_pem(Slice pem, Slice password) {
return Status::Error("Not supported");
}

Result<string> Ed25519::PrivateKey::sign(Slice data) const {
crypto::Ed25519::PrivateKey private_key;
if (!private_key.import_private_key(Slice(octet_string_).ubegin())) {
Expand Down
4 changes: 4 additions & 0 deletions crypto/Ed25519.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class Ed25519 {

Result<string> sign(Slice data) const;

Result<string> as_pem(Slice password) const;

static Result<PrivateKey> from_pem(Slice pem, Slice password);

private:
string octet_string_;
};
Expand Down
13 changes: 13 additions & 0 deletions crypto/block/CreateState.fif
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ dictnew constant special-dict
} : make-catchain-params
{ make-catchain-params 28 config! } : config.catchain_params!

// b [underload soft hard] -- b'
{ untriple 3 roll x{c3} s, 3 roll 32 u, rot 32 u, swap 32 u, } : param_limits,
// bytes-limits gas-limits lt-limits -- c
{ <b x{5d} s, 3 roll param_limits, rot param_limits, swap param_limits, b>
} : make-block-limits
{ make-block-limits 22 config! } : config.mc_block_limits!
{ make-block-limits 23 config! } : config.block_limits!

1000000000 constant Gram
1000000 constant mGram
1000 constant uGram
Expand All @@ -114,6 +122,11 @@ dictnew constant special-dict
1 'nop
} ::_ GR$

{ 10 << } : *Ki
{ 20 << } : *Mi
{ 10 <</r } : */Ki
{ 20 <</r } : */Mi

0 constant recv_internal
-1 constant recv_external
-2 constant run_ticktock
Expand Down
Loading

0 comments on commit 9f2397f

Please sign in to comment.