Use as_slice<Int> if possible.

GitOrigin-RevId: 931b5609e2eae0d5655287e84a71724f07d4090c
This commit is contained in:
levlam 2018-11-06 19:00:03 +03:00
parent ac9e1da20e
commit 2c6bd673c7
8 changed files with 19 additions and 19 deletions

View File

@ -49,11 +49,10 @@ std::tuple<uint32, UInt128> 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<uint32>(msg_key_large_raw) | (1u << 31), res_raw);
return std::make_tuple(as<uint32>(msg_key_large_raw) | (1u << 31), res);
}
template <class HeaderT>

View File

@ -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);

View File

@ -83,8 +83,8 @@ Result<SimpleConfig> 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);

View File

@ -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();
}

View File

@ -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_);
}

View File

@ -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;
}

View File

@ -233,13 +233,13 @@ class TlStorerToString {
void store_field(const char *name, const UInt128 &value) {
store_field_begin(name);
store_binary(Slice(reinterpret_cast<const unsigned char *>(&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<const unsigned char *>(&value), sizeof(value)));
store_binary(as_slice(value));
store_field_end();
}

View File

@ -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<char>::min(), std::numeric_limits<char>::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);
}
}