From 2c6bd673c771202936e5c75d41b57f8de7669d07 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 6 Nov 2018 19:00:03 +0300 Subject: [PATCH] Use as_slice if possible. GitOrigin-RevId: 931b5609e2eae0d5655287e84a71724f07d4090c --- td/mtproto/Transport.cpp | 7 +++---- td/mtproto/crypto.cpp | 2 +- td/telegram/ConfigManager.cpp | 4 ++-- td/telegram/files/FileDownloader.cpp | 6 +++--- tddb/td/db/binlog/Binlog.cpp | 8 ++++---- tdutils/td/utils/crypto.cpp | 3 ++- tdutils/td/utils/tl_storers.h | 4 ++-- tdutils/test/crypto.cpp | 4 ++-- 8 files changed, 19 insertions(+), 19 deletions(-) diff --git a/td/mtproto/Transport.cpp b/td/mtproto/Transport.cpp index 95cce51fa..dfd363123 100644 --- a/td/mtproto/Transport.cpp +++ b/td/mtproto/Transport.cpp @@ -49,11 +49,10 @@ std::tuple Transport::calc_message_key2(const AuthKey &auth_key sha256_final(&state, msg_key_large); // msg_key = substr (msg_key_large, 8, 16); - UInt128 res_raw; - MutableSlice res(res_raw.raw, sizeof(res_raw.raw)); - res.copy_from(msg_key_large.substr(8, 16)); + UInt128 res; + as_slice(res).copy_from(msg_key_large.substr(8, 16)); - return std::make_tuple(as(msg_key_large_raw) | (1u << 31), res_raw); + return std::make_tuple(as(msg_key_large_raw) | (1u << 31), res); } template diff --git a/td/mtproto/crypto.cpp b/td/mtproto/crypto.cpp index cfbab2d08..056712214 100644 --- a/td/mtproto/crypto.cpp +++ b/td/mtproto/crypto.cpp @@ -406,7 +406,7 @@ void tmp_KDF(const UInt128 &server_nonce, const UInt256 &new_nonce, UInt256 *tmp void KDF2(Slice auth_key, const UInt128 &msg_key, int X, UInt256 *aes_key, UInt256 *aes_iv) { uint8 buf_raw[36 + 16]; MutableSlice buf(buf_raw, 36 + 16); - Slice msg_key_slice(msg_key.raw, sizeof(msg_key.raw)); + Slice msg_key_slice = as_slice(msg_key); // sha256_a = SHA256 (msg_key + substr (auth_key, x, 36)); buf.copy_from(msg_key_slice); diff --git a/td/telegram/ConfigManager.cpp b/td/telegram/ConfigManager.cpp index 2cd6751ed..9c821537b 100644 --- a/td/telegram/ConfigManager.cpp +++ b/td/telegram/ConfigManager.cpp @@ -83,8 +83,8 @@ Result decode_config(Slice input) { MutableSlice data_cbc = data_rsa_slice.substr(32); UInt256 key; UInt128 iv; - MutableSlice(key.raw, sizeof(key.raw)).copy_from(data_rsa_slice.substr(0, 32)); - MutableSlice(iv.raw, sizeof(iv.raw)).copy_from(data_rsa_slice.substr(16, 16)); + as_slice(key).copy_from(data_rsa_slice.substr(0, 32)); + as_slice(iv).copy_from(data_rsa_slice.substr(16, 16)); aes_cbc_decrypt(key, &iv, data_cbc, data_cbc); CHECK(data_cbc.size() == 224); diff --git a/td/telegram/files/FileDownloader.cpp b/td/telegram/files/FileDownloader.cpp index e7d7a8530..73c0f5ced 100644 --- a/td/telegram/files/FileDownloader.cpp +++ b/td/telegram/files/FileDownloader.cpp @@ -362,9 +362,9 @@ void FileDownloader::on_progress(int32 part_count, int32 part_size, int32 ready_ } else { LOG(FATAL) << tag("ready_part_count", ready_part_count) << tag("next_part", next_part_); } - callback_->on_partial_download(PartialLocalFileLocation{remote_.file_type_, path_, part_size, ready_part_count, - Slice(iv.raw, sizeof(iv)).str()}, - ready_size); + callback_->on_partial_download( + PartialLocalFileLocation{remote_.file_type_, path_, part_size, ready_part_count, as_slice(iv).str()}, + ready_size); } else { UNREACHABLE(); } diff --git a/tddb/td/db/binlog/Binlog.cpp b/tddb/td/db/binlog/Binlog.cpp index a3a2ed937..dfa53c167 100644 --- a/tddb/td/db/binlog/Binlog.cpp +++ b/tddb/td/db/binlog/Binlog.cpp @@ -340,7 +340,7 @@ void Binlog::do_event(BinlogEvent &&event) { BufferSlice key; if (aes_ctr_key_salt_.as_slice() == encryption_event.key_salt_.as_slice()) { - key = BufferSlice(Slice(aes_ctr_key_.raw, sizeof(aes_ctr_key_.raw))); + key = BufferSlice(as_slice(aes_ctr_key_)); } else if (!db_key_.is_empty()) { key = encryption_event.generate_key(db_key_); } @@ -559,9 +559,9 @@ Status Binlog::load_binlog(const Callback &callback, const Callback &debug_callb } void Binlog::update_encryption(Slice key, Slice iv) { - MutableSlice(aes_ctr_key_.raw, sizeof(aes_ctr_key_.raw)).copy_from(key); + as_slice(aes_ctr_key_).copy_from(key); UInt128 aes_ctr_iv; - MutableSlice(aes_ctr_iv.raw, sizeof(aes_ctr_iv.raw)).copy_from(iv); + as_slice(aes_ctr_iv).copy_from(iv); aes_ctr_state_.init(aes_ctr_key_, aes_ctr_iv); } @@ -585,7 +585,7 @@ void Binlog::reset_encryption() { BufferSlice key; if (aes_ctr_key_salt_.as_slice() == event.key_salt_.as_slice()) { - key = BufferSlice(Slice(aes_ctr_key_.raw, sizeof(aes_ctr_key_.raw))); + key = BufferSlice(as_slice(aes_ctr_key_)); } else { key = event.generate_key(db_key_); } diff --git a/tdutils/td/utils/crypto.cpp b/tdutils/td/utils/crypto.cpp index 9d7917aee..91aa6e541 100644 --- a/tdutils/td/utils/crypto.cpp +++ b/tdutils/td/utils/crypto.cpp @@ -296,10 +296,11 @@ void AesCbcState::decrypt(Slice from, MutableSlice to) { class AesCtrState::Impl { public: Impl(const UInt256 &key, const UInt128 &iv) { + static_assert(AES_BLOCK_SIZE == 16, ""); if (AES_set_encrypt_key(key.raw, 256, &aes_key) < 0) { LOG(FATAL) << "Failed to set encrypt key"; } - MutableSlice(counter, AES_BLOCK_SIZE).copy_from({iv.raw, AES_BLOCK_SIZE}); + MutableSlice(counter, AES_BLOCK_SIZE).copy_from(as_slice(iv)); current_pos = 0; } diff --git a/tdutils/td/utils/tl_storers.h b/tdutils/td/utils/tl_storers.h index b014c7c89..c9caeb877 100644 --- a/tdutils/td/utils/tl_storers.h +++ b/tdutils/td/utils/tl_storers.h @@ -233,13 +233,13 @@ class TlStorerToString { void store_field(const char *name, const UInt128 &value) { store_field_begin(name); - store_binary(Slice(reinterpret_cast(&value), sizeof(value))); + store_binary(as_slice(value)); store_field_end(); } void store_field(const char *name, const UInt256 &value) { store_field_begin(name); - store_binary(Slice(reinterpret_cast(&value), sizeof(value))); + store_binary(as_slice(value)); store_field_end(); } diff --git a/tdutils/test/crypto.cpp b/tdutils/test/crypto.cpp index 5d7128929..c1d116200 100644 --- a/tdutils/test/crypto.cpp +++ b/tdutils/test/crypto.cpp @@ -65,7 +65,7 @@ TEST(Crypto, Sha256State) { for (auto length : {0, 1, 31, 32, 33, 9999, 10000, 10001, 999999, 1000001}) { auto s = td::rand_string(std::numeric_limits::min(), std::numeric_limits::max(), length); td::UInt256 baseline; - td::sha256(s, td::MutableSlice(baseline.raw, 32)); + td::sha256(s, as_slice(baseline)); td::Sha256State state; td::sha256_init(&state); @@ -74,7 +74,7 @@ TEST(Crypto, Sha256State) { td::sha256_update(x, &state); } td::UInt256 result; - td::sha256_final(&state, td::MutableSlice(result.raw, 32)); + td::sha256_final(&state, as_slice(result)); ASSERT_TRUE(baseline == result); } }