From 38e4310b71797844a780c3e4e0a867703463c8d5 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 23 Jul 2019 03:50:03 +0300 Subject: [PATCH] Remove standalone Sha256State functions. GitOrigin-RevId: 5db80ea1902a6fe8a635081a8b050a19528f9f90 --- td/mtproto/Transport.cpp | 2 +- td/telegram/SecureStorage.cpp | 2 +- td/telegram/files/FileHashUploader.cpp | 2 +- tdutils/td/utils/ConcurrentHashTable.h | 2 +- tdutils/td/utils/crypto.cpp | 51 +++++++++++++------------- tdutils/td/utils/crypto.h | 30 ++++++--------- 6 files changed, 41 insertions(+), 48 deletions(-) diff --git a/td/mtproto/Transport.cpp b/td/mtproto/Transport.cpp index a93398de..af96e030 100644 --- a/td/mtproto/Transport.cpp +++ b/td/mtproto/Transport.cpp @@ -150,7 +150,7 @@ std::pair Transport::calc_message_key2(const AuthKey &auth_key, uint8 msg_key_large_raw[32]; MutableSlice msg_key_large(msg_key_large_raw, sizeof(msg_key_large_raw)); - state.extract(msg_key_large); + state.extract(msg_key_large, true); // msg_key = substr (msg_key_large, 8, 16); UInt128 res; diff --git a/td/telegram/SecureStorage.cpp b/td/telegram/SecureStorage.cpp index ce89b960..193ad0c2 100644 --- a/td/telegram/SecureStorage.cpp +++ b/td/telegram/SecureStorage.cpp @@ -300,7 +300,7 @@ Result Decryptor::finish() { } UInt256 res; - sha256_state_.extract(as_slice(res)); + sha256_state_.extract(as_slice(res), true); return ValueHash{res}; } diff --git a/td/telegram/files/FileHashUploader.cpp b/td/telegram/files/FileHashUploader.cpp index 6ad7dce7..1ac80e46 100644 --- a/td/telegram/files/FileHashUploader.cpp +++ b/td/telegram/files/FileHashUploader.cpp @@ -69,7 +69,7 @@ Status FileHashUploader::loop_impl() { if (state_ == State::NetRequest) { // messages.getDocumentByHash#338e2464 sha256:bytes size:int mime_type:string = Document; auto hash = BufferSlice(32); - sha256_state_.extract(hash.as_slice()); + sha256_state_.extract(hash.as_slice(), true); auto mime_type = MimeType::from_extension(PathView(local_.path_).extension(), "image/gif"); auto query = telegram_api::messages_getDocumentByHash(std::move(hash), static_cast(size_), std::move(mime_type)); diff --git a/tdutils/td/utils/ConcurrentHashTable.h b/tdutils/td/utils/ConcurrentHashTable.h index 5b5cf335..96a16ed5 100644 --- a/tdutils/td/utils/ConcurrentHashTable.h +++ b/tdutils/td/utils/ConcurrentHashTable.h @@ -123,7 +123,7 @@ class ConcurrentHashMap { return ValueT{}; } static ValueT migrate_value() { - return (ValueT)(1); // c-style convertion because reinterpret_cast(1) is CE in MSVC + return (ValueT)(1); // c-style conversion because reinterpret_cast(1) is CE in MSVC } ValueT insert(KeyT key, ValueT value) { diff --git a/tdutils/td/utils/crypto.cpp b/tdutils/td/utils/crypto.cpp index 91a3c439..eebb120f 100644 --- a/tdutils/td/utils/crypto.cpp +++ b/tdutils/td/utils/crypto.cpp @@ -377,60 +377,61 @@ string sha512(Slice data) { return result; } -struct Sha256StateImpl { - SHA256_CTX ctx; +class Sha256State::Impl { + public: + SHA256_CTX ctx_; }; Sha256State::Sha256State() = default; Sha256State::Sha256State(Sha256State &&other) { - impl = std::move(other.impl); - is_inited = other.is_inited; - other.is_inited = false; + impl_ = std::move(other.impl_); + is_inited_ = other.is_inited_; + other.is_inited_ = false; } Sha256State &Sha256State::operator=(Sha256State &&other) { Sha256State copy(std::move(other)); using std::swap; - swap(impl, copy.impl); - swap(is_inited, copy.is_inited); + swap(impl_, copy.impl_); + swap(is_inited_, copy.is_inited_); return *this; } Sha256State::~Sha256State() { - if (is_inited) { + if (is_inited_) { char result[32]; extract(MutableSlice{result, 32}); - CHECK(!is_inited); + CHECK(!is_inited_); } } -void sha256_init(Sha256State *state) { - if (!state->impl) { - state->impl = make_unique(); +void Sha256State::init() { + if (!impl_) { + impl_ = make_unique(); } - CHECK(!state->is_inited); - int err = SHA256_Init(&state->impl->ctx); + CHECK(!is_inited_); + int err = SHA256_Init(&impl_->ctx_); LOG_IF(FATAL, err != 1); - state->is_inited = true; + is_inited_ = true; } -void sha256_update(Slice data, Sha256State *state) { - CHECK(state->impl); - CHECK(state->is_inited); - int err = SHA256_Update(&state->impl->ctx, data.ubegin(), data.size()); +void Sha256State::feed(Slice data) { + CHECK(impl_); + CHECK(is_inited_); + int err = SHA256_Update(&impl_->ctx_, data.ubegin(), data.size()); LOG_IF(FATAL, err != 1); } -void sha256_final(Sha256State *state, MutableSlice output, bool destroy) { +void Sha256State::extract(MutableSlice output, bool destroy) { CHECK(output.size() >= 32); - CHECK(state->impl); - CHECK(state->is_inited); - int err = SHA256_Final(output.ubegin(), &state->impl->ctx); + CHECK(impl_); + CHECK(is_inited_); + int err = SHA256_Final(output.ubegin(), &impl_->ctx_); LOG_IF(FATAL, err != 1); - state->is_inited = false; + is_inited_ = false; if (destroy) { - state->impl.reset(); + impl_.reset(); } } diff --git a/tdutils/td/utils/crypto.h b/tdutils/td/utils/crypto.h index f5a4a10d..2a081b4a 100644 --- a/tdutils/td/utils/crypto.h +++ b/tdutils/td/utils/crypto.h @@ -69,14 +69,8 @@ string sha256(Slice data) TD_WARN_UNUSED_RESULT; string sha512(Slice data) TD_WARN_UNUSED_RESULT; -struct Sha256StateImpl; - -struct Sha256State; -void sha256_init(Sha256State *state); -void sha256_update(Slice data, Sha256State *state); -void sha256_final(Sha256State *state, MutableSlice output, bool destroy = true); - -struct Sha256State { +class Sha256State { + public: Sha256State(); Sha256State(const Sha256State &other) = delete; Sha256State &operator=(const Sha256State &other) = delete; @@ -84,18 +78,16 @@ struct Sha256State { Sha256State &operator=(Sha256State &&other); ~Sha256State(); - void init() { - sha256_init(this); - } - void feed(Slice data) { - sha256_update(data, this); - } - void extract(MutableSlice dest) { - sha256_final(this, dest, false); - } + void init(); - unique_ptr impl; - bool is_inited = false; + void feed(Slice data); + + void extract(MutableSlice dest, bool destroy = false); + + private: + class Impl; + unique_ptr impl_; + bool is_inited_ = false; }; void md5(Slice input, MutableSlice output);